xref: /onnv-gate/usr/src/lib/libsecdb/common/getuserattr.c (revision 2830:5228d1267a01)
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
5*2830Sdjl  * Common Development and Distribution License (the "License").
6*2830Sdjl  * 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*2830Sdjl  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
270Sstevel@tonic-gate 
280Sstevel@tonic-gate #include <sys/types.h>
290Sstevel@tonic-gate #include <sys/mman.h>
300Sstevel@tonic-gate #include <stdio.h>
310Sstevel@tonic-gate #include <string.h>
320Sstevel@tonic-gate #include <stdlib.h>
330Sstevel@tonic-gate #include <nss_dbdefs.h>
340Sstevel@tonic-gate #include <user_attr.h>
350Sstevel@tonic-gate #include <getxby_door.h>
360Sstevel@tonic-gate #include <pwd.h>
370Sstevel@tonic-gate 
380Sstevel@tonic-gate 
390Sstevel@tonic-gate /* Externs from libnsl */
400Sstevel@tonic-gate extern userstr_t *_getusernam(const char *, userstr_t *, char *, int, int *);
410Sstevel@tonic-gate extern userstr_t *_getuserattr(userstr_t *, char *, int, int *);
420Sstevel@tonic-gate extern userstr_t *_fgetuserattr(FILE *, userstr_t *, char *, int);
430Sstevel@tonic-gate extern void _setuserattr(void);
440Sstevel@tonic-gate extern void _enduserattr(void);
450Sstevel@tonic-gate 
460Sstevel@tonic-gate 
470Sstevel@tonic-gate static userattr_t *userstr2attr(userstr_t *);
480Sstevel@tonic-gate 
490Sstevel@tonic-gate 
500Sstevel@tonic-gate userattr_t *
getuserattr()510Sstevel@tonic-gate getuserattr()
520Sstevel@tonic-gate {
530Sstevel@tonic-gate 	int		err = 0;
540Sstevel@tonic-gate 	char		buf[NSS_BUFLEN_USERATTR];
550Sstevel@tonic-gate 	userstr_t	user;
560Sstevel@tonic-gate 	userstr_t	*tmp;
570Sstevel@tonic-gate 
580Sstevel@tonic-gate 	(void) memset(&user, 0, sizeof (userattr_t));
590Sstevel@tonic-gate 	tmp = _getuserattr(&user, buf, NSS_BUFLEN_USERATTR, &err);
600Sstevel@tonic-gate 	return (userstr2attr(tmp));
610Sstevel@tonic-gate }
620Sstevel@tonic-gate 
630Sstevel@tonic-gate 
640Sstevel@tonic-gate userattr_t *
fgetuserattr(FILE * f)650Sstevel@tonic-gate fgetuserattr(FILE *f)
660Sstevel@tonic-gate {
670Sstevel@tonic-gate 	char		buf[NSS_BUFLEN_USERATTR];
680Sstevel@tonic-gate 	userstr_t	user;
690Sstevel@tonic-gate 	userstr_t	*tmp;
700Sstevel@tonic-gate 
710Sstevel@tonic-gate 	(void) memset(&user, 0, sizeof (userattr_t));
720Sstevel@tonic-gate 	tmp = _fgetuserattr(f, &user, buf, NSS_BUFLEN_USERATTR);
730Sstevel@tonic-gate 	return (userstr2attr(tmp));
740Sstevel@tonic-gate }
750Sstevel@tonic-gate 
760Sstevel@tonic-gate 
770Sstevel@tonic-gate userattr_t *
getusernam(const char * name)780Sstevel@tonic-gate getusernam(const char *name)
790Sstevel@tonic-gate {
800Sstevel@tonic-gate 	int		err = 0;
810Sstevel@tonic-gate 	char		buf[NSS_BUFLEN_USERATTR];
820Sstevel@tonic-gate 	userstr_t	user;
830Sstevel@tonic-gate 	userstr_t	*resptr = (userstr_t *)NULL;
840Sstevel@tonic-gate 
850Sstevel@tonic-gate 	resptr = _getusernam(name, &user, buf, NSS_BUFLEN_USERATTR, &err);
860Sstevel@tonic-gate 
870Sstevel@tonic-gate 	return (userstr2attr(resptr));
880Sstevel@tonic-gate 
890Sstevel@tonic-gate }
900Sstevel@tonic-gate 
910Sstevel@tonic-gate 
920Sstevel@tonic-gate userattr_t *
getuseruid(uid_t u)930Sstevel@tonic-gate getuseruid(uid_t u)
940Sstevel@tonic-gate {
950Sstevel@tonic-gate 	struct	passwd pwd;
960Sstevel@tonic-gate 	char	buf[NSS_BUFLEN_PASSWD];
970Sstevel@tonic-gate 
980Sstevel@tonic-gate 	if (getpwuid_r(u, &pwd, buf, NSS_BUFLEN_PASSWD) == NULL)
990Sstevel@tonic-gate 		return ((userattr_t *)NULL);
1000Sstevel@tonic-gate 	return (getusernam(pwd.pw_name));
1010Sstevel@tonic-gate }
1020Sstevel@tonic-gate 
1030Sstevel@tonic-gate 
1040Sstevel@tonic-gate void
setuserattr()1050Sstevel@tonic-gate setuserattr()
1060Sstevel@tonic-gate {
1070Sstevel@tonic-gate 	_setuserattr();
1080Sstevel@tonic-gate }
1090Sstevel@tonic-gate 
1100Sstevel@tonic-gate 
1110Sstevel@tonic-gate void
enduserattr()1120Sstevel@tonic-gate enduserattr()
1130Sstevel@tonic-gate {
1140Sstevel@tonic-gate 	_enduserattr();
1150Sstevel@tonic-gate }
1160Sstevel@tonic-gate 
1170Sstevel@tonic-gate 
1180Sstevel@tonic-gate void
free_userattr(userattr_t * user)1190Sstevel@tonic-gate free_userattr(userattr_t *user)
1200Sstevel@tonic-gate {
1210Sstevel@tonic-gate 	if (user) {
1220Sstevel@tonic-gate 		free(user->name);
1230Sstevel@tonic-gate 		free(user->qualifier);
1240Sstevel@tonic-gate 		free(user->res1);
1250Sstevel@tonic-gate 		free(user->res2);
1260Sstevel@tonic-gate 		_kva_free(user->attr);
1270Sstevel@tonic-gate 		free(user);
1280Sstevel@tonic-gate 	}
1290Sstevel@tonic-gate }
1300Sstevel@tonic-gate 
1310Sstevel@tonic-gate 
1320Sstevel@tonic-gate static userattr_t *
userstr2attr(userstr_t * user)1330Sstevel@tonic-gate userstr2attr(userstr_t *user)
1340Sstevel@tonic-gate {
1350Sstevel@tonic-gate 	userattr_t *newuser;
1360Sstevel@tonic-gate 
1370Sstevel@tonic-gate 	if (user == NULL)
1380Sstevel@tonic-gate 		return ((userattr_t *)NULL);
1390Sstevel@tonic-gate 
1400Sstevel@tonic-gate 	if ((newuser = (userattr_t *)malloc(sizeof (userattr_t))) == NULL)
1410Sstevel@tonic-gate 		return ((userattr_t *)NULL);
1420Sstevel@tonic-gate 
1430Sstevel@tonic-gate 	newuser->name = _do_unescape(user->name);
1440Sstevel@tonic-gate 	newuser->qualifier = _do_unescape(user->qualifier);
1450Sstevel@tonic-gate 	newuser->res1 = _do_unescape(user->res1);
1460Sstevel@tonic-gate 	newuser->res2 = _do_unescape(user->res2);
1470Sstevel@tonic-gate 	newuser->attr = _str2kva(user->attr, KV_ASSIGN, KV_DELIMITER);
1480Sstevel@tonic-gate 	return (newuser);
1490Sstevel@tonic-gate }
1500Sstevel@tonic-gate 
1510Sstevel@tonic-gate 
1520Sstevel@tonic-gate #ifdef DEBUG
1530Sstevel@tonic-gate void
print_userattr(userattr_t * user)1540Sstevel@tonic-gate print_userattr(userattr_t *user)
1550Sstevel@tonic-gate {
1560Sstevel@tonic-gate 	extern void print_kva(kva_t *);
1570Sstevel@tonic-gate 	char *empty = "empty";
1580Sstevel@tonic-gate 
1590Sstevel@tonic-gate 	if (user == NULL) {
1600Sstevel@tonic-gate 		printf("NULL\n");
1610Sstevel@tonic-gate 		return;
1620Sstevel@tonic-gate 	}
1630Sstevel@tonic-gate 
1640Sstevel@tonic-gate 	printf("name=%s\n", user->name ? user->name : empty);
1650Sstevel@tonic-gate 	printf("qualifier=%s\n", user->qualifier ? user->qualifier : empty);
1660Sstevel@tonic-gate 	printf("res1=%s\n", user->res1 ? user->res1 : empty);
1670Sstevel@tonic-gate 	printf("res2=%s\n", user->res2 ? user->res2 : empty);
1680Sstevel@tonic-gate 	printf("attr=\n");
1690Sstevel@tonic-gate 	print_kva(user->attr);
1700Sstevel@tonic-gate 	fflush(stdout);
1710Sstevel@tonic-gate }
1720Sstevel@tonic-gate #endif  /* DEBUG */
173