Revision 1bc26957 sysdep/unix/main.c

View differences:

sysdep/unix/main.c
8 8

  
9 9
#undef LOCAL_DEBUG
10 10

  
11
#define _GNU_SOURCE 1
12

  
11 13
#include <stdio.h>
12 14
#include <stdlib.h>
13 15
#include <fcntl.h>
14 16
#include <unistd.h>
15 17
#include <signal.h>
18
#include <pwd.h>
19
#include <grp.h>
16 20

  
17 21
#include "nest/bird.h"
18 22
#include "lib/lists.h"
......
59 63
}
60 64

  
61 65
/*
66
 *	Dropping privileges
67
 */
68

  
69
#ifdef CONFIG_RESTRICTED_PRIVILEGES
70
#include "lib/syspriv.h"
71
#else
72

  
73
static inline void
74
drop_uid(uid_t uid)
75
{
76
  die("Cannot change user on this platform");
77
}
78

  
79
#endif
80

  
81
static inline void
82
drop_gid(gid_t gid)
83
{
84
  if (setgid(gid) < 0)
85
    die("setgid: %m");
86
}
87

  
88
/*
62 89
 *	Reading the Configuration
63 90
 */
64 91

  
......
444 471
 *	Parsing of command-line arguments
445 472
 */
446 473

  
447
static char *opt_list = "c:dD:ps:";
474
static char *opt_list = "c:dD:ps:u:g:";
448 475
static int parse_and_exit;
449 476
char *bird_name;
477
static char *use_user;
478
static char *use_group;
450 479

  
451 480
static void
452 481
usage(void)
453 482
{
454
  fprintf(stderr, "Usage: %s [-c <config-file>] [-d] [-D <debug-file>] [-p] [-s <control-socket>]\n", bird_name);
483
  fprintf(stderr, "Usage: %s [-c <config-file>] [-d] [-D <debug-file>] [-p] [-s <control-socket>] [-u <user>] [-g <group>]\n", bird_name);
455 484
  exit(1);
456 485
}
457 486

  
......
469 498
  return t+1;
470 499
}
471 500

  
501
static inline uid_t
502
get_uid(const char *s)
503
{
504
  struct passwd *pw;
505
  char *endptr;
506
  
507
  errno = 0;
508
  long int rv = strtol(s, &endptr, 10);
509

  
510
  if (!errno && !*endptr)
511
    return rv;
512

  
513
  pw = getpwnam(s);
514
  if (!pw)
515
    die("Cannot find user '%s'", s);
516

  
517
  return pw->pw_uid;
518
}
519

  
520
static inline gid_t
521
get_gid(const char *s)
522
{
523
  struct group *gr;
524
  char *endptr;
525
  
526
  errno = 0;
527
  long int rv = strtol(s, &endptr, 10);
528

  
529
  if (!errno && !*endptr)
530
    return rv;
531

  
532
  gr = getgrnam(s);
533
  if (!gr)
534
    die("Cannot find group '%s'", s);
535

  
536
  return gr->gr_gid;
537
}
538

  
472 539
static void
473 540
parse_args(int argc, char **argv)
474 541
{
......
504 571
      case 's':
505 572
	path_control_socket = optarg;
506 573
	break;
574
      case 'u':
575
	use_user = optarg;
576
	break;
577
      case 'g':
578
	use_group = optarg;
579
	break;
507 580
      default:
508 581
	usage();
509 582
      }
......
528 601
    log_init_debug("");
529 602
  log_switch(debug_flag, NULL, NULL);
530 603

  
604
  if (use_group)
605
    drop_gid(get_gid(use_group));
606

  
607
  if (use_user)
608
    drop_uid(get_uid(use_user));
609

  
531 610
  if (!parse_and_exit)
532 611
    test_old_bird(path_control_socket);
533 612

  

Also available in: Unified diff