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 <signal.h>
  34. #include <unistd.h>
  35. #include <stdlib.h>
  36. #include <stdio.h>
  37. #include <dpkg/i18n.h>
  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.\n"));
  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 (opt_verbose && 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 (chown(filename, filestat->uid, filestat->gid) < 0)
  140. ohshite(_("error setting ownership of `%.255s'"), filename);
  141. if (chmod(filename, filestat->mode))
  142. ohshite(_("error setting permissions of `%.255s'"), filename);
  143. }
  144. static void
  145. statdb_node_print(FILE *out, struct filenamenode *file)
  146. {
  147. struct filestatoverride *filestat = file->statoverride;
  148. struct passwd *pw;
  149. struct group *gr;
  150. if (!filestat)
  151. return;
  152. pw = getpwuid(filestat->uid);
  153. if (pw)
  154. fprintf(out, "%s ", pw->pw_name);
  155. else
  156. fprintf(out, "#%d ", filestat->uid);
  157. gr = getgrgid(filestat->gid);
  158. if (gr)
  159. fprintf(out, "%s ", gr->gr_name);
  160. else
  161. fprintf(out, "#%d ", filestat->gid);
  162. fprintf(out, "%o %s\n", filestat->mode, file->name);
  163. }
  164. static void
  165. statdb_write(void)
  166. {
  167. FILE *dbfile;
  168. struct fileiterator *i;
  169. struct filenamenode *file;
  170. struct varbuf dbname = VARBUF_INIT;
  171. struct varbuf dbname_new = VARBUF_INIT;
  172. struct varbuf dbname_old = VARBUF_INIT;
  173. varbufaddstr(&dbname, admindir);
  174. varbufaddstr(&dbname, "/" STATOVERRIDEFILE);
  175. varbufaddc(&dbname, '\0');
  176. varbufaddstr(&dbname_new, dbname.buf);
  177. varbufaddstr(&dbname_new, NEWDBEXT);
  178. varbufaddc(&dbname_new, '\0');
  179. varbufaddstr(&dbname_old, dbname.buf);
  180. varbufaddstr(&dbname_old, OLDDBEXT);
  181. varbufaddc(&dbname_old, '\0');
  182. dbfile = fopen(dbname_new.buf, "w");
  183. if (!dbfile)
  184. ohshite(_("cannot open new statoverride file"));
  185. i = iterfilestart();
  186. while ((file = iterfilenext(i)))
  187. statdb_node_print(dbfile, file);
  188. iterfileend(i);
  189. fclose(dbfile);
  190. chmod(dbname_new.buf, 0644);
  191. if (unlink(dbname_old.buf) && errno != ENOENT)
  192. ohshite(_("error removing statoverride-old"));
  193. if (link(dbname.buf, dbname_old.buf) && errno != ENOENT)
  194. ohshite(_("error creating new statoverride-old"));
  195. if (rename(dbname_new.buf, dbname.buf))
  196. ohshite(_("error installing new statoverride"));
  197. varbuffree(&dbname);
  198. varbuffree(&dbname_new);
  199. varbuffree(&dbname_old);
  200. }
  201. static int
  202. statoverride_add(const char *const *argv)
  203. {
  204. const char *user = argv[0];
  205. const char *group = argv[1];
  206. const char *mode = argv[2];
  207. const char *path = argv[3];
  208. char *filename;
  209. struct filestatoverride **filestat;
  210. if (!user || !group || !mode || !path || argv[4])
  211. badusage(_("--add needs four arguments"));
  212. if (strchr(path, '\n'))
  213. badusage(_("file may not contain newlines"));
  214. filename = path_cleanup(path);
  215. filestat = statdb_node_find(filename);
  216. if (*filestat != NULL) {
  217. if (opt_force)
  218. warning(_("An override for '%s' already exists, "
  219. "but --force specified so will be ignored."),
  220. filename);
  221. else
  222. ohshit(_("An override for '%s' already exists, "
  223. "aborting."), filename);
  224. }
  225. *filestat = statdb_node_new(user, group, mode);
  226. if (opt_update) {
  227. struct stat st;
  228. if (stat(filename, &st) == 0)
  229. statdb_node_apply(filename, *filestat);
  230. else if (opt_verbose)
  231. warning(_("--update given but %s does not exist"),
  232. filename);
  233. }
  234. statdb_write();
  235. free(filename);
  236. return 0;
  237. }
  238. static int
  239. statoverride_remove(const char *const *argv)
  240. {
  241. const char *path = argv[0];
  242. char *filename;
  243. if (!path || argv[1])
  244. badusage(_("--%s needs a single argument"), "remove");
  245. filename = path_cleanup(path);
  246. if (!statdb_node_remove(filename)) {
  247. if (opt_verbose)
  248. warning(_("No override present."));
  249. if (opt_force)
  250. exit(0);
  251. else
  252. exit(2);
  253. }
  254. if (opt_update && opt_verbose)
  255. warning(_("--update is useless for --remove"));
  256. statdb_write();
  257. free(filename);
  258. return 0;
  259. }
  260. struct glob_node {
  261. struct glob_node *next;
  262. char *pattern;
  263. };
  264. static void
  265. glob_list_prepend(struct glob_node **list, char *pattern)
  266. {
  267. struct glob_node *node;
  268. node = m_malloc(sizeof(*node));
  269. node->pattern = pattern;
  270. node->next = *list;
  271. *list = node;
  272. }
  273. static void
  274. glob_list_free(struct glob_node *head)
  275. {
  276. while (head) {
  277. struct glob_node *node = head;
  278. head = head->next;
  279. free(node->pattern);
  280. free(node);
  281. }
  282. }
  283. static int
  284. statoverride_list(const char *const *argv)
  285. {
  286. struct fileiterator *i;
  287. struct filenamenode *file;
  288. const char *thisarg;
  289. struct glob_node *glob_list = NULL;
  290. int ret = 1;
  291. while ((thisarg = *argv++)) {
  292. char *pattern = path_cleanup(thisarg);
  293. glob_list_prepend(&glob_list, pattern);
  294. }
  295. if (glob_list == NULL)
  296. glob_list_prepend(&glob_list, m_strdup("*"));
  297. i = iterfilestart();
  298. while ((file = iterfilenext(i))) {
  299. struct glob_node *g;
  300. for (g = glob_list; g; g = g->next) {
  301. if (fnmatch(g->pattern, file->name, 0) == 0) {
  302. statdb_node_print(stdout, file);
  303. ret = 0;
  304. break;
  305. }
  306. }
  307. }
  308. iterfileend(i);
  309. glob_list_free(glob_list);
  310. return ret;
  311. }
  312. #define ACTION(longopt, shortopt, code, function) \
  313. { longopt, shortopt, 0, 0, 0, setaction, code, 0, (voidfnp)function }
  314. static const struct cmdinfo cmdinfos[] = {
  315. ACTION("add", 'L', act_listfiles, statoverride_add),
  316. ACTION("remove", 's', act_status, statoverride_remove),
  317. ACTION("list", 'p', act_printavail, statoverride_list),
  318. { "admindir", 0, 1, NULL, &admindir, NULL },
  319. { "quiet", 0, 0, &opt_verbose, NULL, NULL, 0 },
  320. { "force", 0, 0, &opt_force, NULL, NULL, 1 },
  321. { "update", 0, 0, &opt_update, NULL, NULL, 1 },
  322. { "help", 'h', 0, NULL, NULL, usage },
  323. { "version", 0, 0, NULL, NULL, printversion },
  324. /* UK spelling */
  325. { "licence", 0, 0, NULL, NULL, showcopyright },
  326. /* US spelling */
  327. { "license", 0, 0, NULL, NULL, showcopyright },
  328. { NULL, 0 }
  329. };
  330. int
  331. main(int argc, const char *const *argv)
  332. {
  333. jmp_buf ejbuf;
  334. int (*actionfunction)(const char *const *argv);
  335. int ret;
  336. setlocale(LC_ALL, "");
  337. bindtextdomain(PACKAGE, LOCALEDIR);
  338. textdomain(PACKAGE);
  339. standard_startup(&ejbuf);
  340. myopt(&argv, cmdinfos);
  341. if (!cipaction)
  342. badusage(_("need an action option"));
  343. setvbuf(stdout, NULL, _IONBF, 0);
  344. filesdbinit();
  345. ensure_statoverrides();
  346. actionfunction = (int (*)(const char *const *))cipaction->farg;
  347. ret = actionfunction(argv);
  348. standard_shutdown();
  349. return ret;
  350. }