1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
|
/* Copyright (c) 1998 Free Software Foundation, Inc.
This file is part of the GNU C Library.
Contributed by Thorsten Kukuk <kukuk@vt.uni-paderborn.de>, 1998.
The GNU C Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public License as
published by the Free Software Foundation; either version 2 of the
License, or (at your option) any later version.
The GNU C Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with the GNU C Library; see the file COPYING.LIB. If not,
write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
Boston, MA 02111-1307, USA. */
#include <errno.h>
#include <error.h>
#include <fcntl.h>
#include <libintl.h>
#include <locale.h>
#include <pthread.h>
#include <pwd.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/poll.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/uio.h>
#include <sys/un.h>
#include "nscd.h"
#include "dbg_log.h"
/* Socket 0 in the array is named and exported into the file namespace
as a connection point for clients. There's a one to one
correspondence between sock[i] and read_polls[i]. */
static int sock[MAX_NUM_CONNECTIONS];
static int socks_active;
static struct pollfd read_polls[MAX_NUM_CONNECTIONS];
static pthread_mutex_t sock_lock = PTHREAD_MUTEX_INITIALIZER;
/* Cleanup. */
void
close_sockets (void)
{
int i;
if (debug_flag)
dbg_log (_("close_sockets called"));
pthread_mutex_lock (&sock_lock);
/* Close sockets. */
for (i = 0; i < MAX_NUM_CONNECTIONS; ++i)
if (sock[i] != 0)
{
if (close (sock[i]))
dbg_log (_("socket [%d|%d] close: %s"), i, sock[i], strerror (errno));
sock[i] = 0;
read_polls[i].fd = -1;
--socks_active;
}
pthread_mutex_unlock (&sock_lock);
}
void
close_socket (int conn)
{
if (debug_flag > 2)
dbg_log (_("close socket (%d|%d)"), conn, sock[conn]);
pthread_mutex_lock (&sock_lock);
close (sock[conn]);
sock[conn] = 0;
read_polls[conn].fd = -1;
--socks_active;
pthread_mutex_unlock (&sock_lock);
}
/* Local routine, assigns a socket to a new connection request. */
static void
handle_new_connection (void)
{
int i;
if (debug_flag > 2)
dbg_log (_("handle_new_connection"));
pthread_mutex_lock (&sock_lock);
if (socks_active < MAX_NUM_CONNECTIONS)
/* Find a free socket entry to use. */
for (i = 1; i < MAX_NUM_CONNECTIONS; ++i)
{
if (sock[i] == 0)
{
if ((sock[i] = accept (sock[0], NULL, NULL)) < 0)
{
dbg_log (_("socket accept: %s"), strerror (errno));
return;
}
++socks_active;
read_polls[i].fd = sock[i];
read_polls[i].events = POLLRDNORM;
if (debug_flag > 2)
dbg_log (_("handle_new_connection used socket %d|%d"), i,
sock[i]);
break;
}
}
else
{
int black_widow_sock;
dbg_log (_("Supported number of simultaneous connections exceeded"));
dbg_log (_("Ignoring client connect request"));
/* There has to be a better way to ignore a connection request,..
when I get my hands on a sockets wiz I'll modify this. */
black_widow_sock = accept (sock[0], NULL, NULL);
close (black_widow_sock);
}
pthread_mutex_unlock (&sock_lock);
}
/* Local routine, reads a request off a socket indicated by read_polls. */
static int
handle_new_request (int **connp, request_header **reqp, char **key)
{
ssize_t nbytes;
int i, found = 0;
if (debug_flag)
dbg_log ("handle_new_request");
/* Find the descriptor. */
for (i = 1; i < MAX_NUM_CONNECTIONS; ++i) {
if (read_polls[i].fd >= 0
&& read_polls[i].revents & (POLLRDNORM|POLLERR|POLLNVAL))
{
found = i;
break;
}
if (read_polls[i].fd >= 0 && (read_polls[i].revents & POLLHUP))
{
/* Don't close the socket, we still need to send data. Just
stop polling for more data now. */
read_polls[i].fd = -1;
}
}
if (found == 0)
{
dbg_log (_("No sockets with data found !"));
return -1;
}
if (debug_flag > 2)
dbg_log (_("handle_new_request uses socket %d"), i);
/* Read from it. */
nbytes = read (sock[i], *reqp, sizeof (request_header));
if (nbytes != sizeof (request_header))
{
/* Handle non-data read cases. */
if (nbytes == 0)
{
/* Close socket down. */
if (debug_flag > 2)
dbg_log (_("Real close socket %d|%d"), i, sock[i]);
pthread_mutex_lock (&sock_lock);
read_polls[i].fd = -1;
close (sock[i]);
sock[i] = 0;
--socks_active;
pthread_mutex_unlock (&sock_lock);
}
else
if (nbytes < 0)
{
dbg_log (_("Read(%d|%d) error on get request: %s"),
i, sock[i], strerror (errno));
exit (1);
}
else
dbg_log (_("Read, data < request buf size, ignoring data"));
return -1;
}
else
{
*key = malloc ((*reqp)->key_len + 1);
/* Read the key from it */
nbytes = read (sock[i], *key, (*reqp)->key_len);
if (nbytes != (*reqp)->key_len)
{
/* Handle non-data read cases. */
if (nbytes == 0)
{
/* Close socket down. */
if (debug_flag > 2)
dbg_log (_("Real close socket %d|%d"), i, sock[i]);
pthread_mutex_lock (&sock_lock);
read_polls[i].fd = -1;
close (sock[i]);
sock[i] = 0;
--socks_active;
pthread_mutex_unlock (&sock_lock);
}
else
if (nbytes < 0)
{
perror (_("Read() error on get request"));
return 0;
}
else
fputs (_("Read, data < request buf size, ignoring data"),
stderr);
free (*key);
return -1;
}
else
{
/* Ok, have a live one, A real data req buf has been obtained. */
(*key)[(*reqp)->key_len] = '\0';
**connp = i;
return 0;
}
}
}
void
get_request (int *conn, request_header *req, char **key)
{
int done = 0;
int nr;
if (debug_flag)
dbg_log ("get_request");
/* loop, processing new connection requests until a client buffer
is read in on an existing connection. */
while (!done)
{
/* Poll active connections. */
nr = poll (read_polls, MAX_NUM_CONNECTIONS, -1);
if (nr <= 0)
{
perror (_("Poll new reads"));
exit (1);
}
if (read_polls[0].revents & (POLLRDNORM|POLLERR|POLLHUP|POLLNVAL))
/* Handle the case of a new connection request on the named socket. */
handle_new_connection ();
else
{
/* Read data from client specific descriptor. */
if (handle_new_request (&conn, &req, key) == 0)
done = 1;
}
} /* While not_done. */
}
void
init_sockets (void)
{
struct sockaddr_un sock_addr;
int i;
/* Initialize the connections db. */
socks_active = 0;
/* Initialize the poll array. */
for (i = 0; i < MAX_NUM_CONNECTIONS; i++)
read_polls[i].fd = -1;
/* Create the socket. */
sock[0] = socket (AF_UNIX, SOCK_STREAM, 0);
if (sock[0] < 0)
{
perror (_("cannot create socket"));
exit (1);
}
/* Bind a name to the socket. */
sock_addr.sun_family = AF_UNIX;
strcpy (sock_addr.sun_path, _PATH_NSCDSOCKET);
if (bind (sock[0], (struct sockaddr *) &sock_addr, sizeof (sock_addr)) < 0)
{
dbg_log ("%s: %s", _PATH_NSCDSOCKET, strerror (errno));
exit (1);
}
/* Set permissions for the socket. */
chmod (_PATH_NSCDSOCKET, 0666);
/* Set the socket up to accept connections. */
if (listen (sock[0], MAX_NUM_CONNECTIONS) < 0)
{
perror (_("cannot enable socket to accept connections"));
exit (1);
}
/* Add the socket to the server's set of active sockets. */
read_polls[0].fd = sock[0];
read_polls[0].events = POLLRDNORM;
++socks_active;
}
void
pw_send_answer (int conn, struct passwd *pwd)
{
struct iovec vec[6];
pw_response_header resp;
size_t total_len;
int nblocks;
resp.version = NSCD_VERSION;
if (pwd != NULL)
{
resp.found = 1;
resp.pw_name_len = strlen (pwd->pw_name);
resp.pw_passwd_len = strlen (pwd->pw_passwd);
resp.pw_uid = pwd->pw_uid;
resp.pw_gid = pwd->pw_gid;
resp.pw_gecos_len = strlen (pwd->pw_gecos);
resp.pw_dir_len = strlen (pwd->pw_dir);
resp.pw_shell_len = strlen (pwd->pw_shell);
}
else
{
resp.found = 0;
resp.pw_name_len = 0;
resp.pw_passwd_len = 0;
resp.pw_uid = -1;
resp.pw_gid = -1;
resp.pw_gecos_len = 0;
resp.pw_dir_len = 0;
resp.pw_shell_len = 0;
}
if (sock[conn] == 0)
{
dbg_log (_("bad connection id on send response [%d|%d]"),
conn, sock[conn]);
return;
}
/* Add response header. */
vec[0].iov_base = &resp;
vec[0].iov_len = sizeof (pw_response_header);
total_len = sizeof (pw_response_header);
nblocks = 1;
if (resp.found)
{
/* Add pw_name. */
vec[1].iov_base = pwd->pw_name;
vec[1].iov_len = resp.pw_name_len;
total_len += resp.pw_name_len;
/* Add pw_passwd. */
vec[2].iov_base = pwd->pw_passwd;
vec[2].iov_len = resp.pw_passwd_len;
total_len += resp.pw_passwd_len;
/* Add pw_gecos. */
vec[3].iov_base = pwd->pw_gecos;
vec[3].iov_len = resp.pw_gecos_len;
total_len += resp.pw_gecos_len;
/* Add pw_dir. */
vec[4].iov_base = pwd->pw_dir;
vec[4].iov_len = resp.pw_dir_len;
total_len += resp.pw_dir_len;
/* Add pw_shell. */
vec[5].iov_base = pwd->pw_shell;
vec[5].iov_len = resp.pw_shell_len;
total_len += resp.pw_shell_len;
nblocks = 6;
}
/* Send all the data. */
if (writev (sock[conn], vec, nblocks) != total_len)
dbg_log (_("write incomplete on send passwd answer: %s"),
strerror (errno));
}
void
pw_send_disabled (int conn)
{
pw_response_header resp;
resp.version = NSCD_VERSION;
resp.found = -1;
resp.pw_name_len = 0;
resp.pw_passwd_len = 0;
resp.pw_uid = -1;
resp.pw_gid = -1;
resp.pw_gecos_len = 0;
resp.pw_dir_len = 0;
resp.pw_shell_len = 0;
if (sock[conn] == 0)
{
dbg_log (_("bad connection id on send response [%d|%d]"),
conn, sock[conn]);
return;
}
/* Send response header. */
if (write (sock[conn], &resp, sizeof (pw_response_header))
!= sizeof (pw_response_header))
dbg_log (_("write incomplete on send response: %s"), strerror (errno));
}
void
gr_send_answer (int conn, struct group *grp)
{
struct iovec *vec;
size_t *len;
gr_response_header resp;
size_t total_len;
int nblocks;
resp.version = NSCD_VERSION;
if (grp != NULL)
{
resp.found = 1;
resp.gr_name_len = strlen (grp->gr_name);
resp.gr_passwd_len = strlen (grp->gr_passwd);
resp.gr_gid = grp->gr_gid;
resp.gr_mem_len = 0;
while (grp->gr_mem[resp.gr_mem_len])
++resp.gr_mem_len;
}
else
{
resp.found = 0;
resp.gr_name_len = 0;
resp.gr_passwd_len = 0;
resp.gr_gid = -1;
resp.gr_mem_len = 0;
}
if (sock[conn] == 0)
{
dbg_log (_("bad connection id on send response [%d|%d]"),
conn, sock[conn]);
return;
}
/* We have no fixed number of records so allocate the IOV here. */
vec = alloca ((3 + 1 + resp.gr_mem_len) * sizeof (struct iovec));
len = alloca (resp.gr_mem_len * sizeof (size_t));
/* Add response header. */
vec[0].iov_base = &resp;
vec[0].iov_len = sizeof (gr_response_header);
total_len = sizeof (gr_response_header);
nblocks = 1;
if (resp.found)
{
unsigned int l = 0;
/* Add gr_name. */
vec[1].iov_base = grp->gr_name;
vec[1].iov_len = resp.gr_name_len;
total_len += resp.gr_name_len;
/* Add gr_passwd. */
vec[2].iov_base = grp->gr_passwd;
vec[2].iov_len = resp.gr_passwd_len;
total_len += resp.gr_passwd_len;
nblocks = 3;
if (grp->gr_mem[l])
{
vec[3].iov_base = len;
vec[3].iov_len = resp.gr_mem_len * sizeof (size_t);
total_len += resp.gr_mem_len * sizeof (size_t);
nblocks = 4;
do
{
len[l] = strlen (grp->gr_mem[l]);
vec[nblocks].iov_base = grp->gr_mem[l];
vec[nblocks].iov_len = len[l];
total_len += len[l];
++nblocks;
}
while (grp->gr_mem[++l]);
}
}
/* Send all the data. */
while (nblocks > UIO_MAXIOV)
{
if (writev (sock[conn], vec, UIO_MAXIOV) != total_len)
dbg_log (_("write incomplete on send group answer: %s"),
strerror (errno));
vec += UIO_MAXIOV;
nblocks -= UIO_MAXIOV;
}
if (writev (sock[conn], vec, nblocks) != total_len)
dbg_log (_("write incomplete on send group answer: %s"),
strerror (errno));
}
void
gr_send_disabled (int conn)
{
gr_response_header resp;
resp.version = NSCD_VERSION;
resp.found = -1;
resp.gr_name_len = 0;
resp.gr_passwd_len = 0;
resp.gr_gid = -1;
resp.gr_mem_len = 0;
if (sock[conn] == 0)
{
dbg_log (_("bad connection id on send gr_disabled response [%d|%d]"),
conn, sock[conn]);
return;
}
/* Send response header. */
if (write (sock[conn], &resp, sizeof (gr_response_header))
!= sizeof (gr_response_header))
dbg_log (_("write incomplete on send gr_disabled response: %s"),
strerror (errno));
}
void
stat_send (int conn, stat_response_header *resp)
{
if (sock[conn] == 0)
{
dbg_log (_("bad connection id on send stat response [%d|%d]"),
conn, sock[conn]);
return;
}
/* send response header. */
if (write (sock[conn], resp, sizeof (stat_response_header))
!= sizeof (stat_response_header))
dbg_log (_("write incomplete on send stat response: %s"),
strerror (errno));
}
|