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 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 #include <sys/types.h>
30 #include <stdio.h>
31 #include <string.h>
32 #include <stdlib.h>
33 #include <nss_dbdefs.h>
34 #include <auth_attr.h>
35 
36 
37 /* Externs from libnsl */
38 extern authstr_t *_getauthnam(const char *, authstr_t *, char *, int, int *);
39 extern authstr_t *_getauthattr(authstr_t *, char *, int, int *);
40 extern void _setauthattr();
41 extern void _endauthattr(void);
42 
43 
44 static authattr_t *authstr2attr(authstr_t *);
45 
46 
47 authattr_t *
getauthattr()48 getauthattr()
49 {
50 	int		err = 0;
51 	char		buf[NSS_BUFLEN_AUTHATTR];
52 	authstr_t	auth;
53 	authstr_t	*tmp;
54 
55 	(void) memset(&auth, 0, sizeof (authstr_t));
56 	tmp = _getauthattr(&auth, buf, NSS_BUFLEN_AUTHATTR, &err);
57 	return (authstr2attr(tmp));
58 }
59 
60 
61 authattr_t *
getauthnam(const char * name)62 getauthnam(const char *name)
63 {
64 	int		err = 0;
65 	char		buf[NSS_BUFLEN_AUTHATTR];
66 	authstr_t	auth;
67 	authstr_t	*tmp;
68 
69 	if (name == NULL) {
70 		return ((authattr_t *)NULL);
71 	}
72 	(void) memset(&auth, 0, sizeof (authstr_t));
73 	tmp = _getauthnam(name, &auth, buf, NSS_BUFLEN_AUTHATTR, &err);
74 	return (authstr2attr(tmp));
75 }
76 
77 
78 void
setauthattr(void)79 setauthattr(void)
80 {
81 	_setauthattr();
82 }
83 
84 
85 void
endauthattr()86 endauthattr()
87 {
88 	_endauthattr();
89 }
90 
91 
92 void
free_authattr(authattr_t * auth)93 free_authattr(authattr_t *auth)
94 {
95 	if (auth) {
96 		free(auth->name);
97 		free(auth->res1);
98 		free(auth->res2);
99 		free(auth->short_desc);
100 		free(auth->long_desc);
101 		_kva_free(auth->attr);
102 		free(auth);
103 	}
104 }
105 
106 
107 static authattr_t *
authstr2attr(authstr_t * auth)108 authstr2attr(authstr_t *auth)
109 {
110 	authattr_t *newauth;
111 
112 	if (auth == NULL)
113 		return ((authattr_t *)NULL);
114 
115 	if ((newauth = (authattr_t *)malloc(sizeof (authattr_t))) == NULL)
116 		return ((authattr_t *)NULL);
117 
118 	newauth->name = _do_unescape(auth->name);
119 	newauth->res1 = _do_unescape(auth->res1);
120 	newauth->res2 = _do_unescape(auth->res2);
121 	newauth->short_desc = _do_unescape(auth->short_desc);
122 	newauth->long_desc = _do_unescape(auth->long_desc);
123 	newauth->attr = _str2kva(auth->attr, KV_ASSIGN, KV_DELIMITER);
124 	return (newauth);
125 }
126 
127 
128 #ifdef DEBUG
129 void
print_authattr(authattr_t * auth)130 print_authattr(authattr_t *auth)
131 {
132 	extern void print_kva(kva_t *);
133 	char *empty = "empty";
134 
135 	if (auth == NULL) {
136 		printf("NULL\n");
137 		return;
138 	}
139 
140 	printf("name=%s\n", auth->name ? auth->name : empty);
141 	printf("res1=%s\n", auth->res1 ? auth->res1 : empty);
142 	printf("res2=%s\n", auth->res2 ? auth->res2 : empty);
143 	printf("short_desc=%s\n", auth->short_desc ? auth->short_desc : empty);
144 	printf("long_desc=%s\n", auth->long_desc ? auth->long_desc : empty);
145 	printf("attr=\n");
146 	print_kva(auth->attr);
147 	fflush(stdout);
148 }
149 #endif  /* DEBUG */
150