1 /**	$MirOS: src/usr.bin/uudecode/uudecode.c,v 1.5 2014/04/21 15:41:09 tg Exp $ */
2 /*	$OpenBSD: uudecode.c,v 1.14 2004/04/09 22:54:02 millert Exp $	*/
3 /*	$FreeBSD: uudecode.c,v 1.49 2003/05/03 19:44:46 obrien Exp $	*/
4 
5 /*-
6  * Copyright (c) 1983, 1993
7  *	The Regents of the University of California.  All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  * 3. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
33 
34 #include <sys/cdefs.h>
35 __COPYRIGHT("@(#) Copyright (c) 1983, 1993\n\
36 	The Regents of the University of California.  All rights reserved.\n");
37 
38 /*
39  * Create the specified file, decoding as you go.
40  * Used with uuencode.
41  */
42 
43 #include <sys/param.h>
44 #include <sys/socket.h>
45 #include <sys/stat.h>
46 
47 #include <netinet/in.h>
48 
49 #include <err.h>
50 #include <errno.h>
51 #include <fcntl.h>
52 #include <locale.h>
53 #include <pwd.h>
54 #include <resolv.h>
55 #include <stdio.h>
56 #include <stdlib.h>
57 #include <string.h>
58 #include <unistd.h>
59 
60 #ifdef USE_LIBBSD
61 #include <bsd/unistd.h>
62 #endif
63 
64 __SCCSID("@(#)uudecode.c	8.2 (Berkeley) 4/2/94");
65 __RCSID("$MirOS: src/usr.bin/uudecode/uudecode.c,v 1.5 2014/04/21 15:41:09 tg Exp $");
66 
67 static const char *infile, *outfile;
68 static FILE *infp, *outfp;
69 static int base64, cflag, iflag, oflag, pflag, rflag, sflag;
70 
71 static void	usage(void) __dead;
72 static int	decode(void);
73 static int	decode2(void);
74 static int	uu_decode(void);
75 static int	base64_decode(void);
76 
77 int
main(int argc,char * argv[])78 main(int argc, char *argv[])
79 {
80 	int rval, ch;
81 	extern char *__progname;
82 
83 	if (strcmp(__progname, "b64decode") == 0)
84 		base64 = 1;
85 
86 #ifndef __MirBSD__
87 	setlocale(LC_ALL, "");
88 #endif
89 	while ((ch = getopt(argc, argv, "cimo:prs")) != -1) {
90 		switch(ch) {
91 		case 'c':
92 			if (oflag || rflag)
93 				usage();
94 			cflag = 1; /* multiple uudecode'd files */
95 			break;
96 		case 'i':
97 			iflag = 1; /* ask before override files */
98 			break;
99 		case 'm':
100 			base64 = 1;
101 			break;
102 		case 'o':
103 			if (cflag || pflag || rflag || sflag)
104 				usage();
105 			oflag = 1; /* output to the specified file */
106 			sflag = 1; /* do not strip pathnames for output */
107 			outfile = optarg; /* set the output filename */
108 			break;
109 		case 'p':
110 			if (oflag)
111 				usage();
112 			pflag = 1; /* print output to stdout */
113 			break;
114 		case 'r':
115 			if (cflag || oflag)
116 				usage();
117 			rflag = 1; /* decode raw data */
118 			break;
119 		case 's':
120 			if (oflag)
121 				usage();
122 			sflag = 1; /* do not strip pathnames for output */
123 			break;
124 		default:
125 			usage();
126 		}
127 	}
128 	argc -= optind;
129 	argv += optind;
130 
131 	if (*argv) {
132 		rval = 0;
133 		do {
134 			infp = fopen(infile = *argv, "r");
135 			if (infp == NULL) {
136 				warn("%s", *argv);
137 				rval = 1;
138 				continue;
139 			}
140 			rval |= decode();
141 			fclose(infp);
142 		} while (*++argv);
143 	} else {
144 		infile = "stdin";
145 		infp = stdin;
146 		rval = decode();
147 	}
148 	exit(rval);
149 }
150 
151 static int
decode(void)152 decode(void)
153 {
154 	int r, v;
155 
156 	if (rflag) {
157 		/* relaxed alternative to decode2() */
158 		outfile = "/dev/stdout";
159 		outfp = stdout;
160 		if (base64)
161 			return (base64_decode());
162 		else
163 			return (uu_decode());
164 	}
165 	v = decode2();
166 	if (v == EOF) {
167 		warnx("%s: missing or bad \"begin\" line", infile);
168 		return (1);
169 	}
170 	for (r = v; cflag; r |= v) {
171 		v = decode2();
172 		if (v == EOF)
173 			break;
174 	}
175 	return (r);
176 }
177 
178 static int
decode2(void)179 decode2(void)
180 {
181 	int flags, fd, mode;
182 	size_t n, m;
183 	char *p, *q;
184 	void *handle;
185 	struct passwd *pw;
186 	struct stat st;
187 	char buf[MAXPATHLEN];
188 
189 	base64 = 0;
190 	/* search for header line */
191 	for (;;) {
192 		if (fgets(buf, sizeof(buf), infp) == NULL)
193 			return (EOF);
194 		p = buf;
195 		if (strncmp(p, "begin-base64 ", 13) == 0) {
196 			base64 = 1;
197 			p += 13;
198 		} else if (strncmp(p, "begin ", 6) == 0)
199 			p += 6;
200 		else
201 			continue;
202 		/* p points to mode */
203 		q = strchr(p, ' ');
204 		if (q == NULL)
205 			continue;
206 		*q++ = '\0';
207 		/* q points to filename */
208 		n = strlen(q);
209 		while (n > 0 && (q[n-1] == '\n' || q[n-1] == '\r'))
210 			q[--n] = '\0';
211 		/* found valid header? */
212 		if (n > 0)
213 			break;
214 	}
215 
216 	handle = setmode(p);
217 	if (handle == NULL) {
218 		warnx("%s: unable to parse file mode", infile);
219 		return (1);
220 	}
221 	mode = getmode(handle, 0) & 0666;
222 	free(handle);
223 
224 	if (sflag) {
225 		/* don't strip, so try ~user/file expansion */
226 		p = NULL;
227 		pw = NULL;
228 		if (*q == '~')
229 			p = strchr(q, '/');
230 		if (p != NULL) {
231 			*p = '\0';
232 			pw = getpwnam(q + 1);
233 			*p = '/';
234 		}
235 		if (pw != NULL) {
236 			n = strlen(pw->pw_dir);
237 			if (buf + n > p) {
238 				/* make room */
239 				m = strlen(p);
240 				if (sizeof(buf) < n + m) {
241 					warnx("%s: bad output filename",
242 					    infile);
243 					return (1);
244 				}
245 				p = memmove(buf + n, p, m);
246 			}
247 			q = memcpy(p - n, pw->pw_dir, n);
248 		}
249 	} else {
250 		/* strip down to leaf name */
251 		p = strrchr(q, '/');
252 		if (p != NULL)
253 			q = p + 1;
254 	}
255 	if (!oflag)
256 		outfile = q;
257 
258 	/* POSIX says "/dev/stdout" is a 'magic cookie' not a special file. */
259 	if (pflag || strcmp(outfile, "/dev/stdout") == 0)
260 		outfp = stdout;
261 	else {
262 		flags = O_WRONLY|O_CREAT|O_EXCL|O_NOFOLLOW;
263 		if (lstat(outfile, &st) == 0) {
264 			if (iflag) {
265 				errno = EEXIST;
266 				warn("%s: %s", infile, outfile);
267 				return (0);
268 			}
269 			switch (st.st_mode & S_IFMT) {
270 			case S_IFREG:
271 			case S_IFLNK:
272 				/* avoid symlink attacks */
273 				if (unlink(outfile) == 0 || errno == ENOENT)
274 					break;
275 				warn("%s: unlink %s", infile, outfile);
276 				return (1);
277 			case S_IFDIR:
278 				errno = EISDIR;
279 				warn("%s: %s", infile, outfile);
280 				return (1);
281 			default:
282 				if (oflag) {
283 					/* trust command-line names */
284 					flags &= ~(O_EXCL|O_NOFOLLOW);
285 					break;
286 				}
287 				errno = EEXIST;
288 				warn("%s: %s", infile, outfile);
289 				return (1);
290 			}
291 		} else if (errno != ENOENT) {
292 			warn("%s: %s", infile, outfile);
293 			return (1);
294 		}
295 		if ((fd = open(outfile, flags, mode)) < 0 ||
296 		    (outfp = fdopen(fd, "w")) == NULL) {
297 			warn("%s: %s", infile, outfile);
298 			return (1);
299 		}
300 	}
301 
302 	if (base64)
303 		return (base64_decode());
304 	else
305 		return (uu_decode());
306 }
307 
308 static int
getline_l(char * buf,size_t size)309 getline_l(char *buf, size_t size)
310 {
311 	if (fgets(buf, size, infp) != NULL)
312 		return (2);
313 	if (rflag)
314 		return (0);
315 	warnx("%s: %s: short file", infile, outfile);
316 	return (1);
317 }
318 
319 static int
checkend(const char * ptr,const char * end,const char * msg)320 checkend(const char *ptr, const char *end, const char *msg)
321 {
322 	size_t n;
323 
324 	n = strlen(end);
325 	if (strncmp(ptr, end, n) != 0 ||
326 	    strspn(ptr + n, " \t\r\n") != strlen(ptr + n)) {
327 		warnx("%s: %s: %s", infile, outfile, msg);
328 		if (!rflag)
329 			return (1);
330 	}
331 	if (fclose(outfp) != 0) {
332 		warn("%s: %s", infile, outfile);
333 		return (1);
334 	}
335 	return (0);
336 }
337 
338 static int
uu_decode(void)339 uu_decode(void)
340 {
341 	int i, ch;
342 	char *p;
343 	char buf[MAXPATHLEN];
344 
345 	/* for each input line */
346 	for (;;) {
347 		switch (getline_l(buf, sizeof(buf))) {
348 		case 0:
349 			return (0);
350 		case 1:
351 			return (1);
352 		}
353 
354 #define	DEC(c)	(((c) - ' ') & 077)		/* single character decode */
355 #define IS_DEC(c) ( (((c) - ' ') >= 0) && (((c) - ' ') <= 077 + 1) )
356 
357 #define OUT_OF_RANGE do {						\
358 	warnx("%s: %s: character out of range: [%d-%d]",		\
359 	    infile, outfile, 1 + ' ', 077 + ' ' + 1);			\
360 	return (1);							\
361 } while (0)
362 
363 		/*
364 		 * `i' is used to avoid writing out all the characters
365 		 * at the end of the file.
366 		 */
367 		p = buf;
368 		if ((i = DEC(*p)) <= 0)
369 			break;
370 		for (++p; i > 0; p += 4, i -= 3)
371 			if (i >= 3) {
372 				if (!(IS_DEC(*p) && IS_DEC(*(p + 1)) &&
373 				     IS_DEC(*(p + 2)) && IS_DEC(*(p + 3))))
374 					OUT_OF_RANGE;
375 
376 				ch = DEC(p[0]) << 2 | DEC(p[1]) >> 4;
377 				putc(ch, outfp);
378 				ch = DEC(p[1]) << 4 | DEC(p[2]) >> 2;
379 				putc(ch, outfp);
380 				ch = DEC(p[2]) << 6 | DEC(p[3]);
381 				putc(ch, outfp);
382 			}
383 			else {
384 				if (i >= 1) {
385 					if (!(IS_DEC(*p) && IS_DEC(*(p + 1))))
386 						OUT_OF_RANGE;
387 					ch = DEC(p[0]) << 2 | DEC(p[1]) >> 4;
388 					putc(ch, outfp);
389 				}
390 				if (i >= 2) {
391 					if (!(IS_DEC(*(p + 1)) &&
392 					    IS_DEC(*(p + 2))))
393 						OUT_OF_RANGE;
394 
395 					ch = DEC(p[1]) << 4 | DEC(p[2]) >> 2;
396 					putc(ch, outfp);
397 				}
398 				if (i >= 3) {
399 					if (!(IS_DEC(*(p + 2)) &&
400 					    IS_DEC(*(p + 3))))
401 						OUT_OF_RANGE;
402 					ch = DEC(p[2]) << 6 | DEC(p[3]);
403 					putc(ch, outfp);
404 				}
405 			}
406 	}
407 	switch (getline_l(buf, sizeof(buf))) {
408 	case 0:
409 		return (0);
410 	case 1:
411 		return (1);
412 	default:
413 		return (checkend(buf, "end", "no \"end\" line"));
414 	}
415 }
416 
417 static int
base64_decode(void)418 base64_decode(void)
419 {
420 	int n;
421 	char inbuf[(MAXPATHLEN & ~3) + 1];
422 	unsigned char outbuf[MAXPATHLEN * 4];
423 
424 	for (;;) {
425 		switch (getline_l(inbuf, sizeof(inbuf))) {
426 		case 0:
427 			return (0);
428 		case 1:
429 			return (1);
430 		}
431 		n = b64_pton(inbuf, outbuf, sizeof(outbuf));
432 		if (n < 0)
433 			break;
434 		fwrite(outbuf, 1, n, outfp);
435 	}
436 	return (checkend(inbuf, "====",
437 		    "error decoding base64 input stream"));
438 }
439 
440 static void
usage(void)441 usage(void)
442 {
443 	(void)fprintf(stderr,
444 	    "usage: uudecode [-cimprs] [file ...]\n"
445 	    "       uudecode [-i] -o output_file [file]\n"
446 	    "       b64decode [-cimprs] [file ...]\n"
447 	    "       b64decode [-i] -o output_file [file]\n");
448 	exit(1);
449 }
450