xref: /onnv-gate/usr/src/lib/libc/port/gen/getspent_r.c (revision 6812:febeba71273d)
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
52830Sdjl  * Common Development and Distribution License (the "License").
62830Sdjl  * 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  */
21*6812Sraf 
220Sstevel@tonic-gate /*
23*6812Sraf  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
280Sstevel@tonic-gate 
29*6812Sraf #include "lint.h"
300Sstevel@tonic-gate #include <mtlib.h>
310Sstevel@tonic-gate #include <sys/types.h>
320Sstevel@tonic-gate #include <shadow.h>
330Sstevel@tonic-gate #include <stdlib.h>
340Sstevel@tonic-gate #include <string.h>
350Sstevel@tonic-gate #include <nss_dbdefs.h>
360Sstevel@tonic-gate #include <stdio.h>
370Sstevel@tonic-gate #include <synch.h>
380Sstevel@tonic-gate 
390Sstevel@tonic-gate int str2spwd(const char *, int, void *,
400Sstevel@tonic-gate 	char *, int);
410Sstevel@tonic-gate 
420Sstevel@tonic-gate static DEFINE_NSS_DB_ROOT(db_root);
430Sstevel@tonic-gate static DEFINE_NSS_GETENT(context);
440Sstevel@tonic-gate 
452830Sdjl void
_nss_initf_shadow(nss_db_params_t * p)460Sstevel@tonic-gate _nss_initf_shadow(nss_db_params_t *p)
470Sstevel@tonic-gate {
480Sstevel@tonic-gate 	p->name	= NSS_DBNAM_SHADOW;
490Sstevel@tonic-gate 	p->config_name    = NSS_DBNAM_PASSWD;	/* Use config for "passwd" */
500Sstevel@tonic-gate 	p->default_config = NSS_DEFCONF_PASSWD;
510Sstevel@tonic-gate }
520Sstevel@tonic-gate 
530Sstevel@tonic-gate struct spwd *
getspnam_r(const char * name,struct spwd * result,char * buffer,int buflen)540Sstevel@tonic-gate getspnam_r(const char *name, struct spwd *result, char *buffer, int buflen)
550Sstevel@tonic-gate {
560Sstevel@tonic-gate 	nss_XbyY_args_t arg;
570Sstevel@tonic-gate 
580Sstevel@tonic-gate 	NSS_XbyY_INIT(&arg, result, buffer, buflen, str2spwd);
590Sstevel@tonic-gate 	arg.key.name = name;
60*6812Sraf 	(void) nss_search(&db_root, _nss_initf_shadow,
61*6812Sraf 	    NSS_DBOP_SHADOW_BYNAME, &arg);
620Sstevel@tonic-gate 	return ((struct spwd *)NSS_XbyY_FINI(&arg));
630Sstevel@tonic-gate }
640Sstevel@tonic-gate 
650Sstevel@tonic-gate void
setspent(void)660Sstevel@tonic-gate setspent(void)
670Sstevel@tonic-gate {
680Sstevel@tonic-gate 	nss_setent(&db_root, _nss_initf_shadow, &context);
690Sstevel@tonic-gate }
700Sstevel@tonic-gate 
710Sstevel@tonic-gate void
endspent(void)720Sstevel@tonic-gate endspent(void)
730Sstevel@tonic-gate {
740Sstevel@tonic-gate 	nss_endent(&db_root, _nss_initf_shadow, &context);
750Sstevel@tonic-gate 	nss_delete(&db_root);
760Sstevel@tonic-gate }
770Sstevel@tonic-gate 
780Sstevel@tonic-gate struct spwd *
getspent_r(struct spwd * result,char * buffer,int buflen)790Sstevel@tonic-gate getspent_r(struct spwd *result, char *buffer, int buflen)
800Sstevel@tonic-gate {
810Sstevel@tonic-gate 	nss_XbyY_args_t arg;
820Sstevel@tonic-gate 	char		*nam;
830Sstevel@tonic-gate 
840Sstevel@tonic-gate 	/* In getXXent_r(), protect the unsuspecting caller from +/- entries */
850Sstevel@tonic-gate 
860Sstevel@tonic-gate 	do {
870Sstevel@tonic-gate 		NSS_XbyY_INIT(&arg, result, buffer, buflen, str2spwd);
880Sstevel@tonic-gate 		/* No key to fill in */
890Sstevel@tonic-gate 		(void) nss_getent(&db_root, _nss_initf_shadow, &context, &arg);
900Sstevel@tonic-gate 	} while (arg.returnval != 0 &&
91*6812Sraf 	    (nam = ((struct spwd *)arg.returnval)->sp_namp) != 0 &&
92*6812Sraf 	    (*nam == '+' || *nam == '-'));
930Sstevel@tonic-gate 
940Sstevel@tonic-gate 	return (struct spwd *)NSS_XbyY_FINI(&arg);
950Sstevel@tonic-gate }
960Sstevel@tonic-gate 
970Sstevel@tonic-gate struct spwd *
fgetspent_r(FILE * f,struct spwd * result,char * buffer,int buflen)980Sstevel@tonic-gate fgetspent_r(FILE *f, struct spwd *result, char *buffer, int buflen)
990Sstevel@tonic-gate {
1000Sstevel@tonic-gate 	extern void	_nss_XbyY_fgets(FILE *, nss_XbyY_args_t *);
1010Sstevel@tonic-gate 	nss_XbyY_args_t	arg;
1020Sstevel@tonic-gate 
1030Sstevel@tonic-gate 	/* ... but in fgetXXent_r, the caller deserves any +/- entry he gets */
1040Sstevel@tonic-gate 
1050Sstevel@tonic-gate 	/* No key to fill in */
1060Sstevel@tonic-gate 	NSS_XbyY_INIT(&arg, result, buffer, buflen, str2spwd);
1070Sstevel@tonic-gate 	_nss_XbyY_fgets(f, &arg);
1080Sstevel@tonic-gate 	return (struct spwd *)NSS_XbyY_FINI(&arg);
1090Sstevel@tonic-gate }
1100Sstevel@tonic-gate 
1110Sstevel@tonic-gate typedef const char *constp;
1120Sstevel@tonic-gate 
1130Sstevel@tonic-gate static int	/* 1 means success and more input, 0 means error or no more */
getfield(constp * nextp,constp limit,int uns,void * valp)1140Sstevel@tonic-gate getfield(constp *nextp, constp limit, int uns, void *valp)
1150Sstevel@tonic-gate {
1160Sstevel@tonic-gate 	constp		p = *nextp;
1170Sstevel@tonic-gate 	char		*endfield;
1180Sstevel@tonic-gate 	char		numbuf[12];  /* Holds -2^31 and trailing ':' */
1190Sstevel@tonic-gate 	size_t		len;
1200Sstevel@tonic-gate 
121729Sbasabi 	if (p == 0 || p >= limit) {
1220Sstevel@tonic-gate 		return (0);
1230Sstevel@tonic-gate 	}
1240Sstevel@tonic-gate 	if (*p == ':') {
1250Sstevel@tonic-gate 		p++;
1260Sstevel@tonic-gate 		*nextp = p;
1270Sstevel@tonic-gate 		return (p < limit);
1280Sstevel@tonic-gate 	}
1290Sstevel@tonic-gate 	if ((len = limit - p) > sizeof (numbuf) - 1) {
1300Sstevel@tonic-gate 		len = sizeof (numbuf) - 1;
1310Sstevel@tonic-gate 	}
1320Sstevel@tonic-gate 	/*
1330Sstevel@tonic-gate 	 * We want to use strtol() and we have a readonly non-zero-terminated
1340Sstevel@tonic-gate 	 *   string, so first we copy and terminate the interesting bit.
1350Sstevel@tonic-gate 	 *   Ugh.  (It's convenient to terminate with a colon rather than \0).
1360Sstevel@tonic-gate 	 */
1370Sstevel@tonic-gate 	if ((endfield = memccpy(numbuf, p, ':', len)) == 0) {
1380Sstevel@tonic-gate 		if (len != limit - p) {
1390Sstevel@tonic-gate 			/* Error -- field is too big to be a legit number */
1400Sstevel@tonic-gate 			return (0);
1410Sstevel@tonic-gate 		}
1420Sstevel@tonic-gate 		numbuf[len] = ':';
1430Sstevel@tonic-gate 		p = limit;
1440Sstevel@tonic-gate 	} else {
1450Sstevel@tonic-gate 		p += (endfield - numbuf);
1460Sstevel@tonic-gate 	}
1470Sstevel@tonic-gate 	if (uns) {
1480Sstevel@tonic-gate 		unsigned long ux = strtoul(numbuf, &endfield, 10);
1490Sstevel@tonic-gate 		if (*endfield != ':') {
1500Sstevel@tonic-gate 			/* Error -- expected <integer><colon> */
1510Sstevel@tonic-gate 			return (0);
1520Sstevel@tonic-gate 		}
1530Sstevel@tonic-gate 		*((unsigned int *)valp) = (unsigned int)ux;
1540Sstevel@tonic-gate 	} else {
1550Sstevel@tonic-gate 		long x = strtol(numbuf, &endfield, 10);
1560Sstevel@tonic-gate 		if (*endfield != ':') {
1570Sstevel@tonic-gate 			/* Error -- expected <integer><colon> */
1580Sstevel@tonic-gate 			return (0);
1590Sstevel@tonic-gate 		}
1600Sstevel@tonic-gate 		*((int *)valp) = (int)x;
1610Sstevel@tonic-gate 	}
1620Sstevel@tonic-gate 	*nextp = p;
1630Sstevel@tonic-gate 	return (p < limit);
1640Sstevel@tonic-gate }
1650Sstevel@tonic-gate 
1660Sstevel@tonic-gate /*
1670Sstevel@tonic-gate  *  str2spwd() -- convert a string to a shadow passwd entry.  The parser is
1680Sstevel@tonic-gate  *	more liberal than the passwd or group parsers;  since it's legitimate
1690Sstevel@tonic-gate  *	for almost all the fields here to be blank, the parser lets one omit
1700Sstevel@tonic-gate  *	any number of blank fields at the end of the entry.  The acceptable
1710Sstevel@tonic-gate  *	forms for '+' and '-' entries are the same as those for normal entries.
1720Sstevel@tonic-gate  *  === Is this likely to do more harm than good?
1730Sstevel@tonic-gate  *
1740Sstevel@tonic-gate  * Return values: 0 = success, 1 = parse error, 2 = erange ...
1750Sstevel@tonic-gate  * The structure pointer passed in is a structure in the caller's space
1760Sstevel@tonic-gate  * wherein the field pointers would be set to areas in the buffer if
1770Sstevel@tonic-gate  * need be. instring and buffer should be separate areas.
1780Sstevel@tonic-gate  */
1790Sstevel@tonic-gate int
str2spwd(const char * instr,int lenstr,void * ent,char * buffer,int buflen)1800Sstevel@tonic-gate str2spwd(const char *instr, int lenstr, void *ent, char *buffer, int buflen)
1810Sstevel@tonic-gate {
1820Sstevel@tonic-gate 	struct spwd	*shadow	= (struct spwd *)ent;
1830Sstevel@tonic-gate 	const char	*p = instr, *limit;
1840Sstevel@tonic-gate 	char	*bufp;
1850Sstevel@tonic-gate 	int	black_magic;
1860Sstevel@tonic-gate 	size_t	lencopy;
1870Sstevel@tonic-gate 
1880Sstevel@tonic-gate 	limit = p + lenstr;
1890Sstevel@tonic-gate 	if ((p = memchr(instr, ':', lenstr)) == 0 ||
190*6812Sraf 	    ++p >= limit ||
191*6812Sraf 	    (p = memchr(p, ':', limit - p)) == 0) {
1920Sstevel@tonic-gate 		lencopy = (size_t)lenstr;
1930Sstevel@tonic-gate 		p = 0;
1940Sstevel@tonic-gate 	} else {
1950Sstevel@tonic-gate 		lencopy = p - instr;
1960Sstevel@tonic-gate 		p++;
1970Sstevel@tonic-gate 	}
1980Sstevel@tonic-gate 	if (lencopy + 1 > buflen) {
1990Sstevel@tonic-gate 		return (NSS_STR_PARSE_ERANGE);
2000Sstevel@tonic-gate 	}
2012830Sdjl 
2022830Sdjl 	if (instr != buffer) {
2032830Sdjl 		/* Overlapping buffer copies are OK */
2042830Sdjl 		(void) memmove(buffer, instr, lencopy);
2052830Sdjl 		buffer[lencopy] = 0;
2062830Sdjl 	}
2072830Sdjl 
2082830Sdjl 	/* quick exit do not entry fill if not needed */
2092830Sdjl 	if (ent == (void *)NULL)
2102830Sdjl 		return (NSS_STR_PARSE_SUCCESS);
2110Sstevel@tonic-gate 
2120Sstevel@tonic-gate 	black_magic = (*instr == '+' || *instr == '-');
2130Sstevel@tonic-gate 	shadow->sp_namp = bufp = buffer;
2140Sstevel@tonic-gate 	shadow->sp_pwdp	= 0;
2150Sstevel@tonic-gate 	shadow->sp_lstchg = -1;
2160Sstevel@tonic-gate 	shadow->sp_min	= -1;
2170Sstevel@tonic-gate 	shadow->sp_max	= -1;
2180Sstevel@tonic-gate 	shadow->sp_warn	= -1;
2190Sstevel@tonic-gate 	shadow->sp_inact = -1;
2200Sstevel@tonic-gate 	shadow->sp_expire = -1;
2210Sstevel@tonic-gate 	shadow->sp_flag	= 0;
2220Sstevel@tonic-gate 
2230Sstevel@tonic-gate 	if ((bufp = strchr(bufp, ':')) == 0) {
2240Sstevel@tonic-gate 		if (black_magic)
2250Sstevel@tonic-gate 			return (NSS_STR_PARSE_SUCCESS);
2260Sstevel@tonic-gate 		else
2270Sstevel@tonic-gate 			return (NSS_STR_PARSE_PARSE);
2280Sstevel@tonic-gate 	}
2290Sstevel@tonic-gate 	*bufp++ = '\0';
2300Sstevel@tonic-gate 
2310Sstevel@tonic-gate 	shadow->sp_pwdp = bufp;
2320Sstevel@tonic-gate 	if (instr == 0) {
2330Sstevel@tonic-gate 		if ((bufp = strchr(bufp, ':')) == 0) {
2340Sstevel@tonic-gate 			if (black_magic)
2350Sstevel@tonic-gate 				return (NSS_STR_PARSE_SUCCESS);
2360Sstevel@tonic-gate 			else
2370Sstevel@tonic-gate 				return (NSS_STR_PARSE_PARSE);
2380Sstevel@tonic-gate 		}
2390Sstevel@tonic-gate 		*bufp++ = '\0';
2400Sstevel@tonic-gate 		p = bufp;
2410Sstevel@tonic-gate 	} /* else p was set when we copied name and passwd into the buffer */
2420Sstevel@tonic-gate 
2430Sstevel@tonic-gate 	if (!getfield(&p, limit, 0, &shadow->sp_lstchg))
2440Sstevel@tonic-gate 			return (NSS_STR_PARSE_SUCCESS);
2450Sstevel@tonic-gate 	if (!getfield(&p, limit, 0, &shadow->sp_min))
2460Sstevel@tonic-gate 			return (NSS_STR_PARSE_SUCCESS);
2470Sstevel@tonic-gate 	if (!getfield(&p, limit, 0, &shadow->sp_max))
2480Sstevel@tonic-gate 			return (NSS_STR_PARSE_SUCCESS);
2490Sstevel@tonic-gate 	if (!getfield(&p, limit, 0, &shadow->sp_warn))
2500Sstevel@tonic-gate 			return (NSS_STR_PARSE_SUCCESS);
2510Sstevel@tonic-gate 	if (!getfield(&p, limit, 0, &shadow->sp_inact))
2520Sstevel@tonic-gate 			return (NSS_STR_PARSE_SUCCESS);
2530Sstevel@tonic-gate 	if (!getfield(&p, limit, 0, &shadow->sp_expire))
2540Sstevel@tonic-gate 			return (NSS_STR_PARSE_SUCCESS);
2550Sstevel@tonic-gate 	if (!getfield(&p, limit, 1, &shadow->sp_flag))
2560Sstevel@tonic-gate 			return (NSS_STR_PARSE_SUCCESS);
2570Sstevel@tonic-gate 	if (p != limit) {
2580Sstevel@tonic-gate 		/* Syntax error -- garbage at end of line */
2590Sstevel@tonic-gate 		return (NSS_STR_PARSE_PARSE);
2600Sstevel@tonic-gate 	}
2610Sstevel@tonic-gate 	return (NSS_STR_PARSE_SUCCESS);
2620Sstevel@tonic-gate }
263