Theodore Ts'o | 740837d | 2007-12-16 17:21:38 -0500 | [diff] [blame] | 1 | /* |
| 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'o | e5679a6 | 2008-01-01 09:16:16 -0500 | [diff] [blame] | 12 | #define _GNU_SOURCE /* for setres[ug]id() */ |
| 13 | |
Theodore Ts'o | 740837d | 2007-12-16 17:21:38 -0500 | [diff] [blame] | 14 | #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'o | cfa30fd | 2008-02-15 21:24:30 -0500 | [diff] [blame] | 27 | #include <string.h> |
Theodore Ts'o | 740837d | 2007-12-16 17:21:38 -0500 | [diff] [blame] | 28 | #ifdef HAVE_GETOPT_H |
| 29 | #include <getopt.h> |
| 30 | #else |
| 31 | extern int getopt(int argc, char * const argv[], const char *optstring); |
| 32 | extern char *optarg; |
| 33 | extern 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 | |
| 45 | static 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 Koenig | 4415b03 | 2008-03-14 13:29:35 -0400 | [diff] [blame] | 55 | static void die(const char *msg) |
| 56 | { |
| 57 | perror(msg); |
| 58 | exit(1); |
| 59 | } |
| 60 | |
Theodore Ts'o | 113d636 | 2007-12-25 14:19:15 -0500 | [diff] [blame] | 61 | static void create_daemon(void) |
Theodore Ts'o | 740837d | 2007-12-16 17:21:38 -0500 | [diff] [blame] | 62 | { |
| 63 | pid_t pid; |
| 64 | uid_t euid; |
Theodore Ts'o | 740837d | 2007-12-16 17:21:38 -0500 | [diff] [blame] | 65 | |
| 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 Koenig | 4415b03 | 2008-03-14 13:29:35 -0400 | [diff] [blame] | 84 | if (setreuid(euid, euid) < 0) |
| 85 | die("setreuid"); |
Theodore Ts'o | 740837d | 2007-12-16 17:21:38 -0500 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | static 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 | |
| 108 | static const char *cleanup_pidfile, *cleanup_socket; |
| 109 | |
| 110 | static void terminate_intr(int signo CODE_ATTR((unused))) |
| 111 | { |
| 112 | (void) unlink(cleanup_pidfile); |
Theodore Ts'o | 113d636 | 2007-12-25 14:19:15 -0500 | [diff] [blame] | 113 | if (cleanup_socket) |
| 114 | (void) unlink(cleanup_socket); |
Theodore Ts'o | 740837d | 2007-12-16 17:21:38 -0500 | [diff] [blame] | 115 | exit(0); |
| 116 | } |
| 117 | |
Theodore Ts'o | cfa30fd | 2008-02-15 21:24:30 -0500 | [diff] [blame] | 118 | static 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 Koenig | 4415b03 | 2008-03-14 13:29:35 -0400 | [diff] [blame] | 142 | 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'o | cfa30fd | 2008-02-15 21:24:30 -0500 | [diff] [blame] | 144 | |
| 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 | |
| 205 | static void server_loop(const char *socket_path, const char *pidfile_path, |
| 206 | int debug, int timeout, int quiet) |
Theodore Ts'o | 740837d | 2007-12-16 17:21:38 -0500 | [diff] [blame] | 207 | { |
| 208 | struct sockaddr_un my_addr, from_addr; |
Theodore Ts'o | cfa30fd | 2008-02-15 21:24:30 -0500 | [diff] [blame] | 209 | struct flock fl; |
Theodore Ts'o | 740837d | 2007-12-16 17:21:38 -0500 | [diff] [blame] | 210 | socklen_t fromlen; |
| 211 | int32_t reply_len = 0; |
| 212 | uuid_t uu; |
| 213 | mode_t save_umask; |
Theodore Ts'o | 2d328bb | 2008-03-17 23:17:13 -0400 | [diff] [blame] | 214 | char reply_buf[1024], *cp; |
Theodore Ts'o | 740837d | 2007-12-16 17:21:38 -0500 | [diff] [blame] | 215 | char op, str[37]; |
| 216 | int i, s, ns, len, num; |
Theodore Ts'o | cfa30fd | 2008-02-15 21:24:30 -0500 | [diff] [blame] | 217 | 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'o | 740837d | 2007-12-16 17:21:38 -0500 | [diff] [blame] | 251 | |
| 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 Koenig | 4415b03 | 2008-03-14 13:29:35 -0400 | [diff] [blame] | 263 | 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'o | 740837d | 2007-12-16 17:21:38 -0500 | [diff] [blame] | 265 | (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'o | 113d636 | 2007-12-25 14:19:15 -0500 | [diff] [blame] | 285 | 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'o | 740837d | 2007-12-16 17:21:38 -0500 | [diff] [blame] | 292 | signal(SIGPIPE, SIG_IGN); |
| 293 | |
Theodore Ts'o | cfa30fd | 2008-02-15 21:24:30 -0500 | [diff] [blame] | 294 | 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'o | 740837d | 2007-12-16 17:21:38 -0500 | [diff] [blame] | 300 | 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: |
Theodore Ts'o | 2d328bb | 2008-03-17 23:17:13 -0400 | [diff] [blame] | 332 | sprintf(reply_buf, "%d", getpid()); |
| 333 | reply_len = strlen(reply_buf)+1; |
Theodore Ts'o | 740837d | 2007-12-16 17:21:38 -0500 | [diff] [blame] | 334 | break; |
| 335 | case UUIDD_OP_GET_MAXOP: |
Theodore Ts'o | 2d328bb | 2008-03-17 23:17:13 -0400 | [diff] [blame] | 336 | sprintf(reply_buf, "%d", UUIDD_MAX_OP); |
| 337 | reply_len = strlen(reply_buf)+1; |
Theodore Ts'o | 740837d | 2007-12-16 17:21:38 -0500 | [diff] [blame] | 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; |
Theodore Ts'o | 2d328bb | 2008-03-17 23:17:13 -0400 | [diff] [blame] | 378 | uuid__generate_random((unsigned char *) reply_buf + |
| 379 | sizeof(num), &num); |
Theodore Ts'o | 740837d | 2007-12-16 17:21:38 -0500 | [diff] [blame] | 380 | if (debug) { |
| 381 | printf(_("Generated %d UUID's:\n"), num); |
| 382 | for (i=0, cp=reply_buf+sizeof(num); |
| 383 | i < num; i++, cp+=16) { |
Theodore Ts'o | 2d328bb | 2008-03-17 23:17:13 -0400 | [diff] [blame] | 384 | uuid_unparse((unsigned char *)cp, str); |
Theodore Ts'o | 740837d | 2007-12-16 17:21:38 -0500 | [diff] [blame] | 385 | printf("\t%s\n", str); |
| 386 | } |
| 387 | } |
| 388 | reply_len = (num*16) + sizeof(num); |
| 389 | memcpy(reply_buf, &num, sizeof(num)); |
| 390 | break; |
| 391 | default: |
| 392 | if (debug) |
| 393 | printf(_("Invalid operation %d\n"), op); |
| 394 | goto shutdown_socket; |
| 395 | } |
| 396 | write(ns, &reply_len, sizeof(reply_len)); |
| 397 | write(ns, reply_buf, reply_len); |
| 398 | shutdown_socket: |
| 399 | close(ns); |
| 400 | } |
| 401 | } |
| 402 | |
Theodore Ts'o | 740837d | 2007-12-16 17:21:38 -0500 | [diff] [blame] | 403 | int main(int argc, char **argv) |
| 404 | { |
| 405 | const char *socket_path = UUIDD_SOCKET_PATH; |
| 406 | const char *pidfile_path = UUIDD_PIDFILE_PATH; |
| 407 | const char *err_context; |
Theodore Ts'o | 113d636 | 2007-12-25 14:19:15 -0500 | [diff] [blame] | 408 | char buf[1024], *cp; |
Theodore Ts'o | 740837d | 2007-12-16 17:21:38 -0500 | [diff] [blame] | 409 | char str[37], *tmp; |
| 410 | uuid_t uu; |
| 411 | uid_t uid; |
| 412 | gid_t gid; |
Theodore Ts'o | cfa30fd | 2008-02-15 21:24:30 -0500 | [diff] [blame] | 413 | int i, c, ret; |
Theodore Ts'o | 740837d | 2007-12-16 17:21:38 -0500 | [diff] [blame] | 414 | int debug = 0, do_type = 0, do_kill = 0, num = 0; |
| 415 | int timeout = 0, quiet = 0, drop_privs = 0; |
| 416 | |
| 417 | #ifdef ENABLE_NLS |
| 418 | setlocale(LC_MESSAGES, ""); |
| 419 | setlocale(LC_CTYPE, ""); |
| 420 | bindtextdomain(NLS_CAT_NAME, LOCALEDIR); |
| 421 | textdomain(NLS_CAT_NAME); |
| 422 | #endif |
| 423 | |
| 424 | while ((c = getopt (argc, argv, "dkn:qp:s:tT:r")) != EOF) { |
| 425 | switch (c) { |
| 426 | case 'd': |
| 427 | debug++; |
Matthias Koenig | 4415b03 | 2008-03-14 13:29:35 -0400 | [diff] [blame] | 428 | drop_privs = 1; |
Theodore Ts'o | 740837d | 2007-12-16 17:21:38 -0500 | [diff] [blame] | 429 | break; |
| 430 | case 'k': |
| 431 | do_kill++; |
Matthias Koenig | 4415b03 | 2008-03-14 13:29:35 -0400 | [diff] [blame] | 432 | drop_privs = 1; |
Theodore Ts'o | 740837d | 2007-12-16 17:21:38 -0500 | [diff] [blame] | 433 | break; |
| 434 | case 'n': |
| 435 | num = strtol(optarg, &tmp, 0); |
| 436 | if ((num < 0) || *tmp) { |
| 437 | fprintf(stderr, _("Bad number: %s\n"), optarg); |
| 438 | exit(1); |
| 439 | } |
| 440 | case 'p': |
| 441 | pidfile_path = optarg; |
Matthias Koenig | 4415b03 | 2008-03-14 13:29:35 -0400 | [diff] [blame] | 442 | drop_privs = 1; |
Theodore Ts'o | 740837d | 2007-12-16 17:21:38 -0500 | [diff] [blame] | 443 | break; |
| 444 | case 'q': |
| 445 | quiet++; |
| 446 | break; |
| 447 | case 's': |
| 448 | socket_path = optarg; |
Matthias Koenig | 4415b03 | 2008-03-14 13:29:35 -0400 | [diff] [blame] | 449 | drop_privs = 1; |
Theodore Ts'o | 740837d | 2007-12-16 17:21:38 -0500 | [diff] [blame] | 450 | break; |
| 451 | case 't': |
| 452 | do_type = UUIDD_OP_TIME_UUID; |
Matthias Koenig | 4415b03 | 2008-03-14 13:29:35 -0400 | [diff] [blame] | 453 | drop_privs = 1; |
Theodore Ts'o | 740837d | 2007-12-16 17:21:38 -0500 | [diff] [blame] | 454 | break; |
| 455 | case 'T': |
| 456 | timeout = strtol(optarg, &tmp, 0); |
| 457 | if ((timeout < 0) || *tmp) { |
| 458 | fprintf(stderr, _("Bad number: %s\n"), optarg); |
| 459 | exit(1); |
| 460 | } |
| 461 | break; |
| 462 | case 'r': |
| 463 | do_type = UUIDD_OP_RANDOM_UUID; |
Matthias Koenig | 4415b03 | 2008-03-14 13:29:35 -0400 | [diff] [blame] | 464 | drop_privs = 1; |
Theodore Ts'o | 740837d | 2007-12-16 17:21:38 -0500 | [diff] [blame] | 465 | break; |
| 466 | default: |
| 467 | usage(argv[0]); |
| 468 | } |
| 469 | } |
| 470 | uid = getuid(); |
| 471 | if (uid && drop_privs) { |
| 472 | gid = getgid(); |
Theodore Ts'o | 740837d | 2007-12-16 17:21:38 -0500 | [diff] [blame] | 473 | #ifdef HAVE_SETRESGID |
Matthias Koenig | 4415b03 | 2008-03-14 13:29:35 -0400 | [diff] [blame] | 474 | if (setresgid(gid, gid, gid) < 0) |
| 475 | die("setresgid"); |
Theodore Ts'o | 740837d | 2007-12-16 17:21:38 -0500 | [diff] [blame] | 476 | #else |
Matthias Koenig | 4415b03 | 2008-03-14 13:29:35 -0400 | [diff] [blame] | 477 | if (setregid(gid, gid) < 0) |
| 478 | die("setregid"); |
| 479 | #endif |
| 480 | |
| 481 | #ifdef HAVE_SETRESUID |
| 482 | if (setresuid(uid, uid, uid) < 0) |
| 483 | die("setresuid"); |
| 484 | #else |
| 485 | if (setreuid(uid, uid) < 0) |
| 486 | die("setreuid"); |
Theodore Ts'o | 740837d | 2007-12-16 17:21:38 -0500 | [diff] [blame] | 487 | #endif |
| 488 | } |
| 489 | if (num && do_type) { |
| 490 | ret = call_daemon(socket_path, do_type+2, buf, |
| 491 | sizeof(buf), &num, &err_context); |
| 492 | if (ret < 0) { |
| 493 | printf(_("Error calling uuidd daemon (%s): %s\n"), |
| 494 | err_context, strerror(errno)); |
| 495 | exit(1); |
| 496 | } |
| 497 | if (do_type == UUIDD_OP_TIME_UUID) { |
| 498 | if (ret != sizeof(uu) + sizeof(num)) |
| 499 | goto unexpected_size; |
| 500 | |
Theodore Ts'o | 113d636 | 2007-12-25 14:19:15 -0500 | [diff] [blame] | 501 | uuid_unparse((unsigned char *) buf, str); |
Theodore Ts'o | 740837d | 2007-12-16 17:21:38 -0500 | [diff] [blame] | 502 | |
| 503 | printf(_("%s and subsequent %d UUID's\n"), str, num); |
| 504 | } else { |
| 505 | printf(_("List of UUID's:\n")); |
| 506 | cp = buf + 4; |
Theodore Ts'o | 113d636 | 2007-12-25 14:19:15 -0500 | [diff] [blame] | 507 | if (ret != (int) (sizeof(num) + num*sizeof(uu))) |
Theodore Ts'o | 740837d | 2007-12-16 17:21:38 -0500 | [diff] [blame] | 508 | goto unexpected_size; |
| 509 | for (i=0; i < num; i++, cp+=16) { |
Theodore Ts'o | 113d636 | 2007-12-25 14:19:15 -0500 | [diff] [blame] | 510 | uuid_unparse((unsigned char *) cp, str); |
Theodore Ts'o | 740837d | 2007-12-16 17:21:38 -0500 | [diff] [blame] | 511 | printf("\t%s\n", str); |
| 512 | } |
| 513 | } |
| 514 | exit(0); |
| 515 | } |
| 516 | if (do_type) { |
Theodore Ts'o | 113d636 | 2007-12-25 14:19:15 -0500 | [diff] [blame] | 517 | ret = call_daemon(socket_path, do_type, (char *) &uu, |
Theodore Ts'o | 740837d | 2007-12-16 17:21:38 -0500 | [diff] [blame] | 518 | sizeof(uu), 0, &err_context); |
| 519 | if (ret < 0) { |
| 520 | printf(_("Error calling uuidd daemon (%s): %s\n"), |
| 521 | err_context, strerror(errno)); |
| 522 | exit(1); |
| 523 | } |
| 524 | if (ret != sizeof(uu)) { |
| 525 | unexpected_size: |
| 526 | printf(_("Unexpected reply length from server %d\n"), |
| 527 | ret); |
| 528 | exit(1); |
| 529 | } |
| 530 | uuid_unparse(uu, str); |
| 531 | |
| 532 | printf("%s\n", str); |
| 533 | exit(0); |
| 534 | } |
| 535 | |
Theodore Ts'o | 113d636 | 2007-12-25 14:19:15 -0500 | [diff] [blame] | 536 | if (do_kill) { |
| 537 | ret = call_daemon(socket_path, 0, buf, sizeof(buf), 0, 0); |
| 538 | if ((ret > 0) && ((do_kill = atoi((char *) buf)) > 0)) { |
Theodore Ts'o | 740837d | 2007-12-16 17:21:38 -0500 | [diff] [blame] | 539 | ret = kill(do_kill, SIGTERM); |
| 540 | if (ret < 0) { |
| 541 | if (!quiet) |
| 542 | fprintf(stderr, |
| 543 | _("Couldn't kill uuidd running " |
| 544 | "at pid %d: %s\n"), do_kill, |
| 545 | strerror(errno)); |
| 546 | exit(1); |
| 547 | } |
| 548 | if (!quiet) |
| 549 | printf(_("Killed uuidd running at pid %d\n"), |
| 550 | do_kill); |
Theodore Ts'o | 740837d | 2007-12-16 17:21:38 -0500 | [diff] [blame] | 551 | } |
Theodore Ts'o | 113d636 | 2007-12-25 14:19:15 -0500 | [diff] [blame] | 552 | exit(0); |
Theodore Ts'o | 740837d | 2007-12-16 17:21:38 -0500 | [diff] [blame] | 553 | } |
Theodore Ts'o | 740837d | 2007-12-16 17:21:38 -0500 | [diff] [blame] | 554 | |
Theodore Ts'o | cfa30fd | 2008-02-15 21:24:30 -0500 | [diff] [blame] | 555 | server_loop(socket_path, pidfile_path, debug, timeout, quiet); |
Theodore Ts'o | 740837d | 2007-12-16 17:21:38 -0500 | [diff] [blame] | 556 | return 0; |
| 557 | } |