xref: /netbsd-src/bin/rcp/util.c (revision 76dfffe33547c37f8bdd446e3e4ab0f3c16cea4b)
1 /*	$NetBSD: util.c,v 1.2 1995/03/21 08:19:08 cgd Exp $	*/
2 
3 /*-
4  * Copyright (c) 1992, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. All advertising materials mentioning features or use of this software
16  *    must display the following acknowledgement:
17  *	This product includes software developed by the University of
18  *	California, Berkeley and its contributors.
19  * 4. Neither the name of the University nor the names of its contributors
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  */
35 
36 #ifndef lint
37 #if 0
38 static char sccsid[] = "@(#)util.c	8.2 (Berkeley) 4/2/94";
39 #else
40 static char rcsid[] = "$NetBSD: util.c,v 1.2 1995/03/21 08:19:08 cgd Exp $";
41 #endif
42 #endif /* not lint */
43 
44 #include <sys/param.h>
45 #include <sys/stat.h>
46 #include <sys/wait.h>
47 
48 #include <ctype.h>
49 #include <err.h>
50 #include <errno.h>
51 #include <paths.h>
52 #include <signal.h>
53 #include <stdio.h>
54 #include <stdlib.h>
55 #include <string.h>
56 #include <unistd.h>
57 
58 #include "extern.h"
59 
60 char *
61 colon(cp)
62 	char *cp;
63 {
64 	if (*cp == ':')		/* Leading colon is part of file name. */
65 		return (0);
66 
67 	for (; *cp; ++cp) {
68 		if (*cp == ':')
69 			return (cp);
70 		if (*cp == '/')
71 			return (0);
72 	}
73 	return (0);
74 }
75 
76 void
77 verifydir(cp)
78 	char *cp;
79 {
80 	struct stat stb;
81 
82 	if (!stat(cp, &stb)) {
83 		if (S_ISDIR(stb.st_mode))
84 			return;
85 		errno = ENOTDIR;
86 	}
87 	run_err("%s: %s", cp, strerror(errno));
88 	exit(1);
89 }
90 
91 int
92 okname(cp0)
93 	char *cp0;
94 {
95 	int c;
96 	char *cp;
97 
98 	cp = cp0;
99 	do {
100 		c = *cp;
101 		if (c & 0200)
102 			goto bad;
103 		if (!isalpha(c) && !isdigit(c) && c != '_' && c != '-')
104 			goto bad;
105 	} while (*++cp);
106 	return (1);
107 
108 bad:	warnx("%s: invalid user name", cp0);
109 	return (0);
110 }
111 
112 int
113 susystem(s, userid)
114 	int userid;
115 	char *s;
116 {
117 	sig_t istat, qstat;
118 	int status, w;
119 	pid_t pid;
120 
121 	pid = vfork();
122 	switch (pid) {
123 	case -1:
124 		return (127);
125 
126 	case 0:
127 		(void)setuid(userid);
128 		execl(_PATH_BSHELL, "sh", "-c", s, NULL);
129 		_exit(127);
130 	}
131 	istat = signal(SIGINT, SIG_IGN);
132 	qstat = signal(SIGQUIT, SIG_IGN);
133 	if (waitpid(pid, &status, 0) < 0)
134 		status = -1;
135 	(void)signal(SIGINT, istat);
136 	(void)signal(SIGQUIT, qstat);
137 	return (status);
138 }
139 
140 BUF *
141 allocbuf(bp, fd, blksize)
142 	BUF *bp;
143 	int fd, blksize;
144 {
145 	struct stat stb;
146 	size_t size;
147 
148 	if (fstat(fd, &stb) < 0) {
149 		run_err("fstat: %s", strerror(errno));
150 		return (0);
151 	}
152 	size = roundup(stb.st_blksize, blksize);
153 	if (size == 0)
154 		size = blksize;
155 	if (bp->cnt >= size)
156 		return (bp);
157 	if ((bp->buf = realloc(bp->buf, size)) == NULL) {
158 		bp->cnt = 0;
159 		run_err("%s", strerror(errno));
160 		return (0);
161 	}
162 	bp->cnt = size;
163 	return (bp);
164 }
165 
166 void
167 lostconn(signo)
168 	int signo;
169 {
170 	if (!iamremote)
171 		warnx("lost connection");
172 	exit(1);
173 }
174