xref: /titanic_50/usr/src/lib/libsecdb/common/getuserattr.c (revision 67e3a03ed4a2813074d36330f062ed6e593a4937)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27 
28 #include <sys/types.h>
29 #include <sys/mman.h>
30 #include <stdio.h>
31 #include <string.h>
32 #include <stdlib.h>
33 #include <nss_dbdefs.h>
34 #include <user_attr.h>
35 #include <getxby_door.h>
36 #include <pwd.h>
37 
38 
39 /* Externs from libnsl */
40 extern userstr_t *_getusernam(const char *, userstr_t *, char *, int, int *);
41 extern userstr_t *_getuserattr(userstr_t *, char *, int, int *);
42 extern userstr_t *_fgetuserattr(FILE *, userstr_t *, char *, int);
43 extern void _setuserattr(void);
44 extern void _enduserattr(void);
45 
46 
47 static userattr_t *userstr2attr(userstr_t *);
48 
49 
50 userattr_t *
51 getuserattr()
52 {
53 	int		err = 0;
54 	char		buf[NSS_BUFLEN_USERATTR];
55 	userstr_t	user;
56 	userstr_t	*tmp;
57 
58 	(void) memset(&user, 0, sizeof (userattr_t));
59 	tmp = _getuserattr(&user, buf, NSS_BUFLEN_USERATTR, &err);
60 	return (userstr2attr(tmp));
61 }
62 
63 
64 userattr_t *
65 fgetuserattr(FILE *f)
66 {
67 	char		buf[NSS_BUFLEN_USERATTR];
68 	userstr_t	user;
69 	userstr_t	*tmp;
70 
71 	(void) memset(&user, 0, sizeof (userattr_t));
72 	tmp = _fgetuserattr(f, &user, buf, NSS_BUFLEN_USERATTR);
73 	return (userstr2attr(tmp));
74 }
75 
76 
77 userattr_t *
78 getusernam(const char *name)
79 {
80 	int		err = 0;
81 	char		buf[NSS_BUFLEN_USERATTR];
82 	userstr_t	user;
83 	userstr_t	*resptr = (userstr_t *)NULL;
84 
85 	resptr = _getusernam(name, &user, buf, NSS_BUFLEN_USERATTR, &err);
86 
87 	return (userstr2attr(resptr));
88 
89 }
90 
91 
92 userattr_t *
93 getuseruid(uid_t u)
94 {
95 	struct	passwd pwd;
96 	char	buf[NSS_BUFLEN_PASSWD];
97 
98 	if (getpwuid_r(u, &pwd, buf, NSS_BUFLEN_PASSWD) == NULL)
99 		return ((userattr_t *)NULL);
100 	return (getusernam(pwd.pw_name));
101 }
102 
103 
104 void
105 setuserattr()
106 {
107 	_setuserattr();
108 }
109 
110 
111 void
112 enduserattr()
113 {
114 	_enduserattr();
115 }
116 
117 
118 void
119 free_userattr(userattr_t *user)
120 {
121 	if (user) {
122 		free(user->name);
123 		free(user->qualifier);
124 		free(user->res1);
125 		free(user->res2);
126 		_kva_free(user->attr);
127 		free(user);
128 	}
129 }
130 
131 
132 static userattr_t *
133 userstr2attr(userstr_t *user)
134 {
135 	userattr_t *newuser;
136 
137 	if (user == NULL)
138 		return ((userattr_t *)NULL);
139 
140 	if ((newuser = (userattr_t *)malloc(sizeof (userattr_t))) == NULL)
141 		return ((userattr_t *)NULL);
142 
143 	newuser->name = _do_unescape(user->name);
144 	newuser->qualifier = _do_unescape(user->qualifier);
145 	newuser->res1 = _do_unescape(user->res1);
146 	newuser->res2 = _do_unescape(user->res2);
147 	newuser->attr = _str2kva(user->attr, KV_ASSIGN, KV_DELIMITER);
148 	return (newuser);
149 }
150 
151 
152 #ifdef DEBUG
153 void
154 print_userattr(userattr_t *user)
155 {
156 	extern void print_kva(kva_t *);
157 	char *empty = "empty";
158 
159 	if (user == NULL) {
160 		printf("NULL\n");
161 		return;
162 	}
163 
164 	printf("name=%s\n", user->name ? user->name : empty);
165 	printf("qualifier=%s\n", user->qualifier ? user->qualifier : empty);
166 	printf("res1=%s\n", user->res1 ? user->res1 : empty);
167 	printf("res2=%s\n", user->res2 ? user->res2 : empty);
168 	printf("attr=\n");
169 	print_kva(user->attr);
170 	fflush(stdout);
171 }
172 #endif  /* DEBUG */
173