Initial commit 2.0

This commit is contained in:
Johannes Findeisen 2025-06-18 21:31:20 +02:00
commit 9fd2e3573e
8 changed files with 299 additions and 1 deletions

5
.gitignore vendored Normal file
View file

@ -0,0 +1,5 @@
chkgrp
chkgrp-min
chkgrp-native
chkgrp-native-min

View file

@ -1,4 +1,4 @@
Copyright 2024 Johannes Findeisen Copyright 2025 Johannes Findeisen
Licensed under the Apache License, Version 2.0 (the "License"); Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License. you may not use this file except in compliance with the License.

24
Makefile Normal file
View file

@ -0,0 +1,24 @@
all:
$(CC) -Wall -o ./chkgrp ./chkgrp.c
$(CC) -Wall -o ./chkgrp-min ./chkgrp-min.c
$(CC) -Wall -o ./chkgrp-native ./chkgrp-native.c
$(CC) -Wall -o ./chkgrp-native-min ./chkgrp-native-min.c
clean:
rm -f ./chkgrp
rm -f ./chkgrp-min
rm -f ./chkgrp-native
rm -f ./chkgrp-native-min
install:
install -g 0 -o 0 -m 0655 ./chkgrp /usr/bin/
install -g 0 -o 0 -m 0655 ./chkgrp-min /usr/bin/
install -g 0 -o 0 -m 0655 ./chkgrp-native /usr/bin/
install -g 0 -o 0 -m 0655 ./chkgrp-native-min /usr/bin/
uninstall:
rm -f /usr/bin/chkgrp
rm -f /usr/bin/chkgrp-min
rm -f /usr/bin/chkgrp-native
rm -f /usr/bin/chkgrp-native-min

View file

@ -0,0 +1,6 @@
# chkgrp
chkgrp is a set of programs to verify if a user is a member of a group on a UNIX based system. This code is so simple that it describes itself very good.
Mor information will follow...

48
chkgrp-min.c Normal file
View file

