blob: e2d36565f7d885cc480b02be82a987f41811ecaf [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 /*
279 * Create the address we will be binding to.
280 */
281 my_addr.sun_family = AF_UNIX;
Matthias Koenig4415b032008-03-14 13:29:35 -0400282 strncpy(my_addr.sun_path, socket_path, sizeof(my_addr.sun_path));
283 my_addr.sun_path[sizeof(my_addr.sun_path)-1] = '\0';
Theodore Ts'o740837d2007-12-16 17:21:38 -0500284 (void) unlink(socket_path);
285 save_umask = umask(0);
286 if (bind(s, (const struct sockaddr *) &my_addr,
287 sizeof(struct sockaddr_un)) < 0) {
288 if (!quiet)
289 fprintf(stderr,
290 _("Couldn't bind unix socket %s: %s\n"),
291 socket_path, strerror(errno));
292 exit(1);
293 }
294 (void) umask(save_umask);
295
296 if (listen(s, 5) < 0) {
297 if (!quiet)
298 fprintf(stderr, _("Couldn't listen on unix "
299 "socket %s: %s\n"), socket_path,
300 strerror(errno));
301 exit(1);
302 }
303
Theodore Ts'o113d6362007-12-25 14:19:15 -0500304 cleanup_socket = socket_path;
305 if (!debug)
306 create_daemon();
307 signal(SIGHUP, terminate_intr);
308 signal(SIGINT, terminate_intr);
309 signal(SIGTERM, terminate_intr);
310 signal(SIGALRM, terminate_intr);
Theodore Ts'o740837d2007-12-16 17:21:38 -0500311 signal(SIGPIPE, SIG_IGN);
312
Theodore Ts'o38e73852009-04-18 09:14:31 -0400313 sprintf(reply_buf, "%8d\n", getpid());
314 if (ftruncate(fd_pidfile, 0)) {} /* Silence warn_unused_result */
315 write_all(fd_pidfile, reply_buf, strlen(reply_buf));
Theodore Ts'ocfa30fd2008-02-15 21:24:30 -0500316 if (fd_pidfile > 1)
317 close(fd_pidfile); /* Unlock the pid file */
318
Theodore Ts'o740837d2007-12-16 17:21:38 -0500319 while (1) {
320 fromlen = sizeof(from_addr);
321 if (timeout > 0)
322 alarm(timeout);
323 ns = accept(s, (struct sockaddr *) &from_addr, &fromlen);
324 alarm(0);
325 if (ns < 0) {
326 if ((errno == EAGAIN) || (errno == EINTR))
327 continue;
328 perror("accept");
329 exit(1);
330 }
331 len = read(ns, &op, 1);
332 if (len != 1) {
333 if (len < 0)
334 perror("read");
335 else
336 printf(_("Error reading from client, "
337 "len = %d\n"), len);
338 goto shutdown_socket;
339 }
340 if ((op == 4) || (op == 5)) {
341 if (read_all(ns, (char *) &num, sizeof(num)) != 4)
342 goto shutdown_socket;
343 if (debug)
344 printf(_("operation %d, incoming num = %d\n"),
345 op, num);
346 } else if (debug)
347 printf("operation %d\n", op);
348
349 switch(op) {
350 case UUIDD_OP_GETPID:
Theodore Ts'o2d328bb2008-03-17 23:17:13 -0400351 sprintf(reply_buf, "%d", getpid());
352 reply_len = strlen(reply_buf)+1;
Theodore Ts'o740837d2007-12-16 17:21:38 -0500353 break;
354 case UUIDD_OP_GET_MAXOP:
Theodore Ts'o2d328bb2008-03-17 23:17:13 -0400355 sprintf(reply_buf, "%d", UUIDD_MAX_OP);
356 reply_len = strlen(reply_buf)+1;
Theodore Ts'o740837d2007-12-16 17:21:38 -0500357 break;
358 case UUIDD_OP_TIME_UUID:
359 num = 1;
360 uuid__generate_time(uu, &num);
361 if (debug) {
362 uuid_unparse(uu, str);
363 printf(_("Generated time UUID: %s\n"), str);
364 }
365 memcpy(reply_buf, uu, sizeof(uu));
366 reply_len = sizeof(uu);
367 break;
368 case UUIDD_OP_RANDOM_UUID:
369 num = 1;
370 uuid__generate_random(uu, &num);
371 if (debug) {
372 uuid_unparse(uu, str);
373 printf(_("Generated random UUID: %s\n"), str);
374 }
375 memcpy(reply_buf, uu, sizeof(uu));
376 reply_len = sizeof(uu);
377 break;
378 case UUIDD_OP_BULK_TIME_UUID:
379 uuid__generate_time(uu, &num);
380 if (debug) {
381 uuid_unparse(uu, str);
382 printf(_("Generated time UUID %s and %d "
383 "following\n"), str, num);
384 }
385 memcpy(reply_buf, uu, sizeof(uu));
386 reply_len = sizeof(uu);
387 memcpy(reply_buf+reply_len, &num, sizeof(num));
388 reply_len += sizeof(num);
389 break;
390 case UUIDD_OP_BULK_RANDOM_UUID:
391 if (num < 0)
392 num = 1;
393 if (num > 1000)
394 num = 1000;
395 if (num*16 > (int) (sizeof(reply_buf)-sizeof(num)))
396 num = (sizeof(reply_buf)-sizeof(num)) / 16;
Theodore Ts'o2d328bb2008-03-17 23:17:13 -0400397 uuid__generate_random((unsigned char *) reply_buf +
398 sizeof(num), &num);
Theodore Ts'o740837d2007-12-16 17:21:38 -0500399 if (debug) {
400 printf(_("Generated %d UUID's:\n"), num);
401 for (i=0, cp=reply_buf+sizeof(num);
402 i < num; i++, cp+=16) {
Theodore Ts'o2d328bb2008-03-17 23:17:13 -0400403 uuid_unparse((unsigned char *)cp, str);
Theodore Ts'o740837d2007-12-16 17:21:38 -0500404 printf("\t%s\n", str);
405 }
406 }
407 reply_len = (num*16) + sizeof(num);
408 memcpy(reply_buf, &num, sizeof(num));
409 break;
410 default:
411 if (debug)
412 printf(_("Invalid operation %d\n"), op);
413 goto shutdown_socket;
414 }
Theodore Ts'o38e73852009-04-18 09:14:31 -0400415 write_all(ns, (char *) &reply_len, sizeof(reply_len));
416 write_all(ns, reply_buf, reply_len);
Theodore Ts'o740837d2007-12-16 17:21:38 -0500417 shutdown_socket:
418 close(ns);
419 }
420}
421
Theodore Ts'o740837d2007-12-16 17:21:38 -0500422int main(int argc, char **argv)
423{
424 const char *socket_path = UUIDD_SOCKET_PATH;
425 const char *pidfile_path = UUIDD_PIDFILE_PATH;
426 const char *err_context;
Theodore Ts'o113d6362007-12-25 14:19:15 -0500427 char buf[1024], *cp;
Theodore Ts'o740837d2007-12-16 17:21:38 -0500428 char str[37], *tmp;
429 uuid_t uu;
430 uid_t uid;
431 gid_t gid;
Theodore Ts'ocfa30fd2008-02-15 21:24:30 -0500432 int i, c, ret;
Theodore Ts'o740837d2007-12-16 17:21:38 -0500433 int debug = 0, do_type = 0, do_kill = 0, num = 0;
434 int timeout = 0, quiet = 0, drop_privs = 0;
435
436#ifdef ENABLE_NLS
437 setlocale(LC_MESSAGES, "");
438 setlocale(LC_CTYPE, "");
439 bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
440 textdomain(NLS_CAT_NAME);
441#endif
442
443 while ((c = getopt (argc, argv, "dkn:qp:s:tT:r")) != EOF) {
444 switch (c) {
445 case 'd':
446 debug++;
Matthias Koenig4415b032008-03-14 13:29:35 -0400447 drop_privs = 1;
Theodore Ts'o740837d2007-12-16 17:21:38 -0500448 break;
449 case 'k':
450 do_kill++;
Matthias Koenig4415b032008-03-14 13:29:35 -0400451 drop_privs = 1;
Theodore Ts'o740837d2007-12-16 17:21:38 -0500452 break;
453 case 'n':
454 num = strtol(optarg, &tmp, 0);
455 if ((num < 0) || *tmp) {
456 fprintf(stderr, _("Bad number: %s\n"), optarg);
457 exit(1);
458 }
459 case 'p':
460 pidfile_path = optarg;
Matthias Koenig4415b032008-03-14 13:29:35 -0400461 drop_privs = 1;
Theodore Ts'o740837d2007-12-16 17:21:38 -0500462 break;
463 case 'q':
464 quiet++;
465 break;
466 case 's':
467 socket_path = optarg;
Matthias Koenig4415b032008-03-14 13:29:35 -0400468 drop_privs = 1;
Theodore Ts'o740837d2007-12-16 17:21:38 -0500469 break;
470 case 't':
471 do_type = UUIDD_OP_TIME_UUID;
Matthias Koenig4415b032008-03-14 13:29:35 -0400472 drop_privs = 1;
Theodore Ts'o740837d2007-12-16 17:21:38 -0500473 break;
474 case 'T':
475 timeout = strtol(optarg, &tmp, 0);
476 if ((timeout < 0) || *tmp) {
477 fprintf(stderr, _("Bad number: %s\n"), optarg);
478 exit(1);
479 }
480 break;
481 case 'r':
482 do_type = UUIDD_OP_RANDOM_UUID;
Matthias Koenig4415b032008-03-14 13:29:35 -0400483 drop_privs = 1;
Theodore Ts'o740837d2007-12-16 17:21:38 -0500484 break;
485 default:
486 usage(argv[0]);
487 }
488 }
489 uid = getuid();
490 if (uid && drop_privs) {
491 gid = getgid();
Theodore Ts'o740837d2007-12-16 17:21:38 -0500492#ifdef HAVE_SETRESGID
Matthias Koenig4415b032008-03-14 13:29:35 -0400493 if (setresgid(gid, gid, gid) < 0)
494 die("setresgid");
Theodore Ts'o740837d2007-12-16 17:21:38 -0500495#else
Matthias Koenig4415b032008-03-14 13:29:35 -0400496 if (setregid(gid, gid) < 0)
497 die("setregid");
498#endif
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400499
Matthias Koenig4415b032008-03-14 13:29:35 -0400500#ifdef HAVE_SETRESUID
501 if (setresuid(uid, uid, uid) < 0)
502 die("setresuid");
503#else
504 if (setreuid(uid, uid) < 0)
505 die("setreuid");
Theodore Ts'o740837d2007-12-16 17:21:38 -0500506#endif
507 }
508 if (num && do_type) {
509 ret = call_daemon(socket_path, do_type+2, buf,
510 sizeof(buf), &num, &err_context);
511 if (ret < 0) {
512 printf(_("Error calling uuidd daemon (%s): %s\n"),
513 err_context, strerror(errno));
514 exit(1);
515 }
516 if (do_type == UUIDD_OP_TIME_UUID) {
517 if (ret != sizeof(uu) + sizeof(num))
518 goto unexpected_size;
519
Theodore Ts'o113d6362007-12-25 14:19:15 -0500520 uuid_unparse((unsigned char *) buf, str);
Theodore Ts'o740837d2007-12-16 17:21:38 -0500521
522 printf(_("%s and subsequent %d UUID's\n"), str, num);
523 } else {
524 printf(_("List of UUID's:\n"));
525 cp = buf + 4;
Theodore Ts'o113d6362007-12-25 14:19:15 -0500526 if (ret != (int) (sizeof(num) + num*sizeof(uu)))
Theodore Ts'o740837d2007-12-16 17:21:38 -0500527 goto unexpected_size;
528 for (i=0; i < num; i++, cp+=16) {
Theodore Ts'o113d6362007-12-25 14:19:15 -0500529 uuid_unparse((unsigned char *) cp, str);
Theodore Ts'o740837d2007-12-16 17:21:38 -0500530 printf("\t%s\n", str);
531 }
532 }
533 exit(0);
534 }
535 if (do_type) {
Theodore Ts'o113d6362007-12-25 14:19:15 -0500536 ret = call_daemon(socket_path, do_type, (char *) &uu,
Theodore Ts'o740837d2007-12-16 17:21:38 -0500537 sizeof(uu), 0, &err_context);
538 if (ret < 0) {
539 printf(_("Error calling uuidd daemon (%s): %s\n"),
540 err_context, strerror(errno));
541 exit(1);
542 }
543 if (ret != sizeof(uu)) {
544 unexpected_size:
545 printf(_("Unexpected reply length from server %d\n"),
546 ret);
547 exit(1);
548 }
549 uuid_unparse(uu, str);
550
551 printf("%s\n", str);
552 exit(0);
553 }
554
Theodore Ts'o113d6362007-12-25 14:19:15 -0500555 if (do_kill) {
556 ret = call_daemon(socket_path, 0, buf, sizeof(buf), 0, 0);
557 if ((ret > 0) && ((do_kill = atoi((char *) buf)) > 0)) {
Theodore Ts'o740837d2007-12-16 17:21:38 -0500558 ret = kill(do_kill, SIGTERM);
559 if (ret < 0) {
560 if (!quiet)
561 fprintf(stderr,
562 _("Couldn't kill uuidd running "
563 "at pid %d: %s\n"), do_kill,
564 strerror(errno));
565 exit(1);
566 }
567 if (!quiet)
568 printf(_("Killed uuidd running at pid %d\n"),
569 do_kill);
Theodore Ts'o740837d2007-12-16 17:21:38 -0500570 }
Theodore Ts'o113d6362007-12-25 14:19:15 -0500571 exit(0);
Theodore Ts'o740837d2007-12-16 17:21:38 -0500572 }
Theodore Ts'o740837d2007-12-16 17:21:38 -0500573
Theodore Ts'ocfa30fd2008-02-15 21:24:30 -0500574 server_loop(socket_path, pidfile_path, debug, timeout, quiet);
Theodore Ts'o740837d2007-12-16 17:21:38 -0500575 return 0;
576}