xref: /netbsd-src/lib/libpam/modules/pam_guest/pam_guest.c (revision e7d22a2e645325144a9d854bb08f879ae427542a)
1*e7d22a2eSchristos /*	$NetBSD: pam_guest.c,v 1.2 2004/12/12 08:18:45 christos Exp $	*/
2*e7d22a2eSchristos 
36f11bdf1Schristos /*-
46f11bdf1Schristos  * Copyright (c) 2003 Networks Associates Technology, Inc.
56f11bdf1Schristos  * All rights reserved.
66f11bdf1Schristos  *
76f11bdf1Schristos  * Portions of this software were developed for the FreeBSD Project by
86f11bdf1Schristos  * ThinkSec AS and NAI Labs, the Security Research Division of Network
96f11bdf1Schristos  * Associates, Inc.  under DARPA/SPAWAR contract N66001-01-C-8035
106f11bdf1Schristos  * ("CBOSS"), as part of the 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>
38*e7d22a2eSchristos #ifdef __FreeBSD__
396f11bdf1Schristos __FBSDID("$FreeBSD: src/lib/libpam/modules/pam_guest/pam_guest.c,v 1.1 2003/05/31 16:52:58 des Exp $");
40*e7d22a2eSchristos #else
41*e7d22a2eSchristos __RCSID("$NetBSD: pam_guest.c,v 1.2 2004/12/12 08:18:45 christos Exp $");
42*e7d22a2eSchristos #endif
436f11bdf1Schristos 
446f11bdf1Schristos #include <string.h>
456f11bdf1Schristos 
466f11bdf1Schristos #define PAM_SM_AUTH
476f11bdf1Schristos 
486f11bdf1Schristos #include <security/pam_appl.h>
496f11bdf1Schristos #include <security/pam_modules.h>
506f11bdf1Schristos #include <security/openpam.h>
516f11bdf1Schristos 
526f11bdf1Schristos #define DEFAULT_GUESTS	"guest"
536f11bdf1Schristos 
546f11bdf1Schristos static int
lookup(const char * str,const char * list)556f11bdf1Schristos lookup(const char *str, const char *list)
566f11bdf1Schristos {
576f11bdf1Schristos 	const char *next;
586f11bdf1Schristos 	size_t len;
596f11bdf1Schristos 
606f11bdf1Schristos 	len = strlen(str);
616f11bdf1Schristos 	while (*list != '\0') {
626f11bdf1Schristos 		while (*list == ',')
636f11bdf1Schristos 			++list;
646f11bdf1Schristos 		if ((next = strchr(list, ',')) == NULL)
656f11bdf1Schristos 			next = strchr(list, '\0');
666f11bdf1Schristos 		if (next - list == (ptrdiff_t)len &&
676f11bdf1Schristos 		    strncmp(list, str, len) == 0)
686f11bdf1Schristos 			return (1);
696f11bdf1Schristos 		list = next;
706f11bdf1Schristos 	}
716f11bdf1Schristos 	return (0);
726f11bdf1Schristos }
736f11bdf1Schristos 
746f11bdf1Schristos PAM_EXTERN int
pam_sm_authenticate(pam_handle_t * pamh,int flags __unused,int argc __unused,const char * argv[]__unused)756f11bdf1Schristos pam_sm_authenticate(pam_handle_t *pamh, int flags __unused,
766f11bdf1Schristos     int argc __unused, const char *argv[] __unused)
776f11bdf1Schristos {
786f11bdf1Schristos 	const char *authtok, *guests, *user;
796f11bdf1Schristos 	int err, is_guest;
806f11bdf1Schristos 
816f11bdf1Schristos 	/* get target account */
826f11bdf1Schristos 	if (pam_get_user(pamh, &user, NULL) != PAM_SUCCESS || user == NULL)
836f11bdf1Schristos 		return (PAM_AUTH_ERR);
846f11bdf1Schristos 
856f11bdf1Schristos 	/* get list of guest logins */
866f11bdf1Schristos 	if ((guests = openpam_get_option(pamh, "guests")) == NULL)
876f11bdf1Schristos 		guests = DEFAULT_GUESTS;
886f11bdf1Schristos 
896f11bdf1Schristos 	/* check if the target account is on the list */
906f11bdf1Schristos 	is_guest = lookup(user, guests);
916f11bdf1Schristos 
926f11bdf1Schristos 	/* check password */
936f11bdf1Schristos 	if (!openpam_get_option(pamh, "nopass")) {
946f11bdf1Schristos 		err = pam_get_authtok(pamh, PAM_AUTHTOK, &authtok, NULL);
956f11bdf1Schristos 		if (err != PAM_SUCCESS)
966f11bdf1Schristos 			return (err);
976f11bdf1Schristos 		if (openpam_get_option(pamh, "pass_is_user") &&
986f11bdf1Schristos 		    strcmp(user, authtok) != 0)
996f11bdf1Schristos 			return (PAM_AUTH_ERR);
1006f11bdf1Schristos 		if (openpam_get_option(pamh, "pass_as_ruser"))
1016f11bdf1Schristos 			pam_set_item(pamh, PAM_RUSER, authtok);
1026f11bdf1Schristos 	}
1036f11bdf1Schristos 
1046f11bdf1Schristos 	/* done */
1056f11bdf1Schristos 	if (is_guest) {
1066f11bdf1Schristos 		pam_setenv(pamh, "GUEST", user, 1);
1076f11bdf1Schristos 		return (PAM_SUCCESS);
1086f11bdf1Schristos 	}
1096f11bdf1Schristos 	return (PAM_AUTH_ERR);
1106f11bdf1Schristos }
1116f11bdf1Schristos 
1126f11bdf1Schristos PAM_EXTERN int
pam_sm_setcred(pam_handle_t * pamh __unused,int flags __unused,int argc __unused,const char * argv[]__unused)1136f11bdf1Schristos pam_sm_setcred(pam_handle_t * pamh __unused, int flags __unused,
1146f11bdf1Schristos     int argc __unused, const char *argv[] __unused)
1156f11bdf1Schristos {
1166f11bdf1Schristos 
1176f11bdf1Schristos 	return (PAM_SUCCESS);
1186f11bdf1Schristos }
1196f11bdf1Schristos 
1206f11bdf1Schristos PAM_MODULE_ENTRY("pam_guest");
121