xref: /illumos-gate/usr/src/lib/libsecdb/common/getauthattr.c (revision 20a7641f9918de8574b8b3b47dbe35c4bfc78df1)
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, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright (c) 1999, 2001 by Sun Microsystems, Inc.
24  * All rights reserved.
25  */
26 
27 #include <sys/types.h>
28 #include <stdio.h>
29 #include <string.h>
30 #include <stdlib.h>
31 #include <nss_dbdefs.h>
32 #include <auth_attr.h>
33 
34 
35 /* Externs from libnsl */
36 extern authstr_t *_getauthnam(const char *, authstr_t *, char *, int, int *);
37 extern authstr_t *_getauthattr(authstr_t *, char *, int, int *);
38 extern void _setauthattr();
39 extern void _endauthattr(void);
40 
41 
42 static authattr_t *authstr2attr(authstr_t *);
43 
44 
45 authattr_t *
46 getauthattr()
47 {
48 	int		err = 0;
49 	char		buf[NSS_BUFLEN_AUTHATTR];
50 	authstr_t	auth;
51 	authstr_t	*tmp;
52 
53 	(void) memset(&auth, 0, sizeof (authstr_t));
54 	tmp = _getauthattr(&auth, buf, NSS_BUFLEN_AUTHATTR, &err);
55 	return (authstr2attr(tmp));
56 }
57 
58 
59 authattr_t *
60 getauthnam(const char *name)
61 {
62 	int		err = 0;
63 	char		buf[NSS_BUFLEN_AUTHATTR];
64 	authstr_t	auth;
65 	authstr_t	*tmp;
66 
67 	if (name == NULL) {
68 		return ((authattr_t *)NULL);
69 	}
70 	(void) memset(&auth, 0, sizeof (authstr_t));
71 	tmp = _getauthnam(name, &auth, buf, NSS_BUFLEN_AUTHATTR, &err);
72 	return (authstr2attr(tmp));
73 }
74 
75 
76 void
77 setauthattr(void)
78 {
79 	_setauthattr();
80 }
81 
82 
83 void
84 endauthattr()
85 {
86 	_endauthattr();
87 }
88 
89 
90 void
91 free_authattr(authattr_t *auth)
92 {
93 	if (auth) {
94 		free(auth->name);
95 		free(auth->res1);
96 		free(auth->res2);
97 		free(auth->short_desc);
98 		free(auth->long_desc);
99 		_kva_free(auth->attr);
100 		free(auth);
101 	}
102 }
103 
104 
105 static authattr_t *
106 authstr2attr(authstr_t *auth)
107 {
108 	authattr_t *newauth;
109 
110 	if (auth == NULL)
111 		return ((authattr_t *)NULL);
112 
113 	if ((newauth = (authattr_t *)malloc(sizeof (authattr_t))) == NULL)
114 		return ((authattr_t *)NULL);
115 
116 	newauth->name = _do_unescape(auth->name);
117 	newauth->res1 = _do_unescape(auth->res1);
118 	newauth->res2 = _do_unescape(auth->res2);
119 	newauth->short_desc = _do_unescape(auth->short_desc);
120 	newauth->long_desc = _do_unescape(auth->long_desc);
121 	newauth->attr = _str2kva(auth->attr, KV_ASSIGN, KV_DELIMITER);
122 	return (newauth);
123 }
124 
125 
126 #ifdef DEBUG
127 void
128 print_authattr(authattr_t *auth)
129 {
130 	extern void print_kva(kva_t *);
131 	char *empty = "empty";
132 
133 	if (auth == NULL) {
134 		printf("NULL\n");
135 		return;
136 	}
137 
138 	printf("name=%s\n", auth->name ? auth->name : empty);
139 	printf("res1=%s\n", auth->res1 ? auth->res1 : empty);
140 	printf("res2=%s\n", auth->res2 ? auth->res2 : empty);
141 	printf("short_desc=%s\n", auth->short_desc ? auth->short_desc : empty);
142 	printf("long_desc=%s\n", auth->long_desc ? auth->long_desc : empty);
143 	printf("attr=\n");
144 	print_kva(auth->attr);
145 	fflush(stdout);
146 }
147 #endif  /* DEBUG */
148