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