2 Unix SMB/Netbios implementation.
5 Winbind daemon for ntdom nss module
7 Copyright (C) Tim Potter 2000
8 Copyright (C) Jeremy Allison 2001.
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
27 /***************************************************************
28 Empty static struct for negative caching.
29 ****************************************************************/
31 /* Fill a grent structure from various other information */
33 static BOOL fill_grent(struct winbindd_gr *gr, const char *dom_name,
34 const char *gr_name, gid_t unix_gid)
36 fstring full_group_name;
38 fill_domain_username(full_group_name, dom_name, gr_name);
40 gr->gr_gid = unix_gid;
42 /* Group name and password */
44 safe_strcpy(gr->gr_name, full_group_name, sizeof(gr->gr_name) - 1);
45 safe_strcpy(gr->gr_passwd, "x", sizeof(gr->gr_passwd) - 1);
50 /* Fill in the group membership field of a NT group given by group_rid */
52 static BOOL fill_grent_mem(struct winbindd_domain *domain,
54 enum SID_NAME_USE group_name_type,
55 int *num_gr_mem, char **gr_mem, int *gr_mem_len)
57 uint32 *rid_mem = NULL, num_names = 0;
58 uint32 *name_types = NULL;
59 int buf_len, buf_ndx, i;
60 char **names = NULL, *buf;
65 if (!(mem_ctx = talloc_init_named("fill_grent_mem(%s)", domain->name)))
68 /* Initialise group membership information */
70 DEBUG(10, ("group %s rid 0x%x\n", domain ? domain->name : "NULL",
75 if (group_name_type != SID_NAME_DOM_GRP) {
76 DEBUG(1, ("rid %d in domain %s isn't a " "domain group\n",
77 group_rid, domain->name));
81 /* Lookup group members */
82 status = domain->methods->lookup_groupmem(domain, mem_ctx, group_rid, &num_names,
83 &rid_mem, &names, &name_types);
84 if (!NT_STATUS_IS_OK(status)) {
85 DEBUG(1, ("could not lookup membership for group rid %d in domain %s\n",
86 group_rid, domain->name));
91 DEBUG(10, ("looked up %d names\n", num_names));
93 if (DEBUGLEVEL >= 10) {
94 for (i = 0; i < num_names; i++)
95 DEBUG(10, ("\t%20s %x %d\n", names[i], rid_mem[i],
99 /* Add members to list */
102 buf_len = buf_ndx = 0;
106 for (i = 0; i < num_names; i++) {
113 DEBUG(10, ("processing name %s\n", the_name));
115 /* FIXME: need to cope with groups within groups. These
116 occur in Universal groups on a Windows 2000 native mode
119 if (name_types[i] != SID_NAME_USER) {
120 DEBUG(3, ("name %s isn't a domain user\n", the_name));
124 /* Don't bother with machine accounts */
126 if (the_name[strlen(the_name) - 1] == '$') {
127 DEBUG(10, ("%s is machine account\n", the_name));
131 /* Append domain name */
133 fill_domain_username(name, domain->name, the_name);
137 /* Add to list or calculate buffer length */
140 buf_len += len + 1; /* List is comma separated */
142 DEBUG(10, ("buf_len + %d = %d\n", len + 1, buf_len));
144 DEBUG(10, ("appending %s at ndx %d\n", name, len));
145 safe_strcpy(&buf[buf_ndx], name, len);
152 /* Allocate buffer */
155 if (!(buf = malloc(buf_len))) {
156 DEBUG(1, ("out of memory\n"));
160 memset(buf, 0, buf_len);
164 if (buf && buf_ndx > 0) {
165 buf[buf_ndx - 1] = '\0';
169 *gr_mem_len = buf_len;
171 DEBUG(10, ("num_mem = %d, len = %d, mem = %s\n", *num_gr_mem,
172 buf_len, *num_gr_mem ? buf : "NULL"));
177 talloc_destroy(mem_ctx);
179 DEBUG(10, ("fill_grent_mem returning %d\n", result));
184 /* Return a group structure from a group name */
186 enum winbindd_result winbindd_getgrnam(struct winbindd_cli_state *state)
189 struct winbindd_domain *domain;
190 enum SID_NAME_USE name_type;
192 fstring name_domain, name_group;
197 DEBUG(3, ("[%5d]: getgrnam %s\n", state->pid,
198 state->request.data.groupname));
200 /* Parse domain and groupname */
202 memset(name_group, 0, sizeof(fstring));
204 tmp = state->request.data.groupname;
205 if (!parse_domain_user(tmp, name_domain, name_group))
206 return WINBINDD_ERROR;
208 /* Get info for the domain */
210 if ((domain = find_domain_from_name(name_domain)) == NULL) {
211 DEBUG(0, ("could not get domain sid for domain %s\n",
213 return WINBINDD_ERROR;
216 /* Get rid and name type from name */
218 if (!winbindd_lookup_sid_by_name(domain, name_group, &group_sid,
220 DEBUG(1, ("group %s in domain %s does not exist\n",
221 name_group, name_domain));
222 return WINBINDD_ERROR;
225 if ((name_type != SID_NAME_ALIAS) && (name_type != SID_NAME_DOM_GRP)) {
226 DEBUG(1, ("name '%s' is not a local or domain group: %d\n",
227 name_group, name_type));
228 return WINBINDD_ERROR;
231 /* Fill in group structure */
233 sid_split_rid(&group_sid, &group_rid);
235 if (!winbindd_idmap_get_gid_from_rid(domain->name, group_rid, &gid)) {
236 DEBUG(1, ("error converting unix gid to sid\n"));
237 return WINBINDD_ERROR;
240 if (!fill_grent(&state->response.data.gr, name_domain,
242 !fill_grent_mem(domain, group_rid, name_type,
243 &state->response.data.gr.num_gr_mem,
244 &gr_mem, &gr_mem_len)) {
245 return WINBINDD_ERROR;
248 /* Group membership lives at start of extra data */
250 state->response.data.gr.gr_mem_ofs = 0;
252 state->response.length += gr_mem_len;
253 state->response.extra_data = gr_mem;
258 /* Return a group structure from a gid number */
260 enum winbindd_result winbindd_getgrgid(struct winbindd_cli_state *state)
262 struct winbindd_domain *domain;
264 enum SID_NAME_USE name_type;
271 DEBUG(3, ("[%5d]: getgrgid %d\n", state->pid,
272 state->request.data.gid));
274 /* Bug out if the gid isn't in the winbind range */
276 if ((state->request.data.gid < server_state.gid_low) ||
277 (state->request.data.gid > server_state.gid_high))
278 return WINBINDD_ERROR;
280 /* Get rid from gid */
282 if (!winbindd_idmap_get_rid_from_gid(state->request.data.gid,
283 &group_rid, &domain)) {
284 DEBUG(1, ("could not convert gid %d to rid\n",
285 state->request.data.gid));
286 return WINBINDD_ERROR;
289 /* Get sid from gid */
291 sid_copy(&group_sid, &domain->sid);
292 sid_append_rid(&group_sid, group_rid);
294 if (!winbindd_lookup_name_by_sid(&group_sid, dom_name, group_name, &name_type)) {
295 DEBUG(1, ("could not lookup sid\n"));
296 return WINBINDD_ERROR;
299 if (!((name_type == SID_NAME_ALIAS) ||
300 (name_type == SID_NAME_DOM_GRP))) {
301 DEBUG(1, ("name '%s' is not a local or domain group: %d\n",
302 group_name, name_type));
303 return WINBINDD_ERROR;
306 /* Fill in group structure */
308 if (!fill_grent(&state->response.data.gr, dom_name, group_name,
309 state->request.data.gid) ||
310 !fill_grent_mem(domain, group_rid, name_type,
311 &state->response.data.gr.num_gr_mem,
312 &gr_mem, &gr_mem_len))
313 return WINBINDD_ERROR;
315 /* Group membership lives at start of extra data */
317 state->response.data.gr.gr_mem_ofs = 0;
319 state->response.length += gr_mem_len;
320 state->response.extra_data = gr_mem;
326 * set/get/endgrent functions
329 /* "Rewind" file pointer for group database enumeration */
331 enum winbindd_result winbindd_setgrent(struct winbindd_cli_state *state)
333 struct winbindd_domain *domain;
335 DEBUG(3, ("[%5d]: setgrent\n", state->pid));
337 /* Check user has enabled this */
339 if (!lp_winbind_enum_groups())
340 return WINBINDD_ERROR;
342 /* Free old static data if it exists */
344 if (state->getgrent_state != NULL) {
345 free_getent_state(state->getgrent_state);
346 state->getgrent_state = NULL;
349 /* Create sam pipes for each domain we know about */
351 for (domain = domain_list(); domain != NULL; domain = domain->next) {
352 struct getent_state *domain_state;
354 /* Skip domains other than WINBINDD_DOMAIN environment
357 if ((strcmp(state->request.domain, "") != 0) &&
358 !check_domain_env(state->request.domain, domain->name))
361 /* Create a state record for this domain */
363 if ((domain_state = (struct getent_state *)
364 malloc(sizeof(struct getent_state))) == NULL)
365 return WINBINDD_ERROR;
367 ZERO_STRUCTP(domain_state);
369 fstrcpy(domain_state->domain_name, domain->name);
371 /* Add to list of open domains */
373 DLIST_ADD(state->getgrent_state, domain_state);
379 /* Close file pointer to ntdom group database */
381 enum winbindd_result winbindd_endgrent(struct winbindd_cli_state *state)
383 DEBUG(3, ("[%5d]: endgrent\n", state->pid));
385 free_getent_state(state->getgrent_state);
386 state->getgrent_state = NULL;
391 /* Get the list of domain groups and domain aliases for a domain. We fill in
392 the sam_entries and num_sam_entries fields with domain group information.
393 The dispinfo_ndx field is incremented to the index of the next group to
394 fetch. Return True if some groups were returned, False otherwise. */
396 #define MAX_FETCH_SAM_ENTRIES 100
398 static BOOL get_sam_group_entries(struct getent_state *ent)
402 struct acct_info *name_list = NULL;
405 struct acct_info *sam_grp_entries = NULL;
406 struct winbindd_domain *domain;
408 if (ent->got_sam_entries)
411 if (!(mem_ctx = talloc_init_named("get_sam_group_entries(%s)",
415 /* Free any existing group info */
417 SAFE_FREE(ent->sam_entries);
418 ent->num_sam_entries = 0;
419 ent->got_sam_entries = True;
421 /* Enumerate domain groups */
425 if (!(domain = find_domain_from_name(ent->domain_name))) {
426 DEBUG(3, ("no such domain %s in get_sam_group_entries\n", ent->domain_name));
430 status = domain->methods->enum_dom_groups(domain,
435 if (!NT_STATUS_IS_OK(status)) {
440 /* Copy entries into return buffer */
443 name_list = malloc(sizeof(struct acct_info) * num_entries);
444 memcpy(name_list, sam_grp_entries,
445 num_entries * sizeof(struct acct_info));
448 ent->num_sam_entries = num_entries;
450 /* Fill in remaining fields */
452 ent->sam_entries = name_list;
453 ent->sam_entry_index = 0;
455 result = (ent->num_sam_entries > 0);
458 talloc_destroy(mem_ctx);
463 /* Fetch next group entry from ntdom database */
465 #define MAX_GETGRENT_GROUPS 500
467 enum winbindd_result winbindd_getgrent(struct winbindd_cli_state *state)
469 struct getent_state *ent;
470 struct winbindd_gr *group_list = NULL;
471 int num_groups, group_list_ndx = 0, i, gr_mem_list_len = 0;
472 char *new_extra_data, *gr_mem_list = NULL;
474 DEBUG(3, ("[%5d]: getgrent\n", state->pid));
476 /* Check user has enabled this */
478 if (!lp_winbind_enum_groups())
479 return WINBINDD_ERROR;
481 num_groups = MIN(MAX_GETGRENT_GROUPS, state->request.data.num_entries);
483 if ((state->response.extra_data =
484 malloc(num_groups * sizeof(struct winbindd_gr))) == NULL)
485 return WINBINDD_ERROR;
487 state->response.data.num_entries = 0;
489 group_list = (struct winbindd_gr *)state->response.extra_data;
491 if (!(ent = state->getgrent_state))
492 return WINBINDD_ERROR;
494 /* Start sending back groups */
496 for (i = 0; i < num_groups; i++) {
497 struct acct_info *name_list = NULL;
498 fstring domain_group_name;
502 char *gr_mem, *new_gr_mem_list;
504 /* Do we need to fetch another chunk of groups? */
508 DEBUG(10, ("entry_index = %d, num_entries = %d\n",
509 ent->sam_entry_index, ent->num_sam_entries));
511 if (ent->num_sam_entries == ent->sam_entry_index) {
513 while(ent && !get_sam_group_entries(ent)) {
514 struct getent_state *next_ent;
516 DEBUG(10, ("freeing state info for domain %s\n", ent->domain_name));
518 /* Free state information for this domain */
520 SAFE_FREE(ent->sam_entries);
522 next_ent = ent->next;
523 DLIST_REMOVE(state->getgrent_state, ent);
529 /* No more domains */
535 name_list = ent->sam_entries;
537 /* Lookup group info */
539 if (!winbindd_idmap_get_gid_from_rid(
541 name_list[ent->sam_entry_index].rid,
544 DEBUG(1, ("could not look up gid for group %s\n",
545 name_list[ent->sam_entry_index].acct_name));
547 ent->sam_entry_index++;
551 DEBUG(10, ("got gid %d for group %x\n", group_gid,
552 name_list[ent->sam_entry_index].rid));
554 /* Fill in group entry */
556 fill_domain_username(domain_group_name, ent->domain_name,
557 name_list[ent->sam_entry_index].acct_name);
559 result = fill_grent(&group_list[group_list_ndx],
561 name_list[ent->sam_entry_index].acct_name,
564 /* Fill in group membership entry */
567 struct winbindd_domain *domain;
570 find_domain_from_name(ent->domain_name))) {
571 DEBUG(3, ("No such domain %s in winbindd_getgrent\n", ent->domain_name));
576 /* Get group membership */
578 result = fill_grent_mem(
580 name_list[ent->sam_entry_index].rid,
582 &group_list[group_list_ndx].num_gr_mem,
583 &gr_mem, &gr_mem_len);
587 /* Append to group membership list */
588 new_gr_mem_list = Realloc(
590 gr_mem_list_len + gr_mem_len);
592 if (!new_gr_mem_list && (group_list[group_list_ndx].num_gr_mem != 0)) {
593 DEBUG(0, ("out of memory\n"));
594 SAFE_FREE(gr_mem_list);
599 DEBUG(10, ("list_len = %d, mem_len = %d\n",
600 gr_mem_list_len, gr_mem_len));
602 gr_mem_list = new_gr_mem_list;
604 memcpy(&gr_mem_list[gr_mem_list_len], gr_mem,
609 group_list[group_list_ndx].gr_mem_ofs =
612 gr_mem_list_len += gr_mem_len;
615 ent->sam_entry_index++;
617 /* Add group to return list */
621 DEBUG(10, ("adding group num_entries = %d\n",
622 state->response.data.num_entries));
625 state->response.data.num_entries++;
627 state->response.length +=
628 sizeof(struct winbindd_gr);
631 DEBUG(0, ("could not lookup domain group %s\n",
636 /* Copy the list of group memberships to the end of the extra data */
638 if (group_list_ndx == 0)
641 new_extra_data = Realloc(
642 state->response.extra_data,
643 group_list_ndx * sizeof(struct winbindd_gr) + gr_mem_list_len);
645 if (!new_extra_data) {
646 DEBUG(0, ("out of memory\n"));
648 SAFE_FREE(state->response.extra_data);
649 SAFE_FREE(gr_mem_list);
651 return WINBINDD_ERROR;
654 state->response.extra_data = new_extra_data;
656 memcpy(&((char *)state->response.extra_data)
657 [group_list_ndx * sizeof(struct winbindd_gr)],
658 gr_mem_list, gr_mem_list_len);
660 SAFE_FREE(gr_mem_list);
662 state->response.length += gr_mem_list_len;
664 DEBUG(10, ("returning %d groups, length = %d\n",
665 group_list_ndx, gr_mem_list_len));
671 return (group_list_ndx > 0) ? WINBINDD_OK : WINBINDD_ERROR;
674 /* List domain groups without mapping to unix ids */
676 enum winbindd_result winbindd_list_groups(struct winbindd_cli_state *state)
678 uint32 total_entries = 0;
679 struct winbindd_domain *domain;
680 char *extra_data = NULL;
682 int extra_data_len = 0, i;
684 DEBUG(3, ("[%5d]: list groups\n", state->pid));
686 /* Enumerate over trusted domains */
688 for (domain = domain_list(); domain; domain = domain->next) {
689 struct getent_state groups;
693 /* Skip domains other than WINBINDD_DOMAIN environment
695 if ((strcmp(state->request.domain, "") != 0) &&
696 !check_domain_env(state->request.domain, domain->name))
699 /* Get list of sam groups */
701 fstrcpy(groups.domain_name, domain->name);
703 get_sam_group_entries(&groups);
705 if (groups.num_sam_entries == 0) {
706 /* this domain is empty or in an error state */
710 /* keep track the of the total number of groups seen so
711 far over all domains */
712 total_entries += groups.num_sam_entries;
714 /* Allocate some memory for extra data. Note that we limit
715 account names to sizeof(fstring) = 128 characters. */
716 ted = Realloc(extra_data, sizeof(fstring) * total_entries);
719 DEBUG(0,("failed to enlarge buffer!\n"));
720 SAFE_FREE(extra_data);
721 return WINBINDD_ERROR;
725 /* Pack group list into extra data fields */
726 for (i = 0; i < groups.num_sam_entries; i++) {
727 char *group_name = ((struct acct_info *)
728 groups.sam_entries)[i].acct_name;
731 fill_domain_username(name, domain->name, group_name);
732 /* Append to extra data */
733 memcpy(&extra_data[extra_data_len], name,
735 extra_data_len += strlen(name);
736 extra_data[extra_data_len++] = ',';
739 free(groups.sam_entries);
742 /* Assign extra_data fields in response structure */
744 extra_data[extra_data_len - 1] = '\0';
745 state->response.extra_data = extra_data;
746 state->response.length += extra_data_len;
749 /* No domains may have responded but that's still OK so don't
755 /* Get user supplementary groups. This is much quicker than trying to
756 invert the groups database. */
758 enum winbindd_result winbindd_getgroups(struct winbindd_cli_state *state)
760 fstring name_domain, name_user;
762 enum SID_NAME_USE name_type;
763 uint32 user_rid, num_groups, num_gids;
766 struct winbindd_domain *domain;
767 enum winbindd_result result = WINBINDD_ERROR;
772 DEBUG(3, ("[%5d]: getgroups %s\n", state->pid,
773 state->request.data.username));
775 if (!(mem_ctx = talloc_init_named("winbindd_getgroups(%s)",
776 state->request.data.username)))
777 return WINBINDD_ERROR;
779 /* Parse domain and username */
781 if (!parse_domain_user(state->request.data.username, name_domain,
785 /* Get info for the domain */
787 if ((domain = find_domain_from_name(name_domain)) == NULL) {
788 DEBUG(0, ("could not find domain entry for domain %s\n",
793 /* Get rid and name type from name. The following costs 1 packet */
795 if (!winbindd_lookup_sid_by_name(domain, name_user, &user_sid,
797 DEBUG(1, ("user '%s' does not exist\n", name_user));
801 if (name_type != SID_NAME_USER) {
802 DEBUG(1, ("name '%s' is not a user name: %d\n",
803 name_user, name_type));
807 sid_split_rid(&user_sid, &user_rid);
809 status = domain->methods->lookup_usergroups(domain, mem_ctx, user_rid, &num_groups, &user_gids);
810 if (!NT_STATUS_IS_OK(status)) goto done;
812 /* Copy data back to client */
815 gid_list = malloc(sizeof(gid_t) * num_groups);
817 if (state->response.extra_data)
820 for (i = 0; i < num_groups; i++) {
821 if (!winbindd_idmap_get_gid_from_rid(domain->name,
823 &gid_list[num_gids])) {
825 DEBUG(1, ("unable to convert group rid %d to gid\n",
833 state->response.data.num_entries = num_gids;
834 state->response.extra_data = gid_list;
835 state->response.length += num_gids * sizeof(gid_t);
837 result = WINBINDD_OK;
841 talloc_destroy(mem_ctx);