xref: /titanic_41/usr/src/cmd/sendmail/libsm/mbdb.c (revision e9af4bc0b1cc30cea75d6ad4aa2fde97d985e9be)
17c478bd9Sstevel@tonic-gate /*
2*e9af4bc0SJohn Beck  * Copyright (c) 2001-2003,2009 Sendmail, Inc. and its suppliers.
37c478bd9Sstevel@tonic-gate  *      All rights reserved.
47c478bd9Sstevel@tonic-gate  *
57c478bd9Sstevel@tonic-gate  * By using this file, you agree to the terms and conditions set
67c478bd9Sstevel@tonic-gate  * forth in the LICENSE file which can be found at the top level of
77c478bd9Sstevel@tonic-gate  * the sendmail distribution.
87c478bd9Sstevel@tonic-gate  */
97c478bd9Sstevel@tonic-gate 
107c478bd9Sstevel@tonic-gate #include <sm/gen.h>
11*e9af4bc0SJohn Beck SM_RCSID("@(#)$Id: mbdb.c,v 1.41 2009/06/19 22:02:26 guenther Exp $")
127c478bd9Sstevel@tonic-gate 
137c478bd9Sstevel@tonic-gate #include <sys/param.h>
147c478bd9Sstevel@tonic-gate 
157c478bd9Sstevel@tonic-gate #include <ctype.h>
167c478bd9Sstevel@tonic-gate #include <errno.h>
177c478bd9Sstevel@tonic-gate #include <pwd.h>
187c478bd9Sstevel@tonic-gate #include <stdlib.h>
197c478bd9Sstevel@tonic-gate #include <setjmp.h>
207c478bd9Sstevel@tonic-gate #include <unistd.h>
217c478bd9Sstevel@tonic-gate 
227c478bd9Sstevel@tonic-gate #include <sm/limits.h>
237c478bd9Sstevel@tonic-gate #include <sm/conf.h>
247c478bd9Sstevel@tonic-gate #include <sm/assert.h>
257c478bd9Sstevel@tonic-gate #include <sm/bitops.h>
267c478bd9Sstevel@tonic-gate #include <sm/errstring.h>
277c478bd9Sstevel@tonic-gate #include <sm/heap.h>
287c478bd9Sstevel@tonic-gate #include <sm/mbdb.h>
297c478bd9Sstevel@tonic-gate #include <sm/string.h>
307c478bd9Sstevel@tonic-gate # ifdef EX_OK
317c478bd9Sstevel@tonic-gate #  undef EX_OK			/* for SVr4.2 SMP */
327c478bd9Sstevel@tonic-gate # endif /* EX_OK */
337c478bd9Sstevel@tonic-gate #include <sm/sysexits.h>
347c478bd9Sstevel@tonic-gate 
357c478bd9Sstevel@tonic-gate #if LDAPMAP
367c478bd9Sstevel@tonic-gate # if _LDAP_EXAMPLE_
377c478bd9Sstevel@tonic-gate #  include <sm/ldap.h>
387c478bd9Sstevel@tonic-gate # endif /* _LDAP_EXAMPLE_ */
397c478bd9Sstevel@tonic-gate #endif /* LDAPMAP */
407c478bd9Sstevel@tonic-gate 
417c478bd9Sstevel@tonic-gate typedef struct
427c478bd9Sstevel@tonic-gate {
437c478bd9Sstevel@tonic-gate 	char	*mbdb_typename;
447c478bd9Sstevel@tonic-gate 	int	(*mbdb_initialize) __P((char *));
457c478bd9Sstevel@tonic-gate 	int	(*mbdb_lookup) __P((char *name, SM_MBDB_T *user));
467c478bd9Sstevel@tonic-gate 	void	(*mbdb_terminate) __P((void));
477c478bd9Sstevel@tonic-gate } SM_MBDB_TYPE_T;
487c478bd9Sstevel@tonic-gate 
497c478bd9Sstevel@tonic-gate static int	mbdb_pw_initialize __P((char *));
507c478bd9Sstevel@tonic-gate static int	mbdb_pw_lookup __P((char *name, SM_MBDB_T *user));
517c478bd9Sstevel@tonic-gate static void	mbdb_pw_terminate __P((void));
527c478bd9Sstevel@tonic-gate 
537c478bd9Sstevel@tonic-gate #if LDAPMAP
547c478bd9Sstevel@tonic-gate # if _LDAP_EXAMPLE_
557c478bd9Sstevel@tonic-gate static struct sm_ldap_struct LDAPLMAP;
567c478bd9Sstevel@tonic-gate static int	mbdb_ldap_initialize __P((char *));
577c478bd9Sstevel@tonic-gate static int	mbdb_ldap_lookup __P((char *name, SM_MBDB_T *user));
587c478bd9Sstevel@tonic-gate static void	mbdb_ldap_terminate __P((void));
597c478bd9Sstevel@tonic-gate # endif /* _LDAP_EXAMPLE_ */
607c478bd9Sstevel@tonic-gate #endif /* LDAPMAP */
617c478bd9Sstevel@tonic-gate 
627c478bd9Sstevel@tonic-gate static SM_MBDB_TYPE_T SmMbdbTypes[] =
637c478bd9Sstevel@tonic-gate {
647c478bd9Sstevel@tonic-gate 	{ "pw", mbdb_pw_initialize, mbdb_pw_lookup, mbdb_pw_terminate },
657c478bd9Sstevel@tonic-gate #if LDAPMAP
667c478bd9Sstevel@tonic-gate # if _LDAP_EXAMPLE_
677c478bd9Sstevel@tonic-gate 	{ "ldap", mbdb_ldap_initialize, mbdb_ldap_lookup, mbdb_ldap_terminate },
687c478bd9Sstevel@tonic-gate # endif /* _LDAP_EXAMPLE_ */
697c478bd9Sstevel@tonic-gate #endif /* LDAPMAP */
707c478bd9Sstevel@tonic-gate 	{ NULL, NULL, NULL, NULL }
717c478bd9Sstevel@tonic-gate };
727c478bd9Sstevel@tonic-gate 
737c478bd9Sstevel@tonic-gate static SM_MBDB_TYPE_T *SmMbdbType = &SmMbdbTypes[0];
747c478bd9Sstevel@tonic-gate 
757c478bd9Sstevel@tonic-gate /*
767c478bd9Sstevel@tonic-gate **  SM_MBDB_INITIALIZE -- specify which mailbox database to use
777c478bd9Sstevel@tonic-gate **
787c478bd9Sstevel@tonic-gate **	If this function is not called, then the "pw" implementation
797c478bd9Sstevel@tonic-gate **	is used by default; this implementation uses getpwnam().
807c478bd9Sstevel@tonic-gate **
817c478bd9Sstevel@tonic-gate **	Parameters:
827c478bd9Sstevel@tonic-gate **		mbdb -- Which mailbox database to use.
837c478bd9Sstevel@tonic-gate **			The argument has the form "name" or "name.arg".
847c478bd9Sstevel@tonic-gate **			"pw" means use getpwnam().
857c478bd9Sstevel@tonic-gate **
867c478bd9Sstevel@tonic-gate **	Results:
877c478bd9Sstevel@tonic-gate **		EX_OK on success, or an EX_* code on failure.
887c478bd9Sstevel@tonic-gate */
897c478bd9Sstevel@tonic-gate 
907c478bd9Sstevel@tonic-gate int
sm_mbdb_initialize(mbdb)917c478bd9Sstevel@tonic-gate sm_mbdb_initialize(mbdb)
927c478bd9Sstevel@tonic-gate 	char *mbdb;
937c478bd9Sstevel@tonic-gate {
947c478bd9Sstevel@tonic-gate 	size_t namelen;
957c478bd9Sstevel@tonic-gate 	int err;
967c478bd9Sstevel@tonic-gate 	char *name;
977c478bd9Sstevel@tonic-gate 	char *arg;
987c478bd9Sstevel@tonic-gate 	SM_MBDB_TYPE_T *t;
997c478bd9Sstevel@tonic-gate 
1007c478bd9Sstevel@tonic-gate 	SM_REQUIRE(mbdb != NULL);
1017c478bd9Sstevel@tonic-gate 
1027c478bd9Sstevel@tonic-gate 	name = mbdb;
1037c478bd9Sstevel@tonic-gate 	arg = strchr(mbdb, '.');
1047c478bd9Sstevel@tonic-gate 	if (arg == NULL)
1057c478bd9Sstevel@tonic-gate 		namelen = strlen(name);
1067c478bd9Sstevel@tonic-gate 	else
1077c478bd9Sstevel@tonic-gate 	{
1087c478bd9Sstevel@tonic-gate 		namelen = arg - name;
1097c478bd9Sstevel@tonic-gate 		++arg;
1107c478bd9Sstevel@tonic-gate 	}
1117c478bd9Sstevel@tonic-gate 
1127c478bd9Sstevel@tonic-gate 	for (t = SmMbdbTypes; t->mbdb_typename != NULL; ++t)
1137c478bd9Sstevel@tonic-gate 	{
1147c478bd9Sstevel@tonic-gate 		if (strlen(t->mbdb_typename) == namelen &&
1157c478bd9Sstevel@tonic-gate 		    strncmp(name, t->mbdb_typename, namelen) == 0)
1167c478bd9Sstevel@tonic-gate 		{
1177c478bd9Sstevel@tonic-gate 			err = EX_OK;
1187c478bd9Sstevel@tonic-gate 			if (t->mbdb_initialize != NULL)
1197c478bd9Sstevel@tonic-gate 				err = t->mbdb_initialize(arg);
1207c478bd9Sstevel@tonic-gate 			if (err == EX_OK)
1217c478bd9Sstevel@tonic-gate 				SmMbdbType = t;
1227c478bd9Sstevel@tonic-gate 			return err;
1237c478bd9Sstevel@tonic-gate 		}
1247c478bd9Sstevel@tonic-gate 	}
1257c478bd9Sstevel@tonic-gate 	return EX_UNAVAILABLE;
1267c478bd9Sstevel@tonic-gate }
1277c478bd9Sstevel@tonic-gate 
1287c478bd9Sstevel@tonic-gate /*
1297c478bd9Sstevel@tonic-gate **  SM_MBDB_TERMINATE -- terminate connection to the mailbox database
1307c478bd9Sstevel@tonic-gate **
1317c478bd9Sstevel@tonic-gate **	Because this function closes any cached file descriptors that
1327c478bd9Sstevel@tonic-gate **	are being held open for the connection to the mailbox database,
1337c478bd9Sstevel@tonic-gate **	it should be called for security reasons prior to dropping privileges
1347c478bd9Sstevel@tonic-gate **	and execing another process.
1357c478bd9Sstevel@tonic-gate **
1367c478bd9Sstevel@tonic-gate **	Parameters:
1377c478bd9Sstevel@tonic-gate **		none.
1387c478bd9Sstevel@tonic-gate **
1397c478bd9Sstevel@tonic-gate **	Results:
1407c478bd9Sstevel@tonic-gate **		none.
1417c478bd9Sstevel@tonic-gate */
1427c478bd9Sstevel@tonic-gate 
1437c478bd9Sstevel@tonic-gate void
sm_mbdb_terminate()1447c478bd9Sstevel@tonic-gate sm_mbdb_terminate()
1457c478bd9Sstevel@tonic-gate {
1467c478bd9Sstevel@tonic-gate 	if (SmMbdbType->mbdb_terminate != NULL)
1477c478bd9Sstevel@tonic-gate 		SmMbdbType->mbdb_terminate();
1487c478bd9Sstevel@tonic-gate }
1497c478bd9Sstevel@tonic-gate 
1507c478bd9Sstevel@tonic-gate /*
1517c478bd9Sstevel@tonic-gate **  SM_MBDB_LOOKUP -- look up a local mail recipient, given name
1527c478bd9Sstevel@tonic-gate **
1537c478bd9Sstevel@tonic-gate **	Parameters:
1547c478bd9Sstevel@tonic-gate **		name -- name of local mail recipient
1557c478bd9Sstevel@tonic-gate **		user -- pointer to structure to fill in on success
1567c478bd9Sstevel@tonic-gate **
1577c478bd9Sstevel@tonic-gate **	Results:
1587c478bd9Sstevel@tonic-gate **		On success, fill in *user and return EX_OK.
1597c478bd9Sstevel@tonic-gate **		If the user does not exist, return EX_NOUSER.
1607c478bd9Sstevel@tonic-gate **		If a temporary failure (eg, a network failure) occurred,
1617c478bd9Sstevel@tonic-gate **		return EX_TEMPFAIL.  Otherwise return EX_OSERR.
1627c478bd9Sstevel@tonic-gate */
1637c478bd9Sstevel@tonic-gate 
1647c478bd9Sstevel@tonic-gate int
sm_mbdb_lookup(name,user)1657c478bd9Sstevel@tonic-gate sm_mbdb_lookup(name, user)
1667c478bd9Sstevel@tonic-gate 	char *name;
1677c478bd9Sstevel@tonic-gate 	SM_MBDB_T *user;
1687c478bd9Sstevel@tonic-gate {
1697c478bd9Sstevel@tonic-gate 	int ret = EX_NOUSER;
1707c478bd9Sstevel@tonic-gate 
1717c478bd9Sstevel@tonic-gate 	if (SmMbdbType->mbdb_lookup != NULL)
1727c478bd9Sstevel@tonic-gate 		ret = SmMbdbType->mbdb_lookup(name, user);
1737c478bd9Sstevel@tonic-gate 	return ret;
1747c478bd9Sstevel@tonic-gate }
1757c478bd9Sstevel@tonic-gate 
1767c478bd9Sstevel@tonic-gate /*
1777c478bd9Sstevel@tonic-gate **  SM_MBDB_FROMPW -- copy from struct pw to SM_MBDB_T
1787c478bd9Sstevel@tonic-gate **
1797c478bd9Sstevel@tonic-gate **	Parameters:
1807c478bd9Sstevel@tonic-gate **		user -- destination user information structure
1817c478bd9Sstevel@tonic-gate **		pw -- source passwd structure
1827c478bd9Sstevel@tonic-gate **
1837c478bd9Sstevel@tonic-gate **	Results:
1847c478bd9Sstevel@tonic-gate **		none.
1857c478bd9Sstevel@tonic-gate */
1867c478bd9Sstevel@tonic-gate 
1877c478bd9Sstevel@tonic-gate void
sm_mbdb_frompw(user,pw)1887c478bd9Sstevel@tonic-gate sm_mbdb_frompw(user, pw)
1897c478bd9Sstevel@tonic-gate 	SM_MBDB_T *user;
1907c478bd9Sstevel@tonic-gate 	struct passwd *pw;
1917c478bd9Sstevel@tonic-gate {
1927c478bd9Sstevel@tonic-gate 	SM_REQUIRE(user != NULL);
1937c478bd9Sstevel@tonic-gate 	(void) sm_strlcpy(user->mbdb_name, pw->pw_name,
1947c478bd9Sstevel@tonic-gate 			  sizeof(user->mbdb_name));
1957c478bd9Sstevel@tonic-gate 	user->mbdb_uid = pw->pw_uid;
1967c478bd9Sstevel@tonic-gate 	user->mbdb_gid = pw->pw_gid;
1977c478bd9Sstevel@tonic-gate 	sm_pwfullname(pw->pw_gecos, pw->pw_name, user->mbdb_fullname,
1987c478bd9Sstevel@tonic-gate 		      sizeof(user->mbdb_fullname));
1997c478bd9Sstevel@tonic-gate 	(void) sm_strlcpy(user->mbdb_homedir, pw->pw_dir,
2007c478bd9Sstevel@tonic-gate 			  sizeof(user->mbdb_homedir));
2017c478bd9Sstevel@tonic-gate 	(void) sm_strlcpy(user->mbdb_shell, pw->pw_shell,
2027c478bd9Sstevel@tonic-gate 			  sizeof(user->mbdb_shell));
2037c478bd9Sstevel@tonic-gate }
2047c478bd9Sstevel@tonic-gate 
2057c478bd9Sstevel@tonic-gate /*
2067c478bd9Sstevel@tonic-gate **  SM_PWFULLNAME -- build full name of user from pw_gecos field.
2077c478bd9Sstevel@tonic-gate **
2087c478bd9Sstevel@tonic-gate **	This routine interprets the strange entry that would appear
2097c478bd9Sstevel@tonic-gate **	in the GECOS field of the password file.
2107c478bd9Sstevel@tonic-gate **
2117c478bd9Sstevel@tonic-gate **	Parameters:
2127c478bd9Sstevel@tonic-gate **		gecos -- name to build.
2137c478bd9Sstevel@tonic-gate **		user -- the login name of this user (for &).
2147c478bd9Sstevel@tonic-gate **		buf -- place to put the result.
2157c478bd9Sstevel@tonic-gate **		buflen -- length of buf.
2167c478bd9Sstevel@tonic-gate **
2177c478bd9Sstevel@tonic-gate **	Returns:
2187c478bd9Sstevel@tonic-gate **		none.
2197c478bd9Sstevel@tonic-gate */
2207c478bd9Sstevel@tonic-gate 
2217c478bd9Sstevel@tonic-gate #if _FFR_HANDLE_ISO8859_GECOS
2227c478bd9Sstevel@tonic-gate static char Latin1ToASCII[128] =
2237c478bd9Sstevel@tonic-gate {
2247c478bd9Sstevel@tonic-gate 	32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32,
2257c478bd9Sstevel@tonic-gate 	32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 33,
2267c478bd9Sstevel@tonic-gate 	99, 80, 36, 89, 124, 36, 34, 99, 97, 60, 45, 45, 114, 45, 111, 42,
2277c478bd9Sstevel@tonic-gate 	50, 51, 39, 117, 80, 46, 44, 49, 111, 62, 42, 42, 42, 63, 65, 65,
2287c478bd9Sstevel@tonic-gate 	65, 65, 65, 65, 65, 67, 69, 69, 69, 69, 73, 73, 73, 73, 68, 78, 79,
2297c478bd9Sstevel@tonic-gate 	79, 79, 79, 79, 88, 79, 85, 85, 85, 85, 89, 80, 66, 97, 97, 97, 97,
2307c478bd9Sstevel@tonic-gate 	97, 97, 97, 99, 101, 101, 101, 101, 105, 105, 105, 105, 100, 110,
2317c478bd9Sstevel@tonic-gate 	111, 111, 111, 111, 111, 47, 111, 117, 117, 117, 117, 121, 112, 121
2327c478bd9Sstevel@tonic-gate };
2337c478bd9Sstevel@tonic-gate #endif /* _FFR_HANDLE_ISO8859_GECOS */
2347c478bd9Sstevel@tonic-gate 
2357c478bd9Sstevel@tonic-gate void
sm_pwfullname(gecos,user,buf,buflen)2367c478bd9Sstevel@tonic-gate sm_pwfullname(gecos, user, buf, buflen)
2377c478bd9Sstevel@tonic-gate 	register char *gecos;
2387c478bd9Sstevel@tonic-gate 	char *user;
2397c478bd9Sstevel@tonic-gate 	char *buf;
2407c478bd9Sstevel@tonic-gate 	size_t buflen;
2417c478bd9Sstevel@tonic-gate {
2427c478bd9Sstevel@tonic-gate 	register char *p;
2437c478bd9Sstevel@tonic-gate 	register char *bp = buf;
2447c478bd9Sstevel@tonic-gate 
2457c478bd9Sstevel@tonic-gate 	if (*gecos == '*')
2467c478bd9Sstevel@tonic-gate 		gecos++;
2477c478bd9Sstevel@tonic-gate 
2487c478bd9Sstevel@tonic-gate 	/* copy gecos, interpolating & to be full name */
2497c478bd9Sstevel@tonic-gate 	for (p = gecos; *p != '\0' && *p != ',' && *p != ';' && *p != '%'; p++)
2507c478bd9Sstevel@tonic-gate 	{
2517c478bd9Sstevel@tonic-gate 		if (bp >= &buf[buflen - 1])
2527c478bd9Sstevel@tonic-gate 		{
2537c478bd9Sstevel@tonic-gate 			/* buffer overflow -- just use login name */
2547c478bd9Sstevel@tonic-gate 			(void) sm_strlcpy(buf, user, buflen);
2557c478bd9Sstevel@tonic-gate 			return;
2567c478bd9Sstevel@tonic-gate 		}
2577c478bd9Sstevel@tonic-gate 		if (*p == '&')
2587c478bd9Sstevel@tonic-gate 		{
2597c478bd9Sstevel@tonic-gate 			/* interpolate full name */
2607c478bd9Sstevel@tonic-gate 			(void) sm_strlcpy(bp, user, buflen - (bp - buf));
2617c478bd9Sstevel@tonic-gate 			*bp = toupper(*bp);
2627c478bd9Sstevel@tonic-gate 			bp += strlen(bp);
2637c478bd9Sstevel@tonic-gate 		}
2647c478bd9Sstevel@tonic-gate 		else
2657c478bd9Sstevel@tonic-gate 		{
2667c478bd9Sstevel@tonic-gate #if _FFR_HANDLE_ISO8859_GECOS
2677c478bd9Sstevel@tonic-gate 			if ((unsigned char) *p >= 128)
2687c478bd9Sstevel@tonic-gate 				*bp++ = Latin1ToASCII[(unsigned char) *p - 128];
2697c478bd9Sstevel@tonic-gate 			else
2707c478bd9Sstevel@tonic-gate #endif /* _FFR_HANDLE_ISO8859_GECOS */
2717c478bd9Sstevel@tonic-gate 				*bp++ = *p;
2727c478bd9Sstevel@tonic-gate 		}
2737c478bd9Sstevel@tonic-gate 	}
2747c478bd9Sstevel@tonic-gate 	*bp = '\0';
2757c478bd9Sstevel@tonic-gate }
2767c478bd9Sstevel@tonic-gate 
2777c478bd9Sstevel@tonic-gate /*
2787c478bd9Sstevel@tonic-gate **  /etc/passwd implementation.
2797c478bd9Sstevel@tonic-gate */
2807c478bd9Sstevel@tonic-gate 
2817c478bd9Sstevel@tonic-gate /*
2827c478bd9Sstevel@tonic-gate **  MBDB_PW_INITIALIZE -- initialize getpwnam() version
2837c478bd9Sstevel@tonic-gate **
2847c478bd9Sstevel@tonic-gate **	Parameters:
2857c478bd9Sstevel@tonic-gate **		arg -- unused.
2867c478bd9Sstevel@tonic-gate **
2877c478bd9Sstevel@tonic-gate **	Results:
2887c478bd9Sstevel@tonic-gate **		EX_OK.
2897c478bd9Sstevel@tonic-gate */
2907c478bd9Sstevel@tonic-gate 
2917c478bd9Sstevel@tonic-gate /* ARGSUSED0 */
2927c478bd9Sstevel@tonic-gate static int
mbdb_pw_initialize(arg)2937c478bd9Sstevel@tonic-gate mbdb_pw_initialize(arg)
2947c478bd9Sstevel@tonic-gate 	char *arg;
2957c478bd9Sstevel@tonic-gate {
2967c478bd9Sstevel@tonic-gate 	return EX_OK;
2977c478bd9Sstevel@tonic-gate }
2987c478bd9Sstevel@tonic-gate 
2997c478bd9Sstevel@tonic-gate /*
3007c478bd9Sstevel@tonic-gate **  MBDB_PW_LOOKUP -- look up a local mail recipient, given name
3017c478bd9Sstevel@tonic-gate **
3027c478bd9Sstevel@tonic-gate **	Parameters:
3037c478bd9Sstevel@tonic-gate **		name -- name of local mail recipient
3047c478bd9Sstevel@tonic-gate **		user -- pointer to structure to fill in on success
3057c478bd9Sstevel@tonic-gate **
3067c478bd9Sstevel@tonic-gate **	Results:
3077c478bd9Sstevel@tonic-gate **		On success, fill in *user and return EX_OK.
3087c478bd9Sstevel@tonic-gate **		Failure: EX_NOUSER.
3097c478bd9Sstevel@tonic-gate */
3107c478bd9Sstevel@tonic-gate 
3117c478bd9Sstevel@tonic-gate static int
mbdb_pw_lookup(name,user)3127c478bd9Sstevel@tonic-gate mbdb_pw_lookup(name, user)
3137c478bd9Sstevel@tonic-gate 	char *name;
3147c478bd9Sstevel@tonic-gate 	SM_MBDB_T *user;
3157c478bd9Sstevel@tonic-gate {
3167c478bd9Sstevel@tonic-gate 	struct passwd *pw;
3177c478bd9Sstevel@tonic-gate 
3187c478bd9Sstevel@tonic-gate #ifdef HESIOD
3197c478bd9Sstevel@tonic-gate 	/* DEC Hesiod getpwnam accepts numeric strings -- short circuit it */
3207c478bd9Sstevel@tonic-gate 	{
3217c478bd9Sstevel@tonic-gate 		char *p;
3227c478bd9Sstevel@tonic-gate 
3237c478bd9Sstevel@tonic-gate 		for (p = name; *p != '\0'; p++)
3247c478bd9Sstevel@tonic-gate 			if (!isascii(*p) || !isdigit(*p))
3257c478bd9Sstevel@tonic-gate 				break;
3267c478bd9Sstevel@tonic-gate 		if (*p == '\0')
3277c478bd9Sstevel@tonic-gate 			return EX_NOUSER;
3287c478bd9Sstevel@tonic-gate 	}
3297c478bd9Sstevel@tonic-gate #endif /* HESIOD */
3307c478bd9Sstevel@tonic-gate 
3317c478bd9Sstevel@tonic-gate 	errno = 0;
3327c478bd9Sstevel@tonic-gate 	pw = getpwnam(name);
3337c478bd9Sstevel@tonic-gate 	if (pw == NULL)
3347c478bd9Sstevel@tonic-gate 	{
3357c478bd9Sstevel@tonic-gate #if 0
3367c478bd9Sstevel@tonic-gate 		/*
3377c478bd9Sstevel@tonic-gate 		**  getpwnam() isn't advertised as setting errno.
3387c478bd9Sstevel@tonic-gate 		**  In fact, under FreeBSD, non-root getpwnam() on
3397c478bd9Sstevel@tonic-gate 		**  non-existant users returns NULL with errno = EPERM.
3407c478bd9Sstevel@tonic-gate 		**  This test won't work.
3417c478bd9Sstevel@tonic-gate 		*/
3427c478bd9Sstevel@tonic-gate 		switch (errno)
3437c478bd9Sstevel@tonic-gate 		{
3447c478bd9Sstevel@tonic-gate 		  case 0:
3457c478bd9Sstevel@tonic-gate 			return EX_NOUSER;
3467c478bd9Sstevel@tonic-gate 		  case EIO:
3477c478bd9Sstevel@tonic-gate 			return EX_OSERR;
3487c478bd9Sstevel@tonic-gate 		  default:
3497c478bd9Sstevel@tonic-gate 			return EX_TEMPFAIL;
3507c478bd9Sstevel@tonic-gate 		}
3517c478bd9Sstevel@tonic-gate #endif /* 0 */
3527c478bd9Sstevel@tonic-gate 		return EX_NOUSER;
3537c478bd9Sstevel@tonic-gate 	}
3547c478bd9Sstevel@tonic-gate 
3557c478bd9Sstevel@tonic-gate 	sm_mbdb_frompw(user, pw);
3567c478bd9Sstevel@tonic-gate 	return EX_OK;
3577c478bd9Sstevel@tonic-gate }
3587c478bd9Sstevel@tonic-gate 
3597c478bd9Sstevel@tonic-gate /*
3607c478bd9Sstevel@tonic-gate **  MBDB_PW_TERMINATE -- terminate connection to the mailbox database
3617c478bd9Sstevel@tonic-gate **
3627c478bd9Sstevel@tonic-gate **	Parameters:
3637c478bd9Sstevel@tonic-gate **		none.
3647c478bd9Sstevel@tonic-gate **
3657c478bd9Sstevel@tonic-gate **	Results:
3667c478bd9Sstevel@tonic-gate **		none.
3677c478bd9Sstevel@tonic-gate */
3687c478bd9Sstevel@tonic-gate 
3697c478bd9Sstevel@tonic-gate static void
mbdb_pw_terminate()3707c478bd9Sstevel@tonic-gate mbdb_pw_terminate()
3717c478bd9Sstevel@tonic-gate {
3727c478bd9Sstevel@tonic-gate 	endpwent();
3737c478bd9Sstevel@tonic-gate }
3747c478bd9Sstevel@tonic-gate 
3757c478bd9Sstevel@tonic-gate #if LDAPMAP
3767c478bd9Sstevel@tonic-gate # if _LDAP_EXAMPLE_
3777c478bd9Sstevel@tonic-gate /*
3787c478bd9Sstevel@tonic-gate **  LDAP example implementation based on RFC 2307, "An Approach for Using
3797c478bd9Sstevel@tonic-gate **  LDAP as a Network Information Service":
3807c478bd9Sstevel@tonic-gate **
3817c478bd9Sstevel@tonic-gate **	( nisSchema.1.0 NAME 'uidNumber'
3827c478bd9Sstevel@tonic-gate **	  DESC 'An integer uniquely identifying a user in an
3837c478bd9Sstevel@tonic-gate **		administrative domain'
3847c478bd9Sstevel@tonic-gate **	  EQUALITY integerMatch SYNTAX 'INTEGER' SINGLE-VALUE )
3857c478bd9Sstevel@tonic-gate **
3867c478bd9Sstevel@tonic-gate **	( nisSchema.1.1 NAME 'gidNumber'
3877c478bd9Sstevel@tonic-gate **	  DESC 'An integer uniquely identifying a group in an
3887c478bd9Sstevel@tonic-gate **		administrative domain'
3897c478bd9Sstevel@tonic-gate **	  EQUALITY integerMatch SYNTAX 'INTEGER' SINGLE-VALUE )
3907c478bd9Sstevel@tonic-gate **
3917c478bd9Sstevel@tonic-gate **	( nisSchema.1.2 NAME 'gecos'
3927c478bd9Sstevel@tonic-gate **	  DESC 'The GECOS field; the common name'
3937c478bd9Sstevel@tonic-gate **	  EQUALITY caseIgnoreIA5Match
3947c478bd9Sstevel@tonic-gate **	  SUBSTRINGS caseIgnoreIA5SubstringsMatch
3957c478bd9Sstevel@tonic-gate **	  SYNTAX 'IA5String' SINGLE-VALUE )
3967c478bd9Sstevel@tonic-gate **
3977c478bd9Sstevel@tonic-gate **	( nisSchema.1.3 NAME 'homeDirectory'
3987c478bd9Sstevel@tonic-gate **	  DESC 'The absolute path to the home directory'
3997c478bd9Sstevel@tonic-gate **	  EQUALITY caseExactIA5Match
4007c478bd9Sstevel@tonic-gate **	  SYNTAX 'IA5String' SINGLE-VALUE )
4017c478bd9Sstevel@tonic-gate **
4027c478bd9Sstevel@tonic-gate **	( nisSchema.1.4 NAME 'loginShell'
4037c478bd9Sstevel@tonic-gate **	  DESC 'The path to the login shell'
4047c478bd9Sstevel@tonic-gate **	  EQUALITY caseExactIA5Match
4057c478bd9Sstevel@tonic-gate **	  SYNTAX 'IA5String' SINGLE-VALUE )
4067c478bd9Sstevel@tonic-gate **
4077c478bd9Sstevel@tonic-gate **	( nisSchema.2.0 NAME 'posixAccount' SUP top AUXILIARY
4087c478bd9Sstevel@tonic-gate **	  DESC 'Abstraction of an account with POSIX attributes'
4097c478bd9Sstevel@tonic-gate **	  MUST ( cn $ uid $ uidNumber $ gidNumber $ homeDirectory )
4107c478bd9Sstevel@tonic-gate **	  MAY ( userPassword $ loginShell $ gecos $ description ) )
4117c478bd9Sstevel@tonic-gate **
4127c478bd9Sstevel@tonic-gate */
4137c478bd9Sstevel@tonic-gate 
4147c478bd9Sstevel@tonic-gate #  define MBDB_LDAP_LABEL		"MailboxDatabase"
4157c478bd9Sstevel@tonic-gate 
4167c478bd9Sstevel@tonic-gate #  ifndef MBDB_LDAP_FILTER
4177c478bd9Sstevel@tonic-gate #   define MBDB_LDAP_FILTER		"(&(objectClass=posixAccount)(uid=%0))"
4187c478bd9Sstevel@tonic-gate #  endif /* MBDB_LDAP_FILTER */
4197c478bd9Sstevel@tonic-gate 
4207c478bd9Sstevel@tonic-gate #  ifndef MBDB_DEFAULT_LDAP_BASEDN
4217c478bd9Sstevel@tonic-gate #   define MBDB_DEFAULT_LDAP_BASEDN	NULL
4227c478bd9Sstevel@tonic-gate #  endif /* MBDB_DEFAULT_LDAP_BASEDN */
4237c478bd9Sstevel@tonic-gate 
4247c478bd9Sstevel@tonic-gate #  ifndef MBDB_DEFAULT_LDAP_SERVER
4257c478bd9Sstevel@tonic-gate #   define MBDB_DEFAULT_LDAP_SERVER	NULL
4267c478bd9Sstevel@tonic-gate #  endif /* MBDB_DEFAULT_LDAP_SERVER */
4277c478bd9Sstevel@tonic-gate 
4287c478bd9Sstevel@tonic-gate /*
4297c478bd9Sstevel@tonic-gate **  MBDB_LDAP_INITIALIZE -- initialize LDAP version
4307c478bd9Sstevel@tonic-gate **
4317c478bd9Sstevel@tonic-gate **	Parameters:
4327c478bd9Sstevel@tonic-gate **		arg -- LDAP specification
4337c478bd9Sstevel@tonic-gate **
4347c478bd9Sstevel@tonic-gate **	Results:
4357c478bd9Sstevel@tonic-gate **		EX_OK on success, or an EX_* code on failure.
4367c478bd9Sstevel@tonic-gate */
4377c478bd9Sstevel@tonic-gate 
4387c478bd9Sstevel@tonic-gate static int
mbdb_ldap_initialize(arg)4397c478bd9Sstevel@tonic-gate mbdb_ldap_initialize(arg)
4407c478bd9Sstevel@tonic-gate 	char *arg;
4417c478bd9Sstevel@tonic-gate {
4427c478bd9Sstevel@tonic-gate 	sm_ldap_clear(&LDAPLMAP);
4437c478bd9Sstevel@tonic-gate 	LDAPLMAP.ldap_base = MBDB_DEFAULT_LDAP_BASEDN;
4447c478bd9Sstevel@tonic-gate 	LDAPLMAP.ldap_host = MBDB_DEFAULT_LDAP_SERVER;
4457c478bd9Sstevel@tonic-gate 	LDAPLMAP.ldap_filter = MBDB_LDAP_FILTER;
4467c478bd9Sstevel@tonic-gate 
4477c478bd9Sstevel@tonic-gate 	/* Only want one match */
4487c478bd9Sstevel@tonic-gate 	LDAPLMAP.ldap_sizelimit = 1;
4497c478bd9Sstevel@tonic-gate 
4507c478bd9Sstevel@tonic-gate 	/* interpolate new ldap_base and ldap_host from arg if given */
4517c478bd9Sstevel@tonic-gate 	if (arg != NULL && *arg != '\0')
4527c478bd9Sstevel@tonic-gate 	{
4537c478bd9Sstevel@tonic-gate 		char *new;
4547c478bd9Sstevel@tonic-gate 		char *sep;
4557c478bd9Sstevel@tonic-gate 		size_t len;
4567c478bd9Sstevel@tonic-gate 
4577c478bd9Sstevel@tonic-gate 		len = strlen(arg) + 1;
4587c478bd9Sstevel@tonic-gate 		new = sm_malloc(len);
4597c478bd9Sstevel@tonic-gate 		if (new == NULL)
4607c478bd9Sstevel@tonic-gate 			return EX_TEMPFAIL;
4617c478bd9Sstevel@tonic-gate 		(void) sm_strlcpy(new, arg, len);
4627c478bd9Sstevel@tonic-gate 		sep = strrchr(new, '@');
4637c478bd9Sstevel@tonic-gate 		if (sep != NULL)
4647c478bd9Sstevel@tonic-gate 		{
4657c478bd9Sstevel@tonic-gate 			*sep++ = '\0';
4667c478bd9Sstevel@tonic-gate 			LDAPLMAP.ldap_host = sep;
4677c478bd9Sstevel@tonic-gate 		}
4687c478bd9Sstevel@tonic-gate 		LDAPLMAP.ldap_base = new;
4697c478bd9Sstevel@tonic-gate 	}
4707c478bd9Sstevel@tonic-gate 	return EX_OK;
4717c478bd9Sstevel@tonic-gate }
4727c478bd9Sstevel@tonic-gate 
4737c478bd9Sstevel@tonic-gate 
4747c478bd9Sstevel@tonic-gate /*
4757c478bd9Sstevel@tonic-gate **  MBDB_LDAP_LOOKUP -- look up a local mail recipient, given name
4767c478bd9Sstevel@tonic-gate **
4777c478bd9Sstevel@tonic-gate **	Parameters:
4787c478bd9Sstevel@tonic-gate **		name -- name of local mail recipient
4797c478bd9Sstevel@tonic-gate **		user -- pointer to structure to fill in on success
4807c478bd9Sstevel@tonic-gate **
4817c478bd9Sstevel@tonic-gate **	Results:
4827c478bd9Sstevel@tonic-gate **		On success, fill in *user and return EX_OK.
4837c478bd9Sstevel@tonic-gate **		Failure: EX_NOUSER.
4847c478bd9Sstevel@tonic-gate */
4857c478bd9Sstevel@tonic-gate 
4867c478bd9Sstevel@tonic-gate #define NEED_FULLNAME	0x01
4877c478bd9Sstevel@tonic-gate #define NEED_HOMEDIR	0x02
4887c478bd9Sstevel@tonic-gate #define NEED_SHELL	0x04
4897c478bd9Sstevel@tonic-gate #define NEED_UID	0x08
4907c478bd9Sstevel@tonic-gate #define NEED_GID	0x10
4917c478bd9Sstevel@tonic-gate 
4927c478bd9Sstevel@tonic-gate static int
mbdb_ldap_lookup(name,user)4937c478bd9Sstevel@tonic-gate mbdb_ldap_lookup(name, user)
4947c478bd9Sstevel@tonic-gate 	char *name;
4957c478bd9Sstevel@tonic-gate 	SM_MBDB_T *user;
4967c478bd9Sstevel@tonic-gate {
4977c478bd9Sstevel@tonic-gate 	int msgid;
4987c478bd9Sstevel@tonic-gate 	int need;
4997c478bd9Sstevel@tonic-gate 	int ret;
5007c478bd9Sstevel@tonic-gate 	int save_errno;
5017c478bd9Sstevel@tonic-gate 	LDAPMessage *entry;
5027c478bd9Sstevel@tonic-gate 	BerElement *ber;
5037c478bd9Sstevel@tonic-gate 	char *attr = NULL;
5047c478bd9Sstevel@tonic-gate 
5057c478bd9Sstevel@tonic-gate 	if (strlen(name) >= sizeof(user->mbdb_name))
5067c478bd9Sstevel@tonic-gate 	{
5077c478bd9Sstevel@tonic-gate 		errno = EINVAL;
5087c478bd9Sstevel@tonic-gate 		return EX_NOUSER;
5097c478bd9Sstevel@tonic-gate 	}
5107c478bd9Sstevel@tonic-gate 
5117c478bd9Sstevel@tonic-gate 	if (LDAPLMAP.ldap_filter == NULL)
5127c478bd9Sstevel@tonic-gate 	{
5137c478bd9Sstevel@tonic-gate 		/* map not initialized, but don't have arg here */
5147c478bd9Sstevel@tonic-gate 		errno = EFAULT;
5157c478bd9Sstevel@tonic-gate 		return EX_TEMPFAIL;
5167c478bd9Sstevel@tonic-gate 	}
5177c478bd9Sstevel@tonic-gate 
5187c478bd9Sstevel@tonic-gate 	if (LDAPLMAP.ldap_pid != getpid())
5197c478bd9Sstevel@tonic-gate 	{
5207c478bd9Sstevel@tonic-gate 		/* re-open map in this child process */
5217c478bd9Sstevel@tonic-gate 		LDAPLMAP.ldap_ld = NULL;
5227c478bd9Sstevel@tonic-gate 	}
5237c478bd9Sstevel@tonic-gate 
5247c478bd9Sstevel@tonic-gate 	if (LDAPLMAP.ldap_ld == NULL)
5257c478bd9Sstevel@tonic-gate 	{
5267c478bd9Sstevel@tonic-gate 		/* map not open, try to open now */
5277c478bd9Sstevel@tonic-gate 		if (!sm_ldap_start(MBDB_LDAP_LABEL, &LDAPLMAP))
5287c478bd9Sstevel@tonic-gate 			return EX_TEMPFAIL;
5297c478bd9Sstevel@tonic-gate 	}
5307c478bd9Sstevel@tonic-gate 
5317c478bd9Sstevel@tonic-gate 	sm_ldap_setopts(LDAPLMAP.ldap_ld, &LDAPLMAP);
5327c478bd9Sstevel@tonic-gate 	msgid = sm_ldap_search(&LDAPLMAP, name);
5337c478bd9Sstevel@tonic-gate 	if (msgid == -1)
5347c478bd9Sstevel@tonic-gate 	{
5357c478bd9Sstevel@tonic-gate 		save_errno = sm_ldap_geterrno(LDAPLMAP.ldap_ld) + E_LDAPBASE;
5367c478bd9Sstevel@tonic-gate #  ifdef LDAP_SERVER_DOWN
5377c478bd9Sstevel@tonic-gate 		if (errno == LDAP_SERVER_DOWN)
5387c478bd9Sstevel@tonic-gate 		{
5397c478bd9Sstevel@tonic-gate 			/* server disappeared, try reopen on next search */
5407c478bd9Sstevel@tonic-gate 			sm_ldap_close(&LDAPLMAP);
5417c478bd9Sstevel@tonic-gate 		}
5427c478bd9Sstevel@tonic-gate #  endif /* LDAP_SERVER_DOWN */
5437c478bd9Sstevel@tonic-gate 		errno = save_errno;
5447c478bd9Sstevel@tonic-gate 		return EX_TEMPFAIL;
5457c478bd9Sstevel@tonic-gate 	}
5467c478bd9Sstevel@tonic-gate 
5477c478bd9Sstevel@tonic-gate 	/* Get results */
5487c478bd9Sstevel@tonic-gate 	ret = ldap_result(LDAPLMAP.ldap_ld, msgid, 1,
5497c478bd9Sstevel@tonic-gate 			  (LDAPLMAP.ldap_timeout.tv_sec == 0 ? NULL :
5507c478bd9Sstevel@tonic-gate 			   &(LDAPLMAP.ldap_timeout)),
5517c478bd9Sstevel@tonic-gate 			  &(LDAPLMAP.ldap_res));
5527c478bd9Sstevel@tonic-gate 
5537c478bd9Sstevel@tonic-gate 	if (ret != LDAP_RES_SEARCH_RESULT &&
5547c478bd9Sstevel@tonic-gate 	    ret != LDAP_RES_SEARCH_ENTRY)
5557c478bd9Sstevel@tonic-gate 	{
5567c478bd9Sstevel@tonic-gate 		if (ret == 0)
5577c478bd9Sstevel@tonic-gate 			errno = ETIMEDOUT;
5587c478bd9Sstevel@tonic-gate 		else
5597c478bd9Sstevel@tonic-gate 			errno = sm_ldap_geterrno(LDAPLMAP.ldap_ld);
5607c478bd9Sstevel@tonic-gate 		ret = EX_TEMPFAIL;
5617c478bd9Sstevel@tonic-gate 		goto abort;
5627c478bd9Sstevel@tonic-gate 	}
5637c478bd9Sstevel@tonic-gate 
5647c478bd9Sstevel@tonic-gate 	entry = ldap_first_entry(LDAPLMAP.ldap_ld, LDAPLMAP.ldap_res);
5657c478bd9Sstevel@tonic-gate 	if (entry == NULL)
5667c478bd9Sstevel@tonic-gate 	{
567*e9af4bc0SJohn Beck 		int rc;
568*e9af4bc0SJohn Beck 
569*e9af4bc0SJohn Beck 		/*
570*e9af4bc0SJohn Beck 		**  We may have gotten an LDAP_RES_SEARCH_RESULT response
571*e9af4bc0SJohn Beck 		**  with an error inside it, so we have to extract that
572*e9af4bc0SJohn Beck 		**  with ldap_parse_result().  This can happen when talking
573*e9af4bc0SJohn Beck 		**  to an LDAP proxy whose backend has gone down.
574*e9af4bc0SJohn Beck 		*/
575*e9af4bc0SJohn Beck 
576*e9af4bc0SJohn Beck 		save_errno = ldap_parse_result(LDAPLMAP.ldap_ld,
577*e9af4bc0SJohn Beck 					       LDAPLMAP.ldap_res, &rc, NULL,
578*e9af4bc0SJohn Beck 					       NULL, NULL, NULL, 0);
579*e9af4bc0SJohn Beck 		if (save_errno == LDAP_SUCCESS)
580*e9af4bc0SJohn Beck 			save_errno = rc;
5817c478bd9Sstevel@tonic-gate 		if (save_errno == LDAP_SUCCESS)
5827c478bd9Sstevel@tonic-gate 		{
5837c478bd9Sstevel@tonic-gate 			errno = ENOENT;
5847c478bd9Sstevel@tonic-gate 			ret = EX_NOUSER;
5857c478bd9Sstevel@tonic-gate 		}
5867c478bd9Sstevel@tonic-gate 		else
5877c478bd9Sstevel@tonic-gate 		{
5887c478bd9Sstevel@tonic-gate 			errno = save_errno;
5897c478bd9Sstevel@tonic-gate 			ret = EX_TEMPFAIL;
5907c478bd9Sstevel@tonic-gate 		}
5917c478bd9Sstevel@tonic-gate 		goto abort;
5927c478bd9Sstevel@tonic-gate 	}
5937c478bd9Sstevel@tonic-gate 
5947c478bd9Sstevel@tonic-gate # if !defined(LDAP_VERSION_MAX) && !defined(LDAP_OPT_SIZELIMIT)
5957c478bd9Sstevel@tonic-gate 	/*
5967c478bd9Sstevel@tonic-gate 	**  Reset value to prevent lingering
5977c478bd9Sstevel@tonic-gate 	**  LDAP_DECODING_ERROR due to
5987c478bd9Sstevel@tonic-gate 	**  OpenLDAP 1.X's hack (see below)
5997c478bd9Sstevel@tonic-gate 	*/
6007c478bd9Sstevel@tonic-gate 
6017c478bd9Sstevel@tonic-gate 	LDAPLMAP.ldap_ld->ld_errno = LDAP_SUCCESS;
6027c478bd9Sstevel@tonic-gate # endif /* !defined(LDAP_VERSION_MAX) !defined(LDAP_OPT_SIZELIMIT) */
6037c478bd9Sstevel@tonic-gate 
6047c478bd9Sstevel@tonic-gate 	ret = EX_OK;
6057c478bd9Sstevel@tonic-gate 	need = NEED_FULLNAME|NEED_HOMEDIR|NEED_SHELL|NEED_UID|NEED_GID;
6067c478bd9Sstevel@tonic-gate 	for (attr = ldap_first_attribute(LDAPLMAP.ldap_ld, entry, &ber);
6077c478bd9Sstevel@tonic-gate 	     attr != NULL;
6087c478bd9Sstevel@tonic-gate 	     attr = ldap_next_attribute(LDAPLMAP.ldap_ld, entry, ber))
6097c478bd9Sstevel@tonic-gate 	{
6107c478bd9Sstevel@tonic-gate 		char **vals;
6117c478bd9Sstevel@tonic-gate 
6127c478bd9Sstevel@tonic-gate 		vals = ldap_get_values(LDAPLMAP.ldap_ld, entry, attr);
6137c478bd9Sstevel@tonic-gate 		if (vals == NULL)
6147c478bd9Sstevel@tonic-gate 		{
6157c478bd9Sstevel@tonic-gate 			errno = sm_ldap_geterrno(LDAPLMAP.ldap_ld);
6167c478bd9Sstevel@tonic-gate 			if (errno == LDAP_SUCCESS)
6177c478bd9Sstevel@tonic-gate 			{
6187c478bd9Sstevel@tonic-gate 				ldap_memfree(attr);
6197c478bd9Sstevel@tonic-gate 				continue;
6207c478bd9Sstevel@tonic-gate 			}
6217c478bd9Sstevel@tonic-gate 
6227c478bd9Sstevel@tonic-gate 			/* Must be an error */
6237c478bd9Sstevel@tonic-gate 			errno += E_LDAPBASE;
6247c478bd9Sstevel@tonic-gate 			ret = EX_TEMPFAIL;
6257c478bd9Sstevel@tonic-gate 			goto abort;
6267c478bd9Sstevel@tonic-gate 		}
6277c478bd9Sstevel@tonic-gate 
6287c478bd9Sstevel@tonic-gate # if !defined(LDAP_VERSION_MAX) && !defined(LDAP_OPT_SIZELIMIT)
6297c478bd9Sstevel@tonic-gate 		/*
6307c478bd9Sstevel@tonic-gate 		**  Reset value to prevent lingering
6317c478bd9Sstevel@tonic-gate 		**  LDAP_DECODING_ERROR due to
6327c478bd9Sstevel@tonic-gate 		**  OpenLDAP 1.X's hack (see below)
6337c478bd9Sstevel@tonic-gate 		*/
6347c478bd9Sstevel@tonic-gate 
6357c478bd9Sstevel@tonic-gate 		LDAPLMAP.ldap_ld->ld_errno = LDAP_SUCCESS;
6367c478bd9Sstevel@tonic-gate # endif /* !defined(LDAP_VERSION_MAX) !defined(LDAP_OPT_SIZELIMIT) */
6377c478bd9Sstevel@tonic-gate 
6387c478bd9Sstevel@tonic-gate 		if (vals[0] == NULL || vals[0][0] == '\0')
6397c478bd9Sstevel@tonic-gate 			goto skip;
6407c478bd9Sstevel@tonic-gate 
6417c478bd9Sstevel@tonic-gate 		if (strcasecmp(attr, "gecos") == 0)
6427c478bd9Sstevel@tonic-gate 		{
6437c478bd9Sstevel@tonic-gate 			if (!bitset(NEED_FULLNAME, need) ||
6447c478bd9Sstevel@tonic-gate 			    strlen(vals[0]) >= sizeof(user->mbdb_fullname))
6457c478bd9Sstevel@tonic-gate 				goto skip;
6467c478bd9Sstevel@tonic-gate 
6477c478bd9Sstevel@tonic-gate 			sm_pwfullname(vals[0], name, user->mbdb_fullname,
6487c478bd9Sstevel@tonic-gate 				      sizeof(user->mbdb_fullname));
6497c478bd9Sstevel@tonic-gate 			need &= ~NEED_FULLNAME;
6507c478bd9Sstevel@tonic-gate 		}
6517c478bd9Sstevel@tonic-gate 		else if (strcasecmp(attr, "homeDirectory") == 0)
6527c478bd9Sstevel@tonic-gate 		{
6537c478bd9Sstevel@tonic-gate 			if (!bitset(NEED_HOMEDIR, need) ||
6547c478bd9Sstevel@tonic-gate 			    strlen(vals[0]) >= sizeof(user->mbdb_homedir))
6557c478bd9Sstevel@tonic-gate 				goto skip;
6567c478bd9Sstevel@tonic-gate 
6577c478bd9Sstevel@tonic-gate 			(void) sm_strlcpy(user->mbdb_homedir, vals[0],
6587c478bd9Sstevel@tonic-gate 					  sizeof(user->mbdb_homedir));
6597c478bd9Sstevel@tonic-gate 			need &= ~NEED_HOMEDIR;
6607c478bd9Sstevel@tonic-gate 		}
6617c478bd9Sstevel@tonic-gate 		else if (strcasecmp(attr, "loginShell") == 0)
6627c478bd9Sstevel@tonic-gate 		{
6637c478bd9Sstevel@tonic-gate 			if (!bitset(NEED_SHELL, need) ||
6647c478bd9Sstevel@tonic-gate 			    strlen(vals[0]) >= sizeof(user->mbdb_shell))
6657c478bd9Sstevel@tonic-gate 				goto skip;
6667c478bd9Sstevel@tonic-gate 
6677c478bd9Sstevel@tonic-gate 			(void) sm_strlcpy(user->mbdb_shell, vals[0],
6687c478bd9Sstevel@tonic-gate 					  sizeof(user->mbdb_shell));
6697c478bd9Sstevel@tonic-gate 			need &= ~NEED_SHELL;
6707c478bd9Sstevel@tonic-gate 		}
6717c478bd9Sstevel@tonic-gate 		else if (strcasecmp(attr, "uidNumber") == 0)
6727c478bd9Sstevel@tonic-gate 		{
6737c478bd9Sstevel@tonic-gate 			char *p;
6747c478bd9Sstevel@tonic-gate 
6757c478bd9Sstevel@tonic-gate 			if (!bitset(NEED_UID, need))
6767c478bd9Sstevel@tonic-gate 				goto skip;
6777c478bd9Sstevel@tonic-gate 
6787c478bd9Sstevel@tonic-gate 			for (p = vals[0]; *p != '\0'; p++)
6797c478bd9Sstevel@tonic-gate 			{
6807c478bd9Sstevel@tonic-gate 				/* allow negative numbers */
6817c478bd9Sstevel@tonic-gate 				if (p == vals[0] && *p == '-')
6827c478bd9Sstevel@tonic-gate 				{
6837c478bd9Sstevel@tonic-gate 					/* but not simply '-' */
6847c478bd9Sstevel@tonic-gate 					if (*(p + 1) == '\0')
6857c478bd9Sstevel@tonic-gate 						goto skip;
6867c478bd9Sstevel@tonic-gate 				}
6877c478bd9Sstevel@tonic-gate 				else if (!isascii(*p) || !isdigit(*p))
6887c478bd9Sstevel@tonic-gate 					goto skip;
6897c478bd9Sstevel@tonic-gate 			}
6907c478bd9Sstevel@tonic-gate 			user->mbdb_uid = atoi(vals[0]);
6917c478bd9Sstevel@tonic-gate 			need &= ~NEED_UID;
6927c478bd9Sstevel@tonic-gate 		}
6937c478bd9Sstevel@tonic-gate 		else if (strcasecmp(attr, "gidNumber") == 0)
6947c478bd9Sstevel@tonic-gate 		{
6957c478bd9Sstevel@tonic-gate 			char *p;
6967c478bd9Sstevel@tonic-gate 
6977c478bd9Sstevel@tonic-gate 			if (!bitset(NEED_GID, need))
6987c478bd9Sstevel@tonic-gate 				goto skip;
6997c478bd9Sstevel@tonic-gate 
7007c478bd9Sstevel@tonic-gate 			for (p = vals[0]; *p != '\0'; p++)
7017c478bd9Sstevel@tonic-gate 			{
7027c478bd9Sstevel@tonic-gate 				/* allow negative numbers */
7037c478bd9Sstevel@tonic-gate 				if (p == vals[0] && *p == '-')
7047c478bd9Sstevel@tonic-gate 				{
7057c478bd9Sstevel@tonic-gate 					/* but not simply '-' */
7067c478bd9Sstevel@tonic-gate 					if (*(p + 1) == '\0')
7077c478bd9Sstevel@tonic-gate 						goto skip;
7087c478bd9Sstevel@tonic-gate 				}
7097c478bd9Sstevel@tonic-gate 				else if (!isascii(*p) || !isdigit(*p))
7107c478bd9Sstevel@tonic-gate 					goto skip;
7117c478bd9Sstevel@tonic-gate 			}
7127c478bd9Sstevel@tonic-gate 			user->mbdb_gid = atoi(vals[0]);
7137c478bd9Sstevel@tonic-gate 			need &= ~NEED_GID;
7147c478bd9Sstevel@tonic-gate 		}
7157c478bd9Sstevel@tonic-gate 
7167c478bd9Sstevel@tonic-gate skip:
7177c478bd9Sstevel@tonic-gate 		ldap_value_free(vals);
7187c478bd9Sstevel@tonic-gate 		ldap_memfree(attr);
7197c478bd9Sstevel@tonic-gate 	}
7207c478bd9Sstevel@tonic-gate 
7217c478bd9Sstevel@tonic-gate 	errno = sm_ldap_geterrno(LDAPLMAP.ldap_ld);
7227c478bd9Sstevel@tonic-gate 
7237c478bd9Sstevel@tonic-gate 	/*
7247c478bd9Sstevel@tonic-gate 	**  We check errno != LDAP_DECODING_ERROR since
7257c478bd9Sstevel@tonic-gate 	**  OpenLDAP 1.X has a very ugly *undocumented*
7267c478bd9Sstevel@tonic-gate 	**  hack of returning this error code from
7277c478bd9Sstevel@tonic-gate 	**  ldap_next_attribute() if the library freed the
7287c478bd9Sstevel@tonic-gate 	**  ber attribute.  See:
7297c478bd9Sstevel@tonic-gate 	**  http://www.openldap.org/lists/openldap-devel/9901/msg00064.html
7307c478bd9Sstevel@tonic-gate 	*/
7317c478bd9Sstevel@tonic-gate 
7327c478bd9Sstevel@tonic-gate 	if (errno != LDAP_SUCCESS &&
7337c478bd9Sstevel@tonic-gate 	    errno != LDAP_DECODING_ERROR)
7347c478bd9Sstevel@tonic-gate 	{
7357c478bd9Sstevel@tonic-gate 		/* Must be an error */
7367c478bd9Sstevel@tonic-gate 		errno += E_LDAPBASE;
7377c478bd9Sstevel@tonic-gate 		ret = EX_TEMPFAIL;
7387c478bd9Sstevel@tonic-gate 		goto abort;
7397c478bd9Sstevel@tonic-gate 	}
7407c478bd9Sstevel@tonic-gate 
7417c478bd9Sstevel@tonic-gate  abort:
7427c478bd9Sstevel@tonic-gate 	save_errno = errno;
7437c478bd9Sstevel@tonic-gate 	if (attr != NULL)
7447c478bd9Sstevel@tonic-gate 	{
7457c478bd9Sstevel@tonic-gate 		ldap_memfree(attr);
7467c478bd9Sstevel@tonic-gate 		attr = NULL;
7477c478bd9Sstevel@tonic-gate 	}
7487c478bd9Sstevel@tonic-gate 	if (LDAPLMAP.ldap_res != NULL)
7497c478bd9Sstevel@tonic-gate 	{
7507c478bd9Sstevel@tonic-gate 		ldap_msgfree(LDAPLMAP.ldap_res);
7517c478bd9Sstevel@tonic-gate 		LDAPLMAP.ldap_res = NULL;
7527c478bd9Sstevel@tonic-gate 	}
7537c478bd9Sstevel@tonic-gate 	if (ret == EX_OK)
7547c478bd9Sstevel@tonic-gate 	{
7557c478bd9Sstevel@tonic-gate 		if (need == 0)
7567c478bd9Sstevel@tonic-gate 		{
7577c478bd9Sstevel@tonic-gate 			(void) sm_strlcpy(user->mbdb_name, name,
7587c478bd9Sstevel@tonic-gate 					  sizeof(user->mbdb_name));
7597c478bd9Sstevel@tonic-gate 			save_errno = 0;
7607c478bd9Sstevel@tonic-gate 		}
7617c478bd9Sstevel@tonic-gate 		else
7627c478bd9Sstevel@tonic-gate 		{
7637c478bd9Sstevel@tonic-gate 			ret = EX_NOUSER;
7647c478bd9Sstevel@tonic-gate 			save_errno = EINVAL;
7657c478bd9Sstevel@tonic-gate 		}
7667c478bd9Sstevel@tonic-gate 	}
7677c478bd9Sstevel@tonic-gate 	errno = save_errno;
7687c478bd9Sstevel@tonic-gate 	return ret;
7697c478bd9Sstevel@tonic-gate }
7707c478bd9Sstevel@tonic-gate 
7717c478bd9Sstevel@tonic-gate /*
7727c478bd9Sstevel@tonic-gate **  MBDB_LDAP_TERMINATE -- terminate connection to the mailbox database
7737c478bd9Sstevel@tonic-gate **
7747c478bd9Sstevel@tonic-gate **	Parameters:
7757c478bd9Sstevel@tonic-gate **		none.
7767c478bd9Sstevel@tonic-gate **
7777c478bd9Sstevel@tonic-gate **	Results:
7787c478bd9Sstevel@tonic-gate **		none.
7797c478bd9Sstevel@tonic-gate */
7807c478bd9Sstevel@tonic-gate 
7817c478bd9Sstevel@tonic-gate static void
mbdb_ldap_terminate()7827c478bd9Sstevel@tonic-gate mbdb_ldap_terminate()
7837c478bd9Sstevel@tonic-gate {
7847c478bd9Sstevel@tonic-gate 	sm_ldap_close(&LDAPLMAP);
7857c478bd9Sstevel@tonic-gate }
7867c478bd9Sstevel@tonic-gate # endif /* _LDAP_EXAMPLE_ */
7877c478bd9Sstevel@tonic-gate #endif /* LDAPMAP */
788