source: rtems/cpukit/libmisc/untar/untar.c @ 2de05dd

5
Last change on this file since 2de05dd was 2de05dd, checked in by Sebastian Huber <sebastian.huber@…>, on 11/21/19 at 09:27:16

untar: Make path also for symbolic links

Close #3823.

  • Property mode set to 100644
File size: 16.5 KB
Line 
1/**
2 * @file
3
4 * @brief Untar an Image
5 * @ingroup libmisc_untar_img Untar Image
6
7 * FIXME:
8 *   1. Symbolic links are not created.
9 *   2. Untar_FromMemory uses FILE *fp.
10 *   3. How to determine end of archive?
11
12 */
13
14/*
15 *  Written by: Jake Janovetz <janovetz@tempest.ece.uiuc.edu>
16 *
17 *  Copyright 2016 Chris Johns <chrisj@rtems.org>
18 *
19 *  The license and distribution terms for this file may be
20 *  found in the file LICENSE in this distribution or at
21 *  http://www.rtems.org/license/LICENSE.
22 */
23
24#ifdef HAVE_CONFIG_H
25#include "config.h"
26#endif
27
28#include <stdbool.h>
29#include <sys/param.h>
30#include <stdio.h>
31#include <string.h>
32#include <stdlib.h>
33#include <unistd.h>
34#include <errno.h>
35#include <sys/stat.h>
36#include <fcntl.h>
37#include <rtems/untar.h>
38#include <rtems/bspIo.h>
39
40/*
41 * TAR file format:
42
43 *   Offset   Length   Contents
44 *     0    100 bytes  File name ('\0' terminated, 99 maxmum length)
45 *   100      8 bytes  File mode (in octal ascii)
46 *   108      8 bytes  User ID (in octal ascii)
47 *   116      8 bytes  Group ID (in octal ascii)
48 *   124     12 bytes  File size (s) (in octal ascii)
49 *   136     12 bytes  Modify time (in octal ascii)
50 *   148      8 bytes  Header checksum (in octal ascii)
51 *   156      1 bytes  Link flag
52 *   157    100 bytes  Linkname ('\0' terminated, 99 maxmum length)
53 *   257      8 bytes  Magic PAX ("ustar\0" + 2 bytes padding)
54 *   257      8 bytes  Magic GNU tar ("ustar  \0")
55 *   265     32 bytes  User name ('\0' terminated, 31 maxmum length)
56 *   297     32 bytes  Group name ('\0' terminated, 31 maxmum length)
57 *   329      8 bytes  Major device ID (in octal ascii)
58 *   337      8 bytes  Minor device ID (in octal ascii)
59 *   345    155 bytes  Prefix
60 *   512   (s+p)bytes  File contents (s+p) := (((s) + 511) & ~511),
61 *                     round up to 512 bytes
62 *
63 *   Checksum:
64 *   int i, sum;
65 *   char* header = tar_header_pointer;
66 *   sum = 0;
67 *   for(i = 0; i < 512; i++)
68 *       sum += 0xFF & header[i];
69 */
70
71#define MAX_NAME_FIELD_SIZE      99
72
73static int _rtems_tar_header_checksum(const char *bufr);
74
75/*
76 * This converts octal ASCII number representations into an
77 * unsigned long.  Only support 32-bit numbers for now.
78 */
79static unsigned long
80_rtems_octal2ulong(
81  const char *octascii,
82  size_t len
83)
84{
85  size_t        i;
86  unsigned long num;
87
88  num = 0;
89  for (i=0; i < len; i++) {
90    if ((octascii[i] < '0') || (octascii[i] > '9')) {
91      continue;
92    }
93    num  = num * 8 + ((unsigned long)(octascii[i] - '0'));
94  }
95  return(num);
96}
97
98/*
99 * Common error message formatter.
100 */
101static void
102Print_Error(const rtems_printer *printer, const char* message, const char* path)
103{
104  rtems_printf(printer, "untar: %s: %s: (%d) %s\n",
105               message, path, errno, strerror(errno));
106}
107
108/*
109 * Get the type of node on in the file system if present.
110 */
111static int
112Stat_Node(const char* path)
113{
114  struct stat sb;
115  if (stat(path, &sb) < 0)
116    return -1;
117  if (S_ISDIR(sb.st_mode))
118    return DIRTYPE;
119  return REGTYPE;
120}
121
122/*
123 * Make the directory path for a file if it does not exist.
124 */
125static int
126Make_Path(const rtems_printer *printer, const char* filename, int linktype)
127{
128  char* copy = strdup(filename);
129  char* path = copy;
130
131  /*
132   * Skip leading path separators.
133   */
134  while (*path == '/')
135    ++path;
136
137  /*
138   * Any path left?
139   */
140  if (*path != '\0') {
141    bool  path_end = false;
142    char* end = path;
143    int   r;
144
145    /*
146     * Split the path into directory components. Check the node and if a file
147     * and not the end of the path remove it and create a directory. If a
148     * directory and not the end of the path decend into the directory.
149     */
150    while (!path_end) {
151      while (*end != '\0' && *end != '/')
152        ++end;
153
154      /*
155       * Are we at the end of the path?
156       */
157      if (*end == '\0')
158        path_end = true;
159
160      /*
161       * Split the path.
162       */
163      *end = '\0';
164
165      /*
166       * Get the node's status, exists, error, directory or regular? Regular
167       * means not a directory.
168       */
169      r = Stat_Node(path);
170
171      /*
172       * If there are errors other than not existing we are finished.
173       */
174      if (r < 0 && errno != ENOENT) {
175        Print_Error(printer, "stat", path);
176        return -1;
177      }
178
179      /*
180       * If a file remove and create a directory if not the end.
181       */
182      if (r == REGTYPE) {
183        r = unlink(path);
184        if (r < 0) {
185          Print_Error(printer, "unlink", path);
186          free(copy);
187          return -1;
188        }
189        if (!path_end) {
190          r = mkdir(path, S_IRWXU | S_IRWXG | S_IRWXO);
191          if (r < 0) {
192            Print_Error(printer, "mkdir (unlink)", path);
193            free(copy);
194            return -1;
195          }
196        }
197      }
198      else if (r < 0) {
199        /*
200         * Node does not exist which means the rest of the path will not exist.
201         */
202        while (!path_end) {
203          r = mkdir(path, S_IRWXU | S_IRWXG | S_IRWXO);
204          if (r < 0) {
205            Print_Error(printer, "mkdir", path);
206            free(copy);
207            return -1;
208          }
209          if (!path_end) {
210            *end = '/';
211            ++end;
212          }
213          while (*end != '\0' && *end != '/')
214            ++end;
215          if (*end == '\0')
216            path_end = true;
217        }
218      }
219      else if (path_end && r == DIRTYPE && linktype != DIRTYPE) {
220        /*
221         * We only handle a directory if at the end of the path and the end is
222         * a file. If we cannot remove the directory because it is not empty we
223         * raise an error. Otherwise this is a directory and we do nothing
224         * which lets us decend into it.
225         */
226        r = rmdir(path);
227        if (r < 0) {
228          Print_Error(printer, "rmdir", path);
229          free(copy);
230          return -1;
231        }
232      }
233
234      /*
235       * If not the end of the path put back the directory separator.
236       */
237      if (!path_end) {
238        *end = '/';
239        ++end;
240      }
241    }
242  }
243
244  free(copy);
245
246  return 0;
247}
248
249int
250Untar_ProcessHeader(
251  Untar_HeaderContext *ctx,
252  const char          *bufr
253)
254{
255  int            sum;
256  int            hdr_chksum;
257  int            retval = UNTAR_SUCCESSFUL;
258
259  ctx->file_name[0] = '\0';
260  ctx->file_size = 0;
261  ctx->nblocks = 0;
262  ctx->linkflag = -1;
263
264  if (strncmp(&bufr[257], "ustar", 5)) {
265    return UNTAR_SUCCESSFUL;
266  }
267
268  /*
269   * Compute the TAR checksum and check with the value in the archive.  The
270   * checksum is computed over the entire header, but the checksum field is
271   * substituted with blanks.
272   */
273  hdr_chksum = _rtems_octal2ulong(&bufr[148], 8);
274  sum        = _rtems_tar_header_checksum(bufr);
275
276  if (sum != hdr_chksum) {
277    rtems_printf(ctx->printer, "untar: file header checksum error\n");
278    return UNTAR_INVALID_CHECKSUM;
279  }
280
281  strlcpy(ctx->file_name, bufr, UNTAR_FILE_NAME_SIZE);
282
283  ctx->mode = strtoul(&bufr[100], NULL, 8);
284
285  ctx->linkflag   = bufr[156];
286  ctx->file_size = _rtems_octal2ulong(&bufr[124], 12);
287
288  /*
289   * We've decoded the header, now figure out what it contains and do something
290   * with it.
291   */
292
293  if (Make_Path(ctx->printer, ctx->file_path, ctx->linkflag) < 0) {
294    retval = UNTAR_FAIL;
295  }
296
297  if (ctx->linkflag == SYMTYPE) {
298    strlcpy(ctx->link_name, &bufr[157], sizeof(ctx->link_name));
299    rtems_printf(ctx->printer, "untar: symlink: %s -> %s\n",
300                 ctx->link_name, ctx->file_path);
301    symlink(ctx->link_name, ctx->file_path);
302  } else if (ctx->linkflag == REGTYPE) {
303    rtems_printf(ctx->printer, "untar: file: %s (s:%lu,m:%04lo)\n",
304                 ctx->file_path, ctx->file_size, ctx->mode);
305    ctx->nblocks = (((ctx->file_size) + 511) & ~511) / 512;
306  } else if (ctx->linkflag == DIRTYPE) {
307    int r;
308    rtems_printf(ctx->printer, "untar: dir: %s\n", ctx->file_path);
309    r = mkdir(ctx->file_path, S_IRWXU | S_IRWXG | S_IRWXO);
310    if (r < 0) {
311      if (errno == EEXIST) {
312        struct stat stat_buf;
313        if (stat(ctx->file_path, &stat_buf) == 0) {
314          if (S_ISDIR(stat_buf.st_mode)) {
315            r = 0;
316          } else {
317            r = unlink(ctx->file_path);
318            if (r == 0) {
319              r = mkdir(ctx->file_path, ctx->mode);
320            }
321          }
322        }
323      }
324      if (r < 0) {
325        Print_Error(ctx->printer, "mkdir", ctx->file_path);
326        retval = UNTAR_FAIL;
327      }
328    }
329  }
330
331  return retval;
332}
333
334/*
335 * Function: Untar_FromMemory
336 *
337 * Description:
338 *
339 *    This is a simple subroutine used to rip links, directories, and
340 *    files out of a block of memory.
341 *
342 *
343 * Inputs:
344 *
345 *    void *  tar_buf    - Pointer to TAR buffer.
346 *    size_t  size       - Length of TAR buffer.
347 *
348 *
349 * Output:
350 *
351 *    int - UNTAR_SUCCESSFUL (0)    on successful completion.
352 *          UNTAR_INVALID_CHECKSUM  for an invalid header checksum.
353 *          UNTAR_INVALID_HEADER    for an invalid header.
354 *
355 */
356int
357Untar_FromMemory_Print(
358  void                *tar_buf,
359  size_t               size,
360  const rtems_printer *printer
361)
362{
363  int                  fd;
364  const char          *tar_ptr = (const char *)tar_buf;
365  const char          *bufr;
366  char                 buf[UNTAR_FILE_NAME_SIZE];
367  Untar_HeaderContext  ctx;
368  int                  retval = UNTAR_SUCCESSFUL;
369  unsigned long        ptr;
370
371  ctx.file_path = buf;
372  ctx.file_name = buf;
373  ctx.printer = printer;
374  rtems_printf(printer, "untar: memory at %p (%zu)\n", tar_buf, size);
375
376  ptr = 0;
377  while (true) {
378    if (ptr + 512 > size) {
379      retval = UNTAR_SUCCESSFUL;
380      break;
381    }
382
383    /* Read the header */
384    bufr = &tar_ptr[ptr];
385    ptr += 512;
386
387    retval = Untar_ProcessHeader(&ctx, bufr);
388
389    if (retval != UNTAR_SUCCESSFUL)
390      break;
391
392    if (ctx.linkflag == REGTYPE) {
393      if ((fd = open(ctx.file_path,
394                     O_TRUNC | O_CREAT | O_WRONLY, ctx.mode)) == -1) {
395        Print_Error(printer, "open", ctx.file_path);
396        ptr += 512 * ctx.nblocks;
397      } else {
398        unsigned long sizeToGo = ctx.file_size;
399        ssize_t       len;
400        ssize_t       i;
401        ssize_t       n;
402
403        /*
404         * Read out the data.  There are nblocks of data where nblocks is the
405         * file_size rounded to the nearest 512-byte boundary.
406         */
407        for (i = 0; i < ctx.nblocks; i++) {
408          len = ((sizeToGo < 512L) ? (sizeToGo) : (512L));
409          n = write(fd, &tar_ptr[ptr], len);
410          if (n != len) {
411            Print_Error(printer, "write", ctx.file_path);
412            retval  = UNTAR_FAIL;
413            break;
414          }
415          ptr += 512;
416          sizeToGo -= n;
417        }
418        close(fd);
419      }
420
421    }
422  }
423
424  return retval;
425}
426
427/*
428 * Function: Untar_FromMemory
429 *
430 * Description:
431 *
432 *    This is a simple subroutine used to rip links, directories, and
433 *    files out of a block of memory.
434 *
435 *
436 * Inputs:
437 *
438 *    void *  tar_buf    - Pointer to TAR buffer.
439 *    size_t  size       - Length of TAR buffer.
440 *
441 *
442 * Output:
443 *
444 *    int - UNTAR_SUCCESSFUL (0)    on successful completion.
445 *          UNTAR_INVALID_CHECKSUM  for an invalid header checksum.
446 *          UNTAR_INVALID_HEADER    for an invalid header.
447 *
448 */
449int
450Untar_FromMemory(
451  void   *tar_buf,
452  size_t  size
453)
454{
455  return Untar_FromMemory_Print(tar_buf, size, false);
456}
457
458/*
459 * Function: Untar_FromFile
460 *
461 * Description:
462 *
463 *    This is a simple subroutine used to rip links, directories, and
464 *    files out of a TAR file.
465 *
466 * Inputs:
467 *
468 *    const char *tar_name   - TAR filename.
469 *
470 * Output:
471 *
472 *    int - UNTAR_SUCCESSFUL (0)    on successful completion.
473 *          UNTAR_INVALID_CHECKSUM  for an invalid header checksum.
474 *          UNTAR_INVALID_HEADER    for an invalid header.
475 */
476int
477Untar_FromFile_Print(
478  const char          *tar_name,
479  const rtems_printer *printer
480)
481{
482  int                  fd;
483  char                *bufr;
484  ssize_t              n;
485  int                  retval;
486  unsigned long        i;
487  char                 buf[UNTAR_FILE_NAME_SIZE];
488  Untar_HeaderContext  ctx;
489
490  retval = UNTAR_SUCCESSFUL;
491
492  if ((fd = open(tar_name, O_RDONLY)) < 0) {
493    return UNTAR_FAIL;
494  }
495
496  bufr = (char *)malloc(512);
497  if (bufr == NULL) {
498    close(fd);
499    return(UNTAR_FAIL);
500  }
501
502  ctx.file_path = buf;
503  ctx.file_name = buf;
504  ctx.printer = printer;
505
506  while (1) {
507    /* Read the header */
508    /* If the header read fails, we just consider it the end of the tarfile. */
509    if ((n = read(fd, bufr, 512)) != 512) {
510      break;
511    }
512
513    retval = Untar_ProcessHeader(&ctx, bufr);
514
515    if (retval != UNTAR_SUCCESSFUL)
516      break;
517
518    if (ctx.linkflag == REGTYPE) {
519      int out_fd;
520
521      /*
522       * Read out the data.  There are nblocks of data where nblocks
523       * is the size rounded to the nearest 512-byte boundary.
524       */
525
526      if ((out_fd = creat(ctx.file_path, ctx.mode)) == -1) {
527        (void) lseek(fd, SEEK_CUR, 512UL * ctx.nblocks);
528      } else {
529        for (i = 0; i < ctx.nblocks; i++) {
530          n = read(fd, bufr, 512);
531          n = MIN(n, ctx.file_size - (i * 512UL));
532          (void) write(out_fd, bufr, n);
533        }
534        close(out_fd);
535      }
536    }
537  }
538
539  free(bufr);
540  close(fd);
541
542  return retval;
543}
544
545
546void Untar_ChunkContext_Init(Untar_ChunkContext *context)
547{
548  context->base.file_path = context->buf;
549  context->base.file_name = context->buf;
550  context->state = UNTAR_CHUNK_HEADER;
551  context->done_bytes = 0;
552  context->out_fd = -1;
553}
554
555int Untar_FromChunk_Print(
556  Untar_ChunkContext *context,
557  void *chunk,
558  size_t chunk_size,
559  const rtems_printer* printer
560)
561{
562  char *buf;
563  size_t done;
564  size_t todo;
565  size_t remaining;
566  size_t consume;
567  int retval;
568
569  buf = chunk;
570  done = 0;
571  todo = chunk_size;
572
573  context->base.printer = printer;
574
575  while (todo > 0) {
576    switch (context->state) {
577      case UNTAR_CHUNK_HEADER:
578        remaining = 512 - context->done_bytes;
579        consume = MIN(remaining, todo);
580        memcpy(&context->header[context->done_bytes], &buf[done], consume);
581        context->done_bytes += consume;
582
583        if (context->done_bytes == 512) {
584          retval = Untar_ProcessHeader(
585            &context->base,
586            &context->header[0]
587          );
588
589          if (retval != UNTAR_SUCCESSFUL) {
590            context->state = UNTAR_CHUNK_ERROR;
591            return retval;
592          }
593
594          if (context->base.linkflag == REGTYPE) {
595            context->out_fd = creat(context->base.file_path,
596                                    context->base.mode);
597
598            if (context->out_fd >= 0) {
599              context->state = UNTAR_CHUNK_WRITE;
600              context->done_bytes = 0;
601            } else {
602              context->state = UNTAR_CHUNK_SKIP;
603              context->base.file_size = 512 * context->base.nblocks;
604              context->done_bytes = 0;
605            }
606          } else {
607              context->done_bytes = 0;
608          }
609        }
610
611        break;
612      case UNTAR_CHUNK_SKIP:
613        remaining = context->base.file_size - context->done_bytes;
614        consume = MIN(remaining, todo);
615        context->done_bytes += consume;
616
617        if (context->done_bytes == context->base.file_size) {
618          context->state = UNTAR_CHUNK_HEADER;
619          context->done_bytes = 0;
620        }
621
622        break;
623      case UNTAR_CHUNK_WRITE:
624        remaining = context->base.file_size - context->done_bytes;
625        consume = MIN(remaining, todo);
626        write(context->out_fd, &buf[done], consume);
627        context->done_bytes += consume;
628
629        if (context->done_bytes == context->base.file_size) {
630          close(context->out_fd);
631          context->out_fd = -1;
632          context->state = UNTAR_CHUNK_SKIP;
633          context->base.file_size = 512 * context->base.nblocks
634            - context->base.file_size;
635          context->done_bytes = 0;
636        }
637
638        break;
639      default:
640        return UNTAR_FAIL;
641    }
642
643    done += consume;
644    todo -= consume;
645  }
646
647  return UNTAR_SUCCESSFUL;
648}
649
650/*
651 * Function: Untar_FromFile
652 *
653 * Description:
654 *
655 *    This is a simple subroutine used to rip links, directories, and
656 *    files out of a TAR file.
657 *
658 * Inputs:
659 *
660 *    const char *tar_name   - TAR filename.
661 *
662 * Output:
663 *
664 *    int - UNTAR_SUCCESSFUL (0)    on successful completion.
665 *          UNTAR_INVALID_CHECKSUM  for an invalid header checksum.
666 *          UNTAR_INVALID_HEADER    for an invalid header.
667 */
668int
669Untar_FromFile(
670  const char *tar_name
671)
672{
673  return Untar_FromFile_Print(tar_name, NULL);
674}
675
676/*
677 * Compute the TAR checksum and check with the value in
678 * the archive.  The checksum is computed over the entire
679 * header, but the checksum field is substituted with blanks.
680 */
681static int
682_rtems_tar_header_checksum(
683  const char *bufr
684)
685{
686  int  i, sum;
687
688  sum = 0;
689  for (i=0; i<512; i++) {
690    if ((i >= 148) && (i < 156))
691      sum += 0xff & ' ';
692    else
693     sum += 0xff & bufr[i];
694  }
695  return(sum);
696}
Note: See TracBrowser for help on using the repository browser.