Skip to content
Newer
Older
100644 151 lines (122 sloc) 4.14 KB
0c9da97 @klacke added ability to run as different user than root
authored
1 /* author: klacke@hyber.org */
2 /* purpose, make us run under a different username */
930c18c @klacke did away with the idiotic calls to id -u as well as the the broken gr…
authored
3 /* as well as iface to some other idiotic syscalls */
4 /* FIXME replace this entirely with a proper */
5 /* posix interface */
6
0c9da97 @klacke added ability to run as different user than root
authored
7
8 #ifndef WIN32
9 #include <unistd.h>
10 #endif
11
12
13 #include <sys/types.h>
14 #include <stdio.h>
15 #include <string.h>
16 #include <unistd.h>
17 #include <pwd.h>
6dc4cca @klacke ""
authored
18 #include <stdlib.h>
0c9da97 @klacke added ability to run as different user than root
authored
19
20 #include "erl_driver.h"
21
22
23
24 static ErlDrvData setuid_start(ErlDrvPort port, char *buf);
25 static void setuid_stop(ErlDrvData drv_data);
26
27 static ErlDrvEntry setuid_driver_entry;
28
29
30 /* buf is the name of the intented user */
31 static ErlDrvData setuid_start(ErlDrvPort port, char *buf)
32 {
33 char *t;
34 char xbuf[BUFSIZ];
35 struct passwd *pe;
36
37 if ((t = strchr(buf, ' ')) == NULL)
0be3c7e @klacke untabified all of yaws
authored
38 return (ErlDrvData) -1;
b90f5cd @sstrollo Call setpwent() in setuid_drv (which wasn't working without it on Fre…
sstrollo authored
39
40 /* Rewind pw mapping */
41 setpwent();
42
0c9da97 @klacke added ability to run as different user than root
authored
43 t++;
930c18c @klacke did away with the idiotic calls to id -u as well as the the broken gr…
authored
44 switch (*t++) {
45 case 's': /* setuid */
0be3c7e @klacke untabified all of yaws
authored
46 while ((pe = getpwent())) {
47 if (strcmp(pe->pw_name , t) == 0) {
48 if ((setuid(pe->pw_uid) != 0) ||
49 (setreuid(pe->pw_uid, pe->pw_uid) != 0)) {
50 return (ErlDrvData) -1;
51 }
52 sprintf(xbuf, "ok %d", pe->pw_uid);
53 endpwent();
54 driver_output(port,xbuf, strlen(xbuf));
55 return (ErlDrvData) port;
56 }
57 }
58 endpwent();
02feeaa @simonoff patch for no-return-in-nonvoid-function error
simonoff authored
59 break;
6dc4cca @klacke ""
authored
60 case 'n': {
0be3c7e @klacke untabified all of yaws
authored
61 int uid = atoi(t);
62 while ((pe = getpwent())) {
63 if (pe->pw_uid == uid) {
64 sprintf(xbuf, "ok %s", pe->pw_name);
65 endpwent();
66 driver_output(port,xbuf, strlen(xbuf));
67 return (ErlDrvData) port;
68 }
69 }
70 endpwent();
71 driver_output(port, "ok -", 4);
72 return (ErlDrvData) port;
6dc4cca @klacke ""
authored
73 }
930c18c @klacke did away with the idiotic calls to id -u as well as the the broken gr…
authored
74 case 'g': /* getuid */
0be3c7e @klacke untabified all of yaws
authored
75 sprintf(xbuf, "ok %d", getuid());
76 driver_output(port,xbuf, strlen(xbuf));
77 return (ErlDrvData) port;
930c18c @klacke did away with the idiotic calls to id -u as well as the the broken gr…
authored
78 case 'u':
0be3c7e @klacke untabified all of yaws
authored
79 while ((pe = getpwent())) {
80 if (strcmp(pe->pw_name , t) == 0) {
81 sprintf(xbuf, "ok %d", pe->pw_uid);
82 endpwent();
83 driver_output(port,xbuf, strlen(xbuf));
84 return (ErlDrvData) port;
85 }
86 }
87 endpwent();
02feeaa @simonoff patch for no-return-in-nonvoid-function error
simonoff authored
88 break;
930c18c @klacke did away with the idiotic calls to id -u as well as the the broken gr…
authored
89 case 'h':
0be3c7e @klacke untabified all of yaws
authored
90 while ((pe = getpwent())) {
91 if (strcmp(pe->pw_name , t) == 0) {
92 sprintf(xbuf, "ok %s", pe->pw_dir);
93 endpwent();
94 driver_output(port,xbuf, strlen(xbuf));
95 return (ErlDrvData) port;
96 }
97 }
98 endpwent();
02feeaa @simonoff patch for no-return-in-nonvoid-function error
simonoff authored
99 break;
0c9da97 @klacke added ability to run as different user than root
authored
100 }
02feeaa @simonoff patch for no-return-in-nonvoid-function error
simonoff authored
101 // In any case return error(?) for non void function
102 return (ErlDrvData) -1;
7811247 @vinoski whitespace cleanup
vinoski authored
103
104
0c9da97 @klacke added ability to run as different user than root
authored
105 }
106
107
108 static void setuid_stop(ErlDrvData drv_data)
109 {
110 }
111
112
113
114
115 /*
116 * Initialize and return a driver entry struct
117 */
118
119
120
121
122 DRIVER_INIT(setuid_drv)
123 {
9873a60 @tuncer Add missing driver entries
tuncer authored
124 setuid_driver_entry.init = NULL; /* Not used */
125 setuid_driver_entry.start = setuid_start;
126 setuid_driver_entry.stop = setuid_stop;
127 setuid_driver_entry.output = NULL;
128 setuid_driver_entry.ready_input = NULL;
129 setuid_driver_entry.ready_output = NULL;
130 setuid_driver_entry.driver_name = "setuid_drv";
131 setuid_driver_entry.finish = NULL;
132 setuid_driver_entry.handle = NULL;
133 setuid_driver_entry.control = NULL;
134 setuid_driver_entry.timeout = NULL;
135 setuid_driver_entry.outputv = NULL;
136 setuid_driver_entry.ready_async = NULL;
137 setuid_driver_entry.flush = NULL;
138 setuid_driver_entry.call = NULL;
139 setuid_driver_entry.event = NULL;
140 setuid_driver_entry.extended_marker = ERL_DRV_EXTENDED_MARKER;
141 setuid_driver_entry.major_version = ERL_DRV_EXTENDED_MAJOR_VERSION;
142 setuid_driver_entry.minor_version = ERL_DRV_EXTENDED_MINOR_VERSION;
143 setuid_driver_entry.driver_flags = 0;
144 setuid_driver_entry.handle2 = NULL;
145 setuid_driver_entry.process_exit = NULL;
146 setuid_driver_entry.stop_select = NULL;
0c9da97 @klacke added ability to run as different user than root
authored
147 return (ErlDrvEntry*) &setuid_driver_entry;
148 }
149
150
Something went wrong with that request. Please try again.