diff --git a/libexec/xtend/Makefile b/libexec/xtend/Makefile index 2164a73b5242..75fedeff421f 100644 --- a/libexec/xtend/Makefile +++ b/libexec/xtend/Makefile @@ -1,10 +1,11 @@ # Makefile for xtend (Stark) 10/30/93 +# $Id$ -BINMODE= 4555 +BINMODE= 555 PROG= xtend SRCS= xtend.c status.c packet.c user.c -CFLAGS+=-I. +CFLAGS+=-I. -DXTENUNAME=\"xten\" -DXTENGNAME=\"xten\" LDADD+= -lutil MAN8= xtend.8 diff --git a/libexec/xtend/packet.c b/libexec/xtend/packet.c index bc75fcb9393d..19958a980347 100644 --- a/libexec/xtend/packet.c +++ b/libexec/xtend/packet.c @@ -1,5 +1,5 @@ /*- - * Copyright (c) 1992, 1993 Eugene W. Stark + * Copyright (c) 1992, 1993, 1995 Eugene W. Stark * All rights reserved. * * Redistribution and use in source and binary forms, with or without diff --git a/libexec/xtend/paths.h b/libexec/xtend/paths.h index ad49443c5397..77c6ae9c36ea 100644 --- a/libexec/xtend/paths.h +++ b/libexec/xtend/paths.h @@ -2,7 +2,7 @@ * Pathnames for files used by xtend */ -#define X10DIR "/var/spool/xten/" +#define X10DIR "/var/spool/xten" #define X10LOGNAME "Log" #define X10STATNAME "Status" #define X10DUMPNAME "status.out" diff --git a/libexec/xtend/status.c b/libexec/xtend/status.c index 6249577b4585..c09ae8582065 100644 --- a/libexec/xtend/status.c +++ b/libexec/xtend/status.c @@ -1,5 +1,5 @@ /*- - * Copyright (c) 1992, 1993 Eugene W. Stark + * Copyright (c) 1992, 1993, 1995 Eugene W. Stark * All rights reserved. * * Redistribution and use in source and binary forms, with or without diff --git a/libexec/xtend/user.c b/libexec/xtend/user.c index effce3808843..f029f5f69e5d 100644 --- a/libexec/xtend/user.c +++ b/libexec/xtend/user.c @@ -1,5 +1,5 @@ /*- - * Copyright (c) 1992, 1993 Eugene W. Stark + * Copyright (c) 1992, 1993, 1995 Eugene W. Stark * All rights reserved. * * Redistribution and use in source and binary forms, with or without diff --git a/libexec/xtend/xten.h b/libexec/xtend/xten.h index fea7e1786039..e782f155ca37 100644 --- a/libexec/xtend/xten.h +++ b/libexec/xtend/xten.h @@ -1,5 +1,5 @@ /*- - * Copyright (c) 1992, 1993 Eugene W. Stark + * Copyright (c) 1992, 1993, 1995 Eugene W. Stark * All rights reserved. * * Redistribution and use in source and binary forms, with or without diff --git a/libexec/xtend/xtend.c b/libexec/xtend/xtend.c index 08d5d5032a67..609bc7fe016d 100644 --- a/libexec/xtend/xtend.c +++ b/libexec/xtend/xtend.c @@ -1,5 +1,5 @@ /*- - * Copyright (c) 1992, 1993 Eugene W. Stark + * Copyright (c) 1992, 1993, 1995 Eugene W. Stark * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -39,10 +39,14 @@ #include #include #include +#include +#include #include #include #include #include +#include +#include #include #include #include @@ -72,61 +76,79 @@ char *argv[]; char statpath[MAXPATHLEN+1]; struct sockaddr_un sa; struct timeval tv; + struct passwd *pw; + struct group *gr; + struct stat sb; int user; int fd; FILE *pidf; /* - * Open the log file before doing anything + * Make sure we start out running as root + */ + if(geteuid() != 0) { + fprintf(stderr, "You must be root to run %s\n", argv[0]); + exit(1); + } + + /* + * Find out what UID/GID we are to run as + */ + if((pw = getpwnam(XTENUNAME)) == NULL) { + fprintf(stderr, "%s: No such user '%s'\n", argv[0], XTENUNAME); + exit(1); + } + if((gr = getgrnam(XTENGNAME)) == NULL) { + fprintf(stderr, "%s: No such group '%s'\n", argv[0], XTENGNAME); + exit(1); + } + + /* + * Open the log file before doing anything else */ strcpy(logpath, X10DIR); + if(stat(logpath, &sb) == -1 && errno == ENOENT) { + if(mkdir(logpath, 0755) != -1) { + chown(logpath, pw->pw_uid, gr->gr_gid); + } else { + fprintf(stderr, "%s: Can't create directory '%s'\n", argv[0], logpath); + exit(1); + } + } + strcat(logpath, "/"); strcat(logpath, X10LOGNAME); if((Log = fopen(logpath, "a")) == NULL) { fprintf(stderr, "Can't open log file %s\n", logpath); exit(1); } + chown(logpath, pw->pw_uid, gr->gr_gid); /* * Become a daemon */ if(daemon(0, 0) == -1) { - fprintf(Log, "%s: %s unable to become a daemon\n", thedate(), argv[0]); + fprintf(Log, "%s: Unable to become a daemon\n", thedate()); + fclose(Log); exit(1); } - fprintf(Log, "%s: %s[%d] started\n", thedate(), argv[0], getpid()); + fprintf(Log, "%s: %s [%d] started\n", thedate(), argv[0], getpid()); /* * Get ahold of the TW523 device */ if((tw523 = open(twpath, O_RDWR)) < 0) { fprintf(Log, "%s: Can't open %s\n", thedate(), twpath); + fclose(Log); exit(1); } fprintf(Log, "%s: %s successfully opened\n", thedate(), twpath); - /* - * Initialize the status table - */ - strcpy(statpath, X10DIR); - strcat(statpath, X10STATNAME); - if((status = open(statpath, O_RDWR)) < 0) { - if((status = open(statpath, O_RDWR | O_CREAT, 0666)) < 0) { - fprintf(Log, "%s: Can't open %s\n", thedate(), statpath); - exit(1); - } - if(write(status, Status, 16 * 16 * sizeof(STATUS)) - != 16 * 16 * sizeof(STATUS)) { - fprintf(Log, "%s: Error initializing status file\n", thedate()); - exit(1); - } - } - initstatus(); - /* * Put our pid in a file so we can be signalled by shell scripts */ if((pidf = fopen(PIDPATH, "w")) == NULL) { fprintf(Log, "%s: Error writing pid file: %s\n", thedate(), PIDPATH); + fclose(Log); exit(1); } fprintf(pidf, "%d\n", getpid()); @@ -137,6 +159,7 @@ char *argv[]; */ if((sock = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) { fprintf(Log, "%s: Can't create socket\n", thedate()); + fclose(Log); exit(1); } strcpy(sa.sun_path, sockpath); @@ -144,20 +167,59 @@ char *argv[]; unlink(sockpath); if(bind(sock, (struct sockaddr *)(&sa), strlen(sa.sun_path) + 2) < 0) { fprintf(Log, "%s: Can't bind socket to %s\n", thedate(), sockpath); + fclose(Log); exit(1); } if(listen(sock, 5) < 0) { fprintf(Log, "%s: Can't listen on socket\n", thedate()); + fclose(Log); exit(1); } - signal(SIGHUP, onhup); - signal(SIGTERM, onterm); - signal(SIGPIPE, onpipe); + /* + * Set proper ownership and permissions on the socket + */ + if(chown(sockpath, pw->pw_uid, gr->gr_gid) == -1 || + chmod(sockpath, 0660) == -1) { + fprintf(Log, "%s: Can't set owner/permissions on socket\n", thedate()); + fclose(Log); + exit(1); + } + + /* + * Give up root privileges + */ + setgid(pw->pw_gid); + setuid(pw->pw_uid); + + /* + * Initialize the status table + */ + strcpy(statpath, X10DIR); + strcat(statpath, "/"); + strcat(statpath, X10STATNAME); + if((status = open(statpath, O_RDWR)) < 0) { + if((status = open(statpath, O_RDWR | O_CREAT, 0666)) < 0) { + fprintf(Log, "%s: Can't open %s\n", thedate(), statpath); + fclose(Log); + exit(1); + } + if(write(status, Status, 16 * 16 * sizeof(STATUS)) + != 16 * 16 * sizeof(STATUS)) { + fprintf(Log, "%s: Error initializing status file\n", thedate()); + fclose(Log); + exit(1); + } + } + initstatus(); + /* * Return here on SIGHUP after closing and reopening log file. * Also on SIGPIPE after closing user connection. */ + signal(SIGTERM, onterm); + signal(SIGHUP, onhup); + signal(SIGPIPE, onpipe); setjmp(mainloop); /* @@ -268,6 +330,7 @@ void onhup() fprintf(Log, "%s: SIGHUP received, reopening Log\n", thedate()); fclose(Log); strcpy(logpath, X10DIR); + strcat(logpath, "/"); strcat(logpath, X10LOGNAME); if((Log = fopen(logpath, "a")) == NULL) { fprintf(stderr, "Can't open log file %s\n", logpath); @@ -284,6 +347,7 @@ void onhup() void onterm() { fprintf(Log, "%s: SIGTERM received, shutting down\n", thedate()); + fclose(Log); exit(0); } diff --git a/libexec/xtend/xtend.h b/libexec/xtend/xtend.h index d52ea9bc8427..1b8c7067e3f1 100644 --- a/libexec/xtend/xtend.h +++ b/libexec/xtend/xtend.h @@ -1,5 +1,5 @@ /*- - * Copyright (c) 1992, 1993 Eugene W. Stark + * Copyright (c) 1992, 1993, 1995 Eugene W. Stark * All rights reserved. * * Redistribution and use in source and binary forms, with or without