Removed version number from file header.
[samba.git] / source3 / nsswitch / winbind_nss.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    Windows NT Domain nsswitch module
5
6    Copyright (C) Tim Potter 2000
7    
8    This library is free software; you can redistribute it and/or
9    modify it under the terms of the GNU Library General Public
10    License as published by the Free Software Foundation; either
11    version 2 of the License, or (at your option) any later version.
12    
13    This library is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16    Library General Public License for more details.
17    
18    You should have received a copy of the GNU Library General Public
19    License along with this library; if not, write to the
20    Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21    Boston, MA  02111-1307, USA.   
22 */
23
24 #include "winbind_nss_config.h"
25 #include "winbindd_nss.h"
26
27 #ifdef HAVE_NS_API_H
28 #undef VOLATILE
29
30 #include <ns_daemon.h>
31 #endif
32
33 #define MAX_GETPWENT_USERS 250
34 #define MAX_GETGRENT_USERS 250
35
36 /* Prototypes from wb_common.c */
37
38 extern int winbindd_fd;
39
40 void init_request(struct winbindd_request *req,int rq_type);
41 NSS_STATUS winbindd_send_request(int req_type,
42                                  struct winbindd_request *request);
43 NSS_STATUS winbindd_get_response(struct winbindd_response *response);
44 NSS_STATUS winbindd_request(int req_type, 
45                                  struct winbindd_request *request,
46                                  struct winbindd_response *response);
47 int winbind_open_pipe_sock(void);
48 int write_sock(void *buffer, int count);
49 int read_reply(struct winbindd_response *response);
50 void free_response(struct winbindd_response *response);
51
52 #ifdef HAVE_NS_API_H
53 /* IRIX version */
54
55 static int send_next_request(nsd_file_t *, struct winbindd_request *);
56 static int do_list(int state, nsd_file_t *rq);
57
58 static nsd_file_t *current_rq = NULL;
59 static int current_winbind_xid = 0;
60 static int next_winbind_xid = 0;
61
62 typedef struct winbind_xid {
63         int                     xid;
64         nsd_file_t              *rq;
65         struct winbindd_request *request;
66         struct winbind_xid      *next;
67 } winbind_xid_t;
68
69 static winbind_xid_t *winbind_xids = (winbind_xid_t *)0;
70
71 static int
72 winbind_xid_new(int xid, nsd_file_t *rq, struct winbindd_request *request)
73 {
74         winbind_xid_t *new;
75
76         nsd_logprintf(NSD_LOG_LOW,
77                 "entering winbind_xid_new xid = %d rq = 0x%x, request = 0x%x\n",
78                 xid, rq, request);
79         new = (winbind_xid_t *)nsd_calloc(1,sizeof(winbind_xid_t));
80         if (!new) {
81                 nsd_logprintf(NSD_LOG_RESOURCE,"winbind_xid_new: failed malloc\n");
82                 return NSD_ERROR;
83         }
84
85         new->xid = xid;
86         new->rq = rq;
87         new->request = request;
88         new->next = winbind_xids;
89         winbind_xids = new;
90
91         return NSD_CONTINUE;
92 }
93
94 /*
95 ** This routine will look down the xid list and return the request
96 ** associated with an xid.  We remove the record if it is found.
97 */
98 nsd_file_t *
99 winbind_xid_lookup(int xid, struct winbindd_request **requestp)
100 {
101         winbind_xid_t **last, *dx;
102         nsd_file_t *result=0;
103
104         for (last = &winbind_xids, dx = winbind_xids; dx && (dx->xid != xid);
105             last = &dx->next, dx = dx->next);
106         if (dx) {
107                 *last = dx->next;
108                 result = dx->rq;
109                 *requestp = dx->request;
110                 free(dx);
111         }
112         nsd_logprintf(NSD_LOG_LOW,
113                 "entering winbind_xid_lookup xid = %d rq = 0x%x, request = 0x%x\n",
114                 xid, result, dx->request);
115
116         return result;
117 }
118
119 static int
120 winbind_startnext_timeout(nsd_file_t **rqp, nsd_times_t *to)
121 {
122         nsd_file_t *rq;
123         struct winbindd_request *request;
124
125         nsd_logprintf(NSD_LOG_MIN, "timeout (winbind startnext)\n");
126         rq = to->t_file;
127         *rqp = rq;
128         nsd_timeout_remove(rq);
129         request = to->t_clientdata;
130         return(send_next_request(rq, request));
131 }
132
133 static void
134 dequeue_request()
135 {
136         nsd_file_t *rq;
137         struct winbindd_request *request;
138
139         /*
140          * Check for queued requests
141          */
142         if (winbind_xids) {
143             nsd_logprintf(NSD_LOG_MIN, "timeout (winbind) unqueue xid %d\n",
144                         current_winbind_xid);
145             rq = winbind_xid_lookup(current_winbind_xid++, &request);
146             /* cause a timeout on the queued request so we can send it */
147             nsd_timeout_new(rq,1,winbind_startnext_timeout,request);
148         }
149 }
150
151 static int
152 do_request(nsd_file_t *rq, struct winbindd_request *request)
153 {
154         if (winbind_xids == NULL) {
155                 /*
156                  * No outstanding requests.
157                  * Send off the request to winbindd
158                  */
159                 nsd_logprintf(NSD_LOG_MIN, "lookup (winbind) sending request\n");
160                 return(send_next_request(rq, request));
161         } else {
162                 /*
163                  * Just queue it up for now - previous callout or timout
164                  * will start it up
165                  */
166                 nsd_logprintf(NSD_LOG_MIN,
167                         "lookup (winbind): queue request xid = %d\n",
168                         next_winbind_xid);
169                 return(winbind_xid_new(next_winbind_xid++, rq, request));
170         }
171 }
172
173 static int 
174 winbind_callback(nsd_file_t **rqp, int fd)
175 {
176         struct winbindd_response response;
177         struct winbindd_pw *pw = &response.data.pw;
178         struct winbindd_gr *gr = &response.data.gr;
179         nsd_file_t *rq;
180         NSS_STATUS status;
181         char result[1024];
182         char *members;
183         int i;
184
185         dequeue_request();
186
187         nsd_logprintf(NSD_LOG_MIN, "entering callback (winbind)\n");
188
189         rq = current_rq;
190         *rqp = rq;
191
192         nsd_timeout_remove(rq);
193         nsd_callback_remove(fd);
194
195         ZERO_STRUCT(response);
196         status = winbindd_get_response(&response);
197
198         if (status != NSS_STATUS_SUCCESS) {
199                 /* free any extra data area in response structure */
200                 free_response(&response);
201                 nsd_logprintf(NSD_LOG_MIN, 
202                         "callback (winbind) returning not found, status = %d\n",
203                         status);
204                 rq->f_status = NS_NOTFOUND;
205                 return NSD_NEXT;
206         }
207         switch ((int)rq->f_cmd_data) {
208             case WINBINDD_GETPWUID:
209             case WINBINDD_GETPWNAM:
210                 snprintf(result,1023,"%s:%s:%d:%d:%s:%s:%s\n",
211                         pw->pw_name,
212                         pw->pw_passwd,
213                         pw->pw_uid,
214                         pw->pw_gid,
215                         pw->pw_gecos,
216                         pw->pw_dir,
217                         pw->pw_shell);
218                 break;
219             case WINBINDD_GETGRNAM:
220             case WINBINDD_GETGRGID:
221                 if (gr->num_gr_mem && response.extra_data)
222                         members = response.extra_data;
223                 else
224                         members = "";
225                 snprintf(result,1023,"%s:%s:%d:%s\n",
226                         gr->gr_name, gr->gr_passwd, gr->gr_gid, members);
227                 break;
228             case WINBINDD_SETGRENT:
229             case WINBINDD_SETPWENT:
230                 nsd_logprintf(NSD_LOG_MIN, "callback (winbind) - SETPWENT/SETGRENT\n");
231                 free_response(&response);
232                 return(do_list(1,rq));
233             case WINBINDD_GETGRENT:
234                 nsd_logprintf(NSD_LOG_MIN, 
235                         "callback (winbind) - %d GETGRENT responses\n",
236                         response.data.num_entries);
237                 if (response.data.num_entries) {
238                     gr = (struct winbindd_gr *)response.extra_data;
239                     if (! gr ) {
240                         nsd_logprintf(NSD_LOG_MIN, "     no extra_data\n");
241                         free_response(&response);
242                         return NSD_ERROR;
243                     }
244                     members = (char *)response.extra_data + 
245                                 (response.data.num_entries * sizeof(struct winbindd_gr));
246                     for (i = 0; i < response.data.num_entries; i++) {
247                         snprintf(result,1023,"%s:%s:%d:%s\n",
248                                 gr->gr_name, gr->gr_passwd, gr->gr_gid, 
249                                 &members[gr->gr_mem_ofs]);
250                         nsd_logprintf(NSD_LOG_MIN, "     GETGRENT %s\n",result);
251                         nsd_append_element(rq,NS_SUCCESS,result,strlen(result));
252                         gr++;
253                     }
254                 }
255                 i = response.data.num_entries;
256                 free_response(&response);
257                 if (i < MAX_GETPWENT_USERS)
258                     return(do_list(2,rq));
259                 else
260                     return(do_list(1,rq));
261             case WINBINDD_GETPWENT:
262                 nsd_logprintf(NSD_LOG_MIN, 
263                         "callback (winbind) - %d GETPWENT responses\n",
264                         response.data.num_entries);
265                 if (response.data.num_entries) {
266                     pw = (struct winbindd_pw *)response.extra_data;
267                     if (! pw ) {
268                         nsd_logprintf(NSD_LOG_MIN, "     no extra_data\n");
269                         free_response(&response);
270                         return NSD_ERROR;
271                     }
272                     for (i = 0; i < response.data.num_entries; i++) {
273                         snprintf(result,1023,"%s:%s:%d:%d:%s:%s:%s",
274                                 pw->pw_name,
275                                 pw->pw_passwd,
276                                 pw->pw_uid,
277                                 pw->pw_gid,
278                                 pw->pw_gecos,
279                                 pw->pw_dir,
280                                 pw->pw_shell);
281                         nsd_logprintf(NSD_LOG_MIN, "     GETPWENT %s\n",result);
282                         nsd_append_element(rq,NS_SUCCESS,result,strlen(result));
283                         pw++;
284                     }
285                 }
286                 i = response.data.num_entries;
287                 free_response(&response);
288                 if (i < MAX_GETPWENT_USERS)
289                     return(do_list(2,rq));
290                 else
291                     return(do_list(1,rq));
292             case WINBINDD_ENDGRENT:
293             case WINBINDD_ENDPWENT:
294                 nsd_logprintf(NSD_LOG_MIN, "callback (winbind) - ENDPWENT/ENDGRENT\n");
295                 nsd_append_element(rq,NS_SUCCESS,"\n",1);
296                 free_response(&response);
297                 return NSD_NEXT;
298             default:
299                 free_response(&response);
300                 nsd_logprintf(NSD_LOG_MIN, "callback (winbind) - no valid command\n");
301                 return NSD_NEXT;
302         }
303         nsd_logprintf(NSD_LOG_MIN, "callback (winbind) %s\n", result);
304         /* free any extra data area in response structure */
305         free_response(&response);
306         nsd_set_result(rq,NS_SUCCESS,result,strlen(result),VOLATILE);
307         return NSD_OK;
308 }
309
310 static int 
311 winbind_timeout(nsd_file_t **rqp, nsd_times_t *to)
312 {
313         nsd_file_t *rq;
314
315         dequeue_request();
316
317         nsd_logprintf(NSD_LOG_MIN, "timeout (winbind)\n");
318
319         rq = to->t_file;
320         *rqp = rq;
321
322         /* Remove the callback and timeout */
323         nsd_callback_remove(winbindd_fd);
324         nsd_timeout_remove(rq);
325
326         rq->f_status = NS_NOTFOUND;
327         return NSD_NEXT;
328 }
329
330 static int
331 send_next_request(nsd_file_t *rq, struct winbindd_request *request)
332 {
333         NSS_STATUS status;
334         long timeout;
335
336         timeout = 1000;
337
338         nsd_logprintf(NSD_LOG_MIN, "send_next_request (winbind) %d to = %d\n",
339                         rq->f_cmd_data, timeout);
340         status = winbindd_send_request((int)rq->f_cmd_data,request);
341         free(request);
342
343         if (status != NSS_STATUS_SUCCESS) {
344                 nsd_logprintf(NSD_LOG_MIN, 
345                         "send_next_request (winbind) error status = %d\n",status);
346                 rq->f_status = status;
347                 return NSD_NEXT;
348         }
349
350         current_rq = rq;
351
352         /*
353          * Set up callback and timeouts
354          */
355         nsd_logprintf(NSD_LOG_MIN, "send_next_request (winbind) fd = %d\n",winbindd_fd);
356         nsd_callback_new(winbindd_fd,winbind_callback,NSD_READ);
357         nsd_timeout_new(rq,timeout,winbind_timeout,(void *)0);
358         return NSD_CONTINUE;
359 }
360
361 int init(void)
362 {
363         nsd_logprintf(NSD_LOG_MIN, "entering init (winbind)\n");
364         return(NSD_OK);
365 }
366
367 int lookup(nsd_file_t *rq)
368 {
369         char *map;
370         char *key;
371         struct winbindd_request *request;
372
373         nsd_logprintf(NSD_LOG_MIN, "entering lookup (winbind)\n");
374         if (! rq)
375                 return NSD_ERROR;
376
377         map = nsd_attr_fetch_string(rq->f_attrs, "table", (char*)0);
378         key = nsd_attr_fetch_string(rq->f_attrs, "key", (char*)0);
379         if (! map || ! key) {
380                 nsd_logprintf(NSD_LOG_MIN, "lookup (winbind) table or key not defined\n");
381                 rq->f_status = NS_BADREQ;
382                 return NSD_ERROR;
383         }
384
385         nsd_logprintf(NSD_LOG_MIN, "lookup (winbind %s)\n",map);
386
387         request = (struct winbindd_request *)nsd_calloc(1,sizeof(struct winbindd_request));
388         if (! request) {
389                 nsd_logprintf(NSD_LOG_RESOURCE,
390                         "lookup (winbind): failed malloc\n");
391                 return NSD_ERROR;
392         }
393
394         if (strcasecmp(map,"passwd.byuid") == 0) {
395             request->data.uid = atoi(key);
396             rq->f_cmd_data = (void *)WINBINDD_GETPWUID;
397         } else if (strcasecmp(map,"passwd.byname") == 0) {
398             strncpy(request->data.username, key, 
399                 sizeof(request->data.username) - 1);
400             request->data.username[sizeof(request->data.username) - 1] = '\0';
401             rq->f_cmd_data = (void *)WINBINDD_GETPWNAM; 
402         } else if (strcasecmp(map,"group.byname") == 0) {
403             strncpy(request->data.groupname, key, 
404                 sizeof(request->data.groupname) - 1);
405             request->data.groupname[sizeof(request->data.groupname) - 1] = '\0';
406             rq->f_cmd_data = (void *)WINBINDD_GETGRNAM; 
407         } else if (strcasecmp(map,"group.bygid") == 0) {
408             request->data.gid = atoi(key);
409             rq->f_cmd_data = (void *)WINBINDD_GETGRGID;
410         } else {
411                 /*
412                  * Don't understand this map - just return not found
413                  */
414                 nsd_logprintf(NSD_LOG_MIN, "lookup (winbind) unknown table\n");
415                 free(request);
416                 rq->f_status = NS_NOTFOUND;
417                 return NSD_NEXT;
418         }
419
420         return(do_request(rq, request));
421 }
422
423 int list(nsd_file_t *rq)
424 {
425         char *map;
426
427         nsd_logprintf(NSD_LOG_MIN, "entering list (winbind)\n");
428         if (! rq)
429                 return NSD_ERROR;
430
431         map = nsd_attr_fetch_string(rq->f_attrs, "table", (char*)0);
432         if (! map ) {
433                 nsd_logprintf(NSD_LOG_MIN, "list (winbind) table not defined\n");
434                 rq->f_status = NS_BADREQ;
435                 return NSD_ERROR;
436         }
437
438         nsd_logprintf(NSD_LOG_MIN, "list (winbind %s)\n",map);
439
440         return (do_list(0,rq));
441 }
442
443 static int
444 do_list(int state, nsd_file_t *rq)
445 {
446         char *map;
447         struct winbindd_request *request;
448
449         nsd_logprintf(NSD_LOG_MIN, "entering do_list (winbind) state = %d\n",state);
450
451         map = nsd_attr_fetch_string(rq->f_attrs, "table", (char*)0);
452         request = (struct winbindd_request *)nsd_calloc(1,sizeof(struct winbindd_request));
453         if (! request) {
454                 nsd_logprintf(NSD_LOG_RESOURCE,
455                         "do_list (winbind): failed malloc\n");
456                 return NSD_ERROR;
457         }
458
459         if (strcasecmp(map,"passwd.byname") == 0) {
460             switch (state) {
461                 case 0:
462                     rq->f_cmd_data = (void *)WINBINDD_SETPWENT;
463                     break;
464                 case 1:
465                     request->data.num_entries = MAX_GETPWENT_USERS;
466                     rq->f_cmd_data = (void *)WINBINDD_GETPWENT;
467                     break;
468                 case 2:
469                     rq->f_cmd_data = (void *)WINBINDD_ENDPWENT;
470                     break;
471                 default:
472                     nsd_logprintf(NSD_LOG_MIN, "do_list (winbind) unknown state\n");
473                     free(request);
474                     rq->f_status = NS_NOTFOUND;
475                     return NSD_NEXT;
476             }
477         } else if (strcasecmp(map,"group.byname") == 0) {
478             switch (state) {
479                 case 0:
480                     rq->f_cmd_data = (void *)WINBINDD_SETGRENT;
481                     break;
482                 case 1:
483                     request->data.num_entries = MAX_GETGRENT_USERS;
484                     rq->f_cmd_data = (void *)WINBINDD_GETGRENT;
485                     break;
486                 case 2:
487                     rq->f_cmd_data = (void *)WINBINDD_ENDGRENT;
488                     break;
489                 default:
490                     nsd_logprintf(NSD_LOG_MIN, "do_list (winbind) unknown state\n");
491                     free(request);
492                     rq->f_status = NS_NOTFOUND;
493                     return NSD_NEXT;
494             }
495         } else {
496                 /*
497                  * Don't understand this map - just return not found
498                  */
499                 nsd_logprintf(NSD_LOG_MIN, "do_list (winbind) unknown table\n");
500                 free(request);
501                 rq->f_status = NS_NOTFOUND;
502                 return NSD_NEXT;
503         }
504
505         return(do_request(rq, request));
506 }
507
508 #else
509
510 /* Allocate some space from the nss static buffer.  The buffer and buflen
511    are the pointers passed in by the C library to the _nss_ntdom_*
512    functions. */
513
514 static char *get_static(char **buffer, int *buflen, int len)
515 {
516         char *result;
517
518         /* Error check.  We return false if things aren't set up right, or
519            there isn't enough buffer space left. */
520         
521         if ((buffer == NULL) || (buflen == NULL) || (*buflen < len)) {
522                 return NULL;
523         }
524
525         /* Return an index into the static buffer */
526
527         result = *buffer;
528         *buffer += len;
529         *buflen -= len;
530
531         return result;
532 }
533
534 /* I've copied the strtok() replacement function next_token() from
535    lib/util_str.c as I really don't want to have to link in any other
536    objects if I can possibly avoid it. */
537
538 BOOL next_token(char **ptr,char *buff,char *sep, size_t bufsize)
539 {
540         char *s;
541         BOOL quoted;
542         size_t len=1;
543
544         if (!ptr) return(False);
545
546         s = *ptr;
547
548         /* default to simple separators */
549         if (!sep) sep = " \t\n\r";
550
551         /* find the first non sep char */
552         while (*s && strchr(sep,*s)) s++;
553         
554         /* nothing left? */
555         if (! *s) return(False);
556         
557         /* copy over the token */
558         for (quoted = False; len < bufsize && *s && (quoted || !strchr(sep,*s)); s++) {
559                 if (*s == '\"') {
560                         quoted = !quoted;
561                 } else {
562                         len++;
563                         *buff++ = *s;
564                 }
565         }
566         
567         *ptr = (*s) ? s+1 : s;  
568         *buff = 0;
569         
570         return(True);
571 }
572
573
574 /* Fill a pwent structure from a winbindd_response structure.  We use
575    the static data passed to us by libc to put strings and stuff in.
576    Return NSS_STATUS_TRYAGAIN if we run out of memory. */
577
578 static NSS_STATUS fill_pwent(struct passwd *result,
579                                   struct winbindd_pw *pw,
580                                   char **buffer, int *buflen)
581 {
582         /* User name */
583
584         if ((result->pw_name = 
585              get_static(buffer, buflen, strlen(pw->pw_name) + 1)) == NULL) {
586
587                 /* Out of memory */
588
589                 return NSS_STATUS_TRYAGAIN;
590         }
591
592         strcpy(result->pw_name, pw->pw_name);
593
594         /* Password */
595
596         if ((result->pw_passwd = 
597              get_static(buffer, buflen, strlen(pw->pw_passwd) + 1)) == NULL) {
598
599                 /* Out of memory */
600
601                 return NSS_STATUS_TRYAGAIN;
602         }
603
604         strcpy(result->pw_passwd, pw->pw_passwd);
605         
606         /* [ug]id */
607
608         result->pw_uid = pw->pw_uid;
609         result->pw_gid = pw->pw_gid;
610
611         /* GECOS */
612
613         if ((result->pw_gecos = 
614              get_static(buffer, buflen, strlen(pw->pw_gecos) + 1)) == NULL) {
615
616                 /* Out of memory */
617
618                 return NSS_STATUS_TRYAGAIN;
619         }
620
621         strcpy(result->pw_gecos, pw->pw_gecos);
622         
623         /* Home directory */
624         
625         if ((result->pw_dir = 
626              get_static(buffer, buflen, strlen(pw->pw_dir) + 1)) == NULL) {
627
628                 /* Out of memory */
629
630                 return NSS_STATUS_TRYAGAIN;
631         }
632
633         strcpy(result->pw_dir, pw->pw_dir);
634
635         /* Logon shell */
636         
637         if ((result->pw_shell = 
638              get_static(buffer, buflen, strlen(pw->pw_shell) + 1)) == NULL) {
639                 
640                 /* Out of memory */
641
642                 return NSS_STATUS_TRYAGAIN;
643         }
644
645         strcpy(result->pw_shell, pw->pw_shell);
646
647         return NSS_STATUS_SUCCESS;
648 }
649
650 /* Fill a grent structure from a winbindd_response structure.  We use
651    the static data passed to us by libc to put strings and stuff in.
652    Return NSS_STATUS_TRYAGAIN if we run out of memory. */
653
654 static int fill_grent(struct group *result, struct winbindd_gr *gr,
655                       char *gr_mem, char **buffer, int *buflen)
656 {
657         fstring name;
658         int i;
659         char *tst;
660
661         /* Group name */
662
663         if ((result->gr_name =
664              get_static(buffer, buflen, strlen(gr->gr_name) + 1)) == NULL) {
665
666                 /* Out of memory */
667
668                 return NSS_STATUS_TRYAGAIN;
669         }
670
671         strcpy(result->gr_name, gr->gr_name);
672
673         /* Password */
674
675         if ((result->gr_passwd =
676              get_static(buffer, buflen, strlen(gr->gr_passwd) + 1)) == NULL) {
677
678                 /* Out of memory */
679                 
680                 return NSS_STATUS_TRYAGAIN;
681         }
682
683         strcpy(result->gr_passwd, gr->gr_passwd);
684
685         /* gid */
686
687         result->gr_gid = gr->gr_gid;
688
689         /* Group membership */
690
691         if ((gr->num_gr_mem < 0) || !gr_mem) {
692                 gr->num_gr_mem = 0;
693         }
694
695         /* this next value is a pointer to a pointer so let's align it */
696
697         /* Calculate number of extra bytes needed to align on pointer size boundry */
698         if ((i = (int)*buffer % sizeof(char*)) != 0)
699                 i = sizeof(char*) - i;
700         
701         if ((tst = get_static(buffer, buflen, ((gr->num_gr_mem + 1) * 
702                                  sizeof(char *)+i))) == NULL) {
703
704                 /* Out of memory */
705
706                 return NSS_STATUS_TRYAGAIN;
707         }
708         result->gr_mem = (char **)(tst + i);
709
710         if (gr->num_gr_mem == 0) {
711
712                 /* Group is empty */
713
714                 *(result->gr_mem) = NULL;
715                 return NSS_STATUS_SUCCESS;
716         }
717
718         /* Start looking at extra data */
719
720         i = 0;
721
722         while(next_token((char **)&gr_mem, name, ",", sizeof(fstring))) {
723         
724                 /* Allocate space for member */
725         
726                 if (((result->gr_mem)[i] = 
727                      get_static(buffer, buflen, strlen(name) + 1)) == NULL) {
728             
729                         /* Out of memory */
730             
731                         return NSS_STATUS_TRYAGAIN;
732                 }        
733         
734                 strcpy((result->gr_mem)[i], name);
735                 i++;
736         }
737
738         /* Terminate list */
739
740         (result->gr_mem)[i] = NULL;
741
742         return NSS_STATUS_SUCCESS;
743 }
744
745 /*
746  * NSS user functions
747  */
748
749 static struct winbindd_response getpwent_response;
750
751 static int ndx_pw_cache;                 /* Current index into pwd cache */
752 static int num_pw_cache;                 /* Current size of pwd cache */
753
754 /* Rewind "file pointer" to start of ntdom password database */
755
756 NSS_STATUS
757 _nss_winbind_setpwent(void)
758 {
759 #ifdef DEBUG_NSS
760         fprintf(stderr, "[%5d]: setpwent\n", getpid());
761 #endif
762
763         if (num_pw_cache > 0) {
764                 ndx_pw_cache = num_pw_cache = 0;
765                 free_response(&getpwent_response);
766         }
767
768         return winbindd_request(WINBINDD_SETPWENT, NULL, NULL);
769 }
770
771 /* Close ntdom password database "file pointer" */
772
773 NSS_STATUS
774 _nss_winbind_endpwent(void)
775 {
776 #ifdef DEBUG_NSS
777         fprintf(stderr, "[%5d]: endpwent\n", getpid());
778 #endif
779
780         if (num_pw_cache > 0) {
781                 ndx_pw_cache = num_pw_cache = 0;
782                 free_response(&getpwent_response);
783         }
784
785         return winbindd_request(WINBINDD_ENDPWENT, NULL, NULL);
786 }
787
788 /* Fetch the next password entry from ntdom password database */
789
790 NSS_STATUS
791 _nss_winbind_getpwent_r(struct passwd *result, char *buffer, 
792                         size_t buflen, int *errnop)
793 {
794         NSS_STATUS ret;
795         struct winbindd_request request;
796         static int called_again;
797
798 #ifdef DEBUG_NSS
799         fprintf(stderr, "[%5d]: getpwent\n", getpid());
800 #endif
801
802         /* Return an entry from the cache if we have one, or if we are
803            called again because we exceeded our static buffer.  */
804
805         if ((ndx_pw_cache < num_pw_cache) || called_again) {
806                 goto return_result;
807         }
808
809         /* Else call winbindd to get a bunch of entries */
810         
811         if (num_pw_cache > 0) {
812                 free_response(&getpwent_response);
813         }
814
815         ZERO_STRUCT(request);
816         ZERO_STRUCT(getpwent_response);
817
818         request.data.num_entries = MAX_GETPWENT_USERS;
819
820         ret = winbindd_request(WINBINDD_GETPWENT, &request, 
821                                &getpwent_response);
822
823         if (ret == NSS_STATUS_SUCCESS) {
824                 struct winbindd_pw *pw_cache;
825
826                 /* Fill cache */
827
828                 ndx_pw_cache = 0;
829                 num_pw_cache = getpwent_response.data.num_entries;
830
831                 /* Return a result */
832
833         return_result:
834
835                 pw_cache = getpwent_response.extra_data;
836
837                 /* Check data is valid */
838
839                 if (pw_cache == NULL) {
840                         return NSS_STATUS_NOTFOUND;
841                 }
842
843                 ret = fill_pwent(result, &pw_cache[ndx_pw_cache],
844                                  &buffer, &buflen);
845                 
846                 /* Out of memory - try again */
847
848                 if (ret == NSS_STATUS_TRYAGAIN) {
849                         called_again = True;
850                         *errnop = errno = ERANGE;
851                         return ret;
852                 }
853
854                 *errnop = errno = 0;
855                 called_again = False;
856                 ndx_pw_cache++;
857
858                 /* If we've finished with this lot of results free cache */
859
860                 if (ndx_pw_cache == num_pw_cache) {
861                         ndx_pw_cache = num_pw_cache = 0;
862                         free_response(&getpwent_response);
863                 }
864         }
865
866         return ret;
867 }
868
869 /* Return passwd struct from uid */
870
871 NSS_STATUS
872 _nss_winbind_getpwuid_r(uid_t uid, struct passwd *result, char *buffer,
873                         size_t buflen, int *errnop)
874 {
875         NSS_STATUS ret;
876         static struct winbindd_response response;
877         struct winbindd_request request;
878         static int keep_response=0;
879
880         /* If our static buffer needs to be expanded we are called again */
881         if (!keep_response) {
882
883                 /* Call for the first time */
884
885                 ZERO_STRUCT(response);
886                 ZERO_STRUCT(request);
887
888                 request.data.uid = uid;
889
890                 ret = winbindd_request(WINBINDD_GETPWUID, &request, &response);
891
892                 if (ret == NSS_STATUS_SUCCESS) {
893                         ret = fill_pwent(result, &response.data.pw, 
894                                          &buffer, &buflen);
895
896                         if (ret == NSS_STATUS_TRYAGAIN) {
897                                 keep_response = True;
898                                 *errnop = errno = ERANGE;
899                                 return ret;
900                         }
901                 }
902
903         } else {
904
905                 /* We've been called again */
906
907                 ret = fill_pwent(result, &response.data.pw, &buffer, &buflen);
908
909                 if (ret == NSS_STATUS_TRYAGAIN) {
910                         keep_response = True;
911                         *errnop = errno = ERANGE;
912                         return ret;
913                 }
914
915                 keep_response = False;
916                 *errnop = errno = 0;
917         }
918
919         free_response(&response);
920         return ret;
921 }
922
923 /* Return passwd struct from username */
924
925 NSS_STATUS
926 _nss_winbind_getpwnam_r(const char *name, struct passwd *result, char *buffer,
927                         size_t buflen, int *errnop)
928 {
929         NSS_STATUS ret;
930         static struct winbindd_response response;
931         struct winbindd_request request;
932         static int keep_response;
933
934 #ifdef DEBUG_NSS
935         fprintf(stderr, "[%5d]: getpwnam %s\n", getpid(), name);
936 #endif
937
938         /* If our static buffer needs to be expanded we are called again */
939
940         if (!keep_response) {
941
942                 /* Call for the first time */
943
944                 ZERO_STRUCT(response);
945                 ZERO_STRUCT(request);
946
947                 strncpy(request.data.username, name, 
948                         sizeof(request.data.username) - 1);
949                 request.data.username
950                         [sizeof(request.data.username) - 1] = '\0';
951
952                 ret = winbindd_request(WINBINDD_GETPWNAM, &request, &response);
953
954                 if (ret == NSS_STATUS_SUCCESS) {
955                         ret = fill_pwent(result, &response.data.pw, &buffer,
956                                          &buflen);
957
958                         if (ret == NSS_STATUS_TRYAGAIN) {
959                                 keep_response = True;
960                                 *errnop = errno = ERANGE;
961                                 return ret;
962                         }
963                 }
964
965         } else {
966
967                 /* We've been called again */
968
969                 ret = fill_pwent(result, &response.data.pw, &buffer, &buflen);
970
971                 if (ret == NSS_STATUS_TRYAGAIN) {
972                         keep_response = True;
973                         *errnop = errno = ERANGE;
974                         return ret;
975                 }
976
977                 keep_response = False;
978                 *errnop = errno = 0;
979         }
980
981         free_response(&response);
982         return ret;
983 }
984
985 /*
986  * NSS group functions
987  */
988
989 static struct winbindd_response getgrent_response;
990
991 static int ndx_gr_cache;                 /* Current index into grp cache */
992 static int num_gr_cache;                 /* Current size of grp cache */
993
994 /* Rewind "file pointer" to start of ntdom group database */
995
996 NSS_STATUS
997 _nss_winbind_setgrent(void)
998 {
999 #ifdef DEBUG_NSS
1000         fprintf(stderr, "[%5d]: setgrent\n", getpid());
1001 #endif
1002
1003         if (num_gr_cache > 0) {
1004                 ndx_gr_cache = num_gr_cache = 0;
1005                 free_response(&getgrent_response);
1006         }
1007
1008         return winbindd_request(WINBINDD_SETGRENT, NULL, NULL);
1009 }
1010
1011 /* Close "file pointer" for ntdom group database */
1012
1013 NSS_STATUS
1014 _nss_winbind_endgrent(void)
1015 {
1016 #ifdef DEBUG_NSS
1017         fprintf(stderr, "[%5d]: endgrent\n", getpid());
1018 #endif
1019
1020         if (num_gr_cache > 0) {
1021                 ndx_gr_cache = num_gr_cache = 0;
1022                 free_response(&getgrent_response);
1023         }
1024
1025         return winbindd_request(WINBINDD_ENDGRENT, NULL, NULL);
1026 }
1027
1028 /* Get next entry from ntdom group database */
1029
1030 NSS_STATUS
1031 _nss_winbind_getgrent_r(struct group *result,
1032                         char *buffer, size_t buflen, int *errnop)
1033 {
1034         NSS_STATUS ret;
1035         static struct winbindd_request request;
1036         static int called_again;
1037
1038 #ifdef DEBUG_NSS
1039         fprintf(stderr, "[%5d]: getgrent\n", getpid());
1040 #endif
1041
1042         /* Return an entry from the cache if we have one, or if we are
1043            called again because we exceeded our static buffer.  */
1044
1045         if ((ndx_gr_cache < num_gr_cache) || called_again) {
1046                 goto return_result;
1047         }
1048
1049         /* Else call winbindd to get a bunch of entries */
1050         
1051         if (num_gr_cache > 0) {
1052                 free_response(&getgrent_response);
1053         }
1054
1055         ZERO_STRUCT(request);
1056         ZERO_STRUCT(getgrent_response);
1057
1058         request.data.num_entries = MAX_GETGRENT_USERS;
1059
1060         ret = winbindd_request(WINBINDD_GETGRENT, &request, 
1061                                &getgrent_response);
1062
1063         if (ret == NSS_STATUS_SUCCESS) {
1064                 struct winbindd_gr *gr_cache;
1065                 int mem_ofs;
1066
1067                 /* Fill cache */
1068
1069                 ndx_gr_cache = 0;
1070                 num_gr_cache = getgrent_response.data.num_entries;
1071
1072                 /* Return a result */
1073
1074         return_result:
1075
1076                 gr_cache = getgrent_response.extra_data;
1077
1078                 /* Check data is valid */
1079
1080                 if (gr_cache == NULL) {
1081                         return NSS_STATUS_NOTFOUND;
1082                 }
1083
1084                 /* Fill group membership.  The offset into the extra data
1085                    for the group membership is the reported offset plus the
1086                    size of all the winbindd_gr records returned. */
1087
1088                 mem_ofs = gr_cache[ndx_gr_cache].gr_mem_ofs +
1089                         num_gr_cache * sizeof(struct winbindd_gr);
1090
1091                 ret = fill_grent(result, &gr_cache[ndx_gr_cache],
1092                                  ((char *)getgrent_response.extra_data)+mem_ofs,
1093                                  &buffer, &buflen);
1094                 
1095                 /* Out of memory - try again */
1096
1097                 if (ret == NSS_STATUS_TRYAGAIN) {
1098                         called_again = True;
1099                         *errnop = errno = ERANGE;
1100                         return ret;
1101                 }
1102
1103                 *errnop = 0;
1104                 called_again = False;
1105                 ndx_gr_cache++;
1106
1107                 /* If we've finished with this lot of results free cache */
1108
1109                 if (ndx_gr_cache == num_gr_cache) {
1110                         ndx_gr_cache = num_gr_cache = 0;
1111                         free_response(&getgrent_response);
1112                 }
1113         }
1114
1115         return ret;
1116 }
1117
1118 /* Return group struct from group name */
1119
1120 NSS_STATUS
1121 _nss_winbind_getgrnam_r(const char *name,
1122                         struct group *result, char *buffer,
1123                         size_t buflen, int *errnop)
1124 {
1125         NSS_STATUS ret;
1126         static struct winbindd_response response;
1127         struct winbindd_request request;
1128         static int keep_response;
1129         
1130 #ifdef DEBUG_NSS
1131         fprintf(stderr, "[%5d]: getgrnam %s\n", getpid(), name);
1132 #endif
1133
1134         /* If our static buffer needs to be expanded we are called again */
1135         
1136         if (!keep_response) {
1137
1138                 /* Call for the first time */
1139
1140                 ZERO_STRUCT(request);
1141                 ZERO_STRUCT(response);
1142
1143                 strncpy(request.data.groupname, name, 
1144                         sizeof(request.data.groupname));
1145                 request.data.groupname
1146                         [sizeof(request.data.groupname) - 1] = '\0';
1147
1148                 ret = winbindd_request(WINBINDD_GETGRNAM, &request, &response);
1149
1150                 if (ret == NSS_STATUS_SUCCESS) {
1151                         ret = fill_grent(result, &response.data.gr, 
1152                                          response.extra_data,
1153                                          &buffer, &buflen);
1154
1155                         if (ret == NSS_STATUS_TRYAGAIN) {
1156                                 keep_response = True;
1157                                 *errnop = errno = ERANGE;
1158                                 return ret;
1159                         }
1160                 }
1161
1162         } else {
1163                 
1164                 /* We've been called again */
1165                 
1166                 ret = fill_grent(result, &response.data.gr, 
1167                                  response.extra_data, &buffer, &buflen);
1168                 
1169                 if (ret == NSS_STATUS_TRYAGAIN) {
1170                         keep_response = True;
1171                         *errnop = errno = ERANGE;
1172                         return ret;
1173                 }
1174
1175                 keep_response = False;
1176                 *errnop = 0;
1177         }
1178
1179         free_response(&response);
1180         return ret;
1181 }
1182
1183 /* Return group struct from gid */
1184
1185 NSS_STATUS
1186 _nss_winbind_getgrgid_r(gid_t gid,
1187                         struct group *result, char *buffer,
1188                         size_t buflen, int *errnop)
1189 {
1190         NSS_STATUS ret;
1191         static struct winbindd_response response;
1192         struct winbindd_request request;
1193         static int keep_response;
1194
1195 #ifdef DEBUG_NSS
1196         fprintf(stderr, "[%5d]: getgrgid %d\n", getpid(), gid);
1197 #endif
1198
1199         /* If our static buffer needs to be expanded we are called again */
1200
1201         if (!keep_response) {
1202
1203                 /* Call for the first time */
1204
1205                 ZERO_STRUCT(request);
1206                 ZERO_STRUCT(response);
1207
1208                 request.data.gid = gid;
1209
1210                 ret = winbindd_request(WINBINDD_GETGRGID, &request, &response);
1211
1212                 if (ret == NSS_STATUS_SUCCESS) {
1213
1214                         ret = fill_grent(result, &response.data.gr, 
1215                                          response.extra_data, 
1216                                          &buffer, &buflen);
1217
1218                         if (ret == NSS_STATUS_TRYAGAIN) {
1219                                 keep_response = True;
1220                                 *errnop = errno = ERANGE;
1221                                 return ret;
1222                         }
1223                 }
1224
1225         } else {
1226
1227                 /* We've been called again */
1228
1229                 ret = fill_grent(result, &response.data.gr, 
1230                                  response.extra_data, &buffer, &buflen);
1231
1232                 if (ret == NSS_STATUS_TRYAGAIN) {
1233                         keep_response = True;
1234                         *errnop = errno = ERANGE;
1235                         return ret;
1236                 }
1237
1238                 keep_response = False;
1239                 *errnop = 0;
1240         }
1241
1242         free_response(&response);
1243         return ret;
1244 }
1245
1246 /* Initialise supplementary groups */
1247
1248 NSS_STATUS
1249 _nss_winbind_initgroups_dyn(char *user, gid_t group, long int *start,
1250                             long int *size, gid_t **groups, long int limit,
1251                             int *errnop)
1252 {
1253         NSS_STATUS ret;
1254         struct winbindd_request request;
1255         struct winbindd_response response;
1256         int i;
1257
1258 #ifdef DEBUG_NSS
1259         fprintf(stderr, "[%5d]: initgroups %s (%d)\n", getpid(),
1260                 user, group);
1261 #endif
1262
1263         ZERO_STRUCT(request);
1264         ZERO_STRUCT(response);
1265
1266         strncpy(request.data.username, user,
1267                 sizeof(request.data.username) - 1);
1268
1269         ret = winbindd_request(WINBINDD_GETGROUPS, &request, &response);
1270
1271         if (ret == NSS_STATUS_SUCCESS) {
1272                 int num_gids = response.data.num_entries;
1273                 gid_t *gid_list = (gid_t *)response.extra_data;
1274
1275                 /* Copy group list to client */
1276
1277                 for (i = 0; i < num_gids; i++) {
1278
1279                         /* Skip primary group */
1280
1281                         if (gid_list[i] == group) continue;
1282
1283                         /* Add to buffer */
1284
1285                         if (*start == *size && limit <= 0) {
1286                                 (*groups) = realloc(
1287                                         (*groups), (2 * (*size) + 1) * sizeof(**groups));
1288                                 if (! *groups) goto done;
1289                                 *size = 2 * (*size) + 1;
1290                         }
1291
1292                         if (*start == *size) goto done;
1293
1294                         (*groups)[*start] = gid_list[i];
1295                         *start += 1;
1296
1297                         /* Filled buffer? */
1298
1299                         if (*start == limit) goto done;
1300                 }
1301         }
1302         
1303         /* Back to your regularly scheduled programming */
1304
1305  done:
1306         return ret;
1307 }
1308
1309 #endif