statdb.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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-2012 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. errno = 0;
  49. value = strtol(str + 1, &endptr, 10);
  50. if (str + 1 == endptr || *endptr || value < 0 || errno != 0)
  51. ohshit(_("syntax error: invalid uid in statoverride file"));
  52. uid = (uid_t)value;
  53. } else {
  54. struct passwd *pw = getpwnam(str);
  55. if (pw == NULL)
  56. ohshit(_("syntax error: unknown user '%s' in statoverride file"),
  57. str);
  58. uid = pw->pw_uid;
  59. }
  60. return uid;
  61. }
  62. gid_t
  63. statdb_parse_gid(const char *str)
  64. {
  65. char *endptr;
  66. gid_t gid;
  67. if (str[0] == '#') {
  68. long int value;
  69. errno = 0;
  70. value = strtol(str + 1, &endptr, 10);
  71. if (str + 1 == endptr || *endptr || value < 0 || errno != 0)
  72. ohshit(_("syntax error: invalid gid in statoverride file"));
  73. gid = (gid_t)value;
  74. } else {
  75. struct group *gr = getgrnam(str);
  76. if (gr == NULL)
  77. ohshit(_("syntax error: unknown group '%s' in statoverride file"),
  78. str);
  79. gid = gr->gr_gid;
  80. }
  81. return gid;
  82. }
  83. mode_t
  84. statdb_parse_mode(const char *str)
  85. {
  86. char *endptr;
  87. long int mode;
  88. mode = strtol(str, &endptr, 8);
  89. if (str == endptr || *endptr || mode < 0 || mode > 07777)
  90. ohshit(_("syntax error: invalid mode in statoverride file"));
  91. return (mode_t)mode;
  92. }
  93. void
  94. ensure_statoverrides(void)
  95. {
  96. struct stat stab1, stab2;
  97. FILE *file;
  98. char *loaded_list, *loaded_list_end, *thisline, *nextline, *ptr;
  99. struct file_stat *fso;
  100. struct filenamenode *fnn;
  101. if (statoverridename != NULL)
  102. free(statoverridename);
  103. statoverridename = dpkg_db_get_path(STATOVERRIDEFILE);
  104. onerr_abort++;
  105. file = fopen(statoverridename, "r");
  106. if (!file) {
  107. if (errno != ENOENT)
  108. ohshite(_("failed to open statoverride file"));
  109. if (!statoverridefile) {
  110. onerr_abort--;
  111. return;
  112. }
  113. } else {
  114. if (fstat(fileno(file), &stab2))
  115. ohshite(_("failed to fstat statoverride file"));
  116. if (statoverridefile) {
  117. if (fstat(fileno(statoverridefile), &stab1))
  118. ohshite(_("failed to fstat previous statoverride file"));
  119. if (stab1.st_dev == stab2.st_dev &&
  120. stab1.st_ino == stab2.st_ino) {
  121. fclose(file);
  122. onerr_abort--;
  123. return;
  124. }
  125. }
  126. }
  127. if (statoverridefile)
  128. fclose(statoverridefile);
  129. statoverridefile = file;
  130. setcloexec(fileno(statoverridefile), statoverridename);
  131. /* If the statoverride list is empty we don't need to bother
  132. * reading it. */
  133. if (!stab2.st_size) {
  134. onerr_abort--;
  135. return;
  136. }
  137. loaded_list = nfmalloc(stab2.st_size);
  138. loaded_list_end = loaded_list + stab2.st_size;
  139. if (fd_read(fileno(file), loaded_list, stab2.st_size) < 0)
  140. ohshite(_("reading statoverride file '%.250s'"), statoverridename);
  141. thisline = loaded_list;
  142. while (thisline < loaded_list_end) {
  143. fso = nfmalloc(sizeof(struct file_stat));
  144. ptr = memchr(thisline, '\n', loaded_list_end - thisline);
  145. if (ptr == NULL)
  146. ohshit(_("statoverride file is missing final newline"));
  147. /* Where to start next time around. */
  148. nextline = ptr + 1;
  149. if (ptr == thisline)
  150. ohshit(_("statoverride file contains empty line"));
  151. *ptr = '\0';
  152. /* Extract the uid. */
  153. ptr = memchr(thisline, ' ', nextline - thisline);
  154. if (ptr == NULL)
  155. ohshit(_("syntax error in statoverride file"));
  156. *ptr = '\0';
  157. fso->uid = statdb_parse_uid(thisline);
  158. /* Move to the next bit */
  159. thisline = ptr + 1;
  160. if (thisline >= loaded_list_end)
  161. ohshit(_("unexpected end of line in statoverride file"));
  162. /* Extract the gid */
  163. ptr = memchr(thisline, ' ', nextline - thisline);
  164. if (ptr == NULL)
  165. ohshit(_("syntax error in statoverride file"));
  166. *ptr = '\0';
  167. fso->gid = statdb_parse_gid(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. /* Extract the mode */
  173. ptr = memchr(thisline, ' ', nextline - thisline);
  174. if (ptr == NULL)
  175. ohshit(_("syntax error in statoverride file"));
  176. *ptr = '\0';
  177. fso->mode = statdb_parse_mode(thisline);
  178. /* Move to the next bit */
  179. thisline = ptr + 1;
  180. if (thisline >= loaded_list_end)
  181. ohshit(_("unexpected end of line in statoverride file"));
  182. fnn = findnamenode(thisline, 0);
  183. if (fnn->statoverride)
  184. ohshit(_("multiple statoverrides present for file '%.250s'"),
  185. thisline);
  186. fnn->statoverride = fso;
  187. /* Moving on... */
  188. thisline = nextline;
  189. }
  190. onerr_abort--;
  191. }