10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
50Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
60Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
70Sstevel@tonic-gate  * with the License.
80Sstevel@tonic-gate  *
90Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
100Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
110Sstevel@tonic-gate  * See the License for the specific language governing permissions
120Sstevel@tonic-gate  * and limitations under the License.
130Sstevel@tonic-gate  *
140Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
150Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
160Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
170Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
180Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
190Sstevel@tonic-gate  *
200Sstevel@tonic-gate  * CDDL HEADER END
210Sstevel@tonic-gate  */
22132Srobinson 
230Sstevel@tonic-gate /*
24*1219Sraf  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
25132Srobinson  * Use is subject to license terms.
260Sstevel@tonic-gate  */
270Sstevel@tonic-gate 
280Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
290Sstevel@tonic-gate 
30*1219Sraf #include "mt.h"
310Sstevel@tonic-gate #include <stdio.h>
320Sstevel@tonic-gate #include <stdlib.h>
330Sstevel@tonic-gate #include <limits.h>
340Sstevel@tonic-gate #include <sys/types.h>
350Sstevel@tonic-gate #include <nss_dbdefs.h>
360Sstevel@tonic-gate #include <string.h>
370Sstevel@tonic-gate #include <user_attr.h>
380Sstevel@tonic-gate 
390Sstevel@tonic-gate /* externs from libc */
400Sstevel@tonic-gate extern void _nss_XbyY_fgets(FILE *, nss_XbyY_args_t *);
410Sstevel@tonic-gate /* externs from parse.c */
420Sstevel@tonic-gate extern char *_strtok_escape(char *, char *, char **);
430Sstevel@tonic-gate 
440Sstevel@tonic-gate 
450Sstevel@tonic-gate static int userattr_stayopen;
460Sstevel@tonic-gate 
470Sstevel@tonic-gate /*
480Sstevel@tonic-gate  * Unsynchronized, but it affects only
490Sstevel@tonic-gate  * efficiency, not correctness
500Sstevel@tonic-gate  */
510Sstevel@tonic-gate 
520Sstevel@tonic-gate static DEFINE_NSS_DB_ROOT(db_root);
530Sstevel@tonic-gate static DEFINE_NSS_GETENT(context);
540Sstevel@tonic-gate 
550Sstevel@tonic-gate 
560Sstevel@tonic-gate void
570Sstevel@tonic-gate _nss_initf_userattr(nss_db_params_t *p)
580Sstevel@tonic-gate {
590Sstevel@tonic-gate 	p->name = NSS_DBNAM_USERATTR;
600Sstevel@tonic-gate 	p->config_name    = NSS_DBNAM_PASSWD; /* use config for "passwd" */
610Sstevel@tonic-gate 	p->default_config = NSS_DEFCONF_USERATTR;
620Sstevel@tonic-gate }
630Sstevel@tonic-gate 
640Sstevel@tonic-gate 
650Sstevel@tonic-gate /*
660Sstevel@tonic-gate  * Return values: 0 = success, 1 = parse error, 2 = erange ...
670Sstevel@tonic-gate  * The structure pointer passed in is a structure in the caller's space
680Sstevel@tonic-gate  * wherein the field pointers would be set to areas in the buffer if
690Sstevel@tonic-gate  * need be. instring and buffer should be separate areas.
700Sstevel@tonic-gate  */
710Sstevel@tonic-gate int
720Sstevel@tonic-gate str2userattr(const char *instr, int lenstr, void *ent, char *buffer, int buflen)
730Sstevel@tonic-gate {
74132Srobinson 	char		*last = NULL;
750Sstevel@tonic-gate 	char		*sep = KV_TOKEN_DELIMIT;
760Sstevel@tonic-gate 	userstr_t	*user = (userstr_t *)ent;
770Sstevel@tonic-gate 
780Sstevel@tonic-gate 	if ((instr >= buffer && (buffer + buflen) > instr) ||
79132Srobinson 	    (buffer >= instr && (instr + lenstr) > buffer))
800Sstevel@tonic-gate 		return (NSS_STR_PARSE_PARSE);
81132Srobinson 	if (lenstr >= buflen)
820Sstevel@tonic-gate 		return (NSS_STR_PARSE_ERANGE);
83132Srobinson 	(void) strncpy(buffer, instr, buflen);
840Sstevel@tonic-gate 	/*
850Sstevel@tonic-gate 	 * Remove newline that nis (yp_match) puts at the
860Sstevel@tonic-gate 	 * end of the entry it retrieves from the map.
870Sstevel@tonic-gate 	 */
880Sstevel@tonic-gate 	if (buffer[lenstr] == '\n') {
890Sstevel@tonic-gate 		buffer[lenstr] = '\0';
900Sstevel@tonic-gate 	}
910Sstevel@tonic-gate 
920Sstevel@tonic-gate 	user->name = _strtok_escape(buffer, sep, &last);
930Sstevel@tonic-gate 	user->qualifier = _strtok_escape(NULL, sep, &last);
940Sstevel@tonic-gate 	user->res1 = _strtok_escape(NULL, sep, &last);
950Sstevel@tonic-gate 	user->res2 = _strtok_escape(NULL, sep, &last);
960Sstevel@tonic-gate 	user->attr = _strtok_escape(NULL, sep, &last);
970Sstevel@tonic-gate 
980Sstevel@tonic-gate 	return (0);
990Sstevel@tonic-gate }
1000Sstevel@tonic-gate 
1010Sstevel@tonic-gate 
1020Sstevel@tonic-gate void
1030Sstevel@tonic-gate _setuserattr(void)
1040Sstevel@tonic-gate {
1050Sstevel@tonic-gate 	userattr_stayopen = 0;
1060Sstevel@tonic-gate 	nss_setent(&db_root, _nss_initf_userattr, &context);
1070Sstevel@tonic-gate }
1080Sstevel@tonic-gate 
1090Sstevel@tonic-gate 
1100Sstevel@tonic-gate void
1110Sstevel@tonic-gate _enduserattr(void)
1120Sstevel@tonic-gate {
1130Sstevel@tonic-gate 	userattr_stayopen = 0;
1140Sstevel@tonic-gate 	nss_endent(&db_root, _nss_initf_userattr, &context);
1150Sstevel@tonic-gate 	nss_delete(&db_root);
1160Sstevel@tonic-gate }
1170Sstevel@tonic-gate 
1180Sstevel@tonic-gate 
1190Sstevel@tonic-gate userstr_t *
1200Sstevel@tonic-gate _getuserattr(userstr_t *result, char *buffer, int buflen, int *h_errnop)
1210Sstevel@tonic-gate {
1220Sstevel@tonic-gate 	nss_XbyY_args_t arg;
1230Sstevel@tonic-gate 	nss_status_t    res;
1240Sstevel@tonic-gate 
1250Sstevel@tonic-gate 	NSS_XbyY_INIT(&arg, result, buffer, buflen, str2userattr);
1260Sstevel@tonic-gate 	res = nss_getent(&db_root, _nss_initf_userattr, &context, &arg);
1270Sstevel@tonic-gate 	arg.status = res;
1280Sstevel@tonic-gate 	*h_errnop = arg.h_errno;
1290Sstevel@tonic-gate 	return ((userstr_t *)NSS_XbyY_FINI(&arg));
1300Sstevel@tonic-gate }
1310Sstevel@tonic-gate 
1320Sstevel@tonic-gate 
1330Sstevel@tonic-gate userstr_t *
1340Sstevel@tonic-gate _fgetuserattr(FILE *f, userstr_t *result, char *buffer, int buflen)
1350Sstevel@tonic-gate {
1360Sstevel@tonic-gate 	nss_XbyY_args_t arg;
1370Sstevel@tonic-gate 
1380Sstevel@tonic-gate 	NSS_XbyY_INIT(&arg, result, buffer, buflen, str2userattr);
1390Sstevel@tonic-gate 	_nss_XbyY_fgets(f, &arg);
1400Sstevel@tonic-gate 	return ((userstr_t *)NSS_XbyY_FINI(&arg));
1410Sstevel@tonic-gate }
1420Sstevel@tonic-gate 
1430Sstevel@tonic-gate 
1440Sstevel@tonic-gate 
1450Sstevel@tonic-gate userstr_t *
1460Sstevel@tonic-gate _getusernam(const char *name, userstr_t *result, char *buffer, int buflen,
1470Sstevel@tonic-gate     int *errnop)
1480Sstevel@tonic-gate {
1490Sstevel@tonic-gate 	nss_XbyY_args_t arg;
1500Sstevel@tonic-gate 	nss_status_t    res;
1510Sstevel@tonic-gate 
1520Sstevel@tonic-gate 	NSS_XbyY_INIT(&arg, result, buffer, buflen, str2userattr);
1530Sstevel@tonic-gate 	arg.key.name = name;
1540Sstevel@tonic-gate 	arg.stayopen = userattr_stayopen;
1550Sstevel@tonic-gate 	res = nss_search(&db_root, _nss_initf_userattr,
1560Sstevel@tonic-gate 	    NSS_DBOP_USERATTR_BYNAME, &arg);
1570Sstevel@tonic-gate 	arg.status = res;
1580Sstevel@tonic-gate 	*errnop = arg.h_errno;
1590Sstevel@tonic-gate 	return ((userstr_t *)NSS_XbyY_FINI(&arg));
1600Sstevel@tonic-gate }
161