build.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572
  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 <assert.h>
  27. #include <errno.h>
  28. #include <limits.h>
  29. #include <ctype.h>
  30. #include <string.h>
  31. #include <time.h>
  32. #include <dirent.h>
  33. #include <unistd.h>
  34. #include <stdbool.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/buffer.h>
  42. #include <dpkg/subproc.h>
  43. #include <dpkg/compress.h>
  44. #include <dpkg/myopt.h>
  45. #include "dpkg-deb.h"
  46. /* Simple structure to store information about a file.
  47. */
  48. struct file_info {
  49. struct file_info *next;
  50. struct stat st;
  51. char* fn;
  52. };
  53. static const char *arbitrary_fields[] = {
  54. "Package-Type",
  55. "Subarchitecture",
  56. "Kernel-Version",
  57. "Installer-Menu-Item",
  58. "Homepage",
  59. "Tag",
  60. NULL
  61. };
  62. static const char private_prefix[] = "Private-";
  63. static bool
  64. known_arbitrary_field(const struct arbitraryfield *field)
  65. {
  66. const char **known;
  67. /* Always accept fields starting with a private field prefix. */
  68. if (strncasecmp(field->name, private_prefix, strlen(private_prefix)) == 0)
  69. return true;
  70. for (known= arbitrary_fields; *known; known++)
  71. if (strcasecmp(field->name, *known) == 0)
  72. return true;
  73. return false;
  74. }
  75. /* Do a quick check if vstring is a valid versionnumber. Valid in this case
  76. * means it contains at least one digit. If an error is found increment
  77. * *errs.
  78. */
  79. static void checkversion(const char *vstring, const char *valuename, int *errs) {
  80. const char *p;
  81. if (!vstring || !*vstring) return;
  82. for (p=vstring; *p; p++) if (cisdigit(*p)) return;
  83. fprintf(stderr, _("dpkg-deb - error: %s (`%s') doesn't contain any digits\n"),
  84. valuename, vstring);
  85. (*errs)++;
  86. }
  87. static struct file_info *
  88. file_info_new(const char *filename)
  89. {
  90. struct file_info *fi;
  91. fi = m_malloc(sizeof(*fi));
  92. fi->fn = m_strdup(filename);
  93. fi->next = NULL;
  94. return fi;
  95. }
  96. static void
  97. file_info_free(struct file_info *fi)
  98. {
  99. free(fi->fn);
  100. free(fi);
  101. }
  102. static struct file_info *
  103. file_info_find_name(struct file_info *list, const char *filename)
  104. {
  105. struct file_info *node;
  106. for (node = list; node; node = node->next)
  107. if (strcmp(node->fn, filename) == 0)
  108. return node;
  109. return NULL;
  110. }
  111. /*
  112. * Read the next filename from a filedescriptor and create a file_info struct
  113. * for it. If there is nothing to read return NULL.
  114. */
  115. static struct file_info *
  116. getfi(const char *root, int fd)
  117. {
  118. static char* fn = NULL;
  119. static size_t fnlen = 0;
  120. size_t i= 0;
  121. struct file_info *fi;
  122. size_t rl = strlen(root);
  123. if (fn == NULL) {
  124. fnlen = rl + MAXFILENAME;
  125. fn = m_malloc(fnlen);
  126. } else if (fnlen < (rl + MAXFILENAME)) {
  127. fnlen = rl + MAXFILENAME;
  128. fn = m_realloc(fn, fnlen);
  129. }
  130. i=sprintf(fn,"%s/",root);
  131. while (1) {
  132. int res;
  133. if (i>=fnlen) {
  134. fnlen += MAXFILENAME;
  135. fn = m_realloc(fn, fnlen);
  136. }
  137. if ((res=read(fd, (fn+i), sizeof(*fn)))<0) {
  138. if ((errno==EINTR) || (errno==EAGAIN))
  139. continue;
  140. else
  141. return NULL;
  142. }
  143. if (res == 0) /* EOF -> parent died. */
  144. return NULL;
  145. if (fn[i] == '\0')
  146. break;
  147. i++;
  148. if (i >= MAXFILENAME)
  149. ohshit(_("file name '%.50s...' is too long"), fn + rl + 1);
  150. }
  151. fi = file_info_new(fn + rl + 1);
  152. if (lstat(fn, &(fi->st)) != 0)
  153. ohshite(_("unable to stat file name '%.250s'"), fn);
  154. return fi;
  155. }
  156. /*
  157. * Add a new file_info struct to a single linked list of file_info structs.
  158. * We perform a slight optimization to work around a `feature' in tar: tar
  159. * always recurses into subdirectories if you list a subdirectory. So if an
  160. * entry is added and the previous entry in the list is its subdirectory we
  161. * remove the subdirectory.
  162. *
  163. * After a file_info struct is added to a list it may no longer be freed, we
  164. * assume full responsibility for its memory.
  165. */
  166. static void
  167. add_to_filist(struct file_info **start, struct file_info **end,
  168. struct file_info *fi)
  169. {
  170. if (*start==NULL)
  171. *start=*end=fi;
  172. else
  173. *end=(*end)->next=fi;
  174. }
  175. /*
  176. * Free the memory for all entries in a list of file_info structs.
  177. */
  178. static void
  179. free_filist(struct file_info *fi)
  180. {
  181. while (fi) {
  182. struct file_info *fl;
  183. fl=fi; fi=fi->next;
  184. file_info_free(fl);
  185. }
  186. }
  187. /* Overly complex function that builds a .deb file
  188. */
  189. void do_build(const char *const *argv) {
  190. static const char *const maintainerscripts[]= {
  191. PREINSTFILE, POSTINSTFILE, PRERMFILE, POSTRMFILE, NULL
  192. };
  193. char *m;
  194. const char *debar, *directory, *const *mscriptp, *versionstring, *arch;
  195. char *controlfile, *tfbuf;
  196. struct pkginfo *checkedinfo;
  197. struct arbitraryfield *field;
  198. FILE *ar, *cf;
  199. int p1[2],p2[2],p3[2], warns, errs, n, c, subdir, gzfd;
  200. pid_t c1,c2,c3;
  201. struct stat controlstab, datastab, mscriptstab, debarstab;
  202. char conffilename[MAXCONFFILENAME+1];
  203. time_t thetime= 0;
  204. struct file_info *fi;
  205. struct file_info *symlist = NULL;
  206. struct file_info *symlist_end = NULL;
  207. /* Decode our arguments */
  208. directory = *argv++;
  209. if (!directory)
  210. badusage(_("--%s needs a <directory> argument"), cipaction->olong);
  211. subdir= 0;
  212. debar = *argv++;
  213. if (debar != NULL) {
  214. if (*argv) badusage(_("--build takes at most two arguments"));
  215. if (debar) {
  216. if (stat(debar,&debarstab)) {
  217. if (errno != ENOENT)
  218. ohshite(_("unable to check for existence of archive `%.250s'"),debar);
  219. } else if (S_ISDIR(debarstab.st_mode)) {
  220. subdir= 1;
  221. }
  222. }
  223. } else {
  224. m= m_malloc(strlen(directory) + sizeof(DEBEXT));
  225. strcpy(m, directory);
  226. path_rtrim_slash_slashdot(m);
  227. strcat(m, DEBEXT);
  228. debar= m;
  229. }
  230. /* Perform some sanity checks on the to-be-build package.
  231. */
  232. if (nocheckflag) {
  233. if (subdir)
  234. ohshit(_("target is directory - cannot skip control file check"));
  235. warning(_("not checking contents of control area."));
  236. printf(_("dpkg-deb: building an unknown package in '%s'.\n"), debar);
  237. } else {
  238. controlfile= m_malloc(strlen(directory) + sizeof(BUILDCONTROLDIR) +
  239. sizeof(CONTROLFILE) + sizeof(CONFFILESFILE) +
  240. sizeof(POSTINSTFILE) + sizeof(PREINSTFILE) +
  241. sizeof(POSTRMFILE) + sizeof(PRERMFILE) +
  242. MAXCONFFILENAME + 5);
  243. /* Lets start by reading in the control-file so we can check its contents */
  244. strcpy(controlfile, directory);
  245. strcat(controlfile, "/" BUILDCONTROLDIR "/" CONTROLFILE);
  246. warns= 0; errs= 0;
  247. parsedb(controlfile, pdb_recordavailable|pdb_rejectstatus,
  248. &checkedinfo, stderr, &warns);
  249. assert(checkedinfo->available.valid);
  250. if (strspn(checkedinfo->name,
  251. "abcdefghijklmnopqrstuvwxyz0123456789+-.")
  252. != strlen(checkedinfo->name))
  253. ohshit(_("package name has characters that aren't lowercase alphanums or `-+.'"));
  254. if (checkedinfo->priority == pri_other) {
  255. warning(_("'%s' contains user-defined Priority value '%s'"),
  256. controlfile, checkedinfo->otherpriority);
  257. warns++;
  258. }
  259. for (field= checkedinfo->available.arbs; field; field= field->next) {
  260. if (known_arbitrary_field(field))
  261. continue;
  262. warning(_("'%s' contains user-defined field '%s'"),
  263. controlfile, field->name);
  264. warns++;
  265. }
  266. checkversion(checkedinfo->available.version.version,
  267. _("(upstream) version"), &errs);
  268. checkversion(checkedinfo->available.version.revision,
  269. _("Debian revision"), &errs);
  270. if (errs) ohshit(_("%d errors in control file"),errs);
  271. if (subdir) {
  272. versionstring= versiondescribe(&checkedinfo->available.version,vdew_never);
  273. arch= checkedinfo->available.architecture; if (!arch) arch= "";
  274. m= m_malloc(sizeof(DEBEXT)+1+strlen(debar)+1+strlen(checkedinfo->name)+
  275. strlen(versionstring)+1+strlen(arch));
  276. sprintf(m,"%s/%s_%s%s%s" DEBEXT,debar,checkedinfo->name,versionstring,
  277. arch[0] ? "_" : "", arch);
  278. debar= m;
  279. }
  280. printf(_("dpkg-deb: building package `%s' in `%s'.\n"), checkedinfo->name, debar);
  281. /* Check file permissions */
  282. strcpy(controlfile, directory);
  283. strcat(controlfile, "/" BUILDCONTROLDIR "/");
  284. if (lstat(controlfile, &mscriptstab))
  285. ohshite(_("unable to stat control directory"));
  286. if (!S_ISDIR(mscriptstab.st_mode))
  287. ohshit(_("control directory is not a directory"));
  288. if ((mscriptstab.st_mode & 07757) != 0755)
  289. ohshit(_("control directory has bad permissions %03lo (must be >=0755 "
  290. "and <=0775)"), (unsigned long)(mscriptstab.st_mode & 07777));
  291. for (mscriptp= maintainerscripts; *mscriptp; mscriptp++) {
  292. strcpy(controlfile, directory);
  293. strcat(controlfile, "/" BUILDCONTROLDIR "/");
  294. strcat(controlfile, *mscriptp);
  295. if (!lstat(controlfile,&mscriptstab)) {
  296. if (S_ISLNK(mscriptstab.st_mode)) continue;
  297. if (!S_ISREG(mscriptstab.st_mode))
  298. ohshit(_("maintainer script `%.50s' is not a plain file or symlink"),*mscriptp);
  299. if ((mscriptstab.st_mode & 07557) != 0555)
  300. ohshit(_("maintainer script `%.50s' has bad permissions %03lo "
  301. "(must be >=0555 and <=0775)"),
  302. *mscriptp, (unsigned long)(mscriptstab.st_mode & 07777));
  303. } else if (errno != ENOENT) {
  304. ohshite(_("maintainer script `%.50s' is not stattable"),*mscriptp);
  305. }
  306. }
  307. /* Check if conffiles contains sane information */
  308. strcpy(controlfile, directory);
  309. strcat(controlfile, "/" BUILDCONTROLDIR "/" CONFFILESFILE);
  310. if ((cf= fopen(controlfile,"r"))) {
  311. struct file_info *conffiles_head = NULL;
  312. struct file_info *conffiles_tail = NULL;
  313. while (fgets(conffilename,MAXCONFFILENAME+1,cf)) {
  314. n= strlen(conffilename);
  315. if (!n) ohshite(_("empty string from fgets reading conffiles"));
  316. if (conffilename[n-1] != '\n') {
  317. warning(_("conffile name '%.50s...' is too long, or missing final newline"),
  318. conffilename);
  319. warns++;
  320. while ((c= getc(cf)) != EOF && c != '\n');
  321. continue;
  322. }
  323. conffilename[n - 1] = '\0';
  324. strcpy(controlfile, directory);
  325. strcat(controlfile, "/");
  326. strcat(controlfile, conffilename);
  327. if (lstat(controlfile,&controlstab)) {
  328. if (errno == ENOENT) {
  329. if((n > 1) && isspace(conffilename[n-2]))
  330. warning(_("conffile filename '%s' contains trailing white spaces"),
  331. conffilename);
  332. ohshit(_("conffile `%.250s' does not appear in package"), conffilename);
  333. } else
  334. ohshite(_("conffile `%.250s' is not stattable"), conffilename);
  335. } else if (!S_ISREG(controlstab.st_mode)) {
  336. warning(_("conffile '%s' is not a plain file"), conffilename);
  337. warns++;
  338. }
  339. if (file_info_find_name(conffiles_head, conffilename))
  340. warning(_("conffile name '%s' is duplicated"), conffilename);
  341. else {
  342. struct file_info *conffile;
  343. conffile = file_info_new(conffilename);
  344. add_to_filist(&conffiles_head, &conffiles_tail, conffile);
  345. }
  346. }
  347. free_filist(conffiles_head);
  348. if (ferror(cf)) ohshite(_("error reading conffiles file"));
  349. fclose(cf);
  350. } else if (errno != ENOENT) {
  351. ohshite(_("error opening conffiles file"));
  352. }
  353. if (warns)
  354. warning(_("ignoring %d warnings about the control file(s)\n"), warns);
  355. }
  356. m_output(stdout, _("<standard output>"));
  357. /* Now that we have verified everything its time to actually
  358. * build something. Lets start by making the ar-wrapper.
  359. */
  360. if (!(ar=fopen(debar,"wb"))) ohshite(_("unable to create `%.255s'"),debar);
  361. if (setvbuf(ar, NULL, _IONBF, 0))
  362. ohshite(_("unable to unbuffer `%.255s'"), debar);
  363. /* Fork a tar to package the control-section of the package */
  364. unsetenv("TAR_OPTIONS");
  365. m_pipe(p1);
  366. c1 = subproc_fork();
  367. if (!c1) {
  368. m_dup2(p1[1],1); close(p1[0]); close(p1[1]);
  369. if (chdir(directory)) ohshite(_("failed to chdir to `%.255s'"),directory);
  370. if (chdir(BUILDCONTROLDIR)) ohshite(_("failed to chdir to .../DEBIAN"));
  371. execlp(TAR, "tar", "-cf", "-", "--format=gnu", ".", NULL);
  372. ohshite(_("failed to exec tar -cf"));
  373. }
  374. close(p1[1]);
  375. /* Create a temporary file to store the control data in. Immediately unlink
  376. * our temporary file so others can't mess with it.
  377. */
  378. tfbuf = path_make_temp_template("dpkg-deb");
  379. if ((gzfd= mkstemp(tfbuf)) == -1) ohshite(_("failed to make tmpfile (control)"));
  380. /* make sure it's gone, the fd will remain until we close it */
  381. if (unlink(tfbuf)) ohshit(_("failed to unlink tmpfile (control), %s"),
  382. tfbuf);
  383. free(tfbuf);
  384. /* And run gzip to compress our control archive */
  385. c2 = subproc_fork();
  386. if (!c2) {
  387. m_dup2(p1[0],0); m_dup2(gzfd,1); close(p1[0]); close(gzfd);
  388. compress_filter(&compressor_gzip, 0, 1, 9, _("control"));
  389. }
  390. close(p1[0]);
  391. subproc_wait_check(c2, "gzip -9c", 0);
  392. subproc_wait_check(c1, "tar -cf", 0);
  393. if (fstat(gzfd,&controlstab)) ohshite(_("failed to fstat tmpfile (control)"));
  394. /* We have our first file for the ar-archive. Write a header for it to the
  395. * package and insert it.
  396. */
  397. if (oldformatflag) {
  398. if (fprintf(ar, "%-8s\n%ld\n", OLDARCHIVEVERSION, (long)controlstab.st_size) == EOF)
  399. werr(debar);
  400. } else {
  401. thetime = time(NULL);
  402. if (fprintf(ar,
  403. "!<arch>\n"
  404. "%-16s%-12lu0 0 100644 %-10ld`\n"
  405. ARCHIVEVERSION "\n"
  406. "%s"
  407. "%-16s%-12lu0 0 100644 %-10ld`\n",
  408. DEBMAGIC,
  409. thetime,
  410. (long)sizeof(ARCHIVEVERSION),
  411. (sizeof(ARCHIVEVERSION)&1) ? "\n" : "",
  412. ADMINMEMBER,
  413. (unsigned long)thetime,
  414. (long)controlstab.st_size) == EOF)
  415. werr(debar);
  416. }
  417. if (lseek(gzfd,0,SEEK_SET)) ohshite(_("failed to rewind tmpfile (control)"));
  418. fd_fd_copy(gzfd, fileno(ar), -1, _("control"));
  419. /* Control is done, now we need to archive the data. Start by creating
  420. * a new temporary file. Immediately unlink the temporary file so others
  421. * can't mess with it. */
  422. if (!oldformatflag) {
  423. close(gzfd);
  424. tfbuf = path_make_temp_template("dpkg-deb");
  425. if ((gzfd= mkstemp(tfbuf)) == -1) ohshite(_("failed to make tmpfile (data)"));
  426. /* make sure it's gone, the fd will remain until we close it */
  427. if (unlink(tfbuf)) ohshit(_("failed to unlink tmpfile (data), %s"),
  428. tfbuf);
  429. free(tfbuf);
  430. }
  431. /* Fork off a tar. We will feed it a list of filenames on stdin later.
  432. */
  433. m_pipe(p1);
  434. m_pipe(p2);
  435. c1 = subproc_fork();
  436. if (!c1) {
  437. m_dup2(p1[0],0); close(p1[0]); close(p1[1]);
  438. m_dup2(p2[1],1); close(p2[0]); close(p2[1]);
  439. if (chdir(directory)) ohshite(_("failed to chdir to `%.255s'"),directory);
  440. execlp(TAR, "tar", "-cf", "-", "--format=gnu", "--null", "-T", "-", "--no-recursion", NULL);
  441. ohshite(_("failed to exec tar -cf"));
  442. }
  443. close(p1[0]);
  444. close(p2[1]);
  445. /* Of course we should not forget to compress the archive as well.. */
  446. c2 = subproc_fork();
  447. if (!c2) {
  448. close(p1[1]);
  449. m_dup2(p2[0],0); close(p2[0]);
  450. m_dup2(oldformatflag ? fileno(ar) : gzfd,1);
  451. compress_filter(compressor, 0, 1, compress_level, _("data"));
  452. }
  453. close(p2[0]);
  454. /* All the pipes are set, now lets run find, and start feeding
  455. * filenames to tar.
  456. */
  457. m_pipe(p3);
  458. c3 = subproc_fork();
  459. if (!c3) {
  460. m_dup2(p3[1],1); close(p3[0]); close(p3[1]);
  461. if (chdir(directory)) ohshite(_("failed to chdir to `%.255s'"),directory);
  462. execlp(FIND, "find", ".", "-path", "./" BUILDCONTROLDIR, "-prune", "-o",
  463. "-print0", NULL);
  464. ohshite(_("failed to exec find"));
  465. }
  466. close(p3[1]);
  467. /* We need to reorder the files so we can make sure that symlinks
  468. * will not appear before their target.
  469. */
  470. while ((fi=getfi(directory, p3[0]))!=NULL)
  471. if (S_ISLNK(fi->st.st_mode))
  472. add_to_filist(&symlist, &symlist_end, fi);
  473. else {
  474. if (write(p1[1], fi->fn, strlen(fi->fn)+1) ==- 1)
  475. ohshite(_("failed to write filename to tar pipe (data)"));
  476. file_info_free(fi);
  477. }
  478. close(p3[0]);
  479. subproc_wait_check(c3, "find", 0);
  480. for (fi= symlist;fi;fi= fi->next)
  481. if (write(p1[1], fi->fn, strlen(fi->fn)+1) == -1)
  482. ohshite(_("failed to write filename to tar pipe (data)"));
  483. /* All done, clean up wait for tar and gzip to finish their job */
  484. close(p1[1]);
  485. free_filist(symlist);
  486. subproc_wait_check(c2, _("<compress> from tar -cf"), 0);
  487. subproc_wait_check(c1, "tar -cf", 0);
  488. /* Okay, we have data.tar.gz as well now, add it to the ar wrapper */
  489. if (!oldformatflag) {
  490. char datamember[16 + 1];
  491. sprintf(datamember, "%s%s", DATAMEMBER, compressor->extension);
  492. if (fstat(gzfd, &datastab))
  493. ohshite(_("failed to fstat tmpfile (data)"));
  494. if (fprintf(ar,
  495. "%s"
  496. "%-16s%-12lu0 0 100644 %-10ld`\n",
  497. (controlstab.st_size & 1) ? "\n" : "",
  498. datamember,
  499. (unsigned long)thetime,
  500. (long)datastab.st_size) == EOF)
  501. werr(debar);
  502. if (lseek(gzfd,0,SEEK_SET)) ohshite(_("failed to rewind tmpfile (data)"));
  503. fd_fd_copy(gzfd, fileno(ar), -1, _("cat (data)"));
  504. if (datastab.st_size & 1)
  505. if (putc('\n',ar) == EOF)
  506. werr(debar);
  507. }
  508. if (fflush(ar))
  509. ohshite(_("unable to flush file '%s'"), debar);
  510. if (fsync(fileno(ar)))
  511. ohshite(_("unable to sync file '%s'"), debar);
  512. if (fclose(ar)) werr(debar);
  513. exit(0);
  514. }