-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathexec.c
138 lines (127 loc) · 3.52 KB
/
exec.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/*
Eagles Bulletin Board System
Copyright (C) 1995, Ray Rocker, [email protected]
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include "server.h"
#include <fcntl.h>
#include <signal.h>
#define MAXCOMSZ (1024) /* Maximum length of do_exec command */
#define MAXFILELEN (80) /* Maximum length of the executable file name */
#define MAXARGS (40) /* Maximum number of args to a do_exec command */
#define LOOKFIRST (0)
#define LOOKLAST (1)
#define QUOTEMODE (2)
_set_io(fname, fd, append)
char *fname;
int fd;
int append;
{
int newfd;
int mode;
if (fd == 0) mode = O_RDONLY;
else {
mode = O_WRONLY | O_CREAT;
if (append) mode |= O_APPEND;
}
close(fd);
newfd = open(fname, mode, 0600);
if (newfd == -1) {
bbslog(0, "ERROR _set_io: open failed: %s mode %d\n", fname, mode);
return -1;
}
if (newfd != fd && dup2(newfd, fd) == -1) {
bbslog(0, "ERROR _set_io: dup2 failed\n");
return -1;
}
return 0;
}
/*
Adapted from Pirates BBS 1.6, do_exec in stuff.c
Copyright (C) 1990, Edward Luke, [email protected]
This one is suitable for the server, doesn't try to set the screen
*/
execute(com, wd, infile, outfile, errfile, envp, append)
char *com, *wd, *infile, *outfile, *errfile;
char **envp;
int append;
{
char path[MAXFILELEN] ;
char pcom[MAXCOMSZ] ;
char *arglist[MAXARGS] ;
register int i,len ;
register int argptr ;
int status, pid, w ;
int pmode ;
int saved_alarm ;
void (*isig)(), (*qsig)();
strncpy(pcom,com,MAXCOMSZ) ;
len = strlen(com)+1;
if (len > MAXCOMSZ) len = MAXCOMSZ;
pmode = LOOKFIRST ;
for(i=0,argptr=0;i<len;i++) {
if(pcom[i] == '\0')
break ;
if(pmode == QUOTEMODE) {
if(pcom[i] == '\"') {
pmode = LOOKFIRST ;
pcom[i] = '\0' ;
continue ;
}
continue ;
}
if(pcom[i] == '\"') {
pmode = QUOTEMODE ;
arglist[argptr++] = &pcom[i+1] ;
if(argptr+1 == MAXARGS)
break ;
continue ;
}
if(pmode == LOOKFIRST)
if(pcom[i] != ' ') {
arglist[argptr++] = &pcom[i] ;
if(argptr+1 == MAXARGS)
break ;
pmode = LOOKLAST ;
} else continue ;
if(pcom[i] == ' ') {
pmode = LOOKFIRST ;
pcom[i] = '\0' ;
}
}
arglist[argptr] = NULL ;
if(argptr == 0)
return -1 ;
strncpy(path,arglist[0],MAXFILELEN) ;
saved_alarm = alarm(0);
if((pid = fork()) == 0) {
if(wd)
if(chdir(wd)) {
bbslog(0, "ERROR execute: chdir failed: %s\n", wd);
exit(-1) ;
}
if (infile) _set_io(infile, 0, append);
if (outfile) _set_io(outfile, 1, append);
if (errfile) _set_io(errfile, 2, append);
execve(path, arglist, envp);
bbslog(0, "ERROR execute: execve failed: %s\n", path);
exit(-1) ;
}
isig = signal(SIGINT, SIG_IGN);
qsig = signal(SIGQUIT, SIG_IGN);
while ((w = wait(&status)) != pid && w != -1);
if (saved_alarm) alarm(saved_alarm);
signal(SIGINT, isig) ;
signal(SIGQUIT, qsig) ;
return((w == -1)? w: status) ;
}