util.c revision 1557
1/*- 2 * Copyright (c) 1992, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. All advertising materials mentioning features or use of this software 14 * must display the following acknowledgement: 15 * This product includes software developed by the University of 16 * California, Berkeley and its contributors. 17 * 4. Neither the name of the University nor the names of its contributors 18 * may be used to endorse or promote products derived from this software 19 * without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 */ 33 34#ifndef lint 35static char sccsid[] = "@(#)util.c 8.2 (Berkeley) 4/2/94"; 36#endif /* not lint */ 37 38#include <sys/param.h> 39#include <sys/stat.h> 40#include <sys/wait.h> 41 42#include <ctype.h> 43#include <err.h> 44#include <errno.h> 45#include <paths.h> 46#include <signal.h> 47#include <stdio.h> 48#include <stdlib.h> 49#include <string.h> 50#include <unistd.h> 51 52#include "extern.h" 53 54char * 55colon(cp) 56 char *cp; 57{ 58 if (*cp == ':') /* Leading colon is part of file name. */ 59 return (0); 60 61 for (; *cp; ++cp) { 62 if (*cp == ':') 63 return (cp); 64 if (*cp == '/') 65 return (0); 66 } 67 return (0); 68} 69 70void 71verifydir(cp) 72 char *cp; 73{ 74 struct stat stb; 75 76 if (!stat(cp, &stb)) { 77 if (S_ISDIR(stb.st_mode)) 78 return; 79 errno = ENOTDIR; 80 } 81 run_err("%s: %s", cp, strerror(errno)); 82 exit(1); 83} 84 85int 86okname(cp0) 87 char *cp0; 88{ 89 int c; 90 char *cp; 91 92 cp = cp0; 93 do { 94 c = *cp; 95 if (c & 0200) 96 goto bad; 97 if (!isalpha(c) && !isdigit(c) && c != '_' && c != '-') 98 goto bad; 99 } while (*++cp); 100 return (1); 101 102bad: warnx("%s: invalid user name", cp0); 103 return (0); 104} 105 106int 107susystem(s, userid) 108 int userid; 109 char *s; 110{ 111 sig_t istat, qstat; 112 int status, w; 113 pid_t pid; 114 115 pid = vfork(); 116 switch (pid) { 117 case -1: 118 return (127); 119 120 case 0: 121 (void)setuid(userid); 122 execl(_PATH_BSHELL, "sh", "-c", s, NULL); 123 _exit(127); 124 } 125 istat = signal(SIGINT, SIG_IGN); 126 qstat = signal(SIGQUIT, SIG_IGN); 127 if (waitpid(pid, &status, 0) < 0) 128 status = -1; 129 (void)signal(SIGINT, istat); 130 (void)signal(SIGQUIT, qstat); 131 return (status); 132} 133 134BUF * 135allocbuf(bp, fd, blksize) 136 BUF *bp; 137 int fd, blksize; 138{ 139 struct stat stb; 140 size_t size; 141 142 if (fstat(fd, &stb) < 0) { 143 run_err("fstat: %s", strerror(errno)); 144 return (0); 145 } 146 size = roundup(stb.st_blksize, blksize); 147 if (size == 0) 148 size = blksize; 149 if (bp->cnt >= size) 150 return (bp); 151 if ((bp->buf = realloc(bp->buf, size)) == NULL) { 152 bp->cnt = 0; 153 run_err("%s", strerror(errno)); 154 return (0); 155 } 156 bp->cnt = size; 157 return (bp); 158} 159 160void 161lostconn(signo) 162 int signo; 163{ 164 if (!iamremote) 165 warnx("lost connection"); 166 exit(1); 167} 168