util.c revision 79452
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
3836149Scharnierstatic const char rcsid[] =
3950471Speter  "$FreeBSD: head/bin/rcp/util.c 79452 2001-07-09 09:24:06Z brian $";
401556Srgrimes#endif /* not lint */
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 *
591556Srgrimescolon(cp)
601556Srgrimes	char *cp;
611556Srgrimes{
621556Srgrimes	if (*cp == ':')		/* Leading colon is part of file name. */
631556Srgrimes		return (0);
641556Srgrimes
651556Srgrimes	for (; *cp; ++cp) {
661556Srgrimes		if (*cp == ':')
671556Srgrimes			return (cp);
681556Srgrimes		if (*cp == '/')
691556Srgrimes			return (0);
701556Srgrimes	}
711556Srgrimes	return (0);
721556Srgrimes}
731556Srgrimes
741556Srgrimesvoid
751556Srgrimesverifydir(cp)
761556Srgrimes	char *cp;
771556Srgrimes{
781556Srgrimes	struct stat stb;
791556Srgrimes
801556Srgrimes	if (!stat(cp, &stb)) {
811556Srgrimes		if (S_ISDIR(stb.st_mode))
821556Srgrimes			return;
831556Srgrimes		errno = ENOTDIR;
841556Srgrimes	}
851556Srgrimes	run_err("%s: %s", cp, strerror(errno));
861556Srgrimes	exit(1);
871556Srgrimes}
881556Srgrimes
891556Srgrimesint
901556Srgrimesokname(cp0)
911556Srgrimes	char *cp0;
921556Srgrimes{
931556Srgrimes	int c;
941556Srgrimes	char *cp;
951556Srgrimes
961556Srgrimes	cp = cp0;
971556Srgrimes	do {
981556Srgrimes		c = *cp;
991556Srgrimes		if (c & 0200)
1001556Srgrimes			goto bad;
10177491Spirzyk		if (!isalpha(c) && !isdigit(c) && c != '_' && c != '-' && c != '.' )
1021556Srgrimes			goto bad;
1031556Srgrimes	} while (*++cp);
1041556Srgrimes	return (1);
1051556Srgrimes
1061556Srgrimesbad:	warnx("%s: invalid user name", cp0);
1071556Srgrimes	return (0);
1081556Srgrimes}
1091556Srgrimes
1101556Srgrimesint
1111556Srgrimessusystem(s, userid)
1121556Srgrimes	int userid;
1131556Srgrimes	char *s;
1141556Srgrimes{
1151556Srgrimes	sig_t istat, qstat;
1167165Sjoerg	int status;
1171556Srgrimes	pid_t pid;
1181556Srgrimes
1191556Srgrimes	pid = vfork();
1201556Srgrimes	switch (pid) {
1211556Srgrimes	case -1:
1221556Srgrimes		return (127);
1238855Srgrimes
1241556Srgrimes	case 0:
1251556Srgrimes		(void)setuid(userid);
12679452Sbrian		execl(_PATH_BSHELL, "sh", "-c", s, (char *)NULL);
1271556Srgrimes		_exit(127);
1281556Srgrimes	}
1291556Srgrimes	istat = signal(SIGINT, SIG_IGN);
1301556Srgrimes	qstat = signal(SIGQUIT, SIG_IGN);
1311556Srgrimes	if (waitpid(pid, &status, 0) < 0)
1321556Srgrimes		status = -1;
1331556Srgrimes	(void)signal(SIGINT, istat);
1341556Srgrimes	(void)signal(SIGQUIT, qstat);
1351556Srgrimes	return (status);
1361556Srgrimes}
1371556Srgrimes
1381556SrgrimesBUF *
1391556Srgrimesallocbuf(bp, fd, blksize)
1401556Srgrimes	BUF *bp;
1411556Srgrimes	int fd, blksize;
1421556Srgrimes{
1431556Srgrimes	struct stat stb;
1441556Srgrimes	size_t size;
1451556Srgrimes
1461556Srgrimes	if (fstat(fd, &stb) < 0) {
1471556Srgrimes		run_err("fstat: %s", strerror(errno));
1481556Srgrimes		return (0);
1491556Srgrimes	}
1501556Srgrimes	size = roundup(stb.st_blksize, blksize);
1511556Srgrimes	if (size == 0)
1521556Srgrimes		size = blksize;
1531556Srgrimes	if (bp->cnt >= size)
1541556Srgrimes		return (bp);
1551556Srgrimes	if ((bp->buf = realloc(bp->buf, size)) == NULL) {
1561556Srgrimes		bp->cnt = 0;
1571556Srgrimes		run_err("%s", strerror(errno));
1581556Srgrimes		return (0);
1591556Srgrimes	}
1601556Srgrimes	bp->cnt = size;
1611556Srgrimes	return (bp);
1621556Srgrimes}
1631556Srgrimes
1641556Srgrimesvoid
1651556Srgrimeslostconn(signo)
1661556Srgrimes	int signo;
1671556Srgrimes{
1681556Srgrimes	if (!iamremote)
1691556Srgrimes		warnx("lost connection");
1701556Srgrimes	exit(1);
1711556Srgrimes}
172