coreutils

General Software Utilities
git clone http://git.omkov.net/coreutils
Log | Tree | Refs | README | LICENCE | Download

AuthorJamozed <[email protected]>
Date2020-06-27 02:00:03
Commit8b4f9998e7324543aa53e38b36050d3afee83680
Parent998882e59a3dbe5c7c8512ffc95c194f860c9a9b

id: Add POSIX id

Diffstat

M CMakeLists.txt | 1 +
M README.md | 3 +++
A man/id.1 | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++++
A src/id.c | 193 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

4 files changed, 250 insertions, 0 deletions

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 6845312..e835d70 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -19,6 +19,7 @@ ADD_EXECUTABLE(echo     ${CMAKE_SOURCE_DIR}/src/echo.c)
 ADD_EXECUTABLE(env      ${CMAKE_SOURCE_DIR}/src/env.c)
 ADD_EXECUTABLE(false    ${CMAKE_SOURCE_DIR}/src/false.c)
 ADD_EXECUTABLE(head     ${CMAKE_SOURCE_DIR}/src/head.c)
+ADD_EXECUTABLE(id       ${CMAKE_SOURCE_DIR}/src/id.c)
 ADD_EXECUTABLE(link     ${CMAKE_SOURCE_DIR}/src/link.c)
 ADD_EXECUTABLE(logname  ${CMAKE_SOURCE_DIR}/src/logname.c)
 ADD_EXECUTABLE(mkdir    ${CMAKE_SOURCE_DIR}/src/mkdir.c)
diff --git a/README.md b/README.md
index 0976edf..72351b2 100644
--- a/README.md
+++ b/README.md
@@ -18,6 +18,7 @@ UNIX-like systems.
 | env              | Execute with an altered enviroment       | POSIX    |
 | false            | Return false value                       | POSIX    |
 | head             | Output the first part of files           | POSIX    |
+| id\*             | Return user identity                     | POSIX    |
 | link             | Create a link to a file                  | POSIX    |
 | logname          | Return the user's login name             | POSIX    |
 | mkdir            | Make directories                         | POSIX    |
@@ -35,6 +36,8 @@ UNIX-like systems.
 | unlink           | Remove a file using the unlink function  | POSIX    |
 | yes              | Output a string repeatedly               |          |
 
+Utilities marked with a '\*' may be incomplete or non-complaint.
+
 ## Build Instructions
 
 OMKOV coreutils uses CMake to build.
