1 /* group.c - group lookup routines */
4 * Copyright 2008 by Howard Chu, Symas Corp.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted only as authorized by the OpenLDAP
11 * A copy of this license is available in the file LICENSE in the
12 * top-level directory of the distribution or, alternatively, at
13 * <http://www.OpenLDAP.org/license.html>.
16 * This code references portions of the nss-ldapd package
17 * written by Arthur de Jong. The nss-ldapd code was forked
18 * from the nss-ldap library written by Luke Howard.
26 /* ( nisSchema.2.2 NAME 'posixGroup' SUP top STRUCTURAL
27 * DESC 'Abstraction of a group of accounts'
28 * MUST ( cn $ gidNumber )
29 * MAY ( userPassword $ memberUid $ description ) )
31 * apart from that the above the uniqueMember attributes may be
32 * supported in a coming release (they map to DNs, which is an extra
35 * using nested groups (groups that are member of a group) is currently
36 * not supported, this may be added in a later release
39 /* the basic search filter for searches */
40 static struct berval group_filter = BER_BVC("(objectClass=posixGroup)");
42 /* the attributes to request with searches */
43 static struct berval group_keys[] = {
45 BER_BVC("userPassword"),
48 BER_BVC("uniqueMember"),
58 /* default values for attributes */
59 static struct berval default_group_userPassword = BER_BVC("*"); /* unmatchable */
69 /* create a search filter for searching a group entry
70 by member uid, return -1 on errors */
71 static int mkfilter_group_bymember(nssov_group_cbp *cbp,struct berval *buf)
74 /* try to translate uid to DN */
75 nssov_uid2dn(cbp->op,cbp->ni,&cbp->user,&dn);
76 if (BER_BVISNULL(&dn)) {
77 if (cbp->user.bv_len + cbp->mi->mi_filter.bv_len + cbp->mi->mi_attrs[UID_KEY].an_desc->ad_cname.bv_len + 6 >
80 buf->bv_len = snprintf(buf->bv_val, buf->bv_len, "(&%s(%s=%s))",
81 cbp->mi->mi_filter.bv_val, cbp->mi->mi_attrs[UID_KEY].an_desc->ad_cname.bv_val,
83 } else { /* also lookup using user DN */
84 if (cbp->user.bv_len + cbp->mi->mi_filter.bv_len + cbp->mi->mi_attrs[UID_KEY].an_desc->ad_cname.bv_len +
85 dn.bv_len + cbp->mi->mi_attrs[MEM_KEY].an_desc->ad_cname.bv_len + 12 > buf->bv_len )
87 buf->bv_len = snprintf(buf->bv_val, buf->bv_len, "(&%s(|(%s=%s)(%s=%s)))",
88 cbp->mi->mi_filter.bv_val,
89 cbp->mi->mi_attrs[UID_KEY].an_desc->ad_cname.bv_val, cbp->user.bv_val,
90 cbp->mi->mi_attrs[MEM_KEY].an_desc->ad_cname.bv_val, dn.bv_val );
98 Checks to see if the specified name is a valid group name.
100 This test is based on the definition from POSIX (IEEE Std 1003.1, 2004,
101 3.189 Group Name and 3.276 Portable Filename Character Set):
102 http://www.opengroup.org/onlinepubs/009695399/basedefs/xbd_chap03.html#tag_03_189
103 http://www.opengroup.org/onlinepubs/009695399/basedefs/xbd_chap03.html#tag_03_276
105 The standard defines group names valid if they only contain characters from
106 the set [A-Za-z0-9._-] where the hyphen should not be used as first
109 static int isvalidgroupname(struct berval *name)
113 if ( !name->bv_val || !name->bv_len )
115 /* check first character */
116 if ( ! ( (name->bv_val[0]>='A' && name->bv_val[0] <= 'Z') ||
117 (name->bv_val[0]>='a' && name->bv_val[0] <= 'z') ||
118 (name->bv_val[0]>='0' && name->bv_val[0] <= '9') ||
119 name->bv_val[0]=='.' || name->bv_val[0]=='_' ) )
121 /* check other characters */
122 for (i=1;i<name->bv_len;i++)
124 if ( ! ( (name->bv_val[i]>='A' && name->bv_val[i] <= 'Z') ||
125 (name->bv_val[i]>='a' && name->bv_val[i] <= 'z') ||
126 (name->bv_val[i]>='0' && name->bv_val[i] <= '9') ||
127 name->bv_val[i]=='.' || name->bv_val[i]=='_' || name->bv_val[i]=='-') )
130 /* no test failed so it must be good */
134 static int write_group(nssov_group_cbp *cbp,Entry *entry)
136 struct berval tmparr[2], tmpgid[2];
137 struct berval *names,*gids,*members;
138 struct berval passwd = {0};
140 int i,j,nummembers,rc;
142 /* get group name (cn) */
143 if (BER_BVISNULL(&cbp->name))
145 a = attr_find(entry->e_attrs, cbp->mi->mi_attrs[CN_KEY].an_desc);
148 Debug(LDAP_DEBUG_ANY,"group entry %s does not contain %s value",
149 entry->e_name.bv_val, cbp->mi->mi_attrs[CN_KEY].an_desc->ad_cname.bv_val,0);
158 BER_BVZERO(&names[1]);
160 /* get the group id(s) */
161 if (BER_BVISNULL(&cbp->gidnum))
163 a = attr_find(entry->e_attrs, cbp->mi->mi_attrs[GID_KEY].an_desc);
166 Debug(LDAP_DEBUG_ANY,"group entry %s does not contain %s value",
167 entry->e_name.bv_val, cbp->mi->mi_attrs[GID_KEY].an_desc->ad_cname.bv_val,0);
176 BER_BVZERO(&gids[1]);
178 /* get group passwd (userPassword) (use only first entry) */
179 a = attr_find(entry->e_attrs, cbp->mi->mi_attrs[PWD_KEY].an_desc);
181 get_userpassword(&a->a_vals[0], &passwd);
182 if (BER_BVISNULL(&passwd))
183 passwd=default_group_userPassword;
184 /* get group members (memberUid&uniqueMember) */
185 if (cbp->wantmembers) {
188 a = attr_find(entry->e_attrs, cbp->mi->mi_attrs[UID_KEY].an_desc);
189 b = attr_find(entry->e_attrs, cbp->mi->mi_attrs[MEM_KEY].an_desc);
195 members = cbp->op->o_tmpalloc( (i+1) * sizeof(struct berval), cbp->op->o_tmpmemctx );
198 for (i=0; i<a->a_numvals; i++) {
199 if (isvalidusername(&a->a_vals[i])) {
200 ber_dupbv_x(&members[j],&a->a_vals[i],cbp->op->o_tmpmemctx);
207 for (i=0; i<a->a_numvals; i++) {
208 if (nssov_dn2uid(cbp->op,cbp->ni,&a->a_nvals[i],&members[j]))
213 BER_BVZERO(&members[j]);
223 /* write entries for all names and gids */
224 for (i=0;!BER_BVISNULL(&names[i]);i++)
226 if (!isvalidgroupname(&names[i]))
228 Debug(LDAP_DEBUG_ANY,"nssov: group entry %s contains invalid group name: \"%s\"",
229 entry->e_name.bv_val,names[i].bv_val,0);
233 for (j=0;!BER_BVISNULL(&gids[j]);j++)
238 gid = strtol(gids[j].bv_val, &tmp, 0);
240 Debug(LDAP_DEBUG_ANY,"nssov: group entry %s contains non-numeric %s value: \"%s\"",
241 entry->e_name.bv_val, cbp->mi->mi_attrs[GID_KEY].an_desc->ad_cname.bv_val,
245 WRITE_INT32(cbp->fp,NSLCD_RESULT_SUCCESS);
246 WRITE_BERVAL(cbp->fp,&names[i]);
247 WRITE_BERVAL(cbp->fp,&passwd);
248 WRITE_TYPE(cbp->fp,gid,gid_t);
249 /* write a list of values */
250 WRITE_INT32(cbp->fp,nummembers);
254 for (k=0;k<nummembers;k++) {
255 WRITE_BERVAL(cbp->fp,&members[k]);
261 /* free and return */
263 ber_bvarray_free_x( members, cbp->op->o_tmpmemctx );
272 struct berval filter = {sizeof(fbuf)};
273 filter.bv_val = fbuf;
274 READ_STRING_BUF2(fp,cbp.buf,sizeof(cbp.buf));
275 cbp.name.bv_len = tmpint32;
276 cbp.name.bv_val = cbp.buf;
277 if (!isvalidgroupname(&cbp.name)) {
278 Debug(LDAP_DEBUG_ANY,"nssov_group_byname(%s): invalid group name",cbp.name.bv_val,0,0);
283 BER_BVZERO(&cbp.gidnum);
284 BER_BVZERO(&cbp.user);,
285 Debug(LDAP_DEBUG_TRACE,"nslcd_group_byname(%s)",cbp.name.bv_val,0,0);,
286 NSLCD_ACTION_GROUP_BYNAME,
287 nssov_filter_byname(cbp.mi,CN_KEY,&cbp.name,&filter)
294 struct berval filter = {sizeof(fbuf)};
295 filter.bv_val = fbuf;
296 READ_TYPE(fp,gid,gid_t);
297 cbp.gidnum.bv_val = cbp.buf;
298 cbp.gidnum.bv_len = snprintf(cbp.buf,sizeof(cbp.buf),"%d",gid);
301 BER_BVZERO(&cbp.name);
302 BER_BVZERO(&cbp.user);,
303 Debug(LDAP_DEBUG_TRACE,"nssov_group_bygid(%s)",cbp.gidnum.bv_val,0,0);,
304 NSLCD_ACTION_GROUP_BYGID,
305 nssov_filter_byid(cbp.mi,GID_KEY,&cbp.gidnum,&filter)
311 struct berval filter = {sizeof(fbuf)};
312 filter.bv_val = fbuf;
313 READ_STRING_BUF2(fp,cbp.buf,sizeof(cbp.buf));
314 cbp.user.bv_len = tmpint32;
315 cbp.user.bv_val = cbp.buf;
316 if (!isvalidusername(&cbp.user)) {
317 Debug(LDAP_DEBUG_ANY,"nssov_group_bymember(%s): invalid user name",cbp.user.bv_val,0,0);
322 BER_BVZERO(&cbp.name);
323 BER_BVZERO(&cbp.gidnum);,
324 Debug(LDAP_DEBUG_TRACE,"nssov_group_bymember(%s)",cbp.user.bv_val,0,0);,
325 NSLCD_ACTION_GROUP_BYMEMBER,
326 mkfilter_group_bymember(&cbp,&filter)
331 struct berval filter;
332 /* no parameters to read */
335 BER_BVZERO(&cbp.name);
336 BER_BVZERO(&cbp.gidnum);,
337 Debug(LDAP_DEBUG_TRACE,"nssov_group_all()",0,0,0);,
338 NSLCD_ACTION_GROUP_ALL,
339 (filter=cbp.mi->mi_filter,0)