2 Unix SMB/CIFS implementation.
4 Windows NT Domain nsswitch module
6 Copyright (C) Tim Potter 2000
7 Copyright (C) James Peach 2006
9 This library is free software; you can redistribute it and/or
10 modify it under the terms of the GNU Library General Public
11 License as published by the Free Software Foundation; either
12 version 3 of the License, or (at your option) any later version.
14 This library 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 GNU
17 Library General Public License for more details.
19 You should have received a copy of the GNU Library General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>.
23 #include "winbind_client.h"
25 #ifndef PRINTF_ATTRIBUTE
26 #define PRINTF_ATTRIBUTE(m, n)
29 #ifndef HAVE_ASPRINTF_DECL
31 int asprintf(char **,const char *, ...) PRINTF_ATTRIBUTE(2,3);
38 #include <ns_daemon.h>
41 /* Maximum number of users to pass back over the unix domain socket
42 per call. This is not a static limit on the total number of users
43 or groups returned in total. */
45 #define MAX_GETPWENT_USERS 250
46 #define MAX_GETGRENT_USERS 250
48 /* Prototypes from wb_common.c */
50 extern int winbindd_fd;
56 static int send_next_request(nsd_file_t *, struct winbindd_request *);
57 static int do_list(int state, nsd_file_t *rq);
59 static nsd_file_t *current_rq = NULL;
60 static int current_winbind_xid = 0;
61 static int next_winbind_xid = 0;
63 typedef struct winbind_xid {
66 struct winbindd_request *request;
67 struct winbind_xid *next;
70 static winbind_xid_t *winbind_xids = (winbind_xid_t *)0;
73 winbind_xid_new(int xid, nsd_file_t *rq, struct winbindd_request *request)
77 nsd_logprintf(NSD_LOG_LOW,
78 "entering winbind_xid_new xid = %d rq = 0x%x, request = 0x%x\n",
80 new = (winbind_xid_t *)nsd_calloc(1,sizeof(winbind_xid_t));
82 nsd_logprintf(NSD_LOG_RESOURCE,"winbind_xid_new: failed malloc\n");
88 new->request = request;
89 new->next = winbind_xids;
96 ** This routine will look down the xid list and return the request
97 ** associated with an xid. We remove the record if it is found.
100 winbind_xid_lookup(int xid, struct winbindd_request **requestp)
102 winbind_xid_t **last, *dx;
103 nsd_file_t *result=0;
105 for (last = &winbind_xids, dx = winbind_xids; dx && (dx->xid != xid);
106 last = &dx->next, dx = dx->next);
110 *requestp = dx->request;
113 nsd_logprintf(NSD_LOG_LOW,
114 "entering winbind_xid_lookup xid = %d rq = 0x%x, request = 0x%x\n",
115 xid, result, dx->request);
121 winbind_startnext_timeout(nsd_file_t **rqp, nsd_times_t *to)
124 struct winbindd_request *request;
126 nsd_logprintf(NSD_LOG_MIN, "timeout (winbind startnext)\n");
129 nsd_timeout_remove(rq);
130 request = to->t_clientdata;
131 return(send_next_request(rq, request));
135 dequeue_request(void)
138 struct winbindd_request *request;
141 * Check for queued requests
144 nsd_logprintf(NSD_LOG_MIN, "timeout (winbind) unqueue xid %d\n",
145 current_winbind_xid);
146 rq = winbind_xid_lookup(current_winbind_xid++, &request);
147 /* cause a timeout on the queued request so we can send it */
148 nsd_timeout_new(rq,1,winbind_startnext_timeout,request);
153 do_request(nsd_file_t *rq, struct winbindd_request *request)
155 if (winbind_xids == NULL) {
157 * No outstanding requests.
158 * Send off the request to winbindd
160 nsd_logprintf(NSD_LOG_MIN, "lookup (winbind) sending request\n");
161 return(send_next_request(rq, request));
164 * Just queue it up for now - previous callout or timout
167 nsd_logprintf(NSD_LOG_MIN,
168 "lookup (winbind): queue request xid = %d\n",
170 return(winbind_xid_new(next_winbind_xid++, rq, request));
175 winbind_callback(nsd_file_t **rqp, int fd)
177 struct winbindd_response response;
180 char * result = NULL;
185 nsd_logprintf(NSD_LOG_MIN, "entering callback (winbind)\n");
190 nsd_timeout_remove(rq);
191 nsd_callback_remove(fd);
193 ZERO_STRUCT(response);
194 status = winbindd_get_response(&response);
196 if (status != NSS_STATUS_SUCCESS) {
197 /* free any extra data area in response structure */
198 free_response(&response);
199 nsd_logprintf(NSD_LOG_MIN,
200 "callback (winbind) returning not found, status = %d\n",
204 case NSS_STATUS_UNAVAIL:
205 rq->f_status = NS_UNAVAIL;
207 case NSS_STATUS_TRYAGAIN:
208 rq->f_status = NS_TRYAGAIN;
210 case NSS_STATUS_NOTFOUND:
213 rq->f_status = NS_NOTFOUND;
219 switch ((int)rq->f_cmd_data) {
220 case WINBINDD_WINS_BYNAME:
221 case WINBINDD_WINS_BYIP:
222 nsd_logprintf(NSD_LOG_MIN,
223 "callback (winbind) WINS_BYNAME | WINS_BYIP\n");
225 rlen = asprintf(&result, "%s\n", response.data.winsresp);
226 if (rlen == 0 || result == NULL) {
230 free_response(&response);
232 nsd_logprintf(NSD_LOG_MIN, " %s\n", result);
233 nsd_set_result(rq, NS_SUCCESS, result, rlen, DYNAMIC);
236 case WINBINDD_GETPWUID:
237 case WINBINDD_GETPWNAM:
239 struct winbindd_pw *pw = &response.data.pw;
241 nsd_logprintf(NSD_LOG_MIN,
242 "callback (winbind) GETPWUID | GETPWUID\n");
244 rlen = asprintf(&result,"%s:%s:%d:%d:%s:%s:%s\n",
252 if (rlen == 0 || result == NULL)
255 free_response(&response);
257 nsd_logprintf(NSD_LOG_MIN, " %s\n", result);
258 nsd_set_result(rq, NS_SUCCESS, result, rlen, DYNAMIC);
262 case WINBINDD_GETGRNAM:
263 case WINBINDD_GETGRGID:
265 const struct winbindd_gr *gr = &response.data.gr;
266 const char * members;
268 nsd_logprintf(NSD_LOG_MIN,
269 "callback (winbind) GETGRNAM | GETGRGID\n");
271 if (gr->num_gr_mem && response.extra_data.data) {
272 members = response.extra_data.data;
277 rlen = asprintf(&result, "%s:%s:%d:%s\n",
278 gr->gr_name, gr->gr_passwd, gr->gr_gid, members);
279 if (rlen == 0 || result == NULL)
282 free_response(&response);
284 nsd_logprintf(NSD_LOG_MIN, " %s\n", result);
285 nsd_set_result(rq, NS_SUCCESS, result, rlen, DYNAMIC);
289 case WINBINDD_SETGRENT:
290 case WINBINDD_SETPWENT:
291 nsd_logprintf(NSD_LOG_MIN,
292 "callback (winbind) SETGRENT | SETPWENT\n");
293 free_response(&response);
294 return(do_list(1,rq));
296 case WINBINDD_GETGRENT:
297 case WINBINDD_GETGRLST:
301 nsd_logprintf(NSD_LOG_MIN,
302 "callback (winbind) GETGRENT | GETGRLIST %d responses\n",
303 response.data.num_entries);
305 if (response.data.num_entries) {
306 const struct winbindd_gr *gr = &response.data.gr;
307 const char * members;
311 gr = (struct winbindd_gr *)response.extra_data.data;
313 nsd_logprintf(NSD_LOG_MIN, " no extra_data\n");
314 free_response(&response);
318 members = (char *)response.extra_data.data +
319 (response.data.num_entries * sizeof(struct winbindd_gr));
321 for (i = 0; i < response.data.num_entries; i++) {
322 snprintf(grp_name, sizeof(grp_name) - 1, "%s:%s:%d:",
323 gr->gr_name, gr->gr_passwd, gr->gr_gid);
325 nsd_append_element(rq, NS_SUCCESS, result, rlen);
326 nsd_append_result(rq, NS_SUCCESS,
327 &members[gr->gr_mem_ofs],
328 strlen(&members[gr->gr_mem_ofs]));
330 /* Don't log the whole list, because it might be
331 * _really_ long and we probably don't want to clobber
334 nsd_logprintf(NSD_LOG_MIN, " %s (...)\n", grp_name);
340 entries = response.data.num_entries;
341 free_response(&response);
342 if (entries < MAX_GETPWENT_USERS)
343 return(do_list(2,rq));
345 return(do_list(1,rq));
348 case WINBINDD_GETPWENT:
352 nsd_logprintf(NSD_LOG_MIN,
353 "callback (winbind) GETPWENT %d responses\n",
354 response.data.num_entries);
356 if (response.data.num_entries) {
357 struct winbindd_pw *pw = &response.data.pw;
360 pw = (struct winbindd_pw *)response.extra_data.data;
362 nsd_logprintf(NSD_LOG_MIN, " no extra_data\n");
363 free_response(&response);
366 for (i = 0; i < response.data.num_entries; i++) {
368 rlen = asprintf(&result, "%s:%s:%d:%d:%s:%s:%s",
377 if (rlen != 0 && result != NULL) {
378 nsd_logprintf(NSD_LOG_MIN, " %s\n",result);
379 nsd_append_element(rq, NS_SUCCESS, result, rlen);
387 entries = response.data.num_entries;
388 free_response(&response);
389 if (entries < MAX_GETPWENT_USERS)
390 return(do_list(2,rq));
392 return(do_list(1,rq));
395 case WINBINDD_ENDGRENT:
396 case WINBINDD_ENDPWENT:
397 nsd_logprintf(NSD_LOG_MIN, "callback (winbind) ENDGRENT | ENDPWENT\n");
398 nsd_append_element(rq, NS_SUCCESS, "\n", 1);
399 free_response(&response);
403 free_response(&response);
404 nsd_logprintf(NSD_LOG_MIN, "callback (winbind) invalid command %d\n", (int)rq->f_cmd_data);
410 winbind_timeout(nsd_file_t **rqp, nsd_times_t *to)
416 nsd_logprintf(NSD_LOG_MIN, "timeout (winbind)\n");
421 /* Remove the callback and timeout */
422 nsd_callback_remove(winbindd_fd);
423 nsd_timeout_remove(rq);
425 rq->f_status = NS_NOTFOUND;
430 send_next_request(nsd_file_t *rq, struct winbindd_request *request)
435 switch (rq->f_index) {
437 timeout = nsd_attr_fetch_long(rq->f_attrs,
438 "lookup_timeout", 10, 10);
441 timeout = nsd_attr_fetch_long(rq->f_attrs,
442 "list_timeout", 10, 10);
445 nsd_logprintf(NSD_LOG_OPER,
446 "send_next_request (winbind) "
447 "invalid request type %d\n", rq->f_index);
448 rq->f_status = NS_BADREQ;
452 nsd_logprintf(NSD_LOG_MIN,
453 "send_next_request (winbind) %d, timeout = %d sec\n",
454 rq->f_cmd_data, timeout);
455 status = winbindd_send_request((int)rq->f_cmd_data,0,request);
458 if (status != NSS_STATUS_SUCCESS) {
459 nsd_logprintf(NSD_LOG_MIN,
460 "send_next_request (winbind) error status = %d\n",
462 rq->f_status = status;
469 * Set up callback and timeouts
471 nsd_logprintf(NSD_LOG_MIN, "send_next_request (winbind) fd = %d\n",
474 nsd_callback_new(winbindd_fd, winbind_callback, NSD_READ);
475 nsd_timeout_new(rq, timeout * 1000, winbind_timeout, NULL);
481 nsd_logprintf(NSD_LOG_MIN, "entering init (winbind)\n");
485 int lookup(nsd_file_t *rq)
489 struct winbindd_request *request;
491 nsd_logprintf(NSD_LOG_MIN, "entering lookup (winbind)\n");
495 map = nsd_attr_fetch_string(rq->f_attrs, "table", (char*)0);
496 key = nsd_attr_fetch_string(rq->f_attrs, "key", (char*)0);
497 if (! map || ! key) {
498 nsd_logprintf(NSD_LOG_MIN, "lookup (winbind) table or key not defined\n");
499 rq->f_status = NS_BADREQ;
503 nsd_logprintf(NSD_LOG_MIN, "lookup (winbind %s)\n",map);
505 request = (struct winbindd_request *)nsd_calloc(1,sizeof(struct winbindd_request));
507 nsd_logprintf(NSD_LOG_RESOURCE,
508 "lookup (winbind): failed malloc\n");
512 if (strcasecmp(map,"passwd.byuid") == 0) {
513 request->data.uid = atoi(key);
514 rq->f_cmd_data = (void *)WINBINDD_GETPWUID;
515 } else if (strcasecmp(map,"passwd.byname") == 0) {
516 strncpy(request->data.username, key,
517 sizeof(request->data.username) - 1);
518 request->data.username[sizeof(request->data.username) - 1] = '\0';
519 rq->f_cmd_data = (void *)WINBINDD_GETPWNAM;
520 } else if (strcasecmp(map,"group.byname") == 0) {
521 strncpy(request->data.groupname, key,
522 sizeof(request->data.groupname) - 1);
523 request->data.groupname[sizeof(request->data.groupname) - 1] = '\0';
524 rq->f_cmd_data = (void *)WINBINDD_GETGRNAM;
525 } else if (strcasecmp(map,"group.bygid") == 0) {
526 request->data.gid = atoi(key);
527 rq->f_cmd_data = (void *)WINBINDD_GETGRGID;
528 } else if (strcasecmp(map,"hosts.byname") == 0) {
529 strncpy(request->data.winsreq, key, sizeof(request->data.winsreq) - 1);
530 request->data.winsreq[sizeof(request->data.winsreq) - 1] = '\0';
531 rq->f_cmd_data = (void *)WINBINDD_WINS_BYNAME;
532 } else if (strcasecmp(map,"hosts.byaddr") == 0) {
533 strncpy(request->data.winsreq, key, sizeof(request->data.winsreq) - 1);
534 request->data.winsreq[sizeof(request->data.winsreq) - 1] = '\0';
535 rq->f_cmd_data = (void *)WINBINDD_WINS_BYIP;
538 * Don't understand this map - just return not found
540 nsd_logprintf(NSD_LOG_MIN, "lookup (winbind) unknown table\n");
542 rq->f_status = NS_NOTFOUND;
546 return(do_request(rq, request));
549 int list(nsd_file_t *rq)
553 nsd_logprintf(NSD_LOG_MIN, "entering list (winbind)\n");
557 map = nsd_attr_fetch_string(rq->f_attrs, "table", (char*)0);
559 nsd_logprintf(NSD_LOG_MIN, "list (winbind) table not defined\n");
560 rq->f_status = NS_BADREQ;
564 nsd_logprintf(NSD_LOG_MIN, "list (winbind %s)\n",map);
566 return (do_list(0,rq));
570 do_list(int state, nsd_file_t *rq)
573 struct winbindd_request *request;
575 nsd_logprintf(NSD_LOG_MIN, "entering do_list (winbind) state = %d\n",state);
577 map = nsd_attr_fetch_string(rq->f_attrs, "table", (char*)0);
578 request = (struct winbindd_request *)nsd_calloc(1,sizeof(struct winbindd_request));
580 nsd_logprintf(NSD_LOG_RESOURCE,
581 "do_list (winbind): failed malloc\n");
585 if (strcasecmp(map,"passwd.byname") == 0) {
588 rq->f_cmd_data = (void *)WINBINDD_SETPWENT;
591 request->data.num_entries = MAX_GETPWENT_USERS;
592 rq->f_cmd_data = (void *)WINBINDD_GETPWENT;
595 rq->f_cmd_data = (void *)WINBINDD_ENDPWENT;
598 nsd_logprintf(NSD_LOG_MIN, "do_list (winbind) unknown state\n");
600 rq->f_status = NS_NOTFOUND;
603 } else if (strcasecmp(map,"group.byname") == 0) {
606 rq->f_cmd_data = (void *)WINBINDD_SETGRENT;
609 request->data.num_entries = MAX_GETGRENT_USERS;
610 rq->f_cmd_data = (void *)WINBINDD_GETGRENT;
613 rq->f_cmd_data = (void *)WINBINDD_ENDGRENT;
616 nsd_logprintf(NSD_LOG_MIN, "do_list (winbind) unknown state\n");
618 rq->f_status = NS_NOTFOUND;
623 * Don't understand this map - just return not found
625 nsd_logprintf(NSD_LOG_MIN, "do_list (winbind) unknown table\n");
627 rq->f_status = NS_NOTFOUND;
631 return(do_request(rq, request));
634 #endif /* HAVE_NS_API_H */