xref: /netbsd-src/bin/rcp/util.c (revision 6f4d1070fdc609015e8453ba0a2fe3f8ccb83d53)
1*6f4d1070Schristos /*	$NetBSD: util.c,v 1.11 2006/12/15 22:45:34 christos Exp $	*/
26ff461a4Scgd 
3e40b0636Smycroft /*-
4e40b0636Smycroft  * Copyright (c) 1992, 1993
5e40b0636Smycroft  *	The Regents of the University of California.  All rights reserved.
6e40b0636Smycroft  *
7e40b0636Smycroft  * Redistribution and use in source and binary forms, with or without
8e40b0636Smycroft  * modification, are permitted provided that the following conditions
9e40b0636Smycroft  * are met:
10e40b0636Smycroft  * 1. Redistributions of source code must retain the above copyright
11e40b0636Smycroft  *    notice, this list of conditions and the following disclaimer.
12e40b0636Smycroft  * 2. Redistributions in binary form must reproduce the above copyright
13e40b0636Smycroft  *    notice, this list of conditions and the following disclaimer in the
14e40b0636Smycroft  *    documentation and/or other materials provided with the distribution.
15b5b29542Sagc  * 3. Neither the name of the University nor the names of its contributors
16e40b0636Smycroft  *    may be used to endorse or promote products derived from this software
17e40b0636Smycroft  *    without specific prior written permission.
18e40b0636Smycroft  *
19e40b0636Smycroft  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20e40b0636Smycroft  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21e40b0636Smycroft  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22e40b0636Smycroft  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23e40b0636Smycroft  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24e40b0636Smycroft  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25e40b0636Smycroft  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26e40b0636Smycroft  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27e40b0636Smycroft  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28e40b0636Smycroft  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29e40b0636Smycroft  * SUCH DAMAGE.
30e40b0636Smycroft  */
31e40b0636Smycroft 
32e800d25eSchristos #include <sys/cdefs.h>
33e40b0636Smycroft #ifndef lint
346ff461a4Scgd #if 0
35e40b0636Smycroft static char sccsid[] = "@(#)util.c	8.2 (Berkeley) 4/2/94";
366ff461a4Scgd #else
37*6f4d1070Schristos __RCSID("$NetBSD: util.c,v 1.11 2006/12/15 22:45:34 christos Exp $");
386ff461a4Scgd #endif
39e40b0636Smycroft #endif /* not lint */
40e40b0636Smycroft 
41e40b0636Smycroft #include <sys/param.h>
42e40b0636Smycroft #include <sys/stat.h>
43e40b0636Smycroft #include <sys/wait.h>
44e40b0636Smycroft 
45e40b0636Smycroft #include <ctype.h>
46e40b0636Smycroft #include <err.h>
47e40b0636Smycroft #include <errno.h>
48e40b0636Smycroft #include <paths.h>
49e40b0636Smycroft #include <signal.h>
50e40b0636Smycroft #include <stdio.h>
51e40b0636Smycroft #include <stdlib.h>
52e40b0636Smycroft #include <string.h>
53e40b0636Smycroft #include <unistd.h>
54e40b0636Smycroft 
55e40b0636Smycroft #include "extern.h"
56e40b0636Smycroft 
57e40b0636Smycroft char *
colon(char * cp)580b39b8daSxtraeme colon(char *cp)
59e40b0636Smycroft {
60e40b0636Smycroft 	if (*cp == ':')		/* Leading colon is part of file name. */
61e40b0636Smycroft 		return (0);
62e40b0636Smycroft 
63e40b0636Smycroft 	for (; *cp; ++cp) {
64e40b0636Smycroft 		if (*cp == ':')
65e40b0636Smycroft 			return (cp);
66e40b0636Smycroft 		if (*cp == '/')
67e40b0636Smycroft 			return (0);
68e40b0636Smycroft 	}
69e40b0636Smycroft 	return (0);
70e40b0636Smycroft }
71e40b0636Smycroft 
720d1bfba8Sginsbach char *
unbracket(char * cp)730d1bfba8Sginsbach unbracket(char *cp)
740d1bfba8Sginsbach {
750d1bfba8Sginsbach 	char *ep;
760d1bfba8Sginsbach 
770d1bfba8Sginsbach 	if (*cp == '[') {
780d1bfba8Sginsbach 		ep = cp + (strlen(cp) - 1);
790d1bfba8Sginsbach 		if (*ep == ']') {
800d1bfba8Sginsbach 			*ep = '\0';
810d1bfba8Sginsbach 			++cp;
820d1bfba8Sginsbach 		}
830d1bfba8Sginsbach 	}
840d1bfba8Sginsbach 	return (cp);
850d1bfba8Sginsbach }
860d1bfba8Sginsbach 
87e40b0636Smycroft void
verifydir(char * cp)880b39b8daSxtraeme verifydir(char *cp)
89e40b0636Smycroft {
90e40b0636Smycroft 	struct stat stb;
91e40b0636Smycroft 
92e40b0636Smycroft 	if (!stat(cp, &stb)) {
93e40b0636Smycroft 		if (S_ISDIR(stb.st_mode))
94e40b0636Smycroft 			return;
95e40b0636Smycroft 		errno = ENOTDIR;
96e40b0636Smycroft 	}
97e40b0636Smycroft 	run_err("%s: %s", cp, strerror(errno));
98e40b0636Smycroft 	exit(1);
999dc385beSmycroft 	/* NOTREACHED */
100e40b0636Smycroft }
101e40b0636Smycroft 
102e40b0636Smycroft int
okname(char * cp0)1030b39b8daSxtraeme okname(char *cp0)
104e40b0636Smycroft {
105e40b0636Smycroft 	int c;
106e40b0636Smycroft 	char *cp;
107e40b0636Smycroft 
108e40b0636Smycroft 	cp = cp0;
109e40b0636Smycroft 	do {
110e40b0636Smycroft 		c = *cp;
111e40b0636Smycroft 		if (c & 0200)
112e40b0636Smycroft 			goto bad;
113e40b0636Smycroft 		if (!isalpha(c) && !isdigit(c) && c != '_' && c != '-')
114e40b0636Smycroft 			goto bad;
115e40b0636Smycroft 	} while (*++cp);
116e40b0636Smycroft 	return (1);
117e40b0636Smycroft 
118e40b0636Smycroft bad:	warnx("%s: invalid user name", cp0);
119e40b0636Smycroft 	return (0);
120e40b0636Smycroft }
121e40b0636Smycroft 
122e40b0636Smycroft int
susystem(char * s)1230b39b8daSxtraeme susystem(char *s)
124e40b0636Smycroft {
125e40b0636Smycroft 	sig_t istat, qstat;
126e800d25eSchristos 	int status;
127e40b0636Smycroft 	pid_t pid;
128e40b0636Smycroft 
129e40b0636Smycroft 	pid = vfork();
130e40b0636Smycroft 	switch (pid) {
131e40b0636Smycroft 	case -1:
132e40b0636Smycroft 		return (127);
133e40b0636Smycroft 
134e40b0636Smycroft 	case 0:
135*6f4d1070Schristos 		(void)execl(_PATH_BSHELL, "sh", "-c", s, NULL);
136e40b0636Smycroft 		_exit(127);
1379dc385beSmycroft 		/* NOTREACHED */
138e40b0636Smycroft 	}
139e40b0636Smycroft 	istat = signal(SIGINT, SIG_IGN);
140e40b0636Smycroft 	qstat = signal(SIGQUIT, SIG_IGN);
141e40b0636Smycroft 	if (waitpid(pid, &status, 0) < 0)
142e40b0636Smycroft 		status = -1;
143e40b0636Smycroft 	(void)signal(SIGINT, istat);
144e40b0636Smycroft 	(void)signal(SIGQUIT, qstat);
145e40b0636Smycroft 	return (status);
146e40b0636Smycroft }
147e40b0636Smycroft 
148e40b0636Smycroft BUF *
allocbuf(BUF * bp,int fd,int blksize)1490b39b8daSxtraeme allocbuf(BUF *bp, int fd, int blksize)
150e40b0636Smycroft {
151e40b0636Smycroft 	struct stat stb;
152e40b0636Smycroft 	size_t size;
1537e1f4667Sitojun 	char *nbuf;
154e40b0636Smycroft 
155e40b0636Smycroft 	if (fstat(fd, &stb) < 0) {
156e40b0636Smycroft 		run_err("fstat: %s", strerror(errno));
157e40b0636Smycroft 		return (0);
158e40b0636Smycroft 	}
159e40b0636Smycroft 	size = roundup(stb.st_blksize, blksize);
160e40b0636Smycroft 	if (size == 0)
161e40b0636Smycroft 		size = blksize;
162e40b0636Smycroft 	if (bp->cnt >= size)
163e40b0636Smycroft 		return (bp);
1647e1f4667Sitojun 	if ((nbuf = realloc(bp->buf, size)) == NULL) {
1657e1f4667Sitojun 		free(bp->buf);
1667e1f4667Sitojun 		bp->buf = NULL;
167e40b0636Smycroft 		bp->cnt = 0;
168e40b0636Smycroft 		run_err("%s", strerror(errno));
169e40b0636Smycroft 		return (0);
170e40b0636Smycroft 	}
1717e1f4667Sitojun 	bp->buf = nbuf;
172e40b0636Smycroft 	bp->cnt = size;
173e40b0636Smycroft 	return (bp);
174e40b0636Smycroft }
175e40b0636Smycroft 
176e40b0636Smycroft void
177f6dadfcdSchristos /*ARGSUSED*/
lostconn(int signo __unused)178f6dadfcdSchristos lostconn(int signo __unused)
179e40b0636Smycroft {
180e40b0636Smycroft 	if (!iamremote)
181e40b0636Smycroft 		warnx("lost connection");
182e40b0636Smycroft 	exit(1);
1839dc385beSmycroft 	/* NOTREACHED */
184e40b0636Smycroft }
185