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"
  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"
  18. //config: default y if DESKTOP
  19. //config: help
  20. //config: Remove shells from /etc/shells.
  21. //applet:IF_ADD_SHELL( APPLET_ODDNAME(add-shell , add_remove_shell, BB_DIR_USR_SBIN, BB_SUID_DROP, add_shell ))
  22. //applet:IF_REMOVE_SHELL(APPLET_ODDNAME(remove-shell, add_remove_shell, BB_DIR_USR_SBIN, BB_SUID_DROP, remove_shell))
  23. //kbuild:lib-$(CONFIG_ADD_SHELL) += add-remove-shell.o
  24. //kbuild:lib-$(CONFIG_REMOVE_SHELL) += add-remove-shell.o
  25. //usage:#define add_shell_trivial_usage
  26. //usage: "SHELL..."
  27. //usage:#define add_shell_full_usage "\n\n"
  28. //usage: "Add SHELLs to /etc/shells"
  29. //usage:#define remove_shell_trivial_usage
  30. //usage: "SHELL..."
  31. //usage:#define remove_shell_full_usage "\n\n"
  32. //usage: "Remove SHELLs from /etc/shells"
  33. #include "libbb.h"
  34. #define SHELLS_FILE "/etc/shells"
  35. #define REMOVE_SHELL (ENABLE_REMOVE_SHELL && (!ENABLE_ADD_SHELL || applet_name[0] == 'r'))
  36. #define ADD_SHELL (ENABLE_ADD_SHELL && (!ENABLE_REMOVE_SHELL || applet_name[0] == 'a'))
  37. /* NB: we use the _address_, not the value, of this string
  38. * as a "special value of pointer" in the code.
  39. */
  40. static const char dont_add[] ALIGN1 = "\n";
  41. int add_remove_shell_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  42. int add_remove_shell_main(int argc UNUSED_PARAM, char **argv)
  43. {
  44. FILE *orig_fp;
  45. char *orig_fn;
  46. char *new_fn;
  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. new_fn = xasprintf("%s.tmp", orig_fn);
  53. /*
  54. * O_TRUNC or O_EXCL? At the first glance, O_EXCL looks better,
  55. * since it prevents races. But: (1) it requires a retry loop,
  56. * (2) if /etc/shells.tmp is *stale*, then retry loop
  57. * with O_EXCL will never succeed - it should have a timeout,
  58. * after which it should revert to O_TRUNC.
  59. * For now, I settle for O_TRUNC instead.
  60. */
  61. xmove_fd(xopen(new_fn, O_WRONLY | O_CREAT | O_TRUNC), STDOUT_FILENO);
  62. /* TODO:
  63. struct stat sb;
  64. xfstat(fileno(orig_fp), &sb);
  65. xfchown(STDOUT_FILENO, sb.st_uid, sb.st_gid);
  66. xfchmod(STDOUT_FILENO, sb.st_mode);
  67. */
  68. if (orig_fp) {
  69. /* Copy old file, possibly skipping removed shell names */
  70. char *line;
  71. while ((line = xmalloc_fgetline(orig_fp)) != NULL) {
  72. char **cpp = argv;
  73. while (*cpp) {
  74. if (strcmp(*cpp, line) == 0) {
  75. /* Old file has this shell name */
  76. if (REMOVE_SHELL) {
  77. /* we are remove-shell */
  78. /* delete this name by not copying it */
  79. goto next_line;
  80. }
  81. /* we are add-shell */
  82. /* mark this name as "do not add" */
  83. *cpp = (char*)dont_add;
  84. }
  85. cpp++;
  86. }
  87. /* copy shell name from old to new file */
  88. puts(line);
  89. next_line:
  90. free(line);
  91. }
  92. if (ENABLE_FEATURE_CLEAN_UP)
  93. fclose(orig_fp);
  94. }
  95. if (ADD_SHELL) {
  96. char **cpp = argv;
  97. while (*cpp) {
  98. if (*cpp != dont_add)
  99. puts(*cpp);
  100. cpp++;
  101. }
  102. }
  103. /* Ensure we wrote out everything */
  104. if (fclose(stdout) != 0) {
  105. xunlink(new_fn);
  106. bb_perror_msg_and_die("%s: write error", new_fn);
  107. }
  108. /* Small hole: if rename fails, /etc/shells.tmp is not removed */
  109. xrename(new_fn, orig_fn);
  110. if (ENABLE_FEATURE_CLEAN_UP) {
  111. free(orig_fn);
  112. free(new_fn);
  113. }
  114. return EXIT_SUCCESS;
  115. }