xref: /onnv-gate/usr/src/lib/libpam/pam_framework.c (revision 10702:ca0edf2daf1c)
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
52295Sgww  * Common Development and Distribution License (the "License").
62295Sgww  * 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*8563SKenjiro.Tsuji@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 <dlfcn.h>
280Sstevel@tonic-gate #include <sys/types.h>
290Sstevel@tonic-gate #include <sys/stat.h>
300Sstevel@tonic-gate #include <stdlib.h>
310Sstevel@tonic-gate #include <strings.h>
320Sstevel@tonic-gate #include <malloc.h>
330Sstevel@tonic-gate #include <unistd.h>
340Sstevel@tonic-gate #include <fcntl.h>
350Sstevel@tonic-gate #include <errno.h>
360Sstevel@tonic-gate 
370Sstevel@tonic-gate #include <security/pam_appl.h>
380Sstevel@tonic-gate #include <security/pam_modules.h>
390Sstevel@tonic-gate #include <sys/mman.h>
400Sstevel@tonic-gate 
410Sstevel@tonic-gate #include <libintl.h>
420Sstevel@tonic-gate 
430Sstevel@tonic-gate #include "pam_impl.h"
440Sstevel@tonic-gate 
450Sstevel@tonic-gate static char *pam_snames [PAM_NUM_MODULE_TYPES] = {
460Sstevel@tonic-gate 	PAM_ACCOUNT_NAME,
470Sstevel@tonic-gate 	PAM_AUTH_NAME,
480Sstevel@tonic-gate 	PAM_PASSWORD_NAME,
490Sstevel@tonic-gate 	PAM_SESSION_NAME
500Sstevel@tonic-gate };
510Sstevel@tonic-gate 
520Sstevel@tonic-gate static char *pam_inames [PAM_MAX_ITEMS] = {
530Sstevel@tonic-gate /* NONE */		NULL,
540Sstevel@tonic-gate /* PAM_SERVICE */	"service",
550Sstevel@tonic-gate /* PAM_USER */		"user",
560Sstevel@tonic-gate /* PAM_TTY */		"tty",
570Sstevel@tonic-gate /* PAM_RHOST */ 	"rhost",
580Sstevel@tonic-gate /* PAM_CONV */		"conv",
590Sstevel@tonic-gate /* PAM_AUTHTOK */	"authtok",
600Sstevel@tonic-gate /* PAM_OLDAUTHTOK */	"oldauthtok",
610Sstevel@tonic-gate /* PAM_RUSER */ 	"ruser",
620Sstevel@tonic-gate /* PAM_USER_PROMPT */	"user_prompt",
630Sstevel@tonic-gate /* PAM_REPOSITORY */	"repository",
640Sstevel@tonic-gate /* PAM_RESOURCE */	"resource",
652815Sgww /* PAM_AUSER */ 	"auser",
660Sstevel@tonic-gate /* Undefined Items */
670Sstevel@tonic-gate };
680Sstevel@tonic-gate 
690Sstevel@tonic-gate /*
700Sstevel@tonic-gate  * This extra definition is needed in order to build this library
710Sstevel@tonic-gate  * on pre-64-bit-aware systems.
720Sstevel@tonic-gate  */
730Sstevel@tonic-gate #if !defined(_LFS64_LARGEFILE)
740Sstevel@tonic-gate #define	stat64	stat
750Sstevel@tonic-gate #endif	/* !defined(_LFS64_LARGEFILE) */
760Sstevel@tonic-gate 
770Sstevel@tonic-gate /* functions to dynamically load modules */
780Sstevel@tonic-gate static int	load_modules(pam_handle_t *, int, char *, pamtab_t *);
790Sstevel@tonic-gate static void 	*open_module(pam_handle_t *, char *);
800Sstevel@tonic-gate static int	load_function(void *, char *, int (**func)());
810Sstevel@tonic-gate 
820Sstevel@tonic-gate /* functions to read and store the pam.conf configuration file */
830Sstevel@tonic-gate static int	open_pam_conf(struct pam_fh **, pam_handle_t *, char *);
840Sstevel@tonic-gate static void	close_pam_conf(struct pam_fh *);
850Sstevel@tonic-gate static int	read_pam_conf(pam_handle_t *, char *);
860Sstevel@tonic-gate static int 	get_pam_conf_entry(struct pam_fh *, pam_handle_t *,
870Sstevel@tonic-gate     pamtab_t **);
880Sstevel@tonic-gate static char	*read_next_token(char **);
892295Sgww static char	*nextline(struct pam_fh *, pam_handle_t *, int *);
900Sstevel@tonic-gate static int	verify_pam_conf(pamtab_t *, char *);
910Sstevel@tonic-gate 
920Sstevel@tonic-gate /* functions to clean up and free memory */
930Sstevel@tonic-gate static void	clean_up(pam_handle_t *);
940Sstevel@tonic-gate static void	free_pamconf(pamtab_t *);
950Sstevel@tonic-gate static void	free_pam_conf_info(pam_handle_t *);
960Sstevel@tonic-gate static void	free_env(env_list *);
970Sstevel@tonic-gate 
980Sstevel@tonic-gate /* convenience functions for I18N/L10N communication */
990Sstevel@tonic-gate 
1000Sstevel@tonic-gate static void	free_resp(int, struct pam_response *);
1010Sstevel@tonic-gate static int	do_conv(pam_handle_t *, int, int,
1020Sstevel@tonic-gate     char messages[PAM_MAX_NUM_MSG][PAM_MAX_MSG_SIZE], void *,
1030Sstevel@tonic-gate     struct pam_response **);
1040Sstevel@tonic-gate 
1050Sstevel@tonic-gate static int	log_priority;	/* pam_trace syslog priority & facility */
1060Sstevel@tonic-gate static int	pam_debug = 0;
1070Sstevel@tonic-gate 
1080Sstevel@tonic-gate static char *
pam_trace_iname(int item_type,char * iname_buf)1090Sstevel@tonic-gate pam_trace_iname(int item_type, char *iname_buf)
1100Sstevel@tonic-gate {
1110Sstevel@tonic-gate 	char *name;
1120Sstevel@tonic-gate 
1130Sstevel@tonic-gate 	if (item_type <= 0 ||
1140Sstevel@tonic-gate 	    item_type >= PAM_MAX_ITEMS ||
1150Sstevel@tonic-gate 	    (name = pam_inames[item_type]) == NULL) {
1160Sstevel@tonic-gate 		(void) sprintf(iname_buf, "%d", item_type);
1170Sstevel@tonic-gate 		return (iname_buf);
1180Sstevel@tonic-gate 	}
1190Sstevel@tonic-gate 	return (name);
1200Sstevel@tonic-gate }
1210Sstevel@tonic-gate 
1220Sstevel@tonic-gate static char *
pam_trace_fname(int flag)1230Sstevel@tonic-gate pam_trace_fname(int flag)
1240Sstevel@tonic-gate {
1250Sstevel@tonic-gate 	if (flag & PAM_BINDING)
1260Sstevel@tonic-gate 		return (PAM_BINDING_NAME);
1270Sstevel@tonic-gate 	if (flag & PAM_INCLUDE)
1280Sstevel@tonic-gate 		return (PAM_INCLUDE_NAME);
1290Sstevel@tonic-gate 	if (flag & PAM_OPTIONAL)
1300Sstevel@tonic-gate 		return (PAM_OPTIONAL_NAME);
1310Sstevel@tonic-gate 	if (flag & PAM_REQUIRED)
1320Sstevel@tonic-gate 		return (PAM_REQUIRED_NAME);
1330Sstevel@tonic-gate 	if (flag & PAM_REQUISITE)
1340Sstevel@tonic-gate 		return (PAM_REQUISITE_NAME);
1350Sstevel@tonic-gate 	if (flag & PAM_SUFFICIENT)
1360Sstevel@tonic-gate 		return (PAM_SUFFICIENT_NAME);
1370Sstevel@tonic-gate 	return ("bad flag name");
1380Sstevel@tonic-gate }
1390Sstevel@tonic-gate 
1400Sstevel@tonic-gate static char *
pam_trace_cname(pam_handle_t * pamh)1410Sstevel@tonic-gate pam_trace_cname(pam_handle_t *pamh)
1420Sstevel@tonic-gate {
1430Sstevel@tonic-gate 	if (pamh->pam_conf_name[pamh->include_depth] == NULL)
1440Sstevel@tonic-gate 		return ("NULL");
1450Sstevel@tonic-gate 	return (pamh->pam_conf_name[pamh->include_depth]);
1460Sstevel@tonic-gate }
1470Sstevel@tonic-gate 
1480Sstevel@tonic-gate #include <deflt.h>
1490Sstevel@tonic-gate #include <stdarg.h>
1500Sstevel@tonic-gate /*
1510Sstevel@tonic-gate  * pam_settrace - setup configuration for pam tracing
1520Sstevel@tonic-gate  *
1530Sstevel@tonic-gate  * turn on PAM debug if "magic" file exists
1540Sstevel@tonic-gate  * if exists (original), pam_debug = PAM_DEBUG_DEFAULT,
1550Sstevel@tonic-gate  * log_priority = LOG_DEBUG(7) and log_facility = LOG_AUTH(4).
1560Sstevel@tonic-gate  *
1570Sstevel@tonic-gate  * if has contents, keywork=value pairs:
1580Sstevel@tonic-gate  *
1590Sstevel@tonic-gate  *	"log_priority=" 0-7, the pam_trace syslog priority to use
1600Sstevel@tonic-gate  *		(see sys/syslog.h)
1610Sstevel@tonic-gate  *	"log_facility=" 0-23, the pam_trace syslog facility to use
1620Sstevel@tonic-gate  *		(see sys/syslog.h)
1630Sstevel@tonic-gate  *	"debug_flags=" PAM_DEBUG_DEFAULT (0x0001), log traditional
1640Sstevel@tonic-gate  *			(original) debugging.
1650Sstevel@tonic-gate  *		Plus the logical or of:
1660Sstevel@tonic-gate  *		    PAM_DEBUG_ITEM (0x0002), log item values and
1670Sstevel@tonic-gate  *			pam_get_item.
1680Sstevel@tonic-gate  *		    PAM_DEBUG_MODULE (0x0004), log module return status.
1690Sstevel@tonic-gate  *		    PAM_DEBUG_CONF (0x0008), log pam.conf parsing.
1700Sstevel@tonic-gate  *		    PAM_DEBUG_DATA (0x0010), get/set_data.
1710Sstevel@tonic-gate  *		    PAM_DEBUG_CONV (0x0020), conversation/response.
1720Sstevel@tonic-gate  *
1730Sstevel@tonic-gate  *		    If compiled with DEBUG:
1740Sstevel@tonic-gate  *		    PAM_DEBUG_AUTHTOK (0x8000), display AUTHTOK value if
1750Sstevel@tonic-gate  *				PAM_DEBUG_ITEM is set and results from
1760Sstevel@tonic-gate  *				PAM_PROMPT_ECHO_OFF responses.
1770Sstevel@tonic-gate  *		    USE CAREFULLY, THIS EXPOSES THE USER'S PASSWORDS.
1780Sstevel@tonic-gate  *
1790Sstevel@tonic-gate  *		or set to 0 and off even if PAM_DEBUG file exists.
1800Sstevel@tonic-gate  *
1810Sstevel@tonic-gate  * Output has the general form:
1820Sstevel@tonic-gate  * <whatever was set syslog> PAM[<pid>]: <interface>(<handle> and other info)
1830Sstevel@tonic-gate  * <whatever was set syslog> PAM[<pid>]: details requested for <interface> call
1840Sstevel@tonic-gate  *	Where:	<pid> is the process ID of the calling process.
1850Sstevel@tonic-gate  *		<handle> is the Hex value of the pam_handle associated with the
1860Sstevel@tonic-gate  *			call.
1870Sstevel@tonic-gate  */
1880Sstevel@tonic-gate 
1890Sstevel@tonic-gate static void
pam_settrace()1900Sstevel@tonic-gate pam_settrace()
1910Sstevel@tonic-gate {
192*8563SKenjiro.Tsuji@Sun.COM 	void	*defp;
193*8563SKenjiro.Tsuji@Sun.COM 
194*8563SKenjiro.Tsuji@Sun.COM 	if ((defp = defopen_r(PAM_DEBUG)) != NULL) {
1950Sstevel@tonic-gate 		char	*arg;
1960Sstevel@tonic-gate 		int	code;
1970Sstevel@tonic-gate 		int	facility = LOG_AUTH;
1980Sstevel@tonic-gate 
1990Sstevel@tonic-gate 		pam_debug = PAM_DEBUG_DEFAULT;
2000Sstevel@tonic-gate 		log_priority = LOG_DEBUG;
2010Sstevel@tonic-gate 
202*8563SKenjiro.Tsuji@Sun.COM 		(void) defcntl_r(DC_SETFLAGS, DC_CASE, defp);
203*8563SKenjiro.Tsuji@Sun.COM 		if ((arg = defread_r(LOG_PRIORITY, defp)) != NULL) {
2040Sstevel@tonic-gate 			code = (int)strtol(arg, NULL, 10);
2050Sstevel@tonic-gate 			if ((code & ~LOG_PRIMASK) == 0) {
2060Sstevel@tonic-gate 				log_priority = code;
2070Sstevel@tonic-gate 			}
2080Sstevel@tonic-gate 		}
209*8563SKenjiro.Tsuji@Sun.COM 		if ((arg = defread_r(LOG_FACILITY, defp)) != NULL) {
2100Sstevel@tonic-gate 			code = (int)strtol(arg, NULL, 10);
2110Sstevel@tonic-gate 			if (code < LOG_NFACILITIES) {
2120Sstevel@tonic-gate 				facility = code << 3;
2130Sstevel@tonic-gate 			}
2140Sstevel@tonic-gate 		}
215*8563SKenjiro.Tsuji@Sun.COM 		if ((arg = defread_r(DEBUG_FLAGS, defp)) != NULL) {
2160Sstevel@tonic-gate 			pam_debug = (int)strtol(arg, NULL, 0);
2170Sstevel@tonic-gate 		}
218*8563SKenjiro.Tsuji@Sun.COM 		defclose_r(defp);
2190Sstevel@tonic-gate 
2200Sstevel@tonic-gate 		log_priority |= facility;
2210Sstevel@tonic-gate 	}
2220Sstevel@tonic-gate }
2230Sstevel@tonic-gate 
2240Sstevel@tonic-gate /*
2250Sstevel@tonic-gate  * pam_trace - logs tracing messages
2260Sstevel@tonic-gate  *
2270Sstevel@tonic-gate  *	flag = debug_flags from /etc/pam_debug
2280Sstevel@tonic-gate  *	format and args = message to print (PAM[<pid>]: is prepended).
2290Sstevel@tonic-gate  *
2300Sstevel@tonic-gate  *	global log_priority = pam_trace syslog (log_priority | log_facility)
2310Sstevel@tonic-gate  *		from /etc/pam_debug
2320Sstevel@tonic-gate  */
2330Sstevel@tonic-gate /*PRINTFLIKE2*/
2340Sstevel@tonic-gate static void
pam_trace(int flag,char * format,...)2350Sstevel@tonic-gate pam_trace(int flag, char *format, ...)
2360Sstevel@tonic-gate {
2370Sstevel@tonic-gate 	va_list args;
2380Sstevel@tonic-gate 	char message[1024];
2390Sstevel@tonic-gate 	int savemask;
2400Sstevel@tonic-gate 
2410Sstevel@tonic-gate 	if ((pam_debug & flag) == 0)
2420Sstevel@tonic-gate 		return;
2430Sstevel@tonic-gate 
2440Sstevel@tonic-gate 	savemask = setlogmask(LOG_MASK(log_priority & LOG_PRIMASK));
2450Sstevel@tonic-gate 	(void) snprintf(message, sizeof (message), "PAM[%ld]: %s",
2460Sstevel@tonic-gate 	    (long)getpid(), format);
2470Sstevel@tonic-gate 	va_start(args, format);
2480Sstevel@tonic-gate 	(void) vsyslog(log_priority, message, args);
2490Sstevel@tonic-gate 	va_end(args);
2500Sstevel@tonic-gate 	(void) setlogmask(savemask);
2510Sstevel@tonic-gate }
2520Sstevel@tonic-gate 
2530Sstevel@tonic-gate /*
2540Sstevel@tonic-gate  * __pam_log - logs PAM syslog messages
2550Sstevel@tonic-gate  *
2560Sstevel@tonic-gate  *	priority = message priority
2570Sstevel@tonic-gate  *	format and args = message to log
2580Sstevel@tonic-gate  */
2590Sstevel@tonic-gate /*PRINTFLIKE2*/
2600Sstevel@tonic-gate void
__pam_log(int priority,const char * format,...)2610Sstevel@tonic-gate __pam_log(int priority, const char *format, ...)
2620Sstevel@tonic-gate {
2630Sstevel@tonic-gate 	va_list args;
2640Sstevel@tonic-gate 	int savemask = setlogmask(LOG_MASK(priority & LOG_PRIMASK));
2650Sstevel@tonic-gate 
2660Sstevel@tonic-gate 	va_start(args, format);
2670Sstevel@tonic-gate 	(void) vsyslog(priority, format, args);
2680Sstevel@tonic-gate 	va_end(args);
2690Sstevel@tonic-gate 	(void) setlogmask(savemask);
2700Sstevel@tonic-gate }
2710Sstevel@tonic-gate 
2720Sstevel@tonic-gate 
2730Sstevel@tonic-gate /*
2740Sstevel@tonic-gate  *			pam_XXXXX routines
2750Sstevel@tonic-gate  *
2760Sstevel@tonic-gate  *	These are the entry points to the authentication switch
2770Sstevel@tonic-gate  */
2780Sstevel@tonic-gate 
2790Sstevel@tonic-gate /*
2800Sstevel@tonic-gate  * pam_start		- initiate an authentication transaction and
2810Sstevel@tonic-gate  *			  set parameter values to be used during the
2820Sstevel@tonic-gate  *			  transaction
2830Sstevel@tonic-gate  */
2840Sstevel@tonic-gate 
2850Sstevel@tonic-gate int
pam_start(const char * service,const char * user,const struct pam_conv * pam_conv,pam_handle_t ** pamh)2860Sstevel@tonic-gate pam_start(const char *service, const char *user,
2870Sstevel@tonic-gate     const struct pam_conv *pam_conv, pam_handle_t **pamh)
2880Sstevel@tonic-gate {
2890Sstevel@tonic-gate 	int	err;
2900Sstevel@tonic-gate 
2914758Sgww 	*pamh = calloc(1, sizeof (struct pam_handle));
2920Sstevel@tonic-gate 
2930Sstevel@tonic-gate 	pam_settrace();
2940Sstevel@tonic-gate 	pam_trace(PAM_DEBUG_DEFAULT,
2950Sstevel@tonic-gate 	    "pam_start(%s,%s,%p:%p) - debug = %x",
2960Sstevel@tonic-gate 	    service ? service : "NULL", user ? user : "NULL", (void *)pam_conv,
2970Sstevel@tonic-gate 	    (void *)*pamh, pam_debug);
2980Sstevel@tonic-gate 
2990Sstevel@tonic-gate 	if (*pamh == NULL)
3000Sstevel@tonic-gate 		return (PAM_BUF_ERR);
3010Sstevel@tonic-gate 
3020Sstevel@tonic-gate 	(*pamh)->pam_inmodule = RO_OK;		/* OK to set RO items */
3030Sstevel@tonic-gate 	if ((err = pam_set_item(*pamh, PAM_SERVICE, (void *)service))
3040Sstevel@tonic-gate 	    != PAM_SUCCESS) {
3050Sstevel@tonic-gate 		clean_up(*pamh);
3060Sstevel@tonic-gate 		*pamh = NULL;
3070Sstevel@tonic-gate 		return (err);
3080Sstevel@tonic-gate 	}
3090Sstevel@tonic-gate 
3100Sstevel@tonic-gate 	if ((err = pam_set_item(*pamh, PAM_USER, (void *)user))
3110Sstevel@tonic-gate 	    != PAM_SUCCESS) {
3120Sstevel@tonic-gate 		clean_up(*pamh);
3130Sstevel@tonic-gate 		*pamh = NULL;
3140Sstevel@tonic-gate 		return (err);
3150Sstevel@tonic-gate 	}
3160Sstevel@tonic-gate 
3170Sstevel@tonic-gate 	if ((err = pam_set_item(*pamh, PAM_CONV, (void *)pam_conv))
3180Sstevel@tonic-gate 	    != PAM_SUCCESS) {
3190Sstevel@tonic-gate 		clean_up(*pamh);
3200Sstevel@tonic-gate 		*pamh = NULL;
3210Sstevel@tonic-gate 		return (err);
3220Sstevel@tonic-gate 	}
3230Sstevel@tonic-gate 
3240Sstevel@tonic-gate 	(*pamh)->pam_inmodule = RW_OK;
3250Sstevel@tonic-gate 	return (PAM_SUCCESS);
3260Sstevel@tonic-gate }
3270Sstevel@tonic-gate 
3280Sstevel@tonic-gate /*
3290Sstevel@tonic-gate  * pam_end - terminate an authentication transaction
3300Sstevel@tonic-gate  */
3310Sstevel@tonic-gate 
3320Sstevel@tonic-gate int
pam_end(pam_handle_t * pamh,int pam_status)3330Sstevel@tonic-gate pam_end(pam_handle_t *pamh, int pam_status)
3340Sstevel@tonic-gate {
3350Sstevel@tonic-gate 	struct pam_module_data *psd, *p;
3360Sstevel@tonic-gate 	fd_list *expired;
3370Sstevel@tonic-gate 	fd_list *traverse;
3380Sstevel@tonic-gate 	env_list *env_expired;
3390Sstevel@tonic-gate 	env_list *env_traverse;
3400Sstevel@tonic-gate 
3410Sstevel@tonic-gate 	pam_trace(PAM_DEBUG_DEFAULT,
3420Sstevel@tonic-gate 	    "pam_end(%p): status = %s", (void *)pamh,
3430Sstevel@tonic-gate 	    pam_strerror(pamh, pam_status));
3440Sstevel@tonic-gate 
3450Sstevel@tonic-gate 	if (pamh == NULL)
3460Sstevel@tonic-gate 		return (PAM_SYSTEM_ERR);
3470Sstevel@tonic-gate 
3480Sstevel@tonic-gate 	/* call the cleanup routines for module specific data */
3490Sstevel@tonic-gate 
3500Sstevel@tonic-gate 	psd = pamh->ssd;
3510Sstevel@tonic-gate 	while (psd) {
3520Sstevel@tonic-gate 		if (psd->cleanup) {
3530Sstevel@tonic-gate 			psd->cleanup(pamh, psd->data, pam_status);
3540Sstevel@tonic-gate 		}
3550Sstevel@tonic-gate 		p = psd;
3560Sstevel@tonic-gate 		psd = p->next;
3570Sstevel@tonic-gate 		free(p->module_data_name);
3580Sstevel@tonic-gate 		free(p);
3590Sstevel@tonic-gate 	}
3600Sstevel@tonic-gate 	pamh->ssd = NULL;
3610Sstevel@tonic-gate 
3620Sstevel@tonic-gate 	/* dlclose all module fds */
3630Sstevel@tonic-gate 	traverse = pamh->fd;
3640Sstevel@tonic-gate 	while (traverse) {
3650Sstevel@tonic-gate 		expired = traverse;
3660Sstevel@tonic-gate 		traverse = traverse->next;
3670Sstevel@tonic-gate 		(void) dlclose(expired->mh);
3680Sstevel@tonic-gate 		free(expired);
3690Sstevel@tonic-gate 	}
3700Sstevel@tonic-gate 	pamh->fd = 0;
3710Sstevel@tonic-gate 
3720Sstevel@tonic-gate 	/* remove all environment variables */
3730Sstevel@tonic-gate 	env_traverse = pamh->pam_env;
3740Sstevel@tonic-gate 	while (env_traverse) {
3750Sstevel@tonic-gate 		env_expired = env_traverse;
3760Sstevel@tonic-gate 		env_traverse = env_traverse->next;
3770Sstevel@tonic-gate 		free_env(env_expired);
3780Sstevel@tonic-gate 	}
3790Sstevel@tonic-gate 
3800Sstevel@tonic-gate 	clean_up(pamh);
3810Sstevel@tonic-gate 	return (PAM_SUCCESS);
3820Sstevel@tonic-gate }
3830Sstevel@tonic-gate 
3840Sstevel@tonic-gate /*
3850Sstevel@tonic-gate  * pam_set_item		- set the value of a parameter that can be
3860Sstevel@tonic-gate  *			  retrieved via a call to pam_get_item()
3870Sstevel@tonic-gate  */
3880Sstevel@tonic-gate 
3890Sstevel@tonic-gate int
pam_set_item(pam_handle_t * pamh,int item_type,const void * item)3900Sstevel@tonic-gate pam_set_item(pam_handle_t *pamh, int item_type, const void *item)
3910Sstevel@tonic-gate {
3920Sstevel@tonic-gate 	struct pam_item *pip;
3930Sstevel@tonic-gate 	int	size;
3940Sstevel@tonic-gate 	char	iname_buf[PAM_MAX_MSG_SIZE];
3950Sstevel@tonic-gate 
3960Sstevel@tonic-gate 	if (((pam_debug & PAM_DEBUG_ITEM) == 0) || (pamh == NULL)) {
3970Sstevel@tonic-gate 		pam_trace(PAM_DEBUG_DEFAULT,
3980Sstevel@tonic-gate 		    "pam_set_item(%p:%s)", (void *)pamh,
3990Sstevel@tonic-gate 		    pam_trace_iname(item_type, iname_buf));
4000Sstevel@tonic-gate 	}
4010Sstevel@tonic-gate 
4020Sstevel@tonic-gate 	if (pamh == NULL)
4030Sstevel@tonic-gate 		return (PAM_SYSTEM_ERR);
4040Sstevel@tonic-gate 
4050Sstevel@tonic-gate 	/* check read only items */
4060Sstevel@tonic-gate 	if ((item_type == PAM_SERVICE) && (pamh->pam_inmodule != RO_OK))
4070Sstevel@tonic-gate 		return (PAM_PERM_DENIED);
4080Sstevel@tonic-gate 
4090Sstevel@tonic-gate 	/*
4100Sstevel@tonic-gate 	 * Check that item_type is within valid range
4110Sstevel@tonic-gate 	 */
4120Sstevel@tonic-gate 
4130Sstevel@tonic-gate 	if (item_type <= 0 || item_type >= PAM_MAX_ITEMS)
4140Sstevel@tonic-gate 		return (PAM_SYMBOL_ERR);
4150Sstevel@tonic-gate 
4160Sstevel@tonic-gate 	pip = &(pamh->ps_item[item_type]);
4170Sstevel@tonic-gate 
4180Sstevel@tonic-gate 	switch (item_type) {
4190Sstevel@tonic-gate 	case PAM_AUTHTOK:
4200Sstevel@tonic-gate 	case PAM_OLDAUTHTOK:
4210Sstevel@tonic-gate 		if (pip->pi_addr != NULL)
4220Sstevel@tonic-gate 			(void) memset(pip->pi_addr, 0, pip->pi_size);
4230Sstevel@tonic-gate 		/*FALLTHROUGH*/
4240Sstevel@tonic-gate 	case PAM_SERVICE:
4250Sstevel@tonic-gate 	case PAM_USER:
4260Sstevel@tonic-gate 	case PAM_TTY:
4270Sstevel@tonic-gate 	case PAM_RHOST:
4280Sstevel@tonic-gate 	case PAM_RUSER:
4290Sstevel@tonic-gate 	case PAM_USER_PROMPT:
4300Sstevel@tonic-gate 	case PAM_RESOURCE:
4312815Sgww 	case PAM_AUSER:
4320Sstevel@tonic-gate 		if (pip->pi_addr != NULL) {
4330Sstevel@tonic-gate 			free(pip->pi_addr);
4340Sstevel@tonic-gate 		}
4350Sstevel@tonic-gate 
4360Sstevel@tonic-gate 		if (item == NULL) {
4370Sstevel@tonic-gate 			pip->pi_addr = NULL;
4380Sstevel@tonic-gate 			pip->pi_size = 0;
4390Sstevel@tonic-gate 		} else {
4400Sstevel@tonic-gate 			pip->pi_addr = strdup((char *)item);
4410Sstevel@tonic-gate 			if (pip->pi_addr == NULL) {
4420Sstevel@tonic-gate 				pip->pi_size = 0;
4430Sstevel@tonic-gate 				return (PAM_BUF_ERR);
4440Sstevel@tonic-gate 			}
4450Sstevel@tonic-gate 			pip->pi_size = strlen(pip->pi_addr);
4460Sstevel@tonic-gate 		}
4470Sstevel@tonic-gate 		break;
4480Sstevel@tonic-gate 	case PAM_CONV:
4490Sstevel@tonic-gate 		if (pip->pi_addr != NULL)
4500Sstevel@tonic-gate 			free(pip->pi_addr);
4510Sstevel@tonic-gate 		size = sizeof (struct pam_conv);
4524758Sgww 		if ((pip->pi_addr = calloc(1, size)) == NULL)
4530Sstevel@tonic-gate 			return (PAM_BUF_ERR);
4540Sstevel@tonic-gate 		if (item != NULL)
4550Sstevel@tonic-gate 			(void) memcpy(pip->pi_addr, item, (unsigned int) size);
4560Sstevel@tonic-gate 		else
4570Sstevel@tonic-gate 			(void) memset(pip->pi_addr, 0, size);
4580Sstevel@tonic-gate 		pip->pi_size = size;
4590Sstevel@tonic-gate 		break;
4600Sstevel@tonic-gate 	case PAM_REPOSITORY:
4610Sstevel@tonic-gate 		if (pip->pi_addr != NULL) {
4620Sstevel@tonic-gate 			pam_repository_t *auth_rep;
4630Sstevel@tonic-gate 
4640Sstevel@tonic-gate 			auth_rep = (pam_repository_t *)pip->pi_addr;
4650Sstevel@tonic-gate 			if (auth_rep->type != NULL)
4660Sstevel@tonic-gate 				free(auth_rep->type);
4670Sstevel@tonic-gate 			if (auth_rep->scope != NULL)
4680Sstevel@tonic-gate 				free(auth_rep->scope);
4690Sstevel@tonic-gate 			free(auth_rep);
4700Sstevel@tonic-gate 		}
4710Sstevel@tonic-gate 		if (item != NULL) {
4720Sstevel@tonic-gate 			pam_repository_t *s, *d;
4730Sstevel@tonic-gate 
4740Sstevel@tonic-gate 			size = sizeof (struct pam_repository);
4754758Sgww 			pip->pi_addr = calloc(1, size);
4760Sstevel@tonic-gate 			if (pip->pi_addr == NULL)
4770Sstevel@tonic-gate 				return (PAM_BUF_ERR);
4780Sstevel@tonic-gate 
4790Sstevel@tonic-gate 			s = (struct pam_repository *)item;
4800Sstevel@tonic-gate 			d = (struct pam_repository *)pip->pi_addr;
4810Sstevel@tonic-gate 
4820Sstevel@tonic-gate 			d->type = strdup(s->type);
4830Sstevel@tonic-gate 			if (d->type == NULL)
4840Sstevel@tonic-gate 				return (PAM_BUF_ERR);
4850Sstevel@tonic-gate 			d->scope = malloc(s->scope_len);
4860Sstevel@tonic-gate 			if (d->scope == NULL)
4870Sstevel@tonic-gate 				return (PAM_BUF_ERR);
4880Sstevel@tonic-gate 			(void) memcpy(d->scope, s->scope, s->scope_len);
4890Sstevel@tonic-gate 			d->scope_len = s->scope_len;
4900Sstevel@tonic-gate 		}
4910Sstevel@tonic-gate 		pip->pi_size = size;
4920Sstevel@tonic-gate 		break;
4930Sstevel@tonic-gate 	default:
4940Sstevel@tonic-gate 		return (PAM_SYMBOL_ERR);
4950Sstevel@tonic-gate 	}
4960Sstevel@tonic-gate 	switch (item_type) {
4970Sstevel@tonic-gate 	case PAM_CONV:
4980Sstevel@tonic-gate 		pam_trace(PAM_DEBUG_ITEM, "pam_set_item(%p:%s)=%p",
4990Sstevel@tonic-gate 		    (void *)pamh,
5000Sstevel@tonic-gate 		    pam_trace_iname(item_type, iname_buf),
5010Sstevel@tonic-gate 		    item ? (void *)((struct pam_conv *)item)->conv :
5020Sstevel@tonic-gate 		    (void *)0);
5030Sstevel@tonic-gate 		break;
5040Sstevel@tonic-gate 	case PAM_REPOSITORY:
5050Sstevel@tonic-gate 		pam_trace(PAM_DEBUG_ITEM, "pam_set_item(%p:%s)=%s",
5060Sstevel@tonic-gate 		    (void *)pamh,
5070Sstevel@tonic-gate 		    pam_trace_iname(item_type, iname_buf),
5080Sstevel@tonic-gate 		    item ? (((struct pam_repository *)item)->type ?
5090Sstevel@tonic-gate 		    ((struct pam_repository *)item)->type : "NULL") :
5100Sstevel@tonic-gate 		    "NULL");
5110Sstevel@tonic-gate 		break;
5120Sstevel@tonic-gate 	case PAM_AUTHTOK:
5130Sstevel@tonic-gate 	case PAM_OLDAUTHTOK:
5140Sstevel@tonic-gate #ifdef	DEBUG
5150Sstevel@tonic-gate 		if (pam_debug & PAM_DEBUG_AUTHTOK)
5160Sstevel@tonic-gate 			pam_trace(PAM_DEBUG_ITEM,
5170Sstevel@tonic-gate 			    "pam_set_item(%p:%s)=%s", (void *)pamh,
5180Sstevel@tonic-gate 			    pam_trace_iname(item_type, iname_buf),
5190Sstevel@tonic-gate 			    item ? (char *)item : "NULL");
5200Sstevel@tonic-gate 		else
5210Sstevel@tonic-gate #endif	/* DEBUG */
5220Sstevel@tonic-gate 			pam_trace(PAM_DEBUG_ITEM,
5230Sstevel@tonic-gate 			    "pam_set_item(%p:%s)=%s", (void *)pamh,
5240Sstevel@tonic-gate 			    pam_trace_iname(item_type, iname_buf),
5250Sstevel@tonic-gate 			    item ? "********" : "NULL");
5260Sstevel@tonic-gate 		break;
5270Sstevel@tonic-gate 	default:
5280Sstevel@tonic-gate 		pam_trace(PAM_DEBUG_ITEM, "pam_set_item(%p:%s)=%s",
5290Sstevel@tonic-gate 		    (void *)pamh,
5300Sstevel@tonic-gate 		    pam_trace_iname(item_type, iname_buf),
5310Sstevel@tonic-gate 		    item ? (char *)item : "NULL");
5320Sstevel@tonic-gate 	}
5330Sstevel@tonic-gate 
5340Sstevel@tonic-gate 	return (PAM_SUCCESS);
5350Sstevel@tonic-gate }
5360Sstevel@tonic-gate 
5370Sstevel@tonic-gate /*
5380Sstevel@tonic-gate  * pam_get_item		- read the value of a parameter specified in
5390Sstevel@tonic-gate  *			  the call to pam_set_item()
5400Sstevel@tonic-gate  */
5410Sstevel@tonic-gate 
5420Sstevel@tonic-gate int
pam_get_item(const pam_handle_t * pamh,int item_type,void ** item)5430Sstevel@tonic-gate pam_get_item(const pam_handle_t *pamh, int item_type, void **item)
5440Sstevel@tonic-gate {
5450Sstevel@tonic-gate 	struct pam_item *pip;
5460Sstevel@tonic-gate 	char	iname_buf[PAM_MAX_MSG_SIZE];
5470Sstevel@tonic-gate 
5480Sstevel@tonic-gate 	if (((pam_debug & PAM_DEBUG_ITEM) == 0) || (pamh == NULL)) {
5490Sstevel@tonic-gate 		pam_trace(PAM_DEBUG_ITEM, "pam_get_item(%p:%s)",
5500Sstevel@tonic-gate 		    (void *)pamh, pam_trace_iname(item_type, iname_buf));
5510Sstevel@tonic-gate 	}
5520Sstevel@tonic-gate 
5530Sstevel@tonic-gate 	if (pamh == NULL)
5540Sstevel@tonic-gate 		return (PAM_SYSTEM_ERR);
5550Sstevel@tonic-gate 
5560Sstevel@tonic-gate 	if (item_type <= 0 || item_type >= PAM_MAX_ITEMS)
5570Sstevel@tonic-gate 		return (PAM_SYMBOL_ERR);
5580Sstevel@tonic-gate 
5590Sstevel@tonic-gate 	if ((pamh->pam_inmodule != WO_OK) &&
5600Sstevel@tonic-gate 	    ((item_type == PAM_AUTHTOK || item_type == PAM_OLDAUTHTOK))) {
5610Sstevel@tonic-gate 		__pam_log(LOG_AUTH | LOG_NOTICE, "pam_get_item(%s) called from "
5620Sstevel@tonic-gate 		    "a non module context",
5630Sstevel@tonic-gate 		    pam_trace_iname(item_type, iname_buf));
5640Sstevel@tonic-gate 		return (PAM_PERM_DENIED);
5650Sstevel@tonic-gate 	}
5660Sstevel@tonic-gate 
5670Sstevel@tonic-gate 	pip = (struct pam_item *)&(pamh->ps_item[item_type]);
5680Sstevel@tonic-gate 
5690Sstevel@tonic-gate 	*item = pip->pi_addr;
5700Sstevel@tonic-gate 	switch (item_type) {
5710Sstevel@tonic-gate 	case PAM_CONV:
5720Sstevel@tonic-gate 		pam_trace(PAM_DEBUG_ITEM, "pam_get_item(%p:%s)=%p",
5730Sstevel@tonic-gate 		    (void *)pamh,
5740Sstevel@tonic-gate 		    pam_trace_iname(item_type, iname_buf),
5750Sstevel@tonic-gate 		    (void *)((struct pam_conv *)*item)->conv);
5760Sstevel@tonic-gate 		break;
5770Sstevel@tonic-gate 	case PAM_REPOSITORY:
5780Sstevel@tonic-gate 		pam_trace(PAM_DEBUG_ITEM, "pam_get_item(%p:%s)=%s",
5790Sstevel@tonic-gate 		    (void *)pamh,
5800Sstevel@tonic-gate 		    pam_trace_iname(item_type, iname_buf),
5810Sstevel@tonic-gate 		    *item ? (((struct pam_repository *)*item)->type ?
5820Sstevel@tonic-gate 		    ((struct pam_repository *)*item)->type : "NULL") :
5830Sstevel@tonic-gate 		    "NULL");
5840Sstevel@tonic-gate 		break;
5850Sstevel@tonic-gate 	case PAM_AUTHTOK:
5860Sstevel@tonic-gate 	case PAM_OLDAUTHTOK:
5870Sstevel@tonic-gate #ifdef	DEBUG
5880Sstevel@tonic-gate 		if (pam_debug & PAM_DEBUG_AUTHTOK)
5890Sstevel@tonic-gate 			pam_trace(PAM_DEBUG_ITEM,
5900Sstevel@tonic-gate 			    "pam_get_item(%p:%s)=%s", (void *)pamh,
5910Sstevel@tonic-gate 			    pam_trace_iname(item_type, iname_buf),
5920Sstevel@tonic-gate 			    *item ? *(char **)item : "NULL");
5930Sstevel@tonic-gate 		else
5940Sstevel@tonic-gate #endif	/* DEBUG */
5950Sstevel@tonic-gate 			pam_trace(PAM_DEBUG_ITEM,
5960Sstevel@tonic-gate 			    "pam_get_item(%p:%s)=%s", (void *)pamh,
5970Sstevel@tonic-gate 			    pam_trace_iname(item_type, iname_buf),
5980Sstevel@tonic-gate 			    *item ? "********" : "NULL");
5990Sstevel@tonic-gate 		break;
6000Sstevel@tonic-gate 	default:
6010Sstevel@tonic-gate 		pam_trace(PAM_DEBUG_ITEM, "pam_get_item(%p:%s)=%s",
6020Sstevel@tonic-gate 		    (void *)pamh,
6030Sstevel@tonic-gate 		    pam_trace_iname(item_type, iname_buf),
6040Sstevel@tonic-gate 		    *item ? *(char **)item : "NULL");
6050Sstevel@tonic-gate 	}
6060Sstevel@tonic-gate 
6070Sstevel@tonic-gate 	return (PAM_SUCCESS);
6080Sstevel@tonic-gate }
6090Sstevel@tonic-gate 
6100Sstevel@tonic-gate /*
6110Sstevel@tonic-gate  * parse_user_name         - process the user response: ignore
6120Sstevel@tonic-gate  *                           '\t' or ' ' before or after a user name.
6130Sstevel@tonic-gate  *                           user_input is a null terminated string.
6140Sstevel@tonic-gate  *                           *ret_username will be the user name.
6150Sstevel@tonic-gate  */
6160Sstevel@tonic-gate 
6170Sstevel@tonic-gate static int
parse_user_name(char * user_input,char ** ret_username)6180Sstevel@tonic-gate parse_user_name(char *user_input, char **ret_username)
6190Sstevel@tonic-gate {
6200Sstevel@tonic-gate 	register char *ptr;
6210Sstevel@tonic-gate 	register int index = 0;
6220Sstevel@tonic-gate 	char username[PAM_MAX_RESP_SIZE];
6230Sstevel@tonic-gate 
6240Sstevel@tonic-gate 	/* Set the default value for *ret_username */
6250Sstevel@tonic-gate 	*ret_username = NULL;
6260Sstevel@tonic-gate 
6270Sstevel@tonic-gate 	/*
6280Sstevel@tonic-gate 	 * Set the initial value for username - this is a buffer holds
6290Sstevel@tonic-gate 	 * the user name.
6300Sstevel@tonic-gate 	 */
6310Sstevel@tonic-gate 	bzero((void *)username, PAM_MAX_RESP_SIZE);
6320Sstevel@tonic-gate 
6330Sstevel@tonic-gate 	/*
6340Sstevel@tonic-gate 	 * The user_input is guaranteed to be terminated by a null character.
6350Sstevel@tonic-gate 	 */
6360Sstevel@tonic-gate 	ptr = user_input;
6370Sstevel@tonic-gate 
6380Sstevel@tonic-gate 	/* Skip all the leading whitespaces if there are any. */
6390Sstevel@tonic-gate 	while ((*ptr == ' ') || (*ptr == '\t'))
6400Sstevel@tonic-gate 		ptr++;
6410Sstevel@tonic-gate 
6420Sstevel@tonic-gate 	if (*ptr == '\0') {
6430Sstevel@tonic-gate 		/*
6440Sstevel@tonic-gate 		 * We should never get here since the user_input we got
6450Sstevel@tonic-gate 		 * in pam_get_user() is not all whitespaces nor just "\0".
6460Sstevel@tonic-gate 		 */
6470Sstevel@tonic-gate 		return (PAM_BUF_ERR);
6480Sstevel@tonic-gate 	}
6490Sstevel@tonic-gate 
6500Sstevel@tonic-gate 	/*
6510Sstevel@tonic-gate 	 * username will be the first string we get from user_input
6520Sstevel@tonic-gate 	 * - we skip leading whitespaces and ignore trailing whitespaces
6530Sstevel@tonic-gate 	 */
6540Sstevel@tonic-gate 	while (*ptr != '\0') {
6550Sstevel@tonic-gate 		if ((*ptr == ' ') || (*ptr == '\t'))
6560Sstevel@tonic-gate 			break;
6570Sstevel@tonic-gate 		else {
6580Sstevel@tonic-gate 			username[index] = *ptr;
6590Sstevel@tonic-gate 			index++;
6600Sstevel@tonic-gate 			ptr++;
6610Sstevel@tonic-gate 		}
6620Sstevel@tonic-gate 	}
6630Sstevel@tonic-gate 
6640Sstevel@tonic-gate 	/* ret_username will be freed in pam_get_user(). */
6654758Sgww 	if ((*ret_username = malloc(index + 1)) == NULL)
6660Sstevel@tonic-gate 		return (PAM_BUF_ERR);
6670Sstevel@tonic-gate 	(void) strcpy(*ret_username, username);
6680Sstevel@tonic-gate 	return (PAM_SUCCESS);
6690Sstevel@tonic-gate }
6700Sstevel@tonic-gate 
6710Sstevel@tonic-gate /*
6720Sstevel@tonic-gate  * Get the value of PAM_USER. If not set, then use the convenience function
6730Sstevel@tonic-gate  * to prompt for the user. Use prompt if specified, else use PAM_USER_PROMPT
6740Sstevel@tonic-gate  * if it is set, else use default.
6750Sstevel@tonic-gate  */
6760Sstevel@tonic-gate #define	WHITESPACE	0
6770Sstevel@tonic-gate #define	USERNAME	1
6780Sstevel@tonic-gate 
6790Sstevel@tonic-gate int
pam_get_user(pam_handle_t * pamh,char ** user,const char * prompt_override)6800Sstevel@tonic-gate pam_get_user(pam_handle_t *pamh, char **user, const char *prompt_override)
6810Sstevel@tonic-gate {
6820Sstevel@tonic-gate 	int	status;
6830Sstevel@tonic-gate 	char	*prompt = NULL;
6840Sstevel@tonic-gate 	char    *real_username;
6850Sstevel@tonic-gate 	struct pam_response *ret_resp = NULL;
6860Sstevel@tonic-gate 	char messages[PAM_MAX_NUM_MSG][PAM_MAX_MSG_SIZE];
6870Sstevel@tonic-gate 
6880Sstevel@tonic-gate 	pam_trace(PAM_DEBUG_DEFAULT,
6890Sstevel@tonic-gate 	    "pam_get_user(%p, %p, %s)", (void *)pamh, (void *)*user,
6900Sstevel@tonic-gate 	    prompt_override ? prompt_override : "NULL");
6910Sstevel@tonic-gate 	if (pamh == NULL)
6920Sstevel@tonic-gate 		return (PAM_SYSTEM_ERR);
6930Sstevel@tonic-gate 
6940Sstevel@tonic-gate 	if ((status = pam_get_item(pamh, PAM_USER, (void **)user))
6950Sstevel@tonic-gate 	    != PAM_SUCCESS) {
6960Sstevel@tonic-gate 		return (status);
6970Sstevel@tonic-gate 	}
6980Sstevel@tonic-gate 
6990Sstevel@tonic-gate 	/* if the user is set, return it */
7000Sstevel@tonic-gate 
7010Sstevel@tonic-gate 	if (*user != NULL && *user[0] != '\0') {
7020Sstevel@tonic-gate 		return (PAM_SUCCESS);
7030Sstevel@tonic-gate 	}
7040Sstevel@tonic-gate 
7050Sstevel@tonic-gate 	/*
7060Sstevel@tonic-gate 	 * if the module is requesting a special prompt, use it.
7070Sstevel@tonic-gate 	 * else use PAM_USER_PROMPT.
7080Sstevel@tonic-gate 	 */
7090Sstevel@tonic-gate 
7100Sstevel@tonic-gate 	if (prompt_override != NULL) {
7110Sstevel@tonic-gate 		prompt = (char *)prompt_override;
7120Sstevel@tonic-gate 	} else {
7130Sstevel@tonic-gate 		status = pam_get_item(pamh, PAM_USER_PROMPT, (void**)&prompt);
7140Sstevel@tonic-gate 		if (status != PAM_SUCCESS) {
7150Sstevel@tonic-gate 			return (status);
7160Sstevel@tonic-gate 		}
7170Sstevel@tonic-gate 	}
7180Sstevel@tonic-gate 
7190Sstevel@tonic-gate 	/* if the prompt is not set, use default */
7200Sstevel@tonic-gate 
7210Sstevel@tonic-gate 	if (prompt == NULL || prompt[0] == '\0') {
7220Sstevel@tonic-gate 		prompt = dgettext(TEXT_DOMAIN, "Please enter user name: ");
7230Sstevel@tonic-gate 	}
7240Sstevel@tonic-gate 
7250Sstevel@tonic-gate 	/* prompt for the user */
7260Sstevel@tonic-gate 
7270Sstevel@tonic-gate 	(void) strncpy(messages[0], prompt, sizeof (messages[0]));
7280Sstevel@tonic-gate 
7290Sstevel@tonic-gate 	for (;;) {
7300Sstevel@tonic-gate 		int state = WHITESPACE;
7310Sstevel@tonic-gate 
7320Sstevel@tonic-gate 		status = do_conv(pamh, PAM_PROMPT_ECHO_ON, 1, messages,
7330Sstevel@tonic-gate 		    NULL, &ret_resp);
7340Sstevel@tonic-gate 
7350Sstevel@tonic-gate 		if (status != PAM_SUCCESS) {
7360Sstevel@tonic-gate 			return (status);
7370Sstevel@tonic-gate 		}
7380Sstevel@tonic-gate 
7390Sstevel@tonic-gate 		if (ret_resp->resp && ret_resp->resp[0] != '\0') {
7400Sstevel@tonic-gate 			int len = strlen(ret_resp->resp);
7410Sstevel@tonic-gate 			int i;
7420Sstevel@tonic-gate 
7430Sstevel@tonic-gate 			for (i = 0; i < len; i++) {
7440Sstevel@tonic-gate 				if ((ret_resp->resp[i] != ' ') &&
7454758Sgww 				    (ret_resp->resp[i] != '\t')) {
7460Sstevel@tonic-gate 					state = USERNAME;
7470Sstevel@tonic-gate 					break;
7480Sstevel@tonic-gate 				}
7490Sstevel@tonic-gate 			}
7500Sstevel@tonic-gate 
7510Sstevel@tonic-gate 			if (state == USERNAME)
7520Sstevel@tonic-gate 				break;
7530Sstevel@tonic-gate 		}
7544758Sgww 		/* essentially empty response, try again */
7554758Sgww 		free_resp(1, ret_resp);
7564758Sgww 		ret_resp = NULL;
7570Sstevel@tonic-gate 	}
7580Sstevel@tonic-gate 
7590Sstevel@tonic-gate 	/* set PAM_USER */
7600Sstevel@tonic-gate 	/* Parse the user input to get the user name. */
7610Sstevel@tonic-gate 	status = parse_user_name(ret_resp->resp, &real_username);
7620Sstevel@tonic-gate 
7630Sstevel@tonic-gate 	if (status != PAM_SUCCESS) {
7640Sstevel@tonic-gate 		if (real_username != NULL)
7650Sstevel@tonic-gate 			free(real_username);
7660Sstevel@tonic-gate 		free_resp(1, ret_resp);
7670Sstevel@tonic-gate 		return (status);
7680Sstevel@tonic-gate 	}
7690Sstevel@tonic-gate 
7700Sstevel@tonic-gate 	status = pam_set_item(pamh, PAM_USER, real_username);
7710Sstevel@tonic-gate 
7720Sstevel@tonic-gate 	free(real_username);
7730Sstevel@tonic-gate 
7740Sstevel@tonic-gate 	free_resp(1, ret_resp);
7750Sstevel@tonic-gate 	if (status != PAM_SUCCESS) {
7760Sstevel@tonic-gate 		return (status);
7770Sstevel@tonic-gate 	}
7780Sstevel@tonic-gate 
7790Sstevel@tonic-gate 	/*
7800Sstevel@tonic-gate 	 * finally, get PAM_USER. We have to call pam_get_item to get
7810Sstevel@tonic-gate 	 * the value of user because pam_set_item mallocs the memory.
7820Sstevel@tonic-gate 	 */
7830Sstevel@tonic-gate 
7840Sstevel@tonic-gate 	status = pam_get_item(pamh, PAM_USER, (void**)user);
7850Sstevel@tonic-gate 	return (status);
7860Sstevel@tonic-gate }
7870Sstevel@tonic-gate 
7880Sstevel@tonic-gate /*
7890Sstevel@tonic-gate  * Set module specific data
7900Sstevel@tonic-gate  */
7910Sstevel@tonic-gate 
7920Sstevel@tonic-gate int
pam_set_data(pam_handle_t * pamh,const char * module_data_name,void * data,void (* cleanup)(pam_handle_t * pamh,void * data,int pam_end_status))7930Sstevel@tonic-gate pam_set_data(pam_handle_t *pamh, const char *module_data_name, void *data,
7940Sstevel@tonic-gate     void (*cleanup)(pam_handle_t *pamh, void *data, int pam_end_status))
7950Sstevel@tonic-gate {
7960Sstevel@tonic-gate 	struct pam_module_data *psd;
7970Sstevel@tonic-gate 
7980Sstevel@tonic-gate 	pam_trace(PAM_DEBUG_DATA,
7990Sstevel@tonic-gate 	    "pam_set_data(%p:%s:%d)=%p", (void *)pamh,
8000Sstevel@tonic-gate 	    module_data_name ? module_data_name : "NULL", pamh->pam_inmodule,
8010Sstevel@tonic-gate 	    data);
8020Sstevel@tonic-gate 	if (pamh == NULL || (pamh->pam_inmodule != WO_OK) ||
8030Sstevel@tonic-gate 	    module_data_name == NULL) {
8040Sstevel@tonic-gate 		return (PAM_SYSTEM_ERR);
8050Sstevel@tonic-gate 	}
8060Sstevel@tonic-gate 
8070Sstevel@tonic-gate 	/* check if module data already exists */
8080Sstevel@tonic-gate 
8090Sstevel@tonic-gate 	for (psd = pamh->ssd; psd; psd = psd->next) {
8100Sstevel@tonic-gate 		if (strcmp(psd->module_data_name, module_data_name) == 0) {
8110Sstevel@tonic-gate 			/* clean up original data before setting the new data */
8120Sstevel@tonic-gate 			if (psd->cleanup) {
8130Sstevel@tonic-gate 				psd->cleanup(pamh, psd->data, PAM_SUCCESS);
8140Sstevel@tonic-gate 			}
8150Sstevel@tonic-gate 			psd->data = (void *)data;
8160Sstevel@tonic-gate 			psd->cleanup = cleanup;
8170Sstevel@tonic-gate 			return (PAM_SUCCESS);
8180Sstevel@tonic-gate 		}
8190Sstevel@tonic-gate 	}
8200Sstevel@tonic-gate 
8210Sstevel@tonic-gate 	psd = malloc(sizeof (struct pam_module_data));
8220Sstevel@tonic-gate 	if (psd == NULL)
8230Sstevel@tonic-gate 		return (PAM_BUF_ERR);
8240Sstevel@tonic-gate 
8250Sstevel@tonic-gate 	psd->module_data_name = strdup(module_data_name);
8260Sstevel@tonic-gate 	if (psd->module_data_name == NULL) {
8270Sstevel@tonic-gate 		free(psd);
8280Sstevel@tonic-gate 		return (PAM_BUF_ERR);
8290Sstevel@tonic-gate 	}
8300Sstevel@tonic-gate 
8310Sstevel@tonic-gate 	psd->data = (void *)data;
8320Sstevel@tonic-gate 	psd->cleanup = cleanup;
8330Sstevel@tonic-gate 	psd->next = pamh->ssd;
8340Sstevel@tonic-gate 	pamh->ssd = psd;
8350Sstevel@tonic-gate 	return (PAM_SUCCESS);
8360Sstevel@tonic-gate }
8370Sstevel@tonic-gate 
8380Sstevel@tonic-gate /*
8390Sstevel@tonic-gate  * get module specific data
8400Sstevel@tonic-gate  */
8410Sstevel@tonic-gate 
8420Sstevel@tonic-gate int
pam_get_data(const pam_handle_t * pamh,const char * module_data_name,const void ** data)8430Sstevel@tonic-gate pam_get_data(const pam_handle_t *pamh, const char *module_data_name,
8440Sstevel@tonic-gate     const void **data)
8450Sstevel@tonic-gate {
8460Sstevel@tonic-gate 	struct pam_module_data *psd;
8470Sstevel@tonic-gate 
8480Sstevel@tonic-gate 	if (pamh == NULL || (pamh->pam_inmodule != WO_OK) ||
8490Sstevel@tonic-gate 	    module_data_name == NULL) {
8500Sstevel@tonic-gate 		pam_trace(PAM_DEBUG_DATA,
8510Sstevel@tonic-gate 		    "pam_get_data(%p:%s:%d)=%p", (void *)pamh,
8520Sstevel@tonic-gate 		    module_data_name ? module_data_name : "NULL",
8530Sstevel@tonic-gate 		    pamh->pam_inmodule, *data);
8540Sstevel@tonic-gate 		return (PAM_SYSTEM_ERR);
8550Sstevel@tonic-gate 	}
8560Sstevel@tonic-gate 
8570Sstevel@tonic-gate 	for (psd = pamh->ssd; psd; psd = psd->next) {
8580Sstevel@tonic-gate 		if (strcmp(psd->module_data_name, module_data_name) == 0) {
8590Sstevel@tonic-gate 			*data = psd->data;
8600Sstevel@tonic-gate 			pam_trace(PAM_DEBUG_DATA,
8610Sstevel@tonic-gate 			    "pam_get_data(%p:%s)=%p", (void *)pamh,
8620Sstevel@tonic-gate 			    module_data_name, *data);
8630Sstevel@tonic-gate 			return (PAM_SUCCESS);
8640Sstevel@tonic-gate 		}
8650Sstevel@tonic-gate 	}
8660Sstevel@tonic-gate 	pam_trace(PAM_DEBUG_DATA,
8670Sstevel@tonic-gate 	    "pam_get_data(%p:%s)=%s", (void *)pamh, module_data_name,
8680Sstevel@tonic-gate 	    "PAM_NO_MODULE_DATA");
8690Sstevel@tonic-gate 
8700Sstevel@tonic-gate 	return (PAM_NO_MODULE_DATA);
8710Sstevel@tonic-gate }
8720Sstevel@tonic-gate 
8730Sstevel@tonic-gate /*
8740Sstevel@tonic-gate  * PAM equivalent to strerror()
8750Sstevel@tonic-gate  */
8760Sstevel@tonic-gate /* ARGSUSED */
8770Sstevel@tonic-gate const char *
pam_strerror(pam_handle_t * pamh,int errnum)8780Sstevel@tonic-gate pam_strerror(pam_handle_t *pamh, int errnum)
8790Sstevel@tonic-gate {
8800Sstevel@tonic-gate 	switch (errnum) {
8810Sstevel@tonic-gate 	case PAM_SUCCESS:
8820Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "Success"));
8830Sstevel@tonic-gate 	case PAM_OPEN_ERR:
8840Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "Dlopen failure"));
8850Sstevel@tonic-gate 	case PAM_SYMBOL_ERR:
8860Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "Symbol not found"));
8870Sstevel@tonic-gate 	case PAM_SERVICE_ERR:
8880Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN,
8890Sstevel@tonic-gate 		    "Error in underlying service module"));
8900Sstevel@tonic-gate 	case PAM_SYSTEM_ERR:
8910Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "System error"));
8920Sstevel@tonic-gate 	case PAM_BUF_ERR:
8930Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "Memory buffer error"));
8940Sstevel@tonic-gate 	case PAM_CONV_ERR:
8950Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "Conversation failure"));
8960Sstevel@tonic-gate 	case PAM_PERM_DENIED:
8970Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "Permission denied"));
8980Sstevel@tonic-gate 	case PAM_MAXTRIES:
8990Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN,
9000Sstevel@tonic-gate 		    "Maximum number of attempts exceeded"));
9010Sstevel@tonic-gate 	case PAM_AUTH_ERR:
9020Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "Authentication failed"));
9030Sstevel@tonic-gate 	case PAM_NEW_AUTHTOK_REQD:
9040Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "Get new authentication token"));
9050Sstevel@tonic-gate 	case PAM_CRED_INSUFFICIENT:
9060Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "Insufficient credentials"));
9070Sstevel@tonic-gate 	case PAM_AUTHINFO_UNAVAIL:
9080Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN,
9090Sstevel@tonic-gate 		    "Can not retrieve authentication info"));
9100Sstevel@tonic-gate 	case PAM_USER_UNKNOWN:
9110Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "No account present for user"));
9120Sstevel@tonic-gate 	case PAM_CRED_UNAVAIL:
9130Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN,
9140Sstevel@tonic-gate 		    "Can not retrieve user credentials"));
9150Sstevel@tonic-gate 	case PAM_CRED_EXPIRED:
9160Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN,
9170Sstevel@tonic-gate 		    "User credentials have expired"));
9180Sstevel@tonic-gate 	case PAM_CRED_ERR:
9190Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN,
9200Sstevel@tonic-gate 		    "Failure setting user credentials"));
9210Sstevel@tonic-gate 	case PAM_ACCT_EXPIRED:
9220Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "User account has expired"));
9230Sstevel@tonic-gate 	case PAM_AUTHTOK_EXPIRED:
9240Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "User password has expired"));
9250Sstevel@tonic-gate 	case PAM_SESSION_ERR:
9260Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN,
9270Sstevel@tonic-gate 		    "Can not make/remove entry for session"));
9280Sstevel@tonic-gate 	case PAM_AUTHTOK_ERR:
9290Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN,
9300Sstevel@tonic-gate 		    "Authentication token manipulation error"));
9310Sstevel@tonic-gate 	case PAM_AUTHTOK_RECOVERY_ERR:
9320Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN,
9330Sstevel@tonic-gate 		    "Authentication token can not be recovered"));
9340Sstevel@tonic-gate 	case PAM_AUTHTOK_LOCK_BUSY:
9350Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN,
9360Sstevel@tonic-gate 		    "Authentication token lock busy"));
9370Sstevel@tonic-gate 	case PAM_AUTHTOK_DISABLE_AGING:
9380Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN,
9390Sstevel@tonic-gate 		    "Authentication token aging disabled"));
9400Sstevel@tonic-gate 	case PAM_NO_MODULE_DATA:
9410Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN,
9420Sstevel@tonic-gate 		    "Module specific data not found"));
9430Sstevel@tonic-gate 	case PAM_IGNORE:
9440Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "Ignore module"));
9450Sstevel@tonic-gate 	case PAM_ABORT:
9460Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "General PAM failure "));
9470Sstevel@tonic-gate 	case PAM_TRY_AGAIN:
9480Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN,
9490Sstevel@tonic-gate 		    "Unable to complete operation. Try again"));
9500Sstevel@tonic-gate 	default:
9510Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "Unknown error"));
9520Sstevel@tonic-gate 	}
9530Sstevel@tonic-gate }
9540Sstevel@tonic-gate 
9550Sstevel@tonic-gate static void *
sm_name(int ind)9560Sstevel@tonic-gate sm_name(int ind)
9570Sstevel@tonic-gate {
9580Sstevel@tonic-gate 	switch (ind) {
9590Sstevel@tonic-gate 	case PAM_AUTHENTICATE:
9600Sstevel@tonic-gate 		return (PAM_SM_AUTHENTICATE);
9610Sstevel@tonic-gate 	case PAM_SETCRED:
9620Sstevel@tonic-gate 		return (PAM_SM_SETCRED);
9630Sstevel@tonic-gate 	case PAM_ACCT_MGMT:
9640Sstevel@tonic-gate 		return (PAM_SM_ACCT_MGMT);
9650Sstevel@tonic-gate 	case PAM_OPEN_SESSION:
9660Sstevel@tonic-gate 		return (PAM_SM_OPEN_SESSION);
9670Sstevel@tonic-gate 	case PAM_CLOSE_SESSION:
9680Sstevel@tonic-gate 		return (PAM_SM_CLOSE_SESSION);
9690Sstevel@tonic-gate 	case PAM_CHAUTHTOK:
9700Sstevel@tonic-gate 		return (PAM_SM_CHAUTHTOK);
9710Sstevel@tonic-gate 	}
9720Sstevel@tonic-gate 	return (NULL);
9730Sstevel@tonic-gate }
9740Sstevel@tonic-gate 
9750Sstevel@tonic-gate static int
func(pamtab_t * modulep,int ind)9760Sstevel@tonic-gate (*func(pamtab_t *modulep, int ind))()
9770Sstevel@tonic-gate {
9780Sstevel@tonic-gate 	void	*funcp;
9790Sstevel@tonic-gate 
9800Sstevel@tonic-gate 	if ((funcp = modulep->function_ptr) == NULL)
9810Sstevel@tonic-gate 		return (NULL);
9820Sstevel@tonic-gate 
9830Sstevel@tonic-gate 	switch (ind) {
9840Sstevel@tonic-gate 	case PAM_AUTHENTICATE:
9850Sstevel@tonic-gate 		return (((struct auth_module *)funcp)->pam_sm_authenticate);
9860Sstevel@tonic-gate 	case PAM_SETCRED:
9870Sstevel@tonic-gate 		return (((struct auth_module *)funcp)->pam_sm_setcred);
9880Sstevel@tonic-gate 	case PAM_ACCT_MGMT:
9890Sstevel@tonic-gate 		return (((struct account_module *)funcp)->pam_sm_acct_mgmt);
9900Sstevel@tonic-gate 	case PAM_OPEN_SESSION:
9910Sstevel@tonic-gate 		return (((struct session_module *)funcp)->pam_sm_open_session);
9920Sstevel@tonic-gate 	case PAM_CLOSE_SESSION:
9930Sstevel@tonic-gate 		return (((struct session_module *)funcp)->pam_sm_close_session);
9940Sstevel@tonic-gate 	case PAM_CHAUTHTOK:
9950Sstevel@tonic-gate 		return (((struct password_module *)funcp)->pam_sm_chauthtok);
9960Sstevel@tonic-gate 	}
9970Sstevel@tonic-gate 	return (NULL);
9980Sstevel@tonic-gate }
9990Sstevel@tonic-gate 
10000Sstevel@tonic-gate /*
10010Sstevel@tonic-gate  * Run through the PAM service module stack for the given module type.
10020Sstevel@tonic-gate  */
10030Sstevel@tonic-gate static int
run_stack(pam_handle_t * pamh,int flags,int type,int def_err,int ind,char * function_name)10040Sstevel@tonic-gate run_stack(pam_handle_t *pamh, int flags, int type, int def_err, int ind,
10050Sstevel@tonic-gate     char *function_name)
10060Sstevel@tonic-gate {
10070Sstevel@tonic-gate 	int	err = PAM_SYSTEM_ERR;  /* preset */
10080Sstevel@tonic-gate 	int	optional_error = 0;
10090Sstevel@tonic-gate 	int	required_error = 0;
10100Sstevel@tonic-gate 	int	success = 0;
10110Sstevel@tonic-gate 	pamtab_t *modulep;
10120Sstevel@tonic-gate 	int	(*sm_func)();
10130Sstevel@tonic-gate 
10140Sstevel@tonic-gate 	if (pamh == NULL)
10150Sstevel@tonic-gate 		return (PAM_SYSTEM_ERR);
10160Sstevel@tonic-gate 
10170Sstevel@tonic-gate 	/* read initial entries from pam.conf */
10180Sstevel@tonic-gate 	if ((err = read_pam_conf(pamh, PAM_CONFIG)) != PAM_SUCCESS) {
10190Sstevel@tonic-gate 		return (err);
10200Sstevel@tonic-gate 	}
10210Sstevel@tonic-gate 
10220Sstevel@tonic-gate 	if ((modulep =
10230Sstevel@tonic-gate 	    pamh->pam_conf_info[pamh->include_depth][type]) == NULL) {
10240Sstevel@tonic-gate 		__pam_log(LOG_AUTH | LOG_ERR, "%s no initial module present",
10250Sstevel@tonic-gate 		    pam_trace_cname(pamh));
10260Sstevel@tonic-gate 		goto exit_return;
10270Sstevel@tonic-gate 	}
10280Sstevel@tonic-gate 
10290Sstevel@tonic-gate 	pamh->pam_inmodule = WO_OK;	/* OK to get AUTHTOK */
10300Sstevel@tonic-gate include:
10310Sstevel@tonic-gate 	pam_trace(PAM_DEBUG_MODULE,
10320Sstevel@tonic-gate 	    "[%d:%s]:run_stack:%s(%p, %x): %s", pamh->include_depth,
10330Sstevel@tonic-gate 	    pam_trace_cname(pamh), function_name, (void *)pamh, flags,
10340Sstevel@tonic-gate 	    modulep ? modulep->module_path : "NULL");
10350Sstevel@tonic-gate 
10360Sstevel@tonic-gate 	while (modulep != NULL) {
10370Sstevel@tonic-gate 		if (modulep->pam_flag & PAM_INCLUDE) {
10380Sstevel@tonic-gate 			/* save the return location */
10390Sstevel@tonic-gate 			pamh->pam_conf_modulep[pamh->include_depth] =
10400Sstevel@tonic-gate 			    modulep->next;
10410Sstevel@tonic-gate 			pam_trace(PAM_DEBUG_MODULE,
10420Sstevel@tonic-gate 			    "setting for include[%d:%p]",
10430Sstevel@tonic-gate 			    pamh->include_depth, (void *)modulep->next);
10440Sstevel@tonic-gate 			if (pamh->include_depth++ >= PAM_MAX_INCLUDE) {
10450Sstevel@tonic-gate 				__pam_log(LOG_AUTH | LOG_ERR,
10460Sstevel@tonic-gate 				    "run_stack: includes too deep %d "
10470Sstevel@tonic-gate 				    "found trying to include %s from %s, %d "
10480Sstevel@tonic-gate 				    "allowed", pamh->include_depth,
10490Sstevel@tonic-gate 				    modulep->module_path, pamh->pam_conf_name
10500Sstevel@tonic-gate 				    [PAM_MAX_INCLUDE] == NULL ? "NULL" :
10510Sstevel@tonic-gate 				    pamh->pam_conf_name[PAM_MAX_INCLUDE],
10520Sstevel@tonic-gate 				    PAM_MAX_INCLUDE);
10530Sstevel@tonic-gate 				goto exit_return;
10540Sstevel@tonic-gate 			}
10550Sstevel@tonic-gate 			if ((err = read_pam_conf(pamh,
10560Sstevel@tonic-gate 			    modulep->module_path)) != PAM_SUCCESS) {
10570Sstevel@tonic-gate 				__pam_log(LOG_AUTH | LOG_ERR,
10580Sstevel@tonic-gate 				    "run_stack[%d:%s]: can't read included "
10590Sstevel@tonic-gate 				    "conf %s", pamh->include_depth,
10600Sstevel@tonic-gate 				    pam_trace_cname(pamh),
10610Sstevel@tonic-gate 				    modulep->module_path);
10620Sstevel@tonic-gate 				goto exit_return;
10630Sstevel@tonic-gate 			}
10640Sstevel@tonic-gate 			if ((modulep = pamh->pam_conf_info
10650Sstevel@tonic-gate 			    [pamh->include_depth][type]) == NULL) {
10660Sstevel@tonic-gate 				__pam_log(LOG_AUTH | LOG_ERR,
10670Sstevel@tonic-gate 				    "run_stack[%d:%s]: no include module "
10680Sstevel@tonic-gate 				    "present %s", pamh->include_depth,
10690Sstevel@tonic-gate 				    pam_trace_cname(pamh), function_name);
10700Sstevel@tonic-gate 				goto exit_return;
10710Sstevel@tonic-gate 			}
10720Sstevel@tonic-gate 			if (modulep->pam_flag & PAM_INCLUDE) {
10730Sstevel@tonic-gate 				/* first line another include */
10740Sstevel@tonic-gate 				goto include;
10750Sstevel@tonic-gate 			}
10760Sstevel@tonic-gate 			pam_trace(PAM_DEBUG_DEFAULT, "include[%d:%s]"
10770Sstevel@tonic-gate 			    "(%p, %s)=%s", pamh->include_depth,
10780Sstevel@tonic-gate 			    pam_trace_cname(pamh), (void *)pamh,
10790Sstevel@tonic-gate 			    function_name, modulep->module_path);
10800Sstevel@tonic-gate 			if ((err = load_modules(pamh, type, sm_name(ind),
10810Sstevel@tonic-gate 			    pamh->pam_conf_info
10820Sstevel@tonic-gate 			    [pamh->include_depth][type])) != PAM_SUCCESS) {
10830Sstevel@tonic-gate 				pam_trace(PAM_DEBUG_DEFAULT,
10840Sstevel@tonic-gate 				    "[%d:%s]:%s(%p, %x): load_modules failed",
10850Sstevel@tonic-gate 				    pamh->include_depth, pam_trace_cname(pamh),
10860Sstevel@tonic-gate 				    function_name, (void *)pamh, flags);
10870Sstevel@tonic-gate 				goto exit_return;
10880Sstevel@tonic-gate 			}
10890Sstevel@tonic-gate 			if ((modulep = pamh->pam_conf_info
10900Sstevel@tonic-gate 			    [pamh->include_depth][type]) == NULL) {
10910Sstevel@tonic-gate 				__pam_log(LOG_AUTH | LOG_ERR,
10920Sstevel@tonic-gate 				    "%s no initial module present",
10930Sstevel@tonic-gate 				    pam_trace_cname(pamh));
10940Sstevel@tonic-gate 				goto exit_return;
10950Sstevel@tonic-gate 			}
10960Sstevel@tonic-gate 		} else if ((err = load_modules(pamh, type, sm_name(ind),
10970Sstevel@tonic-gate 		    modulep)) != PAM_SUCCESS) {
10980Sstevel@tonic-gate 			pam_trace(PAM_DEBUG_DEFAULT,
10990Sstevel@tonic-gate 			    "[%d:%s]:%s(%p, %x): load_modules failed",
11000Sstevel@tonic-gate 			    pamh->include_depth, pam_trace_cname(pamh),
11010Sstevel@tonic-gate 			    function_name, (void *)pamh, flags);
11020Sstevel@tonic-gate 			goto exit_return;
11030Sstevel@tonic-gate 		}  /* PAM_INCLUDE */
11040Sstevel@tonic-gate 		sm_func = func(modulep, ind);
11050Sstevel@tonic-gate 		if (sm_func) {
11060Sstevel@tonic-gate 			err = sm_func(pamh, flags, modulep->module_argc,
11070Sstevel@tonic-gate 			    (const char **)modulep->module_argv);
11080Sstevel@tonic-gate 
11090Sstevel@tonic-gate 			pam_trace(PAM_DEBUG_MODULE,
11100Sstevel@tonic-gate 			    "[%d:%s]:%s(%p, %x): %s returned %s",
11110Sstevel@tonic-gate 			    pamh->include_depth, pam_trace_cname(pamh),
11120Sstevel@tonic-gate 			    function_name, (void *)pamh, flags,
11130Sstevel@tonic-gate 			    modulep->module_path, pam_strerror(pamh, err));
11140Sstevel@tonic-gate 
11150Sstevel@tonic-gate 			switch (err) {
11160Sstevel@tonic-gate 			case PAM_IGNORE:
11170Sstevel@tonic-gate 				/* do nothing */
11180Sstevel@tonic-gate 				break;
11190Sstevel@tonic-gate 			case PAM_SUCCESS:
11200Sstevel@tonic-gate 				if ((modulep->pam_flag & PAM_SUFFI_BIND) &&
11210Sstevel@tonic-gate 				    !required_error) {
11220Sstevel@tonic-gate 					pamh->pam_inmodule = RW_OK;
11230Sstevel@tonic-gate 					pam_trace(PAM_DEBUG_MODULE,
11240Sstevel@tonic-gate 					    "[%d:%s]:%s(%p, %x): %s: success",
11250Sstevel@tonic-gate 					    pamh->include_depth,
11260Sstevel@tonic-gate 					    pam_trace_cname(pamh),
11270Sstevel@tonic-gate 					    function_name, (void *)pamh, flags,
11280Sstevel@tonic-gate 					    (modulep->pam_flag & PAM_BINDING) ?
11290Sstevel@tonic-gate 					    PAM_BINDING_NAME :
11300Sstevel@tonic-gate 					    PAM_SUFFICIENT_NAME);
11310Sstevel@tonic-gate 					goto exit_return;
11320Sstevel@tonic-gate 				}
11330Sstevel@tonic-gate 				success = 1;
11340Sstevel@tonic-gate 				break;
11350Sstevel@tonic-gate 			case PAM_TRY_AGAIN:
11360Sstevel@tonic-gate 				/*
11370Sstevel@tonic-gate 				 * We need to return immediately, and
11380Sstevel@tonic-gate 				 * we shouldn't reset the AUTHTOK item
11390Sstevel@tonic-gate 				 * since it is not an error per-se.
11400Sstevel@tonic-gate 				 */
11410Sstevel@tonic-gate 				pamh->pam_inmodule = RW_OK;
11420Sstevel@tonic-gate 				pam_trace(PAM_DEBUG_MODULE,
11430Sstevel@tonic-gate 				    "[%d:%s]:%s(%p, %x): TRY_AGAIN: %s",
11440Sstevel@tonic-gate 				    pamh->include_depth, pam_trace_cname(pamh),
11450Sstevel@tonic-gate 				    function_name, (void *)pamh, flags,
11460Sstevel@tonic-gate 				    pam_strerror(pamh, required_error ?
11470Sstevel@tonic-gate 				    required_error : err));
11480Sstevel@tonic-gate 				err = required_error ? required_error : err;
11490Sstevel@tonic-gate 				goto exit_return;
11500Sstevel@tonic-gate 			default:
11510Sstevel@tonic-gate 				if (modulep->pam_flag & PAM_REQUISITE) {
11520Sstevel@tonic-gate 					pamh->pam_inmodule = RW_OK;
11530Sstevel@tonic-gate 					pam_trace(PAM_DEBUG_MODULE,
11540Sstevel@tonic-gate 					    "[%d:%s]:%s(%p, %x): requisite: %s",
11550Sstevel@tonic-gate 					    pamh->include_depth,
11560Sstevel@tonic-gate 					    pam_trace_cname(pamh),
11570Sstevel@tonic-gate 					    function_name, (void *)pamh, flags,
11580Sstevel@tonic-gate 					    pam_strerror(pamh,
11590Sstevel@tonic-gate 					    required_error ? required_error :
11600Sstevel@tonic-gate 					    err));
11610Sstevel@tonic-gate 					err = required_error ?
11620Sstevel@tonic-gate 					    required_error : err;
11630Sstevel@tonic-gate 					goto exit_return;
11640Sstevel@tonic-gate 				} else if (modulep->pam_flag & PAM_REQRD_BIND) {
11650Sstevel@tonic-gate 					if (!required_error)
11660Sstevel@tonic-gate 						required_error = err;
11670Sstevel@tonic-gate 				} else {
11680Sstevel@tonic-gate 					if (!optional_error)
11690Sstevel@tonic-gate 						optional_error = err;
11700Sstevel@tonic-gate 				}
11710Sstevel@tonic-gate 				pam_trace(PAM_DEBUG_DEFAULT,
11720Sstevel@tonic-gate 				    "[%d:%s]:%s(%p, %x): error %s",
11730Sstevel@tonic-gate 				    pamh->include_depth, pam_trace_cname(pamh),
11740Sstevel@tonic-gate 				    function_name, (void *)pamh, flags,
11750Sstevel@tonic-gate 				    pam_strerror(pamh, err));
11760Sstevel@tonic-gate 				break;
11770Sstevel@tonic-gate 			}
11780Sstevel@tonic-gate 		}
11790Sstevel@tonic-gate 		modulep = modulep->next;
11800Sstevel@tonic-gate 	}
11810Sstevel@tonic-gate 
11820Sstevel@tonic-gate 	pam_trace(PAM_DEBUG_MODULE, "[%d:%s]:stack_end:%s(%p, %x): %s %s: %s",
11830Sstevel@tonic-gate 	    pamh->include_depth, pam_trace_cname(pamh), function_name,
11840Sstevel@tonic-gate 	    (void *)pamh, flags, pamh->include_depth ? "included" : "final",
11850Sstevel@tonic-gate 	    required_error ? "required" : success ? "success" :
11860Sstevel@tonic-gate 	    optional_error ? "optional" : "default",
11870Sstevel@tonic-gate 	    pam_strerror(pamh, required_error ? required_error :
11880Sstevel@tonic-gate 	    success ? PAM_SUCCESS : optional_error ? optional_error : def_err));
11890Sstevel@tonic-gate 	if (pamh->include_depth > 0) {
11900Sstevel@tonic-gate 		free_pam_conf_info(pamh);
11910Sstevel@tonic-gate 		pamh->include_depth--;
11920Sstevel@tonic-gate 		/* continue at next entry */
11930Sstevel@tonic-gate 		modulep = pamh->pam_conf_modulep[pamh->include_depth];
11940Sstevel@tonic-gate 		pam_trace(PAM_DEBUG_MODULE, "looping for include[%d:%p]",
11950Sstevel@tonic-gate 		    pamh->include_depth, (void *)modulep);
11960Sstevel@tonic-gate 		goto include;
11970Sstevel@tonic-gate 	}
11980Sstevel@tonic-gate 	free_pam_conf_info(pamh);
11990Sstevel@tonic-gate 	pamh->pam_inmodule = RW_OK;
12000Sstevel@tonic-gate 	if (required_error != 0)
12010Sstevel@tonic-gate 		return (required_error);
12020Sstevel@tonic-gate 	else if (success != 0)
12030Sstevel@tonic-gate 		return (PAM_SUCCESS);
12040Sstevel@tonic-gate 	else if (optional_error != 0)
12050Sstevel@tonic-gate 		return (optional_error);
12060Sstevel@tonic-gate 	else
12070Sstevel@tonic-gate 		return (def_err);
12080Sstevel@tonic-gate 
12090Sstevel@tonic-gate exit_return:
12100Sstevel@tonic-gate 	/*
12110Sstevel@tonic-gate 	 * All done at whatever depth we're at.
12120Sstevel@tonic-gate 	 * Go back to not having read /etc/pam.conf
12130Sstevel@tonic-gate 	 */
12140Sstevel@tonic-gate 	while (pamh->include_depth > 0) {
12150Sstevel@tonic-gate 		free_pam_conf_info(pamh);
12160Sstevel@tonic-gate 		pamh->include_depth--;
12170Sstevel@tonic-gate 	}
12180Sstevel@tonic-gate 	free_pam_conf_info(pamh);
12190Sstevel@tonic-gate 	pamh->pam_inmodule = RW_OK;
12200Sstevel@tonic-gate 	return (err);
12210Sstevel@tonic-gate }
12220Sstevel@tonic-gate 
12230Sstevel@tonic-gate /*
12240Sstevel@tonic-gate  * pam_authenticate - authenticate a user
12250Sstevel@tonic-gate  */
12260Sstevel@tonic-gate 
12270Sstevel@tonic-gate int
pam_authenticate(pam_handle_t * pamh,int flags)12280Sstevel@tonic-gate pam_authenticate(pam_handle_t *pamh, int flags)
12290Sstevel@tonic-gate {
12300Sstevel@tonic-gate 	int	retval;
12310Sstevel@tonic-gate 
12320Sstevel@tonic-gate 	retval = run_stack(pamh, flags, PAM_AUTH_MODULE, PAM_AUTH_ERR,
12330Sstevel@tonic-gate 	    PAM_AUTHENTICATE, "pam_authenticate");
12340Sstevel@tonic-gate 
12350Sstevel@tonic-gate 	if (retval != PAM_SUCCESS)
12360Sstevel@tonic-gate 		(void) pam_set_item(pamh, PAM_AUTHTOK, NULL);
12370Sstevel@tonic-gate 	return (retval);
12380Sstevel@tonic-gate }
12390Sstevel@tonic-gate 
12400Sstevel@tonic-gate /*
12410Sstevel@tonic-gate  * pam_setcred - modify or retrieve user credentials
12420Sstevel@tonic-gate  */
12430Sstevel@tonic-gate 
12440Sstevel@tonic-gate int
pam_setcred(pam_handle_t * pamh,int flags)12450Sstevel@tonic-gate pam_setcred(pam_handle_t *pamh, int flags)
12460Sstevel@tonic-gate {
12470Sstevel@tonic-gate 	int	retval;
12480Sstevel@tonic-gate 
12490Sstevel@tonic-gate 	retval = run_stack(pamh, flags, PAM_AUTH_MODULE, PAM_CRED_ERR,
12500Sstevel@tonic-gate 	    PAM_SETCRED, "pam_setcred");
12510Sstevel@tonic-gate 
12520Sstevel@tonic-gate 	if (retval != PAM_SUCCESS)
12530Sstevel@tonic-gate 		(void) pam_set_item(pamh, PAM_AUTHTOK, NULL);
12540Sstevel@tonic-gate 	return (retval);
12550Sstevel@tonic-gate }
12560Sstevel@tonic-gate 
12570Sstevel@tonic-gate /*
12580Sstevel@tonic-gate  * pam_acct_mgmt - check password aging, account expiration
12590Sstevel@tonic-gate  */
12600Sstevel@tonic-gate 
12610Sstevel@tonic-gate int
pam_acct_mgmt(pam_handle_t * pamh,int flags)12620Sstevel@tonic-gate pam_acct_mgmt(pam_handle_t *pamh, int flags)
12630Sstevel@tonic-gate {
12640Sstevel@tonic-gate 	int	retval;
12650Sstevel@tonic-gate 
12660Sstevel@tonic-gate 	retval = run_stack(pamh, flags, PAM_ACCOUNT_MODULE, PAM_ACCT_EXPIRED,
12670Sstevel@tonic-gate 	    PAM_ACCT_MGMT, "pam_acct_mgmt");
12680Sstevel@tonic-gate 
12690Sstevel@tonic-gate 	if (retval != PAM_SUCCESS &&
12700Sstevel@tonic-gate 	    retval != PAM_NEW_AUTHTOK_REQD) {
12710Sstevel@tonic-gate 		(void) pam_set_item(pamh, PAM_AUTHTOK, NULL);
12720Sstevel@tonic-gate 	}
12730Sstevel@tonic-gate 	return (retval);
12740Sstevel@tonic-gate }
12750Sstevel@tonic-gate 
12760Sstevel@tonic-gate /*
12770Sstevel@tonic-gate  * pam_open_session - begin session management
12780Sstevel@tonic-gate  */
12790Sstevel@tonic-gate 
12800Sstevel@tonic-gate int
pam_open_session(pam_handle_t * pamh,int flags)12810Sstevel@tonic-gate pam_open_session(pam_handle_t *pamh, int flags)
12820Sstevel@tonic-gate {
12830Sstevel@tonic-gate 	int	retval;
12840Sstevel@tonic-gate 
12850Sstevel@tonic-gate 	retval = run_stack(pamh, flags, PAM_SESSION_MODULE, PAM_SESSION_ERR,
12860Sstevel@tonic-gate 	    PAM_OPEN_SESSION, "pam_open_session");
12870Sstevel@tonic-gate 
12880Sstevel@tonic-gate 	if (retval != PAM_SUCCESS)
12890Sstevel@tonic-gate 		(void) pam_set_item(pamh, PAM_AUTHTOK, NULL);
12900Sstevel@tonic-gate 	return (retval);
12910Sstevel@tonic-gate }
12920Sstevel@tonic-gate 
12930Sstevel@tonic-gate /*
12940Sstevel@tonic-gate  * pam_close_session - terminate session management
12950Sstevel@tonic-gate  */
12960Sstevel@tonic-gate 
12970Sstevel@tonic-gate int
pam_close_session(pam_handle_t * pamh,int flags)12980Sstevel@tonic-gate pam_close_session(pam_handle_t *pamh, int flags)
12990Sstevel@tonic-gate {
13000Sstevel@tonic-gate 	int	retval;
13010Sstevel@tonic-gate 
13020Sstevel@tonic-gate 	retval = run_stack(pamh, flags, PAM_SESSION_MODULE, PAM_SESSION_ERR,
13030Sstevel@tonic-gate 	    PAM_CLOSE_SESSION, "pam_close_session");
13040Sstevel@tonic-gate 
13050Sstevel@tonic-gate 	if (retval != PAM_SUCCESS)
13060Sstevel@tonic-gate 		(void) pam_set_item(pamh, PAM_AUTHTOK, NULL);
13070Sstevel@tonic-gate 	return (retval);
13080Sstevel@tonic-gate }
13090Sstevel@tonic-gate 
13100Sstevel@tonic-gate /*
13110Sstevel@tonic-gate  * pam_chauthtok - change user authentication token
13120Sstevel@tonic-gate  */
13130Sstevel@tonic-gate 
13140Sstevel@tonic-gate int
pam_chauthtok(pam_handle_t * pamh,int flags)13150Sstevel@tonic-gate pam_chauthtok(pam_handle_t *pamh, int flags)
13160Sstevel@tonic-gate {
13170Sstevel@tonic-gate 	int	retval;
13180Sstevel@tonic-gate 
13190Sstevel@tonic-gate 	/* do not let apps use PAM_PRELIM_CHECK or PAM_UPDATE_AUTHTOK */
13200Sstevel@tonic-gate 	if (flags & (PAM_PRELIM_CHECK | PAM_UPDATE_AUTHTOK)) {
13210Sstevel@tonic-gate 		pam_trace(PAM_DEBUG_DEFAULT,
13220Sstevel@tonic-gate 		    "pam_chauthtok(%p, %x): %s", (void *)pamh, flags,
13230Sstevel@tonic-gate 		    pam_strerror(pamh, PAM_SYMBOL_ERR));
13240Sstevel@tonic-gate 		return (PAM_SYMBOL_ERR);
13250Sstevel@tonic-gate 	}
13260Sstevel@tonic-gate 
13270Sstevel@tonic-gate 	/* 1st pass: PRELIM CHECK */
13280Sstevel@tonic-gate 	retval = run_stack(pamh, flags | PAM_PRELIM_CHECK, PAM_PASSWORD_MODULE,
13290Sstevel@tonic-gate 	    PAM_AUTHTOK_ERR, PAM_CHAUTHTOK, "pam_chauthtok-prelim");
13300Sstevel@tonic-gate 
13310Sstevel@tonic-gate 	if (retval == PAM_TRY_AGAIN)
13320Sstevel@tonic-gate 		return (retval);
13330Sstevel@tonic-gate 
13340Sstevel@tonic-gate 	if (retval != PAM_SUCCESS) {
13350Sstevel@tonic-gate 		(void) pam_set_item(pamh, PAM_AUTHTOK, NULL);
13360Sstevel@tonic-gate 		return (retval);
13370Sstevel@tonic-gate 	}
13380Sstevel@tonic-gate 
13390Sstevel@tonic-gate 	/* 2nd pass: UPDATE AUTHTOK */
13400Sstevel@tonic-gate 	retval = run_stack(pamh, flags | PAM_UPDATE_AUTHTOK,
13410Sstevel@tonic-gate 	    PAM_PASSWORD_MODULE, PAM_AUTHTOK_ERR, PAM_CHAUTHTOK,
13420Sstevel@tonic-gate 	    "pam_chauthtok-update");
13430Sstevel@tonic-gate 
13440Sstevel@tonic-gate 	if (retval != PAM_SUCCESS)
13450Sstevel@tonic-gate 		(void) pam_set_item(pamh, PAM_AUTHTOK, NULL);
13460Sstevel@tonic-gate 
13470Sstevel@tonic-gate 	return (retval);
13480Sstevel@tonic-gate }
13490Sstevel@tonic-gate 
13500Sstevel@tonic-gate /*
13510Sstevel@tonic-gate  * pam_putenv - add an environment variable to the PAM handle
13520Sstevel@tonic-gate  *	if name_value == 'NAME=VALUE'	then set variable to the value
13530Sstevel@tonic-gate  *	if name_value == 'NAME='	then set variable to an empty value
13540Sstevel@tonic-gate  *	if name_value == 'NAME'		then delete the variable
13550Sstevel@tonic-gate  */
13560Sstevel@tonic-gate 
13570Sstevel@tonic-gate int
pam_putenv(pam_handle_t * pamh,const char * name_value)13580Sstevel@tonic-gate pam_putenv(pam_handle_t *pamh, const char *name_value)
13590Sstevel@tonic-gate {
13600Sstevel@tonic-gate 	int		error = PAM_SYSTEM_ERR;
13610Sstevel@tonic-gate 	char		*equal_sign = 0;
13620Sstevel@tonic-gate 	char		*name = NULL, *value = NULL, *tmp_value = NULL;
13630Sstevel@tonic-gate 	env_list	*traverse, *trail;
13640Sstevel@tonic-gate 
13650Sstevel@tonic-gate 	pam_trace(PAM_DEBUG_DEFAULT,
13660Sstevel@tonic-gate 	    "pam_putenv(%p, %s)", (void *)pamh,
13670Sstevel@tonic-gate 	    name_value ? name_value : "NULL");
13680Sstevel@tonic-gate 
13690Sstevel@tonic-gate 	if (pamh == NULL || name_value == NULL)
13700Sstevel@tonic-gate 		goto out;
13710Sstevel@tonic-gate 
13720Sstevel@tonic-gate 	/* see if we were passed 'NAME=VALUE', 'NAME=', or 'NAME' */
13730Sstevel@tonic-gate 	if ((equal_sign = strchr(name_value, '=')) != 0) {
13744758Sgww 		if ((name = calloc(equal_sign - name_value + 1,
13750Sstevel@tonic-gate 		    sizeof (char))) == 0) {
13760Sstevel@tonic-gate 			error = PAM_BUF_ERR;
13770Sstevel@tonic-gate 			goto out;
13780Sstevel@tonic-gate 		}
13790Sstevel@tonic-gate 		(void) strncpy(name, name_value, equal_sign - name_value);
13800Sstevel@tonic-gate 		if ((value = strdup(++equal_sign)) == 0) {
13810Sstevel@tonic-gate 			error = PAM_BUF_ERR;
13820Sstevel@tonic-gate 			goto out;
13830Sstevel@tonic-gate 		}
13840Sstevel@tonic-gate 	} else {
13850Sstevel@tonic-gate 		if ((name = strdup(name_value)) == 0) {
13860Sstevel@tonic-gate 			error = PAM_BUF_ERR;
13870Sstevel@tonic-gate 			goto out;
13880Sstevel@tonic-gate 		}
13890Sstevel@tonic-gate 	}
13900Sstevel@tonic-gate 
13910Sstevel@tonic-gate 	/* check to see if we already have this variable in the PAM handle */
13920Sstevel@tonic-gate 	traverse = pamh->pam_env;
13930Sstevel@tonic-gate 	trail = traverse;
13940Sstevel@tonic-gate 	while (traverse && strncmp(traverse->name, name, strlen(name))) {
13950Sstevel@tonic-gate 		trail = traverse;
13960Sstevel@tonic-gate 		traverse = traverse->next;
13970Sstevel@tonic-gate 	}
13980Sstevel@tonic-gate 
13990Sstevel@tonic-gate 	if (traverse) {
14000Sstevel@tonic-gate 		/* found a match */
14010Sstevel@tonic-gate 		if (value == 0) {
14020Sstevel@tonic-gate 			/* remove the env variable */
14030Sstevel@tonic-gate 			if (pamh->pam_env == traverse)
14040Sstevel@tonic-gate 				pamh->pam_env = traverse->next;
14050Sstevel@tonic-gate 			else
14060Sstevel@tonic-gate 				trail->next = traverse->next;
14070Sstevel@tonic-gate 			free_env(traverse);
14080Sstevel@tonic-gate 		} else if (strlen(value) == 0) {
14090Sstevel@tonic-gate 			/* set env variable to empty value */
14100Sstevel@tonic-gate 			if ((tmp_value = strdup("")) == 0) {
14110Sstevel@tonic-gate 				error = PAM_SYSTEM_ERR;
14120Sstevel@tonic-gate 				goto out;
14130Sstevel@tonic-gate 			}
14140Sstevel@tonic-gate 			free(traverse->value);
14150Sstevel@tonic-gate 			traverse->value = tmp_value;
14160Sstevel@tonic-gate 		} else {
14170Sstevel@tonic-gate 			/* set the new value */
14180Sstevel@tonic-gate 			if ((tmp_value = strdup(value)) == 0) {
14190Sstevel@tonic-gate 				error = PAM_SYSTEM_ERR;
14200Sstevel@tonic-gate 				goto out;
14210Sstevel@tonic-gate 			}
14220Sstevel@tonic-gate 			free(traverse->value);
14230Sstevel@tonic-gate 			traverse->value = tmp_value;
14240Sstevel@tonic-gate 		}
14250Sstevel@tonic-gate 
14260Sstevel@tonic-gate 	} else if (traverse == 0 && value) {
14270Sstevel@tonic-gate 		/*
14280Sstevel@tonic-gate 		 * could not find a match in the PAM handle.
14290Sstevel@tonic-gate 		 * add the new value if there is one
14300Sstevel@tonic-gate 		 */
14314758Sgww 		if ((traverse = calloc(1, sizeof (env_list))) == 0) {
14320Sstevel@tonic-gate 			error = PAM_BUF_ERR;
14330Sstevel@tonic-gate 			goto out;
14340Sstevel@tonic-gate 		}
14350Sstevel@tonic-gate 		if ((traverse->name = strdup(name)) == 0) {
14360Sstevel@tonic-gate 			free_env(traverse);
14370Sstevel@tonic-gate 			error = PAM_BUF_ERR;
14380Sstevel@tonic-gate 			goto out;
14390Sstevel@tonic-gate 		}
14400Sstevel@tonic-gate 		if ((traverse->value = strdup(value)) == 0) {
14410Sstevel@tonic-gate 			free_env(traverse);
14420Sstevel@tonic-gate 			error = PAM_BUF_ERR;
14430Sstevel@tonic-gate 			goto out;
14440Sstevel@tonic-gate 		}
14450Sstevel@tonic-gate 		if (trail == 0) {
14460Sstevel@tonic-gate 			/* new head of list */
14470Sstevel@tonic-gate 			pamh->pam_env = traverse;
14480Sstevel@tonic-gate 		} else {
14490Sstevel@tonic-gate 			/* adding to end of list */
14500Sstevel@tonic-gate 			trail->next = traverse;
14510Sstevel@tonic-gate 		}
14520Sstevel@tonic-gate 	}
14530Sstevel@tonic-gate 
14540Sstevel@tonic-gate 	error = PAM_SUCCESS;
14550Sstevel@tonic-gate out:
14560Sstevel@tonic-gate 	if (error != PAM_SUCCESS) {
14570Sstevel@tonic-gate 		if (traverse) {
14580Sstevel@tonic-gate 			if (traverse->name)
14590Sstevel@tonic-gate 				free(traverse->name);
14600Sstevel@tonic-gate 			if (traverse->value)
14610Sstevel@tonic-gate 				free(traverse->value);
14620Sstevel@tonic-gate 			free(traverse);
14630Sstevel@tonic-gate 		}
14640Sstevel@tonic-gate 	}
14650Sstevel@tonic-gate 	if (name)
14660Sstevel@tonic-gate 		free(name);
14670Sstevel@tonic-gate 	if (value)
14680Sstevel@tonic-gate 		free(value);
14690Sstevel@tonic-gate 	return (error);
14700Sstevel@tonic-gate }
14710Sstevel@tonic-gate 
14720Sstevel@tonic-gate /*
14730Sstevel@tonic-gate  * pam_getenv - retrieve an environment variable from the PAM handle
14740Sstevel@tonic-gate  */
14750Sstevel@tonic-gate char *
pam_getenv(pam_handle_t * pamh,const char * name)14760Sstevel@tonic-gate pam_getenv(pam_handle_t *pamh, const char *name)
14770Sstevel@tonic-gate {
14780Sstevel@tonic-gate 	int		error = PAM_SYSTEM_ERR;
14790Sstevel@tonic-gate 	env_list	*traverse;
14800Sstevel@tonic-gate 
14810Sstevel@tonic-gate 	pam_trace(PAM_DEBUG_DEFAULT,
14820Sstevel@tonic-gate 	    "pam_getenv(%p, %p)", (void *)pamh, (void *)name);
14830Sstevel@tonic-gate 
14840Sstevel@tonic-gate 	if (pamh == NULL || name == NULL)
14850Sstevel@tonic-gate 		goto out;
14860Sstevel@tonic-gate 
14870Sstevel@tonic-gate 	/* check to see if we already have this variable in the PAM handle */
14880Sstevel@tonic-gate 	traverse = pamh->pam_env;
14890Sstevel@tonic-gate 	while (traverse && strncmp(traverse->name, name, strlen(name))) {
14900Sstevel@tonic-gate 		traverse = traverse->next;
14910Sstevel@tonic-gate 	}
14920Sstevel@tonic-gate 	error = (traverse ? PAM_SUCCESS : PAM_SYSTEM_ERR);
14930Sstevel@tonic-gate 	pam_trace(PAM_DEBUG_DEFAULT,
14940Sstevel@tonic-gate 	    "pam_getenv(%p, %s)=%s", (void *)pamh, name,
14950Sstevel@tonic-gate 	    traverse ? traverse->value : "NULL");
14960Sstevel@tonic-gate out:
14970Sstevel@tonic-gate 	return (error ? NULL : strdup(traverse->value));
14980Sstevel@tonic-gate }
14990Sstevel@tonic-gate 
15000Sstevel@tonic-gate /*
15010Sstevel@tonic-gate  * pam_getenvlist - retrieve all environment variables from the PAM handle
15023358Sjjj  *                  in a NULL terminated array. On error, return NULL.
15030Sstevel@tonic-gate  */
15040Sstevel@tonic-gate char **
pam_getenvlist(pam_handle_t * pamh)15050Sstevel@tonic-gate pam_getenvlist(pam_handle_t *pamh)
15060Sstevel@tonic-gate {
15070Sstevel@tonic-gate 	int		error = PAM_SYSTEM_ERR;
15080Sstevel@tonic-gate 	char		**list = 0;
15090Sstevel@tonic-gate 	int		length = 0;
15100Sstevel@tonic-gate 	env_list	*traverse;
15113358Sjjj 	char		*tenv;
15123358Sjjj 	size_t		tenv_size;
15130Sstevel@tonic-gate 
15140Sstevel@tonic-gate 	pam_trace(PAM_DEBUG_DEFAULT,
15150Sstevel@tonic-gate 	    "pam_getenvlist(%p)", (void *)pamh);
15160Sstevel@tonic-gate 
15170Sstevel@tonic-gate 	if (pamh == NULL)
15180Sstevel@tonic-gate 		goto out;
15190Sstevel@tonic-gate 
15200Sstevel@tonic-gate 	/* find out how many environment variables we have */
15210Sstevel@tonic-gate 	traverse = pamh->pam_env;
15220Sstevel@tonic-gate 	while (traverse) {
15230Sstevel@tonic-gate 		length++;
15240Sstevel@tonic-gate 		traverse = traverse->next;
15250Sstevel@tonic-gate 	}
15260Sstevel@tonic-gate 
15270Sstevel@tonic-gate 	/* allocate the array we will return to the caller */
15284758Sgww 	if ((list = calloc(length + 1, sizeof (char *))) == NULL) {
15290Sstevel@tonic-gate 		error = PAM_BUF_ERR;
15300Sstevel@tonic-gate 		goto out;
15310Sstevel@tonic-gate 	}
15320Sstevel@tonic-gate 
15330Sstevel@tonic-gate 	/* add the variables one by one */
15340Sstevel@tonic-gate 	length = 0;
15350Sstevel@tonic-gate 	traverse = pamh->pam_env;
15363358Sjjj 	while (traverse != NULL) {
15373358Sjjj 		tenv_size = strlen(traverse->name) +
15383358Sjjj 		    strlen(traverse->value) + 2; /* name=val\0 */
15393358Sjjj 		if ((tenv = malloc(tenv_size)) == NULL) {
15400Sstevel@tonic-gate 			error = PAM_BUF_ERR;
15410Sstevel@tonic-gate 			goto out;
15420Sstevel@tonic-gate 		}
15433358Sjjj 		/*LINTED*/
15443358Sjjj 		(void) sprintf(tenv, "%s=%s", traverse->name, traverse->value);
15453358Sjjj 		list[length++] = tenv;
15460Sstevel@tonic-gate 		traverse = traverse->next;
15470Sstevel@tonic-gate 	}
15483358Sjjj 	list[length] = NULL;
15490Sstevel@tonic-gate 
15500Sstevel@tonic-gate 	error = PAM_SUCCESS;
15510Sstevel@tonic-gate out:
15520Sstevel@tonic-gate 	if (error != PAM_SUCCESS) {
15530Sstevel@tonic-gate 		/* free the partially constructed list */
15540Sstevel@tonic-gate 		if (list) {
15550Sstevel@tonic-gate 			length = 0;
15560Sstevel@tonic-gate 			while (list[length] != NULL) {
15570Sstevel@tonic-gate 				free(list[length]);
15580Sstevel@tonic-gate 				length++;
15590Sstevel@tonic-gate 			}
15600Sstevel@tonic-gate 			free(list);
15610Sstevel@tonic-gate 		}
15620Sstevel@tonic-gate 	}
15630Sstevel@tonic-gate 	return (error ? NULL : list);
15640Sstevel@tonic-gate }
15650Sstevel@tonic-gate 
15660Sstevel@tonic-gate /*
15670Sstevel@tonic-gate  * Routines to load a requested module on demand
15680Sstevel@tonic-gate  */
15690Sstevel@tonic-gate 
15700Sstevel@tonic-gate /*
15710Sstevel@tonic-gate  * load_modules - load the requested module.
15720Sstevel@tonic-gate  *		  if the dlopen or dlsym fail, then
15730Sstevel@tonic-gate  *		  the module is ignored.
15740Sstevel@tonic-gate  */
15750Sstevel@tonic-gate 
15760Sstevel@tonic-gate static int
load_modules(pam_handle_t * pamh,int type,char * function_name,pamtab_t * pam_entry)15770Sstevel@tonic-gate load_modules(pam_handle_t *pamh, int type, char *function_name,
15780Sstevel@tonic-gate     pamtab_t *pam_entry)
15790Sstevel@tonic-gate {
15800Sstevel@tonic-gate 	void	*mh;
15810Sstevel@tonic-gate 	struct	auth_module *authp;
15820Sstevel@tonic-gate 	struct	account_module *accountp;
15830Sstevel@tonic-gate 	struct	session_module *sessionp;
15840Sstevel@tonic-gate 	struct	password_module *passwdp;
15850Sstevel@tonic-gate 	int	loading_functions = 0; /* are we currently loading functions? */
15860Sstevel@tonic-gate 
15870Sstevel@tonic-gate 	pam_trace(PAM_DEBUG_MODULE, "load_modules[%d:%s](%p, %s)=%s:%s",
15884758Sgww 	    pamh->include_depth, pam_trace_cname(pamh), (void *)pamh,
15894758Sgww 	    function_name, pam_trace_fname(pam_entry->pam_flag),
15904758Sgww 	    pam_entry->module_path);
15910Sstevel@tonic-gate 
15920Sstevel@tonic-gate 	while (pam_entry != NULL) {
15930Sstevel@tonic-gate 		pam_trace(PAM_DEBUG_DEFAULT,
15940Sstevel@tonic-gate 		    "while load_modules[%d:%s](%p, %s)=%s",
15950Sstevel@tonic-gate 		    pamh->include_depth, pam_trace_cname(pamh), (void *)pamh,
15960Sstevel@tonic-gate 		    function_name, pam_entry->module_path);
15970Sstevel@tonic-gate 
15980Sstevel@tonic-gate 		if (pam_entry->pam_flag & PAM_INCLUDE) {
15990Sstevel@tonic-gate 			pam_trace(PAM_DEBUG_DEFAULT,
16000Sstevel@tonic-gate 			    "done load_modules[%d:%s](%p, %s)=%s",
16010Sstevel@tonic-gate 			    pamh->include_depth, pam_trace_cname(pamh),
16020Sstevel@tonic-gate 			    (void *)pamh, function_name,
16030Sstevel@tonic-gate 			    pam_entry->module_path);
16040Sstevel@tonic-gate 			return (PAM_SUCCESS);
16050Sstevel@tonic-gate 		}
16060Sstevel@tonic-gate 		switch (type) {
16070Sstevel@tonic-gate 		case PAM_AUTH_MODULE:
16080Sstevel@tonic-gate 
16090Sstevel@tonic-gate 			/* if the function has already been loaded, return */
16100Sstevel@tonic-gate 			authp = pam_entry->function_ptr;
16110Sstevel@tonic-gate 			if (!loading_functions &&
16124758Sgww 			    (((strcmp(function_name, PAM_SM_AUTHENTICATE)
16134758Sgww 			    == 0) && authp && authp->pam_sm_authenticate) ||
16144758Sgww 			    ((strcmp(function_name, PAM_SM_SETCRED) == 0) &&
16154758Sgww 			    authp && authp->pam_sm_setcred))) {
16160Sstevel@tonic-gate 				return (PAM_SUCCESS);
16170Sstevel@tonic-gate 			}
16180Sstevel@tonic-gate 
16190Sstevel@tonic-gate 			/* function has not been loaded yet */
16200Sstevel@tonic-gate 			loading_functions = 1;
16210Sstevel@tonic-gate 			if (authp == NULL) {
16224758Sgww 				authp = calloc(1, sizeof (struct auth_module));
16230Sstevel@tonic-gate 				if (authp == NULL)
16240Sstevel@tonic-gate 					return (PAM_BUF_ERR);
16250Sstevel@tonic-gate 			}
16260Sstevel@tonic-gate 
16270Sstevel@tonic-gate 			/* if open_module fails, return error */
16280Sstevel@tonic-gate 			if ((mh = open_module(pamh,
16290Sstevel@tonic-gate 			    pam_entry->module_path)) == NULL) {
16300Sstevel@tonic-gate 				__pam_log(LOG_AUTH | LOG_ERR,
16310Sstevel@tonic-gate 				    "load_modules[%d:%s]: can not open module "
16320Sstevel@tonic-gate 				    "%s", pamh->include_depth,
16330Sstevel@tonic-gate 				    pam_trace_cname(pamh),
16340Sstevel@tonic-gate 				    pam_entry->module_path);
16350Sstevel@tonic-gate 				free(authp);
16360Sstevel@tonic-gate 				return (PAM_OPEN_ERR);
16370Sstevel@tonic-gate 			}
16380Sstevel@tonic-gate 
16390Sstevel@tonic-gate 			/* load the authentication function */
16400Sstevel@tonic-gate 			if (strcmp(function_name, PAM_SM_AUTHENTICATE) == 0) {
16410Sstevel@tonic-gate 				if (load_function(mh, PAM_SM_AUTHENTICATE,
16420Sstevel@tonic-gate 				    &authp->pam_sm_authenticate)
16430Sstevel@tonic-gate 				    != PAM_SUCCESS) {
16440Sstevel@tonic-gate 					/* return error if dlsym fails */
16450Sstevel@tonic-gate 					free(authp);
16460Sstevel@tonic-gate 					return (PAM_SYMBOL_ERR);
16470Sstevel@tonic-gate 				}
16480Sstevel@tonic-gate 
16490Sstevel@tonic-gate 			/* load the setcred function */
16500Sstevel@tonic-gate 			} else if (strcmp(function_name, PAM_SM_SETCRED) == 0) {
16510Sstevel@tonic-gate 				if (load_function(mh, PAM_SM_SETCRED,
16520Sstevel@tonic-gate 				    &authp->pam_sm_setcred) != PAM_SUCCESS) {
16530Sstevel@tonic-gate 					/* return error if dlsym fails */
16540Sstevel@tonic-gate 					free(authp);
16550Sstevel@tonic-gate 					return (PAM_SYMBOL_ERR);
16560Sstevel@tonic-gate 				}
16570Sstevel@tonic-gate 			}
16580Sstevel@tonic-gate 			pam_entry->function_ptr = authp;
16590Sstevel@tonic-gate 			break;
16600Sstevel@tonic-gate 		case PAM_ACCOUNT_MODULE:
16610Sstevel@tonic-gate 			accountp = pam_entry->function_ptr;
16620Sstevel@tonic-gate 			if (!loading_functions &&
16630Sstevel@tonic-gate 			    (strcmp(function_name, PAM_SM_ACCT_MGMT) == 0) &&
16640Sstevel@tonic-gate 			    accountp && accountp->pam_sm_acct_mgmt) {
16650Sstevel@tonic-gate 				return (PAM_SUCCESS);
16660Sstevel@tonic-gate 			}
16670Sstevel@tonic-gate 
16680Sstevel@tonic-gate 			/*
16690Sstevel@tonic-gate 			 * If functions are added to the account module,
16700Sstevel@tonic-gate 			 * verify that one of the other functions hasn't
16710Sstevel@tonic-gate 			 * already loaded it.  See PAM_AUTH_MODULE code.
16720Sstevel@tonic-gate 			 */
16730Sstevel@tonic-gate 			loading_functions = 1;
16744758Sgww 			accountp = calloc(1, sizeof (struct account_module));
16750Sstevel@tonic-gate 			if (accountp == NULL)
16760Sstevel@tonic-gate 				return (PAM_BUF_ERR);
16770Sstevel@tonic-gate 
16780Sstevel@tonic-gate 			/* if open_module fails, return error */
16790Sstevel@tonic-gate 			if ((mh = open_module(pamh,
16800Sstevel@tonic-gate 			    pam_entry->module_path)) == NULL) {
16810Sstevel@tonic-gate 				__pam_log(LOG_AUTH | LOG_ERR,
16820Sstevel@tonic-gate 				    "load_modules[%d:%s]: can not open module "
16830Sstevel@tonic-gate 				    "%s", pamh->include_depth,
16840Sstevel@tonic-gate 				    pam_trace_cname(pamh),
16850Sstevel@tonic-gate 				    pam_entry->module_path);
16860Sstevel@tonic-gate 				free(accountp);
16870Sstevel@tonic-gate 				return (PAM_OPEN_ERR);
16880Sstevel@tonic-gate 			}
16890Sstevel@tonic-gate 
16900Sstevel@tonic-gate 			if (load_function(mh, PAM_SM_ACCT_MGMT,
16910Sstevel@tonic-gate 			    &accountp->pam_sm_acct_mgmt) != PAM_SUCCESS) {
16920Sstevel@tonic-gate 				__pam_log(LOG_AUTH | LOG_ERR,
16930Sstevel@tonic-gate 				    "load_modules[%d:%s]: pam_sm_acct_mgmt() "
16940Sstevel@tonic-gate 				    "missing", pamh->include_depth,
16950Sstevel@tonic-gate 				    pam_trace_cname(pamh));
16960Sstevel@tonic-gate 				free(accountp);
16970Sstevel@tonic-gate 				return (PAM_SYMBOL_ERR);
16980Sstevel@tonic-gate 			}
16990Sstevel@tonic-gate 			pam_entry->function_ptr = accountp;
17000Sstevel@tonic-gate 			break;
17010Sstevel@tonic-gate 		case PAM_SESSION_MODULE:
17020Sstevel@tonic-gate 			sessionp = pam_entry->function_ptr;
17030Sstevel@tonic-gate 			if (!loading_functions &&
17040Sstevel@tonic-gate 			    (((strcmp(function_name,
17050Sstevel@tonic-gate 			    PAM_SM_OPEN_SESSION) == 0) &&
17060Sstevel@tonic-gate 			    sessionp && sessionp->pam_sm_open_session) ||
17070Sstevel@tonic-gate 			    ((strcmp(function_name,
17080Sstevel@tonic-gate 			    PAM_SM_CLOSE_SESSION) == 0) &&
17090Sstevel@tonic-gate 			    sessionp && sessionp->pam_sm_close_session))) {
17100Sstevel@tonic-gate 				return (PAM_SUCCESS);
17110Sstevel@tonic-gate 			}
17120Sstevel@tonic-gate 
17130Sstevel@tonic-gate 			loading_functions = 1;
17140Sstevel@tonic-gate 			if (sessionp == NULL) {
17154758Sgww 				sessionp = calloc(1,
17164758Sgww 				    sizeof (struct session_module));
17170Sstevel@tonic-gate 				if (sessionp == NULL)
17180Sstevel@tonic-gate 					return (PAM_BUF_ERR);
17190Sstevel@tonic-gate 			}
17200Sstevel@tonic-gate 
17210Sstevel@tonic-gate 			/* if open_module fails, return error */
17220Sstevel@tonic-gate 			if ((mh = open_module(pamh,
17230Sstevel@tonic-gate 			    pam_entry->module_path)) == NULL) {
17240Sstevel@tonic-gate 				__pam_log(LOG_AUTH | LOG_ERR,
17250Sstevel@tonic-gate 				    "load_modules[%d:%s]: can not open module "
17260Sstevel@tonic-gate 				    "%s", pamh->include_depth,
17270Sstevel@tonic-gate 				    pam_trace_cname(pamh),
17280Sstevel@tonic-gate 				    pam_entry->module_path);
17290Sstevel@tonic-gate 				free(sessionp);
17300Sstevel@tonic-gate 				return (PAM_OPEN_ERR);
17310Sstevel@tonic-gate 			}
17320Sstevel@tonic-gate 
17330Sstevel@tonic-gate 			if ((strcmp(function_name, PAM_SM_OPEN_SESSION) == 0) &&
17340Sstevel@tonic-gate 			    load_function(mh, PAM_SM_OPEN_SESSION,
17354758Sgww 			    &sessionp->pam_sm_open_session) != PAM_SUCCESS) {
17360Sstevel@tonic-gate 				free(sessionp);
17370Sstevel@tonic-gate 				return (PAM_SYMBOL_ERR);
17380Sstevel@tonic-gate 			} else if ((strcmp(function_name,
17394758Sgww 			    PAM_SM_CLOSE_SESSION) == 0) &&
17404758Sgww 			    load_function(mh, PAM_SM_CLOSE_SESSION,
17414758Sgww 			    &sessionp->pam_sm_close_session) != PAM_SUCCESS) {
17420Sstevel@tonic-gate 				free(sessionp);
17430Sstevel@tonic-gate 				return (PAM_SYMBOL_ERR);
17440Sstevel@tonic-gate 			}
17450Sstevel@tonic-gate 			pam_entry->function_ptr = sessionp;
17460Sstevel@tonic-gate 			break;
17470Sstevel@tonic-gate 		case PAM_PASSWORD_MODULE:
17480Sstevel@tonic-gate 			passwdp = pam_entry->function_ptr;
17490Sstevel@tonic-gate 			if (!loading_functions &&
17500Sstevel@tonic-gate 			    (strcmp(function_name, PAM_SM_CHAUTHTOK) == 0) &&
17510Sstevel@tonic-gate 			    passwdp && passwdp->pam_sm_chauthtok) {
17520Sstevel@tonic-gate 				return (PAM_SUCCESS);
17530Sstevel@tonic-gate 			}
17540Sstevel@tonic-gate 
17550Sstevel@tonic-gate 			/*
17560Sstevel@tonic-gate 			 * If functions are added to the password module,
17570Sstevel@tonic-gate 			 * verify that one of the other functions hasn't
17580Sstevel@tonic-gate 			 * already loaded it.  See PAM_AUTH_MODULE code.
17590Sstevel@tonic-gate 			 */
17600Sstevel@tonic-gate 			loading_functions = 1;
17614758Sgww 			passwdp = calloc(1, sizeof (struct password_module));
17620Sstevel@tonic-gate 			if (passwdp == NULL)
17630Sstevel@tonic-gate 				return (PAM_BUF_ERR);
17640Sstevel@tonic-gate 
17650Sstevel@tonic-gate 			/* if open_module fails, continue */
17660Sstevel@tonic-gate 			if ((mh = open_module(pamh,
17670Sstevel@tonic-gate 			    pam_entry->module_path)) == NULL) {
17680Sstevel@tonic-gate 				__pam_log(LOG_AUTH | LOG_ERR,
17690Sstevel@tonic-gate 				    "load_modules[%d:%s]: can not open module "
17700Sstevel@tonic-gate 				    "%s", pamh->include_depth,
17710Sstevel@tonic-gate 				    pam_trace_cname(pamh),
17720Sstevel@tonic-gate 				    pam_entry->module_path);
17730Sstevel@tonic-gate 				free(passwdp);
17740Sstevel@tonic-gate 				return (PAM_OPEN_ERR);
17750Sstevel@tonic-gate 			}
17760Sstevel@tonic-gate 
17770Sstevel@tonic-gate 			if (load_function(mh, PAM_SM_CHAUTHTOK,
17780Sstevel@tonic-gate 			    &passwdp->pam_sm_chauthtok) != PAM_SUCCESS) {
17790Sstevel@tonic-gate 				free(passwdp);
17800Sstevel@tonic-gate 				return (PAM_SYMBOL_ERR);
17810Sstevel@tonic-gate 			}
17820Sstevel@tonic-gate 			pam_entry->function_ptr = passwdp;
17830Sstevel@tonic-gate 			break;
17840Sstevel@tonic-gate 		default:
17850Sstevel@tonic-gate 			pam_trace(PAM_DEBUG_DEFAULT,
17860Sstevel@tonic-gate 			    "load_modules[%d:%s](%p, %s): unsupported type %d",
17870Sstevel@tonic-gate 			    pamh->include_depth, pam_trace_cname(pamh),
17880Sstevel@tonic-gate 			    (void *)pamh, function_name, type);
17890Sstevel@tonic-gate 			break;
17900Sstevel@tonic-gate 		}
17910Sstevel@tonic-gate 
17920Sstevel@tonic-gate 		pam_entry = pam_entry->next;
17930Sstevel@tonic-gate 	} /* while */
17940Sstevel@tonic-gate 
17950Sstevel@tonic-gate 	pam_trace(PAM_DEBUG_MODULE, "load_modules[%d:%s](%p, %s)=done",
17960Sstevel@tonic-gate 	    pamh->include_depth, pam_trace_cname(pamh), (void *)pamh,
17970Sstevel@tonic-gate 	    function_name);
17980Sstevel@tonic-gate 
17990Sstevel@tonic-gate 	return (PAM_SUCCESS);
18000Sstevel@tonic-gate }
18010Sstevel@tonic-gate 
18020Sstevel@tonic-gate /*
18030Sstevel@tonic-gate  * open_module		- Open the module first checking for
18040Sstevel@tonic-gate  *			  propers modes and ownerships on the file.
18050Sstevel@tonic-gate  */
18060Sstevel@tonic-gate 
18070Sstevel@tonic-gate static void *
open_module(pam_handle_t * pamh,char * module_so)18080Sstevel@tonic-gate open_module(pam_handle_t *pamh, char *module_so)
18090Sstevel@tonic-gate {
18100Sstevel@tonic-gate 	struct stat64	stb;
18110Sstevel@tonic-gate 	char		*errmsg;
18120Sstevel@tonic-gate 	void		*lfd;
18130Sstevel@tonic-gate 	fd_list		*module_fds = 0;
18140Sstevel@tonic-gate 	fd_list		*trail = 0;
18150Sstevel@tonic-gate 	fd_list		*traverse = 0;
18160Sstevel@tonic-gate 
18170Sstevel@tonic-gate 	/* Check the ownership and file modes */
18180Sstevel@tonic-gate 	if (stat64(module_so, &stb) < 0) {
18190Sstevel@tonic-gate 		__pam_log(LOG_AUTH | LOG_ERR,
18200Sstevel@tonic-gate 		    "open_module[%d:%s]: stat(%s) failed: %s",
18210Sstevel@tonic-gate 		    pamh->include_depth, pam_trace_cname(pamh), module_so,
18220Sstevel@tonic-gate 		    strerror(errno));
18230Sstevel@tonic-gate 		return (NULL);
18240Sstevel@tonic-gate 	}
18250Sstevel@tonic-gate 	if (stb.st_uid != (uid_t)0) {
18260Sstevel@tonic-gate 		__pam_log(LOG_AUTH | LOG_ALERT,
18270Sstevel@tonic-gate 		    "open_module[%d:%s]: Owner of the module %s is not root",
18280Sstevel@tonic-gate 		    pamh->include_depth, pam_trace_cname(pamh), module_so);
18290Sstevel@tonic-gate 		return (NULL);
18300Sstevel@tonic-gate 	}
18310Sstevel@tonic-gate 	if (stb.st_mode & S_IWGRP) {
18320Sstevel@tonic-gate 		__pam_log(LOG_AUTH | LOG_ALERT,
18330Sstevel@tonic-gate 		    "open_module[%d:%s]: module %s writable by group",
18340Sstevel@tonic-gate 		    pamh->include_depth, pam_trace_cname(pamh), module_so);
18350Sstevel@tonic-gate 		return (NULL);
18360Sstevel@tonic-gate 	}
18370Sstevel@tonic-gate 	if (stb.st_mode & S_IWOTH) {
18380Sstevel@tonic-gate 		__pam_log(LOG_AUTH | LOG_ALERT,
18390Sstevel@tonic-gate 		    "open_module[%d:%s]: module %s writable by world",
18400Sstevel@tonic-gate 		    pamh->include_depth, pam_trace_cname(pamh), module_so);
18410Sstevel@tonic-gate 		return (NULL);
18420Sstevel@tonic-gate 	}
18430Sstevel@tonic-gate 
18440Sstevel@tonic-gate 	/*
18450Sstevel@tonic-gate 	 * Perform the dlopen()
18460Sstevel@tonic-gate 	 */
18470Sstevel@tonic-gate 	lfd = (void *)dlopen(module_so, RTLD_LAZY);
18480Sstevel@tonic-gate 
18490Sstevel@tonic-gate 	if (lfd == NULL) {
18500Sstevel@tonic-gate 		errmsg = dlerror();
18510Sstevel@tonic-gate 		__pam_log(LOG_AUTH | LOG_ERR, "open_module[%d:%s]: %s "
18520Sstevel@tonic-gate 		    "failed: %s", pamh->include_depth, pam_trace_cname(pamh),
18530Sstevel@tonic-gate 		    module_so, errmsg != NULL ? errmsg : "Unknown error");
18540Sstevel@tonic-gate 		return (NULL);
18550Sstevel@tonic-gate 	} else {
18560Sstevel@tonic-gate 		/* add this fd to the pam handle */
18574758Sgww 		if ((module_fds = calloc(1, sizeof (fd_list))) == 0) {
18580Sstevel@tonic-gate 			(void) dlclose(lfd);
18590Sstevel@tonic-gate 			lfd = 0;
18600Sstevel@tonic-gate 			return (NULL);
18610Sstevel@tonic-gate 		}
18620Sstevel@tonic-gate 		module_fds->mh = lfd;
18630Sstevel@tonic-gate 
18640Sstevel@tonic-gate 		if (pamh->fd == 0) {
18650Sstevel@tonic-gate 			/* adding new head of list */
18660Sstevel@tonic-gate 			pamh->fd = module_fds;
18670Sstevel@tonic-gate 		} else {
18680Sstevel@tonic-gate 			/* appending to end of list */
18690Sstevel@tonic-gate 			traverse = pamh->fd;
18700Sstevel@tonic-gate 			while (traverse) {
18710Sstevel@tonic-gate 				trail = traverse;
18720Sstevel@tonic-gate 				traverse = traverse->next;
18730Sstevel@tonic-gate 			}
18740Sstevel@tonic-gate 			trail->next = module_fds;
18750Sstevel@tonic-gate 		}
18760Sstevel@tonic-gate 	}
18770Sstevel@tonic-gate 
18780Sstevel@tonic-gate 	return (lfd);
18790Sstevel@tonic-gate }
18800Sstevel@tonic-gate 
18810Sstevel@tonic-gate /*
18820Sstevel@tonic-gate  * load_function - call dlsym() to resolve the function address
18830Sstevel@tonic-gate  */
18840Sstevel@tonic-gate static int
load_function(void * lfd,char * name,int (** func)())18850Sstevel@tonic-gate load_function(void *lfd, char *name, int (**func)())
18860Sstevel@tonic-gate {
18870Sstevel@tonic-gate 	char *errmsg = NULL;
18880Sstevel@tonic-gate 
18890Sstevel@tonic-gate 	if (lfd == NULL)
18900Sstevel@tonic-gate 		return (PAM_SYMBOL_ERR);
18910Sstevel@tonic-gate 
18920Sstevel@tonic-gate 	*func = (int (*)())dlsym(lfd, name);
18930Sstevel@tonic-gate 	if (*func == NULL) {
18940Sstevel@tonic-gate 		errmsg = dlerror();
18950Sstevel@tonic-gate 		__pam_log(LOG_AUTH | LOG_ERR, "dlsym failed %s: error %s",
18960Sstevel@tonic-gate 		    name, errmsg != NULL ? errmsg : "Unknown error");
18970Sstevel@tonic-gate 		return (PAM_SYMBOL_ERR);
18980Sstevel@tonic-gate 	}
18990Sstevel@tonic-gate 
19000Sstevel@tonic-gate 	pam_trace(PAM_DEBUG_DEFAULT,
19010Sstevel@tonic-gate 	    "load_function: successful load of %s", name);
19020Sstevel@tonic-gate 	return (PAM_SUCCESS);
19030Sstevel@tonic-gate }
19040Sstevel@tonic-gate 
19050Sstevel@tonic-gate /*
19060Sstevel@tonic-gate  * Routines to read the pam.conf configuration file
19070Sstevel@tonic-gate  */
19080Sstevel@tonic-gate 
19090Sstevel@tonic-gate /*
19100Sstevel@tonic-gate  * open_pam_conf - open the pam.conf config file
19110Sstevel@tonic-gate  */
19120Sstevel@tonic-gate 
19130Sstevel@tonic-gate static int
open_pam_conf(struct pam_fh ** pam_fh,pam_handle_t * pamh,char * config)19140Sstevel@tonic-gate open_pam_conf(struct pam_fh **pam_fh, pam_handle_t *pamh, char *config)
19150Sstevel@tonic-gate {
19160Sstevel@tonic-gate 	struct stat64	stb;
19170Sstevel@tonic-gate 	int		fd;
19180Sstevel@tonic-gate 
19190Sstevel@tonic-gate 	if ((fd = open(config, O_RDONLY)) == -1) {
19200Sstevel@tonic-gate 		__pam_log(LOG_AUTH | LOG_ALERT,
19210Sstevel@tonic-gate 		    "open_pam_conf[%d:%s]: open(%s) failed: %s",
19220Sstevel@tonic-gate 		    pamh->include_depth, pam_trace_cname(pamh), config,
19230Sstevel@tonic-gate 		    strerror(errno));
19240Sstevel@tonic-gate 		return (0);
19250Sstevel@tonic-gate 	}
19260Sstevel@tonic-gate 	/* Check the ownership and file modes */
19270Sstevel@tonic-gate 	if (fstat64(fd, &stb) < 0) {
19280Sstevel@tonic-gate 		__pam_log(LOG_AUTH | LOG_ALERT,
19290Sstevel@tonic-gate 		    "open_pam_conf[%d:%s]: stat(%s) failed: %s",
19300Sstevel@tonic-gate 		    pamh->include_depth, pam_trace_cname(pamh), config,
19310Sstevel@tonic-gate 		    strerror(errno));
19320Sstevel@tonic-gate 		(void) close(fd);
19330Sstevel@tonic-gate 		return (0);
19340Sstevel@tonic-gate 	}
19350Sstevel@tonic-gate 	if (stb.st_uid != (uid_t)0) {
19360Sstevel@tonic-gate 		__pam_log(LOG_AUTH | LOG_ALERT,
19370Sstevel@tonic-gate 		    "open_pam_conf[%d:%s]: Owner of %s is not root",
19380Sstevel@tonic-gate 		    pamh->include_depth, pam_trace_cname(pamh), config);
19390Sstevel@tonic-gate 		(void) close(fd);
19400Sstevel@tonic-gate 		return (0);
19410Sstevel@tonic-gate 	}
19420Sstevel@tonic-gate 	if (stb.st_mode & S_IWGRP) {
19430Sstevel@tonic-gate 		__pam_log(LOG_AUTH | LOG_ALERT,
19440Sstevel@tonic-gate 		    "open_pam_conf[%d:%s]: %s writable by group",
19450Sstevel@tonic-gate 		    pamh->include_depth, pam_trace_cname(pamh), config);
19460Sstevel@tonic-gate 		(void) close(fd);
19470Sstevel@tonic-gate 		return (0);
19480Sstevel@tonic-gate 	}
19490Sstevel@tonic-gate 	if (stb.st_mode & S_IWOTH) {
19500Sstevel@tonic-gate 		__pam_log(LOG_AUTH | LOG_ALERT,
19510Sstevel@tonic-gate 		    "open_pam_conf[%d:%s]: %s writable by world",
19520Sstevel@tonic-gate 		    pamh->include_depth, pam_trace_cname(pamh), config);
19530Sstevel@tonic-gate 		(void) close(fd);
19540Sstevel@tonic-gate 		return (0);
19550Sstevel@tonic-gate 	}
19560Sstevel@tonic-gate 	if ((*pam_fh = calloc(1, sizeof (struct pam_fh))) == NULL) {
19570Sstevel@tonic-gate 		(void) close(fd);
19580Sstevel@tonic-gate 		return (0);
19590Sstevel@tonic-gate 	}
19600Sstevel@tonic-gate 	(*pam_fh)->fconfig = fd;
19610Sstevel@tonic-gate 	(*pam_fh)->bufsize = (size_t)stb.st_size;
19620Sstevel@tonic-gate 	if (((*pam_fh)->data = mmap(0, (*pam_fh)->bufsize, PROT_READ,
19630Sstevel@tonic-gate 	    MAP_PRIVATE, (*pam_fh)->fconfig, 0)) == MAP_FAILED) {
19640Sstevel@tonic-gate 		(void) close(fd);
19650Sstevel@tonic-gate 		free (*pam_fh);
19660Sstevel@tonic-gate 		return (0);
19670Sstevel@tonic-gate 	}
19680Sstevel@tonic-gate 	(*pam_fh)->bufferp = (*pam_fh)->data;
19690Sstevel@tonic-gate 
19700Sstevel@tonic-gate 	return (1);
19710Sstevel@tonic-gate }
19720Sstevel@tonic-gate 
19730Sstevel@tonic-gate /*
19740Sstevel@tonic-gate  * close_pam_conf - close pam.conf
19750Sstevel@tonic-gate  */
19760Sstevel@tonic-gate 
19770Sstevel@tonic-gate static void
close_pam_conf(struct pam_fh * pam_fh)19780Sstevel@tonic-gate close_pam_conf(struct pam_fh *pam_fh)
19790Sstevel@tonic-gate {
19800Sstevel@tonic-gate 	(void) munmap(pam_fh->data, pam_fh->bufsize);
19810Sstevel@tonic-gate 	(void) close(pam_fh->fconfig);
19820Sstevel@tonic-gate 	free(pam_fh);
19830Sstevel@tonic-gate }
19840Sstevel@tonic-gate 
19850Sstevel@tonic-gate /*
19860Sstevel@tonic-gate  * read_pam_conf - read in each entry in pam.conf and store info
19870Sstevel@tonic-gate  *		   under the pam handle.
19880Sstevel@tonic-gate  */
19890Sstevel@tonic-gate 
19900Sstevel@tonic-gate static int
read_pam_conf(pam_handle_t * pamh,char * config)19910Sstevel@tonic-gate read_pam_conf(pam_handle_t *pamh, char *config)
19920Sstevel@tonic-gate {
19930Sstevel@tonic-gate 	struct pam_fh	*pam_fh;
19940Sstevel@tonic-gate 	pamtab_t	*pamentp;
19950Sstevel@tonic-gate 	pamtab_t	*tpament;
19960Sstevel@tonic-gate 	char		*service;
19970Sstevel@tonic-gate 	int		error;
19980Sstevel@tonic-gate 	int		i = pamh->include_depth;	/* include depth */
19990Sstevel@tonic-gate 	/*
20000Sstevel@tonic-gate 	 * service types:
20010Sstevel@tonic-gate 	 * error (-1), "auth" (0), "account" (1), "session" (2), "password" (3)
20020Sstevel@tonic-gate 	 */
20030Sstevel@tonic-gate 	int service_found[PAM_NUM_MODULE_TYPES+1] = {0, 0, 0, 0, 0};
20040Sstevel@tonic-gate 
20050Sstevel@tonic-gate 	(void) pam_get_item(pamh, PAM_SERVICE, (void **)&service);
20060Sstevel@tonic-gate 	if (service == NULL || *service == '\0') {
20070Sstevel@tonic-gate 		__pam_log(LOG_AUTH | LOG_ERR, "No service name");
20080Sstevel@tonic-gate 		return (PAM_SYSTEM_ERR);
20090Sstevel@tonic-gate 	}
20100Sstevel@tonic-gate 
20110Sstevel@tonic-gate 	pamh->pam_conf_name[i] = strdup(config);
20120Sstevel@tonic-gate 	pam_trace(PAM_DEBUG_CONF, "read_pam_conf[%d:%s](%p) open(%s)",
20130Sstevel@tonic-gate 	    i, pam_trace_cname(pamh), (void *)pamh, config);
20142295Sgww 	if (open_pam_conf(&pam_fh, pamh, config) == 0) {
20150Sstevel@tonic-gate 		return (PAM_SYSTEM_ERR);
20162295Sgww 	}
20170Sstevel@tonic-gate 
20180Sstevel@tonic-gate 	while ((error =
20190Sstevel@tonic-gate 	    get_pam_conf_entry(pam_fh, pamh, &pamentp)) == PAM_SUCCESS &&
20200Sstevel@tonic-gate 	    pamentp) {
20210Sstevel@tonic-gate 
20220Sstevel@tonic-gate 		/* See if entry is this service and valid */
20230Sstevel@tonic-gate 		if (verify_pam_conf(pamentp, service)) {
20240Sstevel@tonic-gate 			pam_trace(PAM_DEBUG_CONF,
20250Sstevel@tonic-gate 			    "read_pam_conf[%d:%s](%p): bad entry error %s",
20260Sstevel@tonic-gate 			    i, pam_trace_cname(pamh), (void *)pamh, service);
20270Sstevel@tonic-gate 
20280Sstevel@tonic-gate 			error = PAM_SYSTEM_ERR;
20290Sstevel@tonic-gate 			free_pamconf(pamentp);
20300Sstevel@tonic-gate 			goto out;
20310Sstevel@tonic-gate 		}
20320Sstevel@tonic-gate 		if (strcasecmp(pamentp->pam_service, service) == 0) {
20330Sstevel@tonic-gate 			pam_trace(PAM_DEBUG_CONF,
20340Sstevel@tonic-gate 			    "read_pam_conf[%d:%s](%p): processing %s",
20350Sstevel@tonic-gate 			    i, pam_trace_cname(pamh), (void *)pamh, service);
20360Sstevel@tonic-gate 			/* process first service entry */
20370Sstevel@tonic-gate 			if (service_found[pamentp->pam_type + 1] == 0) {
20380Sstevel@tonic-gate 				/* purge "other" entries */
20390Sstevel@tonic-gate 				while ((tpament = pamh->pam_conf_info[i]
20400Sstevel@tonic-gate 				    [pamentp->pam_type]) != NULL) {
20410Sstevel@tonic-gate 					pam_trace(PAM_DEBUG_CONF,
20420Sstevel@tonic-gate 					    "read_pam_conf(%p): purging "
20430Sstevel@tonic-gate 					    "\"other\"[%d:%s][%s]",
20440Sstevel@tonic-gate 					    (void *)pamh, i,
20450Sstevel@tonic-gate 					    pam_trace_cname(pamh),
20460Sstevel@tonic-gate 					    pam_snames[pamentp->pam_type]);
20470Sstevel@tonic-gate 					pamh->pam_conf_info[i]
20480Sstevel@tonic-gate 					    [pamentp->pam_type] = tpament->next;
20490Sstevel@tonic-gate 					free_pamconf(tpament);
20500Sstevel@tonic-gate 				}
20510Sstevel@tonic-gate 				/* add first service entry */
20520Sstevel@tonic-gate 				pam_trace(PAM_DEBUG_CONF,
20530Sstevel@tonic-gate 				    "read_pam_conf(%p): adding 1st "
20540Sstevel@tonic-gate 				    "%s[%d:%s][%s]",
20550Sstevel@tonic-gate 				    (void *)pamh, service, i,
20560Sstevel@tonic-gate 				    pam_trace_cname(pamh),
20570Sstevel@tonic-gate 				    pam_snames[pamentp->pam_type]);
20580Sstevel@tonic-gate 				pamh->pam_conf_info[i][pamentp->pam_type] =
20590Sstevel@tonic-gate 				    pamentp;
20600Sstevel@tonic-gate 				service_found[pamentp->pam_type + 1] = 1;
20610Sstevel@tonic-gate 			} else {
20620Sstevel@tonic-gate 				/* append more service entries */
20630Sstevel@tonic-gate 				pam_trace(PAM_DEBUG_CONF,
20640Sstevel@tonic-gate 				    "read_pam_conf(%p): adding more "
20650Sstevel@tonic-gate 				    "%s[%d:%s][%s]",
20660Sstevel@tonic-gate 				    (void *)pamh, service, i,
20670Sstevel@tonic-gate 				    pam_trace_cname(pamh),
20680Sstevel@tonic-gate 				    pam_snames[pamentp->pam_type]);
20690Sstevel@tonic-gate 				tpament =
20700Sstevel@tonic-gate 				    pamh->pam_conf_info[i][pamentp->pam_type];
20710Sstevel@tonic-gate 				while (tpament->next != NULL) {
20720Sstevel@tonic-gate 					tpament = tpament->next;
20730Sstevel@tonic-gate 				}
20740Sstevel@tonic-gate 				tpament->next = pamentp;
20750Sstevel@tonic-gate 			}
20760Sstevel@tonic-gate 		} else if (service_found[pamentp->pam_type + 1] == 0) {
20770Sstevel@tonic-gate 			/* See if "other" entry available and valid */
20780Sstevel@tonic-gate 			if (verify_pam_conf(pamentp, "other")) {
20790Sstevel@tonic-gate 				pam_trace(PAM_DEBUG_CONF,
20800Sstevel@tonic-gate 				    "read_pam_conf(%p): bad entry error %s "
20810Sstevel@tonic-gate 				    "\"other\"[%d:%s]",
20820Sstevel@tonic-gate 				    (void *)pamh, service, i,
20830Sstevel@tonic-gate 				    pam_trace_cname(pamh));
20840Sstevel@tonic-gate 				error = PAM_SYSTEM_ERR;
20850Sstevel@tonic-gate 				free_pamconf(pamentp);
20860Sstevel@tonic-gate 				goto out;
20870Sstevel@tonic-gate 			}
20880Sstevel@tonic-gate 			if (strcasecmp(pamentp->pam_service, "other") == 0) {
20890Sstevel@tonic-gate 				pam_trace(PAM_DEBUG_CONF,
20900Sstevel@tonic-gate 				    "read_pam_conf(%p): processing "
20910Sstevel@tonic-gate 				    "\"other\"[%d:%s]", (void *)pamh, i,
20920Sstevel@tonic-gate 				    pam_trace_cname(pamh));
20930Sstevel@tonic-gate 				if ((tpament = pamh->pam_conf_info[i]
20940Sstevel@tonic-gate 				    [pamentp->pam_type]) == NULL) {
20950Sstevel@tonic-gate 					/* add first "other" entry */
20960Sstevel@tonic-gate 					pam_trace(PAM_DEBUG_CONF,
20970Sstevel@tonic-gate 					    "read_pam_conf(%p): adding 1st "
20980Sstevel@tonic-gate 					    "other[%d:%s][%s]", (void *)pamh, i,
20990Sstevel@tonic-gate 					    pam_trace_cname(pamh),
21000Sstevel@tonic-gate 					    pam_snames[pamentp->pam_type]);
21010Sstevel@tonic-gate 					pamh->pam_conf_info[i]
21020Sstevel@tonic-gate 					    [pamentp->pam_type] = pamentp;
21030Sstevel@tonic-gate 				} else {
21040Sstevel@tonic-gate 					/* append more "other" entries */
21050Sstevel@tonic-gate 					pam_trace(PAM_DEBUG_CONF,
21060Sstevel@tonic-gate 					    "read_pam_conf(%p): adding more "
21070Sstevel@tonic-gate 					    "other[%d:%s][%s]", (void *)pamh, i,
21080Sstevel@tonic-gate 					    pam_trace_cname(pamh),
21090Sstevel@tonic-gate 					    pam_snames[pamentp->pam_type]);
21100Sstevel@tonic-gate 					while (tpament->next != NULL) {
21110Sstevel@tonic-gate 						tpament = tpament->next;
21120Sstevel@tonic-gate 					}
21130Sstevel@tonic-gate 					tpament->next = pamentp;
21140Sstevel@tonic-gate 				}
21150Sstevel@tonic-gate 			} else {
21163517Smp204432 				/* irrelevant entry */
21170Sstevel@tonic-gate 				free_pamconf(pamentp);
21180Sstevel@tonic-gate 			}
21190Sstevel@tonic-gate 		} else {
21203517Smp204432 			/* irrelevant entry */
21210Sstevel@tonic-gate 			free_pamconf(pamentp);
21220Sstevel@tonic-gate 		}
21230Sstevel@tonic-gate 	}
21240Sstevel@tonic-gate out:
21250Sstevel@tonic-gate 	(void) close_pam_conf(pam_fh);
21260Sstevel@tonic-gate 	if (error != PAM_SUCCESS)
21270Sstevel@tonic-gate 		free_pam_conf_info(pamh);
21280Sstevel@tonic-gate 	return (error);
21290Sstevel@tonic-gate }
21300Sstevel@tonic-gate 
21310Sstevel@tonic-gate /*
21320Sstevel@tonic-gate  * get_pam_conf_entry - get a pam.conf entry
21330Sstevel@tonic-gate  */
21340Sstevel@tonic-gate 
21350Sstevel@tonic-gate static int
get_pam_conf_entry(struct pam_fh * pam_fh,pam_handle_t * pamh,pamtab_t ** pam)21360Sstevel@tonic-gate get_pam_conf_entry(struct pam_fh *pam_fh, pam_handle_t *pamh, pamtab_t **pam)
21370Sstevel@tonic-gate {
21380Sstevel@tonic-gate 	char		*cp, *arg;
21390Sstevel@tonic-gate 	int		argc;
21400Sstevel@tonic-gate 	char		*tmp, *tmp_free;
21410Sstevel@tonic-gate 	int		i;
21420Sstevel@tonic-gate 	char		*current_line = NULL;
21430Sstevel@tonic-gate 	int		error = PAM_SYSTEM_ERR;	/* preset to error */
21442295Sgww 	int		err;
21450Sstevel@tonic-gate 
21460Sstevel@tonic-gate 	/* get the next line from pam.conf */
21472295Sgww 	if ((cp = nextline(pam_fh, pamh, &err)) == NULL) {
21480Sstevel@tonic-gate 		/* no more lines in pam.conf ==> return */
21490Sstevel@tonic-gate 		error = PAM_SUCCESS;
21500Sstevel@tonic-gate 		*pam = NULL;
21510Sstevel@tonic-gate 		goto out;
21520Sstevel@tonic-gate 	}
21530Sstevel@tonic-gate 
21544758Sgww 	if ((*pam = calloc(1, sizeof (pamtab_t))) == NULL) {
21550Sstevel@tonic-gate 		__pam_log(LOG_AUTH | LOG_ERR, "strdup: out of memory");
21560Sstevel@tonic-gate 		goto out;
21570Sstevel@tonic-gate 	}
21580Sstevel@tonic-gate 
21590Sstevel@tonic-gate 	/* copy full line for error reporting */
21600Sstevel@tonic-gate 	if ((current_line = strdup(cp)) == NULL) {
21610Sstevel@tonic-gate 		__pam_log(LOG_AUTH | LOG_ERR, "strdup: out of memory");
21620Sstevel@tonic-gate 		goto out;
21630Sstevel@tonic-gate 	}
21640Sstevel@tonic-gate 
21650Sstevel@tonic-gate 	pam_trace(PAM_DEBUG_CONF,
21660Sstevel@tonic-gate 	    "pam.conf[%s] entry:\t%s", pam_trace_cname(pamh), current_line);
21670Sstevel@tonic-gate 
21680Sstevel@tonic-gate 	/* get service name (e.g. login, su, passwd) */
21690Sstevel@tonic-gate 	if ((arg = read_next_token(&cp)) == 0) {
21700Sstevel@tonic-gate 		__pam_log(LOG_AUTH | LOG_CRIT,
21710Sstevel@tonic-gate 		    "illegal pam.conf[%s] entry: %s: missing SERVICE NAME",
21720Sstevel@tonic-gate 		    pam_trace_cname(pamh), current_line);
21730Sstevel@tonic-gate 		goto out;
21740Sstevel@tonic-gate 	}
21750Sstevel@tonic-gate 	if (((*pam)->pam_service = strdup(arg)) == 0) {
21760Sstevel@tonic-gate 		__pam_log(LOG_AUTH | LOG_ERR, "strdup: out of memory");
21770Sstevel@tonic-gate 		goto out;
21780Sstevel@tonic-gate 	}
21790Sstevel@tonic-gate 
21800Sstevel@tonic-gate 	/* get module type (e.g. authentication, acct mgmt) */
21810Sstevel@tonic-gate 	if ((arg = read_next_token(&cp)) == 0) {
21820Sstevel@tonic-gate 		__pam_log(LOG_AUTH | LOG_CRIT,
21830Sstevel@tonic-gate 		    "illegal pam.conf[%s] entry: %s: missing MODULE TYPE",
21840Sstevel@tonic-gate 		    pam_trace_cname(pamh), current_line);
21850Sstevel@tonic-gate 		(*pam)->pam_type = -1;	/* 0 is a valid value */
21860Sstevel@tonic-gate 		goto getflag;
21870Sstevel@tonic-gate 	}
21880Sstevel@tonic-gate 	if (strcasecmp(arg, PAM_AUTH_NAME) == 0) {
21890Sstevel@tonic-gate 		(*pam)->pam_type = PAM_AUTH_MODULE;
21900Sstevel@tonic-gate 	} else if (strcasecmp(arg, PAM_ACCOUNT_NAME) == 0) {
21910Sstevel@tonic-gate 		(*pam)->pam_type = PAM_ACCOUNT_MODULE;
21920Sstevel@tonic-gate 	} else if (strcasecmp(arg, PAM_SESSION_NAME) == 0) {
21930Sstevel@tonic-gate 		(*pam)->pam_type = PAM_SESSION_MODULE;
21940Sstevel@tonic-gate 	} else if (strcasecmp(arg, PAM_PASSWORD_NAME) == 0) {
21950Sstevel@tonic-gate 		(*pam)->pam_type = PAM_PASSWORD_MODULE;
21960Sstevel@tonic-gate 	} else {
21970Sstevel@tonic-gate 		/* error */
21980Sstevel@tonic-gate 		__pam_log(LOG_AUTH | LOG_CRIT,
21990Sstevel@tonic-gate 		    "illegal pam.conf[%s] entry: %s: invalid module "
22000Sstevel@tonic-gate 		    "type: %s", pam_trace_cname(pamh), current_line, arg);
22010Sstevel@tonic-gate 		(*pam)->pam_type = -1;	/* 0 is a valid value */
22020Sstevel@tonic-gate 	}
22030Sstevel@tonic-gate 
22040Sstevel@tonic-gate getflag:
22050Sstevel@tonic-gate 	/* get pam flag (e.g., requisite, required, sufficient, optional) */
22060Sstevel@tonic-gate 	if ((arg = read_next_token(&cp)) == 0) {
22070Sstevel@tonic-gate 		__pam_log(LOG_AUTH | LOG_CRIT,
22080Sstevel@tonic-gate 		    "illegal pam.conf[%s] entry: %s: missing CONTROL FLAG",
22090Sstevel@tonic-gate 		    pam_trace_cname(pamh), current_line);
22100Sstevel@tonic-gate 		goto getpath;
22110Sstevel@tonic-gate 	}
22120Sstevel@tonic-gate 	if (strcasecmp(arg, PAM_BINDING_NAME) == 0) {
22130Sstevel@tonic-gate 		(*pam)->pam_flag = PAM_BINDING;
22140Sstevel@tonic-gate 	} else if (strcasecmp(arg, PAM_INCLUDE_NAME) == 0) {
22150Sstevel@tonic-gate 		(*pam)->pam_flag = PAM_INCLUDE;
22160Sstevel@tonic-gate 	} else if (strcasecmp(arg, PAM_OPTIONAL_NAME) == 0) {
22170Sstevel@tonic-gate 		(*pam)->pam_flag = PAM_OPTIONAL;
22180Sstevel@tonic-gate 	} else if (strcasecmp(arg, PAM_REQUIRED_NAME) == 0) {
22190Sstevel@tonic-gate 		(*pam)->pam_flag = PAM_REQUIRED;
22200Sstevel@tonic-gate 	} else if (strcasecmp(arg, PAM_REQUISITE_NAME) == 0) {
22210Sstevel@tonic-gate 		(*pam)->pam_flag = PAM_REQUISITE;
22220Sstevel@tonic-gate 	} else if (strcasecmp(arg, PAM_SUFFICIENT_NAME) == 0) {
22230Sstevel@tonic-gate 		(*pam)->pam_flag = PAM_SUFFICIENT;
22240Sstevel@tonic-gate 	} else {
22250Sstevel@tonic-gate 		/* error */
22260Sstevel@tonic-gate 		__pam_log(LOG_AUTH | LOG_CRIT,
22270Sstevel@tonic-gate 		    "illegal pam.conf[%s] entry: %s",
22280Sstevel@tonic-gate 		    pam_trace_cname(pamh), current_line);
22290Sstevel@tonic-gate 		__pam_log(LOG_AUTH | LOG_CRIT,
22300Sstevel@tonic-gate 		    "\tinvalid control flag: %s", arg);
22310Sstevel@tonic-gate 	}
22320Sstevel@tonic-gate 
22330Sstevel@tonic-gate getpath:
22340Sstevel@tonic-gate 	/* get module path (e.g. /usr/lib/security/pam_unix_auth.so.1) */
22350Sstevel@tonic-gate 	if ((arg = read_next_token(&cp)) == 0) {
22360Sstevel@tonic-gate 		__pam_log(LOG_AUTH | LOG_CRIT,
22370Sstevel@tonic-gate 		    "illegal pam.conf[%s] entry: %s: missing MODULE PATH",
22380Sstevel@tonic-gate 		    pam_trace_cname(pamh), current_line);
22390Sstevel@tonic-gate 		error = PAM_SUCCESS;	/* success */
22400Sstevel@tonic-gate 		goto out;
22410Sstevel@tonic-gate 	}
22420Sstevel@tonic-gate 	if (arg[0] != '/') {
22430Sstevel@tonic-gate 		size_t len;
22440Sstevel@tonic-gate 		/*
22450Sstevel@tonic-gate 		 * If module path does not start with "/", then
22460Sstevel@tonic-gate 		 * prepend PAM_LIB_DIR (/usr/lib/security/).
22470Sstevel@tonic-gate 		 */
22480Sstevel@tonic-gate 		/* sizeof (PAM_LIB_DIR) has room for '\0' */
22490Sstevel@tonic-gate 		len = sizeof (PAM_LIB_DIR) + sizeof (PAM_ISA_DIR) + strlen(arg);
22500Sstevel@tonic-gate 		if (((*pam)->module_path = malloc(len)) == NULL) {
22510Sstevel@tonic-gate 			__pam_log(LOG_AUTH | LOG_ERR, "strdup: out of memory");
22520Sstevel@tonic-gate 			goto out;
22530Sstevel@tonic-gate 		}
22540Sstevel@tonic-gate 		if ((*pam)->pam_flag & PAM_INCLUDE) {
22550Sstevel@tonic-gate 			(void) snprintf((*pam)->module_path, len, "%s%s",
22560Sstevel@tonic-gate 			    PAM_LIB_DIR, arg);
22570Sstevel@tonic-gate 		} else {
22580Sstevel@tonic-gate 			(void) snprintf((*pam)->module_path, len, "%s%s%s",
22590Sstevel@tonic-gate 			    PAM_LIB_DIR, PAM_ISA_DIR, arg);
22600Sstevel@tonic-gate 		}
22610Sstevel@tonic-gate 	} else {
22620Sstevel@tonic-gate 		/* Full path provided for module */
22630Sstevel@tonic-gate 		char *isa;
22640Sstevel@tonic-gate 
22650Sstevel@tonic-gate 		/* Check for Instruction Set Architecture indicator */
22660Sstevel@tonic-gate 		if ((isa = strstr(arg, PAM_ISA)) != NULL) {
22670Sstevel@tonic-gate 			size_t len;
22680Sstevel@tonic-gate 			len = strlen(arg) - (sizeof (PAM_ISA)-1) +
22690Sstevel@tonic-gate 			    sizeof (PAM_ISA_DIR);
22700Sstevel@tonic-gate 
22710Sstevel@tonic-gate 			/* substitute the architecture dependent path */
22720Sstevel@tonic-gate 			if (((*pam)->module_path = malloc(len)) == NULL) {
22730Sstevel@tonic-gate 				__pam_log(LOG_AUTH | LOG_ERR,
22740Sstevel@tonic-gate 				    "strdup: out of memory");
22750Sstevel@tonic-gate 				goto out;
22760Sstevel@tonic-gate 			}
22770Sstevel@tonic-gate 			*isa = '\000';
22780Sstevel@tonic-gate 			isa += strlen(PAM_ISA);
22790Sstevel@tonic-gate 			(void) snprintf((*pam)->module_path, len, "%s%s%s",
22800Sstevel@tonic-gate 			    arg, PAM_ISA_DIR, isa);
22810Sstevel@tonic-gate 		} else if (((*pam)->module_path = strdup(arg)) == 0) {
22820Sstevel@tonic-gate 			__pam_log(LOG_AUTH | LOG_ERR, "strdup: out of memory");
22830Sstevel@tonic-gate 			goto out;
22840Sstevel@tonic-gate 		}
22850Sstevel@tonic-gate 	}
22860Sstevel@tonic-gate 
22870Sstevel@tonic-gate 	/* count the number of module-specific options first */
22880Sstevel@tonic-gate 	argc = 0;
22890Sstevel@tonic-gate 	if ((tmp = strdup(cp)) == NULL) {
22900Sstevel@tonic-gate 		__pam_log(LOG_AUTH | LOG_ERR, "strdup: out of memory");
22910Sstevel@tonic-gate 		goto out;
22920Sstevel@tonic-gate 	}
22930Sstevel@tonic-gate 	tmp_free = tmp;
22940Sstevel@tonic-gate 	for (arg = read_next_token(&tmp); arg; arg = read_next_token(&tmp))
22950Sstevel@tonic-gate 		argc++;
22960Sstevel@tonic-gate 	free(tmp_free);
22970Sstevel@tonic-gate 
22980Sstevel@tonic-gate 	/* allocate array for the module-specific options */
22990Sstevel@tonic-gate 	if (argc > 0) {
23004758Sgww 		if (((*pam)->module_argv =
23014758Sgww 		    calloc(argc+1, sizeof (char *))) == 0) {
23020Sstevel@tonic-gate 			__pam_log(LOG_AUTH | LOG_ERR, "calloc: out of memory");
23030Sstevel@tonic-gate 			goto out;
23040Sstevel@tonic-gate 		}
23050Sstevel@tonic-gate 		i = 0;
23060Sstevel@tonic-gate 		for (arg = read_next_token(&cp); arg;
23074758Sgww 		    arg = read_next_token(&cp)) {
23080Sstevel@tonic-gate 			(*pam)->module_argv[i] = strdup(arg);
23090Sstevel@tonic-gate 			if ((*pam)->module_argv[i] == NULL) {
23100Sstevel@tonic-gate 				__pam_log(LOG_AUTH | LOG_ERR, "strdup failed");
23110Sstevel@tonic-gate 				goto out;
23120Sstevel@tonic-gate 			}
23130Sstevel@tonic-gate 			i++;
23140Sstevel@tonic-gate 		}
23150Sstevel@tonic-gate 		(*pam)->module_argv[argc] = NULL;
23160Sstevel@tonic-gate 	}
23170Sstevel@tonic-gate 	(*pam)->module_argc = argc;
23180Sstevel@tonic-gate 
23190Sstevel@tonic-gate 	error = PAM_SUCCESS;	/* success */
23202295Sgww 	(*pam)->pam_err = err;	/* was the line truncated */
23210Sstevel@tonic-gate 
23220Sstevel@tonic-gate out:
23230Sstevel@tonic-gate 	if (current_line)
23240Sstevel@tonic-gate 		free(current_line);
23250Sstevel@tonic-gate 	if (error != PAM_SUCCESS) {
23260Sstevel@tonic-gate 		/* on error free this */
23270Sstevel@tonic-gate 		if (*pam)
23280Sstevel@tonic-gate 			free_pamconf(*pam);
23290Sstevel@tonic-gate 	}
23300Sstevel@tonic-gate 	return (error);
23310Sstevel@tonic-gate }
23320Sstevel@tonic-gate 
23330Sstevel@tonic-gate 
23340Sstevel@tonic-gate /*
23350Sstevel@tonic-gate  * read_next_token - skip tab and space characters and return the next token
23360Sstevel@tonic-gate  */
23370Sstevel@tonic-gate 
23380Sstevel@tonic-gate static char *
read_next_token(char ** cpp)23390Sstevel@tonic-gate read_next_token(char **cpp)
23400Sstevel@tonic-gate {
23410Sstevel@tonic-gate 	register char *cp = *cpp;
23420Sstevel@tonic-gate 	char *start;
23430Sstevel@tonic-gate 
23440Sstevel@tonic-gate 	if (cp == (char *)0) {
23450Sstevel@tonic-gate 		*cpp = (char *)0;
23460Sstevel@tonic-gate 		return ((char *)0);
23470Sstevel@tonic-gate 	}
23480Sstevel@tonic-gate 	while (*cp == ' ' || *cp == '\t')
23490Sstevel@tonic-gate 		cp++;
23500Sstevel@tonic-gate 	if (*cp == '\0') {
23510Sstevel@tonic-gate 		*cpp = (char *)0;
23520Sstevel@tonic-gate 		return ((char *)0);
23530Sstevel@tonic-gate 	}
23540Sstevel@tonic-gate 	start = cp;
23550Sstevel@tonic-gate 	while (*cp && *cp != ' ' && *cp != '\t')
23560Sstevel@tonic-gate 		cp++;
23570Sstevel@tonic-gate 	if (*cp != '\0')
23580Sstevel@tonic-gate 		*cp++ = '\0';
23590Sstevel@tonic-gate 	*cpp = cp;
23600Sstevel@tonic-gate 	return (start);
23610Sstevel@tonic-gate }
23620Sstevel@tonic-gate 
23630Sstevel@tonic-gate static char *
pam_conf_strnchr(char * sp,int c,intptr_t count)23640Sstevel@tonic-gate pam_conf_strnchr(char *sp, int c, intptr_t count)
23650Sstevel@tonic-gate {
23660Sstevel@tonic-gate 	while (count) {
23670Sstevel@tonic-gate 		if (*sp == (char)c)
23680Sstevel@tonic-gate 			return ((char *)sp);
23690Sstevel@tonic-gate 		else {
23700Sstevel@tonic-gate 			sp++;
23710Sstevel@tonic-gate 			count--;
23720Sstevel@tonic-gate 		}
23730Sstevel@tonic-gate 	};
23740Sstevel@tonic-gate 	return (NULL);
23750Sstevel@tonic-gate }
23760Sstevel@tonic-gate 
23770Sstevel@tonic-gate /*
23780Sstevel@tonic-gate  * nextline - skip all blank lines and comments
23790Sstevel@tonic-gate  */
23800Sstevel@tonic-gate 
23810Sstevel@tonic-gate static char *
nextline(struct pam_fh * pam_fh,pam_handle_t * pamh,int * err)23822295Sgww nextline(struct pam_fh *pam_fh, pam_handle_t *pamh, int *err)
23830Sstevel@tonic-gate {
23840Sstevel@tonic-gate 	char	*ll;
23850Sstevel@tonic-gate 	int	find_a_line = 0;
23860Sstevel@tonic-gate 	char	*data = pam_fh->data;
23870Sstevel@tonic-gate 	char	*bufferp = pam_fh->bufferp;
23880Sstevel@tonic-gate 	char	*bufferendp = &data[pam_fh->bufsize];
23892295Sgww 	size_t	input_len;
23900Sstevel@tonic-gate 
23910Sstevel@tonic-gate 	/*
23920Sstevel@tonic-gate 	 * Skip the blank line, comment line
23930Sstevel@tonic-gate 	 */
23940Sstevel@tonic-gate 	while (!find_a_line) {
23950Sstevel@tonic-gate 		/* if we are at the end of the buffer, there is no next line */
23960Sstevel@tonic-gate 		if (bufferp == bufferendp)
23970Sstevel@tonic-gate 			return (NULL);
23980Sstevel@tonic-gate 
23990Sstevel@tonic-gate 		/* skip blank line */
24000Sstevel@tonic-gate 		while (*bufferp == '\n') {
24010Sstevel@tonic-gate 			/*
24020Sstevel@tonic-gate 			 * If we are at the end of the buffer, there is
24030Sstevel@tonic-gate 			 * no next line.
24040Sstevel@tonic-gate 			 */
24052295Sgww 			if (++bufferp == bufferendp) {
24060Sstevel@tonic-gate 				return (NULL);
24072295Sgww 			}
24080Sstevel@tonic-gate 			/* else we check *bufferp again */
24090Sstevel@tonic-gate 		}
24100Sstevel@tonic-gate 
24110Sstevel@tonic-gate 		/* skip comment line */
24120Sstevel@tonic-gate 		while (*bufferp == '#') {
24130Sstevel@tonic-gate 			if ((ll = pam_conf_strnchr(bufferp, '\n',
24144758Sgww 			    bufferendp - bufferp)) != NULL) {
24150Sstevel@tonic-gate 				bufferp = ll;
24162295Sgww 			} else {
24172295Sgww 				/*
24182295Sgww 				 * this comment line the last line.
24192295Sgww 				 * no next line
24202295Sgww 				 */
24212295Sgww 				return (NULL);
24220Sstevel@tonic-gate 			}
24230Sstevel@tonic-gate 
24240Sstevel@tonic-gate 			/*
24250Sstevel@tonic-gate 			 * If we are at the end of the buffer, there is
24260Sstevel@tonic-gate 			 * no next line.
24270Sstevel@tonic-gate 			 */
24282295Sgww 			if (bufferp == bufferendp) {
24290Sstevel@tonic-gate 				return (NULL);
24302295Sgww 			}
24310Sstevel@tonic-gate 		}
24320Sstevel@tonic-gate 
24332295Sgww 		if ((*bufferp != '\n') && (*bufferp != '#')) {
24340Sstevel@tonic-gate 			find_a_line = 1;
24352295Sgww 		}
24360Sstevel@tonic-gate 	}
24370Sstevel@tonic-gate 
24382295Sgww 	*err = PAM_SUCCESS;
24390Sstevel@tonic-gate 	/* now we find one line */
24400Sstevel@tonic-gate 	if ((ll = pam_conf_strnchr(bufferp, '\n', bufferendp - bufferp))
24412295Sgww 	    != NULL) {
24422295Sgww 		if ((input_len = ll - bufferp) >= sizeof (pam_fh->line)) {
24432295Sgww 			__pam_log(LOG_AUTH | LOG_ERR,
24442295Sgww 			    "nextline[%d:%s]: pam.conf line too long %.256s",
24452295Sgww 			    pamh->include_depth, pam_trace_cname(pamh),
24462295Sgww 			    bufferp);
24472295Sgww 			input_len = sizeof (pam_fh->line) - 1;
24482295Sgww 			*err = PAM_SERVICE_ERR;
24492295Sgww 		}
24502295Sgww 		(void) strncpy(pam_fh->line, bufferp, input_len);
24512295Sgww 		pam_fh->line[input_len] = '\0';
24520Sstevel@tonic-gate 		pam_fh->bufferp = ll++;
24530Sstevel@tonic-gate 	} else {
24540Sstevel@tonic-gate 		ll = bufferendp;
24552295Sgww 		if ((input_len = ll - bufferp) >= sizeof (pam_fh->line)) {
24562295Sgww 			__pam_log(LOG_AUTH | LOG_ERR,
24572295Sgww 			    "nextline[%d:%s]: pam.conf line too long %.256s",
24582295Sgww 			    pamh->include_depth, pam_trace_cname(pamh),
24592295Sgww 			    bufferp);
24602295Sgww 			input_len = sizeof (pam_fh->line) - 1;
24612295Sgww 			*err = PAM_SERVICE_ERR;
24622295Sgww 		}
24632295Sgww 		(void) strncpy(pam_fh->line, bufferp, input_len);
24642295Sgww 		pam_fh->line[input_len] = '\0';
24650Sstevel@tonic-gate 		pam_fh->bufferp = ll;
24660Sstevel@tonic-gate 	}
24670Sstevel@tonic-gate 
24680Sstevel@tonic-gate 	return (pam_fh->line);
24690Sstevel@tonic-gate }
24700Sstevel@tonic-gate 
24710Sstevel@tonic-gate /*
24720Sstevel@tonic-gate  * verify_pam_conf - verify that the pam_conf entry is filled in.
24730Sstevel@tonic-gate  *
24742295Sgww  *	True = Error if there is no service.
24752295Sgww  *	True = Error if there is a service and it matches the requested service
24762295Sgww  *		but, the type, flag, line overflow, or path is in error.
24770Sstevel@tonic-gate  */
24780Sstevel@tonic-gate 
24790Sstevel@tonic-gate static int
verify_pam_conf(pamtab_t * pam,char * service)24800Sstevel@tonic-gate verify_pam_conf(pamtab_t *pam, char *service)
24810Sstevel@tonic-gate {
24820Sstevel@tonic-gate 	return ((pam->pam_service == (char *)NULL) ||
24830Sstevel@tonic-gate 	    ((strcasecmp(pam->pam_service, service) == 0) &&
24840Sstevel@tonic-gate 	    ((pam->pam_type == -1) ||
24850Sstevel@tonic-gate 	    (pam->pam_flag == 0) ||
24862295Sgww 	    (pam->pam_err != PAM_SUCCESS) ||
24870Sstevel@tonic-gate 	    (pam->module_path == (char *)NULL))));
24880Sstevel@tonic-gate }
24890Sstevel@tonic-gate 
24900Sstevel@tonic-gate /*
24910Sstevel@tonic-gate  * Routines to free allocated storage
24920Sstevel@tonic-gate  */
24930Sstevel@tonic-gate 
24940Sstevel@tonic-gate /*
24950Sstevel@tonic-gate  * clean_up -  free allocated storage in the pam handle
24960Sstevel@tonic-gate  */
24970Sstevel@tonic-gate 
24980Sstevel@tonic-gate static void
clean_up(pam_handle_t * pamh)24990Sstevel@tonic-gate clean_up(pam_handle_t *pamh)
25000Sstevel@tonic-gate {
25010Sstevel@tonic-gate 	int i;
25020Sstevel@tonic-gate 	pam_repository_t *auth_rep;
25030Sstevel@tonic-gate 
25040Sstevel@tonic-gate 	if (pamh) {
25050Sstevel@tonic-gate 		while (pamh->include_depth >= 0) {
25060Sstevel@tonic-gate 			free_pam_conf_info(pamh);
25070Sstevel@tonic-gate 			pamh->include_depth--;
25080Sstevel@tonic-gate 		}
25090Sstevel@tonic-gate 
25100Sstevel@tonic-gate 		/* Cleanup PAM_REPOSITORY structure */
25110Sstevel@tonic-gate 		auth_rep = pamh->ps_item[PAM_REPOSITORY].pi_addr;
25120Sstevel@tonic-gate 		if (auth_rep != NULL) {
25130Sstevel@tonic-gate 			if (auth_rep->type != NULL)
25140Sstevel@tonic-gate 				free(auth_rep->type);
25150Sstevel@tonic-gate 			if (auth_rep->scope != NULL)
25160Sstevel@tonic-gate 				free(auth_rep->scope);
25170Sstevel@tonic-gate 		}
25180Sstevel@tonic-gate 
25190Sstevel@tonic-gate 		for (i = 0; i < PAM_MAX_ITEMS; i++) {
25200Sstevel@tonic-gate 			if (pamh->ps_item[i].pi_addr != NULL) {
25210Sstevel@tonic-gate 				if (i == PAM_AUTHTOK || i == PAM_OLDAUTHTOK) {
25220Sstevel@tonic-gate 					(void) memset(pamh->ps_item[i].pi_addr,
25230Sstevel@tonic-gate 					    0, pamh->ps_item[i].pi_size);
25240Sstevel@tonic-gate 				}
25250Sstevel@tonic-gate 				free(pamh->ps_item[i].pi_addr);
25260Sstevel@tonic-gate 			}
25270Sstevel@tonic-gate 		}
25280Sstevel@tonic-gate 		free(pamh);
25290Sstevel@tonic-gate 	}
25300Sstevel@tonic-gate }
25310Sstevel@tonic-gate 
25320Sstevel@tonic-gate /*
25330Sstevel@tonic-gate  * free_pamconf - free memory used to store pam.conf entry
25340Sstevel@tonic-gate  */
25350Sstevel@tonic-gate 
25360Sstevel@tonic-gate static void
free_pamconf(pamtab_t * cp)25370Sstevel@tonic-gate free_pamconf(pamtab_t *cp)
25380Sstevel@tonic-gate {
25390Sstevel@tonic-gate 	int i;
25400Sstevel@tonic-gate 
25410Sstevel@tonic-gate 	if (cp) {
25420Sstevel@tonic-gate 		if (cp->pam_service)
25430Sstevel@tonic-gate 			free(cp->pam_service);
25440Sstevel@tonic-gate 		if (cp->module_path)
25450Sstevel@tonic-gate 			free(cp->module_path);
25460Sstevel@tonic-gate 		for (i = 0; i < cp->module_argc; i++) {
25470Sstevel@tonic-gate 			if (cp->module_argv[i])
25480Sstevel@tonic-gate 				free(cp->module_argv[i]);
25490Sstevel@tonic-gate 		}
25500Sstevel@tonic-gate 		if (cp->module_argc > 0)
25510Sstevel@tonic-gate 			free(cp->module_argv);
25520Sstevel@tonic-gate 		if (cp->function_ptr)
25530Sstevel@tonic-gate 			free(cp->function_ptr);
25540Sstevel@tonic-gate 
25550Sstevel@tonic-gate 		free(cp);
25560Sstevel@tonic-gate 	}
25570Sstevel@tonic-gate }
25580Sstevel@tonic-gate 
25590Sstevel@tonic-gate /*
25600Sstevel@tonic-gate  * free_pam_conf_info - free memory used to store all pam.conf info
25610Sstevel@tonic-gate  *			under the pam handle
25620Sstevel@tonic-gate  */
25630Sstevel@tonic-gate 
25640Sstevel@tonic-gate static void
free_pam_conf_info(pam_handle_t * pamh)25650Sstevel@tonic-gate free_pam_conf_info(pam_handle_t *pamh)
25660Sstevel@tonic-gate {
25670Sstevel@tonic-gate 	pamtab_t *pamentp;
25680Sstevel@tonic-gate 	pamtab_t *pament_trail;
25690Sstevel@tonic-gate 	int i = pamh->include_depth;
25700Sstevel@tonic-gate 	int j;
25710Sstevel@tonic-gate 
25720Sstevel@tonic-gate 	for (j = 0; j < PAM_NUM_MODULE_TYPES; j++) {
25730Sstevel@tonic-gate 		pamentp = pamh->pam_conf_info[i][j];
25740Sstevel@tonic-gate 		pamh->pam_conf_info[i][j] = NULL;
25750Sstevel@tonic-gate 		pament_trail = pamentp;
25760Sstevel@tonic-gate 		while (pamentp) {
25770Sstevel@tonic-gate 			pamentp = pamentp->next;
25780Sstevel@tonic-gate 			free_pamconf(pament_trail);
25790Sstevel@tonic-gate 			pament_trail = pamentp;
25800Sstevel@tonic-gate 		}
25810Sstevel@tonic-gate 	}
25820Sstevel@tonic-gate 	if (pamh->pam_conf_name[i] != NULL) {
25830Sstevel@tonic-gate 		free(pamh->pam_conf_name[i]);
25840Sstevel@tonic-gate 		pamh->pam_conf_name[i] = NULL;
25850Sstevel@tonic-gate 	}
25860Sstevel@tonic-gate }
25870Sstevel@tonic-gate 
25880Sstevel@tonic-gate static void
free_env(env_list * pam_env)25890Sstevel@tonic-gate free_env(env_list *pam_env)
25900Sstevel@tonic-gate {
25910Sstevel@tonic-gate 	if (pam_env) {
25920Sstevel@tonic-gate 		if (pam_env->name)
25930Sstevel@tonic-gate 			free(pam_env->name);
25940Sstevel@tonic-gate 		if (pam_env->value)
25950Sstevel@tonic-gate 			free(pam_env->value);
25960Sstevel@tonic-gate 		free(pam_env);
25970Sstevel@tonic-gate 	}
25980Sstevel@tonic-gate }
25990Sstevel@tonic-gate 
26000Sstevel@tonic-gate /*
26010Sstevel@tonic-gate  *	Internal convenience functions for Solaris PAM service modules.
26020Sstevel@tonic-gate  */
26030Sstevel@tonic-gate 
26040Sstevel@tonic-gate #include <libintl.h>
26050Sstevel@tonic-gate #include <nl_types.h>
26060Sstevel@tonic-gate #include <synch.h>
26070Sstevel@tonic-gate #include <locale.h>
26080Sstevel@tonic-gate #include <thread.h>
26090Sstevel@tonic-gate 
26100Sstevel@tonic-gate typedef struct pam_msg_data {
26110Sstevel@tonic-gate 	nl_catd fd;
26120Sstevel@tonic-gate } pam_msg_data_t;
26130Sstevel@tonic-gate 
26140Sstevel@tonic-gate /*
26150Sstevel@tonic-gate  * free_resp():
26160Sstevel@tonic-gate  *	free storage for responses used in the call back "pam_conv" functions
26170Sstevel@tonic-gate  */
26180Sstevel@tonic-gate 
26190Sstevel@tonic-gate void
free_resp(int num_msg,struct pam_response * resp)26200Sstevel@tonic-gate free_resp(int num_msg, struct pam_response *resp)
26210Sstevel@tonic-gate {
26220Sstevel@tonic-gate 	int			i;
26230Sstevel@tonic-gate 	struct pam_response	*r;
26240Sstevel@tonic-gate 
26250Sstevel@tonic-gate 	if (resp) {
26260Sstevel@tonic-gate 		r = resp;
26270Sstevel@tonic-gate 		for (i = 0; i < num_msg; i++, r++) {
26280Sstevel@tonic-gate 			if (r->resp) {
26290Sstevel@tonic-gate 				/* clear before freeing -- may be a password */
26300Sstevel@tonic-gate 				bzero(r->resp, strlen(r->resp));
26310Sstevel@tonic-gate 				free(r->resp);
26320Sstevel@tonic-gate 				r->resp = NULL;
26330Sstevel@tonic-gate 			}
26340Sstevel@tonic-gate 		}
26350Sstevel@tonic-gate 		free(resp);
26360Sstevel@tonic-gate 	}
26370Sstevel@tonic-gate }
26380Sstevel@tonic-gate 
26390Sstevel@tonic-gate static int
do_conv(pam_handle_t * pamh,int msg_style,int num_msg,char messages[PAM_MAX_NUM_MSG][PAM_MAX_MSG_SIZE],void * conv_apdp,struct pam_response * ret_respp[])26400Sstevel@tonic-gate do_conv(pam_handle_t *pamh, int msg_style, int num_msg,
26410Sstevel@tonic-gate     char messages[PAM_MAX_NUM_MSG][PAM_MAX_MSG_SIZE], void *conv_apdp,
26420Sstevel@tonic-gate     struct pam_response *ret_respp[])
26430Sstevel@tonic-gate {
26440Sstevel@tonic-gate 	struct pam_message	*msg;
26450Sstevel@tonic-gate 	struct pam_message	*m;
26460Sstevel@tonic-gate 	int			i;
26470Sstevel@tonic-gate 	int			k;
26480Sstevel@tonic-gate 	int			retcode;
26490Sstevel@tonic-gate 	struct pam_conv		*pam_convp;
26500Sstevel@tonic-gate 
26510Sstevel@tonic-gate 	if ((retcode = pam_get_item(pamh, PAM_CONV,
26520Sstevel@tonic-gate 	    (void **)&pam_convp)) != PAM_SUCCESS) {
26530Sstevel@tonic-gate 		return (retcode);
26540Sstevel@tonic-gate 	}
26550Sstevel@tonic-gate 
26560Sstevel@tonic-gate 	/*
26570Sstevel@tonic-gate 	 * When pam_set_item() is called to set PAM_CONV and the
26580Sstevel@tonic-gate 	 * item is NULL, memset(pip->pi_addr, 0, size) is called.
26590Sstevel@tonic-gate 	 * So at this point, we should check whether pam_convp->conv
26600Sstevel@tonic-gate 	 * is NULL or not.
26610Sstevel@tonic-gate 	 */
26620Sstevel@tonic-gate 	if ((pam_convp == NULL) || (pam_convp->conv == NULL))
26630Sstevel@tonic-gate 		return (PAM_SYSTEM_ERR);
26640Sstevel@tonic-gate 
26650Sstevel@tonic-gate 	i = 0;
26660Sstevel@tonic-gate 	k = num_msg;
26670Sstevel@tonic-gate 
26684758Sgww 	msg = calloc(num_msg, sizeof (struct pam_message));
26690Sstevel@tonic-gate 	if (msg == NULL) {
26700Sstevel@tonic-gate 		return (PAM_BUF_ERR);
26710Sstevel@tonic-gate 	}
26720Sstevel@tonic-gate 	m = msg;
26730Sstevel@tonic-gate 
26740Sstevel@tonic-gate 	while (k--) {
26750Sstevel@tonic-gate 		/*
26760Sstevel@tonic-gate 		 * fill out the message structure to display prompt message
26770Sstevel@tonic-gate 		 */
26780Sstevel@tonic-gate 		m->msg_style = msg_style;
26790Sstevel@tonic-gate 		m->msg = messages[i];
26800Sstevel@tonic-gate 		pam_trace(PAM_DEBUG_CONV,
26810Sstevel@tonic-gate 		    "pam_conv_msg(%p:%d[%d]=%s)",
26820Sstevel@tonic-gate 		    (void *)pamh, msg_style, i, messages[i]);
26830Sstevel@tonic-gate 		m++;
26840Sstevel@tonic-gate 		i++;
26850Sstevel@tonic-gate 	}
26860Sstevel@tonic-gate 
26870Sstevel@tonic-gate 	/*
26880Sstevel@tonic-gate 	 * The UNIX pam modules always calls __pam_get_authtok() and
26890Sstevel@tonic-gate 	 * __pam_display_msg() with a NULL pointer as the conv_apdp.
26900Sstevel@tonic-gate 	 * In case the conv_apdp is NULL and the pam_convp->appdata_ptr
26910Sstevel@tonic-gate 	 * is not NULL, we should pass the pam_convp->appdata_ptr
26920Sstevel@tonic-gate 	 * to the conversation function.
26930Sstevel@tonic-gate 	 */
26940Sstevel@tonic-gate 	if (conv_apdp == NULL && pam_convp->appdata_ptr != NULL)
26950Sstevel@tonic-gate 		conv_apdp = pam_convp->appdata_ptr;
26960Sstevel@tonic-gate 
26970Sstevel@tonic-gate 	/*
26980Sstevel@tonic-gate 	 * Call conv function to display the prompt.
26990Sstevel@tonic-gate 	 */
27000Sstevel@tonic-gate 	retcode = (pam_convp->conv)(num_msg, &msg, ret_respp, conv_apdp);
27010Sstevel@tonic-gate 	pam_trace(PAM_DEBUG_CONV,
27020Sstevel@tonic-gate 	    "pam_conv_resp(%p pam_conv = %s) ret_respp = %p",
27030Sstevel@tonic-gate 	    (void *)pamh, pam_strerror(pamh, retcode), (void *)ret_respp);
27040Sstevel@tonic-gate 	if (*ret_respp == NULL) {
27050Sstevel@tonic-gate 		pam_trace(PAM_DEBUG_CONV,
27060Sstevel@tonic-gate 		    "pam_conv_resp(%p No response requested)", (void *)pamh);
27070Sstevel@tonic-gate 	} else if ((pam_debug & (PAM_DEBUG_CONV | PAM_DEBUG_AUTHTOK)) != 0) {
27080Sstevel@tonic-gate 		struct pam_response *r = *ret_respp;
27090Sstevel@tonic-gate 
27100Sstevel@tonic-gate 		for (i = 0; i < num_msg; i++, r++) {
27110Sstevel@tonic-gate 			if (r->resp == NULL) {
27120Sstevel@tonic-gate 				pam_trace(PAM_DEBUG_CONV,
27130Sstevel@tonic-gate 				    "pam_conv_resp(%p:"
27140Sstevel@tonic-gate 				    "[%d] NULL response string)",
27150Sstevel@tonic-gate 				    (void *)pamh, i);
27160Sstevel@tonic-gate 			} else {
27170Sstevel@tonic-gate 				if (msg_style == PAM_PROMPT_ECHO_OFF) {
27180Sstevel@tonic-gate #ifdef	DEBUG
27190Sstevel@tonic-gate 					pam_trace(PAM_DEBUG_AUTHTOK,
27200Sstevel@tonic-gate 					    "pam_conv_resp(%p:[%d]=%s, "
27210Sstevel@tonic-gate 					    "code=%d)",
27220Sstevel@tonic-gate 					    (void *)pamh, i, r->resp,
27230Sstevel@tonic-gate 					    r->resp_retcode);
27240Sstevel@tonic-gate #endif	/* DEBUG */
27250Sstevel@tonic-gate 					pam_trace(PAM_DEBUG_CONV,
27260Sstevel@tonic-gate 					    "pam_conv_resp(%p:[%d] len=%lu, "
27270Sstevel@tonic-gate 					    "code=%d)",
27280Sstevel@tonic-gate 					    (void *)pamh, i,
27290Sstevel@tonic-gate 					    (ulong_t)strlen(r->resp),
27300Sstevel@tonic-gate 					    r->resp_retcode);
27310Sstevel@tonic-gate 				} else {
27320Sstevel@tonic-gate 					pam_trace(PAM_DEBUG_CONV,
27330Sstevel@tonic-gate 					    "pam_conv_resp(%p:[%d]=%s, "
27340Sstevel@tonic-gate 					    "code=%d)",
27350Sstevel@tonic-gate 					    (void *)pamh, i, r->resp,
27360Sstevel@tonic-gate 					    r->resp_retcode);
27370Sstevel@tonic-gate 				}
27380Sstevel@tonic-gate 			}
27390Sstevel@tonic-gate 		}
27400Sstevel@tonic-gate 	}
27410Sstevel@tonic-gate 
27420Sstevel@tonic-gate 	if (msg)
27430Sstevel@tonic-gate 		free(msg);
27440Sstevel@tonic-gate 	return (retcode);
27450Sstevel@tonic-gate }
27460Sstevel@tonic-gate 
27470Sstevel@tonic-gate /*
27480Sstevel@tonic-gate  * __pam_display_msg():
27490Sstevel@tonic-gate  *	display message by calling the call back functions
27500Sstevel@tonic-gate  *	provided by the application through "pam_conv" structure
27510Sstevel@tonic-gate  */
27520Sstevel@tonic-gate 
27530Sstevel@tonic-gate int
__pam_display_msg(pam_handle_t * pamh,int msg_style,int num_msg,char messages[PAM_MAX_NUM_MSG][PAM_MAX_MSG_SIZE],void * conv_apdp)27540Sstevel@tonic-gate __pam_display_msg(pam_handle_t *pamh, int msg_style, int num_msg,
27550Sstevel@tonic-gate     char messages[PAM_MAX_NUM_MSG][PAM_MAX_MSG_SIZE], void *conv_apdp)
27560Sstevel@tonic-gate {
27570Sstevel@tonic-gate 	struct pam_response	*ret_respp = NULL;
27587773SPeter.Shoults@Sun.COM 	int ret;
27590Sstevel@tonic-gate 
27607773SPeter.Shoults@Sun.COM 	ret = do_conv(pamh, msg_style, num_msg, messages,
27617773SPeter.Shoults@Sun.COM 	    conv_apdp, &ret_respp);
27627773SPeter.Shoults@Sun.COM 
27637773SPeter.Shoults@Sun.COM 	if (ret_respp != NULL)
27647773SPeter.Shoults@Sun.COM 		free_resp(num_msg, ret_respp);
27657773SPeter.Shoults@Sun.COM 
27667773SPeter.Shoults@Sun.COM 	return (ret);
27670Sstevel@tonic-gate }
27680Sstevel@tonic-gate 
27690Sstevel@tonic-gate /*
27700Sstevel@tonic-gate  * __pam_get_authtok()
27710Sstevel@tonic-gate  *	retrieves a password of at most PASS_MAX length from the pam
27720Sstevel@tonic-gate  *	handle (pam_get_item) or from the input stream (do_conv).
27730Sstevel@tonic-gate  *
27740Sstevel@tonic-gate  * This function allocates memory for the new authtok.
27750Sstevel@tonic-gate  * Applications calling this function are responsible for
27760Sstevel@tonic-gate  * freeing this memory.
27770Sstevel@tonic-gate  *
27780Sstevel@tonic-gate  * If "source" is
27790Sstevel@tonic-gate  *	PAM_HANDLE
27800Sstevel@tonic-gate  * and "type" is:
27810Sstevel@tonic-gate  *	PAM_AUTHTOK - password is taken from pam handle (PAM_AUTHTOK)
27820Sstevel@tonic-gate  *	PAM_OLDAUTHTOK - password is taken from pam handle (PAM_OLDAUTHTOK)
27830Sstevel@tonic-gate  *
27840Sstevel@tonic-gate  * If "source" is
27850Sstevel@tonic-gate  *	PAM_PROMPT
27860Sstevel@tonic-gate  * and "type" is:
27870Sstevel@tonic-gate  *	0:		Prompt for new passwd, do not even attempt
27880Sstevel@tonic-gate  *			to store it in the pam handle.
27890Sstevel@tonic-gate  *	PAM_AUTHTOK:	Prompt for new passwd, store in pam handle as
27900Sstevel@tonic-gate  *			PAM_AUTHTOK item if this value is not already set.
27910Sstevel@tonic-gate  *	PAM_OLDAUTHTOK:	Prompt for new passwd, store in pam handle as
27920Sstevel@tonic-gate  *			PAM_OLDAUTHTOK item if this value is not
27930Sstevel@tonic-gate  *			already set.
27940Sstevel@tonic-gate  */
27950Sstevel@tonic-gate int
__pam_get_authtok(pam_handle_t * pamh,int source,int type,char * prompt,char ** authtok)27960Sstevel@tonic-gate __pam_get_authtok(pam_handle_t *pamh, int source, int type, char *prompt,
27970Sstevel@tonic-gate     char **authtok)
27980Sstevel@tonic-gate {
27990Sstevel@tonic-gate 	int error = PAM_SYSTEM_ERR;
28000Sstevel@tonic-gate 	char *new_password = NULL;
28010Sstevel@tonic-gate 	struct pam_response *ret_resp = NULL;
28020Sstevel@tonic-gate 	char messages[PAM_MAX_NUM_MSG][PAM_MAX_MSG_SIZE];
28030Sstevel@tonic-gate 
28040Sstevel@tonic-gate 	if ((*authtok = calloc(PASS_MAX+1, sizeof (char))) == NULL)
28050Sstevel@tonic-gate 		return (PAM_BUF_ERR);
28060Sstevel@tonic-gate 
28070Sstevel@tonic-gate 	if (prompt == NULL)
28080Sstevel@tonic-gate 		prompt = dgettext(TEXT_DOMAIN, "password: ");
28090Sstevel@tonic-gate 
28100Sstevel@tonic-gate 	switch (source) {
28110Sstevel@tonic-gate 	case PAM_HANDLE:
28120Sstevel@tonic-gate 
28130Sstevel@tonic-gate 		/* get password from pam handle item list */
28140Sstevel@tonic-gate 
28150Sstevel@tonic-gate 		switch (type) {
28160Sstevel@tonic-gate 		case PAM_AUTHTOK:
28170Sstevel@tonic-gate 		case PAM_OLDAUTHTOK:
28180Sstevel@tonic-gate 
28190Sstevel@tonic-gate 			if ((error = pam_get_item(pamh, type,
28200Sstevel@tonic-gate 			    (void **)&new_password)) != PAM_SUCCESS)
28210Sstevel@tonic-gate 				goto err_ret;
28220Sstevel@tonic-gate 
28230Sstevel@tonic-gate 			if (new_password == NULL || new_password[0] == '\0') {
28240Sstevel@tonic-gate 				free(*authtok);
28250Sstevel@tonic-gate 				*authtok = NULL;
28260Sstevel@tonic-gate 			} else {
28270Sstevel@tonic-gate 				(void) strlcpy(*authtok, new_password,
28280Sstevel@tonic-gate 				    PASS_MAX+1);
28290Sstevel@tonic-gate 			}
28300Sstevel@tonic-gate 			break;
28310Sstevel@tonic-gate 		default:
28320Sstevel@tonic-gate 			__pam_log(LOG_AUTH | LOG_ERR,
28330Sstevel@tonic-gate 			    "__pam_get_authtok() invalid type: %d", type);
28340Sstevel@tonic-gate 			error = PAM_SYMBOL_ERR;
28350Sstevel@tonic-gate 			goto err_ret;
28360Sstevel@tonic-gate 		}
28370Sstevel@tonic-gate 		break;
28380Sstevel@tonic-gate 	case PAM_PROMPT:
28390Sstevel@tonic-gate 
28400Sstevel@tonic-gate 		/*
28410Sstevel@tonic-gate 		 * Prompt for new password and save in pam handle item list
28420Sstevel@tonic-gate 		 * if the that item is not already set.
28430Sstevel@tonic-gate 		 */
28440Sstevel@tonic-gate 
28450Sstevel@tonic-gate 		(void) strncpy(messages[0], prompt, sizeof (messages[0]));
28460Sstevel@tonic-gate 		if ((error = do_conv(pamh, PAM_PROMPT_ECHO_OFF, 1, messages,
28470Sstevel@tonic-gate 		    NULL, &ret_resp)) != PAM_SUCCESS)
28480Sstevel@tonic-gate 			goto err_ret;
28490Sstevel@tonic-gate 
28500Sstevel@tonic-gate 		if (ret_resp->resp == NULL) {
28510Sstevel@tonic-gate 			/* getpass didn't return anything */
28520Sstevel@tonic-gate 			error = PAM_SYSTEM_ERR;
28530Sstevel@tonic-gate 			goto err_ret;
28540Sstevel@tonic-gate 		}
28550Sstevel@tonic-gate 
28560Sstevel@tonic-gate 		/* save the new password if this item was NULL */
28570Sstevel@tonic-gate 		if (type) {
28580Sstevel@tonic-gate 			if ((error = pam_get_item(pamh, type,
28590Sstevel@tonic-gate 			    (void **)&new_password)) != PAM_SUCCESS) {
28600Sstevel@tonic-gate 				free_resp(1, ret_resp);
28610Sstevel@tonic-gate 				goto err_ret;
28620Sstevel@tonic-gate 			}
28630Sstevel@tonic-gate 			if (new_password == NULL)
28640Sstevel@tonic-gate 				(void) pam_set_item(pamh, type, ret_resp->resp);
28650Sstevel@tonic-gate 		}
28660Sstevel@tonic-gate 
28670Sstevel@tonic-gate 		(void) strlcpy(*authtok, ret_resp->resp, PASS_MAX+1);
28680Sstevel@tonic-gate 		free_resp(1, ret_resp);
28690Sstevel@tonic-gate 		break;
28700Sstevel@tonic-gate 	default:
28710Sstevel@tonic-gate 		__pam_log(LOG_AUTH | LOG_ERR,
28720Sstevel@tonic-gate 		    "__pam_get_authtok() invalid source: %d", source);
28730Sstevel@tonic-gate 		error = PAM_SYMBOL_ERR;
28740Sstevel@tonic-gate 		goto err_ret;
28750Sstevel@tonic-gate 	}
28760Sstevel@tonic-gate 
28770Sstevel@tonic-gate 	return (PAM_SUCCESS);
28780Sstevel@tonic-gate 
28790Sstevel@tonic-gate err_ret:
28800Sstevel@tonic-gate 	bzero(*authtok, PASS_MAX+1);
28810Sstevel@tonic-gate 	free(*authtok);
28820Sstevel@tonic-gate 	*authtok = NULL;
28830Sstevel@tonic-gate 	return (error);
28840Sstevel@tonic-gate }
2885