2 Unix SMB/CIFS implementation.
4 Winbind daemon for ntdom nss module
6 Copyright (C) Tim Potter 2000
7 Copyright (C) Jeremy Allison 2001.
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
27 #define DBGC_CLASS DBGC_WINBIND
29 /***************************************************************
30 Empty static struct for negative caching.
31 ****************************************************************/
33 /* Fill a grent structure from various other information */
35 static BOOL fill_grent(struct winbindd_gr *gr, const char *dom_name,
36 const char *gr_name, gid_t unix_gid)
38 fstring full_group_name;
40 fill_domain_username(full_group_name, dom_name, gr_name);
42 gr->gr_gid = unix_gid;
44 /* Group name and password */
46 safe_strcpy(gr->gr_name, full_group_name, sizeof(gr->gr_name) - 1);
47 safe_strcpy(gr->gr_passwd, "x", sizeof(gr->gr_passwd) - 1);
52 /* Fill in the group membership field of a NT group given by group_sid */
54 static BOOL fill_grent_mem(struct winbindd_domain *domain,
56 enum SID_NAME_USE group_name_type,
57 int *num_gr_mem, char **gr_mem, int *gr_mem_len)
59 DOM_SID **sid_mem = NULL;
61 uint32 *name_types = NULL;
62 unsigned int buf_len, buf_ndx, i;
63 char **names = NULL, *buf;
69 if (!(mem_ctx = talloc_init("fill_grent_mem(%s)", domain->name)))
72 /* Initialise group membership information */
74 DEBUG(10, ("group SID %s\n", sid_to_string(sid_string, group_sid)));
78 if (group_name_type != SID_NAME_DOM_GRP) {
79 DEBUG(1, ("SID %s in domain %s isn't a domain group\n",
80 sid_to_string(sid_string, group_sid), domain->name));
84 /* Lookup group members */
85 status = domain->methods->lookup_groupmem(domain, mem_ctx, group_sid, &num_names,
86 &sid_mem, &names, &name_types);
87 if (!NT_STATUS_IS_OK(status)) {
88 DEBUG(1, ("could not lookup membership for group rid %s in domain %s (error: %s)\n",
89 sid_to_string(sid_string, group_sid), domain->name, nt_errstr(status)));
94 DEBUG(10, ("looked up %d names\n", num_names));
96 if (DEBUGLEVEL >= 10) {
97 for (i = 0; i < num_names; i++)
98 DEBUG(10, ("\t%20s %s %d\n", names[i], sid_to_string(sid_string, sid_mem[i]),
102 /* Add members to list */
105 buf_len = buf_ndx = 0;
109 for (i = 0; i < num_names; i++) {
116 DEBUG(10, ("processing name %s\n", the_name));
118 /* FIXME: need to cope with groups within groups. These
119 occur in Universal groups on a Windows 2000 native mode
122 if (name_types[i] != SID_NAME_USER) {
123 DEBUG(3, ("name %s isn't a domain user\n", the_name));
127 /* Don't bother with machine accounts */
129 if (the_name[strlen(the_name) - 1] == '$') {
130 DEBUG(10, ("%s is machine account\n", the_name));
134 /* Append domain name */
136 fill_domain_username(name, domain->name, the_name);
140 /* Add to list or calculate buffer length */
143 buf_len += len + 1; /* List is comma separated */
145 DEBUG(10, ("buf_len + %d = %d\n", len + 1, buf_len));
147 DEBUG(10, ("appending %s at ndx %d\n", name, len));
148 safe_strcpy(&buf[buf_ndx], name, len);
155 /* Allocate buffer */
157 if (!buf && buf_len != 0) {
158 if (!(buf = malloc(buf_len))) {
159 DEBUG(1, ("out of memory\n"));
163 memset(buf, 0, buf_len);
167 if (buf && buf_ndx > 0) {
168 buf[buf_ndx - 1] = '\0';
172 *gr_mem_len = buf_len;
174 DEBUG(10, ("num_mem = %d, len = %d, mem = %s\n", *num_gr_mem,
175 buf_len, *num_gr_mem ? buf : "NULL"));
180 talloc_destroy(mem_ctx);
182 DEBUG(10, ("fill_grent_mem returning %d\n", result));
187 /* Return a group structure from a group name */
189 enum winbindd_result winbindd_getgrnam(struct winbindd_cli_state *state)
192 struct winbindd_domain *domain;
193 enum SID_NAME_USE name_type;
194 fstring name_domain, name_group;
200 /* Ensure null termination */
201 state->request.data.groupname[sizeof(state->request.data.groupname)-1]='\0';
203 DEBUG(3, ("[%5d]: getgrnam %s\n", state->pid,
204 state->request.data.groupname));
206 /* Parse domain and groupname */
208 memset(name_group, 0, sizeof(fstring));
210 tmp = state->request.data.groupname;
211 if (!parse_domain_user(tmp, name_domain, name_group))
212 return WINBINDD_ERROR;
214 /* Get info for the domain */
216 if ((domain = find_domain_from_name(name_domain)) == NULL) {
217 DEBUG(0, ("could not get domain sid for domain %s\n",
219 return WINBINDD_ERROR;
222 /* Get rid and name type from name */
224 if (!winbindd_lookup_sid_by_name(domain, name_group, &group_sid,
226 DEBUG(1, ("group %s in domain %s does not exist\n",
227 name_group, name_domain));
228 return WINBINDD_ERROR;
231 if ((name_type != SID_NAME_ALIAS) && (name_type != SID_NAME_DOM_GRP)) {
232 DEBUG(1, ("name '%s' is not a local or domain group: %d\n",
233 name_group, name_type));
234 return WINBINDD_ERROR;
237 id_type = ID_GROUPID;
238 if (NT_STATUS_IS_ERR(idmap_get_id_from_sid(&id, &id_type, &group_sid))) {
239 DEBUG(1, ("error converting unix gid to sid\n"));
240 return WINBINDD_ERROR;
243 if (!fill_grent(&state->response.data.gr, name_domain,
244 name_group, id.gid) ||
245 !fill_grent_mem(domain, &group_sid, name_type,
246 &state->response.data.gr.num_gr_mem,
247 &gr_mem, &gr_mem_len)) {
248 return WINBINDD_ERROR;
251 /* Group membership lives at start of extra data */
253 state->response.data.gr.gr_mem_ofs = 0;
255 state->response.length += gr_mem_len;
256 state->response.extra_data = gr_mem;
261 /* Return a group structure from a gid number */
263 enum winbindd_result winbindd_getgrgid(struct winbindd_cli_state *state)
265 struct winbindd_domain *domain;
267 enum SID_NAME_USE name_type;
274 DEBUG(3, ("[%5d]: getgrgid %d\n", state->pid,
275 state->request.data.gid));
277 /* Bug out if the gid isn't in the winbind range */
279 if ((state->request.data.gid < server_state.gid_low) ||
280 (state->request.data.gid > server_state.gid_high))
281 return WINBINDD_ERROR;
283 /* Get rid from gid */
284 id.gid = state->request.data.gid;
285 if (NT_STATUS_IS_ERR(idmap_get_sid_from_id(&group_sid, id, ID_GROUPID))) {
286 DEBUG(1, ("could not convert gid %d to rid\n",
287 state->request.data.gid));
288 return WINBINDD_ERROR;
291 /* Get name from sid */
293 if (!winbindd_lookup_name_by_sid(&group_sid, dom_name, group_name, &name_type)) {
294 DEBUG(1, ("could not lookup sid\n"));
295 return WINBINDD_ERROR;
298 if (!((name_type == SID_NAME_ALIAS) ||
299 (name_type == SID_NAME_DOM_GRP))) {
300 DEBUG(1, ("name '%s' is not a local or domain group: %d\n",
301 group_name, name_type));
302 return WINBINDD_ERROR;
305 /* Fill in group structure */
307 domain = find_domain_from_sid(&group_sid);
310 DEBUG(1,("Can't find domain from sid\n"));
311 return WINBINDD_ERROR;
314 if (!fill_grent(&state->response.data.gr, dom_name, group_name,
315 state->request.data.gid) ||
316 !fill_grent_mem(domain, &group_sid, name_type,
317 &state->response.data.gr.num_gr_mem,
318 &gr_mem, &gr_mem_len))
319 return WINBINDD_ERROR;
321 /* Group membership lives at start of extra data */
323 state->response.data.gr.gr_mem_ofs = 0;
325 state->response.length += gr_mem_len;
326 state->response.extra_data = gr_mem;
332 * set/get/endgrent functions
335 /* "Rewind" file pointer for group database enumeration */
337 enum winbindd_result winbindd_setgrent(struct winbindd_cli_state *state)
339 struct winbindd_domain *domain;
341 DEBUG(3, ("[%5d]: setgrent\n", state->pid));
343 /* Check user has enabled this */
345 if (!lp_winbind_enum_groups())
346 return WINBINDD_ERROR;
348 /* Free old static data if it exists */
350 if (state->getgrent_state != NULL) {
351 free_getent_state(state->getgrent_state);
352 state->getgrent_state = NULL;
355 /* Create sam pipes for each domain we know about */
357 for (domain = domain_list(); domain != NULL; domain = domain->next) {
358 struct getent_state *domain_state;
360 /* Create a state record for this domain */
362 if ((domain_state = (struct getent_state *)
363 malloc(sizeof(struct getent_state))) == NULL) {
364 DEBUG(1, ("winbindd_setgrent: malloc failed for domain_state!\n"));
365 return WINBINDD_ERROR;
368 ZERO_STRUCTP(domain_state);
370 fstrcpy(domain_state->domain_name, domain->name);
372 /* Add to list of open domains */
374 DLIST_ADD(state->getgrent_state, domain_state);
380 /* Close file pointer to ntdom group database */
382 enum winbindd_result winbindd_endgrent(struct winbindd_cli_state *state)
384 DEBUG(3, ("[%5d]: endgrent\n", state->pid));
386 free_getent_state(state->getgrent_state);
387 state->getgrent_state = NULL;
392 /* Get the list of domain groups and domain aliases for a domain. We fill in
393 the sam_entries and num_sam_entries fields with domain group information.
394 The dispinfo_ndx field is incremented to the index of the next group to
395 fetch. Return True if some groups were returned, False otherwise. */
397 #define MAX_FETCH_SAM_ENTRIES 100
399 static BOOL get_sam_group_entries(struct getent_state *ent)
403 struct acct_info *name_list = NULL, *tmp_name_list = NULL;
406 struct acct_info *sam_grp_entries = NULL;
407 struct winbindd_domain *domain;
409 if (ent->got_sam_entries)
412 if (!(mem_ctx = talloc_init("get_sam_group_entries(%s)",
413 ent->domain_name))) {
414 DEBUG(1, ("get_sam_group_entries: could not create talloc context!\n"));
418 /* Free any existing group info */
420 SAFE_FREE(ent->sam_entries);
421 ent->num_sam_entries = 0;
422 ent->got_sam_entries = True;
424 /* Enumerate domain groups */
428 if (!(domain = find_domain_from_name(ent->domain_name))) {
429 DEBUG(3, ("no such domain %s in get_sam_group_entries\n", ent->domain_name));
433 /* always get the domain global groups */
435 status = domain->methods->enum_dom_groups(domain, mem_ctx, &num_entries, &sam_grp_entries);
437 if (!NT_STATUS_IS_OK(status)) {
438 DEBUG(3, ("get_sam_group_entries: could not enumerate domain groups! Error: %s\n", nt_errstr(status)));
443 /* Copy entries into return buffer */
446 if ( !(name_list = malloc(sizeof(struct acct_info) * num_entries)) ) {
447 DEBUG(0,("get_sam_group_entries: Failed to malloc memory for %d domain groups!\n",
452 memcpy( name_list, sam_grp_entries, num_entries * sizeof(struct acct_info) );
455 ent->num_sam_entries = num_entries;
457 /* get the domain local groups if we are a member of
458 a native win2k domain */
460 if ( domain->native_mode && domain->methods->enum_local_groups )
462 DEBUG(4,("get_sam_group_entries: Native Mode 2k domain; enumerating local groups as well\n"));
464 status = domain->methods->enum_local_groups(domain, mem_ctx, &num_entries, &sam_grp_entries);
466 if ( !NT_STATUS_IS_OK(status) ) {
467 DEBUG(3,("get_sam_group_entries: Failed to enumerate domain local groups!\n"));
471 DEBUG(4,("get_sam_group_entries: Returned %d local groups\n", num_entries));
473 /* Copy entries into return buffer */
476 if ( !(tmp_name_list = Realloc( name_list, sizeof(struct acct_info) * (ent->num_sam_entries+num_entries))) )
478 DEBUG(0,("get_sam_group_entries: Failed to realloc more memory for %d local groups!\n",
481 SAFE_FREE( name_list );
485 name_list = tmp_name_list;
487 memcpy( &name_list[ent->num_sam_entries], sam_grp_entries,
488 num_entries * sizeof(struct acct_info) );
491 ent->num_sam_entries += num_entries;
495 /* Fill in remaining fields */
497 ent->sam_entries = name_list;
498 ent->sam_entry_index = 0;
500 result = (ent->num_sam_entries > 0);
503 talloc_destroy(mem_ctx);
508 /* Fetch next group entry from ntdom database */
510 #define MAX_GETGRENT_GROUPS 500
512 enum winbindd_result winbindd_getgrent(struct winbindd_cli_state *state)
514 struct getent_state *ent;
515 struct winbindd_gr *group_list = NULL;
516 int num_groups, group_list_ndx = 0, i, gr_mem_list_len = 0;
517 char *new_extra_data, *gr_mem_list = NULL;
519 DEBUG(3, ("[%5d]: getgrent\n", state->pid));
521 /* Check user has enabled this */
523 if (!lp_winbind_enum_groups())
524 return WINBINDD_ERROR;
526 num_groups = MIN(MAX_GETGRENT_GROUPS, state->request.data.num_entries);
528 if ((state->response.extra_data =
529 malloc(num_groups * sizeof(struct winbindd_gr))) == NULL)
530 return WINBINDD_ERROR;
532 state->response.data.num_entries = 0;
534 group_list = (struct winbindd_gr *)state->response.extra_data;
536 if (!(ent = state->getgrent_state))
537 return WINBINDD_ERROR;
539 /* Start sending back groups */
541 for (i = 0; i < num_groups; i++) {
542 struct acct_info *name_list = NULL;
543 fstring domain_group_name;
547 char *gr_mem, *new_gr_mem_list;
549 struct winbindd_domain *domain;
553 /* Do we need to fetch another chunk of groups? */
557 DEBUG(10, ("entry_index = %d, num_entries = %d\n",
558 ent->sam_entry_index, ent->num_sam_entries));
560 if (ent->num_sam_entries == ent->sam_entry_index) {
562 while(ent && !get_sam_group_entries(ent)) {
563 struct getent_state *next_ent;
565 DEBUG(10, ("freeing state info for domain %s\n", ent->domain_name));
567 /* Free state information for this domain */
569 SAFE_FREE(ent->sam_entries);
571 next_ent = ent->next;
572 DLIST_REMOVE(state->getgrent_state, ent);
578 /* No more domains */
584 name_list = ent->sam_entries;
587 find_domain_from_name(ent->domain_name))) {
588 DEBUG(3, ("No such domain %s in winbindd_getgrent\n", ent->domain_name));
593 /* Lookup group info */
595 sid_copy(&group_sid, &domain->sid);
596 sid_append_rid(&group_sid, name_list[ent->sam_entry_index].rid);
598 id_type = ID_GROUPID;
599 if (NT_STATUS_IS_ERR(idmap_get_id_from_sid(&id, &id_type, &group_sid))) {
601 DEBUG(1, ("could not look up gid for group %s\n",
602 name_list[ent->sam_entry_index].acct_name));
604 ent->sam_entry_index++;
609 DEBUG(10, ("got gid %d for group %x\n", group_gid,
610 name_list[ent->sam_entry_index].rid));
612 /* Fill in group entry */
614 fill_domain_username(domain_group_name, ent->domain_name,
615 name_list[ent->sam_entry_index].acct_name);
617 result = fill_grent(&group_list[group_list_ndx],
619 name_list[ent->sam_entry_index].acct_name,
622 /* Fill in group membership entry */
626 group_list[group_list_ndx].num_gr_mem = 0;
630 /* Get group membership */
631 if (state->request.cmd == WINBINDD_GETGRLST) {
634 sid_copy(&member_sid, &domain->sid);
635 sid_append_rid(&member_sid, name_list[ent->sam_entry_index].rid);
636 result = fill_grent_mem(
640 &group_list[group_list_ndx].num_gr_mem,
641 &gr_mem, &gr_mem_len);
646 /* Append to group membership list */
647 new_gr_mem_list = Realloc(
649 gr_mem_list_len + gr_mem_len);
651 if (!new_gr_mem_list && (group_list[group_list_ndx].num_gr_mem != 0)) {
652 DEBUG(0, ("out of memory\n"));
653 SAFE_FREE(gr_mem_list);
658 DEBUG(10, ("list_len = %d, mem_len = %d\n",
659 gr_mem_list_len, gr_mem_len));
661 gr_mem_list = new_gr_mem_list;
663 memcpy(&gr_mem_list[gr_mem_list_len], gr_mem,
668 group_list[group_list_ndx].gr_mem_ofs =
671 gr_mem_list_len += gr_mem_len;
674 ent->sam_entry_index++;
676 /* Add group to return list */
680 DEBUG(10, ("adding group num_entries = %d\n",
681 state->response.data.num_entries));
684 state->response.data.num_entries++;
686 state->response.length +=
687 sizeof(struct winbindd_gr);
690 DEBUG(0, ("could not lookup domain group %s\n",
695 /* Copy the list of group memberships to the end of the extra data */
697 if (group_list_ndx == 0)
700 new_extra_data = Realloc(
701 state->response.extra_data,
702 group_list_ndx * sizeof(struct winbindd_gr) + gr_mem_list_len);
704 if (!new_extra_data) {
705 DEBUG(0, ("out of memory\n"));
707 SAFE_FREE(state->response.extra_data);
708 SAFE_FREE(gr_mem_list);
710 return WINBINDD_ERROR;
713 state->response.extra_data = new_extra_data;
715 memcpy(&((char *)state->response.extra_data)
716 [group_list_ndx * sizeof(struct winbindd_gr)],
717 gr_mem_list, gr_mem_list_len);
719 SAFE_FREE(gr_mem_list);
721 state->response.length += gr_mem_list_len;
723 DEBUG(10, ("returning %d groups, length = %d\n",
724 group_list_ndx, gr_mem_list_len));
730 return (group_list_ndx > 0) ? WINBINDD_OK : WINBINDD_ERROR;
733 /* List domain groups without mapping to unix ids */
735 enum winbindd_result winbindd_list_groups(struct winbindd_cli_state *state)
737 uint32 total_entries = 0;
738 struct winbindd_domain *domain;
739 char *extra_data = NULL;
741 unsigned int extra_data_len = 0, i;
743 DEBUG(3, ("[%5d]: list groups\n", state->pid));
745 /* Enumerate over trusted domains */
747 for (domain = domain_list(); domain; domain = domain->next) {
748 struct getent_state groups;
752 /* Get list of sam groups */
754 fstrcpy(groups.domain_name, domain->name);
756 get_sam_group_entries(&groups);
758 if (groups.num_sam_entries == 0) {
759 /* this domain is empty or in an error state */
763 /* keep track the of the total number of groups seen so
764 far over all domains */
765 total_entries += groups.num_sam_entries;
767 /* Allocate some memory for extra data. Note that we limit
768 account names to sizeof(fstring) = 128 characters. */
769 ted = Realloc(extra_data, sizeof(fstring) * total_entries);
772 DEBUG(0,("failed to enlarge buffer!\n"));
773 SAFE_FREE(extra_data);
774 return WINBINDD_ERROR;
778 /* Pack group list into extra data fields */
779 for (i = 0; i < groups.num_sam_entries; i++) {
780 char *group_name = ((struct acct_info *)
781 groups.sam_entries)[i].acct_name;
784 fill_domain_username(name, domain->name, group_name);
785 /* Append to extra data */
786 memcpy(&extra_data[extra_data_len], name,
788 extra_data_len += strlen(name);
789 extra_data[extra_data_len++] = ',';
792 free(groups.sam_entries);
795 /* Assign extra_data fields in response structure */
797 extra_data[extra_data_len - 1] = '\0';
798 state->response.extra_data = extra_data;
799 state->response.length += extra_data_len;
802 /* No domains may have responded but that's still OK so don't
808 /* Get user supplementary groups. This is much quicker than trying to
809 invert the groups database. */
811 enum winbindd_result winbindd_getgroups(struct winbindd_cli_state *state)
813 fstring name_domain, name_user;
815 enum SID_NAME_USE name_type;
816 uint32 num_groups, num_gids;
819 struct winbindd_domain *domain;
820 enum winbindd_result result = WINBINDD_ERROR;
825 /* Ensure null termination */
826 state->request.data.username[sizeof(state->request.data.username)-1]='\0';
828 DEBUG(3, ("[%5d]: getgroups %s\n", state->pid,
829 state->request.data.username));
831 if (!(mem_ctx = talloc_init("winbindd_getgroups(%s)",
832 state->request.data.username)))
833 return WINBINDD_ERROR;
835 /* Parse domain and username */
837 if (!parse_domain_user(state->request.data.username, name_domain,
841 /* Get info for the domain */
843 if ((domain = find_domain_from_name(name_domain)) == NULL) {
844 DEBUG(0, ("could not find domain entry for domain %s\n",
849 /* Get rid and name type from name. The following costs 1 packet */
851 if (!winbindd_lookup_sid_by_name(domain, name_user, &user_sid,
853 DEBUG(1, ("user '%s' does not exist\n", name_user));
857 if (name_type != SID_NAME_USER) {
858 DEBUG(1, ("name '%s' is not a user name: %d\n",
859 name_user, name_type));
863 status = domain->methods->lookup_usergroups(domain, mem_ctx,
864 &user_sid, &num_groups,
866 if (!NT_STATUS_IS_OK(status)) goto done;
868 /* Copy data back to client */
871 gid_list = malloc(sizeof(gid_t) * num_groups);
873 if (state->response.extra_data)
876 for (i = 0; i < num_groups; i++) {
880 id_type = ID_GROUPID;
881 if (NT_STATUS_IS_ERR(idmap_get_id_from_sid(&id, &id_type, user_gids[i]))) {
884 DEBUG(1, ("unable to convert group sid %s to gid\n",
885 sid_to_string(sid_string, user_gids[i])));
888 gid_list[num_gids] = id.gid;
892 state->response.data.num_entries = num_gids;
893 state->response.extra_data = gid_list;
894 state->response.length += num_gids * sizeof(gid_t);
896 result = WINBINDD_OK;
900 talloc_destroy(mem_ctx);