identd.c revision 1.21 1 1.21 christos /* $NetBSD: identd.c,v 1.21 2004/01/31 22:03:31 christos 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.21 christos __RCSID("$NetBSD: identd.c,v 1.21 2004/01/31 22:03:31 christos 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.20 christos char userbuf[LOGIN_NAME_MAX];
267 1.20 christos char idbuf[LOGIN_NAME_MAX];
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.20 christos while (*p != '\0' && isspace(*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.20 christos /* Get username with the 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.20 christos (void)snprintf(idbuf, sizeof(idbuf), "%u", uid);
376 1.20 christos idparse(fd, lport, fport, charset, osname, idbuf);
377 1.20 christos return 0;
378 1.20 christos }
379 1.20 christos
380 1.20 christos if (lflag)
381 1.20 christos syslog(LOG_INFO, "Successfull lookup: %d, %d: %s for %s",
382 1.20 christos lport, fport, pw->pw_name, gethost(&ss[0]));
383 1.20 christos
384 1.20 christos /* No ident enabled? */
385 1.20 christos if (Nflag && check_noident(pw->pw_dir)) {
386 1.20 christos if (lflag)
387 1.20 christos syslog(LOG_NOTICE, "Returning HIDDEN-USER for user %s"
388 1.20 christos " to %s", pw->pw_name, gethost(&ss[0]));
389 1.20 christos iderror(fd, lport, fport, "HIDDEN-USER");
390 1.20 christos return 1;
391 1.20 christos }
392 1.20 christos
393 1.20 christos /* User ident enabled ? */
394 1.20 christos if (iflag && check_userident(pw->pw_dir, idbuf, sizeof(idbuf))) {
395 1.20 christos (void)strlcpy(userbuf, pw->pw_name, sizeof(userbuf));
396 1.20 christos if (!Iflag) {
397 1.20 christos if (strspn(idbuf, "0123456789") &&
398 1.20 christos getpwuid(atoi(idbuf)) != NULL)
399 1.20 christos (void)strlcpy(idbuf, userbuf, sizeof(idbuf));
400 1.20 christos else if (getpwnam(idbuf) != NULL)
401 1.20 christos (void)strlcpy(idbuf, userbuf, sizeof(idbuf));
402 1.20 christos }
403 1.20 christos if (lflag)
404 1.20 christos syslog(LOG_NOTICE, "Returning user specified '%s' for "
405 1.20 christos "user %s to %s", idbuf, userbuf, gethost(&ss[0]));
406 1.20 christos idparse(fd, lport, fport, charset, osname, idbuf);
407 1.20 christos return 0;
408 1.20 christos }
409 1.20 christos
410 1.20 christos /* Send random crap? */
411 1.20 christos if (rflag) {
412 1.20 christos /* Random number or string? */
413 1.20 christos if (nflag)
414 1.20 christos (void)snprintf(idbuf, sizeof(idbuf), "%u",
415 1.20 christos (unsigned int)(arc4random() % 65535));
416 1.20 christos else
417 1.20 christos random_string(idbuf, sizeof(idbuf));
418 1.20 christos
419 1.20 christos if (lflag)
420 1.20 christos syslog(LOG_NOTICE, "Returning random '%s' for user %s"
421 1.20 christos " to %s", idbuf, pw->pw_name, gethost(&ss[0]));
422 1.20 christos idparse(fd, lport, fport, charset, osname, idbuf);
423 1.20 christos return 0;
424 1.20 christos }
425 1.20 christos
426 1.20 christos /* Return number? */
427 1.20 christos if (nflag)
428 1.20 christos (void)snprintf(idbuf, sizeof(idbuf), "%u", uid);
429 1.20 christos else
430 1.20 christos (void)strlcpy(idbuf, pw->pw_name, sizeof(idbuf));
431 1.20 christos
432 1.20 christos if (Fflag) {
433 1.20 christos /* RFC 1413 says that 512 is the limit */
434 1.20 christos change_format(fmt, pw, buf, 512);
435 1.20 christos idparse(fd, lport, fport, charset, osname, buf);
436 1.20 christos } else
437 1.20 christos idparse(fd, lport, fport, charset, osname, idbuf);
438 1.20 christos
439 1.20 christos return 0;
440 1.20 christos }
441 1.20 christos
442 1.20 christos /* Send/parse the ident result */
443 1.20 christos static void
444 1.20 christos idparse(int fd, int lport, int fport, const char *charset, const char *osname,
445 1.20 christos const char *user)
446 1.20 christos {
447 1.20 christos char *p;
448 1.9 msaitoh
449 1.20 christos if (asprintf(&p, "%d , %d : USERID : %s%s%s : %s\r\n", lport, fport,
450 1.20 christos osname, charset ? " , " : "", charset ? charset : "", user) < 0)
451 1.20 christos fatal("asprintf");
452 1.20 christos if (send(fd, p, strlen(p), 0) < 0) {
453 1.20 christos free(p);
454 1.20 christos fatal("send");
455 1.20 christos }
456 1.20 christos free(p);
457 1.20 christos }
458 1.1 cgd
459 1.20 christos /* Return a specified ident error */
460 1.20 christos static void
461 1.20 christos iderror(int fd, int lport, int fport, const char *error)
462 1.20 christos {
463 1.20 christos char *p;
464 1.1 cgd
465 1.20 christos if (asprintf(&p, "%d , %d : ERROR : %s\r\n", lport, fport, error) < 0)
466 1.20 christos fatal("asprintf");
467 1.20 christos if (send(fd, p, strlen(p), 0) < 0) {
468 1.20 christos free(p);
469 1.20 christos fatal("send");
470 1.20 christos }
471 1.20 christos free(p);
472 1.20 christos }
473 1.1 cgd
474 1.20 christos /* Return the IP address of the connecting host */
475 1.20 christos static const char *
476 1.20 christos gethost(struct sockaddr_storage *ss)
477 1.20 christos {
478 1.20 christos static char host[NI_MAXHOST];
479 1.8 mrg
480 1.20 christos if (getnameinfo((struct sockaddr *)ss, ss->ss_len, host,
481 1.20 christos sizeof(host), NULL, 0, NI_NUMERICHOST) == 0)
482 1.20 christos return host;
483 1.9 msaitoh
484 1.20 christos return "UNKNOWN";
485 1.9 msaitoh }
486 1.1 cgd
487 1.20 christos /* Setup sockets, for daemon mode */
488 1.20 christos static int *
489 1.20 christos socketsetup(const char *address, const char *port, int af)
490 1.20 christos {
491 1.20 christos struct addrinfo hints, *res, *res0;
492 1.20 christos int error, maxs, *s, *socks, y = 1;
493 1.20 christos const char *cause = NULL;
494 1.20 christos
495 1.20 christos (void)memset(&hints, 0, sizeof(hints));
496 1.20 christos hints.ai_flags = AI_PASSIVE;
497 1.20 christos hints.ai_family = af;
498 1.20 christos hints.ai_socktype = SOCK_STREAM;
499 1.20 christos error = getaddrinfo(address, port, &hints, &res0);
500 1.20 christos if (error) {
501 1.20 christos if (lflag)
502 1.20 christos syslog(LOG_ERR, "getaddrinfo: %s", gai_strerror(error));
503 1.20 christos errx(1, "%s", gai_strerror(error));
504 1.20 christos /*NOTREACHED*/
505 1.20 christos }
506 1.20 christos
507 1.20 christos /* Count max number of sockets we may open */
508 1.20 christos for (maxs = 0, res = res0; res != NULL; res = res->ai_next)
509 1.20 christos maxs++;
510 1.20 christos
511 1.20 christos socks = malloc((maxs + 1) * sizeof(int));
512 1.20 christos if (socks == NULL) {
513 1.20 christos if (lflag)
514 1.20 christos syslog(LOG_ERR, "malloc: %m");
515 1.20 christos err(1, "malloc");
516 1.20 christos /* NOTREACHED */
517 1.20 christos }
518 1.20 christos
519 1.20 christos *socks = 0;
520 1.20 christos s = socks + 1;
521 1.20 christos for (res = res0; res != NULL; res = res->ai_next) {
522 1.20 christos *s = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
523 1.20 christos if (*s < 0) {
524 1.20 christos cause = "socket";
525 1.20 christos continue;
526 1.20 christos }
527 1.20 christos (void)setsockopt(*s, SOL_SOCKET, SO_REUSEADDR, &y, sizeof(y));
528 1.20 christos if (bind(*s, res->ai_addr, res->ai_addrlen) < 0) {
529 1.20 christos cause = "bind";
530 1.20 christos (void)close(*s);
531 1.20 christos continue;
532 1.20 christos }
533 1.20 christos if (listen(*s, 5) < 0) {
534 1.20 christos cause = "listen";
535 1.20 christos (void)close(*s);
536 1.20 christos continue;
537 1.20 christos }
538 1.20 christos *socks = *socks + 1;
539 1.20 christos s++;
540 1.20 christos }
541 1.20 christos
542 1.20 christos if (*socks == 0) {
543 1.20 christos free(socks);
544 1.20 christos if (lflag)
545 1.20 christos syslog(LOG_ERR, "%s: %m", cause);
546 1.20 christos err(1, "%s", cause);
547 1.20 christos /* NOTREACHED */
548 1.20 christos }
549 1.20 christos if (res0)
550 1.20 christos freeaddrinfo(res0);
551 1.9 msaitoh
552 1.20 christos return socks;
553 1.1 cgd }
554 1.1 cgd
555 1.20 christos /* Return the UID for the connection owner */
556 1.20 christos static int
557 1.20 christos sysctl_getuid(struct sockaddr_storage *ss, socklen_t len, uid_t *uid)
558 1.1 cgd {
559 1.20 christos int mib[4];
560 1.20 christos uid_t myuid;
561 1.20 christos size_t uidlen;
562 1.20 christos
563 1.20 christos uidlen = sizeof(myuid);
564 1.20 christos
565 1.20 christos mib[0] = CTL_NET;
566 1.20 christos mib[1] = ss->ss_family;
567 1.20 christos mib[2] = IPPROTO_TCP;
568 1.20 christos mib[3] = TCPCTL_IDENT;
569 1.20 christos
570 1.20 christos if (sysctl(mib, sizeof(mib)/ sizeof(int), &myuid, &uidlen, ss, len) < 0)
571 1.20 christos return -1;
572 1.20 christos *uid = myuid;
573 1.20 christos
574 1.20 christos return 0;
575 1.1 cgd }
576 1.9 msaitoh
577 1.20 christos /* Check if a .noident file exists in the user home directory */
578 1.20 christos static int
579 1.20 christos check_noident(const char *homedir)
580 1.20 christos {
581 1.20 christos struct stat sb;
582 1.20 christos char *path;
583 1.20 christos int ret;
584 1.20 christos
585 1.20 christos if (homedir == NULL)
586 1.20 christos return 0;
587 1.20 christos if (asprintf(&path, "%s/.noident", homedir) < 0)
588 1.20 christos return 0;
589 1.20 christos ret = stat(path, &sb);
590 1.20 christos
591 1.20 christos free(path);
592 1.20 christos return (ret == 0);
593 1.20 christos }
594 1.1 cgd
595 1.1 cgd /*
596 1.20 christos * Check if a .ident file exists in the user home directory and
597 1.20 christos * return the contents of that file.
598 1.20 christos */
599 1.20 christos static int
600 1.20 christos check_userident(const char *homedir, char *username, size_t len)
601 1.20 christos {
602 1.20 christos struct stat sb;
603 1.20 christos char *path, *p;
604 1.20 christos int fd, n;
605 1.20 christos
606 1.20 christos if (len == 0 || homedir == NULL)
607 1.20 christos return 0;
608 1.20 christos if (asprintf(&path, "%s/.ident", homedir) < 0)
609 1.20 christos return 0;
610 1.20 christos if ((fd = open(path, O_RDONLY|O_NONBLOCK|O_NOFOLLOW, 0)) < 0) {
611 1.20 christos free(path);
612 1.20 christos return 0;
613 1.20 christos }
614 1.20 christos if (fstat(fd, &sb) < 0 || !S_ISREG(sb.st_mode)) {
615 1.20 christos (void)close(fd);
616 1.20 christos free(path);
617 1.20 christos return 0;
618 1.20 christos }
619 1.20 christos if ((n = read(fd, username, len - 1)) < 1) {
620 1.20 christos (void)close(fd);
621 1.20 christos free(path);
622 1.20 christos return 0;
623 1.20 christos }
624 1.20 christos username[n] = '\0';
625 1.20 christos
626 1.20 christos if ((p = strpbrk(username, "\r\n")))
627 1.20 christos *p = '\0';
628 1.20 christos
629 1.20 christos (void)close(fd);
630 1.20 christos free(path);
631 1.20 christos return 1;
632 1.1 cgd }
633 1.1 cgd
634 1.20 christos /* Generate a random string */
635 1.20 christos static void
636 1.20 christos random_string(char *str, size_t len)
637 1.20 christos {
638 1.20 christos static const char chars[] = "abcdefghijklmnopqrstuvwxyz1234567890";
639 1.20 christos char *p;
640 1.20 christos
641 1.20 christos if (len == 0)
642 1.20 christos return;
643 1.20 christos for (p = str; len > 1; len--)
644 1.20 christos *p++ = chars[arc4random() % (sizeof(chars) - 1)];
645 1.20 christos *p = '\0';
646 1.20 christos }
647 1.1 cgd
648 1.20 christos /* Change the output format */
649 1.20 christos static void
650 1.20 christos change_format(const char *format, struct passwd *pw, char *dest, size_t len)
651 1.20 christos {
652 1.20 christos struct group *gr;
653 1.20 christos const char *cp;
654 1.20 christos char **gmp;
655 1.20 christos int bp;
656 1.20 christos
657 1.20 christos if (len == 0)
658 1.20 christos return;
659 1.20 christos if ((gr = getgrgid(pw->pw_gid)) == NULL)
660 1.20 christos return;
661 1.20 christos
662 1.20 christos for (bp = 0, cp = format; *cp != '\0' && bp < 490; cp++) {
663 1.20 christos if (*cp != '%') {
664 1.20 christos dest[bp++] = *cp;
665 1.20 christos continue;
666 1.9 msaitoh }
667 1.20 christos if (*++cp == '\0')
668 1.20 christos break;
669 1.20 christos switch (*cp) {
670 1.20 christos case 'u':
671 1.20 christos (void)snprintf(&dest[bp], len - bp, "%.*s", 490 - bp,
672 1.20 christos pw->pw_name);
673 1.20 christos break;
674 1.20 christos case 'U':
675 1.20 christos (void)snprintf(&dest[bp], len - bp, "%d", pw->pw_uid);
676 1.20 christos break;
677 1.20 christos case 'g':
678 1.20 christos (void)snprintf(&dest[bp], len - bp, "%.*s", 490 - bp,
679 1.20 christos gr->gr_name);
680 1.20 christos break;
681 1.20 christos case 'G':
682 1.20 christos (void)snprintf(&dest[bp], len - bp, "%d", gr->gr_gid);
683 1.20 christos break;
684 1.20 christos case 'l':
685 1.20 christos (void)snprintf(&dest[bp], len - bp, "%.*s", 490 - bp,
686 1.20 christos gr->gr_name);
687 1.20 christos bp += strlen(&dest[bp]);
688 1.20 christos if (bp >= 490)
689 1.20 christos break;
690 1.20 christos setgrent();
691 1.20 christos while ((gr = getgrent()) != NULL) {
692 1.20 christos if (gr->gr_gid == pw->pw_gid)
693 1.20 christos continue;
694 1.20 christos for (gmp = gr->gr_mem; *gmp && **gmp; gmp++) {
695 1.20 christos if (strcmp(*gmp, pw->pw_name) == 0) {
696 1.20 christos (void)snprintf(&dest[bp],
697 1.20 christos len - bp, ",%.*s",
698 1.20 christos 490 - bp, gr->gr_name);
699 1.20 christos bp += strlen(&dest[bp]);
700 1.20 christos break;
701 1.20 christos }
702 1.20 christos }
703 1.20 christos if (bp >= 490)
704 1.20 christos break;
705 1.20 christos }
706 1.20 christos endgrent();
707 1.20 christos break;
708 1.20 christos case 'L':
709 1.20 christos (void)snprintf(&dest[bp], len - bp, "%u", gr->gr_gid);
710 1.20 christos bp += strlen(&dest[bp]);
711 1.20 christos if (bp >= 490)
712 1.20 christos break;
713 1.20 christos setgrent();
714 1.20 christos while ((gr = getgrent()) != NULL) {
715 1.20 christos if (gr->gr_gid == pw->pw_gid)
716 1.20 christos continue;
717 1.20 christos for (gmp = gr->gr_mem; *gmp && **gmp; gmp++) {
718 1.20 christos if (strcmp(*gmp, pw->pw_name) == 0) {
719 1.20 christos (void)snprintf(&dest[bp],
720 1.20 christos len - bp, ",%u",
721 1.20 christos gr->gr_gid);
722 1.20 christos bp += strlen(&dest[bp]);
723 1.20 christos break;
724 1.20 christos }
725 1.20 christos }
726 1.20 christos if (bp >= 490)
727 1.20 christos break;
728 1.20 christos }
729 1.20 christos endgrent();
730 1.20 christos break;
731 1.20 christos default:
732 1.20 christos dest[bp] = *cp;
733 1.20 christos dest[bp+1] = '\0';
734 1.20 christos break;
735 1.9 msaitoh }
736 1.20 christos bp += strlen(&dest[bp]);
737 1.20 christos }
738 1.20 christos if (bp >= 490) {
739 1.20 christos (void)snprintf(&dest[490], len - 490, "...");
740 1.20 christos bp = 493;
741 1.20 christos }
742 1.20 christos dest[bp] = '\0';
743 1.20 christos }
744 1.20 christos
745 1.20 christos /* Just exit when we caught SIGALRM */
746 1.20 christos static void
747 1.20 christos timeout_handler(int s)
748 1.20 christos {
749 1.20 christos if (lflag)
750 1.20 christos syslog(LOG_DEBUG, "SIGALRM triggered, exiting...");
751 1.20 christos exit(1);
752 1.20 christos }
753 1.20 christos
754 1.20 christos /* This is to clean up zombie processes when in daemon mode */
755 1.20 christos static void
756 1.20 christos waitchild(int s)
757 1.20 christos {
758 1.20 christos while (waitpid(-1, NULL, WNOHANG) > 0)
759 1.19 christos continue;
760 1.20 christos }
761 1.20 christos
762 1.20 christos /* Report errno through syslog and quit */
763 1.20 christos static void
764 1.20 christos fatal(const char *func)
765 1.20 christos {
766 1.20 christos if (lflag)
767 1.20 christos syslog(LOG_ERR, "%s: %m", func);
768 1.9 msaitoh exit(1);
769 1.1 cgd }
770