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