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 Lesser 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 Lesser 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);
37 #include <ns_daemon.h>
40 /* Maximum number of users to pass back over the unix domain socket
41 per call. This is not a static limit on the total number of users
42 or groups returned in total. */
44 #define MAX_GETPWENT_USERS 250
45 #define MAX_GETGRENT_USERS 250
47 /* Prototypes from wb_common.c */
49 extern int winbindd_fd
;
55 static int send_next_request(nsd_file_t
*, struct winbindd_request
*);
56 static int do_list(int state
, nsd_file_t
*rq
);
58 static nsd_file_t
*current_rq
= NULL
;
59 static int current_winbind_xid
= 0;
60 static int next_winbind_xid
= 0;
62 typedef struct winbind_xid
{
65 struct winbindd_request
*request
;
66 struct winbind_xid
*next
;
69 static winbind_xid_t
*winbind_xids
= (winbind_xid_t
*)0;
72 winbind_xid_new(int xid
, nsd_file_t
*rq
, struct winbindd_request
*request
)
76 nsd_logprintf(NSD_LOG_LOW
,
77 "entering winbind_xid_new xid = %d rq = 0x%x, request = 0x%x\n",
79 new = (winbind_xid_t
*)nsd_calloc(1,sizeof(winbind_xid_t
));
81 nsd_logprintf(NSD_LOG_RESOURCE
,"winbind_xid_new: failed malloc\n");
87 new->request
= request
;
88 new->next
= winbind_xids
;
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.
99 winbind_xid_lookup(int xid
, struct winbindd_request
**requestp
)
101 winbind_xid_t
**last
, *dx
;
102 nsd_file_t
*result
=0;
104 for (last
= &winbind_xids
, dx
= winbind_xids
; dx
&& (dx
->xid
!= xid
);
105 last
= &dx
->next
, dx
= dx
->next
);
109 *requestp
= dx
->request
;
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
);
120 winbind_startnext_timeout(nsd_file_t
**rqp
, nsd_times_t
*to
)
123 struct winbindd_request
*request
;
125 nsd_logprintf(NSD_LOG_MIN
, "timeout (winbind startnext)\n");
128 nsd_timeout_remove(rq
);
129 request
= to
->t_clientdata
;
130 return(send_next_request(rq
, request
));
134 dequeue_request(void)
137 struct winbindd_request
*request
;
140 * Check for queued requests
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
);
152 do_request(nsd_file_t
*rq
, struct winbindd_request
*request
)
154 if (winbind_xids
== NULL
) {
156 * No outstanding requests.
157 * Send off the request to winbindd
159 nsd_logprintf(NSD_LOG_MIN
, "lookup (winbind) sending request\n");
160 return(send_next_request(rq
, request
));
163 * Just queue it up for now - previous callout or timout
166 nsd_logprintf(NSD_LOG_MIN
,
167 "lookup (winbind): queue request xid = %d\n",
169 return(winbind_xid_new(next_winbind_xid
++, rq
, request
));
174 winbind_callback(nsd_file_t
**rqp
, int fd
)
176 struct winbindd_response response
;
179 char * result
= NULL
;
184 nsd_logprintf(NSD_LOG_MIN
, "entering callback (winbind)\n");
189 nsd_timeout_remove(rq
);
190 nsd_callback_remove(fd
);
192 ZERO_STRUCT(response
);
193 status
= winbindd_get_response(&response
);
195 if (status
!= NSS_STATUS_SUCCESS
) {
196 /* free any extra data area in response structure */
197 winbindd_free_response(&response
);
198 nsd_logprintf(NSD_LOG_MIN
,
199 "callback (winbind) returning not found, status = %d\n",
203 case NSS_STATUS_UNAVAIL
:
204 rq
->f_status
= NS_UNAVAIL
;
206 case NSS_STATUS_TRYAGAIN
:
207 rq
->f_status
= NS_TRYAGAIN
;
209 case NSS_STATUS_NOTFOUND
:
212 rq
->f_status
= NS_NOTFOUND
;
218 switch ((int)rq
->f_cmd_data
) {
219 case WINBINDD_WINS_BYNAME
:
220 case WINBINDD_WINS_BYIP
:
221 nsd_logprintf(NSD_LOG_MIN
,
222 "callback (winbind) WINS_BYNAME | WINS_BYIP\n");
224 rlen
= asprintf(&result
, "%s\n", response
.data
.winsresp
);
225 if (rlen
== 0 || result
== NULL
) {
229 winbindd_free_response(&response
);
231 nsd_logprintf(NSD_LOG_MIN
, " %s\n", result
);
232 nsd_set_result(rq
, NS_SUCCESS
, result
, rlen
, DYNAMIC
);
235 case WINBINDD_GETPWUID
:
236 case WINBINDD_GETPWNAM
:
238 struct winbindd_pw
*pw
= &response
.data
.pw
;
240 nsd_logprintf(NSD_LOG_MIN
,
241 "callback (winbind) GETPWUID | GETPWUID\n");
243 rlen
= asprintf(&result
,"%s:%s:%d:%d:%s:%s:%s\n",
251 if (rlen
== 0 || result
== NULL
)
254 winbindd_free_response(&response
);
256 nsd_logprintf(NSD_LOG_MIN
, " %s\n", result
);
257 nsd_set_result(rq
, NS_SUCCESS
, result
, rlen
, DYNAMIC
);
261 case WINBINDD_GETGRNAM
:
262 case WINBINDD_GETGRGID
:
264 const struct winbindd_gr
*gr
= &response
.data
.gr
;
265 const char * members
;
267 nsd_logprintf(NSD_LOG_MIN
,
268 "callback (winbind) GETGRNAM | GETGRGID\n");
270 if (gr
->num_gr_mem
&& response
.extra_data
.data
) {
271 members
= response
.extra_data
.data
;
276 rlen
= asprintf(&result
, "%s:%s:%d:%s\n",
277 gr
->gr_name
, gr
->gr_passwd
, gr
->gr_gid
, members
);
278 if (rlen
== 0 || result
== NULL
)
281 winbindd_free_response(&response
);
283 nsd_logprintf(NSD_LOG_MIN
, " %s\n", result
);
284 nsd_set_result(rq
, NS_SUCCESS
, result
, rlen
, DYNAMIC
);
288 case WINBINDD_SETGRENT
:
289 case WINBINDD_SETPWENT
:
290 nsd_logprintf(NSD_LOG_MIN
,
291 "callback (winbind) SETGRENT | SETPWENT\n");
292 winbindd_free_response(&response
);
293 return(do_list(1,rq
));
295 case WINBINDD_GETGRENT
:
296 case WINBINDD_GETGRLST
:
300 nsd_logprintf(NSD_LOG_MIN
,
301 "callback (winbind) GETGRENT | GETGRLIST %d responses\n",
302 response
.data
.num_entries
);
304 if (response
.data
.num_entries
) {
305 const struct winbindd_gr
*gr
= &response
.data
.gr
;
306 const char * members
;
310 gr
= (struct winbindd_gr
*)response
.extra_data
.data
;
312 nsd_logprintf(NSD_LOG_MIN
, " no extra_data\n");
313 winbindd_free_response(&response
);
317 members
= (char *)response
.extra_data
.data
+
318 (response
.data
.num_entries
* sizeof(struct winbindd_gr
));
320 for (i
= 0; i
< response
.data
.num_entries
; i
++) {
321 snprintf(grp_name
, sizeof(grp_name
) - 1, "%s:%s:%d:",
322 gr
->gr_name
, gr
->gr_passwd
, gr
->gr_gid
);
324 nsd_append_element(rq
, NS_SUCCESS
, result
, rlen
);
325 nsd_append_result(rq
, NS_SUCCESS
,
326 &members
[gr
->gr_mem_ofs
],
327 strlen(&members
[gr
->gr_mem_ofs
]));
329 /* Don't log the whole list, because it might be
330 * _really_ long and we probably don't want to clobber
333 nsd_logprintf(NSD_LOG_MIN
, " %s (...)\n", grp_name
);
339 entries
= response
.data
.num_entries
;
340 winbindd_free_response(&response
);
341 if (entries
< MAX_GETPWENT_USERS
)
342 return(do_list(2,rq
));
344 return(do_list(1,rq
));
347 case WINBINDD_GETPWENT
:
351 nsd_logprintf(NSD_LOG_MIN
,
352 "callback (winbind) GETPWENT %d responses\n",
353 response
.data
.num_entries
);
355 if (response
.data
.num_entries
) {
356 struct winbindd_pw
*pw
= &response
.data
.pw
;
359 pw
= (struct winbindd_pw
*)response
.extra_data
.data
;
361 nsd_logprintf(NSD_LOG_MIN
, " no extra_data\n");
362 winbindd_free_response(&response
);
365 for (i
= 0; i
< response
.data
.num_entries
; i
++) {
367 rlen
= asprintf(&result
, "%s:%s:%d:%d:%s:%s:%s",
376 if (rlen
!= 0 && result
!= NULL
) {
377 nsd_logprintf(NSD_LOG_MIN
, " %s\n",result
);
378 nsd_append_element(rq
, NS_SUCCESS
, result
, rlen
);
386 entries
= response
.data
.num_entries
;
387 winbindd_free_response(&response
);
388 if (entries
< MAX_GETPWENT_USERS
)
389 return(do_list(2,rq
));
391 return(do_list(1,rq
));
394 case WINBINDD_ENDGRENT
:
395 case WINBINDD_ENDPWENT
:
396 nsd_logprintf(NSD_LOG_MIN
, "callback (winbind) ENDGRENT | ENDPWENT\n");
397 nsd_append_element(rq
, NS_SUCCESS
, "\n", 1);
398 winbindd_free_response(&response
);
402 winbindd_free_response(&response
);
403 nsd_logprintf(NSD_LOG_MIN
, "callback (winbind) invalid command %d\n", (int)rq
->f_cmd_data
);
409 winbind_timeout(nsd_file_t
**rqp
, nsd_times_t
*to
)
415 nsd_logprintf(NSD_LOG_MIN
, "timeout (winbind)\n");
420 /* Remove the callback and timeout */
421 nsd_callback_remove(winbindd_fd
);
422 nsd_timeout_remove(rq
);
424 rq
->f_status
= NS_NOTFOUND
;
429 send_next_request(nsd_file_t
*rq
, struct winbindd_request
*request
)
434 switch (rq
->f_index
) {
436 timeout
= nsd_attr_fetch_long(rq
->f_attrs
,
437 "lookup_timeout", 10, 10);
440 timeout
= nsd_attr_fetch_long(rq
->f_attrs
,
441 "list_timeout", 10, 10);
444 nsd_logprintf(NSD_LOG_OPER
,
445 "send_next_request (winbind) "
446 "invalid request type %d\n", rq
->f_index
);
447 rq
->f_status
= NS_BADREQ
;
451 nsd_logprintf(NSD_LOG_MIN
,
452 "send_next_request (winbind) %d, timeout = %d sec\n",
453 rq
->f_cmd_data
, timeout
);
454 status
= winbindd_send_request((int)rq
->f_cmd_data
,0,request
);
457 if (status
!= NSS_STATUS_SUCCESS
) {
458 nsd_logprintf(NSD_LOG_MIN
,
459 "send_next_request (winbind) error status = %d\n",
461 rq
->f_status
= status
;
468 * Set up callback and timeouts
470 nsd_logprintf(NSD_LOG_MIN
, "send_next_request (winbind) fd = %d\n",
473 nsd_callback_new(winbindd_fd
, winbind_callback
, NSD_READ
);
474 nsd_timeout_new(rq
, timeout
* 1000, winbind_timeout
, NULL
);
480 nsd_logprintf(NSD_LOG_MIN
, "entering init (winbind)\n");
484 int lookup(nsd_file_t
*rq
)
488 struct winbindd_request
*request
;
490 nsd_logprintf(NSD_LOG_MIN
, "entering lookup (winbind)\n");
494 map
= nsd_attr_fetch_string(rq
->f_attrs
, "table", (char*)0);
495 key
= nsd_attr_fetch_string(rq
->f_attrs
, "key", (char*)0);
496 if (! map
|| ! key
) {
497 nsd_logprintf(NSD_LOG_MIN
, "lookup (winbind) table or key not defined\n");
498 rq
->f_status
= NS_BADREQ
;
502 nsd_logprintf(NSD_LOG_MIN
, "lookup (winbind %s)\n",map
);
504 request
= (struct winbindd_request
*)nsd_calloc(1,sizeof(struct winbindd_request
));
506 nsd_logprintf(NSD_LOG_RESOURCE
,
507 "lookup (winbind): failed malloc\n");
511 if (strcasecmp(map
,"passwd.byuid") == 0) {
512 request
->data
.uid
= atoi(key
);
513 rq
->f_cmd_data
= (void *)WINBINDD_GETPWUID
;
514 } else if (strcasecmp(map
,"passwd.byname") == 0) {
515 strncpy(request
->data
.username
, key
,
516 sizeof(request
->data
.username
) - 1);
517 request
->data
.username
[sizeof(request
->data
.username
) - 1] = '\0';
518 rq
->f_cmd_data
= (void *)WINBINDD_GETPWNAM
;
519 } else if (strcasecmp(map
,"group.byname") == 0) {
520 strncpy(request
->data
.groupname
, key
,
521 sizeof(request
->data
.groupname
) - 1);
522 request
->data
.groupname
[sizeof(request
->data
.groupname
) - 1] = '\0';
523 rq
->f_cmd_data
= (void *)WINBINDD_GETGRNAM
;
524 } else if (strcasecmp(map
,"group.bygid") == 0) {
525 request
->data
.gid
= atoi(key
);
526 rq
->f_cmd_data
= (void *)WINBINDD_GETGRGID
;
527 } else if (strcasecmp(map
,"hosts.byname") == 0) {
528 strncpy(request
->data
.winsreq
, key
, sizeof(request
->data
.winsreq
) - 1);
529 request
->data
.winsreq
[sizeof(request
->data
.winsreq
) - 1] = '\0';
530 rq
->f_cmd_data
= (void *)WINBINDD_WINS_BYNAME
;
531 } else if (strcasecmp(map
,"hosts.byaddr") == 0) {
532 strncpy(request
->data
.winsreq
, key
, sizeof(request
->data
.winsreq
) - 1);
533 request
->data
.winsreq
[sizeof(request
->data
.winsreq
) - 1] = '\0';
534 rq
->f_cmd_data
= (void *)WINBINDD_WINS_BYIP
;
537 * Don't understand this map - just return not found
539 nsd_logprintf(NSD_LOG_MIN
, "lookup (winbind) unknown table\n");
541 rq
->f_status
= NS_NOTFOUND
;
545 return(do_request(rq
, request
));
548 int list(nsd_file_t
*rq
)
552 nsd_logprintf(NSD_LOG_MIN
, "entering list (winbind)\n");
556 map
= nsd_attr_fetch_string(rq
->f_attrs
, "table", (char*)0);
558 nsd_logprintf(NSD_LOG_MIN
, "list (winbind) table not defined\n");
559 rq
->f_status
= NS_BADREQ
;
563 nsd_logprintf(NSD_LOG_MIN
, "list (winbind %s)\n",map
);
565 return (do_list(0,rq
));
569 do_list(int state
, nsd_file_t
*rq
)
572 struct winbindd_request
*request
;
574 nsd_logprintf(NSD_LOG_MIN
, "entering do_list (winbind) state = %d\n",state
);
576 map
= nsd_attr_fetch_string(rq
->f_attrs
, "table", (char*)0);
577 request
= (struct winbindd_request
*)nsd_calloc(1,sizeof(struct winbindd_request
));
579 nsd_logprintf(NSD_LOG_RESOURCE
,
580 "do_list (winbind): failed malloc\n");
584 if (strcasecmp(map
,"passwd.byname") == 0) {
587 rq
->f_cmd_data
= (void *)WINBINDD_SETPWENT
;
590 request
->data
.num_entries
= MAX_GETPWENT_USERS
;
591 rq
->f_cmd_data
= (void *)WINBINDD_GETPWENT
;
594 rq
->f_cmd_data
= (void *)WINBINDD_ENDPWENT
;
597 nsd_logprintf(NSD_LOG_MIN
, "do_list (winbind) unknown state\n");
599 rq
->f_status
= NS_NOTFOUND
;
602 } else if (strcasecmp(map
,"group.byname") == 0) {
605 rq
->f_cmd_data
= (void *)WINBINDD_SETGRENT
;
608 request
->data
.num_entries
= MAX_GETGRENT_USERS
;
609 rq
->f_cmd_data
= (void *)WINBINDD_GETGRENT
;
612 rq
->f_cmd_data
= (void *)WINBINDD_ENDGRENT
;
615 nsd_logprintf(NSD_LOG_MIN
, "do_list (winbind) unknown state\n");
617 rq
->f_status
= NS_NOTFOUND
;
622 * Don't understand this map - just return not found
624 nsd_logprintf(NSD_LOG_MIN
, "do_list (winbind) unknown table\n");
626 rq
->f_status
= NS_NOTFOUND
;
630 return(do_request(rq
, request
));
633 #endif /* HAVE_NS_API_H */