id.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. extern 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. bb_opt_complementaly = "u~g:g~u";
  57. flags = bb_getopt_ulflags(argc, argv, "rnug");
  58. if ((flags & BB_GETOPT_ERROR)
  59. /* Don't allow -n -r -nr */
  60. || (flags <= 3 && flags > 0)
  61. /* Don't allow more than one username */
  62. || (argc > optind + 1))
  63. bb_show_usage();
  64. /* This values could be overwritten later */
  65. uid = geteuid();
  66. gid = getegid();
  67. if (flags & PRINT_REAL) {
  68. uid = getuid();
  69. gid = getgid();
  70. }
  71. if(argv[optind]) {
  72. p=getpwnam(argv[optind]);
  73. /* my_getpwnam is needed because it exits on failure */
  74. uid = my_getpwnam(argv[optind]);
  75. gid = p->pw_gid;
  76. /* in this case PRINT_REAL is the same */
  77. }
  78. if(flags & (JUST_GROUP | JUST_USER)) {
  79. /* JUST_GROUP and JUST_USER are mutually exclusive */
  80. if(flags & NAME_NOT_NUMBER) {
  81. /* my_getpwuid and my_getgrgid exit on failure so puts cannot segfault */
  82. puts((flags & JUST_USER) ? my_getpwuid(NULL, uid, -1 ) : my_getgrgid(NULL, gid, -1 ));
  83. } else {
  84. bb_printf("%u\n",(flags & JUST_USER) ? uid : gid);
  85. }
  86. /* exit */
  87. bb_fflush_stdout_and_exit(EXIT_SUCCESS);
  88. }
  89. /* Print full info like GNU id */
  90. /* my_getpwuid doesn't exit on failure here */
  91. status=printf_full(uid, my_getpwuid(NULL, uid, 0), 'u');
  92. putchar(' ');
  93. /* my_getgrgid doesn't exit on failure here */
  94. status|=printf_full(gid, my_getgrgid(NULL, gid, 0), 'g');
  95. #ifdef CONFIG_SELINUX
  96. if ( is_selinux_enabled() ) {
  97. security_context_t mysid;
  98. char context[80];
  99. int len = sizeof(context);
  100. getcon(&mysid);
  101. context[0] = '\0';
  102. if (mysid) {
  103. len = strlen(mysid)+1;
  104. safe_strncpy(context, mysid, len);
  105. freecon(mysid);
  106. }else{
  107. safe_strncpy(context, "unknown",8);
  108. }
  109. bb_printf(" context=%s", context);
  110. }
  111. #endif
  112. putchar('\n');
  113. bb_fflush_stdout_and_exit(status);
  114. }
  115. /* END CODE */
  116. /*
  117. Local Variables:
  118. c-file-style: "linux"
  119. c-basic-offset: 4
  120. tab-width: 4
  121. End:
  122. */