1162852Sdes/*-
2162852Sdes * Copyright (c) 1992, 1993
3162852Sdes *	The Regents of the University of California.  All rights reserved.
4162852Sdes *
5162852Sdes * Redistribution and use in source and binary forms, with or without
6162852Sdes * modification, are permitted provided that the following conditions
7162852Sdes * are met:
8162852Sdes * 1. Redistributions of source code must retain the above copyright
9162852Sdes *    notice, this list of conditions and the following disclaimer.
10162852Sdes * 2. Redistributions in binary form must reproduce the above copyright
11162852Sdes *    notice, this list of conditions and the following disclaimer in the
12162852Sdes *    documentation and/or other materials provided with the distribution.
13162852Sdes * 4. Neither the name of the University nor the names of its contributors
14162852Sdes *    may be used to endorse or promote products derived from this software
15162852Sdes *    without specific prior written permission.
16162852Sdes *
17162852Sdes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18162852Sdes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19162852Sdes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20162852Sdes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21162852Sdes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22162852Sdes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23162852Sdes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24162852Sdes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25162852Sdes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26162852Sdes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27162852Sdes * SUCH DAMAGE.
28162852Sdes */
29162852Sdes
30162852Sdes#ifndef lint
31162852Sdes#if 0
32162852Sdesstatic const char sccsid[] = "@(#)util.c	8.2 (Berkeley) 4/2/94";
33162852Sdes#endif
34162852Sdes#endif /* not lint */
35162852Sdes#include <sys/cdefs.h>
36162852Sdes__FBSDID("$FreeBSD$");
37162852Sdes
38162852Sdes#include <sys/param.h>
39162852Sdes#include <sys/stat.h>
40162852Sdes#include <sys/wait.h>
41162852Sdes
42162852Sdes#include <ctype.h>
43162852Sdes#include <err.h>
44162852Sdes#include <errno.h>
45162852Sdes#include <paths.h>
46162852Sdes#include <signal.h>
47162852Sdes#include <stdio.h>
48162852Sdes#include <stdlib.h>
49162852Sdes#include <string.h>
50162852Sdes#include <unistd.h>
51162852Sdes
52162852Sdes#include "extern.h"
53162852Sdes
54162852Sdeschar *
55162852Sdescolon(char *cp)
56162852Sdes{
57162852Sdes	if (*cp == ':')		/* Leading colon is part of file name. */
58162852Sdes		return (0);
59162852Sdes
60162852Sdes	for (; *cp; ++cp) {
61162852Sdes		if (*cp == ':')
62162852Sdes			return (cp);
63162852Sdes		if (*cp == '/')
64162852Sdes			return (0);
65162852Sdes	}
66162852Sdes	return (0);
67162852Sdes}
68162852Sdes
69162852Sdesvoid
70162852Sdesverifydir(char *cp)
71162852Sdes{
72162852Sdes	struct stat stb;
73162852Sdes
74162852Sdes	if (!stat(cp, &stb)) {
75162852Sdes		if (S_ISDIR(stb.st_mode))
76162852Sdes			return;
77162852Sdes		errno = ENOTDIR;
78162852Sdes	}
79162852Sdes	run_err("%s: %s", cp, strerror(errno));
80162852Sdes	exit(1);
81162852Sdes}
82162852Sdes
83162852Sdesint
84162852Sdesokname(char *cp0)
85162852Sdes{
86162852Sdes	int c;
87162852Sdes	char *cp;
88
89	cp = cp0;
90	do {
91		c = *cp;
92		if (c & 0200)
93			goto bad;
94		if (!isalpha(c) && !isdigit(c) && c != '_' && c != '-' && c != '.')
95			goto bad;
96	} while (*++cp);
97	return (1);
98
99bad:	warnx("%s: invalid user name", cp0);
100	return (0);
101}
102
103int
104susystem(char *s, int userid)
105{
106	sig_t istat, qstat;
107	int status;
108	pid_t pid;
109
110	pid = vfork();
111	switch (pid) {
112	case -1:
113		return (127);
114
115	case 0:
116		(void)setuid(userid);
117		execl(_PATH_BSHELL, "sh", "-c", s, (char *)NULL);
118		_exit(127);
119	}
120	istat = signal(SIGINT, SIG_IGN);
121	qstat = signal(SIGQUIT, SIG_IGN);
122	if (waitpid(pid, &status, 0) < 0)
123		status = -1;
124	(void)signal(SIGINT, istat);
125	(void)signal(SIGQUIT, qstat);
126	return (status);
127}
128
129BUF *
130allocbuf(BUF *bp, int fd, int blksize)
131{
132	struct stat stb;
133	size_t size;
134
135	if (fstat(fd, &stb) < 0) {
136		run_err("fstat: %s", strerror(errno));
137		return (0);
138	}
139	size = roundup(stb.st_blksize, blksize);
140	if (size == 0)
141		size = blksize;
142	if (bp->cnt >= size)
143		return (bp);
144	if ((bp->buf = realloc(bp->buf, size)) == NULL) {
145		bp->cnt = 0;
146		run_err("%s", strerror(errno));
147		return (0);
148	}
149	bp->cnt = size;
150	return (bp);
151}
152
153void
154lostconn(int signo __unused)
155{
156	if (!iamremote)
157		warnx("lost connection");
158	exit(1);
159}
160