uudecode.c revision 111586
1/*- 2 * Copyright (c) 1983, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. All advertising materials mentioning features or use of this software 14 * must display the following acknowledgement: 15 * This product includes software developed by the University of 16 * California, Berkeley and its contributors. 17 * 4. 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#ifndef lint 35static const char copyright[] = 36"@(#) Copyright (c) 1983, 1993\n\ 37 The Regents of the University of California. All rights reserved.\n"; 38#endif /* not lint */ 39 40#if 0 41#ifndef lint 42static char sccsid[] = "@(#)uudecode.c 8.2 (Berkeley) 4/2/94"; 43#endif /* not lint */ 44#endif 45 46#include <sys/cdefs.h> 47__FBSDID("$FreeBSD: head/usr.bin/uudecode/uudecode.c 111586 2003-02-27 02:24:01Z fanf $"); 48 49/* 50 * uudecode [file ...] 51 * 52 * create the specified file, decoding as you go. 53 * used with uuencode. 54 */ 55#include <sys/param.h> 56#include <sys/socket.h> 57#include <sys/stat.h> 58 59#include <netinet/in.h> 60 61#include <err.h> 62#include <errno.h> 63#include <fcntl.h> 64#include <pwd.h> 65#include <resolv.h> 66#include <stdio.h> 67#include <stdlib.h> 68#include <string.h> 69#include <unistd.h> 70 71static const char *infile, *outfile; 72static FILE *infp, *outfp; 73static int cflag, iflag, oflag, pflag, sflag; 74 75static void usage(void); 76static int decode(void); 77static int decode2(void); 78static int uu_decode(void); 79static int base64_decode(void); 80 81int 82main(int argc, char *argv[]) 83{ 84 int rval, ch; 85 86 while ((ch = getopt(argc, argv, "cio:ps")) != -1) { 87 switch(ch) { 88 case 'c': 89 if (oflag) 90 usage(); 91 cflag = 1; /* multiple uudecode'd files */ 92 break; 93 case 'i': 94 iflag = 1; /* ask before override files */ 95 break; 96 case 'o': 97 if (cflag || pflag || sflag) 98 usage(); 99 oflag = 1; /* output to the specified file */ 100 sflag = 1; /* do not strip pathnames for output */ 101 outfile = optarg; /* set the output filename */ 102 break; 103 case 'p': 104 if (oflag) 105 usage(); 106 pflag = 1; /* print output to stdout */ 107 break; 108 case 's': 109 if (oflag) 110 usage(); 111 sflag = 1; /* do not strip pathnames for output */ 112 break; 113 default: 114 usage(); 115 } 116 } 117 argc -= optind; 118 argv += optind; 119 120 if (*argv) { 121 rval = 0; 122 do { 123 infp = fopen(infile = *argv, "r"); 124 if (infp == NULL) { 125 warn("%s", *argv); 126 rval = 1; 127 continue; 128 } 129 rval |= decode(); 130 fclose(infp); 131 } while (*++argv); 132 } else { 133 infile = "stdin"; 134 infp = stdin; 135 rval = decode(); 136 } 137 exit(rval); 138} 139 140static int 141decode(void) 142{ 143 int r, v; 144 145 v = decode2(); 146 if (v == EOF) { 147 warnx("%s: missing or bad \"begin\" line", infile); 148 return (1); 149 } 150 for (r = v; cflag; r |= v) { 151 v = decode2(); 152 if (v == EOF) 153 break; 154 } 155 return (r); 156} 157 158static int 159decode2(void) 160{ 161 int base64, flags, fd, mode; 162 size_t n, m; 163 char *p, *q; 164 void *handle; 165 struct passwd *pw; 166 struct stat st; 167 char buf[MAXPATHLEN+1]; 168 169 base64 = 0; 170 /* search for header line */ 171 for (;;) { 172 if (fgets(buf, sizeof(buf), infp) == NULL) 173 return (EOF); 174 p = buf; 175 if (strncmp(p, "begin-base64 ", 13) == 0) { 176 base64 = 1; 177 p += 13; 178 } else if (strncmp(p, "begin ", 6) == 0) 179 p += 6; 180 else 181 continue; 182 /* p points to mode */ 183 q = strchr(p, ' '); 184 if (q == NULL) 185 continue; 186 *q++ = '\0'; 187 /* q points to filename */ 188 n = strlen(q); 189 while (n > 0 && (q[n-1] == '\n' || q[n-1] == '\r')) 190 q[--n] = '\0'; 191 /* found valid header? */ 192 if (n > 0) 193 break; 194 } 195 196 handle = setmode(p); 197 if (handle == NULL) { 198 warnx("%s: unable to parse file mode", infile); 199 return (1); 200 } 201 mode = getmode(handle, 0) & 0666; 202 free(handle); 203 204 if (sflag) { 205 /* don't strip, so try ~user/file expansion */ 206 p = NULL; 207 pw = NULL; 208 if (*q == '~') 209 p = strchr(q, '/'); 210 if (p != NULL) { 211 *p = '\0'; 212 pw = getpwnam(q + 1); 213 *p = '/'; 214 } 215 if (pw != NULL) { 216 n = strlen(pw->pw_dir); 217 if (buf + n > p) { 218 /* make room */ 219 m = strlen(p); 220 if (sizeof(buf) < n + m) { 221 warnx("%s: bad output filename", 222 infile); 223 return (1); 224 } 225 p = memmove(buf + n, p, m); 226 } 227 q = memcpy(p - n, pw->pw_dir, n); 228 } 229 } else if (strcmp(q, "/dev/stdout") != 0) { 230 /* strip down to leaf name */ 231 p = strrchr(q, '/'); 232 if (p != NULL) 233 q = p + 1; 234 } 235 if (!oflag) 236 outfile = q; 237 238 /* POSIX says "/dev/stdout" is a 'magic cookie' not a special file. */ 239 if (pflag || strcmp(outfile, "/dev/stdout") == 0) 240 outfp = stdout; 241 else { 242 flags = O_WRONLY|O_CREAT|O_EXCL; 243 if (lstat(outfile, &st) == 0) { 244 if (iflag) { 245 warnc(EEXIST, "%s: %s", infile, outfile); 246 return (0); 247 } 248 switch (st.st_mode & S_IFMT) { 249 case S_IFREG: 250 case S_IFLNK: 251 /* avoid symlink attacks */ 252 if (unlink(outfile) == 0 || errno == ENOENT) 253 break; 254 warn("%s: unlink %s", infile, outfile); 255 return (1); 256 case S_IFDIR: 257 warnc(EISDIR, "%s: %s", infile, outfile); 258 return (1); 259 default: 260 if (oflag) { 261 /* trust command-line names */ 262 flags &= ~O_EXCL; 263 break; 264 } 265 warnc(EEXIST, "%s: %s", infile, outfile); 266 return (1); 267 } 268 } else if (errno != ENOENT) { 269 warn("%s: %s", infile, outfile); 270 return (1); 271 } 272 if ((fd = open(outfile, flags, mode)) < 0 || 273 (outfp = fdopen(fd, "w")) == NULL) { 274 warn("%s: %s", infile, outfile); 275 return (1); 276 } 277 } 278 279 if (base64) 280 return (base64_decode()); 281 else 282 return (uu_decode()); 283} 284 285static int 286uu_decode(void) 287{ 288 int i, ch; 289 char *p; 290 char buf[MAXPATHLEN+1]; 291 292 /* for each input line */ 293 for (;;) { 294 if (fgets(p = buf, sizeof(buf), infp) == NULL) { 295 warnx("%s: short file", infile); 296 return (1); 297 } 298 299#define DEC(c) (((c) - ' ') & 077) /* single character decode */ 300#define IS_DEC(c) ( (((c) - ' ') >= 0) && (((c) - ' ') <= 077 + 1) ) 301 302#define OUT_OF_RANGE do { \ 303 warnx("%s: %s: character out of range: [%d-%d]", \ 304 infile, outfile, 1 + ' ', 077 + ' ' + 1); \ 305 return (1); \ 306} while (0) 307 308 /* 309 * `i' is used to avoid writing out all the characters 310 * at the end of the file. 311 */ 312 if ((i = DEC(*p)) <= 0) 313 break; 314 for (++p; i > 0; p += 4, i -= 3) 315 if (i >= 3) { 316 if (!(IS_DEC(*p) && IS_DEC(*(p + 1)) && 317 IS_DEC(*(p + 2)) && IS_DEC(*(p + 3)))) 318 OUT_OF_RANGE; 319 320 ch = DEC(p[0]) << 2 | DEC(p[1]) >> 4; 321 putc(ch, outfp); 322 ch = DEC(p[1]) << 4 | DEC(p[2]) >> 2; 323 putc(ch, outfp); 324 ch = DEC(p[2]) << 6 | DEC(p[3]); 325 putc(ch, outfp); 326 } 327 else { 328 if (i >= 1) { 329 if (!(IS_DEC(*p) && IS_DEC(*(p + 1)))) 330 OUT_OF_RANGE; 331 ch = DEC(p[0]) << 2 | DEC(p[1]) >> 4; 332 putc(ch, outfp); 333 } 334 if (i >= 2) { 335 if (!(IS_DEC(*(p + 1)) && 336 IS_DEC(*(p + 2)))) 337 OUT_OF_RANGE; 338 339 ch = DEC(p[1]) << 4 | DEC(p[2]) >> 2; 340 putc(ch, outfp); 341 } 342 if (i >= 3) { 343 if (!(IS_DEC(*(p + 2)) && 344 IS_DEC(*(p + 3)))) 345 OUT_OF_RANGE; 346 ch = DEC(p[2]) << 6 | DEC(p[3]); 347 putc(ch, outfp); 348 } 349 } 350 } 351 if (fgets(buf, sizeof(buf), infp) == NULL || 352 (strcmp(buf, "end") && strcmp(buf, "end\n") && 353 strcmp(buf, "end\r\n"))) { 354 warnx("%s: no \"end\" line", infile); 355 return (1); 356 } 357 if (fclose(outfp) != 0) { 358 warnx("%s: %s", infile, outfile); 359 return (1); 360 } 361 return (0); 362} 363 364static int 365base64_decode(void) 366{ 367 int n; 368 char inbuf[MAXPATHLEN+1]; 369 unsigned char outbuf[MAXPATHLEN * 4]; 370 371 for (;;) { 372 if (fgets(inbuf, sizeof(inbuf), infp) == NULL) { 373 warnx("%s: short file", infile); 374 return (1); 375 } 376 if (strcmp(inbuf, "====") == 0 || 377 strcmp(inbuf, "====\n") == 0 || 378 strcmp(inbuf, "====\r\n") == 0) { 379 if (fclose(outfp) != 0) { 380 warnx("%s: %s", infile, outfile); 381 return (1); 382 } 383 return (0); 384 } 385 n = strlen(inbuf); 386 while (n > 0 && (inbuf[n-1] == '\n' || inbuf[n-1] == '\r')) 387 inbuf[--n] = '\0'; 388 n = b64_pton(inbuf, outbuf, sizeof(outbuf)); 389 if (n < 0) { 390 warnx("%s: %s: error decoding base64 input stream", infile, outfile); 391 return (1); 392 } 393 fwrite(outbuf, 1, n, outfp); 394 } 395} 396 397static void 398usage(void) 399{ 400 (void)fprintf(stderr, 401"usage: uudecode [-cips] [file ...]\n" 402" uudecode [-i] -o output_file [file]\n" 403" b64decode [-cips] [file ...]\n" 404" b64decode [-i] -o output_file [file]\n"); 405 exit(1); 406} 407