home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
rtsi.com
/
2014.01.www.rtsi.com.tar
/
www.rtsi.com
/
OS9
/
OSK
/
TELECOM
/
UUCP_Blars.lzh
/
uuxqt.c
< prev
Wrap
C/C++ Source or Header
|
1991-10-11
|
7KB
|
323 lines
/*
* uuxqt.c
*
* Unix to Unix Execute
*
* This program searches for work files in the spool
* directory and executes them. Work files have a X. prefix.
*
* It is executed by 'uucico', 'cron', or 'at'
*
* Based on a program by Richard H. Lamb.
* Ported to OS-9/68000: Wolfgang Ocker January 1988
*
*/
#include "uucp.h"
#include <modes.h>
#include <dir.h> /* defines MAXNAMLEN and directory stuff */
#include <strings.h>
#include <stdio.h>
#include <time.h>
#include <errno.h>
#include <pwd.h>
extern int errno;
extern char **environ;
#define MAXLINE 256
#define ERROR -1
#define MAX_CMDS 50
#define TRUE (-1)
#define FALSE (0)
char command[MAXLINE], input[MAXLINE], output[MAXLINE], line[MAXLINE];
char Cfilename[MAXNAMLEN+1], filename[MAXNAMLEN+1], ltime[32];
char systemname[200], username[200];
char *mfgets();
void printmsg();
char *allcmds[MAX_CMDS];
int ncmds; /* # of commands */
char **args;
extern int os9forkc();
FILE *log; /* Log file */
void main(argc, argv)
int argc;
char *argv[];
{
register struct direct *dir;
register char *p;
FILE *fdC;
DIR *dd;
int inull, onull;
int i;
long tstamp;
int lock; /* Lock file */
int cmds; /* allowed commands */
char tmp[MAXLINE]; /* temporary */
char modtmp[200], *cp, *cp2;
char s_spooldir[100], s_libdir[100], s_commands[120];
int tmpfd0, tmpfd1, tmpfd2;
int pid, status;
struct passwd *pw;
if((pw = getpwname("uucp")) != NULL) {
if (getuid() != pw->pw_giduid) {
if (setuid(pw->pw_giduid) < 0) {
fprintf(stderr, "Must run as uucp\n");
exit(1);
}
}
}
strcpy(s_spooldir, SPOOLDIR);
strcpy(s_libdir, LIBDIR);
chdir(s_spooldir);
log = fopen(XQTLOG, "a");
if ((dd = opendir(".")) == (DIR *) NULL) {
exit(1);
}
sprintf(s_commands, "%s/commands", s_libdir);
if ((cmds = open(s_commands, S_IREAD)) < 0) {
closedir(dd);
fprintf(stderr, "Could not open %s\n", s_commands);
exit(1);
}
ncmds = 0;
while ((i = readln(cmds, tmp, sizeof tmp)) > 0) {
if (i == 1 || tmp[0]=='#')
continue;
tmp[--i] = '\0';
if (p = index(tmp, ' '))
*p = '\0';
if ((allcmds[ncmds] = (char *) malloc(strlen(tmp))) == NULL) {
close(cmds);
fprintf(stderr, "malloc failure\n");
exit(1);
}
strcpy(allcmds[ncmds], tmp);
if (++ncmds >= MAX_CMDS)
break;
}
close(cmds);
while ((dir = readdir(dd)) != (struct direct *) NULL) {
if (strncmp(dir->d_name, "X.", 2) != 0)
continue;
fprintf(stderr, "Processing %s\n", dir->d_name);
strncpy(Cfilename, dir->d_name, MAXNAMLEN);
if ((fdC = fopen(Cfilename, "r")) == (FILE *) ERROR)
continue;
input[0] = output[0] = '\0';
inull = onull = FALSE;
while (mfgets(line, MAXLINE, fdC) != NULL) {
switch (line[0]) {
case 'C':
strcpy(command, &line[2]);
break;
case 'F':
strcpy(filename, &line[2]);
if ((p = index(filename, ' ')) != NULL) *p = '\0';
if (access(filename, S_IREAD) < 0) {
fclose(fdC);
goto not_ready;
}
break;
case 'I':
strcpy(input, &line[2]);
break;
case 'O':
strcpy(output, &line[2]);
break;
case 'U':
strcpy(username, &line[2]);
if ((p = index(username, ' ')) != NULL) {
*p++ = '\0';
strcpy(systemname, p);
}
if (username[0] == '\"') {
strcpy(username, username+1);
username[strlen(username)-1] = '\0';
}
break;
default:
break;
}
}
fclose(fdC);
if (strlen(input) == 0) {
inull = TRUE;
strcpy(input, "/nil");
}
if (strlen(output) == 0) {
onull = TRUE;
strcpy(output, "/nil");
}
fprintf(stderr, "input='%s' output='%s' command='%s'\n", input, output, command);
if (getargs(command, &args) < 0) {
printmsg("Fatal: getargs() returns error\n");
goto not_ready;
}
for(i=0; args[i] != NULL; i++) {
fprintf(stderr, "arg[%d]='%s'\n", i, args[i]);
}
if (legalcommand(args[0]) < 0) {
printmsg("System Security check: can't execute \'%s\'", command);
goto not_ready;
}
printmsg("");
tmpfd0 = dup(0);
close(0);
if (open(input, S_IREAD) != 0) {
fprintf(stderr, "Could not open '%s'\n", input);
close(0);
dup(tmpfd0);
close(tmpfd0);
goto not_ready;
}
tmpfd1 = dup(1);
close(1);
if (creat(output, S_IWRITE) != 1) {
fprintf(stderr, "Could not create '%s'\n", output);
close(0);
dup(tmpfd0);
close(1);
dup(tmpfd1);
goto not_ready;
}
tmpfd2 = dup(2);
close(2);
if (open("/nil", S_IWRITE) != 2) {
fprintf(stderr, "could not open /nil for stderr\n");
close(0);
dup(tmpfd0);
close(1);
dup(tmpfd1);
close(2);
dup(tmpfd2);
goto not_ready;
}
pid = os9exec(os9forkc, args[0], args, environ, 0, 0, 3);
close(0);
dup(tmpfd0);
close(1);
dup(tmpfd1);
close(2);
dup(tmpfd2);
close(tmpfd0);
close(tmpfd1);
close(tmpfd2);
free(args);
if (pid == -1)
printmsg("Error %d on line '%s'", errno, line);
else {
while (wait(&status) != pid) ;
if ((status & 0x00ffff) != 0)
printmsg("Error %d on line '%s'", status & 0x0ffff, line);
else {
unlink(Cfilename);
if (!inull)
while (unlink(input) < 0)
if (errno == E_SHARE)
sleep(2);
else
break;
if (!onull)
while (unlink(output) < 0)
if (errno == E_SHARE)
sleep(2);
else
break;
}
}
not_ready:
;
}
if (log != NULL)
fclose(log);
closedir(dd);
}
/*
* l e g a l c o m m a n d
*/
int legalcommand(command)
char *command;
{
int i;
for (i = 0; i < ncmds; i++)
if (!strcmp(command, allcmds[i]))
return(0);
return(-1);
}
/*
* p r i n t m s g
*/
void printmsg(msg, a1, a2, a3)
char *msg;
{
int tstamp;
char ltime[40];
char buf[100];
if (log == NULL)
return;
sprintf(buf, msg, a1, a2, a3);
time(&tstamp);
strcpy(ltime, ctime(&tstamp));
ltime[strlen(ltime) - 1] = '\0';
fprintf(log, "%s %s %s!%s %s\n", ltime, buf, systemname, username, line);
}