add-remove-shell.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /*
  2. * add-shell and remove-shell implementation for busybox
  3. *
  4. * Copyright (C) 2010 Nokia Corporation. All rights reserved.
  5. * Written by Alexander Shishkin <virtuoso@slind.org>
  6. *
  7. * Licensed under GPLv2 or later, see the LICENSE file in this source tree
  8. * for details.
  9. */
  10. //config:config ADD_SHELL
  11. //config: bool "add-shell (2.8 kb)"
  12. //config: default y if DESKTOP
  13. //config: help
  14. //config: Add shells to /etc/shells.
  15. //config:
  16. //config:config REMOVE_SHELL
  17. //config: bool "remove-shell (2.7 kb)"
  18. //config: default y if DESKTOP
  19. //config: help
  20. //config: Remove shells from /etc/shells.
  21. // APPLET_NOEXEC:name main location suid_type help
  22. //applet:IF_ADD_SHELL( APPLET_NOEXEC(add-shell , add_remove_shell, BB_DIR_USR_SBIN, BB_SUID_DROP, add_shell ))
  23. //applet:IF_REMOVE_SHELL(APPLET_NOEXEC(remove-shell, add_remove_shell, BB_DIR_USR_SBIN, BB_SUID_DROP, remove_shell))
  24. //kbuild:lib-$(CONFIG_ADD_SHELL) += add-remove-shell.o
  25. //kbuild:lib-$(CONFIG_REMOVE_SHELL) += add-remove-shell.o
  26. //usage:#define add_shell_trivial_usage
  27. //usage: "SHELL..."
  28. //usage:#define add_shell_full_usage "\n\n"
  29. //usage: "Add SHELLs to /etc/shells"
  30. //usage:#define remove_shell_trivial_usage
  31. //usage: "SHELL..."
  32. //usage:#define remove_shell_full_usage "\n\n"
  33. //usage: "Remove SHELLs from /etc/shells"
  34. #include "libbb.h"
  35. #define SHELLS_FILE "/etc/shells"
  36. #define REMOVE_SHELL (ENABLE_REMOVE_SHELL && (!ENABLE_ADD_SHELL || applet_name[0] == 'r'))
  37. #define ADD_SHELL (ENABLE_ADD_SHELL && (!ENABLE_REMOVE_SHELL || applet_name[0] == 'a'))
  38. #define dont_add ((char*)(uintptr_t)1)
  39. int add_remove_shell_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  40. int add_remove_shell_main(int argc UNUSED_PARAM, char **argv)
  41. {
  42. FILE *orig_fp;
  43. char *orig_fn;
  44. char *new_fn;
  45. struct stat sb;
  46. sb.st_mode = 0666;
  47. argv++;
  48. orig_fn = xmalloc_follow_symlinks(SHELLS_FILE);
  49. if (!orig_fn)
  50. return EXIT_FAILURE;
  51. orig_fp = fopen_for_read(orig_fn);
  52. if (orig_fp)
  53. xfstat(fileno(orig_fp), &sb, orig_fn);
  54. new_fn = xasprintf("%s.tmp", orig_fn);
  55. /*
  56. * O_TRUNC or O_EXCL? At the first glance, O_EXCL looks better,
  57. * since it prevents races. But: (1) it requires a retry loop,
  58. * (2) if /etc/shells.tmp is *stale*, then retry loop
  59. * with O_EXCL will never succeed - it should have a timeout,
  60. * after which it should revert to O_TRUNC.
  61. * For now, I settle for O_TRUNC instead.
  62. */
  63. xmove_fd(xopen3(new_fn, O_WRONLY | O_CREAT | O_TRUNC, sb.st_mode), STDOUT_FILENO);
  64. /* TODO?
  65. xfchown(STDOUT_FILENO, sb.st_uid, sb.st_gid);
  66. */
  67. if (orig_fp) {
  68. /* Copy old file, possibly skipping removed shell names */
  69. char *line;
  70. while ((line = xmalloc_fgetline(orig_fp)) != NULL) {
  71. char **cpp = argv;
  72. while (*cpp) {
  73. if (*cpp != dont_add && strcmp(*cpp, line) == 0) {
  74. /* Old file has this shell name */
  75. if (REMOVE_SHELL) {
  76. /* we are remove-shell */
  77. /* delete this name by not copying it */
  78. goto next_line;
  79. }
  80. /* we are add-shell */
  81. /* mark this name as "do not add" */
  82. *cpp = dont_add;
  83. }
  84. cpp++;
  85. }
  86. /* copy shell name from old to new file */
  87. puts(line);
  88. next_line:
  89. free(line);
  90. }
  91. if (ENABLE_FEATURE_CLEAN_UP)
  92. fclose(orig_fp);
  93. }
  94. if (ADD_SHELL) {
  95. char **cpp = argv;
  96. while (*cpp) {
  97. if (*cpp != dont_add)
  98. puts(*cpp);
  99. cpp++;
  100. }
  101. }
  102. /* Ensure we wrote out everything */
  103. if (fclose(stdout) != 0) {
  104. xunlink(new_fn);
  105. bb_perror_msg_and_die("%s: write error", new_fn);
  106. }
  107. /* Small hole: if rename fails, /etc/shells.tmp is not removed */
  108. xrename(new_fn, orig_fn);
  109. if (ENABLE_FEATURE_CLEAN_UP) {
  110. free(orig_fn);
  111. free(new_fn);
  112. }
  113. return EXIT_SUCCESS;
  114. }