head 1.2; access; symbols pkgsrc-2013Q2:1.2.0.2 pkgsrc-2013Q2-base:1.2 pkgsrc-2013Q1:1.1.0.64 pkgsrc-2013Q1-base:1.1 pkgsrc-2012Q4:1.1.0.62 pkgsrc-2012Q4-base:1.1 pkgsrc-2012Q3:1.1.0.60 pkgsrc-2012Q3-base:1.1 pkgsrc-2012Q2:1.1.0.58 pkgsrc-2012Q2-base:1.1 pkgsrc-2012Q1:1.1.0.56 pkgsrc-2012Q1-base:1.1 pkgsrc-2011Q4:1.1.0.54 pkgsrc-2011Q4-base:1.1 pkgsrc-2011Q3:1.1.0.52 pkgsrc-2011Q3-base:1.1 pkgsrc-2011Q2:1.1.0.50 pkgsrc-2011Q2-base:1.1 pkgsrc-2011Q1:1.1.0.48 pkgsrc-2011Q1-base:1.1 pkgsrc-2010Q4:1.1.0.46 pkgsrc-2010Q4-base:1.1 pkgsrc-2010Q3:1.1.0.44 pkgsrc-2010Q3-base:1.1 pkgsrc-2010Q2:1.1.0.42 pkgsrc-2010Q2-base:1.1 pkgsrc-2010Q1:1.1.0.40 pkgsrc-2010Q1-base:1.1 pkgsrc-2009Q4:1.1.0.38 pkgsrc-2009Q4-base:1.1 pkgsrc-2009Q3:1.1.0.36 pkgsrc-2009Q3-base:1.1 pkgsrc-2009Q2:1.1.0.34 pkgsrc-2009Q2-base:1.1 pkgsrc-2009Q1:1.1.0.32 pkgsrc-2009Q1-base:1.1 pkgsrc-2008Q4:1.1.0.30 pkgsrc-2008Q4-base:1.1 pkgsrc-2008Q3:1.1.0.28 pkgsrc-2008Q3-base:1.1 cube-native-xorg:1.1.0.26 cube-native-xorg-base:1.1 pkgsrc-2008Q2:1.1.0.24 pkgsrc-2008Q2-base:1.1 cwrapper:1.1.0.22 pkgsrc-2008Q1:1.1.0.20 pkgsrc-2008Q1-base:1.1 pkgsrc-2007Q4:1.1.0.18 pkgsrc-2007Q4-base:1.1 pkgsrc-2007Q3:1.1.0.16 pkgsrc-2007Q3-base:1.1 pkgsrc-2007Q2:1.1.0.14 pkgsrc-2007Q2-base:1.1 pkgsrc-2007Q1:1.1.0.12 pkgsrc-2007Q1-base:1.1 pkgsrc-2006Q4:1.1.0.10 pkgsrc-2006Q4-base:1.1 pkgsrc-2006Q3:1.1.0.8 pkgsrc-2006Q3-base:1.1 pkgsrc-2006Q2:1.1.0.6 pkgsrc-2006Q2-base:1.1 pkgsrc-2006Q1:1.1.0.4 pkgsrc-2006Q1-base:1.1 pkgsrc-2005Q4:1.1.0.2; locks; strict; comment @# @; 1.2 date 2013.06.09.14.35.12; author ryoon; state dead; branches; next 1.1; commitid udOnIwGPtyKJSWSw; 1.1 date 2006.02.12.01.44.28; author seb; state Exp; branches 1.1.2.1; next ; 1.1.2.1 date 2006.02.12.01.44.28; author salo; state dead; branches; next 1.1.2.2; 1.1.2.2 date 2006.02.12.02.19.20; author salo; state Exp; branches; next ; desc @@ 1.2 log @Update to 2.11 * Update LICENSE to gnu-gpl-v3 Changelog: 2.11: Fix mt build. In copy-in mode, if directory attributes do not permit writing to it, setting them is delayed until the end of run. This allows to correctly extract files in such directories. In copy-in mode, permissions of a directory are restored if it appears in the file list after files in it (e.g. in listings produced by find . -depth). This fixes debian bug #458079. Fix possible memory overflow in the rmt client code (CVE-2010-0624). 2.10: Ensure record headers are properly packed (fix builds on ARM). Fix exit codes to reliably indicate success or failure of the operation. Fix large file support. Support MinGW builds. Minor bugfixes. 2.9: Licensed under the GPLv3. Bugfixes Honor umask when creating intermediate directories, not specified in the archive (debian bug #430053). 2.8: Option --owner can be used in copy-out mode, allowing to uniformly override the ownership of the files being added to the archive. Bugfixes: Symlinks were handled incorrectly in copy-out mode. Fix handling of large files. Fix setting the file permissions in copy-out mode. Fix CAN-2005-1111 2.7: Improved error checking and diagnostics Fixed CAN-1999-1572 Allow to use --sparse in both copy-in and copy-pass. Fix bug that eventually caused copying out the same hard-linked file several times to archive. Fix several LFS-related issues. Fix Debian bug #335580. @ text @$NetBSD: patch-ap,v 1.1 2006/02/12 01:44:28 seb Exp $ --- src/copyout.c.orig 2004-10-14 09:14:03.000000000 +0000 +++ src/copyout.c @@@@ -159,7 +159,7 @@@@ add_link_defer (struct new_cpio_header * } /* We are about to put a file into a newc or crc archive that is - multiply linked. We have already seen and defered all of the + multiply linked. We have already seen and deferred all of the other links to the file but haven't written them into the archive. Write the other links into the archive, and remove them from the deferouts list. */ @@@@ -231,8 +231,10 @@@@ writeout_defered_file (struct new_cpio_h file_hdr.c_filesize, header->c_name); - write_out_header (&file_hdr, out_file_des); - copy_files_disk_to_tape (in_file_des, out_file_des, file_hdr.c_filesize, header->c_name); + if (write_out_header (&file_hdr, out_file_des)) + return; + copy_files_disk_to_tape (in_file_des, out_file_des, file_hdr.c_filesize, + header->c_name); warn_if_file_changed(header->c_name, file_hdr.c_filesize, file_hdr.c_mtime); if (archive_format == arf_tar || archive_format == arf_ustar) @@@@ -288,153 +290,313 @@@@ writeout_final_defers (int out_des) } } - -/* Write out header FILE_HDR, including the file name, to file - descriptor OUT_DES. */ +/* FIXME: These two defines should be defined in paxutils */ +#define LG_8 3 +#define LG_16 4 + +/* FIXME: to_ascii could be used instead of to_oct() and to_octal() from tar, + so it should be moved to paxutils too. + Allowed values for logbase are: 1 (binary), 2, 3 (octal), 4 (hex) */ +int +to_ascii (char *where, uintmax_t v, size_t digits, unsigned logbase) +{ + static char codetab[] = "0123456789ABCDEF"; + int i = digits; + + do + { + where[--i] = codetab[(v & ((1 << logbase) - 1))]; + v >>= logbase; + } + while (i); + + return v != 0; +} + +static void +field_width_error (const char *filename, const char *fieldname) +{ + error (0, 0, _("%s: field width not sufficient for storing %s"), + filename, fieldname); +} + +static void +field_width_warning (const char *filename, const char *fieldname) +{ + if (warn_option & CPIO_WARN_TRUNCATE) + error (0, 0, _("%s: truncating %s"), filename, fieldname); +} void -write_out_header (struct new_cpio_header *file_hdr, int out_des) +to_ascii_or_warn (char *where, uintmax_t n, size_t digits, + unsigned logbase, + const char *filename, const char *fieldname) { - if (archive_format == arf_newascii || archive_format == arf_crcascii) + if (to_ascii (where, n, digits, logbase)) + field_width_warning (filename, fieldname); +} + +int +to_ascii_or_error (char *where, uintmax_t n, size_t digits, + unsigned logbase, + const char *filename, const char *fieldname) +{ + if (to_ascii (where, n, digits, logbase)) { - char ascii_header[112]; - char *magic_string; + field_width_error (filename, fieldname); + return 1; + } + return 0; +} - if (archive_format == arf_crcascii) - magic_string = "070702"; - else - magic_string = "070701"; - sprintf (ascii_header, - "%6s%08lx%08lx%08lx%08lx%08lx%08lx%08lx%08lx%08lx%08lx%08lx%08lx%08lx", - magic_string, - file_hdr->c_ino, file_hdr->c_mode, file_hdr->c_uid, - file_hdr->c_gid, file_hdr->c_nlink, file_hdr->c_mtime, - file_hdr->c_filesize, file_hdr->c_dev_maj, file_hdr->c_dev_min, - file_hdr->c_rdev_maj, file_hdr->c_rdev_min, file_hdr->c_namesize, - file_hdr->c_chksum); - tape_buffered_write (ascii_header, out_des, 110L); - - /* Write file name to output. */ - tape_buffered_write (file_hdr->c_name, out_des, (long) file_hdr->c_namesize); - tape_pad_output (out_des, file_hdr->c_namesize + 110); - } - else if (archive_format == arf_oldascii || archive_format == arf_hpoldascii) - { - char ascii_header[78]; - dev_t dev; - dev_t rdev; +int +write_out_new_ascii_header (const char *magic_string, + struct new_cpio_header *file_hdr, int out_des) +{ + char ascii_header[110]; + char *p; - if (archive_format == arf_oldascii) - { - dev = makedev (file_hdr->c_dev_maj, file_hdr->c_dev_min); - rdev = makedev (file_hdr->c_rdev_maj, file_hdr->c_rdev_min); - } - else - { - /* HP/UX cpio creates archives that look just like ordinary archives, - but for devices it sets major = 0, minor = 1, and puts the - actual major/minor number in the filesize field. */ - switch (file_hdr->c_mode & CP_IFMT) - { - case CP_IFCHR: - case CP_IFBLK: + (void)strncpy(ascii_header, magic_string, sizeof(ascii_header) - 1); + ascii_header[sizeof(ascii_header) -1] = '\0'; + p = ascii_header + strlen(ascii_header); + to_ascii_or_warn (p, file_hdr->c_ino, 8, LG_16, + file_hdr->c_name, _("inode number")); + p += 8; + to_ascii_or_warn (p, file_hdr->c_mode, 8, LG_16, file_hdr->c_name, + _("file mode")); + p += 8; + to_ascii_or_warn (p, file_hdr->c_uid, 8, LG_16, file_hdr->c_name, + _("uid")); + p += 8; + to_ascii_or_warn (p, file_hdr->c_gid, 8, LG_16, file_hdr->c_name, + _("gid")); + p += 8; + to_ascii_or_warn (p, file_hdr->c_nlink, 8, LG_16, file_hdr->c_name, + _("number of links")); + p += 8; + to_ascii_or_warn (p, file_hdr->c_mtime, 8, LG_16, file_hdr->c_name, + _("modification time")); + p += 8; + if (to_ascii_or_error (p, file_hdr->c_filesize, 8, LG_16, file_hdr->c_name, + _("file size"))) + return 1; + p += 8; + if (to_ascii_or_error (p, file_hdr->c_dev_maj, 8, LG_16, file_hdr->c_name, + _("device major number"))) + return 1; + p += 8; + if (to_ascii_or_error (p, file_hdr->c_dev_min, 8, LG_16, file_hdr->c_name, + _("device minor number"))) + return 1; + p += 8; + if (to_ascii_or_error (p, file_hdr->c_rdev_maj, 8, LG_16, file_hdr->c_name, + _("rdev major"))) + return 1; + p += 8; + if (to_ascii_or_error (p, file_hdr->c_rdev_min, 8, LG_16, file_hdr->c_name, + _("rdev minor"))) + return 1; + p += 8; + if (to_ascii_or_error (p, file_hdr->c_namesize, 8, LG_16, file_hdr->c_name, + _("name size"))) + return 1; + p += 8; + to_ascii (p, file_hdr->c_chksum & 0xffffffff, 8, LG_16); + + tape_buffered_write (ascii_header, out_des, sizeof ascii_header); + + /* Write file name to output. */ + tape_buffered_write (file_hdr->c_name, out_des, (long) file_hdr->c_namesize); + tape_pad_output (out_des, file_hdr->c_namesize + sizeof ascii_header); + return 0; +} + +int +write_out_old_ascii_header (dev_t dev, dev_t rdev, + struct new_cpio_header *file_hdr, int out_des) +{ + char ascii_header[76]; + char *p = ascii_header; + + to_ascii (p, file_hdr->c_magic, 6, LG_8); + p += 6; + to_ascii_or_warn (p, dev, 6, LG_8, file_hdr->c_name, _("device number")); + p += 6; + to_ascii_or_warn (p, file_hdr->c_ino, 6, LG_8, file_hdr->c_name, + _("inode number")); + p += 6; + to_ascii_or_warn (p, file_hdr->c_mode, 6, LG_8, file_hdr->c_name, + _("file mode")); + p += 6; + to_ascii_or_warn (p, file_hdr->c_uid, 6, LG_8, file_hdr->c_name, _("uid")); + p += 6; + to_ascii_or_warn (p, file_hdr->c_gid, 6, LG_8, file_hdr->c_name, _("gid")); + p += 6; + to_ascii_or_warn (p, file_hdr->c_nlink, 6, LG_8, file_hdr->c_name, + _("number of links")); + p += 6; + to_ascii_or_warn (p, rdev, 6, LG_8, file_hdr->c_name, _("rdev")); + p += 6; + to_ascii_or_warn (p, file_hdr->c_mtime, 11, LG_8, file_hdr->c_name, + _("modification time")); + p += 11; + if (to_ascii_or_error (p, file_hdr->c_namesize, 6, LG_8, file_hdr->c_name, + _("name size"))) + return 1; + p += 6; + if (to_ascii_or_error (p, file_hdr->c_filesize, 11, LG_8, file_hdr->c_name, + _("file size"))) + return 1; + + tape_buffered_write (ascii_header, out_des, sizeof ascii_header); + + /* Write file name to output. */ + tape_buffered_write (file_hdr->c_name, out_des, file_hdr->c_namesize); + return 0; +} + +void +hp_compute_dev (struct new_cpio_header *file_hdr, dev_t *pdev, dev_t *prdev) +{ + /* HP/UX cpio creates archives that look just like ordinary archives, + but for devices it sets major = 0, minor = 1, and puts the + actual major/minor number in the filesize field. */ + switch (file_hdr->c_mode & CP_IFMT) + { + case CP_IFCHR: + case CP_IFBLK: #ifdef CP_IFSOCK - case CP_IFSOCK: + case CP_IFSOCK: #endif #ifdef CP_IFIFO - case CP_IFIFO: + case CP_IFIFO: #endif - file_hdr->c_filesize = makedev (file_hdr->c_rdev_maj, - file_hdr->c_rdev_min); - rdev = 1; - break; - default: - dev = makedev (file_hdr->c_dev_maj, file_hdr->c_dev_min); - rdev = makedev (file_hdr->c_rdev_maj, file_hdr->c_rdev_min); - break; - } - } + file_hdr->c_filesize = makedev (file_hdr->c_rdev_maj, + file_hdr->c_rdev_min); + *pdev = *prdev = makedev (0, 1); + break; + + default: + *pdev = makedev (file_hdr->c_dev_maj, file_hdr->c_dev_min); + *prdev = makedev (file_hdr->c_rdev_maj, file_hdr->c_rdev_min); + break; + } +} - if ((warn_option & CPIO_WARN_TRUNCATE) && (file_hdr->c_ino >> 16) != 0) - error (0, 0, _("%s: truncating inode number"), file_hdr->c_name); +int +write_out_binary_header (dev_t rdev, + struct new_cpio_header *file_hdr, int out_des) +{ + struct old_cpio_header short_hdr; - /* Debian hack: The type of dev_t has changed in glibc. Fixed output - to ensure that a long int is passed to sprintf. This has been - reported to "bug-gnu-utils@@prep.ai.mit.edu". (1998/5/26) -BEM */ - sprintf (ascii_header, - "%06ho%06lo%06lo%06lo%06lo%06lo%06lo%06lo%011lo%06lo%011lo", - file_hdr->c_magic & 0xFFFF, (long) dev & 0xFFFF, - file_hdr->c_ino & 0xFFFF, file_hdr->c_mode & 0xFFFF, - file_hdr->c_uid & 0xFFFF, file_hdr->c_gid & 0xFFFF, - file_hdr->c_nlink & 0xFFFF, (long) rdev & 0xFFFF, - file_hdr->c_mtime, file_hdr->c_namesize & 0xFFFF, - file_hdr->c_filesize); - tape_buffered_write (ascii_header, out_des, 76L); + short_hdr.c_magic = 070707; + short_hdr.c_dev = makedev (file_hdr->c_dev_maj, file_hdr->c_dev_min); - /* Write file name to output. */ - tape_buffered_write (file_hdr->c_name, out_des, (long) file_hdr->c_namesize); - } - else if (archive_format == arf_tar || archive_format == arf_ustar) - { - write_out_tar_header (file_hdr, out_des); - } - else - { - struct old_cpio_header short_hdr; + if ((warn_option & CPIO_WARN_TRUNCATE) && (file_hdr->c_ino >> 16) != 0) + error (0, 0, _("%s: truncating inode number"), file_hdr->c_name); - short_hdr.c_magic = 070707; - short_hdr.c_dev = makedev (file_hdr->c_dev_maj, file_hdr->c_dev_min); + short_hdr.c_ino = file_hdr->c_ino & 0xFFFF; + if (short_hdr.c_ino != file_hdr->c_ino) + field_width_warning (file_hdr->c_name, _("inode number")); + + short_hdr.c_mode = file_hdr->c_mode & 0xFFFF; + if (short_hdr.c_mode != file_hdr->c_mode) + field_width_warning (file_hdr->c_name, _("file mode")); + + short_hdr.c_uid = file_hdr->c_uid & 0xFFFF; + if (short_hdr.c_uid != file_hdr->c_uid) + field_width_warning (file_hdr->c_name, _("uid")); + + short_hdr.c_gid = file_hdr->c_gid & 0xFFFF; + if (short_hdr.c_gid != file_hdr->c_gid) + field_width_warning (file_hdr->c_name, _("gid")); + + short_hdr.c_nlink = file_hdr->c_nlink & 0xFFFF; + if (short_hdr.c_nlink != file_hdr->c_nlink) + field_width_warning (file_hdr->c_name, _("number of links")); + + short_hdr.c_rdev = rdev; + short_hdr.c_mtimes[0] = file_hdr->c_mtime >> 16; + short_hdr.c_mtimes[1] = file_hdr->c_mtime & 0xFFFF; + + short_hdr.c_namesize = file_hdr->c_namesize & 0xFFFF; + if (short_hdr.c_namesize != file_hdr->c_namesize) + { + field_width_error (file_hdr->c_name, _("name size")); + return 1; + } + + short_hdr.c_filesize = file_hdr->c_filesize; + if (short_hdr.c_filesize != file_hdr->c_filesize) + { + field_width_error (file_hdr->c_name, _("file size")); + return 1; + } + + short_hdr.c_filesizes[0] = file_hdr->c_filesize >> 16; + short_hdr.c_filesizes[1] = file_hdr->c_filesize & 0xFFFF; - if ((warn_option & CPIO_WARN_TRUNCATE) && (file_hdr->c_ino >> 16) != 0) - error (0, 0, _("%s: truncating inode number"), file_hdr->c_name); + /* Output the file header. */ + tape_buffered_write ((char *) &short_hdr, out_des, 26); - short_hdr.c_ino = file_hdr->c_ino & 0xFFFF; - short_hdr.c_mode = file_hdr->c_mode & 0xFFFF; - short_hdr.c_uid = file_hdr->c_uid & 0xFFFF; - short_hdr.c_gid = file_hdr->c_gid & 0xFFFF; - short_hdr.c_nlink = file_hdr->c_nlink & 0xFFFF; - if (archive_format != arf_hpbinary) - short_hdr.c_rdev = makedev (file_hdr->c_rdev_maj, file_hdr->c_rdev_min); - else - { - switch (file_hdr->c_mode & CP_IFMT) - { - /* HP/UX cpio creates archives that look just like ordinary - archives, but for devices it sets major = 0, minor = 1, and - puts the actual major/minor number in the filesize field. */ - case CP_IFCHR: - case CP_IFBLK: -#ifdef CP_IFSOCK - case CP_IFSOCK: -#endif -#ifdef CP_IFIFO - case CP_IFIFO: -#endif - file_hdr->c_filesize = makedev (file_hdr->c_rdev_maj, - file_hdr->c_rdev_min); - short_hdr.c_rdev = makedev (0, 1); - break; - default: - short_hdr.c_rdev = makedev (file_hdr->c_rdev_maj, - file_hdr->c_rdev_min); - break; - } - } - short_hdr.c_mtimes[0] = file_hdr->c_mtime >> 16; - short_hdr.c_mtimes[1] = file_hdr->c_mtime & 0xFFFF; + /* Write file name to output. */ + tape_buffered_write (file_hdr->c_name, out_des, file_hdr->c_namesize); - short_hdr.c_namesize = file_hdr->c_namesize & 0xFFFF; + tape_pad_output (out_des, file_hdr->c_namesize + 26); + return 0; +} - short_hdr.c_filesizes[0] = file_hdr->c_filesize >> 16; - short_hdr.c_filesizes[1] = file_hdr->c_filesize & 0xFFFF; + +/* Write out header FILE_HDR, including the file name, to file + descriptor OUT_DES. */ - /* Output the file header. */ - tape_buffered_write ((char *) &short_hdr, out_des, 26L); +int +write_out_header (struct new_cpio_header *file_hdr, int out_des) +{ + dev_t dev; + dev_t rdev; + + switch (archive_format) + { + case arf_newascii: + return write_out_new_ascii_header ("070701", file_hdr, out_des); + + case arf_crcascii: + return write_out_new_ascii_header ("070702", file_hdr, out_des); + + case arf_oldascii: + return write_out_old_ascii_header (makedev (file_hdr->c_dev_maj, + file_hdr->c_dev_min), + makedev (file_hdr->c_rdev_maj, + file_hdr->c_rdev_min), + file_hdr, out_des); + + case arf_hpoldascii: + hp_compute_dev (file_hdr, &dev, &rdev); + return write_out_old_ascii_header (dev, rdev, file_hdr, out_des); + + case arf_tar: + case arf_ustar: + if (is_tar_filename_too_long (file_hdr->c_name)) + { + error (0, 0, _("%s: file name too long"), file_hdr->c_name); + return 1; + } + write_out_tar_header (file_hdr, out_des); /* FIXME: No error checking */ + return 0; - /* Write file name to output. */ - tape_buffered_write (file_hdr->c_name, out_des, (long) file_hdr->c_namesize); + case arf_binary: + return write_out_binary_header (makedev (file_hdr->c_rdev_maj, + file_hdr->c_rdev_min), + file_hdr, out_des); + + case arf_hpbinary: + hp_compute_dev (file_hdr, &dev, &rdev); + /* FIXME: dev ignored. Should it be? */ + return write_out_binary_header (rdev, file_hdr, out_des); - tape_pad_output (out_des, file_hdr->c_namesize + 26); + default: + abort (); } } @@@@ -593,14 +755,7 @@@@ process_copy_out () file_hdr.c_namesize = strlen (p) + 1; } #endif - if ((archive_format == arf_tar || archive_format == arf_ustar) - && is_tar_filename_too_long (file_hdr.c_name)) - { - error (0, 0, _("%s: file name too long"), - file_hdr.c_name); - continue; - } - + /* Copy the named file to the output. */ switch (file_hdr.c_mode & CP_IFMT) { @@@@ -613,8 +768,8 @@@@ process_copy_out () file_hdr.c_dev_min))) { file_hdr.c_tar_linkname = otherfile; - write_out_header (&file_hdr, out_file_des); - break; + if (write_out_header (&file_hdr, out_file_des)) + continue; } } if ( (archive_format == arf_newascii || archive_format == arf_crcascii) @@@@ -643,7 +798,8 @@@@ process_copy_out () file_hdr.c_filesize, input_name.ds_string); - write_out_header (&file_hdr, out_file_des); + if (write_out_header (&file_hdr, out_file_des)) + continue; copy_files_disk_to_tape (in_file_des, out_file_des, file_hdr.c_filesize, input_name.ds_string); warn_if_file_changed(input_name.ds_string, file_hdr.c_filesize, file_hdr.c_mtime); @@@@ -673,7 +829,8 @@@@ process_copy_out () case CP_IFDIR: file_hdr.c_filesize = 0; - write_out_header (&file_hdr, out_file_des); + if (write_out_header (&file_hdr, out_file_des)) + continue; break; case CP_IFCHR: @@@@ -702,14 +859,16 @@@@ process_copy_out () file_hdr.c_mode = (file_stat.st_mode & 07777); file_hdr.c_mode |= CP_IFREG; file_hdr.c_tar_linkname = otherfile; - write_out_header (&file_hdr, out_file_des); + if (write_out_header (&file_hdr, out_file_des)) + continue; break; } add_inode (file_hdr.c_ino, file_hdr.c_name, file_hdr.c_dev_maj, file_hdr.c_dev_min); } file_hdr.c_filesize = 0; - write_out_header (&file_hdr, out_file_des); + if (write_out_header (&file_hdr, out_file_des)) + continue; break; #ifdef CP_IFLNK @@@@ -738,12 +897,14 @@@@ process_copy_out () { link_name[link_size] = '\0'; file_hdr.c_tar_linkname = link_name; - write_out_header (&file_hdr, out_file_des); + if (write_out_header (&file_hdr, out_file_des)) + continue; } } else { - write_out_header (&file_hdr, out_file_des); + if (write_out_header (&file_hdr, out_file_des)) + continue; tape_buffered_write (link_name, out_file_des, link_size); tape_pad_output (out_file_des, link_size); } @ 1.1 log @Security fix for http://secunia.com/advisories/18251/ (CVE-2005-4268) adapted from patch attached in redhat bugzilla https://bugzilla.redhat.com/bugzilla/show_bug.cgi?id=172669 While here add test target support. Bump PKGREVISION to 2. @ text @d1 1 a1 1 $NetBSD$ @ 1.1.2.1 log @file patch-ap was added on branch pkgsrc-2005Q4 on 2006-02-12 01:44:28 +0000 @ text @d1 549 @ 1.1.2.2 log @Pullup ticket 1119 - requested by Stoned Elipot security fix for gcpio Revisions pulled up: - pkgsrc/archivers/gcpio/Makefile 1.29 - pkgsrc/archivers/gcpio/distinfo 1.10 - pkgsrc/archivers/gcpio/patches/patch-ak 1.2 - pkgsrc/archivers/gcpio/patches/patch-ap 1.1 Module Name: pkgsrc Committed By: seb Date: Sun Feb 12 01:44:28 UTC 2006 Modified Files: pkgsrc/archivers/gcpio: Makefile distinfo pkgsrc/archivers/gcpio/patches: patch-ak Added Files: pkgsrc/archivers/gcpio/patches: patch-ap Log Message: Security fix for http://secunia.com/advisories/18251/ (CVE-2005-4268) adapted from patch attached in redhat bugzilla https://bugzilla.redhat.com/bugzilla/show_bug.cgi?id=172669 While here add test target support. Bump PKGREVISION to 2. @ text @a0 549 $NetBSD: patch-ap,v 1.1.2.1 2006/02/12 02:19:20 salo Exp $ --- src/copyout.c.orig 2004-10-14 09:14:03.000000000 +0000 +++ src/copyout.c @@@@ -159,7 +159,7 @@@@ add_link_defer (struct new_cpio_header * } /* We are about to put a file into a newc or crc archive that is - multiply linked. We have already seen and defered all of the + multiply linked. We have already seen and deferred all of the other links to the file but haven't written them into the archive. Write the other links into the archive, and remove them from the deferouts list. */ @@@@ -231,8 +231,10 @@@@ writeout_defered_file (struct new_cpio_h file_hdr.c_filesize, header->c_name); - write_out_header (&file_hdr, out_file_des); - copy_files_disk_to_tape (in_file_des, out_file_des, file_hdr.c_filesize, header->c_name); + if (write_out_header (&file_hdr, out_file_des)) + return; + copy_files_disk_to_tape (in_file_des, out_file_des, file_hdr.c_filesize, + header->c_name); warn_if_file_changed(header->c_name, file_hdr.c_filesize, file_hdr.c_mtime); if (archive_format == arf_tar || archive_format == arf_ustar) @@@@ -288,153 +290,313 @@@@ writeout_final_defers (int out_des) } } - -/* Write out header FILE_HDR, including the file name, to file - descriptor OUT_DES. */ +/* FIXME: These two defines should be defined in paxutils */ +#define LG_8 3 +#define LG_16 4 + +/* FIXME: to_ascii could be used instead of to_oct() and to_octal() from tar, + so it should be moved to paxutils too. + Allowed values for logbase are: 1 (binary), 2, 3 (octal), 4 (hex) */ +int +to_ascii (char *where, uintmax_t v, size_t digits, unsigned logbase) +{ + static char codetab[] = "0123456789ABCDEF"; + int i = digits; + + do + { + where[--i] = codetab[(v & ((1 << logbase) - 1))]; + v >>= logbase; + } + while (i); + + return v != 0; +} + +static void +field_width_error (const char *filename, const char *fieldname) +{ + error (0, 0, _("%s: field width not sufficient for storing %s"), + filename, fieldname); +} + +static void +field_width_warning (const char *filename, const char *fieldname) +{ + if (warn_option & CPIO_WARN_TRUNCATE) + error (0, 0, _("%s: truncating %s"), filename, fieldname); +} void -write_out_header (struct new_cpio_header *file_hdr, int out_des) +to_ascii_or_warn (char *where, uintmax_t n, size_t digits, + unsigned logbase, + const char *filename, const char *fieldname) { - if (archive_format == arf_newascii || archive_format == arf_crcascii) + if (to_ascii (where, n, digits, logbase)) + field_width_warning (filename, fieldname); +} + +int +to_ascii_or_error (char *where, uintmax_t n, size_t digits, + unsigned logbase, + const char *filename, const char *fieldname) +{ + if (to_ascii (where, n, digits, logbase)) { - char ascii_header[112]; - char *magic_string; + field_width_error (filename, fieldname); + return 1; + } + return 0; +} - if (archive_format == arf_crcascii) - magic_string = "070702"; - else - magic_string = "070701"; - sprintf (ascii_header, - "%6s%08lx%08lx%08lx%08lx%08lx%08lx%08lx%08lx%08lx%08lx%08lx%08lx%08lx", - magic_string, - file_hdr->c_ino, file_hdr->c_mode, file_hdr->c_uid, - file_hdr->c_gid, file_hdr->c_nlink, file_hdr->c_mtime, - file_hdr->c_filesize, file_hdr->c_dev_maj, file_hdr->c_dev_min, - file_hdr->c_rdev_maj, file_hdr->c_rdev_min, file_hdr->c_namesize, - file_hdr->c_chksum); - tape_buffered_write (ascii_header, out_des, 110L); - - /* Write file name to output. */ - tape_buffered_write (file_hdr->c_name, out_des, (long) file_hdr->c_namesize); - tape_pad_output (out_des, file_hdr->c_namesize + 110); - } - else if (archive_format == arf_oldascii || archive_format == arf_hpoldascii) - { - char ascii_header[78]; - dev_t dev; - dev_t rdev; +int +write_out_new_ascii_header (const char *magic_string, + struct new_cpio_header *file_hdr, int out_des) +{ + char ascii_header[110]; + char *p; - if (archive_format == arf_oldascii) - { - dev = makedev (file_hdr->c_dev_maj, file_hdr->c_dev_min); - rdev = makedev (file_hdr->c_rdev_maj, file_hdr->c_rdev_min); - } - else - { - /* HP/UX cpio creates archives that look just like ordinary archives, - but for devices it sets major = 0, minor = 1, and puts the - actual major/minor number in the filesize field. */ - switch (file_hdr->c_mode & CP_IFMT) - { - case CP_IFCHR: - case CP_IFBLK: + (void)strncpy(ascii_header, magic_string, sizeof(ascii_header) - 1); + ascii_header[sizeof(ascii_header) -1] = '\0'; + p = ascii_header + strlen(ascii_header); + to_ascii_or_warn (p, file_hdr->c_ino, 8, LG_16, + file_hdr->c_name, _("inode number")); + p += 8; + to_ascii_or_warn (p, file_hdr->c_mode, 8, LG_16, file_hdr->c_name, + _("file mode")); + p += 8; + to_ascii_or_warn (p, file_hdr->c_uid, 8, LG_16, file_hdr->c_name, + _("uid")); + p += 8; + to_ascii_or_warn (p, file_hdr->c_gid, 8, LG_16, file_hdr->c_name, + _("gid")); + p += 8; + to_ascii_or_warn (p, file_hdr->c_nlink, 8, LG_16, file_hdr->c_name, + _("number of links")); + p += 8; + to_ascii_or_warn (p, file_hdr->c_mtime, 8, LG_16, file_hdr->c_name, + _("modification time")); + p += 8; + if (to_ascii_or_error (p, file_hdr->c_filesize, 8, LG_16, file_hdr->c_name, + _("file size"))) + return 1; + p += 8; + if (to_ascii_or_error (p, file_hdr->c_dev_maj, 8, LG_16, file_hdr->c_name, + _("device major number"))) + return 1; + p += 8; + if (to_ascii_or_error (p, file_hdr->c_dev_min, 8, LG_16, file_hdr->c_name, + _("device minor number"))) + return 1; + p += 8; + if (to_ascii_or_error (p, file_hdr->c_rdev_maj, 8, LG_16, file_hdr->c_name, + _("rdev major"))) + return 1; + p += 8; + if (to_ascii_or_error (p, file_hdr->c_rdev_min, 8, LG_16, file_hdr->c_name, + _("rdev minor"))) + return 1; + p += 8; + if (to_ascii_or_error (p, file_hdr->c_namesize, 8, LG_16, file_hdr->c_name, + _("name size"))) + return 1; + p += 8; + to_ascii (p, file_hdr->c_chksum & 0xffffffff, 8, LG_16); + + tape_buffered_write (ascii_header, out_des, sizeof ascii_header); + + /* Write file name to output. */ + tape_buffered_write (file_hdr->c_name, out_des, (long) file_hdr->c_namesize); + tape_pad_output (out_des, file_hdr->c_namesize + sizeof ascii_header); + return 0; +} + +int +write_out_old_ascii_header (dev_t dev, dev_t rdev, + struct new_cpio_header *file_hdr, int out_des) +{ + char ascii_header[76]; + char *p = ascii_header; + + to_ascii (p, file_hdr->c_magic, 6, LG_8); + p += 6; + to_ascii_or_warn (p, dev, 6, LG_8, file_hdr->c_name, _("device number")); + p += 6; + to_ascii_or_warn (p, file_hdr->c_ino, 6, LG_8, file_hdr->c_name, + _("inode number")); + p += 6; + to_ascii_or_warn (p, file_hdr->c_mode, 6, LG_8, file_hdr->c_name, + _("file mode")); + p += 6; + to_ascii_or_warn (p, file_hdr->c_uid, 6, LG_8, file_hdr->c_name, _("uid")); + p += 6; + to_ascii_or_warn (p, file_hdr->c_gid, 6, LG_8, file_hdr->c_name, _("gid")); + p += 6; + to_ascii_or_warn (p, file_hdr->c_nlink, 6, LG_8, file_hdr->c_name, + _("number of links")); + p += 6; + to_ascii_or_warn (p, rdev, 6, LG_8, file_hdr->c_name, _("rdev")); + p += 6; + to_ascii_or_warn (p, file_hdr->c_mtime, 11, LG_8, file_hdr->c_name, + _("modification time")); + p += 11; + if (to_ascii_or_error (p, file_hdr->c_namesize, 6, LG_8, file_hdr->c_name, + _("name size"))) + return 1; + p += 6; + if (to_ascii_or_error (p, file_hdr->c_filesize, 11, LG_8, file_hdr->c_name, + _("file size"))) + return 1; + + tape_buffered_write (ascii_header, out_des, sizeof ascii_header); + + /* Write file name to output. */ + tape_buffered_write (file_hdr->c_name, out_des, file_hdr->c_namesize); + return 0; +} + +void +hp_compute_dev (struct new_cpio_header *file_hdr, dev_t *pdev, dev_t *prdev) +{ + /* HP/UX cpio creates archives that look just like ordinary archives, + but for devices it sets major = 0, minor = 1, and puts the + actual major/minor number in the filesize field. */ + switch (file_hdr->c_mode & CP_IFMT) + { + case CP_IFCHR: + case CP_IFBLK: #ifdef CP_IFSOCK - case CP_IFSOCK: + case CP_IFSOCK: #endif #ifdef CP_IFIFO - case CP_IFIFO: + case CP_IFIFO: #endif - file_hdr->c_filesize = makedev (file_hdr->c_rdev_maj, - file_hdr->c_rdev_min); - rdev = 1; - break; - default: - dev = makedev (file_hdr->c_dev_maj, file_hdr->c_dev_min); - rdev = makedev (file_hdr->c_rdev_maj, file_hdr->c_rdev_min); - break; - } - } + file_hdr->c_filesize = makedev (file_hdr->c_rdev_maj, + file_hdr->c_rdev_min); + *pdev = *prdev = makedev (0, 1); + break; + + default: + *pdev = makedev (file_hdr->c_dev_maj, file_hdr->c_dev_min); + *prdev = makedev (file_hdr->c_rdev_maj, file_hdr->c_rdev_min); + break; + } +} - if ((warn_option & CPIO_WARN_TRUNCATE) && (file_hdr->c_ino >> 16) != 0) - error (0, 0, _("%s: truncating inode number"), file_hdr->c_name); +int +write_out_binary_header (dev_t rdev, + struct new_cpio_header *file_hdr, int out_des) +{ + struct old_cpio_header short_hdr; - /* Debian hack: The type of dev_t has changed in glibc. Fixed output - to ensure that a long int is passed to sprintf. This has been - reported to "bug-gnu-utils@@prep.ai.mit.edu". (1998/5/26) -BEM */ - sprintf (ascii_header, - "%06ho%06lo%06lo%06lo%06lo%06lo%06lo%06lo%011lo%06lo%011lo", - file_hdr->c_magic & 0xFFFF, (long) dev & 0xFFFF, - file_hdr->c_ino & 0xFFFF, file_hdr->c_mode & 0xFFFF, - file_hdr->c_uid & 0xFFFF, file_hdr->c_gid & 0xFFFF, - file_hdr->c_nlink & 0xFFFF, (long) rdev & 0xFFFF, - file_hdr->c_mtime, file_hdr->c_namesize & 0xFFFF, - file_hdr->c_filesize); - tape_buffered_write (ascii_header, out_des, 76L); + short_hdr.c_magic = 070707; + short_hdr.c_dev = makedev (file_hdr->c_dev_maj, file_hdr->c_dev_min); - /* Write file name to output. */ - tape_buffered_write (file_hdr->c_name, out_des, (long) file_hdr->c_namesize); - } - else if (archive_format == arf_tar || archive_format == arf_ustar) - { - write_out_tar_header (file_hdr, out_des); - } - else - { - struct old_cpio_header short_hdr; + if ((warn_option & CPIO_WARN_TRUNCATE) && (file_hdr->c_ino >> 16) != 0) + error (0, 0, _("%s: truncating inode number"), file_hdr->c_name); - short_hdr.c_magic = 070707; - short_hdr.c_dev = makedev (file_hdr->c_dev_maj, file_hdr->c_dev_min); + short_hdr.c_ino = file_hdr->c_ino & 0xFFFF; + if (short_hdr.c_ino != file_hdr->c_ino) + field_width_warning (file_hdr->c_name, _("inode number")); + + short_hdr.c_mode = file_hdr->c_mode & 0xFFFF; + if (short_hdr.c_mode != file_hdr->c_mode) + field_width_warning (file_hdr->c_name, _("file mode")); + + short_hdr.c_uid = file_hdr->c_uid & 0xFFFF; + if (short_hdr.c_uid != file_hdr->c_uid) + field_width_warning (file_hdr->c_name, _("uid")); + + short_hdr.c_gid = file_hdr->c_gid & 0xFFFF; + if (short_hdr.c_gid != file_hdr->c_gid) + field_width_warning (file_hdr->c_name, _("gid")); + + short_hdr.c_nlink = file_hdr->c_nlink & 0xFFFF; + if (short_hdr.c_nlink != file_hdr->c_nlink) + field_width_warning (file_hdr->c_name, _("number of links")); + + short_hdr.c_rdev = rdev; + short_hdr.c_mtimes[0] = file_hdr->c_mtime >> 16; + short_hdr.c_mtimes[1] = file_hdr->c_mtime & 0xFFFF; + + short_hdr.c_namesize = file_hdr->c_namesize & 0xFFFF; + if (short_hdr.c_namesize != file_hdr->c_namesize) + { + field_width_error (file_hdr->c_name, _("name size")); + return 1; + } + + short_hdr.c_filesize = file_hdr->c_filesize; + if (short_hdr.c_filesize != file_hdr->c_filesize) + { + field_width_error (file_hdr->c_name, _("file size")); + return 1; + } + + short_hdr.c_filesizes[0] = file_hdr->c_filesize >> 16; + short_hdr.c_filesizes[1] = file_hdr->c_filesize & 0xFFFF; - if ((warn_option & CPIO_WARN_TRUNCATE) && (file_hdr->c_ino >> 16) != 0) - error (0, 0, _("%s: truncating inode number"), file_hdr->c_name); + /* Output the file header. */ + tape_buffered_write ((char *) &short_hdr, out_des, 26); - short_hdr.c_ino = file_hdr->c_ino & 0xFFFF; - short_hdr.c_mode = file_hdr->c_mode & 0xFFFF; - short_hdr.c_uid = file_hdr->c_uid & 0xFFFF; - short_hdr.c_gid = file_hdr->c_gid & 0xFFFF; - short_hdr.c_nlink = file_hdr->c_nlink & 0xFFFF; - if (archive_format != arf_hpbinary) - short_hdr.c_rdev = makedev (file_hdr->c_rdev_maj, file_hdr->c_rdev_min); - else - { - switch (file_hdr->c_mode & CP_IFMT) - { - /* HP/UX cpio creates archives that look just like ordinary - archives, but for devices it sets major = 0, minor = 1, and - puts the actual major/minor number in the filesize field. */ - case CP_IFCHR: - case CP_IFBLK: -#ifdef CP_IFSOCK - case CP_IFSOCK: -#endif -#ifdef CP_IFIFO - case CP_IFIFO: -#endif - file_hdr->c_filesize = makedev (file_hdr->c_rdev_maj, - file_hdr->c_rdev_min); - short_hdr.c_rdev = makedev (0, 1); - break; - default: - short_hdr.c_rdev = makedev (file_hdr->c_rdev_maj, - file_hdr->c_rdev_min); - break; - } - } - short_hdr.c_mtimes[0] = file_hdr->c_mtime >> 16; - short_hdr.c_mtimes[1] = file_hdr->c_mtime & 0xFFFF; + /* Write file name to output. */ + tape_buffered_write (file_hdr->c_name, out_des, file_hdr->c_namesize); - short_hdr.c_namesize = file_hdr->c_namesize & 0xFFFF; + tape_pad_output (out_des, file_hdr->c_namesize + 26); + return 0; +} - short_hdr.c_filesizes[0] = file_hdr->c_filesize >> 16; - short_hdr.c_filesizes[1] = file_hdr->c_filesize & 0xFFFF; + +/* Write out header FILE_HDR, including the file name, to file + descriptor OUT_DES. */ - /* Output the file header. */ - tape_buffered_write ((char *) &short_hdr, out_des, 26L); +int +write_out_header (struct new_cpio_header *file_hdr, int out_des) +{ + dev_t dev; + dev_t rdev; + + switch (archive_format) + { + case arf_newascii: + return write_out_new_ascii_header ("070701", file_hdr, out_des); + + case arf_crcascii: + return write_out_new_ascii_header ("070702", file_hdr, out_des); + + case arf_oldascii: + return write_out_old_ascii_header (makedev (file_hdr->c_dev_maj, + file_hdr->c_dev_min), + makedev (file_hdr->c_rdev_maj, + file_hdr->c_rdev_min), + file_hdr, out_des); + + case arf_hpoldascii: + hp_compute_dev (file_hdr, &dev, &rdev); + return write_out_old_ascii_header (dev, rdev, file_hdr, out_des); + + case arf_tar: + case arf_ustar: + if (is_tar_filename_too_long (file_hdr->c_name)) + { + error (0, 0, _("%s: file name too long"), file_hdr->c_name); + return 1; + } + write_out_tar_header (file_hdr, out_des); /* FIXME: No error checking */ + return 0; - /* Write file name to output. */ - tape_buffered_write (file_hdr->c_name, out_des, (long) file_hdr->c_namesize); + case arf_binary: + return write_out_binary_header (makedev (file_hdr->c_rdev_maj, + file_hdr->c_rdev_min), + file_hdr, out_des); + + case arf_hpbinary: + hp_compute_dev (file_hdr, &dev, &rdev); + /* FIXME: dev ignored. Should it be? */ + return write_out_binary_header (rdev, file_hdr, out_des); - tape_pad_output (out_des, file_hdr->c_namesize + 26); + default: + abort (); } } @@@@ -593,14 +755,7 @@@@ process_copy_out () file_hdr.c_namesize = strlen (p) + 1; } #endif - if ((archive_format == arf_tar || archive_format == arf_ustar) - && is_tar_filename_too_long (file_hdr.c_name)) - { - error (0, 0, _("%s: file name too long"), - file_hdr.c_name); - continue; - } - + /* Copy the named file to the output. */ switch (file_hdr.c_mode & CP_IFMT) { @@@@ -613,8 +768,8 @@@@ process_copy_out () file_hdr.c_dev_min))) { file_hdr.c_tar_linkname = otherfile; - write_out_header (&file_hdr, out_file_des); - break; + if (write_out_header (&file_hdr, out_file_des)) + continue; } } if ( (archive_format == arf_newascii || archive_format == arf_crcascii) @@@@ -643,7 +798,8 @@@@ process_copy_out () file_hdr.c_filesize, input_name.ds_string); - write_out_header (&file_hdr, out_file_des); + if (write_out_header (&file_hdr, out_file_des)) + continue; copy_files_disk_to_tape (in_file_des, out_file_des, file_hdr.c_filesize, input_name.ds_string); warn_if_file_changed(input_name.ds_string, file_hdr.c_filesize, file_hdr.c_mtime); @@@@ -673,7 +829,8 @@@@ process_copy_out () case CP_IFDIR: file_hdr.c_filesize = 0; - write_out_header (&file_hdr, out_file_des); + if (write_out_header (&file_hdr, out_file_des)) + continue; break; case CP_IFCHR: @@@@ -702,14 +859,16 @@@@ process_copy_out () file_hdr.c_mode = (file_stat.st_mode & 07777); file_hdr.c_mode |= CP_IFREG; file_hdr.c_tar_linkname = otherfile; - write_out_header (&file_hdr, out_file_des); + if (write_out_header (&file_hdr, out_file_des)) + continue; break; } add_inode (file_hdr.c_ino, file_hdr.c_name, file_hdr.c_dev_maj, file_hdr.c_dev_min); } file_hdr.c_filesize = 0; - write_out_header (&file_hdr, out_file_des); + if (write_out_header (&file_hdr, out_file_des)) + continue; break; #ifdef CP_IFLNK @@@@ -738,12 +897,14 @@@@ process_copy_out () { link_name[link_size] = '\0'; file_hdr.c_tar_linkname = link_name; - write_out_header (&file_hdr, out_file_des); + if (write_out_header (&file_hdr, out_file_des)) + continue; } } else { - write_out_header (&file_hdr, out_file_des); + if (write_out_header (&file_hdr, out_file_des)) + continue; tape_buffered_write (link_name, out_file_des, link_size); tape_pad_output (out_file_des, link_size); } @