@ -0,0 +1,48 @@
/**
* chkgrp-min is a program that checks if a user is a member of a group.
*
* Author: Johannes Findeisen <you@hanez.org> - 2025
* Homepage: https://git.xw3.org/hanez/chkgrp
* License: Apache-2.0 (see LICENSE)
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <pwd.h>
#include <grp.h>
#define MAX_NAME 256
int main(int argc, char *argv[])
{
if (argc != 3)
return 2;
if (strlen(argv[1]) > MAX_NAME || strlen(argv[2]) > MAX_NAME)
return 2;
struct passwd *pw = getpwnam(argv[1]);
if (!pw)
return 2;
struct group *gr = getgrnam(argv[2]);
if (!gr)
return 2;
if (pw->pw_gid == gr->gr_gid) {
puts("Yes");
return 0;
}
for (char **m = gr->gr_mem; *m; ++m) {
if (!strcmp(*m, argv[1])) {
puts("Yes");
return 0;
}
}
puts("No");
return 1;
}

60
chkgrp-native-min.c Normal file
View file

@ -0,0 +1,60 @@
/**
* chkgrp-min-native is a program that checks if a user is a member of a group.
*
* Author: Johannes Findeisen <you@hanez.org> - 2025
* Homepage: https://git.xw3.org/hanez/chkgrp
* License: Apache-2.0 (see LICENSE)
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_LINE 1024
#define MAX_NAME 256
int main(int argc, char *argv[])
{
if (argc != 3)
return 2;
const char *username = argv[1];
const char *groupname = argv[2];
if (strlen(username) > MAX_NAME || strlen(groupname) > MAX_NAME)
return 2;
FILE *fp = fopen("/etc/group", "r");
if (!fp)
return 2;
char line[MAX_LINE];
while (fgets(line, sizeof(line), fp)) {
char *grp = strtok(line, ":");
if (!grp || strcmp(grp, groupname) != 0)
continue;
strtok(NULL, ":"); // skip password
strtok(NULL, ":"); // skip GID
char *members = strtok(NULL, ":\n");
if (!members) break;
char *m = strtok(members, ",");
while (m) {
if (!strcmp(m, username)) {
puts("Yes");
fclose(fp);
return 0;
}
m = strtok(NULL, ",");
}
break; // group matched, no user found
}
fclose(fp);
puts("No");
return 1;
}

78
chkgrp-native.c Normal file
View file

@ -0,0 +1,78 @@
/**
* chkgrp-native is a program that checks if a user is a member of a group.
*
* Author: Johannes Findeisen <you@hanez.org> - 2025
* Homepage: https://git.xw3.org/hanez/chkgrp
* License: Apache-2.0 (see LICENSE)
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_LINE 1024
#define MAX_NAME 256
int main(int argc, char *argv[])
{
if (argc != 3) {
fprintf(stderr, "Usage: %s <username> <groupname>\n", argv[0]);
return 2;
}
const char *username = argv[1];
const char *groupname = argv[2];
if (strlen(username) > MAX_NAME || strlen(groupname) > MAX_NAME) {
fprintf(stderr, "Error: Name too long (max %d characters).\n", MAX_NAME);
return 2;
}
FILE *fp = fopen("/etc/group", "r");
if (!fp) {
perror("Error opening /etc/group");
return 2;
}
char line[MAX_LINE];
int found = 0;
while (fgets(line, sizeof(line), fp)) {
char *grp_name = strtok(line, ":");
if (!grp_name) continue;
strtok(NULL, ":"); // skip password
strtok(NULL, ":"); // skip GID
char *members = strtok(NULL, ":\n");
if (!members) continue;
if (strcmp(grp_name, groupname) != 0)
continue;
// Group found, now check for user in member list
found = 1;
char *member = strtok(members, ",");
while (member) {
if (strcmp(member, username) == 0) {
fclose(fp);
puts("Yes");
return 0;
}
member = strtok(NULL, ",");
}
break; // no need to continue scanning
}
fclose(fp);
if (!found) {
fprintf(stderr, "Group '%s' not found.\n", groupname);
return 2;
}
puts("No");
return 1;
}

77
chkgrp.c Normal file
View file

@ -0,0 +1,77 @@
/**
* chkgrp is a program that checks if a user is a member of a group.
*
* Author: Johannes Findeisen <you@hanez.org> - 2025
* Homepage: https://git.xw3.org/hanez/chkgrp
* License: Apache-2.0 (see LICENSE)
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <pwd.h>
#include <grp.h>
#include <unistd.h>
#include <errno.h>
#include <limits.h>
#ifndef LOGIN_NAME_MAX
#define LOGIN_NAME_MAX 256
#endif
int main(int argc, char *argv[])
{
if (argc != 3) {
fprintf(stderr, "Usage: %s <username> <groupname>\n", argv[0]);
return 2;
}
const char *username = argv[1];
const char *groupname = argv[2];
// Runtime check for max name length
long max_name_len = sysconf(_SC_LOGIN_NAME_MAX);
if (max_name_len <= 0 || max_name_len > 1024) {
max_name_len = LOGIN_NAME_MAX;
}
if (strlen(username) > (size_t)max_name_len) {
fprintf(stderr, "Error: Username too long (max %ld characters).\n",
max_name_len);
return 2;
}
if (strlen(groupname) > (size_t)max_name_len) {
fprintf(stderr, "Error: Group name too long (max %ld characters).\n",
max_name_len);
return 2;
}
struct passwd *pw = getpwnam(username);
if (!pw) {
fprintf(stderr, "Error: User '%s' not found.\n", username);
return 2;
}
struct group *gr = getgrnam(groupname);
if (!gr) {
fprintf(stderr, "Error: Group '%s' not found.\n", groupname);
return 2;
}
if (pw->pw_gid == gr->gr_gid) {
puts("Yes");
return 0;
}
for (char **members = gr->gr_mem; *members != NULL; members++) {
if (strcmp(*members, username) == 0) {
puts("Yes");
return 0;
}
}
puts("No");
return 1;
}