xref: /onnv-gate/usr/src/lib/pam_modules/sample/sample_acct_mgmt.c (revision 9694:78fafb281255)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*9694SScott.Rotondo@Sun.COM  * Common Development and Distribution License (the "License").
6*9694SScott.Rotondo@Sun.COM  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
22*9694SScott.Rotondo@Sun.COM  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate #include <syslog.h>
270Sstevel@tonic-gate #include <pwd.h>
280Sstevel@tonic-gate #include <unistd.h>
290Sstevel@tonic-gate #include <strings.h>
300Sstevel@tonic-gate #include <security/pam_appl.h>
310Sstevel@tonic-gate #include <security/pam_modules.h>
320Sstevel@tonic-gate #include <libintl.h>
330Sstevel@tonic-gate 
340Sstevel@tonic-gate static int parse_allow_name(char *, char *);
350Sstevel@tonic-gate 
360Sstevel@tonic-gate /*
370Sstevel@tonic-gate  * pam_sm_acct_mgmt	main account managment routine.
380Sstevel@tonic-gate  *			XXX: The routine just prints out a warning message.
390Sstevel@tonic-gate  *			     It may need to force the user to change his/her
400Sstevel@tonic-gate  *			     passwd.
410Sstevel@tonic-gate  */
420Sstevel@tonic-gate 
430Sstevel@tonic-gate int
pam_sm_acct_mgmt(pam_handle_t * pamh,int flags,int argc,const char ** argv)440Sstevel@tonic-gate pam_sm_acct_mgmt(
450Sstevel@tonic-gate 	pam_handle_t *pamh,
460Sstevel@tonic-gate 	int	flags,
470Sstevel@tonic-gate 	int	argc,
480Sstevel@tonic-gate 	const char **argv)
490Sstevel@tonic-gate {
500Sstevel@tonic-gate 	char	*user;
510Sstevel@tonic-gate 	char	*pg;
520Sstevel@tonic-gate 	int	i;
530Sstevel@tonic-gate 	/*LINTED - set but not used. Would be used in a real module. */
540Sstevel@tonic-gate 	int	debug = 0;
550Sstevel@tonic-gate 	/*LINTED - set but not used. Would be used in a real module. */
560Sstevel@tonic-gate 	int	nowarn = 0;
570Sstevel@tonic-gate 	int	error = 0;
580Sstevel@tonic-gate 
590Sstevel@tonic-gate 	if (argc == 0)
600Sstevel@tonic-gate 		return (PAM_SUCCESS);
610Sstevel@tonic-gate 
620Sstevel@tonic-gate 	if (pam_get_item(pamh, PAM_USER, (void **)&user) != PAM_SUCCESS)
630Sstevel@tonic-gate 		return (PAM_SERVICE_ERR);
640Sstevel@tonic-gate 
650Sstevel@tonic-gate 	if (pam_get_item(pamh, PAM_SERVICE, (void **)&pg) != PAM_SUCCESS)
660Sstevel@tonic-gate 		return (PAM_SERVICE_ERR);
670Sstevel@tonic-gate 
680Sstevel@tonic-gate 	/*
690Sstevel@tonic-gate 	 * kludge alert. su needs to be handled specially for allow policy.
700Sstevel@tonic-gate 	 * we want to use the policy of the current user not the "destination"
710Sstevel@tonic-gate 	 * user. This will enable us to prevent su to root but not to rlogin,
720Sstevel@tonic-gate 	 * telnet, rsh, ftp to root.
730Sstevel@tonic-gate 	 *
740Sstevel@tonic-gate 	 * description of problem: user name is the "destination" name. not
750Sstevel@tonic-gate 	 * the current name. The allow policy needs to be applied to the
760Sstevel@tonic-gate 	 * current name in the case of su. user is "root" in this case and
770Sstevel@tonic-gate 	 * we will be getting the root policy instead of the user policy.
780Sstevel@tonic-gate 	 */
790Sstevel@tonic-gate 	if (strcmp(pg, "su") == 0) {
800Sstevel@tonic-gate 		struct passwd *pw;
810Sstevel@tonic-gate 		uid_t uid;
820Sstevel@tonic-gate 		uid = getuid();
830Sstevel@tonic-gate 		pw = getpwuid(uid);
840Sstevel@tonic-gate 		if (pw == NULL)
850Sstevel@tonic-gate 			return (PAM_SYSTEM_ERR);
860Sstevel@tonic-gate 		user = pw->pw_name;
870Sstevel@tonic-gate 	}
880Sstevel@tonic-gate 
890Sstevel@tonic-gate 	if (user == 0 || *user == '\0' || (strcmp(user, "root") == 0))
900Sstevel@tonic-gate 		return (PAM_SUCCESS);
910Sstevel@tonic-gate 
920Sstevel@tonic-gate 	for (i = 0; i < argc; i++) {
930Sstevel@tonic-gate 		if (strcasecmp(argv[i], "debug") == 0)
940Sstevel@tonic-gate 			debug = 1;
950Sstevel@tonic-gate 		else if (strcasecmp(argv[i], "nowarn") == 0) {
960Sstevel@tonic-gate 			nowarn = 1;
970Sstevel@tonic-gate 			flags = flags | PAM_SILENT;
980Sstevel@tonic-gate 		} else if (strncmp(argv[i], "allow=", 6) == 0)
990Sstevel@tonic-gate 			error |= parse_allow_name(user, (char *)(argv[i]+6));
1000Sstevel@tonic-gate 		else
1010Sstevel@tonic-gate 			syslog(LOG_DEBUG, "illegal option %s", argv[i]);
1020Sstevel@tonic-gate 	}
1030Sstevel@tonic-gate 	return (error?PAM_SUCCESS:PAM_AUTH_ERR);
1040Sstevel@tonic-gate }
1050Sstevel@tonic-gate 
106*9694SScott.Rotondo@Sun.COM static char *getname();
107*9694SScott.Rotondo@Sun.COM 
1080Sstevel@tonic-gate static int
parse_allow_name(char * who,char * cp)1090Sstevel@tonic-gate parse_allow_name(char *who, char *cp)
1100Sstevel@tonic-gate {
1110Sstevel@tonic-gate 	char name[256];
1120Sstevel@tonic-gate 
1130Sstevel@tonic-gate 	/* catch "allow=" */
1140Sstevel@tonic-gate 	if (*cp == '\0')
1150Sstevel@tonic-gate 		return (0);
1160Sstevel@tonic-gate 	while (cp) {
1170Sstevel@tonic-gate 		cp = getname(cp, name);
1180Sstevel@tonic-gate 		/* catch things such as =, and ,, */
1190Sstevel@tonic-gate 		if (*name == '\0')
1200Sstevel@tonic-gate 			continue;
1210Sstevel@tonic-gate 		if (strcmp(who, name) == 0)
1220Sstevel@tonic-gate 			return (1);
1230Sstevel@tonic-gate 	}
1240Sstevel@tonic-gate 	return (0);
1250Sstevel@tonic-gate }
1260Sstevel@tonic-gate 
1270Sstevel@tonic-gate static char *
getname(char * cp,char * name)1280Sstevel@tonic-gate getname(char *cp, char *name)
1290Sstevel@tonic-gate {
1300Sstevel@tonic-gate 	/* force name to be initially null string */
1310Sstevel@tonic-gate 	*name = '\0';
1320Sstevel@tonic-gate 
1330Sstevel@tonic-gate 	/* end of string? */
1340Sstevel@tonic-gate 	if (*cp == '\0')
1350Sstevel@tonic-gate 		return ((char *)0);
1360Sstevel@tonic-gate 	while (*cp) {
1370Sstevel@tonic-gate 		/* end of name? */
1380Sstevel@tonic-gate 		if (*cp == ',' || *cp == '\0')
1390Sstevel@tonic-gate 			break;
1400Sstevel@tonic-gate 		*name++ = *cp++;
1410Sstevel@tonic-gate 	}
1420Sstevel@tonic-gate 	/* make name into string */
1430Sstevel@tonic-gate 	*name++ = '\0';
1440Sstevel@tonic-gate 	return ((*cp == '\0')? (char *)0 : ++cp);
1450Sstevel@tonic-gate }
146