bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 1 | /* |
| 2 | ** Copyright 1998-2003 University of Illinois Board of Trustees |
| 3 | ** Copyright 1998-2003 Mark D. Roth |
| 4 | ** All rights reserved. |
| 5 | ** |
| 6 | ** encode.c - libtar code to encode tar header blocks |
| 7 | ** |
| 8 | ** Mark D. Roth <roth@uiuc.edu> |
| 9 | ** Campus Information Technologies and Educational Services |
| 10 | ** University of Illinois at Urbana-Champaign |
| 11 | */ |
| 12 | |
| 13 | #include <internal.h> |
| 14 | |
| 15 | #include <stdio.h> |
| 16 | #include <pwd.h> |
| 17 | #include <grp.h> |
| 18 | #include <sys/types.h> |
Ethan Yonker | fefe591 | 2017-09-30 22:22:13 -0500 | [diff] [blame] | 19 | #include <sys/sysmacros.h> |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 20 | |
| 21 | #ifdef STDC_HEADERS |
| 22 | # include <string.h> |
| 23 | # include <stdlib.h> |
| 24 | #endif |
| 25 | |
| 26 | |
| 27 | /* magic, version, and checksum */ |
| 28 | void |
| 29 | th_finish(TAR *t) |
| 30 | { |
| 31 | if (t->options & TAR_GNU) |
| 32 | { |
| 33 | /* we're aiming for this result, but must do it in |
| 34 | * two calls to avoid FORTIFY segfaults on some Linux |
| 35 | * systems: |
| 36 | * strncpy(t->th_buf.magic, "ustar ", 8); |
| 37 | */ |
| 38 | strncpy(t->th_buf.magic, "ustar ", 6); |
| 39 | strncpy(t->th_buf.version, " ", 2); |
| 40 | } |
| 41 | else |
| 42 | { |
| 43 | strncpy(t->th_buf.version, TVERSION, TVERSLEN); |
| 44 | strncpy(t->th_buf.magic, TMAGIC, TMAGLEN); |
| 45 | } |
| 46 | |
| 47 | int_to_oct(th_crc_calc(t), t->th_buf.chksum, 8); |
| 48 | } |
| 49 | |
| 50 | |
| 51 | /* map a file mode to a typeflag */ |
| 52 | void |
| 53 | th_set_type(TAR *t, mode_t mode) |
| 54 | { |
| 55 | if (S_ISLNK(mode)) |
| 56 | t->th_buf.typeflag = SYMTYPE; |
| 57 | if (S_ISREG(mode)) |
| 58 | t->th_buf.typeflag = REGTYPE; |
| 59 | if (S_ISDIR(mode)) |
| 60 | t->th_buf.typeflag = DIRTYPE; |
| 61 | if (S_ISCHR(mode)) |
| 62 | t->th_buf.typeflag = CHRTYPE; |
| 63 | if (S_ISBLK(mode)) |
| 64 | t->th_buf.typeflag = BLKTYPE; |
| 65 | if (S_ISFIFO(mode) || S_ISSOCK(mode)) |
| 66 | t->th_buf.typeflag = FIFOTYPE; |
| 67 | } |
| 68 | |
| 69 | |
| 70 | /* encode file path */ |
| 71 | void |
James Christopher Adduono | 6f57f7c | 2016-03-01 16:01:53 -0500 | [diff] [blame] | 72 | th_set_path(TAR *t, const char *pathname) |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 73 | { |
| 74 | char suffix[2] = ""; |
| 75 | char *tmp; |
James Christopher Adduono | 6f57f7c | 2016-03-01 16:01:53 -0500 | [diff] [blame] | 76 | size_t pathname_len = strlen(pathname); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 77 | |
| 78 | #ifdef DEBUG |
Mohd Faraz | 1524764 | 2023-01-16 16:16:21 +0100 | [diff] [blame^] | 79 | LOG("in th_set_path(th, pathname=\"%s\")\n", pathname); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 80 | #endif |
| 81 | |
| 82 | if (t->th_buf.gnu_longname != NULL) |
| 83 | free(t->th_buf.gnu_longname); |
| 84 | t->th_buf.gnu_longname = NULL; |
| 85 | |
James Christopher Adduono | 6f57f7c | 2016-03-01 16:01:53 -0500 | [diff] [blame] | 86 | /* old archive compatibility (not needed for gnu): add trailing / to directories */ |
| 87 | if (pathname[pathname_len - 1] != '/' && TH_ISDIR(t)) |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 88 | strcpy(suffix, "/"); |
| 89 | |
James Christopher Adduono | 6f57f7c | 2016-03-01 16:01:53 -0500 | [diff] [blame] | 90 | if (pathname_len >= T_NAMELEN && (t->options & TAR_GNU)) |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 91 | { |
James Christopher Adduono | 6f57f7c | 2016-03-01 16:01:53 -0500 | [diff] [blame] | 92 | /* GNU-style long name (no file name length limit) */ |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 93 | t->th_buf.gnu_longname = strdup(pathname); |
| 94 | strncpy(t->th_buf.name, t->th_buf.gnu_longname, T_NAMELEN); |
| 95 | } |
James Christopher Adduono | 6f57f7c | 2016-03-01 16:01:53 -0500 | [diff] [blame] | 96 | else if (pathname_len >= T_NAMELEN) |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 97 | { |
James Christopher Adduono | 6f57f7c | 2016-03-01 16:01:53 -0500 | [diff] [blame] | 98 | /* POSIX-style prefix field: |
| 99 | * The maximum length of a file name is limited to 256 characters, |
| 100 | * provided that the file name can be split at a directory separator |
| 101 | * in two parts. The first part being at most 155 bytes long and |
| 102 | * the second part being at most 100 bytes long. So, in most cases |
| 103 | * the maximum file name length will be shorter than 256 characters. |
| 104 | */ |
| 105 | char tail_path[T_NAMELEN + 1]; |
| 106 | tmp = strchr(&(pathname[pathname_len - T_NAMELEN]), '/'); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 107 | if (tmp == NULL) |
| 108 | { |
Mohd Faraz | 1524764 | 2023-01-16 16:16:21 +0100 | [diff] [blame^] | 109 | LOG("!!! '/' not found in \"%s\"\n", pathname); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 110 | return; |
| 111 | } |
James Christopher Adduono | 6f57f7c | 2016-03-01 16:01:53 -0500 | [diff] [blame] | 112 | snprintf(tail_path, T_NAMELEN + 1, "%s%s", &tmp[1], suffix); |
| 113 | strncpy(t->th_buf.name, tail_path, T_NAMELEN); |
| 114 | |
| 115 | /* |
| 116 | * first part, max = 155 == sizeof(t->th_buf.prefix) , include NULL if it fits |
| 117 | * trailing '/' is added during decode: decode.c/th_get_pathname() |
| 118 | */ |
| 119 | if (tmp - pathname >= 155) { |
| 120 | strncpy(t->th_buf.prefix, pathname, 155); |
| 121 | } else { |
| 122 | snprintf(t->th_buf.prefix, (tmp - pathname + 1), "%s", pathname); |
| 123 | } |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 124 | } |
James Christopher Adduono | 6f57f7c | 2016-03-01 16:01:53 -0500 | [diff] [blame] | 125 | else { |
| 126 | /* any short name for all formats, or classic tar format (99 chars max) */ |
| 127 | snprintf(t->th_buf.name, T_NAMELEN, "%s%s", pathname, suffix); |
| 128 | } |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 129 | |
| 130 | #ifdef DEBUG |
Mohd Faraz | 1524764 | 2023-01-16 16:16:21 +0100 | [diff] [blame^] | 131 | LOG("returning from th_set_path()..."); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 132 | #endif |
| 133 | } |
| 134 | |
| 135 | |
| 136 | /* encode link path */ |
| 137 | void |
James Christopher Adduono | 6f57f7c | 2016-03-01 16:01:53 -0500 | [diff] [blame] | 138 | th_set_link(TAR *t, const char *linkname) |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 139 | { |
| 140 | #ifdef DEBUG |
Mohd Faraz | 1524764 | 2023-01-16 16:16:21 +0100 | [diff] [blame^] | 141 | LOG("==> th_set_link(th, linkname=\"%s\")\n", linkname); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 142 | #endif |
| 143 | |
James Christopher Adduono | 6f57f7c | 2016-03-01 16:01:53 -0500 | [diff] [blame] | 144 | if (strlen(linkname) >= T_NAMELEN && (t->options & TAR_GNU)) |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 145 | { |
James Christopher Adduono | 6f57f7c | 2016-03-01 16:01:53 -0500 | [diff] [blame] | 146 | /* --format=gnu: GNU-style long name (no file name length limit) */ |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 147 | t->th_buf.gnu_longlink = strdup(linkname); |
| 148 | strcpy(t->th_buf.linkname, "././@LongLink"); |
| 149 | } |
James Christopher Adduono | 6f57f7c | 2016-03-01 16:01:53 -0500 | [diff] [blame] | 150 | else if (strlen(linkname) >= T_NAMELEN) |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 151 | { |
James Christopher Adduono | 6f57f7c | 2016-03-01 16:01:53 -0500 | [diff] [blame] | 152 | /* --format=ustar: 100 chars max limit for symbolic links */ |
| 153 | strncpy(t->th_buf.linkname, linkname, T_NAMELEN); |
| 154 | if (t->th_buf.gnu_longlink != NULL) |
| 155 | free(t->th_buf.gnu_longlink); |
| 156 | t->th_buf.gnu_longlink = NULL; |
| 157 | } else { |
| 158 | /* all short links or v7 tar format: The maximum length of a symbolic link name is limited to 99 characters */ |
| 159 | snprintf(t->th_buf.linkname, T_NAMELEN, "%s", linkname); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 160 | if (t->th_buf.gnu_longlink != NULL) |
| 161 | free(t->th_buf.gnu_longlink); |
| 162 | t->th_buf.gnu_longlink = NULL; |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | |
| 167 | /* encode device info */ |
| 168 | void |
| 169 | th_set_device(TAR *t, dev_t device) |
| 170 | { |
| 171 | #ifdef DEBUG |
Mohd Faraz | 1524764 | 2023-01-16 16:16:21 +0100 | [diff] [blame^] | 172 | LOG("th_set_device(): major = %d, minor = %d\n", |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 173 | major(device), minor(device)); |
| 174 | #endif |
| 175 | int_to_oct(major(device), t->th_buf.devmajor, 8); |
| 176 | int_to_oct(minor(device), t->th_buf.devminor, 8); |
| 177 | } |
| 178 | |
| 179 | |
| 180 | /* encode user info */ |
| 181 | void |
| 182 | th_set_user(TAR *t, uid_t uid) |
| 183 | { |
| 184 | struct passwd *pw; |
| 185 | |
James Christopher Adduono | 6f57f7c | 2016-03-01 16:01:53 -0500 | [diff] [blame] | 186 | if (!(t->options & TAR_USE_NUMERIC_ID)) { |
| 187 | pw = getpwuid(uid); |
| 188 | if (pw != NULL) |
| 189 | strlcpy(t->th_buf.uname, pw->pw_name, sizeof(t->th_buf.uname)); |
| 190 | } |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 191 | |
| 192 | int_to_oct(uid, t->th_buf.uid, 8); |
| 193 | } |
| 194 | |
| 195 | |
| 196 | /* encode group info */ |
| 197 | void |
| 198 | th_set_group(TAR *t, gid_t gid) |
| 199 | { |
| 200 | struct group *gr; |
| 201 | |
James Christopher Adduono | 6f57f7c | 2016-03-01 16:01:53 -0500 | [diff] [blame] | 202 | if (!(t->options & TAR_USE_NUMERIC_ID)) { |
| 203 | gr = getgrgid(gid); |
| 204 | if (gr != NULL) |
| 205 | strlcpy(t->th_buf.gname, gr->gr_name, sizeof(t->th_buf.gname)); |
| 206 | } |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 207 | |
| 208 | int_to_oct(gid, t->th_buf.gid, 8); |
| 209 | } |
| 210 | |
| 211 | |
| 212 | /* encode file mode */ |
| 213 | void |
| 214 | th_set_mode(TAR *t, mode_t fmode) |
| 215 | { |
| 216 | if (S_ISSOCK(fmode)) |
| 217 | { |
| 218 | fmode &= ~S_IFSOCK; |
| 219 | fmode |= S_IFIFO; |
| 220 | } |
| 221 | int_to_oct(fmode, (t)->th_buf.mode, 8); |
| 222 | } |
| 223 | |
| 224 | |
| 225 | void |
| 226 | th_set_from_stat(TAR *t, struct stat *s) |
| 227 | { |
| 228 | th_set_type(t, s->st_mode); |
| 229 | if (S_ISCHR(s->st_mode) || S_ISBLK(s->st_mode)) |
| 230 | th_set_device(t, s->st_rdev); |
| 231 | th_set_user(t, s->st_uid); |
| 232 | th_set_group(t, s->st_gid); |
| 233 | th_set_mode(t, s->st_mode); |
| 234 | th_set_mtime(t, s->st_mtime); |
| 235 | if (S_ISREG(s->st_mode)) |
| 236 | th_set_size(t, s->st_size); |
| 237 | else |
| 238 | th_set_size(t, 0); |
| 239 | } |