util.c revision 104130
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
3536149Scharnier#if 0
3636149Scharnierstatic char sccsid[] = "@(#)util.c	8.2 (Berkeley) 4/2/94";
3736149Scharnier#endif
381556Srgrimes#endif /* not lint */
3999110Sobrien#include <sys/cdefs.h>
4099110Sobrien__FBSDID("$FreeBSD: head/bin/rcp/util.c 104130 2002-09-29 07:59:57Z jmallett $");
411556Srgrimes
421556Srgrimes#include <sys/param.h>
431556Srgrimes#include <sys/stat.h>
441556Srgrimes#include <sys/wait.h>
451556Srgrimes
461556Srgrimes#include <ctype.h>
471556Srgrimes#include <err.h>
481556Srgrimes#include <errno.h>
491556Srgrimes#include <paths.h>
501556Srgrimes#include <signal.h>
511556Srgrimes#include <stdio.h>
521556Srgrimes#include <stdlib.h>
531556Srgrimes#include <string.h>
541556Srgrimes#include <unistd.h>
551556Srgrimes
561556Srgrimes#include "extern.h"
571556Srgrimes
581556Srgrimeschar *
5990110Simpcolon(char *cp)
601556Srgrimes{
611556Srgrimes	if (*cp == ':')		/* Leading colon is part of file name. */
621556Srgrimes		return (0);
631556Srgrimes
641556Srgrimes	for (; *cp; ++cp) {
651556Srgrimes		if (*cp == ':')
661556Srgrimes			return (cp);
671556Srgrimes		if (*cp == '/')
681556Srgrimes			return (0);
691556Srgrimes	}
701556Srgrimes	return (0);
711556Srgrimes}
721556Srgrimes
731556Srgrimesvoid
7490110Simpverifydir(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
8890110Simpokname(char *cp0)
891556Srgrimes{
901556Srgrimes	int c;
911556Srgrimes	char *cp;
921556Srgrimes
931556Srgrimes	cp = cp0;
941556Srgrimes	do {
951556Srgrimes		c = *cp;
961556Srgrimes		if (c & 0200)
971556Srgrimes			goto bad;
9880818Sobrien		if (!isalpha(c) && !isdigit(c) && c != '_' && c != '-' && c != '.')
991556Srgrimes			goto bad;
1001556Srgrimes	} while (*++cp);
1011556Srgrimes	return (1);
1021556Srgrimes
1031556Srgrimesbad:	warnx("%s: invalid user name", cp0);
1041556Srgrimes	return (0);
1051556Srgrimes}
1061556Srgrimes
1071556Srgrimesint
10890110Simpsusystem(char *s, int userid)
1091556Srgrimes{
1101556Srgrimes	sig_t istat, qstat;
1117165Sjoerg	int status;
1121556Srgrimes	pid_t pid;
1131556Srgrimes
1141556Srgrimes	pid = vfork();
1151556Srgrimes	switch (pid) {
1161556Srgrimes	case -1:
1171556Srgrimes		return (127);
1188855Srgrimes
1191556Srgrimes	case 0:
1201556Srgrimes		(void)setuid(userid);
12179452Sbrian		execl(_PATH_BSHELL, "sh", "-c", s, (char *)NULL);
1221556Srgrimes		_exit(127);
1231556Srgrimes	}
1241556Srgrimes	istat = signal(SIGINT, SIG_IGN);
1251556Srgrimes	qstat = signal(SIGQUIT, SIG_IGN);
1261556Srgrimes	if (waitpid(pid, &status, 0) < 0)
1271556Srgrimes		status = -1;
1281556Srgrimes	(void)signal(SIGINT, istat);
1291556Srgrimes	(void)signal(SIGQUIT, qstat);
1301556Srgrimes	return (status);
1311556Srgrimes}
1321556Srgrimes
1331556SrgrimesBUF *
13490110Simpallocbuf(BUF *bp, int fd, int blksize)
1351556Srgrimes{
1361556Srgrimes	struct stat stb;
1371556Srgrimes	size_t size;
1381556Srgrimes
1391556Srgrimes	if (fstat(fd, &stb) < 0) {
1401556Srgrimes		run_err("fstat: %s", strerror(errno));
1411556Srgrimes		return (0);
1421556Srgrimes	}
1431556Srgrimes	size = roundup(stb.st_blksize, blksize);
1441556Srgrimes	if (size == 0)
1451556Srgrimes		size = blksize;
1461556Srgrimes	if (bp->cnt >= size)
1471556Srgrimes		return (bp);
1481556Srgrimes	if ((bp->buf = realloc(bp->buf, size)) == NULL) {
1491556Srgrimes		bp->cnt = 0;
1501556Srgrimes		run_err("%s", strerror(errno));
1511556Srgrimes		return (0);
1521556Srgrimes	}
1531556Srgrimes	bp->cnt = size;
1541556Srgrimes	return (bp);
1551556Srgrimes}
1561556Srgrimes
1571556Srgrimesvoid
158104130Sjmallettlostconn(int signo __unused)
1591556Srgrimes{
1601556Srgrimes	if (!iamremote)
1611556Srgrimes		warnx("lost connection");
1621556Srgrimes	exit(1);
1631556Srgrimes}
164