statdb.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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 <ijackson@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 <https://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 char *statoverridename;
  40. uid_t
  41. statdb_parse_uid(const char *str)
  42. {
  43. char *endptr;
  44. uid_t uid;
  45. if (str[0] == '#') {
  46. long int value;
  47. errno = 0;
  48. value = strtol(str + 1, &endptr, 10);
  49. if (str + 1 == endptr || *endptr || value < 0 || errno != 0)
  50. ohshit(_("invalid statoverride uid %s"), str);
  51. uid = (uid_t)value;
  52. } else {
  53. struct passwd *pw = getpwnam(str);
  54. if (pw == NULL)
  55. uid = (uid_t)-1;
  56. else
  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. errno = 0;
  69. value = strtol(str + 1, &endptr, 10);
  70. if (str + 1 == endptr || *endptr || value < 0 || errno != 0)
  71. ohshit(_("invalid statoverride gid %s"), str);
  72. gid = (gid_t)value;
  73. } else {
  74. struct group *gr = getgrnam(str);
  75. if (gr == NULL)
  76. gid = (gid_t)-1;
  77. else
  78. gid = gr->gr_gid;
  79. }
  80. return gid;
  81. }
  82. mode_t
  83. statdb_parse_mode(const char *str)
  84. {
  85. char *endptr;
  86. long int mode;
  87. mode = strtol(str, &endptr, 8);
  88. if (str == endptr || *endptr || mode < 0 || mode > 07777)
  89. ohshit(_("invalid statoverride mode %s"), str);
  90. return (mode_t)mode;
  91. }
  92. void
  93. ensure_statoverrides(enum statdb_parse_flags flags)
  94. {
  95. static struct stat sb_prev;
  96. struct stat sb_next;
  97. static FILE *file_prev;
  98. FILE *file;
  99. char *loaded_list, *loaded_list_end, *thisline, *nextline, *ptr;
  100. struct file_stat *fso;
  101. struct filenamenode *fnn;
  102. struct fileiterator *iter;
  103. if (statoverridename == NULL)
  104. statoverridename = dpkg_db_get_path(STATOVERRIDEFILE);
  105. onerr_abort++;
  106. file = fopen(statoverridename, "r");
  107. if (!file) {
  108. if (errno != ENOENT)
  109. ohshite(_("failed to open statoverride file"));
  110. } else {
  111. setcloexec(fileno(file), statoverridename);
  112. if (fstat(fileno(file), &sb_next))
  113. ohshite(_("failed to fstat statoverride file"));
  114. /*
  115. * We need to keep the database file open so that the
  116. * filesystem cannot reuse the inode number (f.ex. during
  117. * multiple dpkg-statoverride invocations in a maintainer
  118. * script), otherwise the following check might turn true,
  119. * and we would skip reloading a modified database.
  120. */
  121. if (file_prev &&
  122. sb_prev.st_dev == sb_next.st_dev &&
  123. sb_prev.st_ino == sb_next.st_ino) {
  124. fclose(file);
  125. onerr_abort--;
  126. debug(dbg_general, "%s: same, skipping", __func__);
  127. return;
  128. }
  129. sb_prev = sb_next;
  130. }
  131. if (file_prev)
  132. fclose(file_prev);
  133. file_prev = file;
  134. /* Reset statoverride information. */
  135. iter = files_db_iter_new();
  136. while ((fnn = files_db_iter_next(iter)))
  137. fnn->statoverride = NULL;
  138. files_db_iter_free(iter);
  139. if (!file) {
  140. onerr_abort--;
  141. debug(dbg_general, "%s: none, resetting", __func__);
  142. return;
  143. }
  144. debug(dbg_general, "%s: new, (re)loading", __func__);
  145. /* If the statoverride list is empty we don't need to bother
  146. * reading it. */
  147. if (!sb_next.st_size) {
  148. onerr_abort--;
  149. return;
  150. }
  151. loaded_list = m_malloc(sb_next.st_size);
  152. loaded_list_end = loaded_list + sb_next.st_size;
  153. if (fd_read(fileno(file), loaded_list, sb_next.st_size) < 0)
  154. ohshite(_("reading statoverride file '%.250s'"), statoverridename);
  155. thisline = loaded_list;
  156. while (thisline < loaded_list_end) {
  157. fso = nfmalloc(sizeof(struct file_stat));
  158. ptr = memchr(thisline, '\n', loaded_list_end - thisline);
  159. if (ptr == NULL)
  160. ohshit(_("statoverride file is missing final newline"));
  161. /* Where to start next time around. */
  162. nextline = ptr + 1;
  163. if (ptr == thisline)
  164. ohshit(_("statoverride file contains empty line"));
  165. *ptr = '\0';
  166. /* Extract the uid. */
  167. ptr = memchr(thisline, ' ', nextline - thisline);
  168. if (ptr == NULL)
  169. ohshit(_("syntax error in statoverride file"));
  170. *ptr = '\0';
  171. fso->uid = statdb_parse_uid(thisline);
  172. if (fso->uid == (uid_t)-1)
  173. fso->uname = nfstrsave(thisline);
  174. else
  175. fso->uname = NULL;
  176. if (fso->uid == (uid_t)-1 && !(flags & STATDB_PARSE_LAX))
  177. ohshit(_("unknown user '%s' in statoverride file"),
  178. thisline);
  179. /* Move to the next bit */
  180. thisline = ptr + 1;
  181. if (thisline >= loaded_list_end)
  182. ohshit(_("unexpected end of line in statoverride file"));
  183. /* Extract the gid */
  184. ptr = memchr(thisline, ' ', nextline - thisline);
  185. if (ptr == NULL)
  186. ohshit(_("syntax error in statoverride file"));
  187. *ptr = '\0';
  188. fso->gid = statdb_parse_gid(thisline);
  189. if (fso->gid == (gid_t)-1)
  190. fso->gname = nfstrsave(thisline);
  191. else
  192. fso->gname = NULL;
  193. if (fso->gid == (gid_t)-1 && !(flags & STATDB_PARSE_LAX))
  194. ohshit(_("unknown group '%s' in statoverride file"),
  195. thisline);
  196. /* Move to the next bit */
  197. thisline = ptr + 1;
  198. if (thisline >= loaded_list_end)
  199. ohshit(_("unexpected end of line in statoverride file"));
  200. /* Extract the mode */
  201. ptr = memchr(thisline, ' ', nextline - thisline);
  202. if (ptr == NULL)
  203. ohshit(_("syntax error in statoverride file"));
  204. *ptr = '\0';
  205. fso->mode = statdb_parse_mode(thisline);
  206. /* Move to the next bit */
  207. thisline = ptr + 1;
  208. if (thisline >= loaded_list_end)
  209. ohshit(_("unexpected end of line in statoverride file"));
  210. fnn = findnamenode(thisline, 0);
  211. if (fnn->statoverride)
  212. ohshit(_("multiple statoverrides present for file '%.250s'"),
  213. thisline);
  214. fnn->statoverride = fso;
  215. /* Moving on... */
  216. thisline = nextline;
  217. }
  218. free(loaded_list);
  219. onerr_abort--;
  220. }