Changeset 444:ddaaf8e220bd in roaraudio


Ignore:
Timestamp:
08/12/08 00:57:38 (16 years ago)
Author:
phi
Branch:
default
Phase:
public
Message:

added support vor setuid, setgid and chroot

Location:
roard
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • roard/include/roard.h

    r248 r444  
    88#include <sys/wait.h> 
    99#include <roaraudio.h> 
     10#include <pwd.h> 
    1011 
    1112/* 
  • roard/roard.c

    r440 r444  
    1515        " --realtime            - Trys to get realtime priority,\n" 
    1616        "                         give multible times for being more realtime\n" 
     17        " --chroot DIR          - chroots to the given dir\n" 
     18        " --setgid              - GroupID to the audio group as specified via -G\n" 
     19        " --setuid              - UserID to the audio user as specified via -U\n" 
    1720       ); 
    1821 
     
    4851        " -G  GROUP             - Sets the group for the UNIX Domain Socket, (default: audio)\n" 
    4952        "                         You need the permittions to change the GID\n" 
     53        " -U  USER              - Sets the user for the UNIX Domain Socket, (default: do not set)\n" 
     54        "                         You need the permittions to change the UID (normaly only root has)\n" 
    5055        " --no-listen           - Do not listen for new clients (only usefull for relaing)\n" 
    5156        " --client-fh           - Comunicate with a client over this handle\n" 
     
    5560 printf("\n"); 
    5661} 
     62 
     63#define R_SETUID 1 
     64#define R_SETGID 2 
    5765 
    5866int main (int argc, char * argv[]) { 
     
    6977 int      port = ROAR_DEFAULT_PORT; 
    7078 int               drvid; 
    71  char * s_dev  = NULL; 
    72  char * s_con  = NULL; 
    73  char * s_opt  = NULL; 
    74  int    s_prim = 0; 
    75  char * sock_grp = "audio"; 
    76  struct group * grp; 
     79 char * s_dev     = NULL; 
     80 char * s_con     = NULL; 
     81 char * s_opt     = NULL; 
     82 int    s_prim    = 0; 
     83 char * sock_grp  = "audio"; 
     84 char * sock_user = NULL; 
     85 char * chrootdir = NULL; 
     86 int    setids    = 0; 
     87 struct group  * grp = NULL; 
     88 struct passwd * pwd = NULL; 
    7789 DRIVER_USERDATA_T drvinst; 
    7890 struct roar_client * self = NULL; 
     
    135147  } else if ( strcmp(k, "--realtime") == 0 ) { 
    136148   realtime++; 
     149  } else if ( strcmp(k, "--chroot") == 0 ) { 
     150   chrootdir = argv[++i]; 
     151  } else if ( strcmp(k, "--setgid") == 0 ) { 
     152   setids |= R_SETGID; 
     153  } else if ( strcmp(k, "--setuid") == 0 ) { 
     154   setids |= R_SETUID; 
    137155 
    138156  } else if ( strcmp(k, "--list-cf") == 0 ) { 
     
    182200   // ignore this case as it is the default behavor. 
    183201  } else if ( strcmp(k, "-G") == 0 ) { 
    184    sock_grp = argv[++i]; 
     202   sock_grp  = argv[++i]; 
     203  } else if ( strcmp(k, "-U") == 0 ) { 
     204   sock_user = argv[++i]; 
    185205 
    186206  } else if ( strcmp(k, "--no-listen") == 0 ) { 
     
    225245 
    226246  if ( *server == '/' ) { 
     247   if ( sock_user ) { 
     248    if ( (pwd = getpwnam(sock_user)) == NULL ) { 
     249     ROAR_ERR("Can not get UID for user %s: %s", sock_user, strerror(errno)); 
     250    } 
     251   } 
    227252   if ( (grp = getgrnam(sock_grp)) == NULL ) { 
    228253    ROAR_ERR("Can not get GID for group %s: %s", sock_grp, strerror(errno)); 
    229254   } else { 
    230     chown(server, -1, grp->gr_gid); 
     255    if ( pwd ) { 
     256     chown(server, pwd->pw_uid, grp->gr_gid); 
     257    } else { 
     258     chown(server, -1, grp->gr_gid); 
     259    } 
    231260    if ( getuid() == 0 ) 
    232261     chmod(server, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP); 
     
    272301 } 
    273302 
     303 if ( setids & R_SETGID ) { 
     304  if ( setgroups(0, (const gid_t *) NULL) == -1 ) { 
     305   ROAR_ERR("Can not clear supplementary group IDs: %s", strerror(errno)); 
     306  } 
     307  if ( setgid(grp->gr_gid) == -1 ) { 
     308   ROAR_ERR("Can not set GroupID: %s", strerror(errno)); 
     309  } 
     310 } 
     311 
    274312 
    275313 clients_set_pid(g_self_client, getpid()); 
     
    294332 } 
    295333 
     334 if (chrootdir) { 
     335  if ( chroot(chrootdir) == -1 ) { 
     336   ROAR_ERR("Can not chroot to %s: %s", chrootdir, strerror(errno)); 
     337   return 2; 
     338  } 
     339  if ( chdir("/") == -1 ) { 
     340   ROAR_ERR("Can not chdir to /: %s", strerror(errno)); 
     341   return 2; 
     342  } 
     343 } 
     344 
     345 if ( setids & R_SETUID ) { 
     346  if ( !pwd || setuid(pwd->pw_uid) == -1 ) { 
     347   ROAR_ERR("Can not set UserID: %s", strerror(errno)); 
     348   return 3; 
     349  } 
     350  clients_set_uid(g_self_client, getuid()); 
     351 } 
     352 
    296353 // start main loop... 
    297354 main_loop(drvid, drvinst, &sa); 
Note: See TracChangeset for help on using the changeset viewer.