util.c revision 8855
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.
323044Sdg *
338855Srgrimes *	$Id: util.c,v 1.3 1995/03/19 13:29:16 joerg Exp $
341556Srgrimes */
351556Srgrimes
361556Srgrimes#ifndef lint
371556Srgrimesstatic char sccsid[] = "@(#)util.c	8.2 (Berkeley) 4/2/94";
381556Srgrimes#endif /* not lint */
391556Srgrimes
401556Srgrimes#include <sys/param.h>
411556Srgrimes#include <sys/stat.h>
421556Srgrimes#include <sys/wait.h>
431556Srgrimes
441556Srgrimes#include <ctype.h>
451556Srgrimes#include <err.h>
461556Srgrimes#include <errno.h>
471556Srgrimes#include <paths.h>
481556Srgrimes#include <signal.h>
491556Srgrimes#include <stdio.h>
501556Srgrimes#include <stdlib.h>
511556Srgrimes#include <string.h>
521556Srgrimes#include <unistd.h>
531556Srgrimes
541556Srgrimes#include "extern.h"
551556Srgrimes
561556Srgrimeschar *
571556Srgrimescolon(cp)
581556Srgrimes	char *cp;
591556Srgrimes{
601556Srgrimes	if (*cp == ':')		/* Leading colon is part of file name. */
611556Srgrimes		return (0);
621556Srgrimes
631556Srgrimes	for (; *cp; ++cp) {
641556Srgrimes		if (*cp == ':')
651556Srgrimes			return (cp);
661556Srgrimes		if (*cp == '/')
671556Srgrimes			return (0);
681556Srgrimes	}
691556Srgrimes	return (0);
701556Srgrimes}
711556Srgrimes
721556Srgrimesvoid
731556Srgrimesverifydir(cp)
741556Srgrimes	char *cp;
751556Srgrimes{
761556Srgrimes	struct stat stb;
771556Srgrimes
781556Srgrimes	if (!stat(cp, &stb)) {
791556Srgrimes		if (S_ISDIR(stb.st_mode))
801556Srgrimes			return;
811556Srgrimes		errno = ENOTDIR;
821556Srgrimes	}
831556Srgrimes	run_err("%s: %s", cp, strerror(errno));
841556Srgrimes	exit(1);
851556Srgrimes}
861556Srgrimes
871556Srgrimesint
881556Srgrimesokname(cp0)
891556Srgrimes	char *cp0;
901556Srgrimes{
911556Srgrimes	int c;
921556Srgrimes	char *cp;
931556Srgrimes
941556Srgrimes	cp = cp0;
951556Srgrimes	do {
961556Srgrimes		c = *cp;
971556Srgrimes		if (c & 0200)
981556Srgrimes			goto bad;
991556Srgrimes		if (!isalpha(c) && !isdigit(c) && c != '_' && c != '-')
1001556Srgrimes			goto bad;
1011556Srgrimes	} while (*++cp);
1021556Srgrimes	return (1);
1031556Srgrimes
1041556Srgrimesbad:	warnx("%s: invalid user name", cp0);
1051556Srgrimes	return (0);
1061556Srgrimes}
1071556Srgrimes
1081556Srgrimesint
1091556Srgrimessusystem(s, userid)
1101556Srgrimes	int userid;
1111556Srgrimes	char *s;
1121556Srgrimes{
1131556Srgrimes	sig_t istat, qstat;
1147165Sjoerg	int status;
1151556Srgrimes	pid_t pid;
1161556Srgrimes
1171556Srgrimes	pid = vfork();
1181556Srgrimes	switch (pid) {
1191556Srgrimes	case -1:
1201556Srgrimes		return (127);
1218855Srgrimes
1221556Srgrimes	case 0:
1231556Srgrimes		(void)setuid(userid);
1241556Srgrimes		execl(_PATH_BSHELL, "sh", "-c", s, NULL);
1251556Srgrimes		_exit(127);
1261556Srgrimes	}
1271556Srgrimes	istat = signal(SIGINT, SIG_IGN);
1281556Srgrimes	qstat = signal(SIGQUIT, SIG_IGN);
1291556Srgrimes	if (waitpid(pid, &status, 0) < 0)
1301556Srgrimes		status = -1;
1311556Srgrimes	(void)signal(SIGINT, istat);
1321556Srgrimes	(void)signal(SIGQUIT, qstat);
1331556Srgrimes	return (status);
1341556Srgrimes}
1351556Srgrimes
1361556SrgrimesBUF *
1371556Srgrimesallocbuf(bp, fd, blksize)
1381556Srgrimes	BUF *bp;
1391556Srgrimes	int fd, blksize;
1401556Srgrimes{
1411556Srgrimes	struct stat stb;
1421556Srgrimes	size_t size;
1431556Srgrimes
1441556Srgrimes	if (fstat(fd, &stb) < 0) {
1451556Srgrimes		run_err("fstat: %s", strerror(errno));
1461556Srgrimes		return (0);
1471556Srgrimes	}
1481556Srgrimes	size = roundup(stb.st_blksize, blksize);
1491556Srgrimes	if (size == 0)
1501556Srgrimes		size = blksize;
1511556Srgrimes	if (bp->cnt >= size)
1521556Srgrimes		return (bp);
1531556Srgrimes	if ((bp->buf = realloc(bp->buf, size)) == NULL) {
1541556Srgrimes		bp->cnt = 0;
1551556Srgrimes		run_err("%s", strerror(errno));
1561556Srgrimes		return (0);
1571556Srgrimes	}
1581556Srgrimes	bp->cnt = size;
1591556Srgrimes	return (bp);
1601556Srgrimes}
1611556Srgrimes
1621556Srgrimesvoid
1631556Srgrimeslostconn(signo)
1641556Srgrimes	int signo;
1651556Srgrimes{
1661556Srgrimes	if (!iamremote)
1671556Srgrimes		warnx("lost connection");
1681556Srgrimes	exit(1);
1691556Srgrimes}
170