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