statdb.c 5.3 KB

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