xref: /csrg-svn/bin/rcp/util.c (revision 54148)
1*54148Sbostic /*-
2*54148Sbostic  * Copyright (c) 1992 The Regents of the University of California.
3*54148Sbostic  * All rights reserved.
4*54148Sbostic  *
5*54148Sbostic  * %sccs.include.redist.c%
6*54148Sbostic  */
7*54148Sbostic 
8*54148Sbostic #ifndef lint
9*54148Sbostic static char sccsid[] = "@(#)util.c	5.1 (Berkeley) 06/20/92";
10*54148Sbostic #endif /* not lint */
11*54148Sbostic 
12*54148Sbostic #include <sys/param.h>
13*54148Sbostic #include <sys/stat.h>
14*54148Sbostic #include <sys/wait.h>
15*54148Sbostic 
16*54148Sbostic #include <signal.h>
17*54148Sbostic #include <errno.h>
18*54148Sbostic #include <unistd.h>
19*54148Sbostic #include <stdlib.h>
20*54148Sbostic #include <stdio.h>
21*54148Sbostic #include <ctype.h>
22*54148Sbostic #include <string.h>
23*54148Sbostic #include <paths.h>
24*54148Sbostic #include "extern.h"
25*54148Sbostic 
26*54148Sbostic char *
27*54148Sbostic colon(cp)
28*54148Sbostic 	register char *cp;
29*54148Sbostic {
30*54148Sbostic 	for (; *cp; ++cp) {
31*54148Sbostic 		if (*cp == ':')
32*54148Sbostic 			return (cp);
33*54148Sbostic 		if (*cp == '/')
34*54148Sbostic 			return (0);
35*54148Sbostic 	}
36*54148Sbostic 	return (0);
37*54148Sbostic }
38*54148Sbostic 
39*54148Sbostic void
40*54148Sbostic verifydir(cp)
41*54148Sbostic 	char *cp;
42*54148Sbostic {
43*54148Sbostic 	struct stat stb;
44*54148Sbostic 
45*54148Sbostic 	if (!stat(cp, &stb)) {
46*54148Sbostic 		if (S_ISDIR(stb.st_mode))
47*54148Sbostic 			return;
48*54148Sbostic 		errno = ENOTDIR;
49*54148Sbostic 	}
50*54148Sbostic 	err("%s: %s", cp, strerror(errno));
51*54148Sbostic 	exit(1);
52*54148Sbostic }
53*54148Sbostic 
54*54148Sbostic int
55*54148Sbostic okname(cp0)
56*54148Sbostic 	char *cp0;
57*54148Sbostic {
58*54148Sbostic 	register int c;
59*54148Sbostic 	register char *cp;
60*54148Sbostic 
61*54148Sbostic 	cp = cp0;
62*54148Sbostic 	do {
63*54148Sbostic 		c = *cp;
64*54148Sbostic 		if (c & 0200)
65*54148Sbostic 			goto bad;
66*54148Sbostic 		if (!isalpha(c) && !isdigit(c) && c != '_' && c != '-')
67*54148Sbostic 			goto bad;
68*54148Sbostic 	} while (*++cp);
69*54148Sbostic 	return (1);
70*54148Sbostic 
71*54148Sbostic bad:	(void)fprintf(stderr, "rcp: %s: invalid user name\n", cp0);
72*54148Sbostic 	return (0);
73*54148Sbostic }
74*54148Sbostic 
75*54148Sbostic int
76*54148Sbostic susystem(s, userid)
77*54148Sbostic 	int userid;
78*54148Sbostic 	char *s;
79*54148Sbostic {
80*54148Sbostic 	register sig_t istat, qstat;
81*54148Sbostic 	int status, pid, w;
82*54148Sbostic 
83*54148Sbostic 	if ((pid = vfork()) == 0) {
84*54148Sbostic 		(void)setuid(userid);
85*54148Sbostic 		execl(_PATH_BSHELL, "sh", "-c", s, NULL);
86*54148Sbostic 		_exit(127);
87*54148Sbostic 	}
88*54148Sbostic 	istat = signal(SIGINT, SIG_IGN);
89*54148Sbostic 	qstat = signal(SIGQUIT, SIG_IGN);
90*54148Sbostic 	while ((w = wait(&status)) != pid && w != -1)
91*54148Sbostic 		;
92*54148Sbostic 	if (w == -1)
93*54148Sbostic 		status = -1;
94*54148Sbostic 	(void)signal(SIGINT, istat);
95*54148Sbostic 	(void)signal(SIGQUIT, qstat);
96*54148Sbostic 	return (status);
97*54148Sbostic }
98*54148Sbostic 
99*54148Sbostic BUF *
100*54148Sbostic allocbuf(bp, fd, blksize)
101*54148Sbostic 	BUF *bp;
102*54148Sbostic 	int fd, blksize;
103*54148Sbostic {
104*54148Sbostic 	struct stat stb;
105*54148Sbostic 	size_t size;
106*54148Sbostic 
107*54148Sbostic 	if (fstat(fd, &stb) < 0) {
108*54148Sbostic 		err("fstat: %s", strerror(errno));
109*54148Sbostic 		return (0);
110*54148Sbostic 	}
111*54148Sbostic 	size = roundup(stb.st_blksize, blksize);
112*54148Sbostic 	if (size == 0)
113*54148Sbostic 		size = blksize;
114*54148Sbostic 	if (bp->cnt >= size)
115*54148Sbostic 		return (bp);
116*54148Sbostic 	if ((bp->buf = realloc(bp->buf, size)) == NULL) {
117*54148Sbostic 		bp->cnt = 0;
118*54148Sbostic 		err("%s", strerror(errno));
119*54148Sbostic 		return (0);
120*54148Sbostic 	}
121*54148Sbostic 	bp->cnt = size;
122*54148Sbostic 	return (bp);
123*54148Sbostic }
124*54148Sbostic 
125*54148Sbostic void
126*54148Sbostic lostconn(signo)
127*54148Sbostic 	int signo;
128*54148Sbostic {
129*54148Sbostic 	if (!iamremote)
130*54148Sbostic 		(void)fprintf(stderr, "rcp: lost connection\n");
131*54148Sbostic 	exit(1);
132*54148Sbostic }
133*54148Sbostic 
134*54148Sbostic void
135*54148Sbostic nospace()
136*54148Sbostic {
137*54148Sbostic 	(void)fprintf(stderr, "rcp: %s\n", strerror(errno));
138*54148Sbostic 	exit(1);
139*54148Sbostic }
140