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 * 4. Neither the name of the University nor the names of its contributors
141556Srgrimes *    may be used to endorse or promote products derived from this software
151556Srgrimes *    without specific prior written permission.
161556Srgrimes *
171556Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
181556Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
191556Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
201556Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
211556Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
221556Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
231556Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
241556Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
251556Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
261556Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
271556Srgrimes * SUCH DAMAGE.
281556Srgrimes */
291556Srgrimes
301556Srgrimes#ifndef lint
3136149Scharnier#if 0
32105269Smarkmstatic const char sccsid[] = "@(#)util.c	8.2 (Berkeley) 4/2/94";
3336149Scharnier#endif
341556Srgrimes#endif /* not lint */
3599110Sobrien#include <sys/cdefs.h>
3699110Sobrien__FBSDID("$FreeBSD$");
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 *
5590110Simpcolon(char *cp)
561556Srgrimes{
571556Srgrimes	if (*cp == ':')		/* Leading colon is part of file name. */
581556Srgrimes		return (0);
591556Srgrimes
601556Srgrimes	for (; *cp; ++cp) {
611556Srgrimes		if (*cp == ':')
621556Srgrimes			return (cp);
631556Srgrimes		if (*cp == '/')
641556Srgrimes			return (0);
651556Srgrimes	}
661556Srgrimes	return (0);
671556Srgrimes}
681556Srgrimes
691556Srgrimesvoid
7090110Simpverifydir(char *cp)
711556Srgrimes{
721556Srgrimes	struct stat stb;
731556Srgrimes
741556Srgrimes	if (!stat(cp, &stb)) {
751556Srgrimes		if (S_ISDIR(stb.st_mode))
761556Srgrimes			return;
771556Srgrimes		errno = ENOTDIR;
781556Srgrimes	}
791556Srgrimes	run_err("%s: %s", cp, strerror(errno));
801556Srgrimes	exit(1);
811556Srgrimes}
821556Srgrimes
831556Srgrimesint
8490110Simpokname(char *cp0)
851556Srgrimes{
861556Srgrimes	int c;
871556Srgrimes	char *cp;
881556Srgrimes
891556Srgrimes	cp = cp0;
901556Srgrimes	do {
911556Srgrimes		c = *cp;
921556Srgrimes		if (c & 0200)
931556Srgrimes			goto bad;
9480818Sobrien		if (!isalpha(c) && !isdigit(c) && c != '_' && c != '-' && c != '.')
951556Srgrimes			goto bad;
961556Srgrimes	} while (*++cp);
971556Srgrimes	return (1);
981556Srgrimes
991556Srgrimesbad:	warnx("%s: invalid user name", cp0);
1001556Srgrimes	return (0);
1011556Srgrimes}
1021556Srgrimes
1031556Srgrimesint
10490110Simpsusystem(char *s, int userid)
1051556Srgrimes{
1061556Srgrimes	sig_t istat, qstat;
1077165Sjoerg	int status;
1081556Srgrimes	pid_t pid;
1091556Srgrimes
1101556Srgrimes	pid = vfork();
1111556Srgrimes	switch (pid) {
1121556Srgrimes	case -1:
1131556Srgrimes		return (127);
1148855Srgrimes
1151556Srgrimes	case 0:
1161556Srgrimes		(void)setuid(userid);
11779452Sbrian		execl(_PATH_BSHELL, "sh", "-c", s, (char *)NULL);
1181556Srgrimes		_exit(127);
1191556Srgrimes	}
1201556Srgrimes	istat = signal(SIGINT, SIG_IGN);
1211556Srgrimes	qstat = signal(SIGQUIT, SIG_IGN);
1221556Srgrimes	if (waitpid(pid, &status, 0) < 0)
1231556Srgrimes		status = -1;
1241556Srgrimes	(void)signal(SIGINT, istat);
1251556Srgrimes	(void)signal(SIGQUIT, qstat);
1261556Srgrimes	return (status);
1271556Srgrimes}
1281556Srgrimes
1291556SrgrimesBUF *
13090110Simpallocbuf(BUF *bp, int fd, int blksize)
1311556Srgrimes{
1321556Srgrimes	struct stat stb;
1331556Srgrimes	size_t size;
1341556Srgrimes
1351556Srgrimes	if (fstat(fd, &stb) < 0) {
1361556Srgrimes		run_err("fstat: %s", strerror(errno));
1371556Srgrimes		return (0);
1381556Srgrimes	}
1391556Srgrimes	size = roundup(stb.st_blksize, blksize);
1401556Srgrimes	if (size == 0)
1411556Srgrimes		size = blksize;
1421556Srgrimes	if (bp->cnt >= size)
1431556Srgrimes		return (bp);
1441556Srgrimes	if ((bp->buf = realloc(bp->buf, size)) == NULL) {
1451556Srgrimes		bp->cnt = 0;
1461556Srgrimes		run_err("%s", strerror(errno));
1471556Srgrimes		return (0);
1481556Srgrimes	}
1491556Srgrimes	bp->cnt = size;
1501556Srgrimes	return (bp);
1511556Srgrimes}
1521556Srgrimes
1531556Srgrimesvoid
154104130Sjmallettlostconn(int signo __unused)
1551556Srgrimes{
1561556Srgrimes	if (!iamremote)
1571556Srgrimes		warnx("lost connection");
1581556Srgrimes	exit(1);
1591556Srgrimes}
160