xref: /dflybsd-src/lib/libpam/modules/pam_ftpusers/pam_ftpusers.c (revision c98db40744766ab0803912f29557df02814bcd9d)
1242be47eSzrj /*-
2*c98db407SSascha Wildner  * SPDX-License-Identifier: BSD-3-Clause
3*c98db407SSascha Wildner  *
4242be47eSzrj  * Copyright (c) 2001 Networks Associates Technology, Inc.
5242be47eSzrj  * All rights reserved.
6242be47eSzrj  *
7242be47eSzrj  * This software was developed for the FreeBSD Project by ThinkSec AS and
8242be47eSzrj  * NAI Labs, the Security Research Division of Network Associates, Inc.
9242be47eSzrj  * under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"), as part of the
10242be47eSzrj  * DARPA CHATS research program.
11242be47eSzrj  *
12242be47eSzrj  * Redistribution and use in source and binary forms, with or without
13242be47eSzrj  * modification, are permitted provided that the following conditions
14242be47eSzrj  * are met:
15242be47eSzrj  * 1. Redistributions of source code must retain the above copyright
16242be47eSzrj  *    notice, this list of conditions and the following disclaimer.
17242be47eSzrj  * 2. Redistributions in binary form must reproduce the above copyright
18242be47eSzrj  *    notice, this list of conditions and the following disclaimer in the
19242be47eSzrj  *    documentation and/or other materials provided with the distribution.
20242be47eSzrj  * 3. The name of the author may not be used to endorse or promote
21242be47eSzrj  *    products derived from this software without specific prior written
22242be47eSzrj  *    permission.
23242be47eSzrj  *
24242be47eSzrj  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
25242be47eSzrj  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26242be47eSzrj  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27242be47eSzrj  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
28242be47eSzrj  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29242be47eSzrj  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30242be47eSzrj  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31242be47eSzrj  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32242be47eSzrj  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33242be47eSzrj  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34242be47eSzrj  * SUCH DAMAGE.
35242be47eSzrj  *
36*c98db407SSascha Wildner  * $FreeBSD: head/lib/libpam/modules/pam_ftpusers/pam_ftpusers.c 326219 2017-11-26 02:00:33Z pfg $
37242be47eSzrj  */
38242be47eSzrj 
39242be47eSzrj #include <ctype.h>
40242be47eSzrj #include <grp.h>
41242be47eSzrj #include <paths.h>
42242be47eSzrj #include <pwd.h>
43242be47eSzrj #include <stdio.h>
44242be47eSzrj #include <stdlib.h>
45242be47eSzrj #include <string.h>
46242be47eSzrj 
47242be47eSzrj #define PAM_SM_ACCOUNT
48242be47eSzrj 
49242be47eSzrj #include <security/pam_appl.h>
50242be47eSzrj #include <security/pam_modules.h>
51242be47eSzrj #include <security/pam_mod_misc.h>
52242be47eSzrj #include <security/openpam.h>
53242be47eSzrj 
54242be47eSzrj PAM_EXTERN int
pam_sm_acct_mgmt(pam_handle_t * pamh,int flags __unused,int argc __unused,const char * argv[]__unused)55242be47eSzrj pam_sm_acct_mgmt(pam_handle_t *pamh, int flags __unused,
56242be47eSzrj     int argc __unused, const char *argv[] __unused)
57242be47eSzrj {
58242be47eSzrj 	struct passwd *pwd;
59242be47eSzrj 	struct group *grp;
60242be47eSzrj 	const char *user;
61242be47eSzrj 	int pam_err, found, allow;
62242be47eSzrj 	char *line, *name, **mem;
63242be47eSzrj 	size_t len, ulen;
64242be47eSzrj 	FILE *f;
65242be47eSzrj 
66242be47eSzrj 	pam_err = pam_get_user(pamh, &user, NULL);
67242be47eSzrj 	if (pam_err != PAM_SUCCESS)
68242be47eSzrj 		return (pam_err);
69242be47eSzrj 	if (user == NULL || (pwd = getpwnam(user)) == NULL)
70242be47eSzrj 		return (PAM_SERVICE_ERR);
71242be47eSzrj 
72242be47eSzrj 	found = 0;
73242be47eSzrj 	ulen = strlen(user);
74242be47eSzrj 	if ((f = fopen(_PATH_FTPUSERS, "r")) == NULL) {
75242be47eSzrj 		PAM_LOG("%s: %m", _PATH_FTPUSERS);
76242be47eSzrj 		goto done;
77242be47eSzrj 	}
78242be47eSzrj 	while (!found && (line = fgetln(f, &len)) != NULL) {
79242be47eSzrj 		if (*line == '#')
80242be47eSzrj 			continue;
81242be47eSzrj 		while (len > 0 && isspace(line[len - 1]))
82242be47eSzrj 			--len;
83242be47eSzrj 		if (len == 0)
84242be47eSzrj 			continue;
85242be47eSzrj 		/* simple case first */
86242be47eSzrj 		if (*line != '@') {
87242be47eSzrj 			if (len == ulen && strncmp(user, line, len) == 0)
88242be47eSzrj 				found = 1;
89242be47eSzrj 			continue;
90242be47eSzrj 		}
91242be47eSzrj 		/* member of specified group? */
92242be47eSzrj 		asprintf(&name, "%.*s", (int)len - 1, line + 1);
93242be47eSzrj 		if (name == NULL) {
94242be47eSzrj 			fclose(f);
95242be47eSzrj 			return (PAM_BUF_ERR);
96242be47eSzrj 		}
97242be47eSzrj 		grp = getgrnam(name);
98242be47eSzrj 		free(name);
99242be47eSzrj 		if (grp == NULL)
100242be47eSzrj 			continue;
101242be47eSzrj 		for (mem = grp->gr_mem; mem && *mem && !found; ++mem)
102242be47eSzrj 			if (strcmp(user, *mem) == 0)
103242be47eSzrj 				found = 1;
104242be47eSzrj 	}
105242be47eSzrj  done:
106242be47eSzrj 	allow = (openpam_get_option(pamh, "disallow") == NULL);
107242be47eSzrj 	if (found)
108242be47eSzrj 		pam_err = allow ? PAM_SUCCESS : PAM_AUTH_ERR;
109242be47eSzrj 	else
110242be47eSzrj 		pam_err = allow ? PAM_AUTH_ERR : PAM_SUCCESS;
111242be47eSzrj 	if (f != NULL)
112242be47eSzrj 		fclose(f);
113242be47eSzrj 	return (pam_err);
114242be47eSzrj }
115242be47eSzrj 
116242be47eSzrj PAM_MODULE_ENTRY("pam_ftpusers");
117