2 Unix SMB/Netbios implementation.
5 Winbind daemon for ntdom nss module
7 Copyright (C) Tim Potter 2000
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.
26 /* Fill a grent structure from various other information */
28 static BOOL fill_grent(struct winbindd_gr *gr, char *gr_name,
33 gr->gr_gid = unix_gid;
35 /* Group name and password */
37 safe_strcpy(gr->gr_name, gr_name, sizeof(gr->gr_name) - 1);
38 safe_strcpy(gr->gr_passwd, "x", sizeof(gr->gr_passwd) - 1);
43 /* Fill in the group membership field of a NT group given by group_rid */
45 static BOOL fill_grent_mem(struct winbindd_domain *domain,
47 enum SID_NAME_USE group_name_type,
48 int *num_gr_mem, char **gr_mem, int *gr_mem_len)
50 uint32 *rid_mem = NULL, num_names = 0;
51 uint32 *name_types = NULL;
52 int buf_len, buf_ndx, i;
53 char **names = NULL, *buf;
57 if (!(mem_ctx = talloc_init()))
60 /* Initialise group membership information */
62 DEBUG(10, ("fill_grent_mem(): group %s rid 0x%x\n",
63 domain ? domain->name : "NULL", group_rid));
67 if (group_name_type != SID_NAME_DOM_GRP) {
68 DEBUG(1, ("fill_grent_mem(): rid %d in domain %s isn't a "
69 "domain group\n", group_rid, domain->name));
73 /* Lookup group members */
75 if (!winbindd_lookup_groupmem(domain, mem_ctx, group_rid, &num_names,
76 &rid_mem, &names, &name_types)) {
78 DEBUG(1, ("fill_grent_mem(): could not lookup membership "
79 "for group rid %d in domain %s\n",
80 group_rid, domain->name));
85 DEBUG(10, ("fill_grent_mem(): looked up %d names\n", num_names));
87 if (DEBUGLEVEL >= 10) {
88 for (i = 0; i < num_names; i++)
89 DEBUG(10, ("\t%20s %x %d\n", names[i], rid_mem[i],
93 /* Add members to list */
96 buf_len = buf_ndx = 0;
100 for (i = 0; i < num_names; i++) {
107 DEBUG(10, ("fill_grent_mem(): processing name %s\n",
110 /* Only add domain users */
112 if (name_types[i] != SID_NAME_USER) {
113 DEBUG(3, ("fill_grent_mem(): name %s isn't a domain "
114 "user\n", the_name));
118 /* Don't bother with machine accounts */
120 if (the_name[strlen(the_name) - 1] == '$') {
121 DEBUG(10, ("fill_grent_mem(): %s is machine account\n",
126 /* Append domain name */
128 snprintf(name, sizeof(name), "%s%s%s", domain->name,
129 lp_winbind_separator(), the_name);
133 /* Add to list or calculate buffer length */
136 buf_len += len + 1; /* List is comma separated */
138 DEBUG(10, ("fill_grent_mem(): buf_len + %d = %d\n", len + 1,
141 DEBUG(10, ("fill_grent_mem(): appending %s at index %d\n",
143 safe_strcpy(&buf[buf_ndx], name, len);
150 /* Allocate buffer */
153 if (!(buf = malloc(buf_len))) {
154 DEBUG(1, ("fill_grent_mem(): out of memory\n"));
158 memset(buf, 0, buf_len);
162 if (buf && buf_ndx > 0) {
163 buf[buf_ndx - 1] = '\0';
167 *gr_mem_len = buf_len;
169 DEBUG(10, ("fill_grent_mem(): num_mem = %d, len = %d, mem = %s\n", *num_gr_mem,
175 talloc_destroy(mem_ctx);
177 DEBUG(10, ("fill_grent_mem(): returning %d\n", result));
182 /* Return a group structure from a group name */
184 enum winbindd_result winbindd_getgrnam_from_group(struct winbindd_cli_state
188 struct winbindd_domain *domain;
189 enum SID_NAME_USE name_type;
191 fstring name_domain, name_group, name;
194 int extra_data_len, gr_mem_len;
196 DEBUG(3, ("[%5d]: getgrnam %s\n", state->pid,
197 state->request.data.groupname));
199 /* Parse domain and groupname */
201 memset(name_group, 0, sizeof(fstring));
203 tmp = state->request.data.groupname;
204 parse_domain_user(tmp, name_domain, name_group);
206 /* Reject names that don't have a domain - i.e name_domain contains
209 if (strequal(name_group, ""))
210 return WINBINDD_ERROR;
212 /* Get info for the domain */
214 if ((domain = find_domain_from_name(name_domain)) == NULL) {
215 DEBUG(0, ("getgrname_from_group(): could not get domain "
216 "sid for domain %s\n", name_domain));
217 return WINBINDD_ERROR;
220 /* Check for cached group entry */
222 if (winbindd_fetch_group_cache_entry(domain, name_group,
223 &state->response.data.gr,
224 &state->response.extra_data,
226 state->response.length += extra_data_len;
230 snprintf(name, sizeof(name), "%s\\%s", name_domain, name_group);
232 /* Get rid and name type from name */
234 if (!winbindd_lookup_sid_by_name(name, &group_sid, &name_type)) {
235 DEBUG(1, ("group %s in domain %s does not exist\n",
236 name_group, name_domain));
237 return WINBINDD_ERROR;
240 if ((name_type != SID_NAME_ALIAS) && (name_type != SID_NAME_DOM_GRP)) {
241 DEBUG(1, ("from_group: name '%s' is not a local or domain "
242 "group: %d\n", name_group, name_type));
243 return WINBINDD_ERROR;
246 /* Fill in group structure */
248 sid_split_rid(&group_sid, &group_rid);
250 if (!winbindd_idmap_get_gid_from_rid(domain->name, group_rid, &gid)) {
251 DEBUG(1, ("error sursing unix gid for sid\n"));
252 return WINBINDD_ERROR;
255 if (!fill_grent(&state->response.data.gr,
256 state->request.data.groupname, gid) ||
257 !fill_grent_mem(domain, group_rid, name_type,
258 &state->response.data.gr.num_gr_mem,
259 &gr_mem, &gr_mem_len))
260 return WINBINDD_ERROR;
262 /* Group membership lives at start of extra data */
264 state->response.data.gr.gr_mem_ofs = 0;
266 state->response.length += gr_mem_len;
267 state->response.extra_data = gr_mem;
269 /* Update cached group info */
271 winbindd_store_group_cache_entry(domain, name_group,
272 &state->response.data.gr,
273 state->response.extra_data,
279 /* Return a group structure from a gid number */
281 enum winbindd_result winbindd_getgrnam_from_gid(struct winbindd_cli_state
284 struct winbindd_domain *domain;
286 enum SID_NAME_USE name_type;
289 int extra_data_len, gr_mem_len;
292 DEBUG(3, ("[%5d]: getgrgid %d\n", state->pid,
293 state->request.data.gid));
295 /* Bug out if the gid isn't in the winbind range */
297 if ((state->request.data.gid < server_state.gid_low) ||
298 (state->request.data.gid > server_state.gid_high))
299 return WINBINDD_ERROR;
301 /* Get rid from gid */
303 if (!winbindd_idmap_get_rid_from_gid(state->request.data.gid,
304 &group_rid, &domain)) {
305 DEBUG(1, ("Could not convert gid %d to rid\n",
306 state->request.data.gid));
307 return WINBINDD_ERROR;
310 /* Try a cached entry */
312 if (winbindd_fetch_gid_cache_entry(domain,
313 state->request.data.gid,
314 &state->response.data.gr,
315 &state->response.extra_data,
317 state->response.length += extra_data_len;
321 /* Get sid from gid */
323 sid_copy(&group_sid, &domain->sid);
324 sid_append_rid(&group_sid, group_rid);
326 if (!winbindd_lookup_name_by_sid(&group_sid, group_name, &name_type)) {
327 DEBUG(1, ("Could not lookup sid\n"));
328 return WINBINDD_ERROR;
331 if (strcmp(lp_winbind_separator(),"\\"))
332 string_sub(group_name, "\\", lp_winbind_separator(),
335 if (!((name_type == SID_NAME_ALIAS) ||
336 (name_type == SID_NAME_DOM_GRP))) {
337 DEBUG(1, ("from_gid: name '%s' is not a local or domain "
338 "group: %d\n", group_name, name_type));
339 return WINBINDD_ERROR;
342 /* Fill in group structure */
344 if (!fill_grent(&state->response.data.gr, group_name,
345 state->request.data.gid) ||
346 !fill_grent_mem(domain, group_rid, name_type,
347 &state->response.data.gr.num_gr_mem,
348 &gr_mem, &gr_mem_len))
349 return WINBINDD_ERROR;
351 /* Group membership lives at start of extra data */
353 state->response.data.gr.gr_mem_ofs = 0;
355 state->response.length += gr_mem_len;
356 state->response.extra_data = gr_mem;
358 /* Update cached group info */
360 winbindd_store_gid_cache_entry(domain, state->request.data.gid,
361 &state->response.data.gr,
362 state->response.extra_data,
369 * set/get/endgrent functions
372 /* "Rewind" file pointer for group database enumeration */
374 enum winbindd_result winbindd_setgrent(struct winbindd_cli_state *state)
376 struct winbindd_domain *tmp;
378 DEBUG(3, ("[%5d]: setgrent\n", state->pid));
380 /* Check user has enabled this */
382 if (!lp_winbind_enum_groups())
383 return WINBINDD_ERROR;
385 /* Free old static data if it exists */
387 if (state->getgrent_state != NULL) {
388 free_getent_state(state->getgrent_state);
389 state->getgrent_state = NULL;
392 /* Create sam pipes for each domain we know about */
394 for (tmp = domain_list; tmp != NULL; tmp = tmp->next) {
395 struct getent_state *domain_state;
397 /* Skip domains other than WINBINDD_DOMAIN environment
400 if ((strcmp(state->request.domain, "") != 0) &&
401 !check_domain_env(state->request.domain, tmp->name))
404 /* Create a state record for this domain */
406 if ((domain_state = (struct getent_state *)
407 malloc(sizeof(struct getent_state))) == NULL)
408 return WINBINDD_ERROR;
410 ZERO_STRUCTP(domain_state);
412 domain_state->domain = tmp;
413 domain_state->mem_ctx = talloc_init();
415 /* Add to list of open domains */
417 DLIST_ADD(state->getgrent_state, domain_state);
423 /* Close file pointer to ntdom group database */
425 enum winbindd_result winbindd_endgrent(struct winbindd_cli_state *state)
427 DEBUG(3, ("[%5d]: endgrent\n", state->pid));
429 free_getent_state(state->getgrent_state);
430 state->getgrent_state = NULL;
435 /* Get the list of domain groups and domain aliases for a domain. We fill in
436 the sam_entries and num_sam_entries fields with domain group information.
437 The dispinfo_ndx field is incremented to the index of the next group to
438 fetch. Return True if some groups were returned, False otherwise. */
440 #define MAX_FETCH_SAM_ENTRIES 100
442 static BOOL get_sam_group_entries(struct getent_state *ent)
446 struct acct_info *name_list = NULL, *tnl;
448 if (ent->got_all_sam_entries)
452 if (winbindd_fetch_group_cache(ent->domain,
454 &ent->num_sam_entries))
458 /* Free any existing group info */
460 SAFE_FREE(ent->sam_entries);
461 ent->num_sam_entries = 0;
463 /* Enumerate domain groups */
466 struct acct_info *sam_grp_entries = NULL;
471 if (!(hnd = cm_get_sam_dom_handle(ent->domain->name,
475 status = cli_samr_enum_dom_groups(
476 hnd->cli, ent->mem_ctx, &hnd->pol,
477 &ent->grp_query_start_ndx,
478 0x8000, /* buffer size? */
479 (struct acct_info **) &sam_grp_entries, &num_entries);
481 /* Copy entries into return buffer */
485 tnl = Realloc(name_list,
486 sizeof(struct acct_info) *
487 (ent->num_sam_entries +
491 DEBUG(0,("get_sam_group_entries: unable to "
492 "realloc a structure!\n"));
493 SAFE_FREE(name_list);
499 memcpy(&name_list[ent->num_sam_entries],
501 num_entries * sizeof(struct acct_info));
503 SAFE_FREE(sam_grp_entries);
506 ent->num_sam_entries += num_entries;
508 if (NT_STATUS_V(status) != NT_STATUS_V(STATUS_MORE_ENTRIES))
511 } while (ent->num_sam_entries < MAX_FETCH_SAM_ENTRIES);
514 /* Fill cache with received entries */
516 winbindd_store_group_cache(ent->domain, ent->sam_entries,
517 ent->num_sam_entries);
520 /* Fill in remaining fields */
522 ent->sam_entries = name_list;
523 ent->sam_entry_index = 0;
524 ent->got_all_sam_entries = (NT_STATUS_V(status) !=
525 NT_STATUS_V(STATUS_MORE_ENTRIES));
527 return ent->num_sam_entries > 0;
530 /* Fetch next group entry from ntdom database */
532 #define MAX_GETGRENT_GROUPS 500
534 enum winbindd_result winbindd_getgrent(struct winbindd_cli_state *state)
536 struct getent_state *ent;
537 struct winbindd_gr *group_list = NULL;
538 int num_groups, group_list_ndx = 0, i, gr_mem_list_len = 0;
539 char *sep, *new_extra_data, *gr_mem_list = NULL;
541 DEBUG(3, ("[%5d]: getgrent\n", state->pid));
543 /* Check user has enabled this */
545 if (!lp_winbind_enum_groups())
546 return WINBINDD_ERROR;
548 num_groups = MIN(MAX_GETGRENT_GROUPS, state->request.data.num_entries);
550 if ((state->response.extra_data =
551 malloc(num_groups * sizeof(struct winbindd_gr))) == NULL)
552 return WINBINDD_ERROR;
554 state->response.data.num_entries = 0;
556 group_list = (struct winbindd_gr *)state->response.extra_data;
557 sep = lp_winbind_separator();
559 if (!(ent = state->getgrent_state))
560 return WINBINDD_ERROR;
562 /* Start sending back groups */
564 for (i = 0; i < num_groups; i++) {
565 struct acct_info *name_list = NULL;
566 fstring domain_group_name;
570 /* Do we need to fetch another chunk of groups? */
574 DEBUG(10, ("getgrent(): entry_index = %d, num_entries = %d\n",
575 ent->sam_entry_index, ent->num_sam_entries));
577 if (ent->num_sam_entries == ent->sam_entry_index) {
579 while(ent && !get_sam_group_entries(ent)) {
580 struct getent_state *next_ent;
582 DEBUG(10, ("getgrent(): freeing state info for "
583 "domain %s\n", ent->domain->name));
585 /* Free state information for this domain */
587 SAFE_FREE(ent->sam_entries);
588 ent->sam_entries = NULL;
590 next_ent = ent->next;
591 DLIST_REMOVE(state->getgrent_state, ent);
597 /* No more domains */
603 name_list = ent->sam_entries;
605 /* Lookup group info */
607 if (!winbindd_idmap_get_gid_from_rid(
609 name_list[ent->sam_entry_index].rid,
612 DEBUG(1, ("getgrent(): could not look up gid for group %s\n",
613 name_list[ent->sam_entry_index].acct_name));
615 ent->sam_entry_index++;
619 DEBUG(10, ("getgrent(): got gid %d for group %x\n", group_gid,
620 name_list[ent->sam_entry_index].rid));
622 /* Fill in group entry */
624 slprintf(domain_group_name, sizeof(domain_group_name) - 1,
625 "%s%s%s", ent->domain->name, lp_winbind_separator(),
626 name_list[ent->sam_entry_index].acct_name);
628 result = fill_grent(&group_list[group_list_ndx],
629 domain_group_name, group_gid);
631 /* Fill in group membership entry */
635 char *gr_mem, *new_gr_mem_list;
637 /* Get group membership */
639 result = fill_grent_mem(
641 name_list[ent->sam_entry_index].rid,
643 &group_list[group_list_ndx].num_gr_mem,
644 &gr_mem, &gr_mem_len);
646 /* Append to group membership list */
648 new_gr_mem_list = Realloc(
650 gr_mem_list_len + gr_mem_len);
652 if (!new_gr_mem_list && (group_list[group_list_ndx].num_gr_mem != 0)) {
653 DEBUG(0, ("getgrent(): out of memory\n"));
654 SAFE_FREE(gr_mem_list);
659 DEBUG(10, ("getgrent(): list_len = %d, mem_len = %d\n",
660 gr_mem_list_len, gr_mem_len));
662 gr_mem_list = new_gr_mem_list;
664 memcpy(&gr_mem_list[gr_mem_list_len], gr_mem,
669 group_list[group_list_ndx].gr_mem_ofs =
672 gr_mem_list_len += gr_mem_len;
675 ent->sam_entry_index++;
677 /* Add group to return list */
681 DEBUG(10, ("getgrent(): adding group num_entries = %d\n",
682 state->response.data.num_entries));
685 state->response.data.num_entries++;
687 state->response.length +=
688 sizeof(struct winbindd_gr);
691 DEBUG(0, ("could not lookup domain group %s\n",
696 /* Copy the list of group memberships to the end of the extra data */
698 if (group_list_ndx == 0)
701 new_extra_data = Realloc(
702 state->response.extra_data,
703 group_list_ndx * sizeof(struct winbindd_gr) + gr_mem_list_len);
705 if (!new_extra_data) {
706 DEBUG(0, ("out of memory\n"));
708 SAFE_FREE(state->response.extra_data);
709 SAFE_FREE(gr_mem_list);
711 return WINBINDD_ERROR;
714 state->response.extra_data = new_extra_data;
716 memcpy(&((char *)state->response.extra_data)
717 [group_list_ndx * sizeof(struct winbindd_gr)],
718 gr_mem_list, gr_mem_list_len);
720 SAFE_FREE(gr_mem_list);
722 state->response.length += gr_mem_list_len;
724 DEBUG(10, ("getgrent(): returning %d groups, length = %d\n",
725 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 uint32 num_domain_entries;
739 struct winbindd_domain *domain;
740 struct getent_state groups;
741 char *extra_data = NULL;
743 int extra_data_len = 0, i;
744 void *sam_entries = NULL;
746 DEBUG(3, ("[%5d]: list groups\n", state->pid));
748 /* Enumerate over trusted domains */
752 for (domain = domain_list; domain; domain = domain->next) {
754 /* Skip domains other than WINBINDD_DOMAIN environment
757 if ((strcmp(state->request.domain, "") != 0) &&
758 !check_domain_env(state->request.domain, domain->name))
761 /* Get list of sam groups */
764 groups.domain = domain;
765 groups.mem_ctx = talloc_init();
768 * iterate through all groups
769 * total_entries: maintains a total count over **all domains**
770 * num_domain_entries: is the running count for this domain
773 num_domain_entries = 0;
775 while (get_sam_group_entries(&groups)) {
779 offset = sizeof(struct acct_info) * num_domain_entries;
780 new_size = sizeof(struct acct_info)
781 * (groups.num_sam_entries + num_domain_entries);
782 sam_entries = Realloc(sam_entries, new_size);
785 return WINBINDD_ERROR;
787 num_domain_entries += groups.num_sam_entries;
788 memcpy (((char *)sam_entries)+offset,
790 sizeof(struct acct_info) *
791 groups.num_sam_entries);
793 groups.sam_entries = NULL;
794 groups.num_sam_entries = 0;
797 /* skip remainder of loop if we idn;t retrieve any groups */
799 if (num_domain_entries == 0)
802 /* setup the groups struct to contain all the groups
803 retrieved for this domain */
805 groups.num_sam_entries = num_domain_entries;
806 groups.sam_entries = sam_entries;
808 /* keep track the of the total number of groups seen so
809 far over all domains */
811 total_entries += groups.num_sam_entries;
813 /* Allocate some memory for extra data. Note that we limit
814 account names to sizeof(fstring) = 128 characters. */
816 ted = Realloc(extra_data, sizeof(fstring) * total_entries);
819 DEBUG(0,("winbindd_list_groups: failed to enlarge "
821 SAFE_FREE(extra_data);
822 talloc_destroy(groups.mem_ctx);
823 return WINBINDD_ERROR;
827 /* Pack group list into extra data fields */
829 for (i = 0; i < groups.num_sam_entries; i++) {
830 char *group_name = ((struct acct_info *)
831 groups.sam_entries)[i].acct_name;
834 /* Convert unistring to ascii */
836 snprintf(name, sizeof(name), "%s%s%s", domain->name,
837 lp_winbind_separator(), group_name);
839 /* Append to extra data */
841 memcpy(&extra_data[extra_data_len], name,
844 extra_data_len += strlen(name);
846 extra_data[extra_data_len++] = ',';
850 talloc_destroy(groups.mem_ctx);
853 /* Assign extra_data fields in response structure */
856 extra_data[extra_data_len - 1] = '\0';
857 state->response.extra_data = extra_data;
858 state->response.length += extra_data_len;
861 /* No domains may have responded but that's still OK so don't
867 /* Get user supplementary groups. This is much quicker than trying to
868 invert the groups database. */
870 enum winbindd_result winbindd_getgroups(struct winbindd_cli_state *state)
872 fstring name_domain, name_user, name;
874 enum SID_NAME_USE name_type;
875 uint32 user_rid, num_groups, num_gids;
876 DOM_GID *user_groups = NULL;
877 struct winbindd_domain *domain;
878 enum winbindd_result result;
882 DEBUG(3, ("[%5d]: getgroups %s\n", state->pid,
883 state->request.data.username));
885 /* Parse domain and username */
887 parse_domain_user(state->request.data.username, name_domain,
890 /* Reject names that don't have a domain - i.e name_domain contains
893 if (strequal(name_domain, ""))
894 return WINBINDD_ERROR;
896 /* Get info for the domain */
898 if ((domain = find_domain_from_name(name_domain)) == NULL) {
899 DEBUG(0, ("could not find domain entry for domain %s\n",
901 return WINBINDD_ERROR;
904 slprintf(name, sizeof(name) - 1, "%s\\%s", name_domain, name_user);
906 /* Get rid and name type from name. The following costs 1 packet */
908 if (!winbindd_lookup_sid_by_name(name, &user_sid, &name_type)) {
909 DEBUG(1, ("user '%s' does not exist\n", name_user));
910 return WINBINDD_ERROR;
913 if (name_type != SID_NAME_USER) {
914 DEBUG(1, ("name '%s' is not a user name: %d\n", name_user,
916 return WINBINDD_ERROR;
919 sid_split_rid(&user_sid, &user_rid);
921 if (!winbindd_lookup_usergroups(domain, user_rid, &num_groups,
923 return WINBINDD_ERROR;
925 /* Copy data back to client */
928 gid_list = malloc(sizeof(gid_t) * num_groups);
930 if (state->response.extra_data) {
931 result = WINBINDD_ERROR;
935 for (i = 0; i < num_groups; i++) {
936 if (!winbindd_idmap_get_gid_from_rid(
937 domain->name, user_groups[i].g_rid,
938 &gid_list[num_gids])) {
940 DEBUG(1, ("unable to convert group rid %d to gid\n",
941 user_groups[i].g_rid));
948 state->response.data.num_entries = num_gids;
949 state->response.extra_data = gid_list;
950 state->response.length += num_gids * sizeof(gid_t);
952 result = WINBINDD_OK;
955 SAFE_FREE(user_groups);