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 enum SID_NAME_USE *name_types = NULL;
52 int buf_len, buf_ndx, i;
53 char **names = NULL, *buf;
56 if (!num_gr_mem || !gr_mem || !gr_mem_len) return False;
58 /* Initialise group membership information */
60 DEBUG(10, ("fill_grent_mem(): group %s rid 0x%x\n",
61 domain ? domain->name : "NULL", group_rid));
65 if (group_name_type != SID_NAME_DOM_GRP) {
66 DEBUG(1, ("fill_grent_mem(): rid %d in domain %s isn't a "
67 "domain group\n", group_rid, domain->name));
71 /* Lookup group members */
73 if (!winbindd_lookup_groupmem(domain, group_rid, &num_names,
74 &rid_mem, &names, &name_types)) {
76 DEBUG(1, ("fill_grent_mem(): could not lookup membership "
77 "for group rid %d in domain %s\n",
78 group_rid, domain->name));
83 DEBUG(10, ("fill_grent_mem(): looked up %d names\n", num_names));
85 if (DEBUGLEVEL >= 10) {
86 for (i = 0; i < num_names; i++) {
87 DEBUG(10, ("\t%20s %x %d\n", names[i], rid_mem[i],
92 /* Add members to list */
95 buf_len = buf_ndx = 0;
99 for (i = 0; i < num_names; i++) {
106 DEBUG(10, ("fill_grent_mem(): processing name %s\n", the_name));
108 /* Only add domain users */
110 if (name_types[i] != SID_NAME_USER) {
111 DEBUG(3, ("fill_grent_mem(): name %s isn't a domain "
112 "user\n", the_name));
116 /* Don't bother with machine accounts */
118 if (the_name[strlen(the_name) - 1] == '$') {
119 DEBUG(10, ("fill_grent_mem(): %s is machine account\n",
124 /* Append domain name */
126 snprintf(name, sizeof(name), "%s%s%s", domain->name,
127 lp_winbind_separator(), the_name);
131 /* Add to list or calculate buffer length */
134 buf_len += len + 1; /* List is comma separated */
136 DEBUG(10, ("fill_grent_mem(): buf_len + %d = %d\n", len + 1,
139 DEBUG(10, ("fill_grent_mem(): appending %s at index %d\n",
141 safe_strcpy(&buf[buf_ndx], name, len);
148 /* Allocate buffer */
151 if (!(buf = malloc(buf_len))) {
152 DEBUG(1, ("fill_grent_mem(): out of memory\n"));
156 memset(buf, 0, buf_len);
160 if (buf && buf_ndx > 0) {
161 buf[buf_ndx - 1] = '\0';
165 *gr_mem_len = buf_len;
167 DEBUG(10, ("fill_grent_mem(): num_mem = %d, len = %d, mem = %s\n", *num_gr_mem,
174 /* Free memory allocated in winbindd_lookup_groupmem() */
176 safe_free(name_types);
179 free_char_array(num_names, names);
181 DEBUG(10, ("fill_grent_mem(): returning %d\n", result));
186 /* Return a group structure from a group name */
188 enum winbindd_result winbindd_getgrnam_from_group(struct winbindd_cli_state
192 struct winbindd_domain *domain;
193 enum SID_NAME_USE name_type;
195 fstring name_domain, name_group, name;
198 int extra_data_len, gr_mem_len;
200 DEBUG(3, ("[%5d]: getgrnam %s\n", state->pid,
201 state->request.data.groupname));
203 /* Parse domain and groupname */
205 memset(name_group, 0, sizeof(fstring));
207 tmp = state->request.data.groupname;
208 parse_domain_user(tmp, name_domain, name_group);
210 /* Reject names that don't have a domain - i.e name_domain contains
213 if (strequal(name_group, "")) {
214 return WINBINDD_ERROR;
217 /* Get info for the domain */
219 if ((domain = find_domain_from_name(name_domain)) == NULL) {
220 DEBUG(0, ("getgrname_from_group(): could not get domain "
221 "sid for domain %s\n", name_domain));
222 return WINBINDD_ERROR;
225 if (!domain_handles_open(domain)) {
226 return WINBINDD_ERROR;
229 /* Check for cached group entry */
231 if (winbindd_fetch_group_cache_entry(name_domain, name_group,
232 &state->response.data.gr,
233 &state->response.extra_data,
235 state->response.length += extra_data_len;
239 snprintf(name, sizeof(name), "%s\\%s", name_domain, name_group);
241 /* Get rid and name type from name */
243 if (!winbindd_lookup_sid_by_name(name, &group_sid, &name_type)) {
244 DEBUG(1, ("group %s in domain %s does not exist\n",
245 name_group, name_domain));
246 return WINBINDD_ERROR;
249 if ((name_type != SID_NAME_ALIAS) && (name_type != SID_NAME_DOM_GRP)) {
250 DEBUG(1, ("from_group: name '%s' is not a local or domain "
251 "group: %d\n", name_group, name_type));
252 return WINBINDD_ERROR;
255 /* Fill in group structure */
257 sid_split_rid(&group_sid, &group_rid);
259 if (!winbindd_idmap_get_gid_from_rid(domain->name, group_rid, &gid)) {
260 DEBUG(1, ("error sursing unix gid for sid\n"));
261 return WINBINDD_ERROR;
264 if (!fill_grent(&state->response.data.gr,
265 state->request.data.groupname, gid) ||
266 !fill_grent_mem(domain, group_rid, name_type,
267 &state->response.data.gr.num_gr_mem,
268 &gr_mem, &gr_mem_len)) {
269 return WINBINDD_ERROR;
272 /* Group membership lives at start of extra data */
274 state->response.data.gr.gr_mem_ofs = 0;
276 state->response.length += gr_mem_len;
277 state->response.extra_data = gr_mem;
279 /* Update cached group info */
281 winbindd_store_group_cache_entry(name_domain, name_group,
282 &state->response.data.gr,
283 state->response.extra_data,
289 /* Return a group structure from a gid number */
291 enum winbindd_result winbindd_getgrnam_from_gid(struct winbindd_cli_state
294 struct winbindd_domain *domain;
296 enum SID_NAME_USE name_type;
299 int extra_data_len, gr_mem_len;
302 /* Bug out if the gid isn't in the winbind range */
304 if ((state->request.data.gid < server_state.gid_low) ||
305 (state->request.data.gid > server_state.gid_high)) {
306 return WINBINDD_ERROR;
309 DEBUG(3, ("[%5d]: getgrgid %d\n", state->pid,
310 state->request.data.gid));
312 /* Get rid from gid */
314 if (!winbindd_idmap_get_rid_from_gid(state->request.data.gid,
315 &group_rid, &domain)) {
316 DEBUG(1, ("Could not convert gid %d to rid\n",
317 state->request.data.gid));
318 return WINBINDD_ERROR;
321 if (!domain_handles_open(domain)) {
322 return WINBINDD_ERROR;
325 /* Try a cached entry */
327 if (winbindd_fetch_gid_cache_entry(domain->name,
328 state->request.data.gid,
329 &state->response.data.gr,
330 &state->response.extra_data,
332 state->response.length += extra_data_len;
336 /* Get sid from gid */
338 sid_copy(&group_sid, &domain->sid);
339 sid_append_rid(&group_sid, group_rid);
341 if (!winbindd_lookup_name_by_sid(&group_sid, group_name, &name_type)) {
342 DEBUG(1, ("Could not lookup sid\n"));
343 return WINBINDD_ERROR;
346 if (strcmp(lp_winbind_separator(),"\\")) {
347 string_sub(group_name, "\\", lp_winbind_separator(),
351 if (!((name_type == SID_NAME_ALIAS) ||
352 (name_type == SID_NAME_DOM_GRP))) {
353 DEBUG(1, ("from_gid: name '%s' is not a local or domain "
354 "group: %d\n", group_name, name_type));
355 return WINBINDD_ERROR;
358 /* Fill in group structure */
360 if (!fill_grent(&state->response.data.gr, group_name,
361 state->request.data.gid) ||
362 !fill_grent_mem(domain, group_rid, name_type,
363 &state->response.data.gr.num_gr_mem,
364 &gr_mem, &gr_mem_len)) {
365 return WINBINDD_ERROR;
369 /* Group membership lives at start of extra data */
371 state->response.data.gr.gr_mem_ofs = 0;
373 state->response.length += gr_mem_len;
374 state->response.extra_data = gr_mem;
376 /* Update cached group info */
378 winbindd_store_gid_cache_entry(domain->name, state->request.data.gid,
379 &state->response.data.gr,
380 state->response.extra_data,
387 * set/get/endgrent functions
390 /* "Rewind" file pointer for group database enumeration */
392 enum winbindd_result winbindd_setgrent(struct winbindd_cli_state *state)
394 struct winbindd_domain *tmp;
396 DEBUG(3, ("[%5d]: setgrent\n", state->pid));
398 if (state == NULL) return WINBINDD_ERROR;
400 /* Check user has enabled this */
402 if (!lp_winbind_enum_groups()) {
403 return WINBINDD_ERROR;
406 /* Free old static data if it exists */
408 if (state->getgrent_state != NULL) {
409 free_getent_state(state->getgrent_state);
410 state->getgrent_state = NULL;
413 /* Create sam pipes for each domain we know about */
415 for (tmp = domain_list; tmp != NULL; tmp = tmp->next) {
416 struct getent_state *domain_state;
418 /* Skip domains other than WINBINDD_DOMAIN environment
421 if ((strcmp(state->request.domain, "") != 0) &&
422 !check_domain_env(state->request.domain, tmp->name)) {
426 /* Create a state record for this domain */
428 if ((domain_state = (struct getent_state *)
429 malloc(sizeof(struct getent_state))) == NULL) {
431 return WINBINDD_ERROR;
434 ZERO_STRUCTP(domain_state);
436 /* Add to list of open domains */
438 domain_state->domain = tmp;
439 DLIST_ADD(state->getgrent_state, domain_state);
445 /* Close file pointer to ntdom group database */
447 enum winbindd_result winbindd_endgrent(struct winbindd_cli_state *state)
449 DEBUG(3, ("[%5d]: endgrent\n", state->pid));
451 if (state == NULL) return WINBINDD_ERROR;
453 free_getent_state(state->getgrent_state);
454 state->getgrent_state = NULL;
459 /* Get the list of domain groups and domain aliases for a domain. We fill in
460 the sam_entries and num_sam_entries fields with domain group information.
461 The dispinfo_ndx field is incremented to the index of the next group to
462 fetch. Return True if some groups were returned, False otherwise. */
464 #define MAX_FETCH_SAM_ENTRIES 100
466 static BOOL get_sam_group_entries(struct getent_state *ent)
468 uint32 status, num_entries, start_ndx = 0;
469 struct acct_info *name_list = NULL;
471 if (ent->got_all_sam_entries) {
476 if (winbindd_fetch_group_cache(ent->domain->name,
478 &ent->num_sam_entries)) {
483 /* Fetch group entries */
485 if (!domain_handles_open(ent->domain)) {
489 /* Free any existing group info */
491 if (ent->sam_entries) {
492 free(ent->sam_entries);
493 ent->sam_entries = NULL;
494 ent->num_sam_entries = 0;
497 /* Enumerate domain groups */
500 struct acct_info *sam_grp_entries = NULL;
505 wb_samr_enum_dom_groups(&ent->domain->
508 0x8000, /* buffer size? */
509 (struct acct_info **)
513 /* Copy entries into return buffer */
517 name_list = Realloc(name_list,
518 sizeof(struct acct_info) *
519 (ent->num_sam_entries +
522 memcpy(&name_list[ent->num_sam_entries],
524 num_entries * sizeof(struct acct_info));
527 ent->num_sam_entries += num_entries;
529 if (status != STATUS_MORE_ENTRIES) {
533 } while (ent->num_sam_entries < MAX_FETCH_SAM_ENTRIES);
536 /* Fill cache with received entries */
538 winbindd_store_group_cache(ent->domain->name, ent->sam_entries,
539 ent->num_sam_entries);
542 /* Fill in remaining fields */
544 ent->sam_entries = name_list;
545 ent->sam_entry_index = 0;
546 ent->got_all_sam_entries = (status != STATUS_MORE_ENTRIES);
548 return ent->num_sam_entries > 0;
551 /* Fetch next group entry from ntdom database */
553 #define MAX_GETGRENT_GROUPS 500
555 enum winbindd_result winbindd_getgrent(struct winbindd_cli_state *state)
557 struct getent_state *ent;
558 struct winbindd_gr *group_list = NULL;
559 int num_groups, group_list_ndx = 0, i, gr_mem_list_len = 0;
560 char *sep, *new_extra_data, *gr_mem_list = NULL;
562 DEBUG(3, ("[%5d]: getgrent\n", state->pid));
564 if (state == NULL) return WINBINDD_ERROR;
566 /* Check user has enabled this */
568 if (!lp_winbind_enum_groups()) {
569 return WINBINDD_ERROR;
572 num_groups = MIN(MAX_GETGRENT_GROUPS, state->request.data.num_entries);
574 if ((state->response.extra_data =
575 malloc(num_groups * sizeof(struct winbindd_gr))) == NULL) {
576 return WINBINDD_ERROR;
579 state->response.data.num_entries = 0;
581 group_list = (struct winbindd_gr *)state->response.extra_data;
582 sep = lp_winbind_separator();
584 if (!(ent = state->getgrent_state)) {
585 return WINBINDD_ERROR;
588 /* Start sending back groups */
590 for (i = 0; i < num_groups; i++) {
591 struct acct_info *name_list = NULL;
592 fstring domain_group_name;
596 /* Do we need to fetch another chunk of groups? */
600 DEBUG(10, ("getgrent(): entry_index = %d, num_entries = %d\n",
601 ent->sam_entry_index, ent->num_sam_entries));
603 if (ent->num_sam_entries == ent->sam_entry_index) {
605 while(ent && !get_sam_group_entries(ent)) {
606 struct getent_state *next_ent;
608 DEBUG(10, ("getgrent(): freeing state info for "
609 "domain %s\n", ent->domain->name));
611 /* Free state information for this domain */
613 safe_free(ent->sam_entries);
614 ent->sam_entries = NULL;
616 next_ent = ent->next;
617 DLIST_REMOVE(state->getgrent_state, ent);
623 /* No more domains */
628 name_list = ent->sam_entries;
630 /* Lookup group info */
632 if (!winbindd_idmap_get_gid_from_rid(
634 name_list[ent->sam_entry_index].rid,
637 DEBUG(1, ("getgrent(): could not look up gid for group %s\n",
638 name_list[ent->sam_entry_index].acct_name));
640 ent->sam_entry_index++;
644 DEBUG(10, ("getgrent(): got gid %d for group %x\n", group_gid,
645 name_list[ent->sam_entry_index].rid));
647 /* Fill in group entry */
649 slprintf(domain_group_name, sizeof(domain_group_name) - 1,
650 "%s%s%s", ent->domain->name, lp_winbind_separator(),
651 name_list[ent->sam_entry_index].acct_name);
653 result = fill_grent(&group_list[group_list_ndx],
654 domain_group_name, group_gid);
656 /* Fill in group membership entry */
660 char *gr_mem, *new_gr_mem_list;
662 /* Get group membership */
664 result = fill_grent_mem(
666 name_list[ent->sam_entry_index].rid,
668 &group_list[group_list_ndx].num_gr_mem,
669 &gr_mem, &gr_mem_len);
671 /* Append to group membership list */
673 new_gr_mem_list = Realloc(
675 gr_mem_list_len + gr_mem_len);
677 if (!new_gr_mem_list) {
678 DEBUG(0, ("getgrent(): out of memory\n"));
684 DEBUG(10, ("getgrent(): list_len = %d, mem_len = %d\n",
685 gr_mem_list_len, gr_mem_len));
687 gr_mem_list = new_gr_mem_list;
689 memcpy(&gr_mem_list[gr_mem_list_len], gr_mem,
694 group_list[group_list_ndx].gr_mem_ofs =
697 gr_mem_list_len += gr_mem_len;
700 ent->sam_entry_index++;
702 /* Add group to return list */
706 DEBUG(10, ("getgrent(): adding group num_entries = %d\n",
707 state->response.data.num_entries));
710 state->response.data.num_entries++;
712 state->response.length +=
713 sizeof(struct winbindd_gr);
716 DEBUG(0, ("could not lookup domain group %s\n",
721 /* Copy the list of group memberships to the end of the extra data */
723 if (group_list_ndx == 0) {
727 new_extra_data = Realloc(
728 state->response.extra_data,
729 group_list_ndx * sizeof(struct winbindd_gr) + gr_mem_list_len);
731 if (!new_extra_data) {
732 DEBUG(0, ("out of memory\n"));
734 safe_free(state->response.extra_data);
735 state->response.extra_data = NULL;
736 safe_free(gr_mem_list);
738 return WINBINDD_ERROR;
741 state->response.extra_data = new_extra_data;
743 memcpy(&((char *)state->response.extra_data)
744 [group_list_ndx * sizeof(struct winbindd_gr)],
745 gr_mem_list, gr_mem_list_len);
747 safe_free(gr_mem_list);
749 state->response.length += gr_mem_list_len;
751 DEBUG(10, ("getgrent(): returning %d groups, length = %d\n",
752 group_list_ndx, gr_mem_list_len));
757 return (group_list_ndx > 0) ? WINBINDD_OK : WINBINDD_ERROR;
760 /* List domain groups without mapping to unix ids */
762 enum winbindd_result winbindd_list_groups(struct winbindd_cli_state *state)
764 uint32 total_entries = 0;
765 struct winbindd_domain *domain;
766 struct getent_state groups;
767 char *extra_data = NULL;
768 int extra_data_len = 0, i;
770 DEBUG(3, ("[%5d]: list groups\n", state->pid));
772 /* Enumerate over trusted domains */
774 for (domain = domain_list; domain; domain = domain->next) {
776 /* Skip domains other than WINBINDD_DOMAIN environment
779 if ((strcmp(state->request.domain, "") != 0) &&
780 !check_domain_env(state->request.domain, domain->name)) {
784 /* Get list of sam groups */
787 groups.domain = domain;
789 get_sam_group_entries(&groups);
791 if (groups.num_sam_entries == 0) continue;
793 /* Allocate some memory for extra data. Note that we limit
794 account names to sizeof(fstring) = 128 characters. */
796 total_entries += groups.num_sam_entries;
797 extra_data = Realloc(extra_data,
798 sizeof(fstring) * total_entries);
801 return WINBINDD_ERROR;
804 /* Pack group list into extra data fields */
806 for (i = 0; i < groups.num_sam_entries; i++) {
807 char *group_name = ((struct acct_info *)
808 groups.sam_entries)[i].acct_name;
811 /* Convert unistring to ascii */
813 snprintf(name, sizeof(name), "%s%s%s",
814 domain->name, lp_winbind_separator(),
817 /* Append to extra data */
819 memcpy(&extra_data[extra_data_len], name,
821 extra_data_len += strlen(name);
823 extra_data[extra_data_len++] = ',';
827 /* Assign extra_data fields in response structure */
830 extra_data[extra_data_len - 1] = '\0';
831 state->response.extra_data = extra_data;
832 state->response.length += extra_data_len;
835 /* No domains may have responded but that's still OK so don't
841 /* Get user supplementary groups. This is much quicker than trying to
842 invert the groups database. */
844 enum winbindd_result winbindd_getgroups(struct winbindd_cli_state *state)
846 fstring name_domain, name_user, name;
848 enum SID_NAME_USE name_type;
849 uint32 user_rid, num_groups, num_gids;
850 DOM_GID *user_groups = NULL;
851 struct winbindd_domain *domain;
852 enum winbindd_result result;
856 DEBUG(3, ("[%5d]: getgroups %s\n", state->pid,
857 state->request.data.username));
859 if (state == NULL) return WINBINDD_ERROR;
861 /* Parse domain and username */
863 parse_domain_user(state->request.data.username, name_domain,
866 /* Reject names that don't have a domain - i.e name_domain contains
869 if (strequal(name_domain, "")) {
870 return WINBINDD_ERROR;
873 /* Get info for the domain */
875 if ((domain = find_domain_from_name(name_domain)) == NULL) {
876 DEBUG(0, ("could not find domain entry for domain %s\n",
878 return WINBINDD_ERROR;
881 if (!domain_handles_open(domain)) {
882 return WINBINDD_ERROR;
885 slprintf(name, sizeof(name) - 1, "%s\\%s", name_domain, name_user);
887 /* Get rid and name type from name. The following costs 1 packet */
889 if (!winbindd_lookup_sid_by_name(name, &user_sid, &name_type)) {
890 DEBUG(1, ("user '%s' does not exist\n", name_user));
891 return WINBINDD_ERROR;
894 if (name_type != SID_NAME_USER) {
895 DEBUG(1, ("name '%s' is not a user name: %d\n", name_user,
897 return WINBINDD_ERROR;
900 sid_split_rid(&user_sid, &user_rid);
902 if (!winbindd_lookup_usergroups(domain, user_rid, &num_groups,
904 return WINBINDD_ERROR;
907 /* Copy data back to client */
910 gid_list = malloc(sizeof(gid_t) * num_groups);
912 if (state->response.extra_data) {
913 result = WINBINDD_ERROR;
917 for (i = 0; i < num_groups; i++) {
918 if (!winbindd_idmap_get_gid_from_rid(
919 domain->name, user_groups[i].g_rid,
920 &gid_list[num_gids])) {
922 DEBUG(1, ("unable to convert group rid %d to gid\n",
923 user_groups[i].g_rid));
930 state->response.data.num_entries = num_gids;
931 state->response.extra_data = gid_list;
932 state->response.length += num_gids * sizeof(gid_t);
934 result = WINBINDD_OK;
937 safe_free(user_groups);