1 /* $NetBSD: uudecode.c,v 1.25 2009/04/14 06:16:59 lukem Exp $ */ 2 3 /*- 4 * Copyright (c) 1983, 1993 5 * The Regents of the University of California. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. Neither the name of the University nor the names of its contributors 16 * may be used to endorse or promote products derived from this software 17 * without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 * SUCH DAMAGE. 30 */ 31 32 #if HAVE_NBTOOL_CONFIG_H 33 #include "nbtool_config.h" 34 #endif 35 36 #include <sys/cdefs.h> 37 #if !defined(lint) 38 __COPYRIGHT("@(#) Copyright (c) 1983, 1993\ 39 The Regents of the University of California. All rights reserved."); 40 #if 0 41 static char sccsid[] = "@(#)uudecode.c 8.2 (Berkeley) 4/2/94"; 42 #endif 43 __RCSID("$NetBSD: uudecode.c,v 1.25 2009/04/14 06:16:59 lukem Exp $"); 44 #endif /* not lint */ 45 46 /* 47 * uudecode [file ...] 48 * 49 * create the specified file, decoding as you go. 50 * used with uuencode. 51 */ 52 #include <sys/param.h> 53 #include <sys/stat.h> 54 #include <ctype.h> 55 #include <err.h> 56 #include <errno.h> 57 #include <limits.h> 58 #include <locale.h> 59 #include <pwd.h> 60 #include <stdio.h> 61 #include <stdlib.h> 62 #include <string.h> 63 #include <unistd.h> 64 65 #ifndef NO_BASE64 66 #include <netinet/in.h> 67 #include <resolv.h> 68 #endif 69 70 static int decode(void); 71 static void usage(void); 72 static int checkend(const char *, const char *, const char *); 73 static int base64_decode(void); 74 int main(int, char *[]); 75 76 int base64, pflag; 77 const char *filename; 78 79 int 80 main(int argc, char *argv[]) 81 { 82 int ch, rval; 83 84 setlocale(LC_ALL, ""); 85 setprogname(argv[0]); 86 87 pflag = 0; 88 while ((ch = getopt(argc, argv, "mp")) != -1) 89 switch (ch) { 90 case 'm': 91 base64 = 1; 92 break; 93 case 'p': 94 pflag = 1; 95 break; 96 default: 97 usage(); 98 } 99 argc -= optind; 100 argv += optind; 101 102 if (*argv) { 103 rval = 0; 104 do { 105 if (!freopen(filename = *argv, "r", stdin)) { 106 warn("%s", *argv); 107 rval = 1; 108 continue; 109 } 110 rval |= decode(); 111 } while (*++argv); 112 } else { 113 filename = "stdin"; 114 rval = decode(); 115 } 116 exit(rval); 117 } 118 119 static int 120 decode(void) 121 { 122 struct passwd *pw; 123 int n; 124 char ch, *p; 125 int n1; 126 long mode; 127 char *fn; 128 char buf[MAXPATHLEN]; 129 130 /* search for header line */ 131 for (;;) { 132 if (!fgets(buf, sizeof(buf), stdin)) { 133 warnx("%s: no \"%s\" line", filename, base64 ? 134 "begin-base64" : "begin"); 135 return(1); 136 } 137 p = buf; 138 if (strncmp(p, "begin-base64", 12) == 0) { 139 base64 = 1; 140 p += 13; 141 break; 142 } else if (strncmp(p, "begin", 5) == 0) { 143 p += 6; 144 break; 145 } else 146 continue; 147 } 148 149 /* must be followed by an octal mode and a space */ 150 mode = strtol(p, &fn, 8); 151 if (fn == (p) || !isspace((unsigned char)*fn) || mode==LONG_MIN || mode==LONG_MAX) 152 { 153 warnx("%s: invalid mode on \"%s\" line", filename, 154 base64 ? "begin-base64" : "begin"); 155 return(1); 156 } 157 /* skip whitespace for file name */ 158 while (*fn && isspace((unsigned char)*fn)) fn++; 159 if (*fn == 0) { 160 warnx("%s: no filename on \"%s\" line", filename, 161 base64 ? "begin-base64" : "begin"); 162 return(1); 163 } 164 /* zap newline */ 165 for (p = fn; *p && *p != '\n'; p++) 166 ; 167 if (*p) *p = 0; 168 169 /* handle ~user/file format */ 170 if (*fn == '~') { 171 if (!(p = strchr(fn, '/'))) { 172 warnx("%s: illegal ~user.", filename); 173 return(1); 174 } 175 *p++ = '\0'; 176 if (!(pw = getpwnam(fn + 1))) { 177 warnx("%s: no user %s.", filename, buf); 178 return(1); 179 } 180 n = strlen(pw->pw_dir); 181 n1 = strlen(p); 182 if (n + n1 + 2 > MAXPATHLEN) { 183 warnx("%s: path too long.", filename); 184 return(1); 185 } 186 /* make space at beginning of buf by moving end of pathname */ 187 memmove(buf + n + 1, p, n1 + 1); 188 memmove(buf, pw->pw_dir, n); 189 buf[n] = '/'; 190 fn = buf; 191 } 192 193 /* create output file, set mode */ 194 if (!pflag && (!freopen(fn, "w", stdout) || 195 fchmod(fileno(stdout), mode & 0666))) { 196 warn("%s: %s", fn, filename); 197 return(1); 198 } 199 200 if (base64) 201 return base64_decode(); 202 else { 203 /* for each input line */ 204 for (;;) { 205 if (!fgets(p = buf, sizeof(buf), stdin)) { 206 warnx("%s: short file.", filename); 207 return(1); 208 } 209 #define DEC(c) (((c) - ' ') & 077) /* single character decode */ 210 /* 211 * `n' is used to avoid writing out all the characters 212 * at the end of the file. 213 */ 214 if ((n = DEC(*p)) <= 0) 215 break; 216 for (++p; n > 0; p += 4, n -= 3) 217 if (n >= 3) { 218 ch = DEC(p[0]) << 2 | DEC(p[1]) >> 4; 219 putchar(ch); 220 ch = DEC(p[1]) << 4 | DEC(p[2]) >> 2; 221 putchar(ch); 222 ch = DEC(p[2]) << 6 | DEC(p[3]); 223 putchar(ch); 224 } 225 else { 226 if (n >= 1) { 227 ch = DEC(p[0]) << 2 | DEC(p[1]) >> 4; 228 putchar(ch); 229 } 230 if (n >= 2) { 231 ch = DEC(p[1]) << 4 | DEC(p[2]) >> 2; 232 putchar(ch); 233 } 234 if (n >= 3) { 235 ch = DEC(p[2]) << 6 | DEC(p[3]); 236 putchar(ch); 237 } 238 } 239 } 240 if (!fgets(buf, sizeof(buf), stdin) || strcmp(buf, "end\n")) { 241 warnx("%s: no \"end\" line.", filename); 242 return(1); 243 } 244 return(0); 245 } 246 } 247 248 static int 249 checkend(const char *ptr, const char *end, const char *msg) 250 { 251 size_t n; 252 253 n = strlen(end); 254 if (strncmp(ptr, end, n) != 0 || 255 strspn(ptr + n, "\t\r\n") != strlen(ptr + n)) { 256 warnx("%s", msg); 257 return (1); 258 } 259 return (0); 260 } 261 262 static int 263 base64_decode(void) 264 { 265 int n; 266 char inbuf[MAXPATHLEN]; 267 unsigned char outbuf[MAXPATHLEN * 4]; 268 269 for (;;) { 270 if (!fgets(inbuf, sizeof(inbuf), stdin)) { 271 warnx("%s: short file.", filename); 272 return (1); 273 } 274 #ifdef NO_BASE64 275 warnx("%s: base64 decoding is not supported", filename); 276 return (1); 277 #else 278 n = b64_pton(inbuf, outbuf, sizeof(outbuf)); 279 #endif 280 if (n < 0) 281 break; 282 fwrite(outbuf, 1, n, stdout); 283 } 284 return (checkend(inbuf, "====", 285 "error decoding base64 input stream")); 286 } 287 288 static void 289 usage() 290 { 291 (void)fprintf(stderr, "usage: %s [-m | -p] [encoded-file ...]\n", 292 getprogname()); 293 exit(1); 294 } 295