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