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