• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* cpio.c - a basic cpio
2  *
3  * Copyright 2013 Isaac Dunham <ibid.ag@gmail.com>
4  * Copyright 2015 Frontier Silicon Ltd.
5  *
6  * see https://www.kernel.org/doc/Documentation/early-userspace/buffer-format.txt
7  * and http://refspecs.linuxfoundation.org/LSB_4.1.0/LSB-Core-generic/LSB-Core-generic/cpio.html
8  * and http://pubs.opengroup.org/onlinepubs/7908799/xcu/cpio.html
9  *
10  * Yes, that's SUSv2, newer versions removed it, but RPM and initramfs use
11  * this archive format. We implement (only) the modern "-H newc" variant which
12  * expanded headers to 110 bytes (first field 6 bytes, rest are 8).
13  * In order: magic ino mode uid gid nlink mtime filesize devmajor devminor
14  * rdevmajor rdevminor namesize check
15  * This is the equivalent of mode -H newc in other implementations.
16  * We always do --quiet, but accept it as a compatibility NOP.
17  *
18  * TODO: export/import linux file list text format ala gen_initramfs_list.sh
19  * TODO: hardlink support, -A, -0, -a, -L, --sparse
20  * TODO: --renumber-archives (probably always?) --ignore-devno --reproducible
21 
22 USE_CPIO(NEWTOY(cpio, "(ignore-devno)(renumber-inodes)(quiet)(no-preserve-owner)R(owner):md(make-directories)uH:p|i|t|F:v(verbose)o|[!pio][!pot][!pF]", TOYFLAG_BIN))
23 
24 config CPIO
25   bool "cpio"
26   default y
27   help
28     usage: cpio -{o|t|i|p DEST} [-v] [--verbose] [-F FILE] [-R [USER][:GROUP] [--no-preserve-owner]
29 
30     Copy files into and out of a "newc" format cpio archive.
31 
32     -d	Create directories if needed
33     -F FILE	Use archive FILE instead of stdin/stdout
34     -i	Extract from archive into file system (stdin=archive)
35     -o	Create archive (stdin=list of files, stdout=archive)
36     -p DEST	Copy-pass mode, copy stdin file list to directory DEST
37     -R USER	Replace owner with USER[:GROUP]
38     -t	Test files (list only, stdin=archive, stdout=list of files)
39     -u	Unlink existing files when extracting
40     -v	Verbose
41     --no-preserve-owner     Don't set ownership during extract
42 */
43 
44 #define FOR_cpio
45 #include "toys.h"
46 
47 GLOBALS(
48   char *F, *H, *R;
49 )
50 
51 // Read strings, tail padded to 4 byte alignment. Argument "align" is amount
52 // by which start of string isn't aligned (usually 0, but header is 110 bytes
53 // which is 2 bytes off because the first field wasn't expanded from 6 to 8).
strpad(int fd,unsigned len,unsigned align)54 static char *strpad(int fd, unsigned len, unsigned align)
55 {
56   char *str;
57 
58   align = (align + len) & 3;
59   if (align) len += (4-align);
60   xreadall(fd, str = xmalloc(len+1), len);
61   str[len]=0; // redundant, in case archive is bad
62 
63   return str;
64 }
65 
66 //convert hex to uint; mostly to allow using bits of non-terminated strings
x8u(char * hex)67 static unsigned x8u(char *hex)
68 {
69   unsigned val, inpos = 8, outpos;
70   char pattern[6];
71 
72   while (*hex == '0') {
73     hex++;
74     if (!--inpos) return 0;
75   }
76   // Because scanf gratuitously treats %*X differently than printf does.
77   sprintf(pattern, "%%%dX%%n", inpos);
78   sscanf(hex, pattern, &val, &outpos);
79   if (inpos != outpos) error_exit("bad hex");
80 
81   return val;
82 }
83 
cpio_main(void)84 void cpio_main(void)
85 {
86   int pipe, afd = FLAG(o), reown = !geteuid() && !FLAG(no_preserve_owner),
87       empty = 1;
88   pid_t pid = 0;
89   long Ruid = -1, Rgid = -1;
90   char *tofree = 0;
91 
92   if (TT.R) {
93     char *group = TT.R+strcspn(TT.R, ":.");
94 
95     if (*group) {
96       Rgid = xgetgid(group+1);
97       *group = 0;
98     }
99     if (group != TT.R) Ruid = xgetuid(TT.R);
100   }
101 
102   // In passthrough mode, parent stays in original dir and generates archive
103   // to pipe, child does chdir to new dir and reads archive from stdin (pipe).
104   if (FLAG(p)) {
105     if (FLAG(d)) {
106       if (!*toys.optargs) error_exit("need directory for -p");
107       if (mkdir(*toys.optargs, 0700) == -1 && errno != EEXIST)
108         perror_msg("mkdir %s", *toys.optargs);
109     }
110     if (toys.stacktop) {
111       // xpopen() doesn't return from child due to vfork(), instead restarts
112       // with !toys.stacktop
113       pid = xpopen(0, &pipe, 0);
114       afd = pipe;
115     } else {
116       // child
117       toys.optflags |= FLAG_i;
118       xchdir(*toys.optargs);
119     }
120   }
121 
122   if (TT.F) {
123     int perm = FLAG(o) ? O_CREAT|O_WRONLY|O_TRUNC : O_RDONLY;
124 
125     afd = xcreate(TT.F, perm, 0644);
126   }
127 
128   // read cpio archive
129 
130   if (FLAG(i) || FLAG(t)) for (;; empty = 0) {
131     char *name, *data;
132     unsigned mode, uid, gid, timestamp;
133     int test = FLAG(t), err = 0, size = 0, len;
134 
135     free(tofree);
136     tofree = 0;
137     // read header, skipping arbitrary leading NUL bytes (concatenated archives)
138     for (;;) {
139       if (1>(len = readall(afd, toybuf+size, 110-size))) break;
140       if (size || *toybuf) {
141         size += len;
142         break;
143       }
144       for (size = 0; size<len; size++) if (toybuf[size]) break;
145       memmove(toybuf, toybuf+size, len-size);
146       size = len-size;
147     }
148     if (!size) {
149       if (empty) error_exit("empty archive");
150       else break;
151     }
152     if (size != 110 || smemcmp(toybuf, "070701", 6)) error_exit("bad header");
153     tofree = name = strpad(afd, x8u(toybuf+94), 110);
154     // TODO: this flushes hardlink detection via major/minor/ino match
155     if (!strcmp("TRAILER!!!", name)) continue;
156 
157     // If you want to extract absolute paths, "cd /" and run cpio.
158     while (*name == '/') name++;
159     // TODO: remove .. entries
160 
161     size = x8u(toybuf+54);
162     mode = x8u(toybuf+14);
163     uid = (Ruid>=0) ? Ruid : x8u(toybuf+22);
164     gid = (Rgid>=0) ? Rgid : x8u(toybuf+30);
165     timestamp = x8u(toybuf+46); // unsigned 32 bit, so year 2100 problem
166 
167     // (This output is unaffected by --quiet.)
168     if (FLAG(t) || FLAG(v)) puts(name);
169 
170     if (FLAG(u) && !test) if (unlink(name) && errno == EISDIR) rmdir(name);
171 
172     if (!test && FLAG(d) && strrchr(name, '/') && mkpath(name)) {
173       perror_msg("mkpath '%s'", name);
174       test++;
175     }
176 
177     // Consume entire record even if it couldn't create file, so we're
178     // properly aligned with next file.
179 
180     if (S_ISDIR(mode)) {
181       if (test) continue;
182       err = mkdir(name, mode) && (errno != EEXIST && !FLAG(u));
183 
184       // Creading dir/dev doesn't give us a filehandle, we have to refer to it
185       // by name to chown/utime, but how do we know it's the same item?
186       // Check that we at least have the right type of entity open, and do
187       // NOT restore dropped suid bit in this case.
188       if (S_ISDIR(mode) && reown) {
189         int fd = open(name, O_RDONLY|O_NOFOLLOW);
190         struct stat st;
191 
192         if (fd != -1 && !fstat(fd, &st) && (st.st_mode&S_IFMT) == (mode&S_IFMT))
193           err = fchown(fd, uid, gid);
194         else err = 1;
195 
196         close(fd);
197       }
198     } else if (S_ISREG(mode)) {
199       int fd = test ? 0 : open(name, O_CREAT|O_WRONLY|O_EXCL|O_NOFOLLOW, mode);
200 
201       // If write fails, we still need to read/discard data to continue with
202       // archive. Since doing so overwrites errno, report error now
203       if (fd < 0) {
204         perror_msg("create %s", name);
205         test++;
206       }
207 
208       data = toybuf;
209       while (size) {
210         if (size < sizeof(toybuf)) data = strpad(afd, size, 0);
211         else xreadall(afd, toybuf, sizeof(toybuf));
212         if (!test) xwrite(fd, data, data == toybuf ? sizeof(toybuf) : size);
213         if (data != toybuf) {
214           free(data);
215           break;
216         }
217         size -= sizeof(toybuf);
218       }
219 
220       if (!test) {
221         // set owner, restore dropped suid bit
222         if (reown) err = fchown(fd, uid, gid) && fchmod(fd, mode);
223         close(fd);
224       }
225     } else {
226       data = S_ISLNK(mode) ? strpad(afd, size, 0) : 0;
227       if (!test) {
228         err = data ? symlink(data, name)
229           : mknod(name, mode, dev_makedev(x8u(toybuf+78), x8u(toybuf+86)));
230 
231         // Can't get a filehandle to a symlink or a node on nodev mount,
232         // so do special chown that at least doesn't follow symlinks.
233         // We also don't chmod after, so dropped suid bit isn't restored
234         if (!err && reown) err = lchown(name, uid, gid);
235       }
236       free(data);
237     }
238 
239     // Set timestamp.
240     if (!test && !err) {
241       struct timespec times[2];
242 
243       memset(times, 0, sizeof(struct timespec)*2);
244       times[0].tv_sec = times[1].tv_sec = timestamp;
245       err = utimensat(AT_FDCWD, name, times, AT_SYMLINK_NOFOLLOW);
246     }
247 
248     if (err) perror_msg_raw(name);
249 
250   // Output cpio archive
251 
252   } else {
253     char *name = 0;
254     size_t size = 0;
255     unsigned inode = 0;
256 
257     for (;;) {
258       struct stat st;
259       unsigned nlen, error = 0, zero = 0;
260       int len, fd = -1;
261       char *link = 0;
262       ssize_t llen;
263 
264       len = getline(&name, &size, stdin);
265       if (len<1) break;
266       if (name[len-1] == '\n') name[--len] = 0;
267       nlen = len+1;
268       if (lstat(name, &st) || (S_ISREG(st.st_mode)
269           && st.st_size && (fd = open(name, O_RDONLY))<0)
270           || (S_ISLNK(st.st_mode) && !(link = xreadlink(name))))
271       {
272         perror_msg_raw(name);
273         continue;
274       }
275       // encrypted filesystems can stat the wrong link size
276       if (link) st.st_size = strlen(link);
277 
278       if (Ruid>=0) st.st_uid = Ruid;
279       if (Rgid>=0) st.st_gid = Rgid;
280       if (FLAG(no_preserve_owner)) st.st_uid = st.st_gid = 0;
281       if (!S_ISREG(st.st_mode) && !S_ISLNK(st.st_mode)) st.st_size = 0;
282       if (st.st_size >> 32) perror_msg("skipping >2G file '%s'", name);
283       else {
284         if (FLAG(renumber_inodes)) st.st_ino = ++inode;
285         if (FLAG(ignore_devno)) st.st_rdev = 0;
286         llen = sprintf(toybuf,
287           "070701%08X%08X%08X%08X%08X%08X%08X%08X%08X%08X%08X%08X%08X",
288           (int)st.st_ino, st.st_mode, st.st_uid, st.st_gid, (int)st.st_nlink,
289           (int)st.st_mtime, (int)st.st_size, dev_major(st.st_dev),
290           dev_minor(st.st_dev), dev_major(st.st_rdev), dev_minor(st.st_rdev),
291           nlen, 0);
292         xwrite(afd, toybuf, llen);
293         xwrite(afd, name, nlen);
294 
295         // NUL Pad header up to 4 multiple bytes.
296         llen = (llen + nlen) & 3;
297         if (llen) xwrite(afd, &zero, 4-llen);
298 
299         // Write out body for symlink or regular file
300         if (link) xwrite(afd, link, st.st_size);
301         else for (llen = st.st_size; llen; llen -= nlen) {
302           nlen = llen > sizeof(toybuf) ? sizeof(toybuf) : llen;
303           // If read fails, write anyway (already wrote size in header)
304           if (nlen != readall(fd, toybuf, nlen))
305             if (!error++) perror_msg("bad read from file '%s'", name);
306           xwrite(afd, toybuf, nlen);
307         }
308         llen = st.st_size & 3;
309         if (llen) xwrite(afd, &zero, 4-llen);
310       }
311       free(link);
312       xclose(fd);
313     }
314     if (CFG_TOYBOX_FREE) free(name);
315 
316     // nlink=1, namesize=11, with padding
317     dprintf(afd, "070701%040X%056X%08XTRAILER!!!%c%c%c%c", 1, 11, 0, 0, 0, 0,0);
318   }
319   if (TT.F) xclose(afd);
320 
321   if (FLAG(p) && pid) toys.exitval |= xpclose(pid, pipe);
322 }
323