identd.c revision 1.24 1 1.24 dsl /* $NetBSD: identd.c,v 1.24 2004/11/05 21:56:01 dsl Exp $ */
2 1.8 mrg
3 1.1 cgd /*
4 1.20 christos * identd.c - TCP/IP Ident protocol server.
5 1.20 christos *
6 1.20 christos * This software is in the public domain.
7 1.20 christos * Written by Peter Postma <peter (at) pointless.nl>
8 1.20 christos */
9 1.20 christos
10 1.20 christos #include <sys/types.h>
11 1.20 christos #include <sys/socket.h>
12 1.20 christos #include <sys/stat.h>
13 1.20 christos #include <sys/param.h>
14 1.20 christos #include <sys/sysctl.h>
15 1.20 christos #include <sys/wait.h>
16 1.20 christos
17 1.20 christos #include <netinet/in.h>
18 1.20 christos #include <netinet/ip_var.h>
19 1.20 christos #include <netinet/tcp.h>
20 1.20 christos #include <netinet/tcp_timer.h>
21 1.20 christos #include <netinet/tcp_var.h>
22 1.20 christos
23 1.20 christos #include <arpa/inet.h>
24 1.9 msaitoh
25 1.9 msaitoh #include <ctype.h>
26 1.20 christos #include <err.h>
27 1.9 msaitoh #include <errno.h>
28 1.20 christos #include <fcntl.h>
29 1.20 christos #include <grp.h>
30 1.9 msaitoh #include <netdb.h>
31 1.20 christos #include <poll.h>
32 1.20 christos #include <pwd.h>
33 1.9 msaitoh #include <signal.h>
34 1.20 christos #include <stdio.h>
35 1.20 christos #include <stdlib.h>
36 1.20 christos #include <string.h>
37 1.20 christos #include <syslog.h>
38 1.20 christos #include <unistd.h>
39 1.20 christos
40 1.24 dsl __RCSID("$NetBSD: identd.c,v 1.24 2004/11/05 21:56:01 dsl Exp $");
41 1.21 christos
42 1.20 christos #define OPSYS_NAME "UNIX"
43 1.20 christos #define IDENT_SERVICE "auth"
44 1.20 christos #define TIMEOUT 30 /* seconds */
45 1.20 christos
46 1.20 christos static int idhandle(int, const char *, const char *, const char *,
47 1.20 christos const char *, int);
48 1.20 christos static void idparse(int, int, int, const char *, const char *, const char *);
49 1.20 christos static void iderror(int, int, int, const char *);
50 1.20 christos static const char *gethost(struct sockaddr_storage *);
51 1.20 christos static int *socketsetup(const char *, const char *, int);
52 1.20 christos static int sysctl_getuid(struct sockaddr_storage *, socklen_t, uid_t *);
53 1.20 christos static int check_noident(const char *);
54 1.20 christos static int check_userident(const char *, char *, size_t);
55 1.20 christos static void random_string(char *, size_t);
56 1.20 christos static void change_format(const char *, struct passwd *, char *, size_t);
57 1.20 christos static void timeout_handler(int);
58 1.20 christos static void waitchild(int);
59 1.20 christos static void fatal(const char *);
60 1.20 christos
61 1.20 christos static int bflag, eflag, fflag, Fflag, iflag, Iflag;
62 1.20 christos static int lflag, Lflag, nflag, Nflag, rflag;
63 1.20 christos
64 1.20 christos int
65 1.20 christos main(int argc, char *argv[])
66 1.20 christos {
67 1.20 christos int IPv4or6, ch, *socks, timeout;
68 1.20 christos char *address, *charset, *fmt;
69 1.20 christos const char *osname, *portno;
70 1.20 christos char *p;
71 1.20 christos char user[LOGIN_NAME_MAX];
72 1.20 christos struct group *grp;
73 1.20 christos struct passwd *pw;
74 1.20 christos gid_t gid;
75 1.20 christos uid_t uid;
76 1.20 christos
77 1.20 christos IPv4or6 = AF_UNSPEC;
78 1.20 christos osname = OPSYS_NAME;
79 1.20 christos portno = IDENT_SERVICE;
80 1.20 christos timeout = TIMEOUT;
81 1.20 christos address = NULL;
82 1.20 christos charset = NULL;
83 1.20 christos fmt = NULL;
84 1.20 christos socks = NULL;
85 1.20 christos gid = uid = 0;
86 1.20 christos bflag = eflag = fflag = Fflag = iflag = Iflag = 0;
87 1.20 christos lflag = Lflag = nflag = Nflag = rflag = 0;
88 1.20 christos
89 1.20 christos /* Started from a tty? then run as daemon */
90 1.20 christos if (isatty(0))
91 1.20 christos bflag = 1;
92 1.20 christos
93 1.20 christos /* Parse arguments */
94 1.20 christos while ((ch = getopt(argc, argv, "46a:bceF:f:g:IiL:lNno:p:rt:u:")) != -1)
95 1.20 christos switch (ch) {
96 1.20 christos case '4':
97 1.20 christos IPv4or6 = AF_INET;
98 1.20 christos break;
99 1.20 christos case '6':
100 1.20 christos IPv4or6 = AF_INET6;
101 1.20 christos break;
102 1.20 christos case 'a':
103 1.20 christos address = optarg;
104 1.20 christos break;
105 1.20 christos case 'b':
106 1.20 christos bflag = 1;
107 1.20 christos break;
108 1.20 christos case 'c':
109 1.20 christos charset = optarg;
110 1.20 christos break;
111 1.20 christos case 'e':
112 1.20 christos eflag = 1;
113 1.20 christos break;
114 1.20 christos case 'F':
115 1.20 christos Fflag = 1;
116 1.20 christos fmt = optarg;
117 1.20 christos break;
118 1.20 christos case 'f':
119 1.20 christos fflag = 1;
120 1.20 christos (void)strlcpy(user, optarg, sizeof(user));
121 1.20 christos break;
122 1.20 christos case 'g':
123 1.20 christos gid = (gid_t)strtol(optarg, &p, 0);
124 1.20 christos if (*p != '\0') {
125 1.20 christos if ((grp = getgrnam(optarg)) != NULL)
126 1.20 christos gid = grp->gr_gid;
127 1.20 christos else
128 1.20 christos errx(1, "No such group '%s'", optarg);
129 1.20 christos }
130 1.20 christos break;
131 1.20 christos case 'I':
132 1.20 christos Iflag = 1;
133 1.20 christos /* FALLTHROUGH */
134 1.20 christos case 'i':
135 1.20 christos iflag = 1;
136 1.20 christos break;
137 1.20 christos case 'L':
138 1.20 christos Lflag = 1;
139 1.20 christos (void)strlcpy(user, optarg, sizeof(user));
140 1.20 christos break;
141 1.20 christos case 'l':
142 1.20 christos lflag = 1;
143 1.20 christos break;
144 1.20 christos case 'N':
145 1.20 christos Nflag = 1;
146 1.20 christos break;
147 1.20 christos case 'n':
148 1.20 christos nflag = 1;
149 1.20 christos break;
150 1.20 christos case 'o':
151 1.20 christos osname = optarg;
152 1.20 christos break;
153 1.20 christos case 'p':
154 1.20 christos portno = optarg;
155 1.20 christos break;
156 1.20 christos case 'r':
157 1.20 christos rflag = 1;
158 1.20 christos break;
159 1.20 christos case 't':
160 1.20 christos timeout = (int)strtol(optarg, &p, 0);
161 1.20 christos if (*p != '\0')
162 1.20 christos errx(1, "Invalid timeout value '%s'", optarg);
163 1.20 christos break;
164 1.20 christos case 'u':
165 1.20 christos uid = (uid_t)strtol(optarg, &p, 0);
166 1.20 christos if (*p != '\0') {
167 1.20 christos if ((pw = getpwnam(optarg)) != NULL) {
168 1.20 christos uid = pw->pw_uid;
169 1.20 christos gid = pw->pw_gid;
170 1.20 christos } else
171 1.20 christos errx(1, "No such user '%s'", optarg);
172 1.20 christos }
173 1.20 christos break;
174 1.20 christos default:
175 1.20 christos exit(1);
176 1.20 christos }
177 1.20 christos
178 1.20 christos if (lflag)
179 1.20 christos openlog("identd", LOG_PID, LOG_DAEMON);
180 1.20 christos
181 1.20 christos /* Setup sockets if -b flag */
182 1.20 christos if (bflag) {
183 1.20 christos socks = socketsetup(address, portno, IPv4or6);
184 1.20 christos if (socks == NULL)
185 1.20 christos return 1;
186 1.20 christos }
187 1.20 christos
188 1.20 christos /* Switch to another uid/gid ? */
189 1.20 christos if (gid && setgid(gid) == -1) {
190 1.20 christos if (lflag)
191 1.20 christos syslog(LOG_ERR, "setgid: %m");
192 1.20 christos if (bflag)
193 1.20 christos warn("setgid");
194 1.20 christos exit(1);
195 1.20 christos }
196 1.20 christos if (uid && setuid(uid) == -1) {
197 1.20 christos if (lflag)
198 1.20 christos syslog(LOG_ERR, "setuid: %m");
199 1.20 christos if (bflag)
200 1.20 christos warn("setuid");
201 1.20 christos exit(1);
202 1.20 christos }
203 1.20 christos
204 1.20 christos /* Daemonize, setup pollfds and start mainloop if -b flag */
205 1.20 christos if (bflag) {
206 1.20 christos int fd, i, nfds, rv;
207 1.20 christos struct pollfd *rfds;
208 1.20 christos
209 1.20 christos (void)signal(SIGCHLD, waitchild);
210 1.20 christos (void)daemon(0, 0);
211 1.20 christos
212 1.20 christos rfds = malloc(*socks * sizeof(struct pollfd));
213 1.20 christos if (rfds == NULL)
214 1.20 christos fatal("malloc");
215 1.20 christos nfds = *socks;
216 1.20 christos for (i = 0; i < nfds; i++) {
217 1.20 christos rfds[i].fd = socks[i+1];
218 1.20 christos rfds[i].events = POLLIN;
219 1.20 christos rfds[i].revents = 0;
220 1.20 christos }
221 1.20 christos /* Mainloop for daemon */
222 1.20 christos for (;;) {
223 1.20 christos rv = poll(rfds, nfds, INFTIM);
224 1.20 christos if (rv < 0 && errno == EINTR)
225 1.20 christos continue;
226 1.20 christos if (rv < 0)
227 1.20 christos fatal("poll");
228 1.20 christos for (i = 0; i < nfds; i++) {
229 1.20 christos if (rfds[i].revents & POLLIN) {
230 1.20 christos fd = accept(rfds[i].fd, NULL, NULL);
231 1.20 christos if (fd < 0) {
232 1.20 christos if (lflag)
233 1.20 christos syslog(LOG_ERR,
234 1.20 christos "accept: %m");
235 1.20 christos continue;
236 1.20 christos }
237 1.20 christos switch (fork()) {
238 1.20 christos case -1: /* error */
239 1.20 christos if (lflag)
240 1.20 christos syslog(LOG_ERR,
241 1.20 christos "fork: %m");
242 1.20 christos (void)sleep(1);
243 1.20 christos break;
244 1.20 christos case 0: /* child */
245 1.20 christos (void)idhandle(fd, charset,
246 1.20 christos fmt, osname, user, timeout);
247 1.20 christos _exit(0);
248 1.20 christos default: /* parent */
249 1.20 christos (void)close(fd);
250 1.20 christos }
251 1.20 christos }
252 1.20 christos }
253 1.20 christos }
254 1.20 christos } else
255 1.20 christos (void)idhandle(STDIN_FILENO, charset, fmt, osname, user,
256 1.20 christos timeout);
257 1.20 christos
258 1.20 christos return 0;
259 1.20 christos }
260 1.20 christos
261 1.20 christos static int
262 1.20 christos idhandle(int fd, const char *charset, const char *fmt, const char *osname,
263 1.20 christos const char *user, int timeout)
264 1.20 christos {
265 1.20 christos struct sockaddr_storage ss[2];
266 1.22 christos char userbuf[LOGIN_NAME_MAX]; /* actual user name (or numeric uid) */
267 1.22 christos char idbuf[LOGIN_NAME_MAX]; /* name to be used in response */
268 1.20 christos char buf[BUFSIZ], *p;
269 1.20 christos int n, lport, fport;
270 1.20 christos struct passwd *pw;
271 1.20 christos socklen_t len;
272 1.20 christos uid_t uid;
273 1.20 christos
274 1.20 christos lport = fport = 0;
275 1.20 christos
276 1.20 christos (void)strlcpy(idbuf, user, sizeof(idbuf));
277 1.20 christos (void)signal(SIGALRM, timeout_handler);
278 1.20 christos (void)alarm(timeout);
279 1.20 christos
280 1.20 christos /* Get foreign internet address */
281 1.20 christos len = sizeof(ss[0]);
282 1.20 christos if (getpeername(fd, (struct sockaddr *)&ss[0], &len) < 0)
283 1.20 christos fatal("getpeername");
284 1.20 christos
285 1.20 christos if (lflag)
286 1.20 christos syslog(LOG_INFO, "Connection from %s", gethost(&ss[0]));
287 1.20 christos
288 1.20 christos /* Get local internet address */
289 1.20 christos len = sizeof(ss[1]);
290 1.20 christos if (getsockname(fd, (struct sockaddr *)&ss[1], &len) < 0)
291 1.20 christos fatal("getsockname");
292 1.20 christos
293 1.20 christos /* Be sure to have the same address family's */
294 1.20 christos if (ss[0].ss_family != ss[1].ss_family) {
295 1.20 christos if (lflag)
296 1.20 christos syslog(LOG_ERR, "Foreign/local AF are different!");
297 1.20 christos return 1;
298 1.20 christos }
299 1.20 christos
300 1.20 christos /* Receive data from the client */
301 1.20 christos if ((n = recv(fd, buf, sizeof(buf) - 1, 0)) < 0) {
302 1.20 christos fatal("recv");
303 1.20 christos } else if (n == 0) {
304 1.20 christos if (lflag)
305 1.20 christos syslog(LOG_NOTICE, "recv: EOF");
306 1.20 christos iderror(fd, 0, 0, "UNKNOWN-ERROR");
307 1.20 christos return 1;
308 1.20 christos }
309 1.20 christos buf[n] = '\0';
310 1.20 christos
311 1.20 christos /* Get local and remote ports from the received data */
312 1.20 christos p = buf;
313 1.24 dsl while (*p != '\0' && isspace((unsigned char)*p))
314 1.20 christos p++;
315 1.20 christos if ((p = strtok(p, " \t,")) != NULL) {
316 1.20 christos lport = atoi(p);
317 1.20 christos if ((p = strtok(NULL, " \t,")) != NULL)
318 1.20 christos fport = atoi(p);
319 1.20 christos }
320 1.20 christos
321 1.20 christos /* Are the ports valid? */
322 1.20 christos if (lport < 1 || lport > 65535 || fport < 1 || fport > 65535) {
323 1.20 christos if (lflag)
324 1.20 christos syslog(LOG_NOTICE, "Invalid port(s): %d, %d from %s",
325 1.20 christos lport, fport, gethost(&ss[0]));
326 1.20 christos iderror(fd, 0, 0, eflag ? "UNKNOWN-ERROR" : "INVALID-PORT");
327 1.20 christos return 1;
328 1.20 christos }
329 1.20 christos
330 1.20 christos /* If there is a 'lie' user enabled, then handle it now and quit */
331 1.20 christos if (Lflag) {
332 1.20 christos if (lflag)
333 1.20 christos syslog(LOG_NOTICE, "Lying with name %s to %s",
334 1.20 christos idbuf, gethost(&ss[0]));
335 1.20 christos idparse(fd, lport, fport, charset, osname, idbuf);
336 1.20 christos return 0;
337 1.20 christos }
338 1.20 christos
339 1.20 christos /* Protocol dependent stuff */
340 1.20 christos switch (ss[0].ss_family) {
341 1.20 christos case AF_INET:
342 1.20 christos ((struct sockaddr_in *)&ss[0])->sin_port = htons(fport);
343 1.20 christos ((struct sockaddr_in *)&ss[1])->sin_port = htons(lport);
344 1.20 christos break;
345 1.20 christos case AF_INET6:
346 1.20 christos ((struct sockaddr_in6 *)&ss[0])->sin6_port = htons(fport);
347 1.20 christos ((struct sockaddr_in6 *)&ss[1])->sin6_port = htons(lport);
348 1.20 christos break;
349 1.20 christos default:
350 1.20 christos if (lflag)
351 1.20 christos syslog(LOG_ERR, "Unsupported protocol, proto no. %d",
352 1.20 christos ss[0].ss_family);
353 1.20 christos return 1;
354 1.20 christos }
355 1.20 christos
356 1.20 christos /* Do sysctl call */
357 1.20 christos if (sysctl_getuid(ss, sizeof(ss), &uid) == -1) {
358 1.20 christos if (lflag)
359 1.20 christos syslog(LOG_ERR, "sysctl: %m");
360 1.20 christos if (fflag) {
361 1.20 christos if (lflag)
362 1.20 christos syslog(LOG_NOTICE, "Using fallback name %s "
363 1.20 christos "to %s", idbuf, gethost(&ss[0]));
364 1.20 christos idparse(fd, lport, fport, charset, osname, idbuf);
365 1.20 christos return 0;
366 1.20 christos }
367 1.20 christos iderror(fd, lport, fport, eflag ? "UNKNOWN-ERROR" : "NO-USER");
368 1.20 christos return 1;
369 1.20 christos }
370 1.20 christos
371 1.22 christos /* Fill in userbuf with user name if possible, else numeric uid */
372 1.20 christos if ((pw = getpwuid(uid)) == NULL) {
373 1.20 christos if (lflag)
374 1.20 christos syslog(LOG_ERR, "Couldn't map uid (%u) to name", uid);
375 1.22 christos (void)snprintf(userbuf, sizeof(userbuf), "%u", uid);
376 1.22 christos } else {
377 1.22 christos if (lflag)
378 1.22 christos syslog(LOG_INFO, "Successfull lookup: %d, %d: %s for %s",
379 1.22 christos lport, fport, pw->pw_name, gethost(&ss[0]));
380 1.22 christos (void)strlcpy(userbuf, pw->pw_name, sizeof(userbuf));
381 1.20 christos }
382 1.20 christos
383 1.20 christos /* No ident enabled? */
384 1.22 christos if (Nflag && pw && check_noident(pw->pw_dir)) {
385 1.20 christos if (lflag)
386 1.20 christos syslog(LOG_NOTICE, "Returning HIDDEN-USER for user %s"
387 1.20 christos " to %s", pw->pw_name, gethost(&ss[0]));
388 1.20 christos iderror(fd, lport, fport, "HIDDEN-USER");
389 1.20 christos return 1;
390 1.20 christos }
391 1.20 christos
392 1.20 christos /* User ident enabled ? */
393 1.22 christos if (iflag && pw && check_userident(pw->pw_dir, idbuf, sizeof(idbuf))) {
394 1.20 christos if (!Iflag) {
395 1.22 christos if ((strspn(idbuf, "0123456789") &&
396 1.20 christos getpwuid(atoi(idbuf)) != NULL)
397 1.22 christos || (getpwnam(idbuf) != NULL)) {
398 1.22 christos if (lflag)
399 1.22 christos syslog(LOG_NOTICE,
400 1.22 christos "Ignoring user-specified '%s' for "
401 1.22 christos "user %s", idbuf, userbuf);
402 1.20 christos (void)strlcpy(idbuf, userbuf, sizeof(idbuf));
403 1.22 christos }
404 1.20 christos }
405 1.20 christos if (lflag)
406 1.22 christos syslog(LOG_NOTICE, "Returning user-specified '%s' for "
407 1.20 christos "user %s to %s", idbuf, userbuf, gethost(&ss[0]));
408 1.20 christos idparse(fd, lport, fport, charset, osname, idbuf);
409 1.20 christos return 0;
410 1.20 christos }
411 1.20 christos
412 1.20 christos /* Send random crap? */
413 1.20 christos if (rflag) {
414 1.20 christos /* Random number or string? */
415 1.20 christos if (nflag)
416 1.20 christos (void)snprintf(idbuf, sizeof(idbuf), "%u",
417 1.20 christos (unsigned int)(arc4random() % 65535));
418 1.20 christos else
419 1.20 christos random_string(idbuf, sizeof(idbuf));
420 1.20 christos
421 1.20 christos if (lflag)
422 1.20 christos syslog(LOG_NOTICE, "Returning random '%s' for user %s"
423 1.22 christos " to %s", idbuf, userbuf, gethost(&ss[0]));
424 1.20 christos idparse(fd, lport, fport, charset, osname, idbuf);
425 1.20 christos return 0;
426 1.20 christos }
427 1.20 christos
428 1.20 christos /* Return number? */
429 1.20 christos if (nflag)
430 1.20 christos (void)snprintf(idbuf, sizeof(idbuf), "%u", uid);
431 1.20 christos else
432 1.22 christos (void)strlcpy(idbuf, userbuf, sizeof(idbuf));
433 1.20 christos
434 1.20 christos if (Fflag) {
435 1.20 christos /* RFC 1413 says that 512 is the limit */
436 1.20 christos change_format(fmt, pw, buf, 512);
437 1.20 christos idparse(fd, lport, fport, charset, osname, buf);
438 1.20 christos } else
439 1.20 christos idparse(fd, lport, fport, charset, osname, idbuf);
440 1.20 christos
441 1.20 christos return 0;
442 1.20 christos }
443 1.20 christos
444 1.20 christos /* Send/parse the ident result */
445 1.20 christos static void
446 1.20 christos idparse(int fd, int lport, int fport, const char *charset, const char *osname,
447 1.20 christos const char *user)
448 1.20 christos {
449 1.20 christos char *p;
450 1.9 msaitoh
451 1.23 kim if (asprintf(&p, "%d,%d:USERID:%s%s%s:%s\r\n", lport, fport,
452 1.23 kim osname, charset ? "," : "", charset ? charset : "", user) < 0)
453 1.20 christos fatal("asprintf");
454 1.20 christos if (send(fd, p, strlen(p), 0) < 0) {
455 1.20 christos free(p);
456 1.20 christos fatal("send");
457 1.20 christos }
458 1.20 christos free(p);
459 1.20 christos }
460 1.1 cgd
461 1.20 christos /* Return a specified ident error */
462 1.20 christos static void
463 1.20 christos iderror(int fd, int lport, int fport, const char *error)
464 1.20 christos {
465 1.20 christos char *p;
466 1.1 cgd
467 1.23 kim if (asprintf(&p, "%d,%d:ERROR:%s\r\n", lport, fport, error) < 0)
468 1.20 christos fatal("asprintf");
469 1.20 christos if (send(fd, p, strlen(p), 0) < 0) {
470 1.20 christos free(p);
471 1.20 christos fatal("send");
472 1.20 christos }
473 1.20 christos free(p);
474 1.20 christos }
475 1.1 cgd
476 1.20 christos /* Return the IP address of the connecting host */
477 1.20 christos static const char *
478 1.20 christos gethost(struct sockaddr_storage *ss)
479 1.20 christos {
480 1.20 christos static char host[NI_MAXHOST];
481 1.8 mrg
482 1.20 christos if (getnameinfo((struct sockaddr *)ss, ss->ss_len, host,
483 1.20 christos sizeof(host), NULL, 0, NI_NUMERICHOST) == 0)
484 1.20 christos return host;
485 1.9 msaitoh
486 1.20 christos return "UNKNOWN";
487 1.9 msaitoh }
488 1.1 cgd
489 1.20 christos /* Setup sockets, for daemon mode */
490 1.20 christos static int *
491 1.20 christos socketsetup(const char *address, const char *port, int af)
492 1.20 christos {
493 1.20 christos struct addrinfo hints, *res, *res0;
494 1.20 christos int error, maxs, *s, *socks, y = 1;
495 1.20 christos const char *cause = NULL;
496 1.20 christos
497 1.20 christos (void)memset(&hints, 0, sizeof(hints));
498 1.20 christos hints.ai_flags = AI_PASSIVE;
499 1.20 christos hints.ai_family = af;
500 1.20 christos hints.ai_socktype = SOCK_STREAM;
501 1.20 christos error = getaddrinfo(address, port, &hints, &res0);
502 1.20 christos if (error) {
503 1.20 christos if (lflag)
504 1.20 christos syslog(LOG_ERR, "getaddrinfo: %s", gai_strerror(error));
505 1.20 christos errx(1, "%s", gai_strerror(error));
506 1.20 christos /*NOTREACHED*/
507 1.20 christos }
508 1.20 christos
509 1.20 christos /* Count max number of sockets we may open */
510 1.20 christos for (maxs = 0, res = res0; res != NULL; res = res->ai_next)
511 1.20 christos maxs++;
512 1.20 christos
513 1.20 christos socks = malloc((maxs + 1) * sizeof(int));
514 1.20 christos if (socks == NULL) {
515 1.20 christos if (lflag)
516 1.20 christos syslog(LOG_ERR, "malloc: %m");
517 1.20 christos err(1, "malloc");
518 1.20 christos /* NOTREACHED */
519 1.20 christos }
520 1.20 christos
521 1.20 christos *socks = 0;
522 1.20 christos s = socks + 1;
523 1.20 christos for (res = res0; res != NULL; res = res->ai_next) {
524 1.20 christos *s = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
525 1.20 christos if (*s < 0) {
526 1.20 christos cause = "socket";
527 1.20 christos continue;
528 1.20 christos }
529 1.20 christos (void)setsockopt(*s, SOL_SOCKET, SO_REUSEADDR, &y, sizeof(y));
530 1.20 christos if (bind(*s, res->ai_addr, res->ai_addrlen) < 0) {
531 1.20 christos cause = "bind";
532 1.20 christos (void)close(*s);
533 1.20 christos continue;
534 1.20 christos }
535 1.20 christos if (listen(*s, 5) < 0) {
536 1.20 christos cause = "listen";
537 1.20 christos (void)close(*s);
538 1.20 christos continue;
539 1.20 christos }
540 1.20 christos *socks = *socks + 1;
541 1.20 christos s++;
542 1.20 christos }
543 1.20 christos
544 1.20 christos if (*socks == 0) {
545 1.20 christos free(socks);
546 1.20 christos if (lflag)
547 1.20 christos syslog(LOG_ERR, "%s: %m", cause);
548 1.20 christos err(1, "%s", cause);
549 1.20 christos /* NOTREACHED */
550 1.20 christos }
551 1.20 christos if (res0)
552 1.20 christos freeaddrinfo(res0);
553 1.9 msaitoh
554 1.20 christos return socks;
555 1.1 cgd }
556 1.1 cgd
557 1.20 christos /* Return the UID for the connection owner */
558 1.20 christos static int
559 1.20 christos sysctl_getuid(struct sockaddr_storage *ss, socklen_t len, uid_t *uid)
560 1.1 cgd {
561 1.20 christos int mib[4];
562 1.20 christos uid_t myuid;
563 1.20 christos size_t uidlen;
564 1.20 christos
565 1.20 christos uidlen = sizeof(myuid);
566 1.20 christos
567 1.20 christos mib[0] = CTL_NET;
568 1.20 christos mib[1] = ss->ss_family;
569 1.20 christos mib[2] = IPPROTO_TCP;
570 1.20 christos mib[3] = TCPCTL_IDENT;
571 1.20 christos
572 1.20 christos if (sysctl(mib, sizeof(mib)/ sizeof(int), &myuid, &uidlen, ss, len) < 0)
573 1.20 christos return -1;
574 1.20 christos *uid = myuid;
575 1.20 christos
576 1.20 christos return 0;
577 1.1 cgd }
578 1.9 msaitoh
579 1.20 christos /* Check if a .noident file exists in the user home directory */
580 1.20 christos static int
581 1.20 christos check_noident(const char *homedir)
582 1.20 christos {
583 1.20 christos struct stat sb;
584 1.20 christos char *path;
585 1.20 christos int ret;
586 1.20 christos
587 1.20 christos if (homedir == NULL)
588 1.20 christos return 0;
589 1.20 christos if (asprintf(&path, "%s/.noident", homedir) < 0)
590 1.20 christos return 0;
591 1.20 christos ret = stat(path, &sb);
592 1.20 christos
593 1.20 christos free(path);
594 1.20 christos return (ret == 0);
595 1.20 christos }
596 1.1 cgd
597 1.1 cgd /*
598 1.20 christos * Check if a .ident file exists in the user home directory and
599 1.20 christos * return the contents of that file.
600 1.20 christos */
601 1.20 christos static int
602 1.20 christos check_userident(const char *homedir, char *username, size_t len)
603 1.20 christos {
604 1.20 christos struct stat sb;
605 1.20 christos char *path, *p;
606 1.20 christos int fd, n;
607 1.20 christos
608 1.20 christos if (len == 0 || homedir == NULL)
609 1.20 christos return 0;
610 1.20 christos if (asprintf(&path, "%s/.ident", homedir) < 0)
611 1.20 christos return 0;
612 1.20 christos if ((fd = open(path, O_RDONLY|O_NONBLOCK|O_NOFOLLOW, 0)) < 0) {
613 1.20 christos free(path);
614 1.20 christos return 0;
615 1.20 christos }
616 1.20 christos if (fstat(fd, &sb) < 0 || !S_ISREG(sb.st_mode)) {
617 1.20 christos (void)close(fd);
618 1.20 christos free(path);
619 1.20 christos return 0;
620 1.20 christos }
621 1.20 christos if ((n = read(fd, username, len - 1)) < 1) {
622 1.20 christos (void)close(fd);
623 1.20 christos free(path);
624 1.20 christos return 0;
625 1.20 christos }
626 1.20 christos username[n] = '\0';
627 1.20 christos
628 1.20 christos if ((p = strpbrk(username, "\r\n")))
629 1.20 christos *p = '\0';
630 1.20 christos
631 1.20 christos (void)close(fd);
632 1.20 christos free(path);
633 1.20 christos return 1;
634 1.1 cgd }
635 1.1 cgd
636 1.20 christos /* Generate a random string */
637 1.20 christos static void
638 1.20 christos random_string(char *str, size_t len)
639 1.20 christos {
640 1.20 christos static const char chars[] = "abcdefghijklmnopqrstuvwxyz1234567890";
641 1.20 christos char *p;
642 1.20 christos
643 1.20 christos if (len == 0)
644 1.20 christos return;
645 1.20 christos for (p = str; len > 1; len--)
646 1.20 christos *p++ = chars[arc4random() % (sizeof(chars) - 1)];
647 1.20 christos *p = '\0';
648 1.20 christos }
649 1.1 cgd
650 1.20 christos /* Change the output format */
651 1.20 christos static void
652 1.20 christos change_format(const char *format, struct passwd *pw, char *dest, size_t len)
653 1.20 christos {
654 1.20 christos struct group *gr;
655 1.20 christos const char *cp;
656 1.20 christos char **gmp;
657 1.20 christos int bp;
658 1.20 christos
659 1.20 christos if (len == 0)
660 1.20 christos return;
661 1.20 christos if ((gr = getgrgid(pw->pw_gid)) == NULL)
662 1.20 christos return;
663 1.20 christos
664 1.20 christos for (bp = 0, cp = format; *cp != '\0' && bp < 490; cp++) {
665 1.20 christos if (*cp != '%') {
666 1.20 christos dest[bp++] = *cp;
667 1.20 christos continue;
668 1.9 msaitoh }
669 1.20 christos if (*++cp == '\0')
670 1.20 christos break;
671 1.20 christos switch (*cp) {
672 1.20 christos case 'u':
673 1.20 christos (void)snprintf(&dest[bp], len - bp, "%.*s", 490 - bp,
674 1.20 christos pw->pw_name);
675 1.20 christos break;
676 1.20 christos case 'U':
677 1.20 christos (void)snprintf(&dest[bp], len - bp, "%d", pw->pw_uid);
678 1.20 christos break;
679 1.20 christos case 'g':
680 1.20 christos (void)snprintf(&dest[bp], len - bp, "%.*s", 490 - bp,
681 1.20 christos gr->gr_name);
682 1.20 christos break;
683 1.20 christos case 'G':
684 1.20 christos (void)snprintf(&dest[bp], len - bp, "%d", gr->gr_gid);
685 1.20 christos break;
686 1.20 christos case 'l':
687 1.20 christos (void)snprintf(&dest[bp], len - bp, "%.*s", 490 - bp,
688 1.20 christos gr->gr_name);
689 1.20 christos bp += strlen(&dest[bp]);
690 1.20 christos if (bp >= 490)
691 1.20 christos break;
692 1.20 christos setgrent();
693 1.20 christos while ((gr = getgrent()) != NULL) {
694 1.20 christos if (gr->gr_gid == pw->pw_gid)
695 1.20 christos continue;
696 1.20 christos for (gmp = gr->gr_mem; *gmp && **gmp; gmp++) {
697 1.20 christos if (strcmp(*gmp, pw->pw_name) == 0) {
698 1.20 christos (void)snprintf(&dest[bp],
699 1.20 christos len - bp, ",%.*s",
700 1.20 christos 490 - bp, gr->gr_name);
701 1.20 christos bp += strlen(&dest[bp]);
702 1.20 christos break;
703 1.20 christos }
704 1.20 christos }
705 1.20 christos if (bp >= 490)
706 1.20 christos break;
707 1.20 christos }
708 1.20 christos endgrent();
709 1.20 christos break;
710 1.20 christos case 'L':
711 1.20 christos (void)snprintf(&dest[bp], len - bp, "%u", gr->gr_gid);
712 1.20 christos bp += strlen(&dest[bp]);
713 1.20 christos if (bp >= 490)
714 1.20 christos break;
715 1.20 christos setgrent();
716 1.20 christos while ((gr = getgrent()) != NULL) {
717 1.20 christos if (gr->gr_gid == pw->pw_gid)
718 1.20 christos continue;
719 1.20 christos for (gmp = gr->gr_mem; *gmp && **gmp; gmp++) {
720 1.20 christos if (strcmp(*gmp, pw->pw_name) == 0) {
721 1.20 christos (void)snprintf(&dest[bp],
722 1.20 christos len - bp, ",%u",
723 1.20 christos gr->gr_gid);
724 1.20 christos bp += strlen(&dest[bp]);
725 1.20 christos break;
726 1.20 christos }
727 1.20 christos }
728 1.20 christos if (bp >= 490)
729 1.20 christos break;
730 1.20 christos }
731 1.20 christos endgrent();
732 1.20 christos break;
733 1.20 christos default:
734 1.20 christos dest[bp] = *cp;
735 1.20 christos dest[bp+1] = '\0';
736 1.20 christos break;
737 1.9 msaitoh }
738 1.20 christos bp += strlen(&dest[bp]);
739 1.20 christos }
740 1.20 christos if (bp >= 490) {
741 1.20 christos (void)snprintf(&dest[490], len - 490, "...");
742 1.20 christos bp = 493;
743 1.20 christos }
744 1.20 christos dest[bp] = '\0';
745 1.20 christos }
746 1.20 christos
747 1.20 christos /* Just exit when we caught SIGALRM */
748 1.20 christos static void
749 1.20 christos timeout_handler(int s)
750 1.20 christos {
751 1.20 christos if (lflag)
752 1.20 christos syslog(LOG_DEBUG, "SIGALRM triggered, exiting...");
753 1.20 christos exit(1);
754 1.20 christos }
755 1.20 christos
756 1.20 christos /* This is to clean up zombie processes when in daemon mode */
757 1.20 christos static void
758 1.20 christos waitchild(int s)
759 1.20 christos {
760 1.20 christos while (waitpid(-1, NULL, WNOHANG) > 0)
761 1.19 christos continue;
762 1.20 christos }
763 1.20 christos
764 1.20 christos /* Report errno through syslog and quit */
765 1.20 christos static void
766 1.20 christos fatal(const char *func)
767 1.20 christos {
768 1.20 christos if (lflag)
769 1.20 christos syslog(LOG_ERR, "%s: %m", func);
770 1.9 msaitoh exit(1);
771 1.1 cgd }
772