applets.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * Utility routines.
  4. *
  5. * Copyright (C) tons of folks. Tracking down who wrote what
  6. * isn't something I'm going to worry about... If you wrote something
  7. * here, please feel free to acknowledge your work.
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  22. *
  23. * Based in part on code from sash, Copyright (c) 1999 by David I. Bell
  24. * Permission has been granted to redistribute this code under the GPL.
  25. *
  26. */
  27. #include <unistd.h>
  28. #include <stdio.h>
  29. #include <stdlib.h>
  30. #include <string.h>
  31. #include <assert.h>
  32. #include "busybox.h"
  33. const char usage_messages[] =
  34. #define MAKE_USAGE
  35. #include "usage.h"
  36. #include "applets.h"
  37. ;
  38. #undef MAKE_USAGE
  39. #undef APPLET
  40. #undef APPLET_NOUSAGE
  41. #undef PROTOTYPES
  42. #include "applets.h"
  43. static struct BB_applet *applet_using;
  44. /* The -1 arises because of the {0,NULL,0,-1} entry above. */
  45. const size_t NUM_APPLETS = (sizeof (applets) / sizeof (struct BB_applet) - 1);
  46. #ifdef CONFIG_FEATURE_SUID
  47. static void check_suid (struct BB_applet *app);
  48. #ifdef CONFIG_FEATURE_SUID_CONFIG
  49. #include <sys/stat.h>
  50. #include <ctype.h>
  51. #include "pwd_.h"
  52. #include "grp_.h"
  53. static void parse_config_file (void);
  54. #define CONFIG_FILE "/etc/busybox.conf"
  55. /* applets [] is const, so we have to define this "override" structure */
  56. struct BB_suid_config
  57. {
  58. struct BB_applet *m_applet;
  59. uid_t m_uid;
  60. gid_t m_gid;
  61. mode_t m_mode;
  62. struct BB_suid_config *m_next;
  63. };
  64. static struct BB_suid_config *suid_config;
  65. static int suid_cfg_readable;
  66. #endif /* CONFIG_FEATURE_SUID_CONFIG */
  67. #endif /* CONFIG_FEATURE_SUID */
  68. extern void
  69. bb_show_usage (void)
  70. {
  71. const char *format_string;
  72. const char *usage_string = usage_messages;
  73. int i;
  74. for (i = applet_using - applets; i > 0;) {
  75. if (!*usage_string++) {
  76. --i;
  77. }
  78. }
  79. format_string = "%s\n\nUsage: %s %s\n\n";
  80. if (*usage_string == '\b')
  81. format_string = "%s\n\nNo help available.\n\n";
  82. fprintf (stderr, format_string, bb_msg_full_version, applet_using->name,
  83. usage_string);
  84. exit (EXIT_FAILURE);
  85. }
  86. static int
  87. applet_name_compare (const void *x, const void *y)
  88. {
  89. const char *name = x;
  90. const struct BB_applet *applet = y;
  91. return strcmp (name, applet->name);
  92. }
  93. extern const size_t NUM_APPLETS;
  94. struct BB_applet *
  95. find_applet_by_name (const char *name)
  96. {
  97. return bsearch (name, applets, NUM_APPLETS, sizeof (struct BB_applet),
  98. applet_name_compare);
  99. }
  100. void
  101. run_applet_by_name (const char *name, int argc, char **argv)
  102. {
  103. static int recurse_level = 0;
  104. extern int been_there_done_that; /* From busybox.c */
  105. #ifdef CONFIG_FEATURE_SUID_CONFIG
  106. if (recurse_level == 0)
  107. parse_config_file ();
  108. #endif
  109. recurse_level++;
  110. /* Do a binary search to find the applet entry given the name. */
  111. if ((applet_using = find_applet_by_name (name)) != NULL) {
  112. bb_applet_name = applet_using->name;
  113. if (argv[1] && strcmp (argv[1], "--help") == 0) {
  114. if (strcmp (applet_using->name, "busybox") == 0) {
  115. if (argv[2])
  116. applet_using = find_applet_by_name (argv[2]);
  117. else
  118. applet_using = NULL;
  119. }
  120. if (applet_using)
  121. bb_show_usage ();
  122. been_there_done_that = 1;
  123. busybox_main (0, NULL);
  124. }
  125. #ifdef CONFIG_FEATURE_SUID
  126. check_suid (applet_using);
  127. #endif
  128. exit ((*(applet_using->main)) (argc, argv));
  129. }
  130. /* Just in case they have renamed busybox - Check argv[1] */
  131. if (recurse_level == 1) {
  132. run_applet_by_name ("busybox", argc, argv);
  133. }
  134. recurse_level--;
  135. }
  136. #ifdef CONFIG_FEATURE_SUID
  137. #ifdef CONFIG_FEATURE_SUID_CONFIG
  138. /* check if u is member of group g */
  139. static int
  140. ingroup (uid_t u, gid_t g)
  141. {
  142. struct group *grp = getgrgid (g);
  143. if (grp) {
  144. char **mem;
  145. for (mem = grp->gr_mem; *mem; mem++) {
  146. struct passwd *pwd = getpwnam (*mem);
  147. if (pwd && (pwd->pw_uid == u))
  148. return 1;
  149. }
  150. }
  151. return 0;
  152. }
  153. #endif
  154. void
  155. check_suid (struct BB_applet *applet)
  156. {
  157. uid_t ruid = getuid (); /* real [ug]id */
  158. uid_t rgid = getgid ();
  159. #ifdef CONFIG_FEATURE_SUID_CONFIG
  160. if (suid_cfg_readable) {
  161. struct BB_suid_config *sct;
  162. for (sct = suid_config; sct; sct = sct->m_next) {
  163. if (sct->m_applet == applet)
  164. break;
  165. }
  166. if (sct) {
  167. mode_t m = sct->m_mode;
  168. if (sct->m_uid == ruid) /* same uid */
  169. m >>= 6;
  170. else if ((sct->m_gid == rgid) || ingroup (ruid, sct->m_gid)) /* same group / in group */
  171. m >>= 3;
  172. if (!(m & S_IXOTH)) /* is x bit not set ? */
  173. bb_error_msg_and_die ("You have no permission to run this applet!");
  174. if ((sct->m_mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP)) { /* *both* have to be set for sgid */
  175. if (setegid (sct->m_gid))
  176. bb_error_msg_and_die
  177. ("BusyBox binary has insufficient rights to set proper GID for applet!");
  178. } else
  179. setgid (rgid); /* no sgid -> drop */
  180. if (sct->m_mode & S_ISUID) {
  181. if (seteuid (sct->m_uid))
  182. bb_error_msg_and_die
  183. ("BusyBox binary has insufficient rights to set proper UID for applet!");
  184. } else
  185. setuid (ruid); /* no suid -> drop */
  186. } else {
  187. /* default: drop all priviledges */
  188. setgid (rgid);
  189. setuid (ruid);
  190. }
  191. return;
  192. } else {
  193. #ifndef CONFIG_FEATURE_SUID_CONFIG_QUIET
  194. static int onetime = 0;
  195. if (!onetime) {
  196. onetime = 1;
  197. fprintf (stderr, "Using fallback suid method\n");
  198. }
  199. #endif
  200. }
  201. #endif
  202. if (applet->need_suid == _BB_SUID_ALWAYS) {
  203. if (geteuid () != 0)
  204. bb_error_msg_and_die ("This applet requires root priviledges!");
  205. } else if (applet->need_suid == _BB_SUID_NEVER) {
  206. setgid (rgid); /* drop all priviledges */
  207. setuid (ruid);
  208. }
  209. }
  210. #ifdef CONFIG_FEATURE_SUID_CONFIG
  211. /* This should probably be a libbb routine. In that case,
  212. * I'd probably rename it to something like bb_trimmed_slice.
  213. */
  214. static char *get_trimmed_slice(char *s, char *e)
  215. {
  216. /* First, consider the value at e to be nul and back up until we
  217. * reach a non-space char. Set the char after that (possibly at
  218. * the original e) to nul. */
  219. while (e-- > s) {
  220. if (!isspace(*e)) {
  221. break;
  222. }
  223. }
  224. e[1] = 0;
  225. /* Next, advance past all leading space and return a ptr to the
  226. * first non-space char; possibly the terminating nul. */
  227. return (char *) bb_skip_whitespace(s);
  228. }
  229. #define parse_error(x) { err=x; goto pe_label; }
  230. /* Don't depend on the tools to combine strings. */
  231. static const char config_file[] = CONFIG_FILE;
  232. /* There are 4 chars + 1 nul for each of user/group/other. */
  233. static const char mode_chars[] = "Ssx-\0Ssx-\0Ttx-";
  234. /* We don't supply a value for the nul, so an index adjustment is
  235. * necessary below. Also, we use unsigned short here to save some
  236. * space even though these are really mode_t values. */
  237. static const unsigned short mode_mask[] = {
  238. /* SST sst xxx --- */
  239. S_ISUID, S_ISUID|S_IXUSR, S_IXUSR, 0, /* user */
  240. S_ISGID, S_ISGID|S_IXGRP, S_IXGRP, 0, /* group */
  241. 0, S_IXOTH, S_IXOTH, 0 /* other */
  242. };
  243. static void parse_config_file(void)
  244. {
  245. struct BB_suid_config *sct_head;
  246. struct BB_suid_config *sct;
  247. struct BB_applet *applet;
  248. FILE *f;
  249. char *err;
  250. char *s;
  251. char *e;
  252. int i, lc, section;
  253. char buffer[256];
  254. struct stat st;
  255. assert(!suid_config); /* Should be set to NULL by bss init. */
  256. if ((stat(config_file, &st) != 0) /* No config file? */
  257. || !S_ISREG(st.st_mode) /* Not a regular file? */
  258. || (st.st_uid != 0) /* Not owned by root? */
  259. || (st.st_mode & (S_IWGRP | S_IWOTH)) /* Writable by non-root? */
  260. || !(f = fopen(config_file, "r")) /* Can not open? */
  261. ) {
  262. return;
  263. }
  264. suid_cfg_readable = 1;
  265. sct_head = NULL;
  266. section = lc = 0;
  267. do {
  268. s = buffer;
  269. if (!fgets(s, sizeof(buffer), f)) { /* Are we done? */
  270. if (ferror(f)) { /* Make sure it wasn't a read error. */
  271. parse_error("reading");
  272. }
  273. fclose(f);
  274. suid_config = sct_head; /* Success, so set the pointer. */
  275. return;
  276. }
  277. lc++; /* Got a (partial) line. */
  278. /* If a line is too long for our buffer, we consider it an error.
  279. * The following test does mistreat one corner case though.
  280. * If the final line of the file does not end with a newline and
  281. * yet exactly fills the buffer, it will be treated as too long
  282. * even though there isn't really a problem. But it isn't really
  283. * worth adding code to deal with such an unlikely situation, and
  284. * we do err on the side of caution. Besides, the line would be
  285. * too long if it did end with a newline. */
  286. if (!strchr(s, '\n') && !feof(f)) {
  287. parse_error("line too long");
  288. }
  289. /* Trim leading and trailing whitespace, ignoring comments, and
  290. * check if the resulting string is empty. */
  291. if (!*(s = get_trimmed_slice(s, strchrnul(s, '#')))) {
  292. continue;
  293. }
  294. /* Check for a section header. */
  295. if (*s == '[') {
  296. /* Unlike the old code, we ignore leading and trailing
  297. * whitespace for the section name. We also require that
  298. * there are no stray characters after the closing bracket. */
  299. if (!(e = strchr(s, ']')) /* Missing right bracket? */
  300. || e[1] /* Trailing characters? */
  301. || !*(s = get_trimmed_slice(s+1, e)) /* Missing name? */
  302. ) {
  303. parse_error("section header");
  304. }
  305. /* Right now we only have one section so just check it.
  306. * If more sections are added in the future, please don't
  307. * resort to cascading ifs with multiple strcasecmp calls.
  308. * That kind of bloated code is all too common. A loop
  309. * and a string table would be a better choice unless the
  310. * number of sections is very small. */
  311. if (strcasecmp(s, "SUID") == 0) {
  312. section = 1;
  313. continue;
  314. }
  315. section = -1; /* Unknown section so set to skip. */
  316. continue;
  317. }
  318. /* Process sections. */
  319. if (section == 1) { /* SUID */
  320. /* Since we trimmed leading and trailing space above, we're
  321. * now looking for strings of the form
  322. * <key>[::space::]*=[::space::]*<value>
  323. * where both key and value could contain inner whitespace. */
  324. /* First get the key (an applet name in our case). */
  325. if (!!(e = strchr(s, '='))) {
  326. s = get_trimmed_slice(s, e);
  327. }
  328. if (!e || !*s) { /* Missing '=' or empty key. */
  329. parse_error("keyword");
  330. }
  331. /* Ok, we have an applet name. Process the rhs if this
  332. * applet is currently built in and ignore it otherwise.
  333. * Note: This can hide config file bugs which only pop
  334. * up when the busybox configuration is changed. */
  335. if ((applet = find_applet_by_name(s))) {
  336. /* Note: We currently don't check for duplicates!
  337. * The last config line for each applet will be the
  338. * one used since we insert at the head of the list.
  339. * I suppose this could be considered a feature. */
  340. sct = xmalloc(sizeof(struct BB_suid_config));
  341. sct->m_applet = applet;
  342. sct->m_mode = 0;
  343. sct->m_next = sct_head;
  344. sct_head = sct;
  345. /* Get the specified mode. */
  346. e = (char *) bb_skip_whitespace(e+1);
  347. for (i=0 ; i < 3 ; i++) {
  348. const char *q;
  349. if (!*(q = strchrnul(mode_chars + 5*i, *e++))) {
  350. parse_error("mode");
  351. }
  352. /* Adjust by -i to account for nul. */
  353. sct->m_mode |= mode_mask[(q - mode_chars) - i];
  354. }
  355. /* Now get the the user/group info. */
  356. s = (char *) bb_skip_whitespace(e);
  357. /* Note: We require whitespace between the mode and the
  358. * user/group info. */
  359. if ((s == e) || !(e = strchr(s, '.'))) {
  360. parse_error("<uid>.<gid>");
  361. }
  362. *e++ = 0;
  363. /* We can't use get_ug_id here since it would exit()
  364. * if a uid or gid was not found. Oh well... */
  365. {
  366. char *e2;
  367. sct->m_uid = strtoul(s, &e2, 10);
  368. if (*e2 || (s == e2)) {
  369. struct passwd *pwd;
  370. if (!(pwd = getpwnam(s))) {
  371. parse_error("user");
  372. }
  373. sct->m_uid = pwd->pw_uid;
  374. }
  375. sct->m_gid = strtoul(e, &e2, 10);
  376. if (*e2 || (e == e2)) {
  377. struct group *grp;
  378. if (!(grp = getgrnam(e))) {
  379. parse_error("group");
  380. }
  381. sct->m_gid = grp->gr_gid;
  382. }
  383. }
  384. }
  385. continue;
  386. }
  387. /* Unknown sections are ignored. */
  388. /* Encountering configuration lines prior to seeing a
  389. * section header is treated as an error. This is how
  390. * the old code worked, but it may not be desirable.
  391. * We may want to simply ignore such lines in case they
  392. * are used in some future version of busybox. */
  393. if (!section) {
  394. parse_error("keyword outside section");
  395. }
  396. } while (1);
  397. pe_label:
  398. fprintf(stderr, "Parse error in %s, line %d: %s\n",
  399. config_file, lc, err);
  400. fclose(f);
  401. /* Release any allocated memory before returning. */
  402. while (sct_head) {
  403. sct = sct_head->m_next;
  404. free(sct_head);
  405. sct_head = sct;
  406. }
  407. return;
  408. }
  409. #endif
  410. #endif
  411. /* END CODE */
  412. /*
  413. Local Variables:
  414. c-file-style: "linux"
  415. c-basic-offset: 4
  416. tab-width: 4
  417. End:
  418. */