build.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604
  1. /*
  2. * dpkg-deb - construction and deconstruction of *.deb archives
  3. * build.c - building archives
  4. *
  5. * Copyright © 1994,1995 Ian Jackson <ian@chiark.greenend.org.uk>
  6. * Copyright © 2000,2001 Wichert Akkerman <wakkerma@debian.org>
  7. *
  8. * This is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. */
  21. #include <config.h>
  22. #include <compat.h>
  23. #include <sys/types.h>
  24. #include <sys/stat.h>
  25. #include <sys/wait.h>
  26. #include <errno.h>
  27. #include <limits.h>
  28. #include <ctype.h>
  29. #include <string.h>
  30. #include <dirent.h>
  31. #include <fcntl.h>
  32. #include <unistd.h>
  33. #include <stdbool.h>
  34. #include <stdint.h>
  35. #include <stdlib.h>
  36. #include <stdio.h>
  37. #include <dpkg/i18n.h>
  38. #include <dpkg/dpkg.h>
  39. #include <dpkg/dpkg-db.h>
  40. #include <dpkg/path.h>
  41. #include <dpkg/varbuf.h>
  42. #include <dpkg/fdio.h>
  43. #include <dpkg/buffer.h>
  44. #include <dpkg/subproc.h>
  45. #include <dpkg/compress.h>
  46. #include <dpkg/ar.h>
  47. #include <dpkg/options.h>
  48. #include "dpkg-deb.h"
  49. /**
  50. * Simple structure to store information about a file.
  51. */
  52. struct file_info {
  53. struct file_info *next;
  54. struct stat st;
  55. char* fn;
  56. };
  57. static struct file_info *
  58. file_info_new(const char *filename)
  59. {
  60. struct file_info *fi;
  61. fi = m_malloc(sizeof(*fi));
  62. fi->fn = m_strdup(filename);
  63. fi->next = NULL;
  64. return fi;
  65. }
  66. static void
  67. file_info_free(struct file_info *fi)
  68. {
  69. free(fi->fn);
  70. free(fi);
  71. }
  72. static struct file_info *
  73. file_info_find_name(struct file_info *list, const char *filename)
  74. {
  75. struct file_info *node;
  76. for (node = list; node; node = node->next)
  77. if (strcmp(node->fn, filename) == 0)
  78. return node;
  79. return NULL;
  80. }
  81. /**
  82. * Read a filename from the file descriptor and create a file_info struct.
  83. *
  84. * @return A file_info struct or NULL if there is nothing to read.
  85. */
  86. static struct file_info *
  87. file_info_get(const char *root, int fd)
  88. {
  89. static struct varbuf fn = VARBUF_INIT;
  90. struct file_info *fi;
  91. size_t root_len;
  92. varbuf_reset(&fn);
  93. root_len = varbuf_printf(&fn, "%s/", root);
  94. while (1) {
  95. int res;
  96. varbuf_grow(&fn, 1);
  97. res = fd_read(fd, (fn.buf + fn.used), 1);
  98. if (res < 0)
  99. return NULL;
  100. if (res == 0) /* EOF -> parent died. */
  101. return NULL;
  102. if (fn.buf[fn.used] == '\0')
  103. break;
  104. varbuf_trunc(&fn, fn.used + 1);
  105. if (fn.used >= MAXFILENAME)
  106. ohshit(_("file name '%.50s...' is too long"), fn.buf + root_len);
  107. }
  108. fi = file_info_new(fn.buf + root_len);
  109. if (lstat(fn.buf, &(fi->st)) != 0)
  110. ohshite(_("unable to stat file name '%.250s'"), fn.buf);
  111. return fi;
  112. }
  113. /**
  114. * Add a new file_info struct to a single linked list of file_info structs.
  115. *
  116. * We perform a slight optimization to work around a ‘feature’ in tar: tar
  117. * always recurses into subdirectories if you list a subdirectory. So if an
  118. * entry is added and the previous entry in the list is its subdirectory we
  119. * remove the subdirectory.
  120. *
  121. * After a file_info struct is added to a list it may no longer be freed, we
  122. * assume full responsibility for its memory.
  123. */
  124. static void
  125. file_info_list_append(struct file_info **head, struct file_info **tail,
  126. struct file_info *fi)
  127. {
  128. if (*head == NULL)
  129. *head = *tail = fi;
  130. else
  131. *tail = (*tail)->next =fi;
  132. }
  133. /**
  134. * Free the memory for all entries in a list of file_info structs.
  135. */
  136. static void
  137. file_info_list_free(struct file_info *fi)
  138. {
  139. while (fi) {
  140. struct file_info *fl;
  141. fl=fi; fi=fi->next;
  142. file_info_free(fl);
  143. }
  144. }
  145. static const char *const maintainerscripts[] = {
  146. PREINSTFILE,
  147. POSTINSTFILE,
  148. PRERMFILE,
  149. POSTRMFILE,
  150. NULL,
  151. };
  152. /**
  153. * Check control directory and file permissions.
  154. */
  155. static void
  156. check_file_perms(const char *dir)
  157. {
  158. struct varbuf path = VARBUF_INIT;
  159. const char *const *mscriptp;
  160. struct stat mscriptstab;
  161. varbuf_printf(&path, "%s/%s/", dir, BUILDCONTROLDIR);
  162. if (lstat(path.buf, &mscriptstab))
  163. ohshite(_("unable to stat control directory"));
  164. if (!S_ISDIR(mscriptstab.st_mode))
  165. ohshit(_("control directory is not a directory"));
  166. if ((mscriptstab.st_mode & 07757) != 0755)
  167. ohshit(_("control directory has bad permissions %03lo "
  168. "(must be >=0755 and <=0775)"),
  169. (unsigned long)(mscriptstab.st_mode & 07777));
  170. for (mscriptp = maintainerscripts; *mscriptp; mscriptp++) {
  171. varbuf_reset(&path);
  172. varbuf_printf(&path, "%s/%s/%s", dir, BUILDCONTROLDIR, *mscriptp);
  173. if (!lstat(path.buf, &mscriptstab)) {
  174. if (S_ISLNK(mscriptstab.st_mode))
  175. continue;
  176. if (!S_ISREG(mscriptstab.st_mode))
  177. ohshit(_("maintainer script `%.50s' is not a plain file or symlink"),
  178. *mscriptp);
  179. if ((mscriptstab.st_mode & 07557) != 0555)
  180. ohshit(_("maintainer script `%.50s' has bad permissions %03lo "
  181. "(must be >=0555 and <=0775)"),
  182. *mscriptp, (unsigned long)(mscriptstab.st_mode & 07777));
  183. } else if (errno != ENOENT) {
  184. ohshite(_("maintainer script `%.50s' is not stattable"), *mscriptp);
  185. }
  186. }
  187. varbuf_destroy(&path);
  188. }
  189. /**
  190. * Check if conffiles contains sane information.
  191. */
  192. static void
  193. check_conffiles(const char *dir)
  194. {
  195. FILE *cf;
  196. struct varbuf controlfile = VARBUF_INIT;
  197. char conffilename[MAXCONFFILENAME + 1];
  198. struct file_info *conffiles_head = NULL;
  199. struct file_info *conffiles_tail = NULL;
  200. varbuf_printf(&controlfile, "%s/%s/%s", dir, BUILDCONTROLDIR, CONFFILESFILE);
  201. cf = fopen(controlfile.buf, "r");
  202. if (cf == NULL) {
  203. if (errno == ENOENT)
  204. return;
  205. ohshite(_("error opening conffiles file"));
  206. }
  207. while (fgets(conffilename, MAXCONFFILENAME + 1, cf)) {
  208. struct stat controlstab;
  209. int n;
  210. n = strlen(conffilename);
  211. if (!n)
  212. ohshite(_("empty string from fgets reading conffiles"));
  213. if (conffilename[n - 1] != '\n') {
  214. int c;
  215. warning(_("conffile name '%.50s...' is too long, or missing final newline"),
  216. conffilename);
  217. while ((c = getc(cf)) != EOF && c != '\n');
  218. continue;
  219. }
  220. conffilename[n - 1] = '\0';
  221. varbuf_reset(&controlfile);
  222. varbuf_printf(&controlfile, "%s/%s", dir, conffilename);
  223. if (lstat(controlfile.buf, &controlstab)) {
  224. if (errno == ENOENT) {
  225. if ((n > 1) && isspace(conffilename[n - 2]))
  226. warning(_("conffile filename '%s' contains trailing white spaces"),
  227. conffilename);
  228. ohshit(_("conffile `%.250s' does not appear in package"), conffilename);
  229. } else
  230. ohshite(_("conffile `%.250s' is not stattable"), conffilename);
  231. } else if (!S_ISREG(controlstab.st_mode)) {
  232. warning(_("conffile '%s' is not a plain file"), conffilename);
  233. }
  234. if (file_info_find_name(conffiles_head, conffilename)) {
  235. warning(_("conffile name '%s' is duplicated"), conffilename);
  236. } else {
  237. struct file_info *conffile;
  238. conffile = file_info_new(conffilename);
  239. file_info_list_append(&conffiles_head, &conffiles_tail, conffile);
  240. }
  241. }
  242. file_info_list_free(conffiles_head);
  243. varbuf_destroy(&controlfile);
  244. if (ferror(cf))
  245. ohshite(_("error reading conffiles file"));
  246. fclose(cf);
  247. }
  248. static const char *arbitrary_fields[] = {
  249. "Built-Using",
  250. "Package-Type",
  251. "Subarchitecture",
  252. "Kernel-Version",
  253. "Installer-Menu-Item",
  254. "Homepage",
  255. "Tag",
  256. NULL
  257. };
  258. static const char private_prefix[] = "Private-";
  259. static bool
  260. known_arbitrary_field(const struct arbitraryfield *field)
  261. {
  262. const char **known;
  263. /* Always accept fields starting with a private field prefix. */
  264. if (strncasecmp(field->name, private_prefix, strlen(private_prefix)) == 0)
  265. return true;
  266. for (known = arbitrary_fields; *known; known++)
  267. if (strcasecmp(field->name, *known) == 0)
  268. return true;
  269. return false;
  270. }
  271. /**
  272. * Perform some sanity checks on the to-be-built package.
  273. *
  274. * @return The pkginfo struct from the parsed control file.
  275. */
  276. static struct pkginfo *
  277. check_new_pkg(const char *dir)
  278. {
  279. struct pkginfo *pkg;
  280. struct arbitraryfield *field;
  281. char *controlfile;
  282. int warns;
  283. /* Start by reading in the control file so we can check its contents. */
  284. m_asprintf(&controlfile, "%s/%s/%s", dir, BUILDCONTROLDIR, CONTROLFILE);
  285. parsedb(controlfile, pdb_recordavailable | pdb_rejectstatus, &pkg);
  286. if (strspn(pkg->name, "abcdefghijklmnopqrstuvwxyz0123456789+-.") !=
  287. strlen(pkg->name))
  288. ohshit(_("package name has characters that aren't lowercase alphanums or `-+.'"));
  289. if (pkg->priority == pri_other)
  290. warning(_("'%s' contains user-defined Priority value '%s'"),
  291. controlfile, pkg->otherpriority);
  292. for (field = pkg->available.arbs; field; field = field->next) {
  293. if (known_arbitrary_field(field))
  294. continue;
  295. warning(_("'%s' contains user-defined field '%s'"), controlfile,
  296. field->name);
  297. }
  298. free(controlfile);
  299. check_file_perms(dir);
  300. check_conffiles(dir);
  301. warns = warning_get_count();
  302. if (warns)
  303. warning(P_("ignoring %d warning about the control file(s)\n",
  304. "ignoring %d warnings about the control file(s)\n", warns),
  305. warns);
  306. return pkg;
  307. }
  308. /**
  309. * Generate the pathname for the to-be-built package.
  310. *
  311. * @return The pathname for the package being built.
  312. */
  313. static char *
  314. pkg_get_pathname(const char *dir, struct pkginfo *pkg)
  315. {
  316. char *path;
  317. const char *versionstring, *arch_sep;
  318. versionstring = versiondescribe(&pkg->available.version, vdew_never);
  319. arch_sep = pkg->available.arch[0] == '\0' ? "" : "_";
  320. m_asprintf(&path, "%s/%s_%s%s%s%s", dir, pkg->name, versionstring,
  321. arch_sep, pkg->available.arch, DEBEXT);
  322. return path;
  323. }
  324. /**
  325. * Overly complex function that builds a .deb file.
  326. */
  327. int
  328. do_build(const char *const *argv)
  329. {
  330. const char *debar, *dir;
  331. bool subdir;
  332. char *tfbuf;
  333. int arfd;
  334. int p1[2], p2[2], p3[2], gzfd;
  335. pid_t c1,c2,c3;
  336. struct file_info *fi;
  337. struct file_info *symlist = NULL;
  338. struct file_info *symlist_end = NULL;
  339. /* Decode our arguments. */
  340. dir = *argv++;
  341. if (!dir)
  342. badusage(_("--%s needs a <directory> argument"), cipaction->olong);
  343. subdir = false;
  344. debar = *argv++;
  345. if (debar != NULL) {
  346. struct stat debarstab;
  347. if (*argv) badusage(_("--build takes at most two arguments"));
  348. if (stat(debar, &debarstab)) {
  349. if (errno != ENOENT)
  350. ohshite(_("unable to check for existence of archive `%.250s'"), debar);
  351. } else if (S_ISDIR(debarstab.st_mode)) {
  352. subdir = true;
  353. }
  354. } else {
  355. char *m;
  356. m= m_malloc(strlen(dir) + sizeof(DEBEXT));
  357. strcpy(m, dir);
  358. path_trim_slash_slashdot(m);
  359. strcat(m, DEBEXT);
  360. debar= m;
  361. }
  362. /* Perform some sanity checks on the to-be-build package. */
  363. if (nocheckflag) {
  364. if (subdir)
  365. ohshit(_("target is directory - cannot skip control file check"));
  366. warning(_("not checking contents of control area."));
  367. printf(_("dpkg-deb: building an unknown package in '%s'.\n"), debar);
  368. } else {
  369. struct pkginfo *pkg;
  370. pkg = check_new_pkg(dir);
  371. if (subdir)
  372. debar = pkg_get_pathname(debar, pkg);
  373. printf(_("dpkg-deb: building package `%s' in `%s'.\n"), pkg->name, debar);
  374. }
  375. m_output(stdout, _("<standard output>"));
  376. /* Now that we have verified everything its time to actually
  377. * build something. Let's start by making the ar-wrapper. */
  378. arfd = creat(debar, 0644);
  379. if (arfd < 0)
  380. ohshite(_("unable to create `%.255s'"), debar);
  381. /* Fork a tar to package the control-section of the package. */
  382. unsetenv("TAR_OPTIONS");
  383. m_pipe(p1);
  384. c1 = subproc_fork();
  385. if (!c1) {
  386. m_dup2(p1[1],1); close(p1[0]); close(p1[1]);
  387. if (chdir(dir))
  388. ohshite(_("failed to chdir to `%.255s'"), dir);
  389. if (chdir(BUILDCONTROLDIR))
  390. ohshite(_("failed to chdir to `%.255s'"), ".../DEBIAN");
  391. execlp(TAR, "tar", "-cf", "-", "--format=gnu", ".", NULL);
  392. ohshite(_("unable to execute %s (%s)"), "tar -cf", TAR);
  393. }
  394. close(p1[1]);
  395. /* Create a temporary file to store the control data in. Immediately
  396. * unlink our temporary file so others can't mess with it. */
  397. tfbuf = path_make_temp_template("dpkg-deb");
  398. gzfd = mkstemp(tfbuf);
  399. if (gzfd == -1)
  400. ohshite(_("failed to make temporary file (%s)"), _("control member"));
  401. /* Make sure it's gone, the fd will remain until we close it. */
  402. if (unlink(tfbuf))
  403. ohshit(_("failed to unlink temporary file (%s), %s"), _("control member"),
  404. tfbuf);
  405. free(tfbuf);
  406. /* And run gzip to compress our control archive. */
  407. c2 = subproc_fork();
  408. if (!c2) {
  409. compress_filter(&compressor_gzip, p1[0], gzfd, 9, _("control member"));
  410. exit(0);
  411. }
  412. close(p1[0]);
  413. subproc_wait_check(c2, "gzip -9c", 0);
  414. subproc_wait_check(c1, "tar -cf", 0);
  415. if (lseek(gzfd, 0, SEEK_SET))
  416. ohshite(_("failed to rewind temporary file (%s)"), _("control member"));
  417. /* We have our first file for the ar-archive. Write a header for it
  418. * to the package and insert it. */
  419. if (oldformatflag) {
  420. struct stat controlstab;
  421. char versionbuf[40];
  422. if (fstat(gzfd, &controlstab))
  423. ohshite(_("failed to stat temporary file (%s)"), _("control member"));
  424. sprintf(versionbuf, "%-8s\n%jd\n", OLDARCHIVEVERSION,
  425. (intmax_t)controlstab.st_size);
  426. if (fd_write(arfd, versionbuf, strlen(versionbuf)) < 0)
  427. ohshite(_("error writing `%s'"), debar);
  428. fd_fd_copy(gzfd, arfd, -1, _("control member"));
  429. } else {
  430. const char deb_magic[] = ARCHIVEVERSION "\n";
  431. dpkg_ar_put_magic(debar, arfd);
  432. dpkg_ar_member_put_mem(debar, arfd, DEBMAGIC, deb_magic, strlen(deb_magic));
  433. dpkg_ar_member_put_file(debar, arfd, ADMINMEMBER, gzfd, -1);
  434. }
  435. close(gzfd);
  436. /* Control is done, now we need to archive the data. */
  437. if (oldformatflag) {
  438. /* In old format, the data member is just concatenated after the
  439. * control member, so we do not need a temporary file and can use
  440. * the compression file descriptor. */
  441. gzfd = arfd;
  442. } else {
  443. /* Start by creating a new temporary file. Immediately unlink the
  444. * temporary file so others can't mess with it. */
  445. tfbuf = path_make_temp_template("dpkg-deb");
  446. gzfd = mkstemp(tfbuf);
  447. if (gzfd == -1)
  448. ohshite(_("failed to make temporary file (%s)"), _("data member"));
  449. /* Make sure it's gone, the fd will remain until we close it. */
  450. if (unlink(tfbuf))
  451. ohshit(_("failed to unlink temporary file (%s), %s"), _("data member"),
  452. tfbuf);
  453. free(tfbuf);
  454. }
  455. /* Fork off a tar. We will feed it a list of filenames on stdin later. */
  456. m_pipe(p1);
  457. m_pipe(p2);
  458. c1 = subproc_fork();
  459. if (!c1) {
  460. m_dup2(p1[0],0); close(p1[0]); close(p1[1]);
  461. m_dup2(p2[1],1); close(p2[0]); close(p2[1]);
  462. if (chdir(dir))
  463. ohshite(_("failed to chdir to `%.255s'"), dir);
  464. execlp(TAR, "tar", "-cf", "-", "--format=gnu", "--null", "-T", "-", "--no-recursion", NULL);
  465. ohshite(_("unable to execute %s (%s)"), "tar -cf", TAR);
  466. }
  467. close(p1[0]);
  468. close(p2[1]);
  469. /* Of course we should not forget to compress the archive as well. */
  470. c2 = subproc_fork();
  471. if (!c2) {
  472. close(p1[1]);
  473. compress_filter(compressor, p2[0], gzfd, compress_level, _("data member"));
  474. exit(0);
  475. }
  476. close(p2[0]);
  477. /* All the pipes are set, now lets run find, and start feeding
  478. * filenames to tar. */
  479. m_pipe(p3);
  480. c3 = subproc_fork();
  481. if (!c3) {
  482. m_dup2(p3[1],1); close(p3[0]); close(p3[1]);
  483. if (chdir(dir))
  484. ohshite(_("failed to chdir to `%.255s'"), dir);
  485. execlp(FIND, "find", ".", "-path", "./" BUILDCONTROLDIR, "-prune", "-o",
  486. "-print0", NULL);
  487. ohshite(_("unable to execute %s (%s)"), "find", FIND);
  488. }
  489. close(p3[1]);
  490. /* We need to reorder the files so we can make sure that symlinks
  491. * will not appear before their target. */
  492. while ((fi = file_info_get(dir, p3[0])) != NULL)
  493. if (S_ISLNK(fi->st.st_mode))
  494. file_info_list_append(&symlist, &symlist_end, fi);
  495. else {
  496. if (fd_write(p1[1], fi->fn, strlen(fi->fn) + 1) < 0)
  497. ohshite(_("failed to write filename to tar pipe (%s)"),
  498. _("data member"));
  499. file_info_free(fi);
  500. }
  501. close(p3[0]);
  502. subproc_wait_check(c3, "find", 0);
  503. for (fi= symlist;fi;fi= fi->next)
  504. if (fd_write(p1[1], fi->fn, strlen(fi->fn) + 1) < 0)
  505. ohshite(_("failed to write filename to tar pipe (%s)"), _("data member"));
  506. /* All done, clean up wait for tar and gzip to finish their job. */
  507. close(p1[1]);
  508. file_info_list_free(symlist);
  509. subproc_wait_check(c2, _("<compress> from tar -cf"), 0);
  510. subproc_wait_check(c1, "tar -cf", 0);
  511. /* Okay, we have data.tar as well now, add it to the ar wrapper. */
  512. if (!oldformatflag) {
  513. char datamember[16 + 1];
  514. sprintf(datamember, "%s%s", DATAMEMBER, compressor->extension);
  515. if (lseek(gzfd, 0, SEEK_SET))
  516. ohshite(_("failed to rewind temporary file (%s)"), _("data member"));
  517. dpkg_ar_member_put_file(debar, arfd, datamember, gzfd, -1);
  518. }
  519. if (fsync(arfd))
  520. ohshite(_("unable to sync file '%s'"), debar);
  521. if (close(arfd))
  522. ohshite(_("unable to close file '%s'"), debar);
  523. return 0;
  524. }