e24e15429f392d1aa59cfb7d7acaeb177595dab0
[metze/samba/wip.git] / source3 / nsswitch / winbindd_util.c
1 /* 
2    Unix SMB/Netbios implementation.
3    Version 2.0
4
5    Winbind daemon for ntdom nss module
6
7    Copyright (C) Tim Potter 2000
8    
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.
13    
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.
18    
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.
22 */
23
24 #include "winbindd.h"
25 #include "sids.h"
26
27 /* Debug connection state */
28
29 void debug_conn_state(void)
30 {
31         struct winbindd_domain *domain;
32
33         DEBUG(3, ("server: dc=%s, pwdb_init=%d, lsa_hnd=%d\n", 
34                   server_state.controller,
35                   server_state.pwdb_initialised,
36                   server_state.lsa_handle_open));
37
38         for (domain = domain_list; domain; domain = domain->next) {
39                 DEBUG(3, ("%s: dc=%s, got_sid=%d, sam_hnd=%d sam_dom_hnd=%d\n",
40                           domain->name, domain->controller,
41                           domain->got_domain_info, domain->sam_handle_open,
42                           domain->sam_dom_handle_open));
43         }
44 }
45
46 /* Add a trusted domain to our list of domains */
47
48 static struct winbindd_domain *add_trusted_domain(char *domain_name)
49 {
50     struct winbindd_domain *domain, *tmp;
51
52     for (tmp = domain_list; tmp != NULL; tmp = tmp->next) {
53             if (strcmp(domain_name, tmp->name) == 0) {
54                     DEBUG(3, ("domain %s already in trusted list\n",
55                               domain_name));
56                     return tmp;
57             }
58     }
59
60     DEBUG(1, ("adding trusted domain %s\n", domain_name));
61
62     /* Create new domain entry */
63
64     if ((domain = (struct winbindd_domain *)malloc(sizeof(*domain))) == NULL) {
65         return NULL;
66     }
67
68     /* Fill in fields */
69
70     ZERO_STRUCTP(domain);
71
72     if (domain_name) {
73         fstrcpy(domain->name, domain_name);
74     }
75
76     /* Link to domain list */
77
78     DLIST_ADD(domain_list, domain);
79
80     return domain;
81 }
82
83 /* Look up global info for the winbind daemon */
84
85 static BOOL get_trusted_domains(void)
86 {
87         uint32 enum_ctx = 0;
88         uint32 num_doms = 0;
89         char **domains = NULL;
90         DOM_SID *sids = NULL;
91         BOOL result;
92         int i;
93         
94         DEBUG(1, ("getting trusted domain list\n"));
95
96         /* Add our workgroup - keep handle to look up trusted domains */
97         if (!add_trusted_domain(lp_workgroup())) {
98                 DEBUG(0, ("could not add record for domain %s\n", 
99                           lp_workgroup()));
100                 return False;
101         }
102         
103         /* Enumerate list of trusted domains */ 
104         result = wb_lsa_enum_trust_dom(&server_state.lsa_handle, &enum_ctx,
105                                        &num_doms, &domains, &sids);
106         
107         if (!result || !domains) return False;
108         
109         /* Add each domain to the trusted domain list */
110         for(i = 0; i < num_doms; i++) {
111                 if (!add_trusted_domain(domains[i])) {
112                         DEBUG(0, ("could not add record for domain %s\n", 
113                                   domains[i]));
114                         result = False;
115                 }
116         }
117         
118         return True;
119 }
120
121 /* Open sam and sam domain handles */
122
123 static BOOL open_sam_handles(struct winbindd_domain *domain)
124 {
125         /* Get domain info (sid and controller name) */
126
127         if (!domain->got_domain_info) {
128                 domain->got_domain_info = get_domain_info(domain);
129                 if (!domain->got_domain_info) return False;
130         }
131
132         /* Shut down existing sam handles */
133
134         if (domain->sam_dom_handle_open) {
135                 wb_samr_close(&domain->sam_dom_handle);
136                 domain->sam_dom_handle_open = False;
137         }
138
139         if (domain->sam_handle_open) {
140                 wb_samr_close(&domain->sam_handle);
141                 domain->sam_handle_open = False;
142         }
143
144         /* Open sam handle */
145
146         domain->sam_handle_open = 
147                 wb_samr_connect(domain->controller, 
148                                 SEC_RIGHTS_MAXIMUM_ALLOWED, 
149                                 &domain->sam_handle);
150
151         if (!domain->sam_handle_open) return False;
152
153         /* Open sam domain handle */
154
155         domain->sam_dom_handle_open =
156                 wb_samr_open_domain(&domain->sam_handle, 
157                                     SEC_RIGHTS_MAXIMUM_ALLOWED, 
158                                     &domain->sid, 
159                                     &domain->sam_dom_handle);
160
161         if (!domain->sam_dom_handle_open) return False;
162         
163         return True;
164 }
165
166 static BOOL rpc_hnd_ok(CLI_POLICY_HND *hnd)
167 {
168         return hnd->cli->fd != -1;
169 }
170
171 /* Return true if the SAM domain handles are open and responding.  */
172
173 BOOL domain_handles_open(struct winbindd_domain *domain)
174 {
175         time_t t;
176         BOOL result;
177
178         /* Check we haven't checked too recently */
179
180         t = time(NULL);
181
182         if ((t - domain->last_check) < WINBINDD_ESTABLISH_LOOP) {
183                 return domain->sam_handle_open &&
184                         domain->sam_dom_handle_open;
185         }
186         
187         DEBUG(3, ("checking domain handles for domain %s\n", domain->name));
188         debug_conn_state();
189
190         domain->last_check = t;
191
192         /* Open sam handles if they are marked as closed */
193
194         if (!domain->sam_handle_open || !domain->sam_dom_handle_open) {
195         reopen:
196                 DEBUG(3, ("opening sam handles\n"));
197                 return open_sam_handles(domain);
198         }
199
200         /* Check sam handles are ok - the domain controller may have failed
201            and we need to move to a BDC. */
202
203         if (!rpc_hnd_ok(&domain->sam_handle) || 
204             !rpc_hnd_ok(&domain->sam_dom_handle)) {
205
206                 /* We want to close the current connection but attempt
207                    to open a new set, possibly to a new dc.  If this
208                    doesn't work then return False as we have no dc
209                    to talk to. */
210
211                 DEBUG(3, ("sam handles not responding\n"));
212
213                 winbindd_kill_connections(domain);
214                 goto reopen;
215         }
216
217         result = domain->sam_handle_open && domain->sam_dom_handle_open;
218
219         return result;
220 }
221
222 /* Shut down connections to all domain controllers */
223
224 void winbindd_kill_connections(struct winbindd_domain *domain)
225 {
226         /* Kill all connections */
227
228         if (!domain) {
229                 struct winbindd_domain *tmp;
230
231                 for (tmp = domain_list; tmp; tmp = tmp->next) {
232                         winbindd_kill_connections(domain);
233                 }
234
235                 return;
236         }
237
238         /* Log a level 0 message - this is probably a domain controller
239            failure */
240
241         if (!domain->controller[0])
242                 return;
243
244         DEBUG(0, ("killing connections to domain %s with controller %s\n", 
245                   domain->name, domain->controller));
246
247         debug_conn_state();
248
249         /* Close LSA connections if we are killing connections to the dc
250            that has them open. */
251
252         if (strequal(server_state.controller, domain->controller)) {
253                 server_state.pwdb_initialised = False;
254                 server_state.lsa_handle_open = False;
255                 wb_lsa_close(&server_state.lsa_handle);
256         }
257         
258         /* Close domain sam handles but don't free them as this
259            severely traumatises the getent state.  The connections
260            will be reopened later. */
261
262         if (domain->sam_dom_handle_open) {
263                 wb_samr_close(&domain->sam_dom_handle);
264                 domain->sam_dom_handle_open = False;
265         }
266         
267         if (domain->sam_handle_open) {
268                 wb_samr_close(&domain->sam_handle);
269                 domain->sam_handle_open = False;
270         }
271
272         /* Re-lookup domain info which includes domain controller name */
273         
274         domain->got_domain_info = False;
275 }
276
277 /* Kill connections to all servers */
278
279 void winbindd_kill_all_connections(void)
280 {
281         struct winbindd_domain *domain;
282
283         /* Iterate over domain list */
284
285         domain = domain_list;
286
287         while (domain) {
288                 struct winbindd_domain *next;
289
290                 /* Kill conections */
291
292                 winbindd_kill_connections(domain);
293
294                 /* Remove domain from list */
295
296                 next = domain->next;
297                 DLIST_REMOVE(domain_list, domain);
298                 free(domain);
299
300                 domain = next;
301         }
302 }
303
304 static BOOL get_any_dc_name(char *domain, fstring srv_name)
305 {
306         struct in_addr *ip_list, dc_ip;
307         extern pstring global_myname;
308         int count, i;
309
310         /* Lookup domain controller name */
311                 
312         if (!get_dc_list(False, domain, &ip_list, &count))
313                 return False;
314                 
315         /* Firstly choose a PDC/BDC who has the same network address as any
316            of our interfaces. */
317         
318         for (i = 0; i < count; i++) {
319                 if(!is_local_net(ip_list[i]))
320                         goto got_ip;
321         }
322         
323         i = (sys_random() % count);
324         
325  got_ip:
326         dc_ip = ip_list[i];
327         free(ip_list);
328                 
329         if (!lookup_pdc_name(global_myname, domain, &dc_ip, srv_name))
330                 return False;
331
332         return True;
333 }
334
335 /* Attempt to connect to all domain controllers we know about */
336
337 void establish_connections(BOOL force_reestablish) 
338 {
339         static time_t lastt;
340         time_t t;
341
342         /* Check we haven't checked too recently */
343
344         t = time(NULL);
345         if ((t - lastt < WINBINDD_ESTABLISH_LOOP) && !force_reestablish) {
346                 return;
347         }
348         lastt = t;
349
350         DEBUG(3, ("establishing connections\n"));
351         debug_conn_state();
352
353         /* Maybe the connection died - if so then close up and restart */
354
355         if (server_state.pwdb_initialised &&
356             server_state.lsa_handle_open &&
357             !rpc_hnd_ok(&server_state.lsa_handle)) {
358                 winbindd_kill_connections(NULL);
359         }
360
361         if (!server_state.pwdb_initialised) {
362
363                 /* Lookup domain controller name */
364
365                 if (!get_any_dc_name(lp_workgroup(), 
366                                      server_state.controller)) {
367                         DEBUG(3, ("could not find any domain controllers "
368                                   "for domain %s\n", lp_workgroup()));
369                         return;
370                 }
371
372                 /* Initialise password database and sids */
373                 
374                 /* server_state.pwdb_initialised = pwdb_initialise(False); */
375                 server_state.pwdb_initialised = True;
376
377                 if (!server_state.pwdb_initialised) {
378                         DEBUG(3, ("could not initialise pwdb\n"));
379                         return;
380                 }
381         }
382
383         /* Open lsa handle if it isn't already open */
384         
385         if (!server_state.lsa_handle_open) {
386                 
387                 server_state.lsa_handle_open =
388                         wb_lsa_open_policy(server_state.controller, 
389                                            False, SEC_RIGHTS_MAXIMUM_ALLOWED,
390                                            &server_state.lsa_handle);
391
392                 if (!server_state.lsa_handle_open) {
393                         DEBUG(0, ("error opening lsa handle on dc %s\n",
394                                   server_state.controller));
395                         return;
396                 }
397
398                 /* Now we can talk to the server we can get some info */
399
400                 get_trusted_domains();
401         }
402
403         debug_conn_state();
404 }
405
406 /* Connect to a domain controller using get_any_dc_name() to discover 
407    the domain name and sid */
408
409 BOOL lookup_domain_sid(char *domain_name, struct winbindd_domain *domain)
410 {
411     fstring level5_dom;
412     BOOL res;
413     uint32 enum_ctx = 0;
414     uint32 num_doms = 0;
415     char **domains = NULL;
416     DOM_SID *sids = NULL;
417
418     if (domain == NULL) {
419         return False;
420     }
421
422     DEBUG(1, ("looking up sid for domain %s\n", domain_name));
423
424     /* Get controller name for domain */
425
426     if (!get_any_dc_name(domain_name, domain->controller)) {
427             DEBUG(0, ("Could not resolve domain controller for domain %s\n",
428                       domain_name));
429             return False;
430     }
431
432     /* Do a level 5 query info policy if we are looking up our own SID */
433
434     if (strequal(domain_name, lp_workgroup())) {
435             return wb_lsa_query_info_pol(&server_state.lsa_handle, 0x05, 
436                                          level5_dom, &domain->sid);
437     } 
438
439     /* Use lsaenumdomains to get sid for this domain */
440     
441     res = wb_lsa_enum_trust_dom(&server_state.lsa_handle, &enum_ctx,
442                                 &num_doms, &domains, &sids);
443     
444     /* Look for domain name */
445     
446     if (res && domains && sids) {
447             int found = False;
448             int i;
449             
450             for(i = 0; i < num_doms; i++) {
451                     if (strequal(domain_name, domains[i])) {
452                             sid_copy(&domain->sid, &sids[i]);
453                             found = True;
454                             break;
455                     }
456             }
457             
458             res = found;
459     }
460     
461     return res;
462 }
463
464 /* Lookup domain controller and sid for a domain */
465
466 BOOL get_domain_info(struct winbindd_domain *domain)
467 {
468     fstring sid_str;
469
470     DEBUG(1, ("Getting domain info for domain %s\n", domain->name));
471
472     /* Lookup domain sid */        
473
474     if (!lookup_domain_sid(domain->name, domain)) {
475             DEBUG(0, ("could not find sid for domain %s\n", domain->name));
476
477             /* Could be a DC failure - shut down connections to this domain */
478
479             winbindd_kill_connections(domain);
480
481             return False;
482     }
483     
484     /* Lookup OK */
485
486     domain->got_domain_info = 1;
487
488     sid_to_string(sid_str, &domain->sid);
489     DEBUG(1, ("found sid %s for domain %s\n", sid_str, domain->name));
490
491     return True;
492 }        
493
494 /* Lookup a sid in a domain from a name */
495
496 BOOL winbindd_lookup_sid_by_name(char *name, DOM_SID *sid,
497                                  enum SID_NAME_USE *type)
498 {
499     int num_sids = 0, num_names = 1;
500     DOM_SID *sids = NULL;
501     uint32 *types = NULL;
502     BOOL res;
503
504     /* Don't bother with machine accounts */
505
506     if (name[strlen(name) - 1] == '$') {
507         return False;
508     }
509
510     /* Lookup name */
511
512     res = wb_lsa_lookup_names(&server_state.lsa_handle, num_names, 
513                               (char **)&name, &sids, &types, &num_sids);
514
515     /* Return rid and type if lookup successful */
516
517     if (res) {
518
519         /* Return sid */
520
521         if ((sid != NULL) && (sids != NULL)) {
522             sid_copy(sid, &sids[0]);
523         }
524
525         /* Return name type */
526
527         if ((type != NULL) && (types != NULL)) {
528             *type = types[0];
529         }
530     }
531     
532     return res;
533 }
534
535 /* Lookup a name in a domain from a sid */
536
537 BOOL winbindd_lookup_name_by_sid(DOM_SID *sid, fstring name,
538                                  enum SID_NAME_USE *type)
539 {
540     int num_sids = 1, num_names = 0;
541     uint32 *types = NULL;
542     char **names;
543     BOOL res;
544
545     /* Lookup name */
546
547     res = wb_lsa_lookup_sids(&server_state.lsa_handle, num_sids, sid, 
548                              &names, &types, &num_names);
549
550     /* Return name and type if successful */
551
552     if (res) {
553
554         /* Return name */
555
556         if ((names != NULL) && (name != NULL)) {
557             fstrcpy(name, names[0]);
558         }
559
560         /* Return name type */
561
562         if ((type != NULL) && (types != NULL)) {
563             *type = types[0];
564         }
565     }
566
567     return res;
568 }
569
570 /* Lookup user information from a rid */
571
572 BOOL winbindd_lookup_userinfo(struct winbindd_domain *domain,
573                               uint32 user_rid, SAM_USERINFO_CTR **user_info)
574 {
575         return wb_get_samr_query_userinfo(&domain->sam_dom_handle, 0x15, 
576                                           user_rid, user_info);
577 }                                   
578
579 /* Lookup groups a user is a member of.  I wish Unix had a call like this! */
580
581 BOOL winbindd_lookup_usergroups(struct winbindd_domain *domain,
582                                 uint32 user_rid, uint32 *num_groups,
583                                 DOM_GID **user_groups)
584 {
585         POLICY_HND user_pol;
586         BOOL result;
587
588         if (!wb_samr_open_user(&domain->sam_dom_handle, 
589                                SEC_RIGHTS_MAXIMUM_ALLOWED,
590                                user_rid, &user_pol)) {
591                 return False;
592         }
593
594         if (cli_samr_query_usergroups(domain->sam_dom_handle.cli,
595                                       domain->sam_dom_handle.mem_ctx,
596                                       &user_pol, num_groups, user_groups)
597             != NT_STATUS_OK) {
598                 result = False;
599                 goto done;
600         }
601
602         result = True;
603
604 done:
605         cli_samr_close(domain->sam_dom_handle.cli,
606                        domain->sam_dom_handle.mem_ctx, &user_pol);
607
608         return True;
609 }
610
611 /* Lookup group information from a rid */
612
613 BOOL winbindd_lookup_groupinfo(struct winbindd_domain *domain,
614                               uint32 group_rid, GROUP_INFO_CTR *info)
615 {
616         return wb_get_samr_query_groupinfo(&domain->sam_dom_handle, 1, 
617                                            group_rid, info);
618 }
619
620 /* Lookup group membership given a rid */
621
622 BOOL winbindd_lookup_groupmem(struct winbindd_domain *domain,
623                               uint32 group_rid, uint32 *num_names, 
624                               uint32 **rid_mem, char ***names, 
625                               enum SID_NAME_USE **name_types)
626 {
627         return wb_sam_query_groupmem(&domain->sam_dom_handle, group_rid, 
628                                      num_names, rid_mem, names, name_types);
629 }
630
631 /* Globals for domain list stuff */
632
633 struct winbindd_domain *domain_list = NULL;
634
635 /* Given a domain name, return the struct winbindd domain info for it 
636    if it is actually working. */
637
638 struct winbindd_domain *find_domain_from_name(char *domain_name)
639 {
640         struct winbindd_domain *tmp;
641
642         /* Search through list */
643
644         for (tmp = domain_list; tmp != NULL; tmp = tmp->next) {
645                 if (strcmp(domain_name, tmp->name) == 0) {
646
647                         if (!tmp->got_domain_info) {
648                                 get_domain_info(tmp);
649                         }
650
651                         return tmp->got_domain_info ? tmp : NULL;
652                 }
653         }
654
655         /* Not found */
656
657         return NULL;
658 }
659
660 /* Given a domain name, return the struct winbindd domain info for it */
661
662 struct winbindd_domain *find_domain_from_sid(DOM_SID *sid)
663 {
664         struct winbindd_domain *tmp;
665
666         /* Search through list */
667         for (tmp = domain_list; tmp != NULL; tmp = tmp->next) {
668                 if (sid_equal(sid, &tmp->sid)) {
669                         if (!tmp->got_domain_info) return NULL;
670                         return tmp;
671                 }
672         }
673
674         /* Not found */
675         return NULL;
676 }
677
678 /* Free state information held for {set,get,end}{pw,gr}ent() functions */
679
680 void free_getent_state(struct getent_state *state)
681 {
682     struct getent_state *temp;
683
684     /* Iterate over state list */
685
686     temp = state;
687
688     while(temp != NULL) {
689         struct getent_state *next;
690
691         /* Free sam entries then list entry */
692
693         safe_free(state->sam_entries);
694         DLIST_REMOVE(state, state);
695         next = temp->next;
696
697         free(temp);
698         temp = next;
699     }
700 }
701
702 /* Parse list of arguments to winbind uid or winbind gid parameters */
703
704 static BOOL parse_id_list(char *paramstr, BOOL is_user)
705 {
706     uid_t id_low, id_high = 0;
707
708     /* Give a nicer error message if no parameters specified */
709
710     if (strequal(paramstr, "")) {
711         DEBUG(0, ("winbind %s parameter missing\n", is_user ? "uid" : "gid"));
712         return False;
713     }
714     
715     /* Parse entry */
716
717     if (sscanf(paramstr, "%u-%u", &id_low, &id_high) != 2) {
718         DEBUG(0, ("winbind %s parameter invalid\n", 
719                   is_user ? "uid" : "gid"));
720         return False;
721     }
722     
723     /* Store id info */
724     
725     if (is_user) {
726         server_state.uid_low = id_low;
727         server_state.uid_high = id_high;
728     } else {
729         server_state.gid_low = id_low;
730         server_state.gid_high = id_high;
731     }
732
733     return True;
734 }
735
736 /* Initialise trusted domain info */
737
738 BOOL winbindd_param_init(void)
739 {
740     /* Parse winbind uid and winbind_gid parameters */
741
742     if (!(parse_id_list(lp_winbind_uid(), True) &&
743           parse_id_list(lp_winbind_gid(), False))) {
744         return False;
745     }
746
747     /* Check for reversed uid and gid ranges */
748         
749     if (server_state.uid_low > server_state.uid_high) {
750         DEBUG(0, ("uid range invalid\n"));
751         return False;
752     }
753     
754     if (server_state.gid_low > server_state.gid_high) {
755         DEBUG(0, ("gid range invalid\n"));
756         return False;
757     }
758     
759     return True;
760 }
761
762 /* Convert a enum winbindd_cmd to a string */
763
764 struct cmdstr_table {
765         enum winbindd_cmd cmd;
766         char *desc;
767 };
768
769 static struct cmdstr_table cmdstr_table[] = {
770         
771         /* User functions */
772
773         { WINBINDD_GETPWNAM_FROM_USER, "getpwnam from user" },
774         { WINBINDD_GETPWNAM_FROM_UID, "getpwnam from uid" },
775         { WINBINDD_SETPWENT, "setpwent" },
776         { WINBINDD_ENDPWENT, "endpwent" },
777         { WINBINDD_GETPWENT, "getpwent" },
778         { WINBINDD_GETGROUPS, "getgroups" },
779
780         /* Group functions */
781
782         { WINBINDD_GETGRNAM_FROM_GROUP, "getgrnam from group" },
783         { WINBINDD_GETGRNAM_FROM_GID, "getgrnam from gid" },
784         { WINBINDD_SETGRENT, "setgrent" },
785         { WINBINDD_ENDGRENT, "endgrent" },
786         { WINBINDD_GETGRENT, "getgrent" },
787
788         /* PAM auth functions */
789
790         { WINBINDD_PAM_AUTH, "pam auth" },
791         { WINBINDD_PAM_AUTH_CRAP, "pam auth crap" },
792         { WINBINDD_PAM_CHAUTHTOK, "pam chauthtok" },
793
794         /* List things */
795
796         { WINBINDD_LIST_USERS, "list users" },
797         { WINBINDD_LIST_GROUPS, "list groups" },
798         { WINBINDD_LIST_TRUSTDOM, "list trusted domains" },
799
800         /* SID related functions */
801
802         { WINBINDD_LOOKUPSID, "lookup sid" },
803         { WINBINDD_LOOKUPNAME, "lookup name" },
804
805         /* S*RS related functions */
806
807         { WINBINDD_SID_TO_UID, "sid to uid" },
808         { WINBINDD_SID_TO_GID, "sid to gid " },
809         { WINBINDD_GID_TO_SID, "gid to sid" },
810         { WINBINDD_UID_TO_SID, "uid to sid" },
811
812         /* Miscellaneous other stuff */
813
814         { WINBINDD_CHECK_MACHACC, "check machine acct pw" },
815
816         /* End of list */
817
818         { WINBINDD_NUM_CMDS, NULL }
819 };
820
821 char *winbindd_cmd_to_string(enum winbindd_cmd cmd)
822 {
823         struct cmdstr_table *table = cmdstr_table;
824         char *result = NULL;
825
826         for(table = cmdstr_table; table->desc; table++) {
827                 if (cmd == table->cmd) {
828                         result = table->desc;
829                         break;
830                 }
831         }
832         
833         if (result == NULL) {
834                 result = "invalid command";
835         }
836
837         return result;
838 };
839
840 /* find the sequence number for a domain */
841
842 uint32 domain_sequence_number(char *domain_name)
843 {
844         struct winbindd_domain *domain;
845         SAM_UNK_CTR ctr;
846
847         domain = find_domain_from_name(domain_name);
848         if (!domain) return DOM_SEQUENCE_NONE;
849
850         if (!wb_samr_query_dom_info(&domain->sam_dom_handle, 2, &ctr)) {
851
852                 /* If this fails, something bad has gone wrong */
853
854                 winbindd_kill_connections(domain);
855
856                 DEBUG(2,("domain sequence query failed\n"));
857                 return DOM_SEQUENCE_NONE;
858         }
859
860         DEBUG(4,("got domain sequence number for %s of %u\n", 
861                  domain_name, (unsigned)ctr.info.inf2.seq_num));
862         
863         return ctr.info.inf2.seq_num;
864 }
865
866 /* Query display info for a domain.  This returns enough information plus a
867    bit extra to give an overview of domain users for the User Manager
868    application. */
869
870 uint32 winbindd_query_dispinfo(struct winbindd_domain *domain,
871                              uint32 *start_ndx, uint16 info_level, 
872                              uint32 *num_entries, SAM_DISPINFO_CTR *ctr)
873 {
874         NTSTATUS status;
875
876         status = wb_samr_query_dispinfo(&domain->sam_dom_handle, start_ndx,
877                                         info_level, num_entries, ctr);
878
879         return status;
880 }
881
882 /* Check if a domain is present in a comma-separated list of domains */
883
884 BOOL check_domain_env(char *domain_env, char *domain)
885 {
886         fstring name;
887         char *tmp = domain_env;
888
889         while(next_token(&tmp, name, ",", sizeof(fstring))) {
890                 if (strequal(name, domain)) {
891                         return True;
892                 }
893         }
894
895         return False;
896 }
897
898
899 /* Parse a string of the form DOMAIN/user into a domain and a user */
900
901 void parse_domain_user(char *domuser, fstring domain, fstring user)
902 {
903         char *p;
904         char *sep = lp_winbind_separator();
905         if (!sep) sep = "\\";
906         p = strchr(domuser,*sep);
907         if (!p) p = strchr(domuser,'\\');
908         if (!p) {
909                 fstrcpy(domain,"");
910                 fstrcpy(user, domuser);
911                 return;
912         }
913         
914         fstrcpy(user, p+1);
915         fstrcpy(domain, domuser);
916         domain[PTR_DIFF(p, domuser)] = 0;
917         strupper(domain);
918 }