statcmd.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  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 "glob.h"
  44. #include "filesdb.h"
  45. const char thisname[] = "dpkg-statoverrides";
  46. const char printforhelp[] = N_("Use --help for help about querying packages.");
  47. static void DPKG_ATTR_NORET
  48. printversion(const struct cmdinfo *cip, const char *value)
  49. {
  50. printf(_("Debian %s version %s.\n"), thisname, DPKG_VERSION_ARCH);
  51. printf(_(
  52. "Copyright (C) 2000, 2001 Wichert Akkerman.\n"
  53. "Copyright (C) 2006-2009 Guillem Jover.\n"));
  54. printf(_(
  55. "This is free software; see the GNU General Public License version 2 or\n"
  56. "later for copying conditions. There is NO warranty.\n"));
  57. m_output(stdout, _("<standard output>"));
  58. exit(0);
  59. }
  60. static void DPKG_ATTR_NORET
  61. usage(const struct cmdinfo *cip, const char *value)
  62. {
  63. printf(_(
  64. "Usage: %s [<option> ...] <command>\n"
  65. "\n"), thisname);
  66. printf(_(
  67. "Commands:\n"
  68. " --add <owner> <group> <mode> <file>\n"
  69. " add a new entry into the database.\n"
  70. " --remove <file> remove file from the database.\n"
  71. " --list [<glob-pattern>] list current overrides in the database.\n"
  72. "\n"));
  73. printf(_(
  74. "Options:\n"
  75. " --admindir <directory> set the directory with the statoverride file.\n"
  76. " --update immediately update file permissions.\n"
  77. " --force force an action even if a sanity check fails.\n"
  78. " --quiet quiet operation, minimal output.\n"
  79. " --help show this help message.\n"
  80. " --version show the version.\n"
  81. "\n"));
  82. m_output(stdout, _("<standard output>"));
  83. exit(0);
  84. }
  85. const struct cmdinfo *cipaction = NULL;
  86. const char *admindir = ADMINDIR;
  87. static int opt_verbose = 1;
  88. static int opt_force = 0;
  89. static int opt_update = 0;
  90. static void
  91. setaction(const struct cmdinfo *cip, const char *value)
  92. {
  93. if (cipaction)
  94. badusage(_("conflicting actions -%c (--%s) and -%c (--%s)"),
  95. cip->oshort, cip->olong,
  96. cipaction->oshort, cipaction->olong);
  97. cipaction = cip;
  98. }
  99. static char *
  100. path_cleanup(const char *path)
  101. {
  102. char *new_path = m_strdup(path);
  103. path_rtrim_slash_slashdot(new_path);
  104. if (opt_verbose && strcmp(path, new_path) != 0)
  105. warning(_("stripping trailing /"));
  106. return new_path;
  107. }
  108. static struct filestatoverride *
  109. statdb_node_new(const char *user, const char *group, const char *mode)
  110. {
  111. struct filestatoverride *filestat;
  112. filestat = nfmalloc(sizeof(*filestat));
  113. filestat->uid = statdb_parse_uid(user);
  114. filestat->gid = statdb_parse_gid(group);
  115. filestat->mode = statdb_parse_mode(mode);
  116. return filestat;
  117. }
  118. static struct filestatoverride **
  119. statdb_node_find(const char *filename)
  120. {
  121. struct filenamenode *file;
  122. file = findnamenode(filename, 0);
  123. return &file->statoverride;
  124. }
  125. static int
  126. statdb_node_remove(const char *filename)
  127. {
  128. struct filenamenode *file;
  129. file = findnamenode(filename, fnn_nonew);
  130. if (!file || (file && !file->statoverride))
  131. return 0;
  132. file->statoverride = NULL;
  133. return 1;
  134. }
  135. static void
  136. statdb_node_apply(const char *filename, struct filestatoverride *filestat)
  137. {
  138. if (chown(filename, filestat->uid, filestat->gid) < 0)
  139. ohshite(_("error setting ownership of `%.255s'"), filename);
  140. if (chmod(filename, filestat->mode))
  141. ohshite(_("error setting permissions of `%.255s'"), filename);
  142. }
  143. static void
  144. statdb_node_print(FILE *out, struct filenamenode *file)
  145. {
  146. struct filestatoverride *filestat = file->statoverride;
  147. struct passwd *pw;
  148. struct group *gr;
  149. if (!filestat)
  150. return;
  151. pw = getpwuid(filestat->uid);
  152. if (pw)
  153. fprintf(out, "%s ", pw->pw_name);
  154. else
  155. fprintf(out, "#%d ", filestat->uid);
  156. gr = getgrgid(filestat->gid);
  157. if (gr)
  158. fprintf(out, "%s ", gr->gr_name);
  159. else
  160. fprintf(out, "#%d ", filestat->gid);
  161. fprintf(out, "%o %s\n", filestat->mode, file->name);
  162. }
  163. static void
  164. statdb_write(void)
  165. {
  166. FILE *dbfile;
  167. struct fileiterator *i;
  168. struct filenamenode *file;
  169. struct varbuf dbname = VARBUF_INIT;
  170. struct varbuf dbname_new = VARBUF_INIT;
  171. struct varbuf dbname_old = VARBUF_INIT;
  172. varbufaddstr(&dbname, admindir);
  173. varbufaddstr(&dbname, "/" STATOVERRIDEFILE);
  174. varbufaddc(&dbname, '\0');
  175. varbufaddstr(&dbname_new, dbname.buf);
  176. varbufaddstr(&dbname_new, NEWDBEXT);
  177. varbufaddc(&dbname_new, '\0');
  178. varbufaddstr(&dbname_old, dbname.buf);
  179. varbufaddstr(&dbname_old, OLDDBEXT);
  180. varbufaddc(&dbname_old, '\0');
  181. dbfile = fopen(dbname_new.buf, "w");
  182. if (!dbfile)
  183. ohshite(_("cannot open new statoverride file"));
  184. i = iterfilestart();
  185. while ((file = iterfilenext(i)))
  186. statdb_node_print(dbfile, file);
  187. iterfileend(i);
  188. if (fflush(dbfile))
  189. ohshite(_("unable to flush file '%s'"), dbname_new.buf);
  190. if (fsync(fileno(dbfile)))
  191. ohshite(_("unable to sync file '%s'"), dbname_new.buf);
  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. dir_sync_path(admindir);
  201. varbuf_destroy(&dbname);
  202. varbuf_destroy(&dbname_new);
  203. varbuf_destroy(&dbname_old);
  204. }
  205. static int
  206. statoverride_add(const char *const *argv)
  207. {
  208. const char *user = argv[0];
  209. const char *group = argv[1];
  210. const char *mode = argv[2];
  211. const char *path = argv[3];
  212. char *filename;
  213. struct filestatoverride **filestat;
  214. if (!user || !group || !mode || !path || argv[4])
  215. badusage(_("--add needs four arguments"));
  216. if (strchr(path, '\n'))
  217. badusage(_("file may not contain newlines"));
  218. filename = path_cleanup(path);
  219. filestat = statdb_node_find(filename);
  220. if (*filestat != NULL) {
  221. if (opt_force)
  222. warning(_("An override for '%s' already exists, "
  223. "but --force specified so will be ignored."),
  224. filename);
  225. else
  226. ohshit(_("An override for '%s' already exists, "
  227. "aborting."), filename);
  228. }
  229. *filestat = statdb_node_new(user, group, mode);
  230. if (opt_update) {
  231. struct stat st;
  232. if (stat(filename, &st) == 0)
  233. statdb_node_apply(filename, *filestat);
  234. else if (opt_verbose)
  235. warning(_("--update given but %s does not exist"),
  236. filename);
  237. }
  238. statdb_write();
  239. free(filename);
  240. return 0;
  241. }
  242. static int
  243. statoverride_remove(const char *const *argv)
  244. {
  245. const char *path = argv[0];
  246. char *filename;
  247. if (!path || argv[1])
  248. badusage(_("--%s needs a single argument"), "remove");
  249. filename = path_cleanup(path);
  250. if (!statdb_node_remove(filename)) {
  251. if (opt_verbose)
  252. warning(_("No override present."));
  253. if (opt_force)
  254. exit(0);
  255. else
  256. exit(2);
  257. }
  258. if (opt_update && opt_verbose)
  259. warning(_("--update is useless for --remove"));
  260. statdb_write();
  261. free(filename);
  262. return 0;
  263. }
  264. static int
  265. statoverride_list(const char *const *argv)
  266. {
  267. struct fileiterator *i;
  268. struct filenamenode *file;
  269. const char *thisarg;
  270. struct glob_node *glob_list = NULL;
  271. int ret = 1;
  272. while ((thisarg = *argv++)) {
  273. char *pattern = path_cleanup(thisarg);
  274. glob_list_prepend(&glob_list, pattern);
  275. }
  276. if (glob_list == NULL)
  277. glob_list_prepend(&glob_list, m_strdup("*"));
  278. i = iterfilestart();
  279. while ((file = iterfilenext(i))) {
  280. struct glob_node *g;
  281. for (g = glob_list; g; g = g->next) {
  282. if (fnmatch(g->pattern, file->name, 0) == 0) {
  283. statdb_node_print(stdout, file);
  284. ret = 0;
  285. break;
  286. }
  287. }
  288. }
  289. iterfileend(i);
  290. glob_list_free(glob_list);
  291. return ret;
  292. }
  293. #define ACTION(longopt, shortopt, code, function) \
  294. { longopt, shortopt, 0, 0, 0, setaction, code, 0, (voidfnp)function }
  295. static const struct cmdinfo cmdinfos[] = {
  296. ACTION("add", 0, act_install, statoverride_add),
  297. ACTION("remove", 0, act_remove, statoverride_remove),
  298. ACTION("list", 0, act_listfiles, statoverride_list),
  299. { "admindir", 0, 1, NULL, &admindir, NULL },
  300. { "quiet", 0, 0, &opt_verbose, NULL, NULL, 0 },
  301. { "force", 0, 0, &opt_force, NULL, NULL, 1 },
  302. { "update", 0, 0, &opt_update, NULL, NULL, 1 },
  303. { "help", 'h', 0, NULL, NULL, usage },
  304. { "version", 0, 0, NULL, NULL, printversion },
  305. { NULL, 0 }
  306. };
  307. int
  308. main(int argc, const char *const *argv)
  309. {
  310. jmp_buf ejbuf;
  311. int (*actionfunction)(const char *const *argv);
  312. int ret;
  313. setlocale(LC_ALL, "");
  314. bindtextdomain(PACKAGE, LOCALEDIR);
  315. textdomain(PACKAGE);
  316. standard_startup(&ejbuf);
  317. myopt(&argv, cmdinfos);
  318. if (!cipaction)
  319. badusage(_("need an action option"));
  320. setvbuf(stdout, NULL, _IONBF, 0);
  321. filesdbinit();
  322. ensure_statoverrides();
  323. actionfunction = (int (*)(const char *const *))cipaction->farg;
  324. ret = actionfunction(argv);
  325. standard_shutdown();
  326. return ret;
  327. }