statcmd.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. /*
  2. * dpkg-statoverride - override ownership and mode of files
  3. *
  4. * Copyright © 2000, 2001 Wichert Akkerman <wakkerma@debian.org>
  5. * Copyright © 2006-2015 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 <https://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/options.h>
  43. #include "main.h"
  44. #include "filesdb.h"
  45. static const char printforhelp[] = N_(
  46. "Use --help for help about overriding file stat information.");
  47. static void DPKG_ATTR_NORET
  48. printversion(const struct cmdinfo *cip, const char *value)
  49. {
  50. printf(_("Debian %s version %s.\n"), dpkg_get_progname(),
  51. DPKG_VERSION_ARCH);
  52. printf(_(
  53. "This is free software; see the GNU General Public License version 2 or\n"
  54. "later for copying conditions. There is NO warranty.\n"));
  55. m_output(stdout, _("<standard output>"));
  56. exit(0);
  57. }
  58. static void DPKG_ATTR_NORET
  59. usage(const struct cmdinfo *cip, const char *value)
  60. {
  61. printf(_(
  62. "Usage: %s [<option> ...] <command>\n"
  63. "\n"), dpkg_get_progname());
  64. printf(_(
  65. "Commands:\n"
  66. " --add <owner> <group> <mode> <path>\n"
  67. " add a new <path> entry into the database.\n"
  68. " --remove <path> remove <path> from the database.\n"
  69. " --list [<glob-pattern>] list current overrides in the database.\n"
  70. "\n"));
  71. printf(_(
  72. "Options:\n"
  73. " --admindir <directory> set the directory with the statoverride file.\n"
  74. " --update immediately update <path> permissions.\n"
  75. " --force force an action even if a sanity check fails.\n"
  76. " --quiet quiet operation, minimal output.\n"
  77. " --help show this help message.\n"
  78. " --version show the version.\n"
  79. "\n"));
  80. m_output(stdout, _("<standard output>"));
  81. exit(0);
  82. }
  83. static const char *admindir;
  84. static int opt_verbose = 1;
  85. static int opt_force = 0;
  86. static int opt_update = 0;
  87. static char *
  88. path_cleanup(const char *path)
  89. {
  90. char *new_path = m_strdup(path);
  91. path_trim_slash_slashdot(new_path);
  92. if (opt_verbose && strcmp(path, new_path) != 0)
  93. warning(_("stripping trailing /"));
  94. return new_path;
  95. }
  96. static struct file_stat *
  97. statdb_node_new(const char *user, const char *group, const char *mode)
  98. {
  99. struct file_stat *filestat;
  100. filestat = nfmalloc(sizeof(*filestat));
  101. filestat->uid = statdb_parse_uid(user);
  102. if (filestat->uid == (uid_t)-1)
  103. ohshit(_("user '%s' does not exist"), user);
  104. filestat->uname = NULL;
  105. filestat->gid = statdb_parse_gid(group);
  106. if (filestat->gid == (gid_t)-1)
  107. ohshit(_("group '%s' does not exist"), group);
  108. filestat->gname = NULL;
  109. filestat->mode = statdb_parse_mode(mode);
  110. return filestat;
  111. }
  112. static struct file_stat **
  113. statdb_node_find(const char *filename)
  114. {
  115. struct filenamenode *file;
  116. file = findnamenode(filename, 0);
  117. return &file->statoverride;
  118. }
  119. static int
  120. statdb_node_remove(const char *filename)
  121. {
  122. struct filenamenode *file;
  123. file = findnamenode(filename, fnn_nonew);
  124. if (!file || (file && !file->statoverride))
  125. return 0;
  126. file->statoverride = NULL;
  127. return 1;
  128. }
  129. static void
  130. statdb_node_apply(const char *filename, struct file_stat *filestat)
  131. {
  132. if (chown(filename, filestat->uid, filestat->gid) < 0)
  133. ohshite(_("error setting ownership of '%.255s'"), filename);
  134. if (chmod(filename, filestat->mode))
  135. ohshite(_("error setting permissions of '%.255s'"), filename);
  136. dpkg_selabel_load();
  137. dpkg_selabel_set_context(filename, filename, filestat->mode);
  138. dpkg_selabel_close();
  139. }
  140. static void
  141. statdb_node_print(FILE *out, struct filenamenode *file)
  142. {
  143. struct file_stat *filestat = file->statoverride;
  144. struct passwd *pw;
  145. struct group *gr;
  146. if (!filestat)
  147. return;
  148. pw = getpwuid(filestat->uid);
  149. if (pw)
  150. fprintf(out, "%s ", pw->pw_name);
  151. else if (filestat->uname)
  152. fprintf(out, "%s ", filestat->uname);
  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 if (filestat->gname)
  159. fprintf(out, "%s ", filestat->gname);
  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. char *dbname;
  168. struct atomic_file *dbfile;
  169. struct fileiterator *iter;
  170. struct filenamenode *file;
  171. dbname = dpkg_db_get_path(STATOVERRIDEFILE);
  172. dbfile = atomic_file_new(dbname, ATOMIC_FILE_BACKUP);
  173. atomic_file_open(dbfile);
  174. iter = files_db_iter_new();
  175. while ((file = files_db_iter_next(iter)))
  176. statdb_node_print(dbfile->fp, file);
  177. files_db_iter_free(iter);
  178. atomic_file_sync(dbfile);
  179. atomic_file_close(dbfile);
  180. atomic_file_commit(dbfile);
  181. atomic_file_free(dbfile);
  182. dir_sync_path(dpkg_db_get_dir());
  183. free(dbname);
  184. }
  185. static int
  186. statoverride_add(const char *const *argv)
  187. {
  188. const char *user = argv[0];
  189. const char *group = argv[1];
  190. const char *mode = argv[2];
  191. const char *path = argv[3];
  192. char *filename;
  193. struct file_stat **filestat;
  194. if (!user || !group || !mode || !path || argv[4])
  195. badusage(_("--%s needs four arguments"), cipaction->olong);
  196. if (strchr(path, '\n'))
  197. badusage(_("path may not contain newlines"));
  198. filename = path_cleanup(path);
  199. filestat = statdb_node_find(filename);
  200. if (*filestat != NULL) {
  201. if (opt_force)
  202. warning(_("an override for '%s' already exists, "
  203. "but --force specified so will be ignored"),
  204. filename);
  205. else
  206. ohshit(_("an override for '%s' already exists; "
  207. "aborting"), filename);
  208. }
  209. *filestat = statdb_node_new(user, group, mode);
  210. if (opt_update) {
  211. struct stat st;
  212. if (stat(filename, &st) == 0)
  213. statdb_node_apply(filename, *filestat);
  214. else if (opt_verbose)
  215. warning(_("--update given but %s does not exist"),
  216. filename);
  217. }
  218. statdb_write();
  219. free(filename);
  220. return 0;
  221. }
  222. static int
  223. statoverride_remove(const char *const *argv)
  224. {
  225. const char *path = argv[0];
  226. char *filename;
  227. if (!path || argv[1])
  228. badusage(_("--%s needs a single argument"), "remove");
  229. filename = path_cleanup(path);
  230. if (!statdb_node_remove(filename)) {
  231. if (opt_verbose)
  232. warning(_("no override present"));
  233. if (opt_force)
  234. return 0;
  235. else
  236. return 2;
  237. }
  238. if (opt_update && opt_verbose)
  239. warning(_("--update is useless for --remove"));
  240. statdb_write();
  241. free(filename);
  242. return 0;
  243. }
  244. static int
  245. statoverride_list(const char *const *argv)
  246. {
  247. struct fileiterator *iter;
  248. struct filenamenode *file;
  249. const char *thisarg;
  250. struct glob_node *glob_list = NULL;
  251. int ret = 1;
  252. while ((thisarg = *argv++)) {
  253. char *pattern = path_cleanup(thisarg);
  254. glob_list_prepend(&glob_list, pattern);
  255. }
  256. if (glob_list == NULL)
  257. glob_list_prepend(&glob_list, m_strdup("*"));
  258. iter = files_db_iter_new();
  259. while ((file = files_db_iter_next(iter))) {
  260. struct glob_node *g;
  261. for (g = glob_list; g; g = g->next) {
  262. if (fnmatch(g->pattern, file->name, 0) == 0) {
  263. statdb_node_print(stdout, file);
  264. ret = 0;
  265. break;
  266. }
  267. }
  268. }
  269. files_db_iter_free(iter);
  270. glob_list_free(glob_list);
  271. return ret;
  272. }
  273. static const struct cmdinfo cmdinfos[] = {
  274. ACTION("add", 0, act_install, statoverride_add),
  275. ACTION("remove", 0, act_remove, statoverride_remove),
  276. ACTION("list", 0, act_listfiles, statoverride_list),
  277. { "admindir", 0, 1, NULL, &admindir, NULL },
  278. { "quiet", 0, 0, &opt_verbose, NULL, NULL, 0 },
  279. { "force", 0, 0, &opt_force, NULL, NULL, 1 },
  280. { "update", 0, 0, &opt_update, NULL, NULL, 1 },
  281. { "help", '?', 0, NULL, NULL, usage },
  282. { "version", 0, 0, NULL, NULL, printversion },
  283. { NULL, 0 }
  284. };
  285. int
  286. main(int argc, const char *const *argv)
  287. {
  288. int ret;
  289. dpkg_locales_init(PACKAGE);
  290. dpkg_program_init("dpkg-statoverride");
  291. dpkg_options_parse(&argv, cmdinfos, printforhelp);
  292. admindir = dpkg_db_set_dir(admindir);
  293. if (!cipaction)
  294. badusage(_("need an action option"));
  295. filesdbinit();
  296. ensure_statoverrides(STATDB_PARSE_LAX);
  297. ret = cipaction->action(argv);
  298. dpkg_program_done();
  299. return ret;
  300. }