statcmd.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  1. /*
  2. * dpkg-statoverrides - override ownership and mode of files
  3. *
  4. * Copyright © 2000, 2001 Wichert Akkerman <wakkerma@debian.org>
  5. * Copyright © 2006-2009 Guillem Jover <guillem@debian.org>
  6. *
  7. * This is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. *
  20. */
  21. #include <config.h>
  22. #include <compat.h>
  23. #include <sys/types.h>
  24. #include <sys/stat.h>
  25. #include <errno.h>
  26. #if HAVE_LOCALE_H
  27. #include <locale.h>
  28. #endif
  29. #include <string.h>
  30. #include <grp.h>
  31. #include <pwd.h>
  32. #include <fnmatch.h>
  33. #include <unistd.h>
  34. #include <stdlib.h>
  35. #include <stdio.h>
  36. #include <dpkg/i18n.h>
  37. #include <dpkg/dpkg.h>
  38. #include <dpkg/dpkg-db.h>
  39. #include <dpkg/path.h>
  40. #include <dpkg/dir.h>
  41. #include <dpkg/myopt.h>
  42. #include "main.h"
  43. #include "filesdb.h"
  44. const char thisname[] = "dpkg-statoverrides";
  45. const char printforhelp[] = N_("Use --help for help about querying packages.");
  46. static void DPKG_ATTR_NORET
  47. printversion(const struct cmdinfo *cip, const char *value)
  48. {
  49. printf(_("Debian %s version %s.\n"), thisname, DPKG_VERSION_ARCH);
  50. printf(_(
  51. "Copyright (C) 2000, 2001 Wichert Akkerman.\n"
  52. "Copyright (C) 2006-2009 Guillem Jover.\n"));
  53. printf(_(
  54. "This is free software; see the GNU General Public License version 2 or\n"
  55. "later for copying conditions. There is NO warranty.\n"));
  56. m_output(stdout, _("<standard output>"));
  57. exit(0);
  58. }
  59. static void DPKG_ATTR_NORET
  60. usage(const struct cmdinfo *cip, const char *value)
  61. {
  62. printf(_(
  63. "Usage: %s [<option> ...] <command>\n"
  64. "\n"), thisname);
  65. printf(_(
  66. "Commands:\n"
  67. " --add <owner> <group> <mode> <file>\n"
  68. " add a new entry into the database.\n"
  69. " --remove <file> remove file from the database.\n"
  70. " --list [<glob-pattern>] list current overrides in the database.\n"
  71. "\n"));
  72. printf(_(
  73. "Options:\n"
  74. " --admindir <directory> set the directory with the statoverride file.\n"
  75. " --update immediately update file permissions.\n"
  76. " --force force an action even if a sanity check fails.\n"
  77. " --quiet quiet operation, minimal output.\n"
  78. " --help show this help message.\n"
  79. " --version show the version.\n"
  80. "\n"));
  81. m_output(stdout, _("<standard output>"));
  82. exit(0);
  83. }
  84. const struct cmdinfo *cipaction = NULL;
  85. const char *admindir = ADMINDIR;
  86. static int opt_verbose = 1;
  87. static int opt_force = 0;
  88. static int opt_update = 0;
  89. static void
  90. setaction(const struct cmdinfo *cip, const char *value)
  91. {
  92. if (cipaction)
  93. badusage(_("conflicting actions -%c (--%s) and -%c (--%s)"),
  94. cip->oshort, cip->olong,
  95. cipaction->oshort, cipaction->olong);
  96. cipaction = cip;
  97. }
  98. static char *
  99. path_cleanup(const char *path)
  100. {
  101. char *new_path = m_strdup(path);
  102. path_rtrim_slash_slashdot(new_path);
  103. if (opt_verbose && strcmp(path, new_path) != 0)
  104. warning(_("stripping trailing /"));
  105. return new_path;
  106. }
  107. static struct filestatoverride *
  108. statdb_node_new(const char *user, const char *group, const char *mode)
  109. {
  110. struct filestatoverride *filestat;
  111. filestat = nfmalloc(sizeof(*filestat));
  112. filestat->uid = statdb_parse_uid(user);
  113. filestat->gid = statdb_parse_gid(group);
  114. filestat->mode = statdb_parse_mode(mode);
  115. return filestat;
  116. }
  117. static struct filestatoverride **
  118. statdb_node_find(const char *filename)
  119. {
  120. struct filenamenode *file;
  121. file = findnamenode(filename, 0);
  122. return &file->statoverride;
  123. }
  124. static int
  125. statdb_node_remove(const char *filename)
  126. {
  127. struct filenamenode *file;
  128. file = findnamenode(filename, fnn_nonew);
  129. if (!file || (file && !file->statoverride))
  130. return 0;
  131. file->statoverride = NULL;
  132. return 1;
  133. }
  134. static void
  135. statdb_node_apply(const char *filename, struct filestatoverride *filestat)
  136. {
  137. if (chown(filename, filestat->uid, filestat->gid) < 0)
  138. ohshite(_("error setting ownership of `%.255s'"), filename);
  139. if (chmod(filename, filestat->mode))
  140. ohshite(_("error setting permissions of `%.255s'"), filename);
  141. }
  142. static void
  143. statdb_node_print(FILE *out, struct filenamenode *file)
  144. {
  145. struct filestatoverride *filestat = file->statoverride;
  146. struct passwd *pw;
  147. struct group *gr;
  148. if (!filestat)
  149. return;
  150. pw = getpwuid(filestat->uid);
  151. if (pw)
  152. fprintf(out, "%s ", pw->pw_name);
  153. else
  154. fprintf(out, "#%d ", filestat->uid);
  155. gr = getgrgid(filestat->gid);
  156. if (gr)
  157. fprintf(out, "%s ", gr->gr_name);
  158. else
  159. fprintf(out, "#%d ", filestat->gid);
  160. fprintf(out, "%o %s\n", filestat->mode, file->name);
  161. }
  162. static void
  163. statdb_write(void)
  164. {
  165. FILE *dbfile;
  166. struct fileiterator *i;
  167. struct filenamenode *file;
  168. struct varbuf dbname = VARBUF_INIT;
  169. struct varbuf dbname_new = VARBUF_INIT;
  170. struct varbuf dbname_old = VARBUF_INIT;
  171. varbufaddstr(&dbname, admindir);
  172. varbufaddstr(&dbname, "/" STATOVERRIDEFILE);
  173. varbufaddc(&dbname, '\0');
  174. varbufaddstr(&dbname_new, dbname.buf);
  175. varbufaddstr(&dbname_new, NEWDBEXT);
  176. varbufaddc(&dbname_new, '\0');
  177. varbufaddstr(&dbname_old, dbname.buf);
  178. varbufaddstr(&dbname_old, OLDDBEXT);
  179. varbufaddc(&dbname_old, '\0');
  180. dbfile = fopen(dbname_new.buf, "w");
  181. if (!dbfile)
  182. ohshite(_("cannot open new statoverride file"));
  183. i = iterfilestart();
  184. while ((file = iterfilenext(i)))
  185. statdb_node_print(dbfile, file);
  186. iterfileend(i);
  187. if (fflush(dbfile))
  188. ohshite(_("unable to flush file '%s'"), dbname_new.buf);
  189. if (fsync(fileno(dbfile)))
  190. ohshite(_("unable to sync file '%s'"), dbname_new.buf);
  191. fclose(dbfile);
  192. chmod(dbname_new.buf, 0644);
  193. if (unlink(dbname_old.buf) && errno != ENOENT)
  194. ohshite(_("error removing statoverride-old"));
  195. if (link(dbname.buf, dbname_old.buf) && errno != ENOENT)
  196. ohshite(_("error creating new statoverride-old"));
  197. if (rename(dbname_new.buf, dbname.buf))
  198. ohshite(_("error installing new statoverride"));
  199. dir_sync_path(admindir);
  200. varbuf_destroy(&dbname);
  201. varbuf_destroy(&dbname_new);
  202. varbuf_destroy(&dbname_old);
  203. }
  204. static int
  205. statoverride_add(const char *const *argv)
  206. {
  207. const char *user = argv[0];
  208. const char *group = argv[1];
  209. const char *mode = argv[2];
  210. const char *path = argv[3];
  211. char *filename;
  212. struct filestatoverride **filestat;
  213. if (!user || !group || !mode || !path || argv[4])
  214. badusage(_("--add needs four arguments"));
  215. if (strchr(path, '\n'))
  216. badusage(_("file may not contain newlines"));
  217. filename = path_cleanup(path);
  218. filestat = statdb_node_find(filename);
  219. if (*filestat != NULL) {
  220. if (opt_force)
  221. warning(_("An override for '%s' already exists, "
  222. "but --force specified so will be ignored."),
  223. filename);
  224. else
  225. ohshit(_("An override for '%s' already exists, "
  226. "aborting."), filename);
  227. }
  228. *filestat = statdb_node_new(user, group, mode);
  229. if (opt_update) {
  230. struct stat st;
  231. if (stat(filename, &st) == 0)
  232. statdb_node_apply(filename, *filestat);
  233. else if (opt_verbose)
  234. warning(_("--update given but %s does not exist"),
  235. filename);
  236. }
  237. statdb_write();
  238. free(filename);
  239. return 0;
  240. }
  241. static int
  242. statoverride_remove(const char *const *argv)
  243. {
  244. const char *path = argv[0];
  245. char *filename;
  246. if (!path || argv[1])
  247. badusage(_("--%s needs a single argument"), "remove");
  248. filename = path_cleanup(path);
  249. if (!statdb_node_remove(filename)) {
  250. if (opt_verbose)
  251. warning(_("No override present."));
  252. if (opt_force)
  253. exit(0);
  254. else
  255. exit(2);
  256. }
  257. if (opt_update && opt_verbose)
  258. warning(_("--update is useless for --remove"));
  259. statdb_write();
  260. free(filename);
  261. return 0;
  262. }
  263. struct glob_node {
  264. struct glob_node *next;
  265. char *pattern;
  266. };
  267. static void
  268. glob_list_prepend(struct glob_node **list, char *pattern)
  269. {
  270. struct glob_node *node;
  271. node = m_malloc(sizeof(*node));
  272. node->pattern = pattern;
  273. node->next = *list;
  274. *list = node;
  275. }
  276. static void
  277. glob_list_free(struct glob_node *head)
  278. {
  279. while (head) {
  280. struct glob_node *node = head;
  281. head = head->next;
  282. free(node->pattern);
  283. free(node);
  284. }
  285. }
  286. static int
  287. statoverride_list(const char *const *argv)
  288. {
  289. struct fileiterator *i;
  290. struct filenamenode *file;
  291. const char *thisarg;
  292. struct glob_node *glob_list = NULL;
  293. int ret = 1;
  294. while ((thisarg = *argv++)) {
  295. char *pattern = path_cleanup(thisarg);
  296. glob_list_prepend(&glob_list, pattern);
  297. }
  298. if (glob_list == NULL)
  299. glob_list_prepend(&glob_list, m_strdup("*"));
  300. i = iterfilestart();
  301. while ((file = iterfilenext(i))) {
  302. struct glob_node *g;
  303. for (g = glob_list; g; g = g->next) {
  304. if (fnmatch(g->pattern, file->name, 0) == 0) {
  305. statdb_node_print(stdout, file);
  306. ret = 0;
  307. break;
  308. }
  309. }
  310. }
  311. iterfileend(i);
  312. glob_list_free(glob_list);
  313. return ret;
  314. }
  315. #define ACTION(longopt, shortopt, code, function) \
  316. { longopt, shortopt, 0, 0, 0, setaction, code, 0, (voidfnp)function }
  317. static const struct cmdinfo cmdinfos[] = {
  318. ACTION("add", 0, act_install, statoverride_add),
  319. ACTION("remove", 0, act_remove, statoverride_remove),
  320. ACTION("list", 0, act_listfiles, statoverride_list),
  321. { "admindir", 0, 1, NULL, &admindir, NULL },
  322. { "quiet", 0, 0, &opt_verbose, NULL, NULL, 0 },
  323. { "force", 0, 0, &opt_force, NULL, NULL, 1 },
  324. { "update", 0, 0, &opt_update, NULL, NULL, 1 },
  325. { "help", 'h', 0, NULL, NULL, usage },
  326. { "version", 0, 0, NULL, NULL, printversion },
  327. { NULL, 0 }
  328. };
  329. int
  330. main(int argc, const char *const *argv)
  331. {
  332. jmp_buf ejbuf;
  333. int (*actionfunction)(const char *const *argv);
  334. int ret;
  335. setlocale(LC_ALL, "");
  336. bindtextdomain(PACKAGE, LOCALEDIR);
  337. textdomain(PACKAGE);
  338. standard_startup(&ejbuf);
  339. myopt(&argv, cmdinfos);
  340. if (!cipaction)
  341. badusage(_("need an action option"));
  342. setvbuf(stdout, NULL, _IONBF, 0);
  343. filesdbinit();
  344. ensure_statoverrides();
  345. actionfunction = (int (*)(const char *const *))cipaction->farg;
  346. ret = actionfunction(argv);
  347. standard_shutdown();
  348. return ret;
  349. }