blob: 6913bf7e2125eeb42030c5ea8bde351e30849f23 [file] [log] [blame]
Theodore Ts'o740837d2007-12-16 17:21:38 -05001/*
2 * uuidd.c --- UUID-generation daemon
3 *
4 * Copyright (C) 2007 Theodore Ts'o
5 *
6 * %Begin-Header%
7 * This file may be redistributed under the terms of the GNU Public
8 * License.
9 * %End-Header%
10 */
11
Theodore Ts'oe5679a62008-01-01 09:16:16 -050012#define _GNU_SOURCE /* for setres[ug]id() */
13
Theodore Ts'o740837d2007-12-16 17:21:38 -050014#include <stdio.h>
15#ifdef HAVE_STDLIB_H
16#include <stdlib.h>
17#endif
18#include <unistd.h>
19#include <inttypes.h>
20#include <errno.h>
21#include <sys/types.h>
22#include <sys/stat.h>
23#include <sys/socket.h>
24#include <sys/un.h>
25#include <fcntl.h>
26#include <signal.h>
Theodore Ts'ocfa30fd2008-02-15 21:24:30 -050027#include <string.h>
Theodore Ts'o740837d2007-12-16 17:21:38 -050028#ifdef HAVE_GETOPT_H
29#include <getopt.h>
30#else
31extern int getopt(int argc, char * const argv[], const char *optstring);
32extern char *optarg;
33extern int optind;
34#endif
35#include "uuid/uuid.h"
36#include "uuid/uuidd.h"
37#include "nls-enable.h"
38
39#ifdef __GNUC__
40#define CODE_ATTR(x) __attribute__(x)
41#else
42#define CODE_ATTR(x)
43#endif
44
45static void usage(const char *progname)
46{
47 fprintf(stderr, _("Usage: %s [-d] [-p pidfile] [-s socketpath] "
48 "[-T timeout]\n"), progname);
49 fprintf(stderr, _(" %s [-r|t] [-n num] [-s socketpath]\n"),
50 progname);
51 fprintf(stderr, _(" %s -k\n"), progname);
52 exit(1);
53}
54
Matthias Koenig4415b032008-03-14 13:29:35 -040055static void die(const char *msg)
56{
57 perror(msg);
58 exit(1);
59}
60
Theodore Ts'o113d6362007-12-25 14:19:15 -050061static void create_daemon(void)
Theodore Ts'o740837d2007-12-16 17:21:38 -050062{
63 pid_t pid;
64 uid_t euid;
Theodore Ts'o740837d2007-12-16 17:21:38 -050065
66 pid = fork();
67 if (pid == -1) {
68 perror("fork");
69 exit(1);
70 } else if (pid != 0) {
71 exit(0);
72 }
73
74 close(0);
75 close(1);
76 close(2);
77 open("/dev/null", O_RDWR);
78 open("/dev/null", O_RDWR);
79 open("/dev/null", O_RDWR);
80
Theodore Ts'o38e73852009-04-18 09:14:31 -040081 if (chdir("/")) {} /* Silence warn_unused_result warning */
Theodore Ts'o740837d2007-12-16 17:21:38 -050082 (void) setsid();
83 euid = geteuid();
Matthias Koenig4415b032008-03-14 13:29:35 -040084 if (setreuid(euid, euid) < 0)
85 die("setreuid");
Theodore Ts'o740837d2007-12-16 17:21:38 -050086}
87
88static int read_all(int fd, char *buf, size_t count)
89{
90 ssize_t ret;
91 int c = 0;
92
93 memset(buf, 0, count);
94 while (count > 0) {
95 ret = read(fd, buf, count);
96 if (ret < 0) {
97 if ((errno == EAGAIN) || (errno == EINTR))
98 continue;
99 return -1;
100 }
101 count -= ret;
102 buf += ret;
103 c += ret;
104 }
105 return c;
106}
107
Theodore Ts'o38e73852009-04-18 09:14:31 -0400108static int write_all(int fd, char *buf, size_t count)
109{
110 ssize_t ret;
111 int c = 0;
112
113 while (count > 0) {
114 ret = write(fd, buf, count);
115 if (ret < 0) {
116 if ((errno == EAGAIN) || (errno == EINTR))
117 continue;
118 return -1;
119 }
120 count -= ret;
121 buf += ret;
122 c += ret;
123 }
124 return c;
125}
126
Theodore Ts'o740837d2007-12-16 17:21:38 -0500127static const char *cleanup_pidfile, *cleanup_socket;
128
129static void terminate_intr(int signo CODE_ATTR((unused)))
130{
131 (void) unlink(cleanup_pidfile);
Theodore Ts'o113d6362007-12-25 14:19:15 -0500132 if (cleanup_socket)
133 (void) unlink(cleanup_socket);
Theodore Ts'o740837d2007-12-16 17:21:38 -0500134 exit(0);
135}
136
Theodore Ts'ocfa30fd2008-02-15 21:24:30 -0500137static int call_daemon(const char *socket_path, int op, char *buf,
138 int buflen, int *num, const char **err_context)
139{
140 char op_buf[8];
141 int op_len;
142 int s;
143 ssize_t ret;
144 int32_t reply_len = 0;
145 struct sockaddr_un srv_addr;
146
147 if (((op == 4) || (op == 5)) && !num) {
148 if (err_context)
149 *err_context = _("bad arguments");
150 errno = EINVAL;
151 return -1;
152 }
153
154 if ((s = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
155 if (err_context)
156 *err_context = _("socket");
157 return -1;
158 }
159
160 srv_addr.sun_family = AF_UNIX;
Matthias Koenig4415b032008-03-14 13:29:35 -0400161 strncpy(srv_addr.sun_path, socket_path, sizeof(srv_addr.sun_path));
162 srv_addr.sun_path[sizeof(srv_addr.sun_path)-1] = '\0';
Theodore Ts'ocfa30fd2008-02-15 21:24:30 -0500163
164 if (connect(s, (const struct sockaddr *) &srv_addr,
165 sizeof(struct sockaddr_un)) < 0) {
166 if (err_context)
167 *err_context = _("connect");
168 close(s);
169 return -1;
170 }
171
172 if (op == 5) {
173 if ((*num)*16 > buflen-4)
174 *num = (buflen-4) / 16;
175 }
176 op_buf[0] = op;
177 op_len = 1;
178 if ((op == 4) || (op == 5)) {
179 memcpy(op_buf+1, num, sizeof(int));
180 op_len += sizeof(int);
181 }
182
Theodore Ts'o38e73852009-04-18 09:14:31 -0400183 ret = write_all(s, op_buf, op_len);
Theodore Ts'ocfa30fd2008-02-15 21:24:30 -0500184 if (ret < op_len) {
185 if (err_context)
186 *err_context = _("write");
187 close(s);
188 return -1;
189 }
190
191 ret = read_all(s, (char *) &reply_len, sizeof(reply_len));
192 if (ret < 0) {
193 if (err_context)
194 *err_context = _("read count");
195 close(s);
196 return -1;
197 }
198 if (reply_len < 0 || reply_len > buflen) {
199 if (err_context)
200 *err_context = _("bad response length");
201 close(s);
202 return -1;
203 }
204 ret = read_all(s, (char *) buf, reply_len);
205
206 if ((ret > 0) && (op == 4)) {
207 if (reply_len >= (int) (16+sizeof(int)))
208 memcpy(buf+16, num, sizeof(int));
209 else
210 *num = -1;
211 }
212 if ((ret > 0) && (op == 5)) {
213 if (*num >= (int) sizeof(int))
214 memcpy(buf, num, sizeof(int));
215 else
216 *num = -1;
217 }
218
219 close(s);
220
221 return ret;
222}
223
224static void server_loop(const char *socket_path, const char *pidfile_path,
225 int debug, int timeout, int quiet)
Theodore Ts'o740837d2007-12-16 17:21:38 -0500226{
227 struct sockaddr_un my_addr, from_addr;
Theodore Ts'ocfa30fd2008-02-15 21:24:30 -0500228 struct flock fl;
Theodore Ts'o740837d2007-12-16 17:21:38 -0500229 socklen_t fromlen;
230 int32_t reply_len = 0;
231 uuid_t uu;
232 mode_t save_umask;
Theodore Ts'o2d328bb2008-03-17 23:17:13 -0400233 char reply_buf[1024], *cp;
Theodore Ts'o740837d2007-12-16 17:21:38 -0500234 char op, str[37];
235 int i, s, ns, len, num;
Theodore Ts'ocfa30fd2008-02-15 21:24:30 -0500236 int fd_pidfile, ret;
237
238 fd_pidfile = open(pidfile_path, O_CREAT | O_RDWR, 0664);
239 if (fd_pidfile < 0) {
240 if (!quiet)
241 fprintf(stderr, "Failed to open/create %s: %s\n",
242 pidfile_path, strerror(errno));
243 exit(1);
244 }
245 cleanup_pidfile = pidfile_path;
246 cleanup_socket = 0;
247 signal(SIGALRM, terminate_intr);
248 alarm(30);
249 fl.l_type = F_WRLCK;
250 fl.l_whence = SEEK_SET;
251 fl.l_start = 0;
252 fl.l_len = 0;
253 fl.l_pid = 0;
254 while (fcntl(fd_pidfile, F_SETLKW, &fl) < 0) {
255 if ((errno == EAGAIN) || (errno == EINTR))
256 continue;
257 if (!quiet)
258 fprintf(stderr, "Failed to lock %s: %s\n",
259 pidfile_path, strerror(errno));
260 exit(1);
261 }
262 ret = call_daemon(socket_path, 0, reply_buf, sizeof(reply_buf), 0, 0);
263 if (ret > 0) {
264 if (!quiet)
265 printf(_("uuidd daemon already running at pid %s\n"),
266 reply_buf);
267 exit(1);
268 }
269 alarm(0);
Theodore Ts'o740837d2007-12-16 17:21:38 -0500270
271 if ((s = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
272 if (!quiet)
273 fprintf(stderr, _("Couldn't create unix stream "
274 "socket: %s"), strerror(errno));
275 exit(1);
276 }
277
278 /*
Theodore Ts'o2842bb32009-06-29 19:32:50 -0400279 * Make sure the socket isn't using fd numbers 0-2 to avoid it
280 * getting closed by create_daemon()
281 */
282 while (!debug && s <= 2) {
283 s = dup(s);
284 if (s < 0) {
285 perror("dup");
286 exit(1);
287 }
288 }
289
290 /*
Theodore Ts'o740837d2007-12-16 17:21:38 -0500291 * Create the address we will be binding to.
292 */
293 my_addr.sun_family = AF_UNIX;
Matthias Koenig4415b032008-03-14 13:29:35 -0400294 strncpy(my_addr.sun_path, socket_path, sizeof(my_addr.sun_path));
295 my_addr.sun_path[sizeof(my_addr.sun_path)-1] = '\0';
Theodore Ts'o740837d2007-12-16 17:21:38 -0500296 (void) unlink(socket_path);
297 save_umask = umask(0);
298 if (bind(s, (const struct sockaddr *) &my_addr,
299 sizeof(struct sockaddr_un)) < 0) {
300 if (!quiet)
301 fprintf(stderr,
302 _("Couldn't bind unix socket %s: %s\n"),
303 socket_path, strerror(errno));
304 exit(1);
305 }
306 (void) umask(save_umask);
307
308 if (listen(s, 5) < 0) {
309 if (!quiet)
310 fprintf(stderr, _("Couldn't listen on unix "
311 "socket %s: %s\n"), socket_path,
312 strerror(errno));
313 exit(1);
314 }
315
Theodore Ts'o113d6362007-12-25 14:19:15 -0500316 cleanup_socket = socket_path;
317 if (!debug)
318 create_daemon();
319 signal(SIGHUP, terminate_intr);
320 signal(SIGINT, terminate_intr);
321 signal(SIGTERM, terminate_intr);
322 signal(SIGALRM, terminate_intr);
Theodore Ts'o740837d2007-12-16 17:21:38 -0500323 signal(SIGPIPE, SIG_IGN);
324
Theodore Ts'o38e73852009-04-18 09:14:31 -0400325 sprintf(reply_buf, "%8d\n", getpid());
326 if (ftruncate(fd_pidfile, 0)) {} /* Silence warn_unused_result */
327 write_all(fd_pidfile, reply_buf, strlen(reply_buf));
Theodore Ts'ocfa30fd2008-02-15 21:24:30 -0500328 if (fd_pidfile > 1)
329 close(fd_pidfile); /* Unlock the pid file */
330
Theodore Ts'o740837d2007-12-16 17:21:38 -0500331 while (1) {
332 fromlen = sizeof(from_addr);
333 if (timeout > 0)
334 alarm(timeout);
335 ns = accept(s, (struct sockaddr *) &from_addr, &fromlen);
336 alarm(0);
337 if (ns < 0) {
338 if ((errno == EAGAIN) || (errno == EINTR))
339 continue;
340 perror("accept");
341 exit(1);
342 }
343 len = read(ns, &op, 1);
344 if (len != 1) {
345 if (len < 0)
346 perror("read");
347 else
348 printf(_("Error reading from client, "
349 "len = %d\n"), len);
350 goto shutdown_socket;
351 }
352 if ((op == 4) || (op == 5)) {
353 if (read_all(ns, (char *) &num, sizeof(num)) != 4)
354 goto shutdown_socket;
355 if (debug)
356 printf(_("operation %d, incoming num = %d\n"),
357 op, num);
358 } else if (debug)
359 printf("operation %d\n", op);
360
361 switch(op) {
362 case UUIDD_OP_GETPID:
Theodore Ts'o2d328bb2008-03-17 23:17:13 -0400363 sprintf(reply_buf, "%d", getpid());
364 reply_len = strlen(reply_buf)+1;
Theodore Ts'o740837d2007-12-16 17:21:38 -0500365 break;
366 case UUIDD_OP_GET_MAXOP:
Theodore Ts'o2d328bb2008-03-17 23:17:13 -0400367 sprintf(reply_buf, "%d", UUIDD_MAX_OP);
368 reply_len = strlen(reply_buf)+1;
Theodore Ts'o740837d2007-12-16 17:21:38 -0500369 break;
370 case UUIDD_OP_TIME_UUID:
371 num = 1;
372 uuid__generate_time(uu, &num);
373 if (debug) {
374 uuid_unparse(uu, str);
375 printf(_("Generated time UUID: %s\n"), str);
376 }
377 memcpy(reply_buf, uu, sizeof(uu));
378 reply_len = sizeof(uu);
379 break;
380 case UUIDD_OP_RANDOM_UUID:
381 num = 1;
382 uuid__generate_random(uu, &num);
383 if (debug) {
384 uuid_unparse(uu, str);
385 printf(_("Generated random UUID: %s\n"), str);
386 }
387 memcpy(reply_buf, uu, sizeof(uu));
388 reply_len = sizeof(uu);
389 break;
390 case UUIDD_OP_BULK_TIME_UUID:
391 uuid__generate_time(uu, &num);
392 if (debug) {
393 uuid_unparse(uu, str);
394 printf(_("Generated time UUID %s and %d "
395 "following\n"), str, num);
396 }
397 memcpy(reply_buf, uu, sizeof(uu));
398 reply_len = sizeof(uu);
399 memcpy(reply_buf+reply_len, &num, sizeof(num));
400 reply_len += sizeof(num);
401 break;
402 case UUIDD_OP_BULK_RANDOM_UUID:
403 if (num < 0)
404 num = 1;
405 if (num > 1000)
406 num = 1000;
407 if (num*16 > (int) (sizeof(reply_buf)-sizeof(num)))
408 num = (sizeof(reply_buf)-sizeof(num)) / 16;
Theodore Ts'o2d328bb2008-03-17 23:17:13 -0400409 uuid__generate_random((unsigned char *) reply_buf +
410 sizeof(num), &num);
Theodore Ts'o740837d2007-12-16 17:21:38 -0500411 if (debug) {
412 printf(_("Generated %d UUID's:\n"), num);
413 for (i=0, cp=reply_buf+sizeof(num);
414 i < num; i++, cp+=16) {
Theodore Ts'o2d328bb2008-03-17 23:17:13 -0400415 uuid_unparse((unsigned char *)cp, str);
Theodore Ts'o740837d2007-12-16 17:21:38 -0500416 printf("\t%s\n", str);
417 }
418 }
419 reply_len = (num*16) + sizeof(num);
420 memcpy(reply_buf, &num, sizeof(num));
421 break;
422 default:
423 if (debug)
424 printf(_("Invalid operation %d\n"), op);
425 goto shutdown_socket;
426 }
Theodore Ts'o38e73852009-04-18 09:14:31 -0400427 write_all(ns, (char *) &reply_len, sizeof(reply_len));
428 write_all(ns, reply_buf, reply_len);
Theodore Ts'o740837d2007-12-16 17:21:38 -0500429 shutdown_socket:
430 close(ns);
431 }
432}
433
Theodore Ts'o740837d2007-12-16 17:21:38 -0500434int main(int argc, char **argv)
435{
436 const char *socket_path = UUIDD_SOCKET_PATH;
437 const char *pidfile_path = UUIDD_PIDFILE_PATH;
438 const char *err_context;
Theodore Ts'o113d6362007-12-25 14:19:15 -0500439 char buf[1024], *cp;
Theodore Ts'o740837d2007-12-16 17:21:38 -0500440 char str[37], *tmp;
441 uuid_t uu;
442 uid_t uid;
443 gid_t gid;
Theodore Ts'ocfa30fd2008-02-15 21:24:30 -0500444 int i, c, ret;
Theodore Ts'o740837d2007-12-16 17:21:38 -0500445 int debug = 0, do_type = 0, do_kill = 0, num = 0;
446 int timeout = 0, quiet = 0, drop_privs = 0;
447
448#ifdef ENABLE_NLS
449 setlocale(LC_MESSAGES, "");
450 setlocale(LC_CTYPE, "");
451 bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
452 textdomain(NLS_CAT_NAME);
453#endif
454
455 while ((c = getopt (argc, argv, "dkn:qp:s:tT:r")) != EOF) {
456 switch (c) {
457 case 'd':
458 debug++;
Matthias Koenig4415b032008-03-14 13:29:35 -0400459 drop_privs = 1;
Theodore Ts'o740837d2007-12-16 17:21:38 -0500460 break;
461 case 'k':
462 do_kill++;
Matthias Koenig4415b032008-03-14 13:29:35 -0400463 drop_privs = 1;
Theodore Ts'o740837d2007-12-16 17:21:38 -0500464 break;
465 case 'n':
466 num = strtol(optarg, &tmp, 0);
467 if ((num < 0) || *tmp) {
468 fprintf(stderr, _("Bad number: %s\n"), optarg);
469 exit(1);
470 }
471 case 'p':
472 pidfile_path = optarg;
Matthias Koenig4415b032008-03-14 13:29:35 -0400473 drop_privs = 1;
Theodore Ts'o740837d2007-12-16 17:21:38 -0500474 break;
475 case 'q':
476 quiet++;
477 break;
478 case 's':
479 socket_path = optarg;
Matthias Koenig4415b032008-03-14 13:29:35 -0400480 drop_privs = 1;
Theodore Ts'o740837d2007-12-16 17:21:38 -0500481 break;
482 case 't':
483 do_type = UUIDD_OP_TIME_UUID;
Matthias Koenig4415b032008-03-14 13:29:35 -0400484 drop_privs = 1;
Theodore Ts'o740837d2007-12-16 17:21:38 -0500485 break;
486 case 'T':
487 timeout = strtol(optarg, &tmp, 0);
488 if ((timeout < 0) || *tmp) {
489 fprintf(stderr, _("Bad number: %s\n"), optarg);
490 exit(1);
491 }
492 break;
493 case 'r':
494 do_type = UUIDD_OP_RANDOM_UUID;
Matthias Koenig4415b032008-03-14 13:29:35 -0400495 drop_privs = 1;
Theodore Ts'o740837d2007-12-16 17:21:38 -0500496 break;
497 default:
498 usage(argv[0]);
499 }
500 }
501 uid = getuid();
502 if (uid && drop_privs) {
503 gid = getgid();
Theodore Ts'o740837d2007-12-16 17:21:38 -0500504#ifdef HAVE_SETRESGID
Matthias Koenig4415b032008-03-14 13:29:35 -0400505 if (setresgid(gid, gid, gid) < 0)
506 die("setresgid");
Theodore Ts'o740837d2007-12-16 17:21:38 -0500507#else
Matthias Koenig4415b032008-03-14 13:29:35 -0400508 if (setregid(gid, gid) < 0)
509 die("setregid");
510#endif
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400511
Matthias Koenig4415b032008-03-14 13:29:35 -0400512#ifdef HAVE_SETRESUID
513 if (setresuid(uid, uid, uid) < 0)
514 die("setresuid");
515#else
516 if (setreuid(uid, uid) < 0)
517 die("setreuid");
Theodore Ts'o740837d2007-12-16 17:21:38 -0500518#endif
519 }
520 if (num && do_type) {
521 ret = call_daemon(socket_path, do_type+2, buf,
522 sizeof(buf), &num, &err_context);
523 if (ret < 0) {
524 printf(_("Error calling uuidd daemon (%s): %s\n"),
525 err_context, strerror(errno));
526 exit(1);
527 }
528 if (do_type == UUIDD_OP_TIME_UUID) {
529 if (ret != sizeof(uu) + sizeof(num))
530 goto unexpected_size;
531
Theodore Ts'o113d6362007-12-25 14:19:15 -0500532 uuid_unparse((unsigned char *) buf, str);
Theodore Ts'o740837d2007-12-16 17:21:38 -0500533
534 printf(_("%s and subsequent %d UUID's\n"), str, num);
535 } else {
536 printf(_("List of UUID's:\n"));
537 cp = buf + 4;
Theodore Ts'o113d6362007-12-25 14:19:15 -0500538 if (ret != (int) (sizeof(num) + num*sizeof(uu)))
Theodore Ts'o740837d2007-12-16 17:21:38 -0500539 goto unexpected_size;
540 for (i=0; i < num; i++, cp+=16) {
Theodore Ts'o113d6362007-12-25 14:19:15 -0500541 uuid_unparse((unsigned char *) cp, str);
Theodore Ts'o740837d2007-12-16 17:21:38 -0500542 printf("\t%s\n", str);
543 }
544 }
545 exit(0);
546 }
547 if (do_type) {
Theodore Ts'o113d6362007-12-25 14:19:15 -0500548 ret = call_daemon(socket_path, do_type, (char *) &uu,
Theodore Ts'o740837d2007-12-16 17:21:38 -0500549 sizeof(uu), 0, &err_context);
550 if (ret < 0) {
551 printf(_("Error calling uuidd daemon (%s): %s\n"),
552 err_context, strerror(errno));
553 exit(1);
554 }
555 if (ret != sizeof(uu)) {
556 unexpected_size:
557 printf(_("Unexpected reply length from server %d\n"),
558 ret);
559 exit(1);
560 }
561 uuid_unparse(uu, str);
562
563 printf("%s\n", str);
564 exit(0);
565 }
566
Theodore Ts'o113d6362007-12-25 14:19:15 -0500567 if (do_kill) {
568 ret = call_daemon(socket_path, 0, buf, sizeof(buf), 0, 0);
569 if ((ret > 0) && ((do_kill = atoi((char *) buf)) > 0)) {
Theodore Ts'o740837d2007-12-16 17:21:38 -0500570 ret = kill(do_kill, SIGTERM);
571 if (ret < 0) {
572 if (!quiet)
573 fprintf(stderr,
574 _("Couldn't kill uuidd running "
575 "at pid %d: %s\n"), do_kill,
576 strerror(errno));
577 exit(1);
578 }
579 if (!quiet)
580 printf(_("Killed uuidd running at pid %d\n"),
581 do_kill);
Theodore Ts'o740837d2007-12-16 17:21:38 -0500582 }
Theodore Ts'o113d6362007-12-25 14:19:15 -0500583 exit(0);
Theodore Ts'o740837d2007-12-16 17:21:38 -0500584 }
Theodore Ts'o740837d2007-12-16 17:21:38 -0500585
Theodore Ts'ocfa30fd2008-02-15 21:24:30 -0500586 server_loop(socket_path, pidfile_path, debug, timeout, quiet);
Theodore Ts'o740837d2007-12-16 17:21:38 -0500587 return 0;
588}