diff --git a/man/id.1 b/man/id.1
new file mode 100644
index 0000000..0e25e1f
--- /dev/null
+++ b/man/id.1
@@ -0,0 +1,53 @@
+.TH ID 1 2020-06-27 "OMKOV coreutils" "General Commands Manual"
+.SH NAME
+id \(em return user identity
+.SH SYNOPSYS
+\fBid\fR [-G|-g|-u] [-nr] [\fIuser\fR...]
+.SH DESCRIPTION
+Print user and group information for the specified \fIuser\fR, or the current
+user if no \fIuser\fR is specified.
+.SH OPTIONS
+The following options are supported:
+.TP
+.B -G
+Output all group IDs.
+.TP
+.B -g
+Output only the effective group ID.
+.TP
+.B -n
+Output the names of groups and users instead of their IDs.
+.TP
+.B -r
+Output the real ID instead of the effective ID.
+.TP
+.B -u
+Output only the effective user ID.
+.TP
+.B --help
+Display help information.
+.TP
+.B --version
+Display version information.
+.SH OPERANDS
+The following operand is supported:
+.TP
+.I user
+A username to show ID information for.
+.SH EXIT STATUS
+The following exit values will be returned:
+.TP
+\ 0
+Successful completion.
+.TP
+>0
+An error occurred.
+.SH STANDARDS
+The \fIid\fR utility is compliant with the IEEE Std 1003.2-1992 ("POSIX.2")
+specification.
+.SH COPYRIGHT
+.nf
+Copyright (C) 2020, Jakob Wakeling
+All rights reserved.
+OMKOV Permissive Licence (https://www.omkov.net/OLPE)
+.fi
diff --git a/src/id.c b/src/id.c
new file mode 100644
index 0000000..c246c38
--- /dev/null
+++ b/src/id.c
@@ -0,0 +1,193 @@
+// id.c, version 0.1.0
+// OMKOV coreutils implementation of POSIX id
+// Copyright (C) 2020, Jakob Wakeling
+// All rights reserved.
+
+/*
+OMKOV Permissive Licence, version 1.0
+
+Copyright (C) 2020, Jakob Wakeling
+All rights reserved.
+
+Permission is hereby granted, free of charge, to any person obtaining a copy of
+this software and associated documentation files (the "Software"), to deal with
+the Software without restriction, including without limitation the rights to
+use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
+the Software, and to permit persons to whom the Software is furnished to do so,
+subject to the following conditions:
+
+* Redistributions of source code must retain the above copyright notice, this
+  list of conditions and the following disclaimers.
+* Redistributions in binary form must reproduce the above copyright notice, this
+  list of conditions and the following disclaimers in the documentation and/or
+  other materials provided with the distribution.
+* Neither the names of the copyright holders, nor the names of its contributors
+  may be used to endorse or promote products derived from this Software without
+  specific prior written permission.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
+FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE COPYRIGHT
+HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS WITH THE SOFTWARE.
+*/
+
+/*
+	TODO Fix memory leak when listing supplementary groups (may be unfixable)
+*/
+
+#include "error.h"
+#include "optget.h"
+
+#include <grp.h>
+#include <pwd.h>
+#include <sys/types.h>
+#include <unistd.h>
+
+#include <stdbool.h>
+#include <stdlib.h>
+
+typedef struct passwd pwd_t;
+typedef struct group  grp_t;
+
+static char mode;
+static bool nflag;
+static bool rflag;
+
+static inline void id(uid_t uid, uid_t euid, gid_t gid, gid_t egid);
+static inline void groups(const char *user, gid_t gid, gid_t egid);
+
+static void help(void);
+static void version(void);
+
+int main(int argc, char *argv[]) {
+	lop_t lops[] = {
+		{ "help",    ARG_NUL, 256 },
+		{ "version", ARG_NUL, 257 },
+		{ NULL, 0, 0 }
+	};
+	
+	opt_t opt = OPTGET_INIT; opt.str = "Ggnru"; opt.lops = lops; int o;
+	while ((o = optget(&opt, argv, 1)) != -1) switch (o) {
+	case 'G': case 'g': case 'u': {
+		if (mode) {
+			error(1, "%s: options 'G', 'g', and 'u' are contradictory");
+		} else { mode = (char)o; } break;
+	}
+	case 'n': { nflag = true; break; }
+	case 'r': { rflag = true; break; }
+	case 256: { help(); return 0; }
+	case 257: { version(); return 0; }
+	default: { return 1; }
+	}
+	
+	if (!mode && (nflag || rflag)) {
+		error(1, "%s: options 'n' and 'r' require 'G', 'g', or 'u'", argv[0]);
+	}
+	
+	uid_t uid, euid; gid_t gid, egid;
+	
+	if (opt.ind == argc) {
+		uid = getuid(); euid = geteuid();
+		gid = getgid(); egid = getegid();
+		id (uid, euid, gid, egid);
+	}
+	else for (char **p = &argv[opt.ind]; *p; ++p) {
+		pwd_t *pwd;
+		
+		if (!(pwd = getpwnam(*p))) {
+			error(1, "%s: %s: invalid user", argv[0], *p);
+		}
+		
+		uid = euid = pwd->pw_uid;
+		gid = egid = pwd->pw_gid;
+		id (uid, euid, gid, egid);
+	}
+	
+	return 0;
+}
+
+static inline void id(uid_t uid, uid_t euid, gid_t gid, gid_t egid) {
+	pwd_t *pwd; grp_t *grp;
+	
+	if (mode == 'G') {
+		pwd = getpwuid(uid);
+		groups(pwd->pw_name, gid, egid);
+	}
+	else if (mode == 'g') {
+		grp = getgrgid(rflag ? gid : egid);
+		nflag ? fputs(grp->gr_name, stdout) : printf("%u", grp->gr_gid);
+	}
+	else if (mode == 'u') {
+		pwd = getpwuid(rflag ? uid : euid);
+		nflag ? fputs(pwd->pw_name, stdout) : printf("%u", pwd->pw_uid);
+	}
+	else {
+		pwd = getpwuid(uid); grp = getgrgid(gid);
+		printf("uid=%u(%s) gid=%u(%s) ", uid, pwd->pw_name, gid, grp->gr_name);
+		if (uid != euid) {
+			pwd = getpwuid(euid);
+			printf("euid=%u(%s) ", euid, pwd->pw_name);
+			pwd = getpwuid(uid);
+		}
+		if (gid != egid) {
+			grp = getgrgid(egid);
+			printf("egid=%u(%s) ", egid, grp->gr_name);
+		}
+		fputs("groups=", stdout);
+		groups(pwd->pw_name, gid, egid);
+	}
+	fputc('\n', stdout);
+	
+	return;
+}
+
+static inline void groups(const char *user, gid_t gid, gid_t egid) {
+	grp_t *grp = getgrgid(gid);
+	if (mode) { nflag ? fputs(grp->gr_name, stdout) : printf("%u", gid); }
+	else { printf("%u(%s)", gid, grp->gr_name); }
+	
+	if (gid != egid) {
+		grp = getgrgid(egid);
+		if (mode) { nflag ? printf(" %s", grp->gr_name) : printf(" %u", egid); }
+		else { printf(",%u(%s)", egid, grp->gr_name); }
+	}
+	
+	setgrent();
+	while ((grp = getgrent()) != NULL) {
+		if (grp->gr_gid == gid || grp->gr_gid == egid) { continue; }
+		for (int i = 0; grp->gr_mem[i]; ++i) {
+			if (strcmp(grp->gr_mem[i], user) == 0) {
+				if (mode) { nflag ? printf(" %s", grp->gr_name) :
+					printf(" %u", grp->gr_gid); }
+				else { printf(",%u(%s)", grp->gr_gid, grp->gr_name); }
+			}
+		}
+	}
+	endgrent();
+	
+	return;
+}
+
+static void help(void) {
+	puts("id - return user identity\n");
+	puts("usage: id [-G|-g|-u] [-nr] [user...]\n");
+	puts("options:");
+	puts("  -G         Output all group IDs");
+	puts("  -g         Output only the effective group ID");
+	puts("  -n         Output names of groups and users instead of their IDs");
+	puts("  -r         Output the real ID instead of the effective ID");
+	puts("  -u         Output only the effective user ID");
+	puts("  --help     Display help information");
+	puts("  --version  Display version information");
+	return;
+}
+
+static void version(void) {
+	puts("OMKOV coreutils id, version 0.1.0");
+	puts("Copyright (C) 2020, Jakob Wakeling");
+	puts("All rights reserved.");
+	puts("OMKOV Permissive Licence (https://www.omkov.net/OLPE)");
+	return;
+}