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 2 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
20
License along with this library; if not, write to the
21
Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22
Boston, MA 02111-1307, USA.
25
#include "winbind_client.h"
27
#ifndef PRINTF_ATTRIBUTE
28
#define PRINTF_ATTRIBUTE(m, n)
31
#ifndef HAVE_ASPRINTF_DECL
33
int asprintf(char **,const char *, ...) PRINTF_ATTRIBUTE(2,3);
40
#include <ns_daemon.h>
43
/* Maximum number of users to pass back over the unix domain socket
44
per call. This is not a static limit on the total number of users
45
or groups returned in total. */
47
#define MAX_GETPWENT_USERS 250
48
#define MAX_GETGRENT_USERS 250
50
/* Prototypes from wb_common.c */
52
extern int winbindd_fd;
58
static int send_next_request(nsd_file_t *, struct winbindd_request *);
59
static int do_list(int state, nsd_file_t *rq);
61
static nsd_file_t *current_rq = NULL;
62
static int current_winbind_xid = 0;
63
static int next_winbind_xid = 0;
65
typedef struct winbind_xid {
68
struct winbindd_request *request;
69
struct winbind_xid *next;
72
static winbind_xid_t *winbind_xids = (winbind_xid_t *)0;
75
winbind_xid_new(int xid, nsd_file_t *rq, struct winbindd_request *request)
79
nsd_logprintf(NSD_LOG_LOW,
80
"entering winbind_xid_new xid = %d rq = 0x%x, request = 0x%x\n",
82
new = (winbind_xid_t *)nsd_calloc(1,sizeof(winbind_xid_t));
84
nsd_logprintf(NSD_LOG_RESOURCE,"winbind_xid_new: failed malloc\n");
90
new->request = request;
91
new->next = winbind_xids;
98
** This routine will look down the xid list and return the request
99
** associated with an xid. We remove the record if it is found.
102
winbind_xid_lookup(int xid, struct winbindd_request **requestp)
104
winbind_xid_t **last, *dx;
105
nsd_file_t *result=0;
107
for (last = &winbind_xids, dx = winbind_xids; dx && (dx->xid != xid);
108
last = &dx->next, dx = dx->next);
112
*requestp = dx->request;
115
nsd_logprintf(NSD_LOG_LOW,
116
"entering winbind_xid_lookup xid = %d rq = 0x%x, request = 0x%x\n",
117
xid, result, dx->request);
123
winbind_startnext_timeout(nsd_file_t **rqp, nsd_times_t *to)
126
struct winbindd_request *request;
128
nsd_logprintf(NSD_LOG_MIN, "timeout (winbind startnext)\n");
131
nsd_timeout_remove(rq);
132
request = to->t_clientdata;
133
return(send_next_request(rq, request));
137
dequeue_request(void)
140
struct winbindd_request *request;
143
* Check for queued requests
146
nsd_logprintf(NSD_LOG_MIN, "timeout (winbind) unqueue xid %d\n",
147
current_winbind_xid);
148
rq = winbind_xid_lookup(current_winbind_xid++, &request);
149
/* cause a timeout on the queued request so we can send it */
150
nsd_timeout_new(rq,1,winbind_startnext_timeout,request);
155
do_request(nsd_file_t *rq, struct winbindd_request *request)
157
if (winbind_xids == NULL) {
159
* No outstanding requests.
160
* Send off the request to winbindd
162
nsd_logprintf(NSD_LOG_MIN, "lookup (winbind) sending request\n");
163
return(send_next_request(rq, request));
166
* Just queue it up for now - previous callout or timout
169
nsd_logprintf(NSD_LOG_MIN,
170
"lookup (winbind): queue request xid = %d\n",
172
return(winbind_xid_new(next_winbind_xid++, rq, request));
177
winbind_callback(nsd_file_t **rqp, int fd)
179
struct winbindd_response response;
182
char * result = NULL;
187
nsd_logprintf(NSD_LOG_MIN, "entering callback (winbind)\n");
192
nsd_timeout_remove(rq);
193
nsd_callback_remove(fd);
195
ZERO_STRUCT(response);
196
status = winbindd_get_response(&response);
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",
206
case NSS_STATUS_UNAVAIL:
207
rq->f_status = NS_UNAVAIL;
209
case NSS_STATUS_TRYAGAIN:
210
rq->f_status = NS_TRYAGAIN;
212
case NSS_STATUS_NOTFOUND:
215
rq->f_status = NS_NOTFOUND;
221
switch ((int)rq->f_cmd_data) {
222
case WINBINDD_WINS_BYNAME:
223
case WINBINDD_WINS_BYIP:
224
nsd_logprintf(NSD_LOG_MIN,
225
"callback (winbind) WINS_BYNAME | WINS_BYIP\n");
227
rlen = asprintf(&result, "%s\n", response.data.winsresp);
228
if (rlen == 0 || result == NULL) {
232
free_response(&response);
234
nsd_logprintf(NSD_LOG_MIN, " %s\n", result);
235
nsd_set_result(rq, NS_SUCCESS, result, rlen, DYNAMIC);
238
case WINBINDD_GETPWUID:
239
case WINBINDD_GETPWNAM:
241
struct winbindd_pw *pw = &response.data.pw;
243
nsd_logprintf(NSD_LOG_MIN,
244
"callback (winbind) GETPWUID | GETPWUID\n");
246
rlen = asprintf(&result,"%s:%s:%d:%d:%s:%s:%s\n",
254
if (rlen == 0 || result == NULL)
257
free_response(&response);
259
nsd_logprintf(NSD_LOG_MIN, " %s\n", result);
260
nsd_set_result(rq, NS_SUCCESS, result, rlen, DYNAMIC);
264
case WINBINDD_GETGRNAM:
265
case WINBINDD_GETGRGID:
267
const struct winbindd_gr *gr = &response.data.gr;
268
const char * members;
270
nsd_logprintf(NSD_LOG_MIN,
271
"callback (winbind) GETGRNAM | GETGRGID\n");
273
if (gr->num_gr_mem && response.extra_data.data) {
274
members = response.extra_data.data;
279
rlen = asprintf(&result, "%s:%s:%d:%s\n",
280
gr->gr_name, gr->gr_passwd, gr->gr_gid, members);
281
if (rlen == 0 || result == NULL)
284
free_response(&response);
286
nsd_logprintf(NSD_LOG_MIN, " %s\n", result);
287
nsd_set_result(rq, NS_SUCCESS, result, rlen, DYNAMIC);
291
case WINBINDD_SETGRENT:
292
case WINBINDD_SETPWENT:
293
nsd_logprintf(NSD_LOG_MIN,
294
"callback (winbind) SETGRENT | SETPWENT\n");
295
free_response(&response);
296
return(do_list(1,rq));
298
case WINBINDD_GETGRENT:
299
case WINBINDD_GETGRLST:
303
nsd_logprintf(NSD_LOG_MIN,
304
"callback (winbind) GETGRENT | GETGRLIST %d responses\n",
305
response.data.num_entries);
307
if (response.data.num_entries) {
308
const struct winbindd_gr *gr = &response.data.gr;
309
const char * members;
313
gr = (struct winbindd_gr *)response.extra_data.data;
315
nsd_logprintf(NSD_LOG_MIN, " no extra_data\n");
316
free_response(&response);
320
members = (char *)response.extra_data.data +
321
(response.data.num_entries * sizeof(struct winbindd_gr));
323
for (i = 0; i < response.data.num_entries; i++) {
324
snprintf(grp_name, sizeof(grp_name) - 1, "%s:%s:%d:",
325
gr->gr_name, gr->gr_passwd, gr->gr_gid);
327
nsd_append_element(rq, NS_SUCCESS, result, rlen);
328
nsd_append_result(rq, NS_SUCCESS,
329
&members[gr->gr_mem_ofs],
330
strlen(&members[gr->gr_mem_ofs]));
332
/* Don't log the whole list, because it might be
333
* _really_ long and we probably don't want to clobber
336
nsd_logprintf(NSD_LOG_MIN, " %s (...)\n", grp_name);
342
entries = response.data.num_entries;
343
free_response(&response);
344
if (entries < MAX_GETPWENT_USERS)
345
return(do_list(2,rq));
347
return(do_list(1,rq));
350
case WINBINDD_GETPWENT:
354
nsd_logprintf(NSD_LOG_MIN,
355
"callback (winbind) GETPWENT %d responses\n",
356
response.data.num_entries);
358
if (response.data.num_entries) {
359
struct winbindd_pw *pw = &response.data.pw;
362
pw = (struct winbindd_pw *)response.extra_data.data;
364
nsd_logprintf(NSD_LOG_MIN, " no extra_data\n");
365
free_response(&response);
368
for (i = 0; i < response.data.num_entries; i++) {
370
rlen = asprintf(&result, "%s:%s:%d:%d:%s:%s:%s",
379
if (rlen != 0 && result != NULL) {
380
nsd_logprintf(NSD_LOG_MIN, " %s\n",result);
381
nsd_append_element(rq, NS_SUCCESS, result, rlen);
389
entries = response.data.num_entries;
390
free_response(&response);
391
if (entries < MAX_GETPWENT_USERS)
392
return(do_list(2,rq));
394
return(do_list(1,rq));
397
case WINBINDD_ENDGRENT:
398
case WINBINDD_ENDPWENT:
399
nsd_logprintf(NSD_LOG_MIN, "callback (winbind) ENDGRENT | ENDPWENT\n");
400
nsd_append_element(rq, NS_SUCCESS, "\n", 1);
401
free_response(&response);
405
free_response(&response);
406
nsd_logprintf(NSD_LOG_MIN, "callback (winbind) invalid command %d\n", (int)rq->f_cmd_data);
412
winbind_timeout(nsd_file_t **rqp, nsd_times_t *to)
418
nsd_logprintf(NSD_LOG_MIN, "timeout (winbind)\n");
423
/* Remove the callback and timeout */
424
nsd_callback_remove(winbindd_fd);
425
nsd_timeout_remove(rq);
427
rq->f_status = NS_NOTFOUND;
432
send_next_request(nsd_file_t *rq, struct winbindd_request *request)
437
switch (rq->f_index) {
439
timeout = nsd_attr_fetch_long(rq->f_attrs,
440
"lookup_timeout", 10, 10);
443
timeout = nsd_attr_fetch_long(rq->f_attrs,
444
"list_timeout", 10, 10);
447
nsd_logprintf(NSD_LOG_OPER,
448
"send_next_request (winbind) "
449
"invalid request type %d\n", rq->f_index);
450
rq->f_status = NS_BADREQ;
454
nsd_logprintf(NSD_LOG_MIN,
455
"send_next_request (winbind) %d, timeout = %d sec\n",
456
rq->f_cmd_data, timeout);
457
status = winbindd_send_request((int)rq->f_cmd_data,request);
460
if (status != NSS_STATUS_SUCCESS) {
461
nsd_logprintf(NSD_LOG_MIN,
462
"send_next_request (winbind) error status = %d\n",
464
rq->f_status = status;
471
* Set up callback and timeouts
473
nsd_logprintf(NSD_LOG_MIN, "send_next_request (winbind) fd = %d\n",
476
nsd_callback_new(winbindd_fd, winbind_callback, NSD_READ);
477
nsd_timeout_new(rq, timeout * 1000, winbind_timeout, NULL);
483
nsd_logprintf(NSD_LOG_MIN, "entering init (winbind)\n");
487
int lookup(nsd_file_t *rq)
491
struct winbindd_request *request;
493
nsd_logprintf(NSD_LOG_MIN, "entering lookup (winbind)\n");
497
map = nsd_attr_fetch_string(rq->f_attrs, "table", (char*)0);
498
key = nsd_attr_fetch_string(rq->f_attrs, "key", (char*)0);
499
if (! map || ! key) {
500
nsd_logprintf(NSD_LOG_MIN, "lookup (winbind) table or key not defined\n");
501
rq->f_status = NS_BADREQ;
505
nsd_logprintf(NSD_LOG_MIN, "lookup (winbind %s)\n",map);
507
request = (struct winbindd_request *)nsd_calloc(1,sizeof(struct winbindd_request));
509
nsd_logprintf(NSD_LOG_RESOURCE,
510
"lookup (winbind): failed malloc\n");
514
if (strcasecmp(map,"passwd.byuid") == 0) {
515
request->data.uid = atoi(key);
516
rq->f_cmd_data = (void *)WINBINDD_GETPWUID;
517
} else if (strcasecmp(map,"passwd.byname") == 0) {
518
strncpy(request->data.username, key,
519
sizeof(request->data.username) - 1);
520
request->data.username[sizeof(request->data.username) - 1] = '\0';
521
rq->f_cmd_data = (void *)WINBINDD_GETPWNAM;
522
} else if (strcasecmp(map,"group.byname") == 0) {
523
strncpy(request->data.groupname, key,
524
sizeof(request->data.groupname) - 1);
525
request->data.groupname[sizeof(request->data.groupname) - 1] = '\0';
526
rq->f_cmd_data = (void *)WINBINDD_GETGRNAM;
527
} else if (strcasecmp(map,"group.bygid") == 0) {
528
request->data.gid = atoi(key);
529
rq->f_cmd_data = (void *)WINBINDD_GETGRGID;
530
} else if (strcasecmp(map,"hosts.byname") == 0) {
531
strncpy(request->data.winsreq, key, sizeof(request->data.winsreq) - 1);
532
request->data.winsreq[sizeof(request->data.winsreq) - 1] = '\0';
533
rq->f_cmd_data = (void *)WINBINDD_WINS_BYNAME;
534
} else if (strcasecmp(map,"hosts.byaddr") == 0) {
535
strncpy(request->data.winsreq, key, sizeof(request->data.winsreq) - 1);
536
request->data.winsreq[sizeof(request->data.winsreq) - 1] = '\0';
537
rq->f_cmd_data = (void *)WINBINDD_WINS_BYIP;
540
* Don't understand this map - just return not found
542
nsd_logprintf(NSD_LOG_MIN, "lookup (winbind) unknown table\n");
544
rq->f_status = NS_NOTFOUND;
548
return(do_request(rq, request));
551
int list(nsd_file_t *rq)
555
nsd_logprintf(NSD_LOG_MIN, "entering list (winbind)\n");
559
map = nsd_attr_fetch_string(rq->f_attrs, "table", (char*)0);
561
nsd_logprintf(NSD_LOG_MIN, "list (winbind) table not defined\n");
562
rq->f_status = NS_BADREQ;
566
nsd_logprintf(NSD_LOG_MIN, "list (winbind %s)\n",map);
568
return (do_list(0,rq));
572
do_list(int state, nsd_file_t *rq)
575
struct winbindd_request *request;
577
nsd_logprintf(NSD_LOG_MIN, "entering do_list (winbind) state = %d\n",state);
579
map = nsd_attr_fetch_string(rq->f_attrs, "table", (char*)0);
580
request = (struct winbindd_request *)nsd_calloc(1,sizeof(struct winbindd_request));
582
nsd_logprintf(NSD_LOG_RESOURCE,
583
"do_list (winbind): failed malloc\n");
587
if (strcasecmp(map,"passwd.byname") == 0) {
590
rq->f_cmd_data = (void *)WINBINDD_SETPWENT;
593
request->data.num_entries = MAX_GETPWENT_USERS;
594
rq->f_cmd_data = (void *)WINBINDD_GETPWENT;
597
rq->f_cmd_data = (void *)WINBINDD_ENDPWENT;
600
nsd_logprintf(NSD_LOG_MIN, "do_list (winbind) unknown state\n");
602
rq->f_status = NS_NOTFOUND;
605
} else if (strcasecmp(map,"group.byname") == 0) {
608
rq->f_cmd_data = (void *)WINBINDD_SETGRENT;
611
request->data.num_entries = MAX_GETGRENT_USERS;
612
rq->f_cmd_data = (void *)WINBINDD_GETGRENT;
615
rq->f_cmd_data = (void *)WINBINDD_ENDGRENT;
618
nsd_logprintf(NSD_LOG_MIN, "do_list (winbind) unknown state\n");
620
rq->f_status = NS_NOTFOUND;
625
* Don't understand this map - just return not found
627
nsd_logprintf(NSD_LOG_MIN, "do_list (winbind) unknown table\n");
629
rq->f_status = NS_NOTFOUND;
633
return(do_request(rq, request));
636
#endif /* HAVE_NS_API_H */