Skip to content

Commit 0c9da97

Browse files
author
Claes Wikstrom
committed
added ability to run as different user than root
git-svn-id: https://erlyaws.svn.sourceforge.net/svnroot/erlyaws/trunk/yaws@315 9fbdc01b-0d2c-0410-bfb7-fb27d70d8b52
1 parent 8a876cd commit 0c9da97

File tree

15 files changed

+2388
-43
lines changed

15 files changed

+2388
-43
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
SUBDIRS = src scripts man www/shopingcart doc
1+
SUBDIRS = c_src src scripts man www/shopingcart doc
22
APPS = webmail
33
include ./include.mk
44

c_src/Makefile

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
include ../include.mk
2+
3+
4+
PRIV_FILES= ../priv/setuid_drv.$(DLL)
5+
6+
CFLAGS += -I${ERLDIR}/usr/include
7+
8+
#
9+
# Targets
10+
#
11+
12+
all: $(PRIV_FILES)
13+
14+
clean:
15+
-rm -f $(PRIV_FILES) setuid_drv.$(OBJ)
16+
17+
install:
18+
install -d $(INSTALLPREFIX)/lib/yaws/priv
19+
install ../priv/setuid_drv.$(DLL) $(INSTALLPREFIX)/lib/yaws/priv
20+
21+
22+
../priv/setuid_drv.$(DLL): setuid_drv.$(OBJ)
23+
$(LD_SHARED) $(OUT)$@ setuid_drv.$(OBJ) $(DLL_LIBS)
24+
25+
setuid_drv.$(OBJ): setuid_drv.c
26+
$(CC) -c $(FPIC) $(CFLAGS) -DDYNAMIC_DRIVER setuid_drv.c
27+
28+
29+
30+
31+

c_src/setuid_drv.c

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/* author: [email protected] */
2+
/* purpose, make us run under a different username */
3+
4+
#ifndef WIN32
5+
#include <unistd.h>
6+
#endif
7+
8+
9+
#include <sys/types.h>
10+
#include <stdio.h>
11+
#include <string.h>
12+
#include <unistd.h>
13+
#include <pwd.h>
14+
15+
#include "erl_driver.h"
16+
#ifndef ERL_DRV_NIL
17+
#include "erl_driver_compat.h"
18+
#endif
19+
20+
21+
22+
static ErlDrvData setuid_start(ErlDrvPort port, char *buf);
23+
static void setuid_stop(ErlDrvData drv_data);
24+
25+
static ErlDrvEntry setuid_driver_entry;
26+
27+
28+
/* buf is the name of the intented user */
29+
static ErlDrvData setuid_start(ErlDrvPort port, char *buf)
30+
{
31+
char *t;
32+
char xbuf[BUFSIZ];
33+
struct passwd *pe;
34+
35+
if ((t = strchr(buf, ' ')) == NULL)
36+
return (ErlDrvData) -1;
37+
t++;
38+
39+
while ((pe = getpwent())) {
40+
if (strcmp(pe->pw_name , t) == 0) {
41+
if ((setuid(pe->pw_uid) != 0) ||
42+
(setreuid(pe->pw_uid, pe->pw_uid) != 0)) {
43+
return (ErlDrvData) -1;
44+
}
45+
sprintf(xbuf, "ok %d", pe->pw_uid);
46+
endpwent();
47+
driver_output(port,xbuf, strlen(xbuf));
48+
return (ErlDrvData) port;
49+
}
50+
}
51+
endpwent();
52+
return (ErlDrvData) -1;
53+
}
54+
55+
56+
static void setuid_stop(ErlDrvData drv_data)
57+
{
58+
}
59+
60+
61+
62+
63+
/*
64+
* Initialize and return a driver entry struct
65+
*/
66+
67+
68+
69+
70+
DRIVER_INIT(setuid_drv)
71+
{
72+
setuid_driver_entry.init = NULL; /* Not used */
73+
setuid_driver_entry.start = setuid_start;
74+
setuid_driver_entry.stop = setuid_stop;
75+
setuid_driver_entry.output = NULL;
76+
setuid_driver_entry.ready_input = NULL;
77+
setuid_driver_entry.ready_output = NULL;
78+
setuid_driver_entry.driver_name = "setuid_drv";
79+
setuid_driver_entry.finish = NULL;
80+
setuid_driver_entry.control = NULL;
81+
setuid_driver_entry.outputv = NULL;
82+
return (ErlDrvEntry*) &setuid_driver_entry;
83+
}
84+
85+

0 commit comments

Comments
 (0)