blob: c964b4eafd8649b0a9d6cba922027443dc58bc86 [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
81 chdir("/");
82 (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
108static const char *cleanup_pidfile, *cleanup_socket;
109
110static void terminate_intr(int signo CODE_ATTR((unused)))
111{
112 (void) unlink(cleanup_pidfile);
Theodore Ts'o113d6362007-12-25 14:19:15 -0500113 if (cleanup_socket)
114 (void) unlink(cleanup_socket);
Theodore Ts'o740837d2007-12-16 17:21:38 -0500115 exit(0);
116}
117
Theodore Ts'ocfa30fd2008-02-15 21:24:30 -0500118static int call_daemon(const char *socket_path, int op, char *buf,
119 int buflen, int *num, const char **err_context)
120{
121 char op_buf[8];
122 int op_len;
123 int s;
124 ssize_t ret;
125 int32_t reply_len = 0;
126 struct sockaddr_un srv_addr;
127
128 if (((op == 4) || (op == 5)) && !num) {
129 if (err_context)
130 *err_context = _("bad arguments");
131 errno = EINVAL;
132 return -1;
133 }
134
135 if ((s = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
136 if (err_context)
137 *err_context = _("socket");
138 return -1;
139 }
140
141 srv_addr.sun_family = AF_UNIX;
Matthias Koenig4415b032008-03-14 13:29:35 -0400142 strncpy(srv_addr.sun_path, socket_path, sizeof(srv_addr.sun_path));
143 srv_addr.sun_path[sizeof(srv_addr.sun_path)-1] = '\0';
Theodore Ts'ocfa30fd2008-02-15 21:24:30 -0500144
145 if (connect(s, (const struct sockaddr *) &srv_addr,
146 sizeof(struct sockaddr_un)) < 0) {
147 if (err_context)
148 *err_context = _("connect");
149 close(s);
150 return -1;
151 }
152
153 if (op == 5) {
154 if ((*num)*16 > buflen-4)
155 *num = (buflen-4) / 16;
156 }
157 op_buf[0] = op;
158 op_len = 1;
159 if ((op == 4) || (op == 5)) {
160 memcpy(op_buf+1, num, sizeof(int));
161 op_len += sizeof(int);
162 }
163
164 ret = write(s, op_buf, op_len);
165 if (ret < op_len) {
166 if (err_context)
167 *err_context = _("write");
168 close(s);
169 return -1;
170 }
171
172 ret = read_all(s, (char *) &reply_len, sizeof(reply_len));
173 if (ret < 0) {
174 if (err_context)
175 *err_context = _("read count");
176 close(s);
177 return -1;
178 }
179 if (reply_len < 0 || reply_len > buflen) {
180 if (err_context)
181 *err_context = _("bad response length");
182 close(s);
183 return -1;
184 }
185 ret = read_all(s, (char *) buf, reply_len);
186
187 if ((ret > 0) && (op == 4)) {
188 if (reply_len >= (int) (16+sizeof(int)))
189 memcpy(buf+16, num, sizeof(int));
190 else
191 *num = -1;
192 }
193 if ((ret > 0) && (op == 5)) {
194 if (*num >= (int) sizeof(int))
195 memcpy(buf, num, sizeof(int));
196 else
197 *num = -1;
198 }
199
200 close(s);
201
202 return ret;
203}
204
205static void server_loop(const char *socket_path, const char *pidfile_path,
206 int debug, int timeout, int quiet)
Theodore Ts'o740837d2007-12-16 17:21:38 -0500207{
208 struct sockaddr_un my_addr, from_addr;
209 unsigned char reply_buf[1024], *cp;
Theodore Ts'ocfa30fd2008-02-15 21:24:30 -0500210 struct flock fl;
Theodore Ts'o740837d2007-12-16 17:21:38 -0500211 socklen_t fromlen;
212 int32_t reply_len = 0;
213 uuid_t uu;
214 mode_t save_umask;
215 char op, str[37];
216 int i, s, ns, len, num;
Theodore Ts'ocfa30fd2008-02-15 21:24:30 -0500217 int fd_pidfile, ret;
218
219 fd_pidfile = open(pidfile_path, O_CREAT | O_RDWR, 0664);
220 if (fd_pidfile < 0) {
221 if (!quiet)
222 fprintf(stderr, "Failed to open/create %s: %s\n",
223 pidfile_path, strerror(errno));
224 exit(1);
225 }
226 cleanup_pidfile = pidfile_path;
227 cleanup_socket = 0;
228 signal(SIGALRM, terminate_intr);
229 alarm(30);
230 fl.l_type = F_WRLCK;
231 fl.l_whence = SEEK_SET;
232 fl.l_start = 0;
233 fl.l_len = 0;
234 fl.l_pid = 0;
235 while (fcntl(fd_pidfile, F_SETLKW, &fl) < 0) {
236 if ((errno == EAGAIN) || (errno == EINTR))
237 continue;
238 if (!quiet)
239 fprintf(stderr, "Failed to lock %s: %s\n",
240 pidfile_path, strerror(errno));
241 exit(1);
242 }
243 ret = call_daemon(socket_path, 0, reply_buf, sizeof(reply_buf), 0, 0);
244 if (ret > 0) {
245 if (!quiet)
246 printf(_("uuidd daemon already running at pid %s\n"),
247 reply_buf);
248 exit(1);
249 }
250 alarm(0);
Theodore Ts'o740837d2007-12-16 17:21:38 -0500251
252 if ((s = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
253 if (!quiet)
254 fprintf(stderr, _("Couldn't create unix stream "
255 "socket: %s"), strerror(errno));
256 exit(1);
257 }
258
259 /*
260 * Create the address we will be binding to.
261 */
262 my_addr.sun_family = AF_UNIX;
Matthias Koenig4415b032008-03-14 13:29:35 -0400263 strncpy(my_addr.sun_path, socket_path, sizeof(my_addr.sun_path));
264 my_addr.sun_path[sizeof(my_addr.sun_path)-1] = '\0';
Theodore Ts'o740837d2007-12-16 17:21:38 -0500265 (void) unlink(socket_path);
266 save_umask = umask(0);
267 if (bind(s, (const struct sockaddr *) &my_addr,
268 sizeof(struct sockaddr_un)) < 0) {
269 if (!quiet)
270 fprintf(stderr,
271 _("Couldn't bind unix socket %s: %s\n"),
272 socket_path, strerror(errno));
273 exit(1);
274 }
275 (void) umask(save_umask);
276
277 if (listen(s, 5) < 0) {
278 if (!quiet)
279 fprintf(stderr, _("Couldn't listen on unix "
280 "socket %s: %s\n"), socket_path,
281 strerror(errno));
282 exit(1);
283 }
284
Theodore Ts'o113d6362007-12-25 14:19:15 -0500285 cleanup_socket = socket_path;
286 if (!debug)
287 create_daemon();
288 signal(SIGHUP, terminate_intr);
289 signal(SIGINT, terminate_intr);
290 signal(SIGTERM, terminate_intr);
291 signal(SIGALRM, terminate_intr);
Theodore Ts'o740837d2007-12-16 17:21:38 -0500292 signal(SIGPIPE, SIG_IGN);
293
Theodore Ts'ocfa30fd2008-02-15 21:24:30 -0500294 sprintf(reply_buf, "%d\n", getpid());
295 ftruncate(fd_pidfile, 0);
296 write(fd_pidfile, reply_buf, strlen(reply_buf));
297 if (fd_pidfile > 1)
298 close(fd_pidfile); /* Unlock the pid file */
299
Theodore Ts'o740837d2007-12-16 17:21:38 -0500300 while (1) {
301 fromlen = sizeof(from_addr);
302 if (timeout > 0)
303 alarm(timeout);
304 ns = accept(s, (struct sockaddr *) &from_addr, &fromlen);
305 alarm(0);
306 if (ns < 0) {
307 if ((errno == EAGAIN) || (errno == EINTR))
308 continue;
309 perror("accept");
310 exit(1);
311 }
312 len = read(ns, &op, 1);
313 if (len != 1) {
314 if (len < 0)
315 perror("read");
316 else
317 printf(_("Error reading from client, "
318 "len = %d\n"), len);
319 goto shutdown_socket;
320 }
321 if ((op == 4) || (op == 5)) {
322 if (read_all(ns, (char *) &num, sizeof(num)) != 4)
323 goto shutdown_socket;
324 if (debug)
325 printf(_("operation %d, incoming num = %d\n"),
326 op, num);
327 } else if (debug)
328 printf("operation %d\n", op);
329
330 switch(op) {
331 case UUIDD_OP_GETPID:
332 sprintf((char *) reply_buf, "%d", getpid());
333 reply_len = strlen((char *) reply_buf)+1;
334 break;
335 case UUIDD_OP_GET_MAXOP:
336 sprintf((char *) reply_buf, "%d", UUIDD_MAX_OP);
337 reply_len = strlen((char *) reply_buf)+1;
338 break;
339 case UUIDD_OP_TIME_UUID:
340 num = 1;
341 uuid__generate_time(uu, &num);
342 if (debug) {
343 uuid_unparse(uu, str);
344 printf(_("Generated time UUID: %s\n"), str);
345 }
346 memcpy(reply_buf, uu, sizeof(uu));
347 reply_len = sizeof(uu);
348 break;
349 case UUIDD_OP_RANDOM_UUID:
350 num = 1;
351 uuid__generate_random(uu, &num);
352 if (debug) {
353 uuid_unparse(uu, str);
354 printf(_("Generated random UUID: %s\n"), str);
355 }
356 memcpy(reply_buf, uu, sizeof(uu));
357 reply_len = sizeof(uu);
358 break;
359 case UUIDD_OP_BULK_TIME_UUID:
360 uuid__generate_time(uu, &num);
361 if (debug) {
362 uuid_unparse(uu, str);
363 printf(_("Generated time UUID %s and %d "
364 "following\n"), str, num);
365 }
366 memcpy(reply_buf, uu, sizeof(uu));
367 reply_len = sizeof(uu);
368 memcpy(reply_buf+reply_len, &num, sizeof(num));
369 reply_len += sizeof(num);
370 break;
371 case UUIDD_OP_BULK_RANDOM_UUID:
372 if (num < 0)
373 num = 1;
374 if (num > 1000)
375 num = 1000;
376 if (num*16 > (int) (sizeof(reply_buf)-sizeof(num)))
377 num = (sizeof(reply_buf)-sizeof(num)) / 16;
378 uuid__generate_random(reply_buf+sizeof(num), &num);
379 if (debug) {
380 printf(_("Generated %d UUID's:\n"), num);
381 for (i=0, cp=reply_buf+sizeof(num);
382 i < num; i++, cp+=16) {
383 uuid_unparse(cp, str);
384 printf("\t%s\n", str);
385 }
386 }
387 reply_len = (num*16) + sizeof(num);
388 memcpy(reply_buf, &num, sizeof(num));
389 break;
390 default:
391 if (debug)
392 printf(_("Invalid operation %d\n"), op);
393 goto shutdown_socket;
394 }
395 write(ns, &reply_len, sizeof(reply_len));
396 write(ns, reply_buf, reply_len);
397 shutdown_socket:
398 close(ns);
399 }
400}
401
Theodore Ts'o740837d2007-12-16 17:21:38 -0500402int main(int argc, char **argv)
403{
404 const char *socket_path = UUIDD_SOCKET_PATH;
405 const char *pidfile_path = UUIDD_PIDFILE_PATH;
406 const char *err_context;
Theodore Ts'o113d6362007-12-25 14:19:15 -0500407 char buf[1024], *cp;
Theodore Ts'o740837d2007-12-16 17:21:38 -0500408 char str[37], *tmp;
409 uuid_t uu;
410 uid_t uid;
411 gid_t gid;
Theodore Ts'ocfa30fd2008-02-15 21:24:30 -0500412 int i, c, ret;
Theodore Ts'o740837d2007-12-16 17:21:38 -0500413 int debug = 0, do_type = 0, do_kill = 0, num = 0;
414 int timeout = 0, quiet = 0, drop_privs = 0;
415
416#ifdef ENABLE_NLS
417 setlocale(LC_MESSAGES, "");
418 setlocale(LC_CTYPE, "");
419 bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
420 textdomain(NLS_CAT_NAME);
421#endif
422
423 while ((c = getopt (argc, argv, "dkn:qp:s:tT:r")) != EOF) {
424 switch (c) {
425 case 'd':
426 debug++;
Matthias Koenig4415b032008-03-14 13:29:35 -0400427 drop_privs = 1;
Theodore Ts'o740837d2007-12-16 17:21:38 -0500428 break;
429 case 'k':
430 do_kill++;
Matthias Koenig4415b032008-03-14 13:29:35 -0400431 drop_privs = 1;
Theodore Ts'o740837d2007-12-16 17:21:38 -0500432 break;
433 case 'n':
434 num = strtol(optarg, &tmp, 0);
435 if ((num < 0) || *tmp) {
436 fprintf(stderr, _("Bad number: %s\n"), optarg);
437 exit(1);
438 }
439 case 'p':
440 pidfile_path = optarg;
Matthias Koenig4415b032008-03-14 13:29:35 -0400441 drop_privs = 1;
Theodore Ts'o740837d2007-12-16 17:21:38 -0500442 break;
443 case 'q':
444 quiet++;
445 break;
446 case 's':
447 socket_path = optarg;
Matthias Koenig4415b032008-03-14 13:29:35 -0400448 drop_privs = 1;
Theodore Ts'o740837d2007-12-16 17:21:38 -0500449 break;
450 case 't':
451 do_type = UUIDD_OP_TIME_UUID;
Matthias Koenig4415b032008-03-14 13:29:35 -0400452 drop_privs = 1;
Theodore Ts'o740837d2007-12-16 17:21:38 -0500453 break;
454 case 'T':
455 timeout = strtol(optarg, &tmp, 0);
456 if ((timeout < 0) || *tmp) {
457 fprintf(stderr, _("Bad number: %s\n"), optarg);
458 exit(1);
459 }
460 break;
461 case 'r':
462 do_type = UUIDD_OP_RANDOM_UUID;
Matthias Koenig4415b032008-03-14 13:29:35 -0400463 drop_privs = 1;
Theodore Ts'o740837d2007-12-16 17:21:38 -0500464 break;
465 default:
466 usage(argv[0]);
467 }
468 }
469 uid = getuid();
470 if (uid && drop_privs) {
471 gid = getgid();
Theodore Ts'o740837d2007-12-16 17:21:38 -0500472#ifdef HAVE_SETRESGID
Matthias Koenig4415b032008-03-14 13:29:35 -0400473 if (setresgid(gid, gid, gid) < 0)
474 die("setresgid");
Theodore Ts'o740837d2007-12-16 17:21:38 -0500475#else
Matthias Koenig4415b032008-03-14 13:29:35 -0400476 if (setregid(gid, gid) < 0)
477 die("setregid");
478#endif
479
480#ifdef HAVE_SETRESUID
481 if (setresuid(uid, uid, uid) < 0)
482 die("setresuid");
483#else
484 if (setreuid(uid, uid) < 0)
485 die("setreuid");
Theodore Ts'o740837d2007-12-16 17:21:38 -0500486#endif
487 }
488 if (num && do_type) {
489 ret = call_daemon(socket_path, do_type+2, buf,
490 sizeof(buf), &num, &err_context);
491 if (ret < 0) {
492 printf(_("Error calling uuidd daemon (%s): %s\n"),
493 err_context, strerror(errno));
494 exit(1);
495 }
496 if (do_type == UUIDD_OP_TIME_UUID) {
497 if (ret != sizeof(uu) + sizeof(num))
498 goto unexpected_size;
499
Theodore Ts'o113d6362007-12-25 14:19:15 -0500500 uuid_unparse((unsigned char *) buf, str);
Theodore Ts'o740837d2007-12-16 17:21:38 -0500501
502 printf(_("%s and subsequent %d UUID's\n"), str, num);
503 } else {
504 printf(_("List of UUID's:\n"));
505 cp = buf + 4;
Theodore Ts'o113d6362007-12-25 14:19:15 -0500506 if (ret != (int) (sizeof(num) + num*sizeof(uu)))
Theodore Ts'o740837d2007-12-16 17:21:38 -0500507 goto unexpected_size;
508 for (i=0; i < num; i++, cp+=16) {
Theodore Ts'o113d6362007-12-25 14:19:15 -0500509 uuid_unparse((unsigned char *) cp, str);
Theodore Ts'o740837d2007-12-16 17:21:38 -0500510 printf("\t%s\n", str);
511 }
512 }
513 exit(0);
514 }
515 if (do_type) {
Theodore Ts'o113d6362007-12-25 14:19:15 -0500516 ret = call_daemon(socket_path, do_type, (char *) &uu,
Theodore Ts'o740837d2007-12-16 17:21:38 -0500517 sizeof(uu), 0, &err_context);
518 if (ret < 0) {
519 printf(_("Error calling uuidd daemon (%s): %s\n"),
520 err_context, strerror(errno));
521 exit(1);
522 }
523 if (ret != sizeof(uu)) {
524 unexpected_size:
525 printf(_("Unexpected reply length from server %d\n"),
526 ret);
527 exit(1);
528 }
529 uuid_unparse(uu, str);
530
531 printf("%s\n", str);
532 exit(0);
533 }
534
Theodore Ts'o113d6362007-12-25 14:19:15 -0500535 if (do_kill) {
536 ret = call_daemon(socket_path, 0, buf, sizeof(buf), 0, 0);
537 if ((ret > 0) && ((do_kill = atoi((char *) buf)) > 0)) {
Theodore Ts'o740837d2007-12-16 17:21:38 -0500538 ret = kill(do_kill, SIGTERM);
539 if (ret < 0) {
540 if (!quiet)
541 fprintf(stderr,
542 _("Couldn't kill uuidd running "
543 "at pid %d: %s\n"), do_kill,
544 strerror(errno));
545 exit(1);
546 }
547 if (!quiet)
548 printf(_("Killed uuidd running at pid %d\n"),
549 do_kill);
Theodore Ts'o740837d2007-12-16 17:21:38 -0500550 }
Theodore Ts'o113d6362007-12-25 14:19:15 -0500551 exit(0);
Theodore Ts'o740837d2007-12-16 17:21:38 -0500552 }
Theodore Ts'o740837d2007-12-16 17:21:38 -0500553
Theodore Ts'ocfa30fd2008-02-15 21:24:30 -0500554 server_loop(socket_path, pidfile_path, debug, timeout, quiet);
Theodore Ts'o740837d2007-12-16 17:21:38 -0500555 return 0;
556}