Skip to content

Commit

Permalink
msudo - Execute a command as another user version 0.2.
Browse files Browse the repository at this point in the history
Added support for regexp in config file

Example of config line, user "rohel" can start "/usr/sbin/arping some_parameters":
rohel ^/usr/sbin/arping.*$

git-svn-id: https://ais-dataserver:8443/svn/WinConfig/trunk/projects/msudo@2951 30e310cc-b365-5d4b-8166-626653d85fd0
  • Loading branch information
roh committed Oct 29, 2013
1 parent d3753c2 commit 2ef6f35
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 17 deletions.
57 changes: 41 additions & 16 deletions msudo.c
Original file line number Diff line number Diff line change
@@ -1,57 +1,82 @@
/*
msudo - Execute a command as another user.
Jaroslav Rohel, 2013
email: [email protected]
*/

#define CFG_FILE_NAME "/etc/msudo.conf"
//#define CFG_FILE_NAME "msudo.conf"

#include <pwd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>

//#define CFG_FILE_NAME "/etc/msudo.conf"
#define CFG_FILE_NAME "msudo.conf"
#include <regex.h>

int main(int argc, const char *argv[])
{
FILE *cfgFile;
const char *wantPrgName;
char username[1024];
char prgname[1024];
uid_t my_uid;
struct passwd *pw;

if (argc!=2 || argv[1]=='\0')
{
fprintf(stdout, "msudo version 0.1 Jaroslav Rohel, 2013\n", argv[0]);
fprintf(stdout, "msudo version 0.2 Jaroslav Rohel, 2013\n\n", argv[0]);
fprintf(stdout, "Usage:\n");
fprintf(stdout, "%s pathToPrgForExec\n\n", argv[0]);
fprintf(stdout, "%s pathToPrgForExec_with_arguments\n\n", argv[0]);
fprintf(stdout, "Example:\n");
fprintf(stdout, "%s \"/usr/sbin/arping -i eth0 192.168.10.100\"\n\n", argv[0]);
return -1;
}

// get my real name
my_uid = getuid();
pw = getpwuid(my_uid);
// printf("%s\n", pw->pw_name);

wantPrgName = argv[1];
cfgFile = fopen(CFG_FILE_NAME, "r");
if (cfgFile)
{
const char *wantPrg = argv[1];

char username[1024];
char pattern[1024];
// char *pattern = "a[a-c]u";
int found = 0;
while (fscanf(cfgFile, "%1023s%*10000[ \t]%1023s%*10000[ \t\n\r]", username, prgname) >= 2)

while (fscanf(cfgFile, "%1023s%*10000[ \t]%1023[^\n\r]%*10000[\n\r]", username, pattern) >= 2)
{
// printf("UserName: %s\n", username);
// printf("PrgName: %s\n", prgname);
if (strcmp(username, pw->pw_name)==0 && strcmp(wantPrgName, prgname)==0)
if (strcmp(username, pw->pw_name) == 0)
{
found = 1;
break;
regex_t preg;
int rc;

rc = regcomp(&preg, pattern, 0);
if (rc)
{
fprintf(stderr, "regcomp() failed, returning nonzero (%d)\n", rc);
break;
}
rc = regexec(&preg, wantPrg, 0, NULL, 0);
regfree(&preg);
if (!rc)
{
found = 1;
break;
}
}
}
fclose(cfgFile);
if (found)
{
printf("Starting\n");
system(wantPrgName);
system(wantPrg);
}
else fprintf(stdout, "User \"%s\" is not allowed to start program %s\n", pw->pw_name, wantPrgName);
else fprintf(stdout, "User \"%s\" is not allowed to start \"%s\"\n", pw->pw_name, wantPrg);
}
else fprintf(stderr, "Can't open config file %s\n", CFG_FILE_NAME);
return 0;
Expand Down
3 changes: 2 additions & 1 deletion msudo.conf
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
rohel /home/rohel/prace/AIS/SVN/WinConfig/trunk/projects/msudo/test.sh
rohel ^/home/rohel/prace/AIS/SVN/WinConfig/trunk/projects/msudo/test.sh$
rohel ^/usr/sbin/arping.*$

0 comments on commit 2ef6f35

Please sign in to comment.