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