hush: remove env builtin (it is buggy). Add comments
[oweals/busybox.git] / coreutils / id.c
index 602b26ec38fdc573dbdbd47465507bcc477e7440..8a604195d12f98789a0c66a75fcbd66f48cb2ac9 100644 (file)
  *
  * Copyright (C) 2000 by Randolph Chung <tausq@debian.org>
  *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
- *
+ * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
  */
 
 /* BB_AUDIT SUSv3 _NOT_ compliant -- option -G is not currently supported. */
+/* Hacked by Tito Ragusa (C) 2004 to handle usernames of whatever length and to
+ * be more similar to GNU id.
+ * -Z option support: by Yuichi Nakamura <ynakam@hitachisoft.jp>
+ */
 
 #include "busybox.h"
 #include <stdio.h>
 #include <unistd.h>
-#include <getopt.h>
-#include <string.h>
 #include <sys/types.h>
-#ifdef CONFIG_SELINUX
-#include <proc_secure.h>
-#include <flask_util.h>
+
+#define PRINT_REAL        1
+#define NAME_NOT_NUMBER   2
+#define JUST_USER         4
+#define JUST_GROUP        8
+#if ENABLE_SELINUX
+#define JUST_CONTEXT    16
 #endif
 
-#define JUST_USER         1
-#define JUST_GROUP        2
-#define PRINT_REAL        4
-#define NAME_NOT_NUMBER   8
+static short printf_full(unsigned int id, const char *arg, const char prefix)
+{
+       const char *fmt = "%cid=%u";
+       short status = EXIT_FAILURE;
 
-extern int id_main(int argc, char **argv)
+       if (arg) {
+               fmt = "%cid=%u(%s)";
+               status = EXIT_SUCCESS;
+       }
+       printf(fmt, prefix, id, arg);
+       return status;
+}
+
+int id_main(int argc, char **argv);
+int id_main(int argc, char **argv)
 {
-       char user[9], group[9];
-       long pwnam, grnam;
-       int uid, gid;
-       int flags;
-#ifdef CONFIG_SELINUX
-       int is_flask_enabled_flag = is_flask_enabled();
+       struct passwd *p;
+       uid_t uid;
+       gid_t gid;
+       unsigned long flags;
+       short status;
+#if ENABLE_SELINUX
+       security_context_t scontext;
 #endif
+       /* Don't allow -n -r -nr -ug -rug -nug -rnug */
+       /* Don't allow more than one username */
+       opt_complementary = "?1:?:u--g:g--u:r?ug:n?ug" USE_SELINUX(":u--Z:Z--u:g--Z:Z--g");
+       flags = getopt32(argc, argv, "rnug" USE_SELINUX("Z"));
 
-       flags = bb_getopt_ulflags(argc, argv, "ugrn");
-
-       if (((flags & (JUST_USER | JUST_GROUP)) == (JUST_USER | JUST_GROUP))
-               || (argc > optind + 1)
-       ) {
-               bb_show_usage();
+       /* This values could be overwritten later */
+       uid = geteuid();
+       gid = getegid();
+       if (flags & PRINT_REAL) {
+               uid = getuid();
+               gid = getgid();
        }
 
-       if (argv[optind] == NULL) {
-               if (flags & PRINT_REAL) {
-                       uid = getuid();
-                       gid = getgid();
-               } else {
-                       uid = geteuid();
-                       gid = getegid();
-               }
-               my_getpwuid(user, uid);
-       } else {
-               safe_strncpy(user, argv[optind], sizeof(user));
-           gid = my_getpwnamegid(user);
+       if (argv[optind]) {
+               p = getpwnam(argv[optind]);
+               /* xuname2uid is needed because it exits on failure */
+               uid = xuname2uid(argv[optind]);
+               gid = p->pw_gid;
+               /* in this case PRINT_REAL is the same */
        }
-       my_getgrgid(group, gid);
-
-       pwnam=my_getpwnam(user);
-       grnam=my_getgrnam(group);
 
-       if (flags & (JUST_GROUP | JUST_USER)) {
-               char *s = group;
-               if (flags & JUST_USER) {
-                       s = user;
-                       grnam = pwnam;
-               }
+       if (flags & (JUST_GROUP | JUST_USER USE_SELINUX(| JUST_CONTEXT))) {
+               /* JUST_GROUP and JUST_USER are mutually exclusive */
                if (flags & NAME_NOT_NUMBER) {
-                       puts(s);
+                       /* bb_getpwuid and bb_getgrgid exit on failure so puts cannot segfault */
+                       puts((flags & JUST_USER) ? bb_getpwuid(NULL, uid, -1 ) : bb_getgrgid(NULL, gid, -1 ));
                } else {
-                       printf("%ld\n", grnam);
+                       if (flags & JUST_USER) {
+                               printf("%u\n", uid);
+                       }
+                       if (flags & JUST_GROUP) {
+                               printf("%u\n", gid);
+                       }
                }
-       } else {
-#ifdef CONFIG_SELINUX
-               printf("uid=%ld(%s) gid=%ld(%s)", pwnam, user, grnam, group);
-               if(is_flask_enabled_flag)
-               {
-                       security_id_t mysid = getsecsid();
-                       char context[80];
-                       int len = sizeof(context);
-                       context[0] = '\0';
-                       if(security_sid_to_context(mysid, context, &len))
-                               strcpy(context, "unknown");
-                       printf(" context=%s\n", context);
+
+#if ENABLE_SELINUX
+               if (flags & JUST_CONTEXT) {
+                       selinux_or_die();
+                       if (argc - optind == 1) {
+                               bb_error_msg_and_die("user name can't be passed with -Z");
+                       }
+
+                       if (getcon(&scontext)) {
+                               bb_error_msg_and_die("can't get process context");
+                       }
+                       printf("%s\n", scontext);
                }
-               else
-                       printf("\n");
-#else
-               printf("uid=%ld(%s) gid=%ld(%s)\n", pwnam, user, grnam, group);
 #endif
+               /* exit */
+               fflush_stdout_and_exit(EXIT_SUCCESS);
+       }
+
+       /* Print full info like GNU id */
+       /* bb_getpwuid doesn't exit on failure here */
+       status = printf_full(uid, bb_getpwuid(NULL, uid, 0), 'u');
+       putchar(' ');
+       /* bb_getgrgid doesn't exit on failure here */
+       status |= printf_full(gid, bb_getgrgid(NULL, gid, 0), 'g');
+
+#if ENABLE_SELINUX
+       if (is_selinux_enabled()) {
+               security_context_t mysid;
+               const char *context;
 
+               context = "unknown";
+               getcon(&mysid);
+               if (mysid) {
+                       context = alloca(strlen(mysid) + 1);
+                       strcpy((char*)context, mysid);
+                       freecon(mysid);
+               }
+               printf(" context=%s", context);
        }
+#endif
 
-       bb_fflush_stdout_and_exit(0);
+       putchar('\n');
+       fflush_stdout_and_exit(status);
 }