[xiph-cvs] cvs commit: icecast/src main.c
Michael Smith
msmith at xiph.org
Mon May 20 06:25:32 PDT 2002
msmith 02/05/20 06:25:31
Modified: src main.c
Log:
chroot and setuid interacted badly in the previous version. Fixed now.
Revision Changes Path
1.6 +39 -32 icecast/src/main.c
Index: main.c
===================================================================
RCS file: /usr/local/cvsroot/icecast/src/main.c,v
retrieving revision 1.5
retrieving revision 1.6
diff -u -r1.5 -r1.6
--- main.c 2002/05/14 11:06:54 1.5
+++ main.c 2002/05/20 13:25:31 1.6
@@ -162,14 +162,35 @@
sock_close(global.serversock);
}
-#ifdef CHROOT
/* chroot the process. Watch out - we need to do this before starting other
- * threads */
+ * threads. Change uid as well, after figuring out uid _first_ */
-static void _chroot_setup(void)
+static void _ch_root_uid__setup(void)
{
ice_config_t *conf = config_get_config();
+#ifdef CHUID
+ struct passwd *user;
+ struct group *group;
+ uid_t uid=-1;
+ gid_t gid=-1;
+
+ if(conf->chuid)
+ {
+ user = getpwnam(conf->user);
+ group = getgrnam(conf->group);
+ if(user)
+ uid = user->pw_uid;
+ else
+ fprintf(stderr, "Couldn't find user \"%s\" in password file\n", conf->user);
+ if(group)
+ gid = group->gr_gid;
+ else
+ fprintf(stderr, "Couldn't find group \"%s\" in groups file\n", conf->group);
+ }
+#endif
+
+#ifdef CHROOT
if (conf->chroot)
{
if(getuid()) /* root check */
@@ -186,17 +207,9 @@
fprintf(stdout, "Changed root successfully to \"%s\".\n", conf->base_dir);
}
-}
#endif
-
#ifdef CHUID
-/* change uid and gid */
-static void _chuid_setup(void)
-{
- ice_config_t *conf = config_get_config();
- struct passwd *user;
- struct group *group;
-
+
if(conf->chuid)
{
if(getuid()) /* root check */
@@ -205,22 +218,22 @@
return;
}
- user = getpwnam(conf->user);
- group = getgrnam(conf->group);
-
- if(!setgid(group->gr_gid))
- fprintf(stdout, "Changed groupid to %i.\n", group->gr_gid);
- else
- fprintf(stdout, "Error changing groupid: %s.\n", strerror(errno));
-
- if(!setuid(user->pw_uid))
- fprintf(stdout, "Changed userid to %i.\n", user->pw_uid);
- else
- fprintf(stdout, "Error changing userid: %s.\n", strerror(errno));
+ if(gid != -1) {
+ if(!setgid(gid))
+ fprintf(stdout, "Changed groupid to %i.\n", group->gr_gid);
+ else
+ fprintf(stdout, "Error changing groupid: %s.\n", strerror(errno));
+ }
+ if(uid != -1) {
+ if(!setuid(uid))
+ fprintf(stdout, "Changed userid to %i.\n", user->pw_uid);
+ else
+ fprintf(stdout, "Error changing userid: %s.\n", strerror(errno));
+ }
}
-}
#endif
+}
int main(int argc, char **argv)
{
@@ -263,15 +276,9 @@
/* override config file options with commandline options */
config_parse_cmdline(argc, argv);
-#ifdef CHROOT
- _chroot_setup(); /* Perform chroot, if requested */
-#endif
-
_server_proc_init(); /* Bind socket, before we change userid */
-#ifdef CHUID
- _chuid_setup(); /* change user id */
-#endif
+ _ch_root_uid__setup(); /* Change user id and root if requested/possible */
stats_initialize(); /* We have to do this later on because of threading */
<p><p><p>--- >8 ----
List archives: http://www.xiph.org/archives/
Ogg project homepage: http://www.xiph.org/ogg/
To unsubscribe from this list, send a message to 'cvs-request at xiph.org'
containing only the word 'unsubscribe' in the body. No subject is needed.
Unsubscribe messages sent to the list will be ignored/filtered.
More information about the commits
mailing list