xref: /netbsd-src/usr.bin/uudecode/uudecode.c (revision 325671c8e2070712af081cce4aac6127ca7d6b2f)
1*325671c8Sapb /*	$NetBSD: uudecode.c,v 1.28 2013/01/28 19:50:30 apb Exp $	*/
288d0435bSjtc 
388d0435bSjtc /*-
488d0435bSjtc  * Copyright (c) 1983, 1993
588d0435bSjtc  *	The Regents of the University of California.  All rights reserved.
661f28255Scgd  *
761f28255Scgd  * Redistribution and use in source and binary forms, with or without
861f28255Scgd  * modification, are permitted provided that the following conditions
961f28255Scgd  * are met:
1061f28255Scgd  * 1. Redistributions of source code must retain the above copyright
1161f28255Scgd  *    notice, this list of conditions and the following disclaimer.
1261f28255Scgd  * 2. Redistributions in binary form must reproduce the above copyright
1361f28255Scgd  *    notice, this list of conditions and the following disclaimer in the
1461f28255Scgd  *    documentation and/or other materials provided with the distribution.
1589aaa1bbSagc  * 3. Neither the name of the University nor the names of its contributors
1661f28255Scgd  *    may be used to endorse or promote products derived from this software
1761f28255Scgd  *    without specific prior written permission.
1861f28255Scgd  *
1961f28255Scgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2061f28255Scgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2161f28255Scgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2261f28255Scgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2361f28255Scgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2461f28255Scgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2561f28255Scgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2661f28255Scgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2761f28255Scgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2861f28255Scgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2961f28255Scgd  * SUCH DAMAGE.
3061f28255Scgd  */
3161f28255Scgd 
32171d6532Slukem #if HAVE_NBTOOL_CONFIG_H
33171d6532Slukem #include "nbtool_config.h"
34201e4f38Slukem #endif
3588d0435bSjtc 
36171d6532Slukem #include <sys/cdefs.h>
37171d6532Slukem #if !defined(lint)
3898e5374cSlukem __COPYRIGHT("@(#) Copyright (c) 1983, 1993\
3998e5374cSlukem  The Regents of the University of California.  All rights reserved.");
4088d0435bSjtc #if 0
4188d0435bSjtc static char sccsid[] = "@(#)uudecode.c	8.2 (Berkeley) 4/2/94";
4288d0435bSjtc #endif
43*325671c8Sapb __RCSID("$NetBSD: uudecode.c,v 1.28 2013/01/28 19:50:30 apb Exp $");
4461f28255Scgd #endif /* not lint */
4561f28255Scgd 
4661f28255Scgd /*
4761f28255Scgd  * uudecode [file ...]
4861f28255Scgd  *
4961f28255Scgd  * create the specified file, decoding as you go.
5061f28255Scgd  * used with uuencode.
5161f28255Scgd  */
5261f28255Scgd #include <sys/param.h>
5361f28255Scgd #include <sys/stat.h>
54c187f70eStv #include <ctype.h>
55201e4f38Slukem #include <err.h>
56201e4f38Slukem #include <errno.h>
57c187f70eStv #include <limits.h>
58201e4f38Slukem #include <locale.h>
5961f28255Scgd #include <pwd.h>
60201e4f38Slukem #include <stdio.h>
61c187f70eStv #include <stdlib.h>
62201e4f38Slukem #include <string.h>
630605e44aSjtc #include <unistd.h>
6461f28255Scgd 
65f3b496ecSdbj #ifndef NO_BASE64
66f3b496ecSdbj #include <netinet/in.h>
67f3b496ecSdbj #include <resolv.h>
68f3b496ecSdbj #endif
69f3b496ecSdbj 
709a73b122Sapb static int decode(char *);
714d731124Sjoerg __dead static void usage(void);
723b4778f8Selad static int checkend(const char *, const char *, const char *);
733b4778f8Selad static int base64_decode(void);
74201e4f38Slukem 
759a73b122Sapb static int base64;
769a73b122Sapb static const char *inputname;
7761f28255Scgd 
78f7c6bf57Sjtc int
main(int argc,char * argv[])7905da4551Swiz main(int argc, char *argv[])
8061f28255Scgd {
815c39723cSkleink 	int ch, rval;
829a73b122Sapb 	char *outputname = NULL;
8361f28255Scgd 
840605e44aSjtc 	setlocale(LC_ALL, "");
8505da4551Swiz 	setprogname(argv[0]);
860605e44aSjtc 
879a73b122Sapb 	while ((ch = getopt(argc, argv, "mo:p")) != -1)
885c39723cSkleink 		switch (ch) {
893b4778f8Selad 		case 'm':
903b4778f8Selad 			base64 = 1;
913b4778f8Selad 			break;
929a73b122Sapb 		case 'o':
939a73b122Sapb 			outputname = optarg;
949a73b122Sapb 			break;
955c39723cSkleink 		case 'p':
969a73b122Sapb 			outputname = __UNCONST("/dev/stdout");
975c39723cSkleink 			break;
985c39723cSkleink 		default:
990605e44aSjtc 			usage();
1005c39723cSkleink 		}
1010605e44aSjtc 	argc -= optind;
1020605e44aSjtc 	argv += optind;
1030605e44aSjtc 
1040605e44aSjtc 	if (*argv) {
10561f28255Scgd 		rval = 0;
10661f28255Scgd 		do {
1079a73b122Sapb 			if (!freopen(inputname = *argv, "r", stdin)) {
1088e32c24cSitojun 				warn("%s", *argv);
10961f28255Scgd 				rval = 1;
11061f28255Scgd 				continue;
11161f28255Scgd 			}
1129a73b122Sapb 			rval |= decode(outputname);
11361f28255Scgd 		} while (*++argv);
11461f28255Scgd 	} else {
1159a73b122Sapb 		inputname = "stdin";
1169a73b122Sapb 		rval = decode(outputname);
11761f28255Scgd 	}
11861f28255Scgd 	exit(rval);
11961f28255Scgd }
12061f28255Scgd 
1219a73b122Sapb /*
1229a73b122Sapb  * Decode one file from stdin.  If outputname is not NULL
1239a73b122Sapb  * then it overrides the file name embedded in the input data.
1249a73b122Sapb  */
125f7c6bf57Sjtc static int
decode(char * outputname)1269a73b122Sapb decode(char *outputname)
12761f28255Scgd {
12861f28255Scgd 	struct passwd *pw;
129201e4f38Slukem 	int n;
130201e4f38Slukem 	char ch, *p;
1318139c502Shubertf 	int n1;
1328139c502Shubertf 	long mode;
1338139c502Shubertf 	char *fn;
13461f28255Scgd 	char buf[MAXPATHLEN];
13561f28255Scgd 
13661f28255Scgd 	/* search for header line */
1373b4778f8Selad 	for (;;) {
13861f28255Scgd 		if (!fgets(buf, sizeof(buf), stdin)) {
1399a73b122Sapb 			warnx("%s: no \"%s\" line", inputname, base64 ?
1403b4778f8Selad 					"begin-base64" : "begin");
14161f28255Scgd 			return(1);
14261f28255Scgd 		}
1433b4778f8Selad 		p = buf;
1443b4778f8Selad 		if (strncmp(p, "begin-base64", 12) == 0) {
1453b4778f8Selad 			base64 = 1;
1463b4778f8Selad 			p += 13;
1473b4778f8Selad 			break;
1483b4778f8Selad 		} else if (strncmp(p, "begin", 5) == 0)  {
1493b4778f8Selad 			p += 6;
1503b4778f8Selad 			break;
1513b4778f8Selad 		} else
1523b4778f8Selad 			continue;
1533b4778f8Selad 	}
1543b4778f8Selad 
1558139c502Shubertf         /* must be followed by an octal mode and a space */
1563b4778f8Selad 	mode = strtol(p, &fn, 8);
1573b4778f8Selad 	if (fn == (p) || !isspace((unsigned char)*fn) || mode==LONG_MIN || mode==LONG_MAX)
1588139c502Shubertf 	{
1599a73b122Sapb 	        warnx("%s: invalid mode on \"%s\" line", inputname,
1603b4778f8Selad 			base64 ? "begin-base64" : "begin");
1618139c502Shubertf 		return(1);
1628139c502Shubertf 	}
1638139c502Shubertf 	/* skip whitespace for file name */
164df38245cSdsl 	while (*fn && isspace((unsigned char)*fn)) fn++;
1658139c502Shubertf 	if (*fn == 0) {
1669a73b122Sapb                 warnx("%s: no filename on \"%s\" line", inputname,
1673b4778f8Selad 			base64 ? "begin-base64" : "begin");
1688139c502Shubertf 		return(1);
1698139c502Shubertf 	}
1708139c502Shubertf 	/* zap newline */
1718139c502Shubertf 	for (p = fn; *p && *p != '\n'; p++)
1728139c502Shubertf 	        ;
1738139c502Shubertf 	if (*p) *p = 0;
17461f28255Scgd 
1759a73b122Sapb 	/* outputname overrides fn */
1769a73b122Sapb 	if (outputname)
1779a73b122Sapb 		fn = outputname;
1789a73b122Sapb 
17961f28255Scgd 	/* handle ~user/file format */
1808139c502Shubertf 	if (*fn == '~') {
1818139c502Shubertf 		if (!(p = strchr(fn, '/'))) {
1829a73b122Sapb 			warnx("%s: illegal ~user.", inputname);
18361f28255Scgd 			return(1);
18461f28255Scgd 		}
185b55c3729Spk 		*p++ = '\0';
1868139c502Shubertf 		if (!(pw = getpwnam(fn + 1))) {
1879a73b122Sapb 			warnx("%s: no user %s.", inputname, buf);
18861f28255Scgd 			return(1);
18961f28255Scgd 		}
19061f28255Scgd 		n = strlen(pw->pw_dir);
19161f28255Scgd 		n1 = strlen(p);
19261f28255Scgd 		if (n + n1 + 2 > MAXPATHLEN) {
1939a73b122Sapb 			warnx("%s: path too long.", inputname);
19461f28255Scgd 			return(1);
19561f28255Scgd 		}
1968139c502Shubertf 		/* make space at beginning of buf by moving end of pathname */
197201e4f38Slukem 		memmove(buf + n + 1, p, n1 + 1);
198201e4f38Slukem 		memmove(buf, pw->pw_dir, n);
19961f28255Scgd 		buf[n] = '/';
2008139c502Shubertf 		fn = buf;
20161f28255Scgd 	}
20261f28255Scgd 
203*325671c8Sapb 	if (strcmp(fn, "/dev/stdout") == 0 || strcmp(fn, "-") == 0) {
204*325671c8Sapb 		/*
205*325671c8Sapb 		 * POSIX.1-2008 says that both "-" and "/dev/stdout"
206*325671c8Sapb 		 * refer to standard output when they appear in the file
207*325671c8Sapb 		 * header, but only "/dev/stdout" refers to standard
208*325671c8Sapb 		 * output when it appears as the argument to the "-o"
209*325671c8Sapb 		 * command line option.
210*325671c8Sapb 		 *
211*325671c8Sapb 		 * We handle both special names, regardless of whether
212*325671c8Sapb 		 * they came from the "-o" option or from the header of
213*325671c8Sapb 		 * the input stream.
214*325671c8Sapb 		 */
2159a73b122Sapb 	} else {
216*325671c8Sapb 		/*
217*325671c8Sapb 		 * Create output file, and set its mode.  POSIX.1-2008
218*325671c8Sapb 		 * requires the mode to be used exactly, ignoring the
219*325671c8Sapb 		 * umask and anything else, but we mask it with 0666.
220*325671c8Sapb 		 */
2219a73b122Sapb 		if (freopen(fn, "w", stdout) == NULL ||
2229a73b122Sapb 		    fchmod(fileno(stdout), mode & 0666) != 0) {
2239a73b122Sapb 			warn("%s: %s", fn, inputname);
22461f28255Scgd 			return(1);
22561f28255Scgd 		}
2269a73b122Sapb 	}
22761f28255Scgd 
2283b4778f8Selad 	if (base64)
2293b4778f8Selad 		return base64_decode();
2303b4778f8Selad 	else {
23161f28255Scgd 		/* for each input line */
23261f28255Scgd 		for (;;) {
23361f28255Scgd 			if (!fgets(p = buf, sizeof(buf), stdin)) {
2349a73b122Sapb 				warnx("%s: short file.", inputname);
23561f28255Scgd 				return(1);
23661f28255Scgd 			}
23761f28255Scgd #define	DEC(c)	(((c) - ' ') & 077)		/* single character decode */
23861f28255Scgd 			/*
23961f28255Scgd 		 	* `n' is used to avoid writing out all the characters
24061f28255Scgd 		 	* at the end of the file.
24161f28255Scgd 		 	*/
24261f28255Scgd 			if ((n = DEC(*p)) <= 0)
24361f28255Scgd 				break;
24461f28255Scgd 			for (++p; n > 0; p += 4, n -= 3)
24561f28255Scgd 				if (n >= 3) {
24661f28255Scgd 					ch = DEC(p[0]) << 2 | DEC(p[1]) >> 4;
24761f28255Scgd 					putchar(ch);
24861f28255Scgd 					ch = DEC(p[1]) << 4 | DEC(p[2]) >> 2;
24961f28255Scgd 					putchar(ch);
25061f28255Scgd 					ch = DEC(p[2]) << 6 | DEC(p[3]);
25161f28255Scgd 					putchar(ch);
25261f28255Scgd 				}
25361f28255Scgd 				else {
25461f28255Scgd 					if (n >= 1) {
25561f28255Scgd 						ch = DEC(p[0]) << 2 | DEC(p[1]) >> 4;
25661f28255Scgd 						putchar(ch);
25761f28255Scgd 					}
25861f28255Scgd 					if (n >= 2) {
25961f28255Scgd 						ch = DEC(p[1]) << 4 | DEC(p[2]) >> 2;
26061f28255Scgd 						putchar(ch);
26161f28255Scgd 					}
26261f28255Scgd 					if (n >= 3) {
26361f28255Scgd 						ch = DEC(p[2]) << 6 | DEC(p[3]);
26461f28255Scgd 						putchar(ch);
26561f28255Scgd 					}
26661f28255Scgd 				}
26761f28255Scgd 		}
26861f28255Scgd 		if (!fgets(buf, sizeof(buf), stdin) || strcmp(buf, "end\n")) {
2699a73b122Sapb 			warnx("%s: no \"end\" line.", inputname);
27061f28255Scgd 			return(1);
27161f28255Scgd 		}
27261f28255Scgd 		return(0);
27361f28255Scgd 	}
2743b4778f8Selad }
2753b4778f8Selad 
2763b4778f8Selad static int
checkend(const char * ptr,const char * end,const char * msg)2773b4778f8Selad checkend(const char *ptr, const char *end, const char *msg)
2783b4778f8Selad {
2793b4778f8Selad 	size_t n;
2803b4778f8Selad 
2813b4778f8Selad 	n = strlen(end);
2823b4778f8Selad 	if (strncmp(ptr, end, n) != 0 ||
2833b4778f8Selad 	    strspn(ptr + n, "\t\r\n") != strlen(ptr + n)) {
2843b4778f8Selad 		warnx("%s", msg);
2853b4778f8Selad 		return (1);
2863b4778f8Selad 	}
2873b4778f8Selad 	return (0);
2883b4778f8Selad }
2893b4778f8Selad 
2903b4778f8Selad static int
base64_decode(void)2913b4778f8Selad base64_decode(void)
2923b4778f8Selad {
2933b4778f8Selad 	int n;
2943b4778f8Selad 	char inbuf[MAXPATHLEN];
2953b4778f8Selad 	unsigned char outbuf[MAXPATHLEN * 4];
2963b4778f8Selad 
2973b4778f8Selad 	for (;;) {
2983b4778f8Selad 		if (!fgets(inbuf, sizeof(inbuf), stdin)) {
2999a73b122Sapb 			warnx("%s: short file.", inputname);
3003b4778f8Selad 			return (1);
3013b4778f8Selad 		}
302f3b496ecSdbj #ifdef NO_BASE64
3039a73b122Sapb 		warnx("%s: base64 decoding is not supported", inputname);
304f3b496ecSdbj 		return (1);
305f3b496ecSdbj #else
3063b4778f8Selad 		n = b64_pton(inbuf, outbuf, sizeof(outbuf));
307f3b496ecSdbj #endif
3083b4778f8Selad 		if (n < 0)
3093b4778f8Selad 			break;
3103b4778f8Selad 		fwrite(outbuf, 1, n, stdout);
3113b4778f8Selad 	}
3123b4778f8Selad 	return (checkend(inbuf, "====",
3133b4778f8Selad 			"error decoding base64 input stream"));
3143b4778f8Selad }
31561f28255Scgd 
316f7c6bf57Sjtc static void
usage(void)3174d731124Sjoerg usage(void)
31861f28255Scgd {
3199a73b122Sapb 	(void)fprintf(stderr,
3209a73b122Sapb 		      "usage: %s [-m] [-p | -o outfile] [encoded-file ...]\n",
32105da4551Swiz 		      getprogname());
32261f28255Scgd 	exit(1);
32361f28255Scgd }
324