xref: /illumos-gate/usr/src/lib/libnsl/nss/getprofattr.c (revision 1da57d551424de5a9d469760be7c4b4d4f10a755)
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 /*
23  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #include "mt.h"
28 #include <stdlib.h>
29 #include <sys/types.h>
30 #include <nss_dbdefs.h>
31 #include <string.h>
32 #include <prof_attr.h>
33 
34 /* externs from parse.c */
35 extern char *_strtok_escape(char *, char *, char **);
36 
37 static int profattr_stayopen;
38 /*
39  * Unsynchronized, but it affects only
40  * efficiency, not correctness
41  */
42 
43 static DEFINE_NSS_DB_ROOT(db_root);
44 static DEFINE_NSS_GETENT(context);
45 
46 void
_nss_initf_profattr(nss_db_params_t * p)47 _nss_initf_profattr(nss_db_params_t *p)
48 {
49 	p->name    = NSS_DBNAM_PROFATTR;
50 	p->default_config = NSS_DEFCONF_PROFATTR;
51 }
52 
53 
54 /*
55  * Return values: 0 = success, 1 = parse error, 2 = erange ...
56  * The structure pointer passed in is a structure in the caller's space
57  * wherein the field pointers would be set to areas in the buffer if
58  * need be. instring and buffer should be separate areas.
59  */
60 int
str2profattr(const char * instr,int lenstr,void * ent,char * buffer,int buflen)61 str2profattr(const char *instr, int lenstr, void *ent, char *buffer, int buflen)
62 {
63 	char		*last = NULL;
64 	char		*sep = KV_TOKEN_DELIMIT;
65 	profstr_t	*prof = (profstr_t *)ent;
66 
67 	if (lenstr >= buflen)
68 		return (NSS_STR_PARSE_ERANGE);
69 	if (instr != buffer)
70 		(void) strncpy(buffer, instr, buflen);
71 
72 	/*
73 	 * Remove newline that nis (yp_match) puts at the
74 	 * end of the entry it retrieves from the map.
75 	 */
76 	if (buffer[lenstr] == '\n')
77 		buffer[lenstr] = '\0';
78 
79 	/* quick exit do not entry fill if not needed */
80 	if (ent == (void *)NULL)
81 		return (NSS_STR_PARSE_SUCCESS);
82 
83 	prof->name = _strtok_escape(buffer, sep, &last);
84 	prof->res1 = _strtok_escape(NULL, sep, &last);
85 	prof->res2 = _strtok_escape(NULL, sep, &last);
86 	prof->desc = _strtok_escape(NULL, sep, &last);
87 	prof->attr = _strtok_escape(NULL, sep, &last);
88 
89 	return (0);
90 }
91 
92 
93 void
_setprofattr(void)94 _setprofattr(void)
95 {
96 	profattr_stayopen = 0;
97 	nss_setent(&db_root, _nss_initf_profattr, &context);
98 }
99 
100 
101 void
_endprofattr(void)102 _endprofattr(void)
103 {
104 	profattr_stayopen = 0;
105 	nss_endent(&db_root, _nss_initf_profattr, &context);
106 	nss_delete(&db_root);
107 }
108 
109 
110 profstr_t *
_getprofattr(profstr_t * result,char * buffer,int buflen,int * h_errnop)111 _getprofattr(profstr_t *result, char *buffer, int buflen, int *h_errnop)
112 {
113 	nss_XbyY_args_t arg;
114 	nss_status_t    res;
115 
116 	NSS_XbyY_INIT(&arg, result, buffer, buflen, str2profattr);
117 	res = nss_getent(&db_root, _nss_initf_profattr, &context, &arg);
118 	arg.status = res;
119 	*h_errnop = arg.h_errno;
120 	return ((profstr_t *)NSS_XbyY_FINI(&arg));
121 }
122 
123 
124 profstr_t *
_getprofnam(const char * name,profstr_t * result,char * buffer,int buflen,int * errnop)125 _getprofnam(const char *name, profstr_t *result, char *buffer, int buflen,
126     int *errnop)
127 {
128 	nss_XbyY_args_t arg;
129 	nss_status_t    res;
130 
131 	NSS_XbyY_INIT(&arg, result, buffer, buflen, str2profattr);
132 	arg.key.name = name;
133 	arg.stayopen = profattr_stayopen;
134 	res = nss_search(&db_root, _nss_initf_profattr,
135 						NSS_DBOP_PROFATTR_BYNAME, &arg);
136 	arg.status = res;
137 	*errnop = arg.h_errno;
138 	return ((profstr_t *)NSS_XbyY_FINI(&arg));
139 }
140