statcmd.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. /*
  2. * dpkg-statoverride - override ownership and mode of files
  3. *
  4. * Copyright © 2000, 2001 Wichert Akkerman <wakkerma@debian.org>
  5. * Copyright © 2006-2010 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/glob.h>
  42. #include <dpkg/myopt.h>
  43. #include "main.h"
  44. #include "filesdb.h"
  45. const char thisname[] = "dpkg-statoverride";
  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 char *admindir;
  86. static int opt_verbose = 1;
  87. static int opt_force = 0;
  88. static int opt_update = 0;
  89. static char *
  90. path_cleanup(const char *path)
  91. {
  92. char *new_path = m_strdup(path);
  93. path_trim_slash_slashdot(new_path);
  94. if (opt_verbose && strcmp(path, new_path) != 0)
  95. warning(_("stripping trailing /"));
  96. return new_path;
  97. }
  98. static struct file_stat *
  99. statdb_node_new(const char *user, const char *group, const char *mode)
  100. {
  101. struct file_stat *filestat;
  102. filestat = nfmalloc(sizeof(*filestat));
  103. filestat->uid = statdb_parse_uid(user);
  104. filestat->gid = statdb_parse_gid(group);
  105. filestat->mode = statdb_parse_mode(mode);
  106. return filestat;
  107. }
  108. static struct file_stat **
  109. statdb_node_find(const char *filename)
  110. {
  111. struct filenamenode *file;
  112. file = findnamenode(filename, 0);
  113. return &file->statoverride;
  114. }
  115. static int
  116. statdb_node_remove(const char *filename)
  117. {
  118. struct filenamenode *file;
  119. file = findnamenode(filename, fnn_nonew);
  120. if (!file || (file && !file->statoverride))
  121. return 0;
  122. file->statoverride = NULL;
  123. return 1;
  124. }
  125. static void
  126. statdb_node_apply(const char *filename, struct file_stat *filestat)
  127. {
  128. if (chown(filename, filestat->uid, filestat->gid) < 0)
  129. ohshite(_("error setting ownership of `%.255s'"), filename);
  130. if (chmod(filename, filestat->mode))
  131. ohshite(_("error setting permissions of `%.255s'"), filename);
  132. }
  133. static void
  134. statdb_node_print(FILE *out, struct filenamenode *file)
  135. {
  136. struct file_stat *filestat = file->statoverride;
  137. struct passwd *pw;
  138. struct group *gr;
  139. if (!filestat)
  140. return;
  141. pw = getpwuid(filestat->uid);
  142. if (pw)
  143. fprintf(out, "%s ", pw->pw_name);
  144. else
  145. fprintf(out, "#%d ", filestat->uid);
  146. gr = getgrgid(filestat->gid);
  147. if (gr)
  148. fprintf(out, "%s ", gr->gr_name);
  149. else
  150. fprintf(out, "#%d ", filestat->gid);
  151. fprintf(out, "%o %s\n", filestat->mode, file->name);
  152. }
  153. static void
  154. statdb_write(void)
  155. {
  156. char *dbname, *dbname_new, *dbname_old;
  157. FILE *dbfile;
  158. struct fileiterator *i;
  159. struct filenamenode *file;
  160. dbname = dpkg_db_get_path(STATOVERRIDEFILE);
  161. m_asprintf(&dbname_new, "%s%s", dbname, NEWDBEXT);
  162. m_asprintf(&dbname_old, "%s%s", dbname, OLDDBEXT);
  163. dbfile = fopen(dbname_new, "w");
  164. if (!dbfile)
  165. ohshite(_("cannot open new statoverride file"));
  166. i = iterfilestart();
  167. while ((file = iterfilenext(i)))
  168. statdb_node_print(dbfile, file);
  169. iterfileend(i);
  170. if (fflush(dbfile))
  171. ohshite(_("unable to flush file '%s'"), dbname_new);
  172. if (fsync(fileno(dbfile)))
  173. ohshite(_("unable to sync file '%s'"), dbname_new);
  174. fclose(dbfile);
  175. chmod(dbname_new, 0644);
  176. if (unlink(dbname_old) && errno != ENOENT)
  177. ohshite(_("error removing statoverride-old"));
  178. if (link(dbname, dbname_old) && errno != ENOENT)
  179. ohshite(_("error creating new statoverride-old"));
  180. if (rename(dbname_new, dbname))
  181. ohshite(_("error installing new statoverride"));
  182. dir_sync_path(admindir);
  183. free(dbname);
  184. free(dbname_new);
  185. free(dbname_old);
  186. }
  187. static int
  188. statoverride_add(const char *const *argv)
  189. {
  190. const char *user = argv[0];
  191. const char *group = argv[1];
  192. const char *mode = argv[2];
  193. const char *path = argv[3];
  194. char *filename;
  195. struct file_stat **filestat;
  196. if (!user || !group || !mode || !path || argv[4])
  197. badusage(_("--add needs four arguments"));
  198. if (strchr(path, '\n'))
  199. badusage(_("file may not contain newlines"));
  200. filename = path_cleanup(path);
  201. filestat = statdb_node_find(filename);
  202. if (*filestat != NULL) {
  203. if (opt_force)
  204. warning(_("An override for '%s' already exists, "
  205. "but --force specified so will be ignored."),
  206. filename);
  207. else
  208. ohshit(_("An override for '%s' already exists, "
  209. "aborting."), filename);
  210. }
  211. *filestat = statdb_node_new(user, group, mode);
  212. if (opt_update) {
  213. struct stat st;
  214. if (stat(filename, &st) == 0)
  215. statdb_node_apply(filename, *filestat);
  216. else if (opt_verbose)
  217. warning(_("--update given but %s does not exist"),
  218. filename);
  219. }
  220. statdb_write();
  221. free(filename);
  222. return 0;
  223. }
  224. static int
  225. statoverride_remove(const char *const *argv)
  226. {
  227. const char *path = argv[0];
  228. char *filename;
  229. if (!path || argv[1])
  230. badusage(_("--%s needs a single argument"), "remove");
  231. filename = path_cleanup(path);
  232. if (!statdb_node_remove(filename)) {
  233. if (opt_verbose)
  234. warning(_("No override present."));
  235. if (opt_force)
  236. exit(0);
  237. else
  238. exit(2);
  239. }
  240. if (opt_update && opt_verbose)
  241. warning(_("--update is useless for --remove"));
  242. statdb_write();
  243. free(filename);
  244. return 0;
  245. }
  246. static int
  247. statoverride_list(const char *const *argv)
  248. {
  249. struct fileiterator *i;
  250. struct filenamenode *file;
  251. const char *thisarg;
  252. struct glob_node *glob_list = NULL;
  253. int ret = 1;
  254. while ((thisarg = *argv++)) {
  255. char *pattern = path_cleanup(thisarg);
  256. glob_list_prepend(&glob_list, pattern);
  257. }
  258. if (glob_list == NULL)
  259. glob_list_prepend(&glob_list, m_strdup("*"));
  260. i = iterfilestart();
  261. while ((file = iterfilenext(i))) {
  262. struct glob_node *g;
  263. for (g = glob_list; g; g = g->next) {
  264. if (fnmatch(g->pattern, file->name, 0) == 0) {
  265. statdb_node_print(stdout, file);
  266. ret = 0;
  267. break;
  268. }
  269. }
  270. }
  271. iterfileend(i);
  272. glob_list_free(glob_list);
  273. return ret;
  274. }
  275. static const struct cmdinfo cmdinfos[] = {
  276. ACTION("add", 0, act_install, statoverride_add),
  277. ACTION("remove", 0, act_remove, statoverride_remove),
  278. ACTION("list", 0, act_listfiles, statoverride_list),
  279. { "admindir", 0, 1, NULL, &admindir, NULL },
  280. { "quiet", 0, 0, &opt_verbose, NULL, NULL, 0 },
  281. { "force", 0, 0, &opt_force, NULL, NULL, 1 },
  282. { "update", 0, 0, &opt_update, NULL, NULL, 1 },
  283. { "help", 'h', 0, NULL, NULL, usage },
  284. { "version", 0, 0, NULL, NULL, printversion },
  285. { NULL, 0 }
  286. };
  287. int
  288. main(int argc, const char *const *argv)
  289. {
  290. int (*actionfunction)(const char *const *argv);
  291. int ret;
  292. setlocale(LC_ALL, "");
  293. bindtextdomain(PACKAGE, LOCALEDIR);
  294. textdomain(PACKAGE);
  295. standard_startup();
  296. myopt(&argv, cmdinfos);
  297. admindir = dpkg_db_set_dir(admindir);
  298. if (!cipaction)
  299. badusage(_("need an action option"));
  300. setvbuf(stdout, NULL, _IONBF, 0);
  301. filesdbinit();
  302. ensure_statoverrides();
  303. actionfunction = (int (*)(const char *const *))cipaction->arg_func;
  304. ret = actionfunction(argv);
  305. standard_shutdown();
  306. return ret;
  307. }