summary |
shortlog |
log |
commit | commitdiff |
tree
raw |
patch |
inline | side by side (from parent 1:
0def9f1)
More sensible username lookup.
Signed-off-by: Richard Kettlewell <rjk@terraraq.org.uk>
#include "secnet.h"
#include <stdio.h>
#include "secnet.h"
#include <stdio.h>
#include <string.h>
#include <getopt.h>
#include <errno.h>
#include <string.h>
#include <getopt.h>
#include <errno.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <pwd.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <pwd.h>
#include "util.h"
#include "conffile.h"
#include "util.h"
#include "conffile.h"
bool_t just_check_config=False;
static char *userid=NULL;
static uid_t uid=0;
bool_t just_check_config=False;
static char *userid=NULL;
static uid_t uid=0;
bool_t background=True;
static char *pidfile=NULL;
bool_t require_root_privileges=False;
bool_t background=True;
static char *pidfile=NULL;
bool_t require_root_privileges=False;
/* Who are we supposed to run as? */
userid=dict_read_string(system,"userid",False,"system",loc);
if (userid) {
/* Who are we supposed to run as? */
userid=dict_read_string(system,"userid",False,"system",loc);
if (userid) {
- do {
- pw=getpwent();
- if (pw && strcmp(pw->pw_name,userid)==0) {
- uid=pw->pw_uid;
- break;
- }
- } while(pw);
- endpwent();
- if (uid==0) {
+ if(!(pw=getpwnam(userid)))
fatal("userid \"%s\" not found",userid);
fatal("userid \"%s\" not found",userid);
+ uid=pw->pw_uid;
+ gid=pw->pw_gid;
}
/* Now drop privileges */
}
/* Now drop privileges */
+ if (userid) {
+ if (setgid(gid)!=0)
+ fatal_perror("can't set gid to %ld",(long)gid);
+ if(initgroups(userid, gid) < 0)
+ fatal_perror("initgroups");
if (setuid(uid)!=0) {
fatal_perror("can't set uid to \"%s\"",userid);
}
if (setuid(uid)!=0) {
fatal_perror("can't set uid to \"%s\"",userid);
}
+ assert(getuid() == uid);
+ assert(geteuid() == uid);
+ assert(getgid() == gid);
+ assert(getegid() == gid);
}
if (background) {
p=fork();
}
if (background) {
p=fork();