-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsetuid_script_helper.c
executable file
·81 lines (69 loc) · 2.03 KB
/
setuid_script_helper.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/*
* This program is release under GPLv3!!!
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <syslog.h>
int main(int argc, char ** argv)
{
struct stat stat_buf;
char *ident = "automationHelper";
char message[512] = {0};
char cmd[512] = {0};
int logopt = LOG_PID | LOG_CONS;
int facility = LOG_USER;
int priority = LOG_ERR | LOG_USER;
int result=0;
openlog(ident, logopt, facility);
// int mask = LOG_MASK (LOG_ERR);
// result = setlogmask(mask);
if (argc < 2 || argc > 3) {
snprintf(message,512,"Usage: setuid_script_helper \"[arguments]\"");
printf("%s\n", message);
syslog(priority,"%s",message);
return -1;
}
if (stat(argv[1],&stat_buf)!=0) {
snprintf(message,512,"Shell scripte file does not exist!");
printf("%s\n", message);
syslog(priority,"%s",message);
return -1;
}
/* "owner" has X permission */
if (((stat_buf.st_mode & S_IXUSR) \
/* "group" has X, and "group" is equal to current "group" */
&& ((stat_buf.st_mode & S_IXGRP) && stat_buf.st_gid == getgid()))
/* if the current user is root, let *him* pass */
|| ((stat_buf.st_mode & S_IXUSR) && stat_buf.st_uid == getuid())) \
{
snprintf(message,512,"Run script:[%s]",argv[1]);
printf("%s\n", message);
syslog(priority,"%s",message);
/* check setuid bit */
if (stat_buf.st_mode & S_ISUID) {
printf("my uid: %d\n", stat_buf.st_uid);
result=setuid(stat_buf.st_uid);
snprintf(message,512,"Set uid to %d , return code is %d!",stat_buf.st_uid,result);
printf("%s\n", message);
syslog(priority,"%s",message);
} else {
/* do nothing */
}
if( argc == 2)
snprintf(cmd, 512, "%s", argv[1]);
if( argc == 3)
snprintf(cmd, 512, "%s %s", argv[1], argv[2]);
result=system(cmd);
snprintf(message,512,"Script executed, return code is %d!",result);
printf("%s\n", message);
syslog(priority,"%s",message);
} else {
snprintf(message,512,"Permission denied to run script:[%s]",argv[1]);
printf("%s\n", message);
syslog(priority,"%s",message);
}
}