statcmd.c 10 KB

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