statdb.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. /*
  2. * dpkg - main program for package management
  3. * statdb.c - management of database of ownership and mode of files
  4. *
  5. * Copyright © 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
  10. * published by the Free Software Foundation; either version 2,
  11. * or (at your option) any later version.
  12. *
  13. * This is distributed in the hope that it will be useful, but
  14. * 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
  19. * License along with dpkg; if not, write to the Free Software
  20. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  21. */
  22. #include <config.h>
  23. #include <compat.h>
  24. #include <dpkg-i18n.h>
  25. #include <stdlib.h>
  26. #include <unistd.h>
  27. #include <fcntl.h>
  28. #include <string.h>
  29. #include <errno.h>
  30. #include <sys/stat.h>
  31. #include <pwd.h>
  32. #include <grp.h>
  33. #include <sys/types.h>
  34. #include <dpkg.h>
  35. #include <dpkg-db.h>
  36. #include "filesdb.h"
  37. #include "main.h"
  38. static FILE *statoverridefile = NULL;
  39. uid_t
  40. statdb_parse_uid(const char *str)
  41. {
  42. char* endptr;
  43. uid_t uid;
  44. if (str[0] == '#') {
  45. uid = strtol(str + 1, &endptr, 10);
  46. if (str + 1 == endptr || *endptr)
  47. ohshit(_("syntax error: invalid uid in statoverride file"));
  48. } else {
  49. struct passwd* pw = getpwnam(str);
  50. if (pw == NULL)
  51. ohshit(_("syntax error: unknown user '%s' in statoverride file"),
  52. str);
  53. uid = pw->pw_uid;
  54. }
  55. return uid;
  56. }
  57. gid_t
  58. statdb_parse_gid(const char *str)
  59. {
  60. char* endptr;
  61. gid_t gid;
  62. if (str[0] == '#') {
  63. gid = strtol(str + 1, &endptr, 10);
  64. if (str + 1 == endptr || *endptr)
  65. ohshit(_("syntax error: invalid gid in statoverride file"));
  66. } else {
  67. struct group* gr = getgrnam(str);
  68. if (gr == NULL)
  69. ohshit(_("syntax error: unknown group '%s' in statoverride file"),
  70. str);
  71. gid = gr->gr_gid;
  72. }
  73. return gid;
  74. }
  75. mode_t
  76. statdb_parse_mode(const char *str)
  77. {
  78. char* endptr;
  79. mode_t mode;
  80. mode = strtol(str, &endptr, 8);
  81. if (str == endptr || *endptr)
  82. ohshit(_("syntax error: invalid mode in statoverride file"));
  83. return mode;
  84. }
  85. void
  86. ensure_statoverrides(void)
  87. {
  88. static struct varbuf vb;
  89. struct stat stab1, stab2;
  90. FILE *file;
  91. char *loaded_list, *loaded_list_end, *thisline, *nextline, *ptr;
  92. struct filestatoverride *fso;
  93. struct filenamenode *fnn;
  94. varbufreset(&vb);
  95. varbufaddstr(&vb, admindir);
  96. varbufaddstr(&vb, "/" STATOVERRIDEFILE);
  97. varbufaddc(&vb, 0);
  98. onerr_abort++;
  99. file = fopen(vb.buf,"r");
  100. if (!file) {
  101. if (errno != ENOENT)
  102. ohshite(_("failed to open statoverride file"));
  103. if (!statoverridefile) {
  104. onerr_abort--;
  105. return;
  106. }
  107. } else {
  108. if (fstat(fileno(file), &stab2))
  109. ohshite(_("failed to fstat statoverride file"));
  110. if (statoverridefile) {
  111. if (fstat(fileno(statoverridefile), &stab1))
  112. ohshite(_("failed to fstat previous statoverride file"));
  113. if (stab1.st_dev == stab2.st_dev &&
  114. stab1.st_ino == stab2.st_ino) {
  115. fclose(file);
  116. onerr_abort--;
  117. return;
  118. }
  119. }
  120. }
  121. if (statoverridefile)
  122. fclose(statoverridefile);
  123. statoverridefile = file;
  124. setcloexec(fileno(statoverridefile), vb.buf);
  125. /* If the statoverride list is empty we don't need to bother
  126. * reading it. */
  127. if (!stab2.st_size) {
  128. onerr_abort--;
  129. return;
  130. }
  131. loaded_list = nfmalloc(stab2.st_size);
  132. loaded_list_end = loaded_list + stab2.st_size;
  133. fd_buf_copy(fileno(file), loaded_list, stab2.st_size,
  134. _("statoverride file `%.250s'"), vb.buf);
  135. thisline = loaded_list;
  136. while (thisline < loaded_list_end) {
  137. fso = nfmalloc(sizeof(struct filestatoverride));
  138. if (!(ptr = memchr(thisline, '\n', loaded_list_end - thisline)))
  139. ohshit(_("statoverride file is missing final newline"));
  140. /* Where to start next time around. */
  141. nextline = ptr + 1;
  142. if (ptr == thisline)
  143. ohshit(_("statoverride file contains empty line"));
  144. *ptr = '\0';
  145. /* Extract the uid. */
  146. if (!(ptr = memchr(thisline, ' ', nextline - thisline)))
  147. ohshit(_("syntax error in statoverride file"));
  148. *ptr = '\0';
  149. fso->uid = statdb_parse_uid(thisline);
  150. /* Move to the next bit */
  151. thisline = ptr + 1;
  152. if (thisline >= loaded_list_end)
  153. ohshit(_("unexpected end of line in statoverride file"));
  154. /* Extract the gid */
  155. if (!(ptr = memchr(thisline, ' ', nextline - thisline)))
  156. ohshit(_("syntax error in statoverride file"));
  157. *ptr = '\0';
  158. fso->gid = statdb_parse_gid(thisline);
  159. /* Move to the next bit */
  160. thisline = ptr + 1;
  161. if (thisline >= loaded_list_end)
  162. ohshit(_("unexpected end of line in statoverride file"));
  163. /* Extract the mode */
  164. if (!(ptr = memchr(thisline, ' ', nextline - thisline)))
  165. ohshit(_("syntax error in statoverride file"));
  166. *ptr = '\0';
  167. fso->mode = statdb_parse_mode(thisline);
  168. /* Move to the next bit */
  169. thisline = ptr + 1;
  170. if (thisline >= loaded_list_end)
  171. ohshit(_("unexpected end of line in statoverride file"));
  172. fnn = findnamenode(thisline, 0);
  173. if (fnn->statoverride)
  174. ohshit(_("multiple statusoverides present for file '%.250s'"),
  175. thisline);
  176. fnn->statoverride = fso;
  177. /* Moving on.. */
  178. thisline = nextline;
  179. }
  180. onerr_abort--;
  181. }