statdb.c 5.4 KB

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