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
50Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
60Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
70Sstevel@tonic-gate  * with the License.
80Sstevel@tonic-gate  *
90Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
100Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
110Sstevel@tonic-gate  * See the License for the specific language governing permissions
120Sstevel@tonic-gate  * and limitations under the License.
130Sstevel@tonic-gate  *
140Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
150Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
160Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
170Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
180Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
190Sstevel@tonic-gate  *
200Sstevel@tonic-gate  * CDDL HEADER END
210Sstevel@tonic-gate  */
220Sstevel@tonic-gate /*
23*781Sgtb  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
280Sstevel@tonic-gate 
290Sstevel@tonic-gate #include <security/pam_appl.h>
300Sstevel@tonic-gate #include <pwd.h>
310Sstevel@tonic-gate #include <string.h>
320Sstevel@tonic-gate #include <stdlib.h>
330Sstevel@tonic-gate #include <malloc.h>
340Sstevel@tonic-gate #include <unistd.h>
350Sstevel@tonic-gate #include <ctype.h>
360Sstevel@tonic-gate #include <syslog.h>
37*781Sgtb #include <errno.h>
380Sstevel@tonic-gate 
390Sstevel@tonic-gate #include "utils.h"
400Sstevel@tonic-gate 
410Sstevel@tonic-gate extern const char *error_message(long);
420Sstevel@tonic-gate 
430Sstevel@tonic-gate /* ******************************************************************** */
440Sstevel@tonic-gate /*									*/
450Sstevel@tonic-gate /* 		Utilities Functions					*/
460Sstevel@tonic-gate /*									*/
470Sstevel@tonic-gate /* ******************************************************************** */
480Sstevel@tonic-gate 
490Sstevel@tonic-gate /*
500Sstevel@tonic-gate  * get_pw_uid():
510Sstevel@tonic-gate  *	To get the uid from the passwd entry for specified user
520Sstevel@tonic-gate  *	It returns 0 if the user can't be found, otherwise returns 1.
530Sstevel@tonic-gate  */
540Sstevel@tonic-gate int
550Sstevel@tonic-gate get_pw_uid(char *user, uid_t *uid)
560Sstevel@tonic-gate {
570Sstevel@tonic-gate 	struct passwd sp;
580Sstevel@tonic-gate 	char buffer[1024];
590Sstevel@tonic-gate 
600Sstevel@tonic-gate 	if (getpwnam_r(user, &sp, buffer, sizeof (buffer)) == NULL) {
610Sstevel@tonic-gate 		return (0);
620Sstevel@tonic-gate 	}
630Sstevel@tonic-gate 
640Sstevel@tonic-gate 	*uid = sp.pw_uid;
650Sstevel@tonic-gate 
660Sstevel@tonic-gate 	return (1);
670Sstevel@tonic-gate }
680Sstevel@tonic-gate 
690Sstevel@tonic-gate /*
700Sstevel@tonic-gate  * get_pw_gid():
710Sstevel@tonic-gate  *	To get the gid from the passwd entry for specified user
720Sstevel@tonic-gate  *	It returns 0 if the user can't be found, otherwise returns 1.
730Sstevel@tonic-gate  */
740Sstevel@tonic-gate int
750Sstevel@tonic-gate get_pw_gid(char *user, gid_t *gid)
760Sstevel@tonic-gate {
770Sstevel@tonic-gate 	struct passwd sp;
780Sstevel@tonic-gate 	char buffer[1024];
790Sstevel@tonic-gate 
800Sstevel@tonic-gate 	if (getpwnam_r(user, &sp, buffer, sizeof (buffer)) == NULL) {
810Sstevel@tonic-gate 		return (0);
820Sstevel@tonic-gate 	}
830Sstevel@tonic-gate 
840Sstevel@tonic-gate 	*gid = sp.pw_gid;
850Sstevel@tonic-gate 
860Sstevel@tonic-gate 	return (1);
870Sstevel@tonic-gate }
880Sstevel@tonic-gate 
890Sstevel@tonic-gate 
900Sstevel@tonic-gate /*
910Sstevel@tonic-gate  * get_kmd_kuser():
920Sstevel@tonic-gate  *	To get the kerberos user name for the specified user.
930Sstevel@tonic-gate  *	Assumes that the kuser string is allocated.  It will be
940Sstevel@tonic-gate  *	overwritten.  This saves us having to deal will allocating
950Sstevel@tonic-gate  *	and freeing the kuser string.
960Sstevel@tonic-gate  *
970Sstevel@tonic-gate  * RFC 1510 does not mention how to handle mixed case domainnames
980Sstevel@tonic-gate  * while constructing client principals. So we will follow the same
990Sstevel@tonic-gate  * procedure as for server principals and lowercase the domainname.
1000Sstevel@tonic-gate  *
1010Sstevel@tonic-gate  * Returns:
1020Sstevel@tonic-gate  *	PAM_AUTH_ERR	- if local host name is not found
1030Sstevel@tonic-gate  *	PAM_BUF_ERR	- if there is an error from krb5_sname_to_principal(),
1040Sstevel@tonic-gate  *			  or krb5_unparse_name()
1050Sstevel@tonic-gate  *	0		- if there was no error
1060Sstevel@tonic-gate  */
1070Sstevel@tonic-gate int
1080Sstevel@tonic-gate get_kmd_kuser(krb5_context kcontext, const char *user, char *kuser, int length)
1090Sstevel@tonic-gate {
1100Sstevel@tonic-gate 	if (strcmp(user, ROOT_UNAME) == 0) {
1110Sstevel@tonic-gate 		krb5_principal princ;
1120Sstevel@tonic-gate 		char *name, *princname, *lasts;
1130Sstevel@tonic-gate 
1140Sstevel@tonic-gate 		if (krb5_sname_to_principal(kcontext, NULL, ROOT_UNAME,
1150Sstevel@tonic-gate 			KRB5_NT_SRV_HST, &princ)) {
1160Sstevel@tonic-gate 			return (PAM_BUF_ERR);
1170Sstevel@tonic-gate 		}
1180Sstevel@tonic-gate 		if (krb5_unparse_name(kcontext, princ, &princname)) {
1190Sstevel@tonic-gate 			krb5_free_principal(kcontext, princ);
1200Sstevel@tonic-gate 			return (PAM_BUF_ERR);
1210Sstevel@tonic-gate 		}
1220Sstevel@tonic-gate 		/* just interested in princ name before the @REALM part */
1230Sstevel@tonic-gate 		if ((name = strtok_r(princname, "@", &lasts)) == NULL) {
1240Sstevel@tonic-gate 			krb5_free_principal(kcontext, princ);
1250Sstevel@tonic-gate 			free(princname);
1260Sstevel@tonic-gate 			return (PAM_BUF_ERR);
1270Sstevel@tonic-gate 		}
1280Sstevel@tonic-gate 		if (strlcpy(kuser, name, length) >= length) {
1290Sstevel@tonic-gate 			krb5_free_principal(kcontext, princ);
1300Sstevel@tonic-gate 			free(princname);
1310Sstevel@tonic-gate 			return (PAM_BUF_ERR);
1320Sstevel@tonic-gate 		}
1330Sstevel@tonic-gate 		krb5_free_principal(kcontext, princ);
1340Sstevel@tonic-gate 		free(princname);
1350Sstevel@tonic-gate 	} else {
1360Sstevel@tonic-gate 		if (strlcpy(kuser, user, length) >= length) {
1370Sstevel@tonic-gate 			return (PAM_BUF_ERR);
1380Sstevel@tonic-gate 		}
1390Sstevel@tonic-gate 	}
1400Sstevel@tonic-gate 	return (0);
1410Sstevel@tonic-gate }
1420Sstevel@tonic-gate 
1430Sstevel@tonic-gate /*
1440Sstevel@tonic-gate  * return true (1) if the user's key is in the (default) keytab
1450Sstevel@tonic-gate  */
1460Sstevel@tonic-gate int
1470Sstevel@tonic-gate key_in_keytab(const char *user, int debug)
1480Sstevel@tonic-gate {
1490Sstevel@tonic-gate 	krb5_keytab kt_handle;
1500Sstevel@tonic-gate 	krb5_keytab_entry kt_ent;
1510Sstevel@tonic-gate 	char *whoami = "key_in_keytab";
1520Sstevel@tonic-gate 	krb5_error_code retval = 0;
1530Sstevel@tonic-gate 	krb5_error_code code = 0;
1540Sstevel@tonic-gate 	krb5_context kcontext = NULL;
1550Sstevel@tonic-gate 	krb5_principal	princ = NULL;
1560Sstevel@tonic-gate 	char		kuser[2*MAXHOSTNAMELEN];
1570Sstevel@tonic-gate 
1580Sstevel@tonic-gate 
1590Sstevel@tonic-gate 	if (debug)
1600Sstevel@tonic-gate 		syslog(LOG_DEBUG,
1610Sstevel@tonic-gate 		    "PAM-KRB5 (%s): start for user '%s'",
1620Sstevel@tonic-gate 				    whoami, user ? user : "<null>");
1630Sstevel@tonic-gate 
1640Sstevel@tonic-gate 	if (!user)
1650Sstevel@tonic-gate 		return (retval);
1660Sstevel@tonic-gate 
1670Sstevel@tonic-gate 	/* need to free context with krb5_free_context */
1680Sstevel@tonic-gate 	if (code = krb5_init_context(&kcontext)) {
1690Sstevel@tonic-gate 		if (debug)
1700Sstevel@tonic-gate 			syslog(LOG_DEBUG,
1710Sstevel@tonic-gate 			    "PAM-KRB5 (%s): Error initializing "
1720Sstevel@tonic-gate 			    "krb5: %s", whoami,
1730Sstevel@tonic-gate 			    error_message(code));
1740Sstevel@tonic-gate 		return (retval);
1750Sstevel@tonic-gate 	}
1760Sstevel@tonic-gate 
1770Sstevel@tonic-gate 	if ((code = get_kmd_kuser(kcontext, (const char *)user, kuser,
1780Sstevel@tonic-gate 		2*MAXHOSTNAMELEN)) != 0) {
1790Sstevel@tonic-gate 		goto out;
1800Sstevel@tonic-gate 	}
1810Sstevel@tonic-gate 
1820Sstevel@tonic-gate 	/* need to free princ with krb5_free_principal */
1830Sstevel@tonic-gate 	if ((code = krb5_parse_name(kcontext, kuser, &princ)) != 0) {
1840Sstevel@tonic-gate 		if (debug)
1850Sstevel@tonic-gate 			syslog(LOG_DEBUG,
1860Sstevel@tonic-gate 			    "PAM-KRB5 (%s): can't parse name (%s)",
1870Sstevel@tonic-gate 				    whoami, error_message(code));
1880Sstevel@tonic-gate 		goto out;
1890Sstevel@tonic-gate 	}
1900Sstevel@tonic-gate 
1910Sstevel@tonic-gate 	/* need to close keytab handle with krb5_kt_close */
1920Sstevel@tonic-gate 	if ((code = krb5_kt_default(kcontext, &kt_handle))) {
1930Sstevel@tonic-gate 		if (debug)
1940Sstevel@tonic-gate 			syslog(LOG_DEBUG,
1950Sstevel@tonic-gate 			    "PAM-KRB5 (%s): krb5_kt_default failed (%s)",
1960Sstevel@tonic-gate 			    whoami, error_message(code));
1970Sstevel@tonic-gate 		goto out;
1980Sstevel@tonic-gate 	}
1990Sstevel@tonic-gate 
2000Sstevel@tonic-gate 	code = krb5_kt_get_entry(kcontext, kt_handle, princ, 0, 0, &kt_ent);
2010Sstevel@tonic-gate 	if (code != 0) {
2020Sstevel@tonic-gate 		if (code == ENOENT) {
2030Sstevel@tonic-gate 				if (debug)
2040Sstevel@tonic-gate 					syslog(LOG_DEBUG,
2050Sstevel@tonic-gate 					    "PAM-KRB5 (%s): "
2060Sstevel@tonic-gate 					    "Keytab does not exist",
2070Sstevel@tonic-gate 					    whoami);
2080Sstevel@tonic-gate 		} else if (code == KRB5_KT_NOTFOUND) {
2090Sstevel@tonic-gate 				if (debug)
2100Sstevel@tonic-gate 					syslog(LOG_DEBUG,
2110Sstevel@tonic-gate 					    "PAM-KRB5 (%s): "
2120Sstevel@tonic-gate 					    "No entry for principal "
2130Sstevel@tonic-gate 					    "'%s' exists in keytab",
2140Sstevel@tonic-gate 					    whoami, kuser);
2150Sstevel@tonic-gate 		} else {
2160Sstevel@tonic-gate 				if (debug)
2170Sstevel@tonic-gate 					syslog(LOG_DEBUG,
2180Sstevel@tonic-gate 					    "PAM-KRB5 (%s): "
2190Sstevel@tonic-gate 					    "krb5_kt_get_entry failed (%s)",
2200Sstevel@tonic-gate 					    whoami, error_message(code));
2210Sstevel@tonic-gate 		}
2220Sstevel@tonic-gate 	} else { /* Key found in keytab, return success */
2230Sstevel@tonic-gate 			(void) krb5_kt_free_entry(kcontext, &kt_ent);
2240Sstevel@tonic-gate 			if (debug)
2250Sstevel@tonic-gate 				syslog(LOG_DEBUG,
2260Sstevel@tonic-gate 				    "PAM-KRB5 (%s): "
2270Sstevel@tonic-gate 				    "keytab entry for '%s' found",
2280Sstevel@tonic-gate 				    whoami, user);
2290Sstevel@tonic-gate 			retval = 1;
2300Sstevel@tonic-gate 	}
2310Sstevel@tonic-gate 
2320Sstevel@tonic-gate 	(void) krb5_kt_close(kcontext, kt_handle);
2330Sstevel@tonic-gate out:
2340Sstevel@tonic-gate 	if (princ && kcontext)
2350Sstevel@tonic-gate 		krb5_free_principal(kcontext, princ);
2360Sstevel@tonic-gate 
2370Sstevel@tonic-gate 	if (kcontext)
2380Sstevel@tonic-gate 		krb5_free_context(kcontext);
2390Sstevel@tonic-gate 
2400Sstevel@tonic-gate 	return (retval);
2410Sstevel@tonic-gate }
242