/* Copyright (c) 2010 Juan J. Martinez Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /* suidexec * * This is a simple process executor to use with setuid. * * It requires glib2. * * gcc -O2 -s `pkg-config --cflags --libs glib-2.0` suidexec.c -o suidexec * * Example: run fast-cgi from a www-data process as nobody user. * * 1. $ cp suidexec suidexec_nobody * 2. # chown nobody:nobody suidexec_nobody * 3. # chmod +s suidexec_nobody * 4. edit /etc/suidexec.conf to allow the command execution: * * [suidexec_nobody] * commands = /path/to/fast-cgi * www-data = true * * 5. configure your www-data process: * * suidexec_nobody /path/to/fast-cgi -- fast_cgi arguments * * Troubleshooting: add -d flag to suidexec arguments before the command * and check stderr/syslog. * * Perhaps you should think about using SUDO instead! * * Juan J. Martinez */ #include #include #include #include #include #include #include #include #define APPNAME "suidexec" #define VERSION "0.1" /* read only for all users */ #define CONF "/etc/suidexec.conf" int main(int argc, char *argv[]) { char debug = 0; int opt, error, i; char **args; GKeyFile *gkf; char *base, **cmds, *user; gsize length; while ((opt = getopt(argc, argv, "hvd")) != -1) switch (opt) { case 'h': printf("Usage: %s [-h] [-d] command [-- arguments]\n", argv[0]); return 0; case 'v': printf("%s v%s\n", APPNAME, VERSION); return 0; case 'd': debug = 1; break; } openlog(APPNAME, LOG_PID | LOG_PERROR, LOG_USER); if (optind >= argc) { syslog(LOG_WARNING, "nothing to execute"); closelog(); return 1; } if (debug) { syslog(LOG_DEBUG, "starting in debug mode"); syslog(LOG_DEBUG, "command: %s", argv[optind]); } /* read the configuration file in order to verify if the user / comand * are allowed: * * 1. look for a conf group with the executable name * 2. look for the command in the commands list * 3. look for the user running the executor, and check it's allowied */ gkf = g_key_file_new(); if (!gkf || !g_key_file_load_from_file(gkf, CONF, 0, NULL)) { syslog(LOG_ERR, "failed to load %s conf file", CONF); return 1; } base = basename(argv[0]); cmds = g_key_file_get_string_list(gkf, base, "commands", &length, NULL); if (!cmds) { syslog(LOG_ERR, "forbidden: command list not found for %s", base); return 1; } for (i = 0; i < length; i++) if (cmds[i] && !strcmp(cmds[i], argv[optind])) break; if (i == length) { syslog(LOG_ERR, "forbidden: command not found for %s", base); return 1; } if (debug) syslog(LOG_DEBUG, "command %s found for %s", argv[optind], base); user = getlogin(); if (!g_key_file_get_boolean(gkf, base, user, NULL)) { syslog(LOG_ERR, "forbidden: %s not found or not allowed for %s", user, base); return 1; } if (debug) syslog(LOG_DEBUG, "user %s allowed for %s", user, base); args = (char **) calloc(argc - optind, sizeof(char *)); if (!args) { syslog(LOG_ERR, "failed to calloc memory"); closelog(); return 1; } for (i = 0; i < argc - optind; i++) { args[i] = argv[optind + i]; if (debug) syslog(LOG_DEBUG, "argument: %s", args[i]); } args[i] = NULL; closelog(); g_key_file_free(gkf); execv(argv[optind], args); error = errno; openlog(APPNAME, LOG_PID | LOG_PERROR, LOG_USER); syslog(LOG_ERR, "command execution failed: error %d: %s", error, strerror(error)); closelog(); return error; } /* EOF */