blob: 2fb10575189f4b32d7c20d182bf2a24196916ab9 [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>
27#ifdef HAVE_GETOPT_H
28#include <getopt.h>
29#else
30extern int getopt(int argc, char * const argv[], const char *optstring);
31extern char *optarg;
32extern int optind;
33#endif
34#include "uuid/uuid.h"
35#include "uuid/uuidd.h"
36#include "nls-enable.h"
37
38#ifdef __GNUC__
39#define CODE_ATTR(x) __attribute__(x)
40#else
41#define CODE_ATTR(x)
42#endif
43
44static void usage(const char *progname)
45{
46 fprintf(stderr, _("Usage: %s [-d] [-p pidfile] [-s socketpath] "
47 "[-T timeout]\n"), progname);
48 fprintf(stderr, _(" %s [-r|t] [-n num] [-s socketpath]\n"),
49 progname);
50 fprintf(stderr, _(" %s -k\n"), progname);
51 exit(1);
52}
53
Theodore Ts'o113d6362007-12-25 14:19:15 -050054static void create_daemon(void)
Theodore Ts'o740837d2007-12-16 17:21:38 -050055{
56 pid_t pid;
57 uid_t euid;
Theodore Ts'o740837d2007-12-16 17:21:38 -050058
59 pid = fork();
60 if (pid == -1) {
61 perror("fork");
62 exit(1);
63 } else if (pid != 0) {
64 exit(0);
65 }
66
67 close(0);
68 close(1);
69 close(2);
70 open("/dev/null", O_RDWR);
71 open("/dev/null", O_RDWR);
72 open("/dev/null", O_RDWR);
73
74 chdir("/");
75 (void) setsid();
76 euid = geteuid();
77 (void) setreuid(euid, euid);
Theodore Ts'o740837d2007-12-16 17:21:38 -050078}
79
80static int read_all(int fd, char *buf, size_t count)
81{
82 ssize_t ret;
83 int c = 0;
84
85 memset(buf, 0, count);
86 while (count > 0) {
87 ret = read(fd, buf, count);
88 if (ret < 0) {
89 if ((errno == EAGAIN) || (errno == EINTR))
90 continue;
91 return -1;
92 }
93 count -= ret;
94 buf += ret;
95 c += ret;
96 }
97 return c;
98}
99
100static const char *cleanup_pidfile, *cleanup_socket;
101
102static void terminate_intr(int signo CODE_ATTR((unused)))
103{
104 (void) unlink(cleanup_pidfile);
Theodore Ts'o113d6362007-12-25 14:19:15 -0500105 if (cleanup_socket)
106 (void) unlink(cleanup_socket);
Theodore Ts'o740837d2007-12-16 17:21:38 -0500107 exit(0);
108}
109
110static void server_loop(const char *socket_path, int debug,
Theodore Ts'o113d6362007-12-25 14:19:15 -0500111 int fd_pidfile, int timeout, int quiet)
Theodore Ts'o740837d2007-12-16 17:21:38 -0500112{
113 struct sockaddr_un my_addr, from_addr;
114 unsigned char reply_buf[1024], *cp;
115 socklen_t fromlen;
116 int32_t reply_len = 0;
117 uuid_t uu;
118 mode_t save_umask;
119 char op, str[37];
120 int i, s, ns, len, num;
121
122 if ((s = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
123 if (!quiet)
124 fprintf(stderr, _("Couldn't create unix stream "
125 "socket: %s"), strerror(errno));
126 exit(1);
127 }
128
129 /*
130 * Create the address we will be binding to.
131 */
132 my_addr.sun_family = AF_UNIX;
133 strcpy(my_addr.sun_path, socket_path);
134 (void) unlink(socket_path);
135 save_umask = umask(0);
136 if (bind(s, (const struct sockaddr *) &my_addr,
137 sizeof(struct sockaddr_un)) < 0) {
138 if (!quiet)
139 fprintf(stderr,
140 _("Couldn't bind unix socket %s: %s\n"),
141 socket_path, strerror(errno));
142 exit(1);
143 }
144 (void) umask(save_umask);
145
146 if (listen(s, 5) < 0) {
147 if (!quiet)
148 fprintf(stderr, _("Couldn't listen on unix "
149 "socket %s: %s\n"), socket_path,
150 strerror(errno));
151 exit(1);
152 }
153
Theodore Ts'o113d6362007-12-25 14:19:15 -0500154 if (fd_pidfile > 1)
155 close(fd_pidfile); /* Unlock the pid file */
156 cleanup_socket = socket_path;
157 if (!debug)
158 create_daemon();
159 signal(SIGHUP, terminate_intr);
160 signal(SIGINT, terminate_intr);
161 signal(SIGTERM, terminate_intr);
162 signal(SIGALRM, terminate_intr);
Theodore Ts'o740837d2007-12-16 17:21:38 -0500163 signal(SIGPIPE, SIG_IGN);
164
165 while (1) {
166 fromlen = sizeof(from_addr);
167 if (timeout > 0)
168 alarm(timeout);
169 ns = accept(s, (struct sockaddr *) &from_addr, &fromlen);
170 alarm(0);
171 if (ns < 0) {
172 if ((errno == EAGAIN) || (errno == EINTR))
173 continue;
174 perror("accept");
175 exit(1);
176 }
177 len = read(ns, &op, 1);
178 if (len != 1) {
179 if (len < 0)
180 perror("read");
181 else
182 printf(_("Error reading from client, "
183 "len = %d\n"), len);
184 goto shutdown_socket;
185 }
186 if ((op == 4) || (op == 5)) {
187 if (read_all(ns, (char *) &num, sizeof(num)) != 4)
188 goto shutdown_socket;
189 if (debug)
190 printf(_("operation %d, incoming num = %d\n"),
191 op, num);
192 } else if (debug)
193 printf("operation %d\n", op);
194
195 switch(op) {
196 case UUIDD_OP_GETPID:
197 sprintf((char *) reply_buf, "%d", getpid());
198 reply_len = strlen((char *) reply_buf)+1;
199 break;
200 case UUIDD_OP_GET_MAXOP:
201 sprintf((char *) reply_buf, "%d", UUIDD_MAX_OP);
202 reply_len = strlen((char *) reply_buf)+1;
203 break;
204 case UUIDD_OP_TIME_UUID:
205 num = 1;
206 uuid__generate_time(uu, &num);
207 if (debug) {
208 uuid_unparse(uu, str);
209 printf(_("Generated time UUID: %s\n"), str);
210 }
211 memcpy(reply_buf, uu, sizeof(uu));
212 reply_len = sizeof(uu);
213 break;
214 case UUIDD_OP_RANDOM_UUID:
215 num = 1;
216 uuid__generate_random(uu, &num);
217 if (debug) {
218 uuid_unparse(uu, str);
219 printf(_("Generated random UUID: %s\n"), str);
220 }
221 memcpy(reply_buf, uu, sizeof(uu));
222 reply_len = sizeof(uu);
223 break;
224 case UUIDD_OP_BULK_TIME_UUID:
225 uuid__generate_time(uu, &num);
226 if (debug) {
227 uuid_unparse(uu, str);
228 printf(_("Generated time UUID %s and %d "
229 "following\n"), str, num);
230 }
231 memcpy(reply_buf, uu, sizeof(uu));
232 reply_len = sizeof(uu);
233 memcpy(reply_buf+reply_len, &num, sizeof(num));
234 reply_len += sizeof(num);
235 break;
236 case UUIDD_OP_BULK_RANDOM_UUID:
237 if (num < 0)
238 num = 1;
239 if (num > 1000)
240 num = 1000;
241 if (num*16 > (int) (sizeof(reply_buf)-sizeof(num)))
242 num = (sizeof(reply_buf)-sizeof(num)) / 16;
243 uuid__generate_random(reply_buf+sizeof(num), &num);
244 if (debug) {
245 printf(_("Generated %d UUID's:\n"), num);
246 for (i=0, cp=reply_buf+sizeof(num);
247 i < num; i++, cp+=16) {
248 uuid_unparse(cp, str);
249 printf("\t%s\n", str);
250 }
251 }
252 reply_len = (num*16) + sizeof(num);
253 memcpy(reply_buf, &num, sizeof(num));
254 break;
255 default:
256 if (debug)
257 printf(_("Invalid operation %d\n"), op);
258 goto shutdown_socket;
259 }
260 write(ns, &reply_len, sizeof(reply_len));
261 write(ns, reply_buf, reply_len);
262 shutdown_socket:
263 close(ns);
264 }
265}
266
Theodore Ts'o113d6362007-12-25 14:19:15 -0500267static int call_daemon(const char *socket_path, int op, char *buf,
Theodore Ts'o740837d2007-12-16 17:21:38 -0500268 int buflen, int *num, const char **err_context)
269{
270 char op_buf[8];
271 int op_len;
272 int s;
273 ssize_t ret;
274 int32_t reply_len = 0;
275 struct sockaddr_un srv_addr;
276
277 if (((op == 4) || (op == 5)) && !num) {
278 if (err_context)
279 *err_context = _("bad arguments");
280 errno = EINVAL;
281 return -1;
282 }
283
284 if ((s = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
285 if (err_context)
286 *err_context = _("socket");
287 return -1;
288 }
289
290 srv_addr.sun_family = AF_UNIX;
291 strcpy(srv_addr.sun_path, socket_path);
292
293 if (connect(s, (const struct sockaddr *) &srv_addr,
294 sizeof(struct sockaddr_un)) < 0) {
295 if (err_context)
296 *err_context = _("connect");
297 close(s);
298 return -1;
299 }
300
301 if (op == 5) {
302 if ((*num)*16 > buflen-4)
303 *num = (buflen-4) / 16;
304 }
305 op_buf[0] = op;
306 op_len = 1;
307 if ((op == 4) || (op == 5)) {
308 memcpy(op_buf+1, num, sizeof(int));
309 op_len += sizeof(int);
310 }
311
312 ret = write(s, op_buf, op_len);
313 if (ret < op_len) {
314 if (err_context)
315 *err_context = _("write");
316 close(s);
317 return -1;
318 }
319
320 ret = read_all(s, (char *) &reply_len, sizeof(reply_len));
321 if (ret < 0) {
322 if (err_context)
323 *err_context = _("read count");
324 close(s);
325 return -1;
326 }
327 if (reply_len < 0 || reply_len > buflen) {
328 if (err_context)
329 *err_context = _("bad response length");
330 close(s);
331 return -1;
332 }
333 ret = read_all(s, (char *) buf, reply_len);
334
335 if ((ret > 0) && (op == 4)) {
336 if (reply_len >= (int) (16+sizeof(int)))
337 memcpy(buf+16, num, sizeof(int));
338 else
339 *num = -1;
340 }
341 if ((ret > 0) && (op == 5)) {
342 if (*num >= (int) sizeof(int))
343 memcpy(buf, num, sizeof(int));
344 else
345 *num = -1;
346 }
347
348 close(s);
349
350 return ret;
351}
352
Theodore Ts'o113d6362007-12-25 14:19:15 -0500353static int create_pidfile(const char *socket_path, const char *pidfile_path,
354 int quiet)
355{
356 int fd, ret;
357 char buf[20];
358
359 fd = open(pidfile_path, O_CREAT | O_RDWR, 0664);
360 if (fd < 0) {
361 if (!quiet)
362 fprintf(stderr, "Failed to open/create %s: %s\n",
363 pidfile_path, strerror(errno));
364 exit(1);
365 }
366 cleanup_pidfile = pidfile_path;
367 cleanup_socket = 0;
368 signal(SIGALRM, terminate_intr);
369 alarm(30);
370 if (lockf(fd, F_LOCK, 0) < 0) {
371 if (!quiet)
372 fprintf(stderr, "Failed to lock %s: %s\n",
373 pidfile_path, strerror(errno));
374 exit(1);
375 }
376 ret = call_daemon(socket_path, 0, buf, sizeof(buf), 0, 0);
377 if (ret > 0) {
378 if (!quiet)
379 printf(_("uuidd daemon already running at pid %s\n"),
380 buf);
381 exit(1);
382 }
383 alarm(0);
384
385 sprintf(buf, "%d\n", getpid());
386 ftruncate(fd, 0);
387 write(fd, buf, strlen(buf));
388 return(fd);
389}
Theodore Ts'o740837d2007-12-16 17:21:38 -0500390
391int main(int argc, char **argv)
392{
393 const char *socket_path = UUIDD_SOCKET_PATH;
394 const char *pidfile_path = UUIDD_PIDFILE_PATH;
395 const char *err_context;
Theodore Ts'o113d6362007-12-25 14:19:15 -0500396 char buf[1024], *cp;
Theodore Ts'o740837d2007-12-16 17:21:38 -0500397 char str[37], *tmp;
398 uuid_t uu;
399 uid_t uid;
400 gid_t gid;
Theodore Ts'o113d6362007-12-25 14:19:15 -0500401 int i, c, ret, fd_pidfile = -1;
Theodore Ts'o740837d2007-12-16 17:21:38 -0500402 int debug = 0, do_type = 0, do_kill = 0, num = 0;
403 int timeout = 0, quiet = 0, drop_privs = 0;
404
405#ifdef ENABLE_NLS
406 setlocale(LC_MESSAGES, "");
407 setlocale(LC_CTYPE, "");
408 bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
409 textdomain(NLS_CAT_NAME);
410#endif
411
412 while ((c = getopt (argc, argv, "dkn:qp:s:tT:r")) != EOF) {
413 switch (c) {
414 case 'd':
415 debug++;
416 drop_privs++;
417 break;
418 case 'k':
419 do_kill++;
420 drop_privs++;
421 break;
422 case 'n':
423 num = strtol(optarg, &tmp, 0);
424 if ((num < 0) || *tmp) {
425 fprintf(stderr, _("Bad number: %s\n"), optarg);
426 exit(1);
427 }
428 case 'p':
429 pidfile_path = optarg;
430 drop_privs++;
431 break;
432 case 'q':
433 quiet++;
434 break;
435 case 's':
436 socket_path = optarg;
437 drop_privs++;
438 break;
439 case 't':
440 do_type = UUIDD_OP_TIME_UUID;
441 drop_privs++;
442 break;
443 case 'T':
444 timeout = strtol(optarg, &tmp, 0);
445 if ((timeout < 0) || *tmp) {
446 fprintf(stderr, _("Bad number: %s\n"), optarg);
447 exit(1);
448 }
449 break;
450 case 'r':
451 do_type = UUIDD_OP_RANDOM_UUID;
452 drop_privs++;
453 break;
454 default:
455 usage(argv[0]);
456 }
457 }
458 uid = getuid();
459 if (uid && drop_privs) {
460 gid = getgid();
461#ifdef HAVE_SETRESUID
462 setresuid(uid, uid, uid);
463#else
464 setreuid(uid, uid);
465#endif
466#ifdef HAVE_SETRESGID
467 setresgid(gid, gid, gid);
468#else
469 setregid(gid, gid);
470#endif
471 }
472 if (num && do_type) {
473 ret = call_daemon(socket_path, do_type+2, buf,
474 sizeof(buf), &num, &err_context);
475 if (ret < 0) {
476 printf(_("Error calling uuidd daemon (%s): %s\n"),
477 err_context, strerror(errno));
478 exit(1);
479 }
480 if (do_type == UUIDD_OP_TIME_UUID) {
481 if (ret != sizeof(uu) + sizeof(num))
482 goto unexpected_size;
483
Theodore Ts'o113d6362007-12-25 14:19:15 -0500484 uuid_unparse((unsigned char *) buf, str);
Theodore Ts'o740837d2007-12-16 17:21:38 -0500485
486 printf(_("%s and subsequent %d UUID's\n"), str, num);
487 } else {
488 printf(_("List of UUID's:\n"));
489 cp = buf + 4;
Theodore Ts'o113d6362007-12-25 14:19:15 -0500490 if (ret != (int) (sizeof(num) + num*sizeof(uu)))
Theodore Ts'o740837d2007-12-16 17:21:38 -0500491 goto unexpected_size;
492 for (i=0; i < num; i++, cp+=16) {
Theodore Ts'o113d6362007-12-25 14:19:15 -0500493 uuid_unparse((unsigned char *) cp, str);
Theodore Ts'o740837d2007-12-16 17:21:38 -0500494 printf("\t%s\n", str);
495 }
496 }
497 exit(0);
498 }
499 if (do_type) {
Theodore Ts'o113d6362007-12-25 14:19:15 -0500500 ret = call_daemon(socket_path, do_type, (char *) &uu,
Theodore Ts'o740837d2007-12-16 17:21:38 -0500501 sizeof(uu), 0, &err_context);
502 if (ret < 0) {
503 printf(_("Error calling uuidd daemon (%s): %s\n"),
504 err_context, strerror(errno));
505 exit(1);
506 }
507 if (ret != sizeof(uu)) {
508 unexpected_size:
509 printf(_("Unexpected reply length from server %d\n"),
510 ret);
511 exit(1);
512 }
513 uuid_unparse(uu, str);
514
515 printf("%s\n", str);
516 exit(0);
517 }
518
Theodore Ts'o113d6362007-12-25 14:19:15 -0500519 if (do_kill) {
520 ret = call_daemon(socket_path, 0, buf, sizeof(buf), 0, 0);
521 if ((ret > 0) && ((do_kill = atoi((char *) buf)) > 0)) {
Theodore Ts'o740837d2007-12-16 17:21:38 -0500522 ret = kill(do_kill, SIGTERM);
523 if (ret < 0) {
524 if (!quiet)
525 fprintf(stderr,
526 _("Couldn't kill uuidd running "
527 "at pid %d: %s\n"), do_kill,
528 strerror(errno));
529 exit(1);
530 }
531 if (!quiet)
532 printf(_("Killed uuidd running at pid %d\n"),
533 do_kill);
Theodore Ts'o740837d2007-12-16 17:21:38 -0500534 }
Theodore Ts'o113d6362007-12-25 14:19:15 -0500535 exit(0);
Theodore Ts'o740837d2007-12-16 17:21:38 -0500536 }
Theodore Ts'o740837d2007-12-16 17:21:38 -0500537
Theodore Ts'o113d6362007-12-25 14:19:15 -0500538 fd_pidfile = create_pidfile(socket_path, pidfile_path, quiet);
539
540 server_loop(socket_path, debug, fd_pidfile, timeout, quiet);
Theodore Ts'o740837d2007-12-16 17:21:38 -0500541 return 0;
542}