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  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #include <secdb.h>
27 #include <prof_attr.h>
28 #include "ldap_common.h"
29 
30 
31 /* prof_attr attributes filters */
32 #define	_PROF_NAME		"cn"
33 #define	_PROF_RES1		"SolarisAttrReserved1"
34 #define	_PROF_RES2		"SolarisAttrReserved2"
35 #define	_PROF_DESC		"SolarisAttrLongDesc"
36 #define	_PROF_ATTRS		"SolarisAttrKeyValue"
37 /* Negate an exec_attr attribute to exclude exec_attr entries */
38 #define	_PROF_GETPROFNAME \
39 "(&(objectClass=SolarisProfAttr)(!(SolarisKernelSecurityPolicy=*))(cn=%s))"
40 #define	_PROF_GETPROFNAME_SSD	"(&(%%s)(cn=%s))"
41 
42 static const char *prof_attrs[] = {
43 	_PROF_NAME,
44 	_PROF_RES1,
45 	_PROF_RES2,
46 	_PROF_DESC,
47 	_PROF_ATTRS,
48 	(char *)NULL
49 };
50 /*
51  * _nss_ldap_prof2str is the data marshaling method for the prof_attr
52  * system call getprofattr, getprofnam and getproflist.
53  * This method is called after a successful search has been performed.
54  * This method will parse the search results into the file format.
55  * e.g.
56  *
57  * All:::Execute any command as the user or role:help=RtAll.html
58  *
59  */
60 static int
_nss_ldap_prof2str(ldap_backend_ptr be,nss_XbyY_args_t * argp)61 _nss_ldap_prof2str(ldap_backend_ptr be, nss_XbyY_args_t *argp)
62 {
63 	int			nss_result;
64 	int			buflen = 0;
65 	unsigned long		len = 0L;
66 	char			*buffer = NULL;
67 	ns_ldap_result_t	*result = be->result;
68 	char			**name, **res1, **res2, **des, **attr;
69 	char			*res1_str, *res2_str, *des_str, *attr_str;
70 
71 	if (result == NULL)
72 		return (NSS_STR_PARSE_PARSE);
73 
74 	buflen = argp->buf.buflen;
75 	nss_result = NSS_STR_PARSE_SUCCESS;
76 	(void) memset(argp->buf.buffer, 0, buflen);
77 
78 	name = __ns_ldap_getAttr(result->entry, _PROF_NAME);
79 	if (name == NULL || name[0] == NULL ||
80 			(strlen(name[0]) < 1)) {
81 		nss_result = NSS_STR_PARSE_PARSE;
82 		goto result_prof2str;
83 	}
84 	res1 = __ns_ldap_getAttr(result->entry, _PROF_RES1);
85 	if (res1 == NULL || res1[0] == NULL || (strlen(res1[0]) < 1))
86 		res1_str = _NO_VALUE;
87 	else
88 		res1_str = res1[0];
89 
90 	res2 = __ns_ldap_getAttr(result->entry, _PROF_RES2);
91 	if (res2 == NULL || res2[0] == NULL || (strlen(res2[0]) < 1))
92 		res2_str = _NO_VALUE;
93 	else
94 		res2_str = res2[0];
95 
96 	des = __ns_ldap_getAttr(result->entry, _PROF_DESC);
97 	if (des == NULL || des[0] == NULL || (strlen(des[0]) < 1))
98 		des_str = _NO_VALUE;
99 	else
100 		des_str = des[0];
101 
102 	attr = __ns_ldap_getAttr(result->entry, _PROF_ATTRS);
103 	if (attr == NULL || attr[0] == NULL || (strlen(attr[0]) < 1))
104 		attr_str = _NO_VALUE;
105 	else
106 		attr_str = attr[0];
107 	/* 5 = 4 ':' + 1 '\0' */
108 	len = strlen(name[0]) + strlen(res1_str) + strlen(res2_str) +
109 		strlen(des_str) + strlen(attr_str) + 6;
110 	if (len > buflen) {
111 		nss_result = NSS_STR_PARSE_ERANGE;
112 		goto result_prof2str;
113 	}
114 
115 	if (argp->buf.result != NULL) {
116 		if ((be->buffer = calloc(1, len)) == NULL) {
117 			nss_result = NSS_STR_PARSE_PARSE;
118 			goto result_prof2str;
119 		}
120 		buffer = be->buffer;
121 	} else
122 		buffer = argp->buf.buffer;
123 	(void) snprintf(buffer, len, "%s:%s:%s:%s:%s",
124 			name[0], res1_str, res2_str, des_str, attr_str);
125 	/* The front end marshaller doesn't need the trailing null */
126 	if (argp->buf.result != NULL)
127 		be->buflen = strlen(be->buffer);
128 
129 result_prof2str:
130 	(void) __ns_ldap_freeResult(&be->result);
131 	return (nss_result);
132 }
133 
134 
135 static nss_status_t
getbyname(ldap_backend_ptr be,void * a)136 getbyname(ldap_backend_ptr be, void *a)
137 {
138 	char		searchfilter[SEARCHFILTERLEN];
139 	char		userdata[SEARCHFILTERLEN];
140 	char		name[SEARCHFILTERLEN];
141 	int		ret;
142 	nss_XbyY_args_t	*argp = (nss_XbyY_args_t *)a;
143 
144 	if (_ldap_filter_name(name, argp->key.name, sizeof (name)) != 0)
145 		return ((nss_status_t)NSS_NOTFOUND);
146 
147 	ret = snprintf(searchfilter, sizeof (searchfilter),
148 	    _PROF_GETPROFNAME, name);
149 	if (ret < 0 || ret >= sizeof (searchfilter))
150 		return ((nss_status_t)NSS_NOTFOUND);
151 
152 	ret = snprintf(userdata, sizeof (userdata),
153 	    _PROF_GETPROFNAME_SSD, name);
154 	if (ret < 0 || ret >= sizeof (userdata))
155 		return ((nss_status_t)NSS_NOTFOUND);
156 
157 	return (_nss_ldap_lookup(be, argp,
158 	    _PROFATTR, searchfilter, NULL, _merge_SSD_filter, userdata));
159 }
160 
161 
162 static ldap_backend_op_t profattr_ops[] = {
163 	_nss_ldap_destr,
164 	_nss_ldap_endent,
165 	_nss_ldap_setent,
166 	_nss_ldap_getent,
167 	getbyname
168 };
169 
170 
171 /*ARGSUSED0*/
172 nss_backend_t *
_nss_ldap_prof_attr_constr(const char * dummy1,const char * dummy2,const char * dummy3,const char * dummy4,const char * dummy5)173 _nss_ldap_prof_attr_constr(const char *dummy1,
174     const char *dummy2,
175     const char *dummy3,
176     const char *dummy4,
177     const char *dummy5)
178 {
179 	return ((nss_backend_t *)_nss_ldap_constr(profattr_ops,
180 		sizeof (profattr_ops)/sizeof (profattr_ops[0]), _PROFATTR,
181 		prof_attrs, _nss_ldap_prof2str));
182 }
183