build.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613
  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_parse_binary, &pkg);
  286. if (strspn(pkg->set->name, "abcdefghijklmnopqrstuvwxyz0123456789+-.") !=
  287. strlen(pkg->set->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->type == arch_none ? "" : "_";
  320. m_asprintf(&path, "%s/%s_%s%s%s%s", dir, pkg->set->name, versionstring,
  321. arch_sep, pkg->available.arch->name, 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)
  348. badusage(_("--%s takes at most two arguments"), cipaction->olong);
  349. if (stat(debar, &debarstab)) {
  350. if (errno != ENOENT)
  351. ohshite(_("unable to check for existence of archive `%.250s'"), debar);
  352. } else if (S_ISDIR(debarstab.st_mode)) {
  353. subdir = true;
  354. }
  355. } else {
  356. char *m;
  357. m= m_malloc(strlen(dir) + sizeof(DEBEXT));
  358. strcpy(m, dir);
  359. path_trim_slash_slashdot(m);
  360. strcat(m, DEBEXT);
  361. debar= m;
  362. }
  363. /* Perform some sanity checks on the to-be-build package. */
  364. if (nocheckflag) {
  365. if (subdir)
  366. ohshit(_("target is directory - cannot skip control file check"));
  367. warning(_("not checking contents of control area."));
  368. printf(_("dpkg-deb: building an unknown package in '%s'.\n"), debar);
  369. } else {
  370. struct pkginfo *pkg;
  371. pkg = check_new_pkg(dir);
  372. if (subdir)
  373. debar = pkg_get_pathname(debar, pkg);
  374. printf(_("dpkg-deb: building package `%s' in `%s'.\n"),
  375. pkgbin_name(pkg, &pkg->available, pnaw_nonambig), debar);
  376. }
  377. m_output(stdout, _("<standard output>"));
  378. /* Now that we have verified everything its time to actually
  379. * build something. Let's start by making the ar-wrapper. */
  380. arfd = creat(debar, 0644);
  381. if (arfd < 0)
  382. ohshite(_("unable to create `%.255s'"), debar);
  383. /* Fork a tar to package the control-section of the package. */
  384. unsetenv("TAR_OPTIONS");
  385. m_pipe(p1);
  386. c1 = subproc_fork();
  387. if (!c1) {
  388. m_dup2(p1[1],1); close(p1[0]); close(p1[1]);
  389. if (chdir(dir))
  390. ohshite(_("failed to chdir to `%.255s'"), dir);
  391. if (chdir(BUILDCONTROLDIR))
  392. ohshite(_("failed to chdir to `%.255s'"), ".../DEBIAN");
  393. execlp(TAR, "tar", "-cf", "-", "--format=gnu", ".", NULL);
  394. ohshite(_("unable to execute %s (%s)"), "tar -cf", TAR);
  395. }
  396. close(p1[1]);
  397. /* Create a temporary file to store the control data in. Immediately
  398. * unlink our temporary file so others can't mess with it. */
  399. tfbuf = path_make_temp_template("dpkg-deb");
  400. gzfd = mkstemp(tfbuf);
  401. if (gzfd == -1)
  402. ohshite(_("failed to make temporary file (%s)"), _("control member"));
  403. /* Make sure it's gone, the fd will remain until we close it. */
  404. if (unlink(tfbuf))
  405. ohshit(_("failed to unlink temporary file (%s), %s"), _("control member"),
  406. tfbuf);
  407. free(tfbuf);
  408. /* And run gzip to compress our control archive. */
  409. c2 = subproc_fork();
  410. if (!c2) {
  411. struct compress_params params;
  412. params.type = compressor_type_gzip;
  413. params.level = 9;
  414. params.strategy = NULL;
  415. compress_filter(&params, p1[0], gzfd, _("control member"));
  416. exit(0);
  417. }
  418. close(p1[0]);
  419. subproc_wait_check(c2, "gzip -9c", 0);
  420. subproc_wait_check(c1, "tar -cf", 0);
  421. if (lseek(gzfd, 0, SEEK_SET))
  422. ohshite(_("failed to rewind temporary file (%s)"), _("control member"));
  423. /* We have our first file for the ar-archive. Write a header for it
  424. * to the package and insert it. */
  425. if (oldformatflag) {
  426. struct stat controlstab;
  427. char versionbuf[40];
  428. if (fstat(gzfd, &controlstab))
  429. ohshite(_("failed to stat temporary file (%s)"), _("control member"));
  430. sprintf(versionbuf, "%-8s\n%jd\n", OLDARCHIVEVERSION,
  431. (intmax_t)controlstab.st_size);
  432. if (fd_write(arfd, versionbuf, strlen(versionbuf)) < 0)
  433. ohshite(_("error writing `%s'"), debar);
  434. fd_fd_copy(gzfd, arfd, -1, _("control member"));
  435. } else {
  436. const char deb_magic[] = ARCHIVEVERSION "\n";
  437. dpkg_ar_put_magic(debar, arfd);
  438. dpkg_ar_member_put_mem(debar, arfd, DEBMAGIC, deb_magic, strlen(deb_magic));
  439. dpkg_ar_member_put_file(debar, arfd, ADMINMEMBER, gzfd, -1);
  440. }
  441. close(gzfd);
  442. /* Control is done, now we need to archive the data. */
  443. if (oldformatflag) {
  444. /* In old format, the data member is just concatenated after the
  445. * control member, so we do not need a temporary file and can use
  446. * the compression file descriptor. */
  447. gzfd = arfd;
  448. } else {
  449. /* Start by creating a new temporary file. Immediately unlink the
  450. * temporary file so others can't mess with it. */
  451. tfbuf = path_make_temp_template("dpkg-deb");
  452. gzfd = mkstemp(tfbuf);
  453. if (gzfd == -1)
  454. ohshite(_("failed to make temporary file (%s)"), _("data member"));
  455. /* Make sure it's gone, the fd will remain until we close it. */
  456. if (unlink(tfbuf))
  457. ohshit(_("failed to unlink temporary file (%s), %s"), _("data member"),
  458. tfbuf);
  459. free(tfbuf);
  460. }
  461. /* Fork off a tar. We will feed it a list of filenames on stdin later. */
  462. m_pipe(p1);
  463. m_pipe(p2);
  464. c1 = subproc_fork();
  465. if (!c1) {
  466. m_dup2(p1[0],0); close(p1[0]); close(p1[1]);
  467. m_dup2(p2[1],1); close(p2[0]); close(p2[1]);
  468. if (chdir(dir))
  469. ohshite(_("failed to chdir to `%.255s'"), dir);
  470. execlp(TAR, "tar", "-cf", "-", "--format=gnu", "--null", "-T", "-", "--no-recursion", NULL);
  471. ohshite(_("unable to execute %s (%s)"), "tar -cf", TAR);
  472. }
  473. close(p1[0]);
  474. close(p2[1]);
  475. /* Of course we should not forget to compress the archive as well. */
  476. c2 = subproc_fork();
  477. if (!c2) {
  478. close(p1[1]);
  479. compress_filter(&compress_params, p2[0], gzfd, _("data member"));
  480. exit(0);
  481. }
  482. close(p2[0]);
  483. /* All the pipes are set, now lets run find, and start feeding
  484. * filenames to tar. */
  485. m_pipe(p3);
  486. c3 = subproc_fork();
  487. if (!c3) {
  488. m_dup2(p3[1],1); close(p3[0]); close(p3[1]);
  489. if (chdir(dir))
  490. ohshite(_("failed to chdir to `%.255s'"), dir);
  491. execlp(FIND, "find", ".", "-path", "./" BUILDCONTROLDIR, "-prune", "-o",
  492. "-print0", NULL);
  493. ohshite(_("unable to execute %s (%s)"), "find", FIND);
  494. }
  495. close(p3[1]);
  496. /* We need to reorder the files so we can make sure that symlinks
  497. * will not appear before their target. */
  498. while ((fi = file_info_get(dir, p3[0])) != NULL)
  499. if (S_ISLNK(fi->st.st_mode))
  500. file_info_list_append(&symlist, &symlist_end, fi);
  501. else {
  502. if (fd_write(p1[1], fi->fn, strlen(fi->fn) + 1) < 0)
  503. ohshite(_("failed to write filename to tar pipe (%s)"),
  504. _("data member"));
  505. file_info_free(fi);
  506. }
  507. close(p3[0]);
  508. subproc_wait_check(c3, "find", 0);
  509. for (fi= symlist;fi;fi= fi->next)
  510. if (fd_write(p1[1], fi->fn, strlen(fi->fn) + 1) < 0)
  511. ohshite(_("failed to write filename to tar pipe (%s)"), _("data member"));
  512. /* All done, clean up wait for tar and gzip to finish their job. */
  513. close(p1[1]);
  514. file_info_list_free(symlist);
  515. subproc_wait_check(c2, _("<compress> from tar -cf"), 0);
  516. subproc_wait_check(c1, "tar -cf", 0);
  517. /* Okay, we have data.tar as well now, add it to the ar wrapper. */
  518. if (!oldformatflag) {
  519. char datamember[16 + 1];
  520. sprintf(datamember, "%s%s", DATAMEMBER,
  521. compressor_get_extension(compress_params.type));
  522. if (lseek(gzfd, 0, SEEK_SET))
  523. ohshite(_("failed to rewind temporary file (%s)"), _("data member"));
  524. dpkg_ar_member_put_file(debar, arfd, datamember, gzfd, -1);
  525. }
  526. if (fsync(arfd))
  527. ohshite(_("unable to sync file '%s'"), debar);
  528. if (close(arfd))
  529. ohshite(_("unable to close file '%s'"), debar);
  530. return 0;
  531. }