id.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * Mini id implementation for busybox
  4. *
  5. * Copyright (C) 2000 by Randolph Chung <tausq@debian.org>
  6. *
  7. * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
  8. */
  9. /* BB_AUDIT SUSv3 _NOT_ compliant -- option -G is not currently supported. */
  10. /* Hacked by Tito Ragusa (C) 2004 to handle usernames of whatever length and to
  11. * be more similar to GNU id.
  12. */
  13. #include "busybox.h"
  14. #include <stdio.h>
  15. #include <unistd.h>
  16. #include <sys/types.h>
  17. #ifdef CONFIG_SELINUX
  18. #include <selinux/selinux.h> /* for is_selinux_enabled() */
  19. #endif
  20. #define PRINT_REAL 1
  21. #define NAME_NOT_NUMBER 2
  22. #define JUST_USER 4
  23. #define JUST_GROUP 8
  24. static short printf_full(unsigned int id, const char *arg, const char prefix)
  25. {
  26. const char *fmt = "%cid=%u";
  27. short status = EXIT_FAILURE;
  28. if (arg) {
  29. fmt = "%cid=%u(%s)";
  30. status = EXIT_SUCCESS;
  31. }
  32. printf(fmt, prefix, id, arg);
  33. return status;
  34. }
  35. int id_main(int argc, char **argv)
  36. {
  37. struct passwd *p;
  38. uid_t uid;
  39. gid_t gid;
  40. unsigned long flags;
  41. short status;
  42. /* Don't allow -n -r -nr -ug -rug -nug -rnug */
  43. /* Don't allow more than one username */
  44. opt_complementary = "?1:?:u--g:g--u:r?ug:n?ug";
  45. flags = getopt32(argc, argv, "rnug");
  46. /* This values could be overwritten later */
  47. uid = geteuid();
  48. gid = getegid();
  49. if (flags & PRINT_REAL) {
  50. uid = getuid();
  51. gid = getgid();
  52. }
  53. if (argv[optind]) {
  54. p = getpwnam(argv[optind]);
  55. /* bb_xgetpwnam is needed because it exits on failure */
  56. uid = bb_xgetpwnam(argv[optind]);
  57. gid = p->pw_gid;
  58. /* in this case PRINT_REAL is the same */
  59. }
  60. if (flags & (JUST_GROUP | JUST_USER)) {
  61. /* JUST_GROUP and JUST_USER are mutually exclusive */
  62. if (flags & NAME_NOT_NUMBER) {
  63. /* bb_getpwuid and bb_getgrgid exit on failure so puts cannot segfault */
  64. puts((flags & JUST_USER) ? bb_getpwuid(NULL, uid, -1 ) : bb_getgrgid(NULL, gid, -1 ));
  65. } else {
  66. printf("%u\n", (flags & JUST_USER) ? uid : gid);
  67. }
  68. /* exit */
  69. fflush_stdout_and_exit(EXIT_SUCCESS);
  70. }
  71. /* Print full info like GNU id */
  72. /* bb_getpwuid doesn't exit on failure here */
  73. status = printf_full(uid, bb_getpwuid(NULL, uid, 0), 'u');
  74. putchar(' ');
  75. /* bb_getgrgid doesn't exit on failure here */
  76. status |= printf_full(gid, bb_getgrgid(NULL, gid, 0), 'g');
  77. #ifdef CONFIG_SELINUX
  78. if (is_selinux_enabled()) {
  79. security_context_t mysid;
  80. char context[80];
  81. int len = sizeof(context);
  82. getcon(&mysid);
  83. context[0] = '\0';
  84. if (mysid) {
  85. len = strlen(mysid)+1;
  86. safe_strncpy(context, mysid, len);
  87. freecon(mysid);
  88. } else {
  89. safe_strncpy(context, "unknown", 8);
  90. }
  91. printf(" context=%s", context);
  92. }
  93. #endif
  94. putchar('\n');
  95. fflush_stdout_and_exit(status);
  96. }