Commit | Line | Data |
---|---|---|
460b9539 | 1 | /* |
2 | * This file is part of DisOrder | |
659d87e8 | 3 | * Copyright (C) 2005, 2007 Richard Kettlewell |
460b9539 | 4 | * |
5 | * This program is free software; you can redistribute it and/or modify | |
6 | * it under the terms of the GNU General Public License as published by | |
7 | * the Free Software Foundation; either version 2 of the License, or | |
8 | * (at your option) any later version. | |
9 | * | |
10 | * This program is distributed in the hope that it will be useful, but | |
11 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
13 | * General Public License for more details. | |
14 | * | |
15 | * You should have received a copy of the GNU General Public License | |
16 | * along with this program; if not, write to the Free Software | |
17 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 | |
18 | * USA | |
19 | */ | |
659d87e8 RK |
20 | /** @file lib/user.c |
21 | * @brief Jukebox user management | |
22 | */ | |
460b9539 | 23 | |
05b75f8d | 24 | #include "common.h" |
460b9539 | 25 | |
26 | #include <sys/types.h> | |
659d87e8 | 27 | #include <sys/stat.h> |
460b9539 | 28 | #include <errno.h> |
29 | #include <pwd.h> | |
30 | #include <grp.h> | |
31 | #include <unistd.h> | |
32 | ||
33 | #include "user.h" | |
34 | #include "log.h" | |
35 | #include "configuration.h" | |
659d87e8 | 36 | #include "mem.h" |
460b9539 | 37 | |
659d87e8 RK |
38 | /** @brief Become the jukebox user |
39 | * | |
40 | * If a jukebox user is configured then becomes that user. | |
41 | */ | |
460b9539 | 42 | void become_mortal(void) { |
43 | struct passwd *pw; | |
44 | ||
45 | if(config->user) { | |
46 | if(!(pw = getpwnam(config->user))) | |
47 | fatal(0, "cannot find user %s", config->user); | |
48 | if(pw->pw_uid != getuid()) { | |
49 | if(initgroups(config->user, pw->pw_gid)) | |
50 | fatal(errno, "error calling initgroups"); | |
51 | if(setgid(pw->pw_gid) < 0) fatal(errno, "error calling setgid"); | |
52 | if(setuid(pw->pw_uid) < 0) fatal(errno, "error calling setgid"); | |
53 | info("changed to user %s (uid %lu)", config->user, (unsigned long)getuid()); | |
54 | } | |
55 | /* sanity checks */ | |
56 | if(getuid() != pw->pw_uid) fatal(0, "wrong real uid"); | |
57 | if(geteuid() != pw->pw_uid) fatal(0, "wrong effective uid"); | |
58 | if(getgid() != pw->pw_gid) fatal(0, "wrong real gid"); | |
59 | if(getegid() != pw->pw_gid) fatal(0, "wrong effective gid"); | |
60 | if(setuid(0) != -1) fatal(0, "setuid(0) unexpectedly succeeded"); | |
61 | if(seteuid(0) != -1) fatal(0, "seteuid(0) unexpectedly succeeded"); | |
62 | } | |
63 | } | |
64 | ||
659d87e8 RK |
65 | /** @brief Create the jukebox state directory |
66 | * | |
67 | * If the home directory does not exist then creates it and assigns | |
68 | * it suitable permissions. | |
69 | */ | |
70 | void make_home(void) { | |
71 | struct stat sb; | |
72 | struct passwd *pw; | |
73 | char *home, *p; | |
74 | ||
75 | if(stat(config->home, &sb) < 0) { | |
76 | /* create parent directories */ | |
77 | home = xstrdup(config->home); | |
78 | p = home; | |
79 | while(*p) { | |
80 | if(*p == '/' && p > home) { | |
81 | *p = 0; | |
82 | mkdir(home, 0755); | |
83 | *p = '/'; | |
84 | } | |
85 | ++p; | |
86 | } | |
87 | /* create the directory itself */ | |
88 | if(mkdir(config->home, 02755) < 0) | |
89 | fatal(errno, "error creating %s", config->home); | |
90 | /* make sure it has the right ownership */ | |
91 | if(config->user) { | |
92 | if(!(pw = getpwnam(config->user))) | |
93 | fatal(0, "cannot find user %s", config->user); | |
94 | if(chown(config->home, pw->pw_uid, pw->pw_gid) < 0) | |
95 | fatal(errno, "error chowning %s", config->home); | |
96 | } | |
97 | } | |
98 | } | |
99 | ||
460b9539 | 100 | /* |
101 | Local Variables: | |
102 | c-basic-offset:2 | |
103 | comment-column:40 | |
104 | fill-column:79 | |
105 | indent-tabs-mode:nil | |
106 | End: | |
107 | */ |