Initial commit 2.0
This commit is contained in:
parent
aa57c07eb7
commit
9fd2e3573e
8 changed files with 299 additions and 1 deletions
48
chkgrp-min.c
Normal file
48
chkgrp-min.c
Normal 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;
|
||||
}
|
||||
|
||||
Reference in a new issue