statdb.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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 published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This is distributed in the hope that it will be useful,
  14. * but 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 License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. */
  21. #include <config.h>
  22. #include <compat.h>
  23. #include <sys/types.h>
  24. #include <sys/stat.h>
  25. #include <errno.h>
  26. #include <string.h>
  27. #include <pwd.h>
  28. #include <grp.h>
  29. #include <fcntl.h>
  30. #include <unistd.h>
  31. #include <stdlib.h>
  32. #include <dpkg/i18n.h>
  33. #include <dpkg/dpkg.h>
  34. #include <dpkg/dpkg-db.h>
  35. #include <dpkg/buffer.h>
  36. #include "filesdb.h"
  37. #include "main.h"
  38. static FILE *statoverridefile = NULL;
  39. uid_t
  40. statdb_parse_uid(const char *str)
  41. {
  42. char* endptr;
  43. uid_t uid;
  44. if (str[0] == '#') {
  45. long int value;
  46. value = strtol(str + 1, &endptr, 10);
  47. if (str + 1 == endptr || *endptr || value < 0)
  48. ohshit(_("syntax error: invalid uid in statoverride file"));
  49. uid = (uid_t)value;
  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. long int value;
  66. value = strtol(str + 1, &endptr, 10);
  67. if (str + 1 == endptr || *endptr || value < 0)
  68. ohshit(_("syntax error: invalid gid in statoverride file"));
  69. gid = (gid_t)value;
  70. } else {
  71. struct group* gr = getgrnam(str);
  72. if (gr == NULL)
  73. ohshit(_("syntax error: unknown group '%s' in statoverride file"),
  74. str);
  75. gid = gr->gr_gid;
  76. }
  77. return gid;
  78. }
  79. mode_t
  80. statdb_parse_mode(const char *str)
  81. {
  82. char* endptr;
  83. long int mode;
  84. mode = strtol(str, &endptr, 8);
  85. if (str == endptr || *endptr || mode < 0 || mode > 07777)
  86. ohshit(_("syntax error: invalid mode in statoverride file"));
  87. return (mode_t)mode;
  88. }
  89. void
  90. ensure_statoverrides(void)
  91. {
  92. static struct varbuf vb;
  93. struct stat stab1, stab2;
  94. FILE *file;
  95. char *loaded_list, *loaded_list_end, *thisline, *nextline, *ptr;
  96. struct filestatoverride *fso;
  97. struct filenamenode *fnn;
  98. varbufreset(&vb);
  99. varbufaddstr(&vb, admindir);
  100. varbufaddstr(&vb, "/" STATOVERRIDEFILE);
  101. varbufaddc(&vb, 0);
  102. onerr_abort++;
  103. file = fopen(vb.buf,"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), vb.buf);
  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. fd_buf_copy(fileno(file), loaded_list, stab2.st_size,
  138. _("statoverride file `%.250s'"), vb.buf);
  139. thisline = loaded_list;
  140. while (thisline < loaded_list_end) {
  141. fso = nfmalloc(sizeof(struct filestatoverride));
  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 statusoverides present for file '%.250s'"),
  179. thisline);
  180. fnn->statoverride = fso;
  181. /* Moving on.. */
  182. thisline = nextline;
  183. }
  184. onerr_abort--;
  185. }