util.c revision 1556
11556Srgrimes/*- 21556Srgrimes * Copyright (c) 1992, 1993 31556Srgrimes * The Regents of the University of California. All rights reserved. 41556Srgrimes * 51556Srgrimes * Redistribution and use in source and binary forms, with or without 61556Srgrimes * modification, are permitted provided that the following conditions 71556Srgrimes * are met: 81556Srgrimes * 1. Redistributions of source code must retain the above copyright 91556Srgrimes * notice, this list of conditions and the following disclaimer. 101556Srgrimes * 2. Redistributions in binary form must reproduce the above copyright 111556Srgrimes * notice, this list of conditions and the following disclaimer in the 121556Srgrimes * documentation and/or other materials provided with the distribution. 131556Srgrimes * 3. All advertising materials mentioning features or use of this software 141556Srgrimes * must display the following acknowledgement: 151556Srgrimes * This product includes software developed by the University of 161556Srgrimes * California, Berkeley and its contributors. 171556Srgrimes * 4. Neither the name of the University nor the names of its contributors 181556Srgrimes * may be used to endorse or promote products derived from this software 191556Srgrimes * without specific prior written permission. 201556Srgrimes * 211556Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 221556Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 231556Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 241556Srgrimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 251556Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 261556Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 271556Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 281556Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 291556Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 301556Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 311556Srgrimes * SUCH DAMAGE. 321556Srgrimes */ 331556Srgrimes 341556Srgrimes#ifndef lint 351556Srgrimesstatic char sccsid[] = "@(#)util.c 8.2 (Berkeley) 4/2/94"; 361556Srgrimes#endif /* not lint */ 371556Srgrimes 381556Srgrimes#include <sys/param.h> 391556Srgrimes#include <sys/stat.h> 401556Srgrimes#include <sys/wait.h> 411556Srgrimes 421556Srgrimes#include <ctype.h> 431556Srgrimes#include <err.h> 441556Srgrimes#include <errno.h> 451556Srgrimes#include <paths.h> 461556Srgrimes#include <signal.h> 471556Srgrimes#include <stdio.h> 481556Srgrimes#include <stdlib.h> 491556Srgrimes#include <string.h> 501556Srgrimes#include <unistd.h> 511556Srgrimes 521556Srgrimes#include "extern.h" 531556Srgrimes 541556Srgrimeschar * 551556Srgrimescolon(cp) 561556Srgrimes char *cp; 571556Srgrimes{ 581556Srgrimes if (*cp == ':') /* Leading colon is part of file name. */ 591556Srgrimes return (0); 601556Srgrimes 611556Srgrimes for (; *cp; ++cp) { 621556Srgrimes if (*cp == ':') 631556Srgrimes return (cp); 641556Srgrimes if (*cp == '/') 651556Srgrimes return (0); 661556Srgrimes } 671556Srgrimes return (0); 681556Srgrimes} 691556Srgrimes 701556Srgrimesvoid 711556Srgrimesverifydir(cp) 721556Srgrimes char *cp; 731556Srgrimes{ 741556Srgrimes struct stat stb; 751556Srgrimes 761556Srgrimes if (!stat(cp, &stb)) { 771556Srgrimes if (S_ISDIR(stb.st_mode)) 781556Srgrimes return; 791556Srgrimes errno = ENOTDIR; 801556Srgrimes } 811556Srgrimes run_err("%s: %s", cp, strerror(errno)); 821556Srgrimes exit(1); 831556Srgrimes} 841556Srgrimes 851556Srgrimesint 861556Srgrimesokname(cp0) 871556Srgrimes char *cp0; 881556Srgrimes{ 891556Srgrimes int c; 901556Srgrimes char *cp; 911556Srgrimes 921556Srgrimes cp = cp0; 931556Srgrimes do { 941556Srgrimes c = *cp; 951556Srgrimes if (c & 0200) 961556Srgrimes goto bad; 971556Srgrimes if (!isalpha(c) && !isdigit(c) && c != '_' && c != '-') 981556Srgrimes goto bad; 991556Srgrimes } while (*++cp); 1001556Srgrimes return (1); 1011556Srgrimes 1021556Srgrimesbad: warnx("%s: invalid user name", cp0); 1031556Srgrimes return (0); 1041556Srgrimes} 1051556Srgrimes 1061556Srgrimesint 1071556Srgrimessusystem(s, userid) 1081556Srgrimes int userid; 1091556Srgrimes char *s; 1101556Srgrimes{ 1111556Srgrimes sig_t istat, qstat; 1121556Srgrimes int status, w; 1131556Srgrimes pid_t pid; 1141556Srgrimes 1151556Srgrimes pid = vfork(); 1161556Srgrimes switch (pid) { 1171556Srgrimes case -1: 1181556Srgrimes return (127); 1191556Srgrimes 1201556Srgrimes case 0: 1211556Srgrimes (void)setuid(userid); 1221556Srgrimes execl(_PATH_BSHELL, "sh", "-c", s, NULL); 1231556Srgrimes _exit(127); 1241556Srgrimes } 1251556Srgrimes istat = signal(SIGINT, SIG_IGN); 1261556Srgrimes qstat = signal(SIGQUIT, SIG_IGN); 1271556Srgrimes if (waitpid(pid, &status, 0) < 0) 1281556Srgrimes status = -1; 1291556Srgrimes (void)signal(SIGINT, istat); 1301556Srgrimes (void)signal(SIGQUIT, qstat); 1311556Srgrimes return (status); 1321556Srgrimes} 1331556Srgrimes 1341556SrgrimesBUF * 1351556Srgrimesallocbuf(bp, fd, blksize) 1361556Srgrimes BUF *bp; 1371556Srgrimes int fd, blksize; 1381556Srgrimes{ 1391556Srgrimes struct stat stb; 1401556Srgrimes size_t size; 1411556Srgrimes 1421556Srgrimes if (fstat(fd, &stb) < 0) { 1431556Srgrimes run_err("fstat: %s", strerror(errno)); 1441556Srgrimes return (0); 1451556Srgrimes } 1461556Srgrimes size = roundup(stb.st_blksize, blksize); 1471556Srgrimes if (size == 0) 1481556Srgrimes size = blksize; 1491556Srgrimes if (bp->cnt >= size) 1501556Srgrimes return (bp); 1511556Srgrimes if ((bp->buf = realloc(bp->buf, size)) == NULL) { 1521556Srgrimes bp->cnt = 0; 1531556Srgrimes run_err("%s", strerror(errno)); 1541556Srgrimes return (0); 1551556Srgrimes } 1561556Srgrimes bp->cnt = size; 1571556Srgrimes return (bp); 1581556Srgrimes} 1591556Srgrimes 1601556Srgrimesvoid 1611556Srgrimeslostconn(signo) 1621556Srgrimes int signo; 1631556Srgrimes{ 1641556Srgrimes if (!iamremote) 1651556Srgrimes warnx("lost connection"); 1661556Srgrimes exit(1); 1671556Srgrimes} 168