id.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. * This program 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 program 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 GNU
  15. * 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, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. *
  21. */
  22. /* BB_AUDIT SUSv3 _NOT_ compliant -- option -G is not currently supported. */
  23. /* Hacked by Tito Ragusa (C) 2004 to handle usernames of whatever length and to
  24. * be more similar to GNU id.
  25. */
  26. #include "busybox.h"
  27. #include "pwd_.h"
  28. #include <stdio.h>
  29. #include <unistd.h>
  30. #include <sys/types.h>
  31. #ifdef CONFIG_SELINUX
  32. #include <selinux/selinux.h> /* for is_selinux_enabled() */
  33. #endif
  34. #define PRINT_REAL 1
  35. #define NAME_NOT_NUMBER 2
  36. #define JUST_USER 4
  37. #define JUST_GROUP 8
  38. static short printf_full(unsigned int id, const char *arg, const char prefix)
  39. {
  40. const char *fmt = "%cid=%u";
  41. short status=EXIT_FAILURE;
  42. if(arg) {
  43. fmt = "%cid=%u(%s)";
  44. status=EXIT_SUCCESS;
  45. }
  46. bb_printf(fmt, prefix, id, arg);
  47. return status;
  48. }
  49. int id_main(int argc, char **argv)
  50. {
  51. struct passwd *p;
  52. uid_t uid;
  53. gid_t gid;
  54. unsigned long flags;
  55. short status;
  56. /* Don't allow -n -r -nr -ug -rug -nug -rnug */
  57. /* Don't allow more than one username */
  58. bb_opt_complementally = "?1:?:u--g:g--u:r?ug:n?ug";
  59. flags = bb_getopt_ulflags(argc, argv, "rnug");
  60. /* This values could be overwritten later */
  61. uid = geteuid();
  62. gid = getegid();
  63. if (flags & PRINT_REAL) {
  64. uid = getuid();
  65. gid = getgid();
  66. }
  67. if(argv[optind]) {
  68. p=getpwnam(argv[optind]);
  69. /* bb_xgetpwnam is needed because it exits on failure */
  70. uid = bb_xgetpwnam(argv[optind]);
  71. gid = p->pw_gid;
  72. /* in this case PRINT_REAL is the same */
  73. }
  74. if(flags & (JUST_GROUP | JUST_USER)) {
  75. /* JUST_GROUP and JUST_USER are mutually exclusive */
  76. if(flags & NAME_NOT_NUMBER) {
  77. /* bb_getpwuid and bb_getgrgid exit on failure so puts cannot segfault */
  78. puts((flags & JUST_USER) ? bb_getpwuid(NULL, uid, -1 ) : bb_getgrgid(NULL, gid, -1 ));
  79. } else {
  80. bb_printf("%u\n",(flags & JUST_USER) ? uid : gid);
  81. }
  82. /* exit */
  83. bb_fflush_stdout_and_exit(EXIT_SUCCESS);
  84. }
  85. /* Print full info like GNU id */
  86. /* bb_getpwuid doesn't exit on failure here */
  87. status=printf_full(uid, bb_getpwuid(NULL, uid, 0), 'u');
  88. putchar(' ');
  89. /* bb_getgrgid doesn't exit on failure here */
  90. status|=printf_full(gid, bb_getgrgid(NULL, gid, 0), 'g');
  91. #ifdef CONFIG_SELINUX
  92. if ( is_selinux_enabled() ) {
  93. security_context_t mysid;
  94. char context[80];
  95. int len = sizeof(context);
  96. getcon(&mysid);
  97. context[0] = '\0';
  98. if (mysid) {
  99. len = strlen(mysid)+1;
  100. safe_strncpy(context, mysid, len);
  101. freecon(mysid);
  102. }else{
  103. safe_strncpy(context, "unknown",8);
  104. }
  105. bb_printf(" context=%s", context);
  106. }
  107. #endif
  108. putchar('\n');
  109. bb_fflush_stdout_and_exit(status);
  110. }
  111. /* END CODE */
  112. /*
  113. Local Variables:
  114. c-file-style: "linux"
  115. c-basic-offset: 4
  116. tab-width: 4
  117. End:
  118. */