Changeset 1042 for trunk/src/ifd


Ignore:
Timestamp:
05/14/08 15:22:03 (4 years ago)
Author:
alonbl
Message:

Non privileged operation

As OpenCT is a security component, it best to use least privileged mode, and udev allows this now.
The attached patch allows users to run the ifdhandler as none root user.

The configuration file was modified, not the ifdhandler is a node in the following format:

#
# Path to ifdhandler
ifdhandler {

program = SBINDIR/ifdhandler;

# user = openctd;
# groups = {
# usb,
# };
};

I believe this place is correct, but if people want to keep backward compatibility I guess
this can be splitted, and keep current ifdhandler key.

The openct-control running from init.d script or udev rule script will fork the ifdhandler using
the specified user and set the context to the specified groups. There may be more than one
group as there are more than one device type.

It also set the /var/run/openct/status owner to the specified user.

M src/tools/openct-control.c
M src/ifd/utils.c
M src/ifd/init.c
M src/include/openct/openct.h
M src/ct/status.c
M etc/openct.conf.in

Location:
trunk/src/ifd
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/ifd/init.c

    r1009 r1042  
    6262                ct_config.debug = ival; 
    6363 
    64         if (ifd_conf_get_string("ifdhandler", &sval) >= 0) 
     64        if (ifd_conf_get_string("ifdhandler.program", &sval) >= 0) 
    6565                ct_config.ifdhandler = sval; 
    6666 
  • trunk/src/ifd/utils.c

    r964 r1042  
    1414#include <sys/stat.h> 
    1515#include <sys/wait.h> 
     16#include <sys/types.h> 
     17#include <pwd.h> 
     18#include <grp.h> 
    1619 
    1720#ifndef __GNUC__ 
     
    8790        int argc, n; 
    8891        pid_t pid; 
     92        char *user = NULL; 
    8993 
    9094        ifd_debug(1, "driver=%s, devtype=%s, index=%d", driver, devtype, idx); 
     
    141145        while (--n > 2) 
    142146                close(n); 
     147         
     148        if ((n = ifd_conf_get_string_list("ifdhandler.groups", NULL, 0)) > 0) { 
     149                char **groups = (char **)calloc(n, sizeof(char *)); 
     150                gid_t *gids = (gid_t *)calloc(n, sizeof(gid_t)); 
     151                int j; 
     152                if (!groups || !gids) { 
     153                        ct_error("out of memory"); 
     154                        exit(1); 
     155                } 
     156                n = ifd_conf_get_string_list("ifdhandler.groups", groups, n); 
     157                for (j = 0; j < n; j++) { 
     158                        struct group *g = getgrnam(groups[j]); 
     159                        if (g == NULL) { 
     160                                ct_error("failed to parse group %s", groups[j]); 
     161                                exit(1); 
     162                        } 
     163                        gids[j] = g->gr_gid; 
     164                } 
     165                if (setgroups(n-1, &gids[1]) == -1) { 
     166                        ct_error("failed set groups %m"); 
     167                        exit(1); 
     168                } 
     169                if (setgid(gids[0]) == -1) { 
     170                        ct_error("failed setgid %d %m", gids[0]); 
     171                        exit(1); 
     172                } 
     173                free(groups); 
     174                free(gids); 
     175        } 
     176 
     177        if (ifd_conf_get_string("ifdhandler.user", &user) >= 0) { 
     178                struct passwd *p = getpwnam(user); 
     179 
     180                if (p == NULL) { 
     181                        ct_error("failed to parse user %s", user); 
     182                        exit(1); 
     183                } 
     184 
     185                if (setuid(p->pw_uid) == -1) { 
     186                        ct_error("failed to set*uid user %s %m", user); 
     187                        exit(1); 
     188                } 
     189        } 
    143190 
    144191        execv(ct_config.ifdhandler, (char **)argv); 
Note: See TracChangeset for help on using the changeset viewer.