statdb.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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. * Copyright © 2008-2010 Guillem Jover <guillem@debian.org>
  8. *
  9. * This is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. */
  22. #include <config.h>
  23. #include <compat.h>
  24. #include <sys/types.h>
  25. #include <sys/stat.h>
  26. #include <errno.h>
  27. #include <string.h>
  28. #include <pwd.h>
  29. #include <grp.h>
  30. #include <fcntl.h>
  31. #include <unistd.h>
  32. #include <stdlib.h>
  33. #include <dpkg/i18n.h>
  34. #include <dpkg/dpkg.h>
  35. #include <dpkg/dpkg-db.h>
  36. #include <dpkg/fdio.h>
  37. #include "filesdb.h"
  38. #include "main.h"
  39. static FILE *statoverridefile = NULL;
  40. static char *statoverridename;
  41. uid_t
  42. statdb_parse_uid(const char *str)
  43. {
  44. char *endptr;
  45. uid_t uid;
  46. if (str[0] == '#') {
  47. long int value;
  48. value = strtol(str + 1, &endptr, 10);
  49. if (str + 1 == endptr || *endptr || value < 0)
  50. ohshit(_("syntax error: invalid uid in statoverride file"));
  51. uid = (uid_t)value;
  52. } else {
  53. struct passwd *pw = getpwnam(str);
  54. if (pw == NULL)
  55. ohshit(_("syntax error: unknown user '%s' in statoverride file"),
  56. str);
  57. uid = pw->pw_uid;
  58. }
  59. return uid;
  60. }
  61. gid_t
  62. statdb_parse_gid(const char *str)
  63. {
  64. char *endptr;
  65. gid_t gid;
  66. if (str[0] == '#') {
  67. long int value;
  68. value = strtol(str + 1, &endptr, 10);
  69. if (str + 1 == endptr || *endptr || value < 0)
  70. ohshit(_("syntax error: invalid gid in statoverride file"));
  71. gid = (gid_t)value;
  72. } else {
  73. struct group *gr = getgrnam(str);
  74. if (gr == NULL)
  75. ohshit(_("syntax error: unknown group '%s' in statoverride file"),
  76. str);
  77. gid = gr->gr_gid;
  78. }
  79. return gid;
  80. }
  81. mode_t
  82. statdb_parse_mode(const char *str)
  83. {
  84. char *endptr;
  85. long int mode;
  86. mode = strtol(str, &endptr, 8);
  87. if (str == endptr || *endptr || mode < 0 || mode > 07777)
  88. ohshit(_("syntax error: invalid mode in statoverride file"));
  89. return (mode_t)mode;
  90. }
  91. void
  92. ensure_statoverrides(void)
  93. {
  94. struct stat stab1, stab2;
  95. FILE *file;
  96. char *loaded_list, *loaded_list_end, *thisline, *nextline, *ptr;
  97. struct file_stat *fso;
  98. struct filenamenode *fnn;
  99. if (statoverridename != NULL)
  100. free(statoverridename);
  101. statoverridename = dpkg_db_get_path(STATOVERRIDEFILE);
  102. onerr_abort++;
  103. file = fopen(statoverridename, "r");
  104. if (!file) {
  105. if (errno != ENOENT)
  106. ohshite(_("failed to open statoverride file"));
  107. if (!statoverridefile) {
  108. onerr_abort--;
  109. return;
  110. }
  111. } else {
  112. if (fstat(fileno(file), &stab2))
  113. ohshite(_("failed to fstat statoverride file"));
  114. if (statoverridefile) {
  115. if (fstat(fileno(statoverridefile), &stab1))
  116. ohshite(_("failed to fstat previous statoverride file"));
  117. if (stab1.st_dev == stab2.st_dev &&
  118. stab1.st_ino == stab2.st_ino) {
  119. fclose(file);
  120. onerr_abort--;
  121. return;
  122. }
  123. }
  124. }
  125. if (statoverridefile)
  126. fclose(statoverridefile);
  127. statoverridefile = file;
  128. setcloexec(fileno(statoverridefile), statoverridename);
  129. /* If the statoverride list is empty we don't need to bother
  130. * reading it. */
  131. if (!stab2.st_size) {
  132. onerr_abort--;
  133. return;
  134. }
  135. loaded_list = nfmalloc(stab2.st_size);
  136. loaded_list_end = loaded_list + stab2.st_size;
  137. if (fd_read(fileno(file), loaded_list, stab2.st_size) < 0)
  138. ohshite(_("reading statoverride file '%.250s'"), statoverridename);
  139. thisline = loaded_list;
  140. while (thisline < loaded_list_end) {
  141. fso = nfmalloc(sizeof(struct file_stat));
  142. if (!(ptr = memchr(thisline, '\n', loaded_list_end - thisline)))
  143. ohshit(_("statoverride file is missing final newline"));
  144. /* Where to start next time around. */
  145. nextline = ptr + 1;
  146. if (ptr == thisline)
  147. ohshit(_("statoverride file contains empty line"));
  148. *ptr = '\0';
  149. /* Extract the uid. */
  150. if (!(ptr = memchr(thisline, ' ', nextline - thisline)))
  151. ohshit(_("syntax error in statoverride file"));
  152. *ptr = '\0';
  153. fso->uid = statdb_parse_uid(thisline);
  154. /* Move to the next bit */
  155. thisline = ptr + 1;
  156. if (thisline >= loaded_list_end)
  157. ohshit(_("unexpected end of line in statoverride file"));
  158. /* Extract the gid */
  159. if (!(ptr = memchr(thisline, ' ', nextline - thisline)))
  160. ohshit(_("syntax error in statoverride file"));
  161. *ptr = '\0';
  162. fso->gid = statdb_parse_gid(thisline);
  163. /* Move to the next bit */
  164. thisline = ptr + 1;
  165. if (thisline >= loaded_list_end)
  166. ohshit(_("unexpected end of line in statoverride file"));
  167. /* Extract the mode */
  168. if (!(ptr = memchr(thisline, ' ', nextline - thisline)))
  169. ohshit(_("syntax error in statoverride file"));
  170. *ptr = '\0';
  171. fso->mode = statdb_parse_mode(thisline);
  172. /* Move to the next bit */
  173. thisline = ptr + 1;
  174. if (thisline >= loaded_list_end)
  175. ohshit(_("unexpected end of line in statoverride file"));
  176. fnn = findnamenode(thisline, 0);
  177. if (fnn->statoverride)
  178. ohshit(_("multiple statusoverrides present for file '%.250s'"),
  179. thisline);
  180. fnn->statoverride = fso;
  181. /* Moving on... */
  182. thisline = nextline;
  183. }
  184. onerr_abort--;
  185. }