xref: /titanic_52/usr/src/lib/nsswitch/ldap/common/getprofattr.c (revision 10db1377dafab8ba3feedef26db9c5d8539a5cd1)
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 2003 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 #include <secdb.h>
30 #include <prof_attr.h>
31 #include "ldap_common.h"
32 
33 
34 /* prof_attr attributes filters */
35 #define	_PROF_NAME		"cn"
36 #define	_PROF_RES1		"SolarisAttrReserved1"
37 #define	_PROF_RES2		"SolarisAttrReserved2"
38 #define	_PROF_DESC		"SolarisAttrLongDesc"
39 #define	_PROF_ATTRS		"SolarisAttrKeyValue"
40 #define	_PROF_GETPROFNAME	"(&(objectClass=SolarisProfAttr)(cn=%s))"
41 #define	_PROF_GETPROFNAME_SSD	"(&(%%s)(cn=%s))"
42 
43 static const char *prof_attrs[] = {
44 	_PROF_NAME,
45 	_PROF_RES1,
46 	_PROF_RES2,
47 	_PROF_DESC,
48 	_PROF_ATTRS,
49 	(char *)NULL
50 };
51 
52 
53 static int
54 _nss_ldap_prof2ent(ldap_backend_ptr be, nss_XbyY_args_t *argp)
55 {
56 	int			i, nss_result;
57 	int			buflen = (int)0;
58 	unsigned long		len = 0L;
59 	char			*nullstring = (char *)NULL;
60 	char			*buffer = (char *)NULL;
61 	char			*ceiling = (char *)NULL;
62 	profstr_t		*prof = (profstr_t *)NULL;
63 	ns_ldap_attr_t		*attrptr;
64 	ns_ldap_result_t	*result = be->result;
65 
66 	buffer = argp->buf.buffer;
67 	buflen = (size_t)argp->buf.buflen;
68 	if (!argp->buf.result) {
69 		nss_result = (int)NSS_STR_PARSE_ERANGE;
70 		goto result_prof2ent;
71 	}
72 	prof = (profstr_t *)(argp->buf.result);
73 	ceiling = buffer + buflen;
74 	prof->name = (char *)NULL;
75 	prof->res1 = (char *)NULL;
76 	prof->res2 = (char *)NULL;
77 	prof->desc = (char *)NULL;
78 	prof->attr = (char *)NULL;
79 	nss_result = (int)NSS_STR_PARSE_SUCCESS;
80 	(void) memset(argp->buf.buffer, 0, buflen);
81 
82 	attrptr = getattr(result, 0);
83 	if (attrptr == NULL) {
84 		nss_result = (int)NSS_STR_PARSE_PARSE;
85 		goto result_prof2ent;
86 	}
87 
88 	for (i = 0; i < result->entry->attr_count; i++) {
89 		attrptr = getattr(result, i);
90 		if (attrptr == NULL) {
91 			nss_result = (int)NSS_STR_PARSE_PARSE;
92 			goto result_prof2ent;
93 		}
94 		if (strcasecmp(attrptr->attrname, _PROF_NAME) == 0) {
95 			if ((attrptr->attrvalue[0] == NULL) ||
96 			    (len = strlen(attrptr->attrvalue[0])) < 1) {
97 				nss_result = (int)NSS_STR_PARSE_PARSE;
98 				goto result_prof2ent;
99 			}
100 			prof->name = buffer;
101 			buffer += len + 1;
102 			if (buffer >= ceiling) {
103 				nss_result = (int)NSS_STR_PARSE_ERANGE;
104 				goto result_prof2ent;
105 			}
106 			(void) strcpy(prof->name, attrptr->attrvalue[0]);
107 			continue;
108 		}
109 		if (strcasecmp(attrptr->attrname, _PROF_RES1) == 0) {
110 			if ((attrptr->attrvalue[0] == NULL) ||
111 			    (len = strlen(attrptr->attrvalue[0])) < 1) {
112 				prof->res1 = nullstring;
113 			} else {
114 				prof->res1 = buffer;
115 				buffer += len + 1;
116 				if (buffer >= ceiling) {
117 					nss_result = (int)NSS_STR_PARSE_ERANGE;
118 					goto result_prof2ent;
119 				}
120 				(void) strcpy(prof->res1,
121 				    attrptr->attrvalue[0]);
122 			}
123 			continue;
124 		}
125 		if (strcasecmp(attrptr->attrname, _PROF_RES2) == 0) {
126 			if ((attrptr->attrvalue[0] == NULL) ||
127 			    (len = strlen(attrptr->attrvalue[0])) < 1) {
128 				prof->res2 = nullstring;
129 			} else {
130 				prof->res2 = buffer;
131 				buffer += len + 1;
132 				if (buffer >= ceiling) {
133 					nss_result = (int)NSS_STR_PARSE_ERANGE;
134 					goto result_prof2ent;
135 				}
136 				(void) strcpy(prof->res2,
137 				    attrptr->attrvalue[0]);
138 			}
139 			continue;
140 		}
141 		if (strcasecmp(attrptr->attrname, _PROF_DESC) == 0) {
142 			if ((attrptr->attrvalue[0] == NULL) ||
143 			    (len = strlen(attrptr->attrvalue[0])) < 1) {
144 				prof->desc = nullstring;
145 			} else {
146 				prof->desc = buffer;
147 				buffer += len + 1;
148 				if (buffer >= ceiling) {
149 					nss_result = (int)NSS_STR_PARSE_ERANGE;
150 					goto result_prof2ent;
151 				}
152 				(void) strcpy(prof->desc,
153 				    attrptr->attrvalue[0]);
154 			}
155 			continue;
156 		}
157 		if (strcasecmp(attrptr->attrname, _PROF_ATTRS) == 0) {
158 			if ((attrptr->attrvalue[0] == NULL) ||
159 			    (len = strlen(attrptr->attrvalue[0])) < 1) {
160 				prof->attr = nullstring;
161 			} else {
162 				prof->attr = buffer;
163 				buffer += len + 1;
164 				if (buffer >= ceiling) {
165 					nss_result = (int)NSS_STR_PARSE_ERANGE;
166 					goto result_prof2ent;
167 				}
168 				(void) strcpy(prof->attr,
169 				    attrptr->attrvalue[0]);
170 			}
171 			continue;
172 		}
173 	}
174 
175 #ifdef	DEBUG
176 	(void) fprintf(stdout, "\n[getprofattr.c: _nss_ldap_prof2ent]\n");
177 	(void) fprintf(stdout, "      prof-name: [%s]\n", prof->name);
178 	if (prof->res1 != (char *)NULL) {
179 		(void) fprintf(stdout, "      res1: [%s]\n", prof->res1);
180 	}
181 	if (prof->res2 != (char *)NULL) {
182 		(void) fprintf(stdout, "      res2: [%s]\n", prof->res2);
183 	}
184 	if (prof->desc != (char *)NULL) {
185 		(void) fprintf(stdout, "      desc: [%s]\n", prof->desc);
186 	}
187 	if (prof->attr != (char *)NULL) {
188 		(void) fprintf(stdout, "      attr: [%s]\n", prof->attr);
189 	}
190 #endif	/* DEBUG */
191 
192 result_prof2ent:
193 	(void) __ns_ldap_freeResult(&be->result);
194 	return ((int)nss_result);
195 }
196 
197 
198 static nss_status_t
199 getbyname(ldap_backend_ptr be, void *a)
200 {
201 	char		searchfilter[SEARCHFILTERLEN];
202 	char		userdata[SEARCHFILTERLEN];
203 	char		name[SEARCHFILTERLEN];
204 	int		ret;
205 	nss_XbyY_args_t	*argp = (nss_XbyY_args_t *)a;
206 
207 #ifdef	DEBUG
208 	(void) fprintf(stdout, "\n[getprofattr.c: getbyname]\n");
209 #endif	/* DEBUG */
210 
211 	if (_ldap_filter_name(name, argp->key.name, sizeof (name)) != 0)
212 		return ((nss_status_t)NSS_NOTFOUND);
213 
214 	ret = snprintf(searchfilter, sizeof (searchfilter),
215 	    _PROF_GETPROFNAME, name);
216 	if (ret < 0 || ret >= sizeof (searchfilter))
217 		return ((nss_status_t)NSS_NOTFOUND);
218 
219 	ret = snprintf(userdata, sizeof (userdata),
220 	    _PROF_GETPROFNAME_SSD, name);
221 	if (ret < 0 || ret >= sizeof (userdata))
222 		return ((nss_status_t)NSS_NOTFOUND);
223 
224 	return (_nss_ldap_lookup(be, argp,
225 	    _PROFATTR, searchfilter, NULL, _merge_SSD_filter, userdata));
226 }
227 
228 
229 static ldap_backend_op_t profattr_ops[] = {
230 	_nss_ldap_destr,
231 	_nss_ldap_endent,
232 	_nss_ldap_setent,
233 	_nss_ldap_getent,
234 	getbyname
235 };
236 
237 
238 /*ARGSUSED0*/
239 nss_backend_t *
240 _nss_ldap_prof_attr_constr(const char *dummy1,
241     const char *dummy2,
242     const char *dummy3,
243     const char *dummy4,
244     const char *dummy5)
245 {
246 #ifdef	DEBUG
247 	(void) fprintf(stdout,
248 	    "\n[getprofattr.c: _nss_ldap_prof_attr_constr]\n");
249 #endif
250 	return ((nss_backend_t *)_nss_ldap_constr(profattr_ops,
251 		sizeof (profattr_ops)/sizeof (profattr_ops[0]), _PROFATTR,
252 		prof_attrs, _nss_ldap_prof2ent));
253 }
254