8322 nl: misleading-indentation
[unleashed/tickless.git] / usr / src / lib / libsecdb / common / getuserattr.c
blobdd0789b5dd7dd9365a6e3d0d37b935116d65c657
1 /*
2 * CDDL HEADER START
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.
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.
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]
19 * CDDL HEADER END
22 * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
26 #pragma ident "%Z%%M% %I% %E% SMI"
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>
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);
47 static userattr_t *userstr2attr(userstr_t *);
50 userattr_t *
51 getuserattr()
53 int err = 0;
54 char buf[NSS_BUFLEN_USERATTR];
55 userstr_t user;
56 userstr_t *tmp;
58 (void) memset(&user, 0, sizeof (userattr_t));
59 tmp = _getuserattr(&user, buf, NSS_BUFLEN_USERATTR, &err);
60 return (userstr2attr(tmp));
64 userattr_t *
65 fgetuserattr(FILE *f)
67 char buf[NSS_BUFLEN_USERATTR];
68 userstr_t user;
69 userstr_t *tmp;
71 (void) memset(&user, 0, sizeof (userattr_t));
72 tmp = _fgetuserattr(f, &user, buf, NSS_BUFLEN_USERATTR);
73 return (userstr2attr(tmp));
77 userattr_t *
78 getusernam(const char *name)
80 int err = 0;
81 char buf[NSS_BUFLEN_USERATTR];
82 userstr_t user;
83 userstr_t *resptr = (userstr_t *)NULL;
85 resptr = _getusernam(name, &user, buf, NSS_BUFLEN_USERATTR, &err);
87 return (userstr2attr(resptr));
92 userattr_t *
93 getuseruid(uid_t u)
95 struct passwd pwd;
96 char buf[NSS_BUFLEN_PASSWD];
98 if (getpwuid_r(u, &pwd, buf, NSS_BUFLEN_PASSWD) == NULL)
99 return ((userattr_t *)NULL);
100 return (getusernam(pwd.pw_name));
104 void
105 setuserattr()
107 _setuserattr();
111 void
112 enduserattr()
114 _enduserattr();
118 void
119 free_userattr(userattr_t *user)
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);
132 static userattr_t *
133 userstr2attr(userstr_t *user)
135 userattr_t *newuser;
137 if (user == NULL)
138 return ((userattr_t *)NULL);
140 if ((newuser = (userattr_t *)malloc(sizeof (userattr_t))) == NULL)
141 return ((userattr_t *)NULL);
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);
152 #ifdef DEBUG
153 void
154 print_userattr(userattr_t *user)
156 extern void print_kva(kva_t *);
157 char *empty = "empty";
159 if (user == NULL) {
160 printf("NULL\n");
161 return;
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);
172 #endif /* DEBUG */