util.c revision 36149
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 35#if 0 36static char sccsid[] = "@(#)util.c 8.2 (Berkeley) 4/2/94"; 37#endif 38static const char rcsid[] = 39 "$Id$"; 40#endif /* not lint */ 41 42#include <sys/param.h> 43#include <sys/stat.h> 44#include <sys/wait.h> 45 46#include <ctype.h> 47#include <err.h> 48#include <errno.h> 49#include <paths.h> 50#include <signal.h> 51#include <stdio.h> 52#include <stdlib.h> 53#include <string.h> 54#include <unistd.h> 55 56#include "extern.h" 57 58char * 59colon(cp) 60 char *cp; 61{ 62 if (*cp == ':') /* Leading colon is part of file name. */ 63 return (0); 64 65 for (; *cp; ++cp) { 66 if (*cp == ':') 67 return (cp); 68 if (*cp == '/') 69 return (0); 70 } 71 return (0); 72} 73 74void 75verifydir(cp) 76 char *cp; 77{ 78 struct stat stb; 79 80 if (!stat(cp, &stb)) { 81 if (S_ISDIR(stb.st_mode)) 82 return; 83 errno = ENOTDIR; 84 } 85 run_err("%s: %s", cp, strerror(errno)); 86 exit(1); 87} 88 89int 90okname(cp0) 91 char *cp0; 92{ 93 int c; 94 char *cp; 95 96 cp = cp0; 97 do { 98 c = *cp; 99 if (c & 0200) 100 goto bad; 101 if (!isalpha(c) && !isdigit(c) && c != '_' && c != '-') 102 goto bad; 103 } while (*++cp); 104 return (1); 105 106bad: warnx("%s: invalid user name", cp0); 107 return (0); 108} 109 110int 111susystem(s, userid) 112 int userid; 113 char *s; 114{ 115 sig_t istat, qstat; 116 int status; 117 pid_t pid; 118 119 pid = vfork(); 120 switch (pid) { 121 case -1: 122 return (127); 123 124 case 0: 125 (void)setuid(userid); 126 execl(_PATH_BSHELL, "sh", "-c", s, NULL); 127 _exit(127); 128 } 129 istat = signal(SIGINT, SIG_IGN); 130 qstat = signal(SIGQUIT, SIG_IGN); 131 if (waitpid(pid, &status, 0) < 0) 132 status = -1; 133 (void)signal(SIGINT, istat); 134 (void)signal(SIGQUIT, qstat); 135 return (status); 136} 137 138BUF * 139allocbuf(bp, fd, blksize) 140 BUF *bp; 141 int fd, blksize; 142{ 143 struct stat stb; 144 size_t size; 145 146 if (fstat(fd, &stb) < 0) { 147 run_err("fstat: %s", strerror(errno)); 148 return (0); 149 } 150 size = roundup(stb.st_blksize, blksize); 151 if (size == 0) 152 size = blksize; 153 if (bp->cnt >= size) 154 return (bp); 155 if ((bp->buf = realloc(bp->buf, size)) == NULL) { 156 bp->cnt = 0; 157 run_err("%s", strerror(errno)); 158 return (0); 159 } 160 bp->cnt = size; 161 return (bp); 162} 163 164void 165lostconn(signo) 166 int signo; 167{ 168 if (!iamremote) 169 warnx("lost connection"); 170 exit(1); 171} 172