1*1b695acdSchristos /* $NetBSD: pam_ftpusers.c,v 1.6 2012/01/03 19:02:55 christos Exp $ */
2e7d22a2eSchristos
36f11bdf1Schristos /*-
46f11bdf1Schristos * Copyright (c) 2001 Networks Associates Technology, Inc.
56f11bdf1Schristos * All rights reserved.
66f11bdf1Schristos *
76f11bdf1Schristos * This software was developed for the FreeBSD Project by ThinkSec AS and
86f11bdf1Schristos * NAI Labs, the Security Research Division of Network Associates, Inc.
96f11bdf1Schristos * under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"), as part of the
106f11bdf1Schristos * DARPA CHATS research program.
116f11bdf1Schristos *
126f11bdf1Schristos * Redistribution and use in source and binary forms, with or without
136f11bdf1Schristos * modification, are permitted provided that the following conditions
146f11bdf1Schristos * are met:
156f11bdf1Schristos * 1. Redistributions of source code must retain the above copyright
166f11bdf1Schristos * notice, this list of conditions and the following disclaimer.
176f11bdf1Schristos * 2. Redistributions in binary form must reproduce the above copyright
186f11bdf1Schristos * notice, this list of conditions and the following disclaimer in the
196f11bdf1Schristos * documentation and/or other materials provided with the distribution.
206f11bdf1Schristos * 3. The name of the author may not be used to endorse or promote
216f11bdf1Schristos * products derived from this software without specific prior written
226f11bdf1Schristos * permission.
236f11bdf1Schristos *
246f11bdf1Schristos * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
256f11bdf1Schristos * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
266f11bdf1Schristos * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
276f11bdf1Schristos * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
286f11bdf1Schristos * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
296f11bdf1Schristos * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
306f11bdf1Schristos * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
316f11bdf1Schristos * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
326f11bdf1Schristos * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
336f11bdf1Schristos * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
346f11bdf1Schristos * SUCH DAMAGE.
356f11bdf1Schristos */
366f11bdf1Schristos
376f11bdf1Schristos #include <sys/cdefs.h>
38e7d22a2eSchristos #ifdef __FreeBSD__
396f11bdf1Schristos __FBSDID("$FreeBSD: src/lib/libpam/modules/pam_ftpusers/pam_ftpusers.c,v 1.1 2002/05/08 00:30:10 des Exp $");
40e7d22a2eSchristos #else
41*1b695acdSchristos __RCSID("$NetBSD: pam_ftpusers.c,v 1.6 2012/01/03 19:02:55 christos Exp $");
42e7d22a2eSchristos #endif
436f11bdf1Schristos
446f11bdf1Schristos #include <ctype.h>
456f11bdf1Schristos #include <grp.h>
466f11bdf1Schristos #include <paths.h>
476f11bdf1Schristos #include <pwd.h>
486f11bdf1Schristos #include <stdio.h>
49*1b695acdSchristos #include <errno.h>
506f11bdf1Schristos #include <stdlib.h>
516f11bdf1Schristos #include <string.h>
526f11bdf1Schristos
536f11bdf1Schristos #define PAM_SM_ACCOUNT
546f11bdf1Schristos
556f11bdf1Schristos #include <security/pam_appl.h>
566f11bdf1Schristos #include <security/pam_modules.h>
576f11bdf1Schristos #include <security/pam_mod_misc.h>
586f11bdf1Schristos #include <security/openpam.h>
596f11bdf1Schristos
606f11bdf1Schristos PAM_EXTERN int
pam_sm_acct_mgmt(pam_handle_t * pamh,int flags __unused,int argc __unused,const char * argv[]__unused)616f11bdf1Schristos pam_sm_acct_mgmt(pam_handle_t *pamh, int flags __unused,
626f11bdf1Schristos int argc __unused, const char *argv[] __unused)
636f11bdf1Schristos {
6459cbc9e2Sthorpej struct passwd *pwd, pwres;
65b4073cddSchristos struct group *grp, grres;
666f11bdf1Schristos const char *user;
676f11bdf1Schristos int pam_err, found, allow;
686f11bdf1Schristos char *line, *name, **mem;
696f11bdf1Schristos size_t len, ulen;
706f11bdf1Schristos FILE *f;
71b4073cddSchristos char pwbuf[1024], grbuf[1024];
726f11bdf1Schristos
736f11bdf1Schristos pam_err = pam_get_user(pamh, &user, NULL);
746f11bdf1Schristos if (pam_err != PAM_SUCCESS)
756f11bdf1Schristos return (pam_err);
7659cbc9e2Sthorpej if (user == NULL ||
772a62e4e1Schristos getpwnam_r(user, &pwres, pwbuf, sizeof(pwbuf), &pwd) != 0 ||
782a62e4e1Schristos pwd == NULL)
796f11bdf1Schristos return (PAM_SERVICE_ERR);
806f11bdf1Schristos
816f11bdf1Schristos found = 0;
826f11bdf1Schristos ulen = strlen(user);
836f11bdf1Schristos if ((f = fopen(_PATH_FTPUSERS, "r")) == NULL) {
84*1b695acdSchristos PAM_LOG("%s: %s", _PATH_FTPUSERS, strerror(errno));
856f11bdf1Schristos goto done;
866f11bdf1Schristos }
876f11bdf1Schristos while (!found && (line = fgetln(f, &len)) != NULL) {
886f11bdf1Schristos if (*line == '#')
896f11bdf1Schristos continue;
90e7d22a2eSchristos while (len > 0 && isspace((unsigned char)line[len - 1]))
916f11bdf1Schristos --len;
926f11bdf1Schristos if (len == 0)
936f11bdf1Schristos continue;
946f11bdf1Schristos /* simple case first */
956f11bdf1Schristos if (*line != '@') {
966f11bdf1Schristos if (len == ulen && strncmp(user, line, len) == 0)
976f11bdf1Schristos found = 1;
986f11bdf1Schristos continue;
996f11bdf1Schristos }
1006f11bdf1Schristos /* member of specified group? */
1016f11bdf1Schristos asprintf(&name, "%.*s", (int)len - 1, line + 1);
1026f11bdf1Schristos if (name == NULL) {
1036f11bdf1Schristos fclose(f);
1046f11bdf1Schristos return (PAM_BUF_ERR);
1056f11bdf1Schristos }
106b4073cddSchristos (void)getgrnam_r(name, &grres, grbuf, sizeof(grbuf), &grp);
1076f11bdf1Schristos free(name);
1086f11bdf1Schristos if (grp == NULL)
1096f11bdf1Schristos continue;
1106f11bdf1Schristos for (mem = grp->gr_mem; mem && *mem && !found; ++mem)
1116f11bdf1Schristos if (strcmp(user, *mem) == 0)
1126f11bdf1Schristos found = 1;
1136f11bdf1Schristos }
1146f11bdf1Schristos done:
1156f11bdf1Schristos allow = (openpam_get_option(pamh, "disallow") == NULL);
1166f11bdf1Schristos if (found)
1176f11bdf1Schristos pam_err = allow ? PAM_SUCCESS : PAM_AUTH_ERR;
1186f11bdf1Schristos else
1196f11bdf1Schristos pam_err = allow ? PAM_AUTH_ERR : PAM_SUCCESS;
1206f11bdf1Schristos if (f != NULL)
1216f11bdf1Schristos fclose(f);
1226f11bdf1Schristos return (pam_err);
1236f11bdf1Schristos }
1246f11bdf1Schristos
1256f11bdf1Schristos PAM_MODULE_ENTRY("pam_ftpusers");
126