From 71555fc27548af1cddffec75bb24bb9188b5b89f Mon Sep 17 00:00:00 2001 From: hanez Date: Mon, 8 Sep 2025 02:34:46 +0200 Subject: [PATCH] Merged chkpwr to chkusr and moved defines to config.h. --- .gitignore | 1 + Makefile | 4 ++ chkgrp-min.c | 4 +- chkgrp-native-min-extended.c | 5 +- chkgrp-native-min.c | 5 +- chkgrp-native.c | 5 +- chkgrp-ng.c | 5 +- chkgrp.c | 8 +-- chkpwd.c | 34 ++++-------- chkpwr.c | 100 +++++++++++++++++++++++++++++++++++ config.h | 50 ++++++++++++++++++ version.h | 1 - 12 files changed, 171 insertions(+), 51 deletions(-) create mode 100644 chkpwr.c create mode 100644 config.h delete mode 100644 version.h diff --git a/.gitignore b/.gitignore index bd76ebb..d980a9c 100644 --- a/.gitignore +++ b/.gitignore @@ -8,5 +8,6 @@ chkgrp-native-min-extended chkgrp-ng chkpwd chkpwd.1.gz +chkpwr test test.c diff --git a/Makefile b/Makefile index f4058d2..1628df2 100644 --- a/Makefile +++ b/Makefile @@ -7,6 +7,7 @@ all: $(CC) -Wall -o ./chkgrp-ng ./chkgrp-ng.c $(CC) -Wall -o ./chkpwd ./chkpwd.c -lpam gzip -fk ./chkpwd.1 + $(CC) -Wall -o ./chkpwr ./chkpwr.c clean: rm -f ./chkgrp @@ -17,6 +18,7 @@ clean: rm -f ./chkgrp-ng rm -f ./chkpwd rm -f ./chkpwd.1.gz + rm -f ./chkpwr rm -f ./test install: @@ -29,6 +31,7 @@ install: install -g 0 -o 0 -m 0655 ./chkpwd /usr/bin/ install -g 0 -o 0 -m 0644 ./chkpwd.h /usr/include/ install -g 0 -o 0 -m 0644 ./chkpwd.1.gz /usr/share/man/man1/ + install -g 0 -o 0 -m 0655 ./chkpwr /usr/bin/ install -g 0 -o 0 -m 0655 ./lua/chkgrp-native-min-extended.lua /usr/bin/ uninstall: @@ -40,6 +43,7 @@ uninstall: rm -f /usr/bin/chkgrp-native-min-extended.lua rm -f /usr/bin/chkgrp-ng rm -f /usr/bin/chkpwd + rm -f /usr/bin/chkpwr rm -f /usr/include/chkpwd.h rm -f /usr/share/man/man1/chkpwd.1.gz diff --git a/chkgrp-min.c b/chkgrp-min.c index 728d0d6..4bc4dcb 100644 --- a/chkgrp-min.c +++ b/chkgrp-min.c @@ -6,7 +6,7 @@ * License: Apache-2.0 (see LICENSE) */ - #include "version.h" + #include "config.h" #include #include @@ -14,8 +14,6 @@ #include #include -#define MAX_NAME 256 - int main(int argc, char *argv[]) { if (argc != 3) diff --git a/chkgrp-native-min-extended.c b/chkgrp-native-min-extended.c index a1c3a38..320c57f 100644 --- a/chkgrp-native-min-extended.c +++ b/chkgrp-native-min-extended.c @@ -7,16 +7,13 @@ * License: Apache-2.0 (see LICENSE) */ -#include "version.h" +#include "config.h" #include #include #include #include -#define MAX_LINE 1024 -#define MAX_NAME 256 - int main(int argc, char *argv[]) { if (argc != 3) diff --git a/chkgrp-native-min.c b/chkgrp-native-min.c index 1e810ed..8786a20 100644 --- a/chkgrp-native-min.c +++ b/chkgrp-native-min.c @@ -6,15 +6,12 @@ * License: Apache-2.0 (see LICENSE) */ -#include "version.h" +#include "config.h" #include #include #include -#define MAX_LINE 1024 -#define MAX_NAME 256 - int main(int argc, char *argv[]) { if (argc != 3) diff --git a/chkgrp-native.c b/chkgrp-native.c index 695fd6d..d19de61 100644 --- a/chkgrp-native.c +++ b/chkgrp-native.c @@ -6,15 +6,12 @@ * License: Apache-2.0 (see LICENSE) */ -#include "version.h" +#include "config.h" #include #include #include -#define MAX_LINE 1024 -#define MAX_NAME 256 - int main(int argc, char *argv[]) { if (argc != 3) { diff --git a/chkgrp-ng.c b/chkgrp-ng.c index 3387a70..e22c053 100644 --- a/chkgrp-ng.c +++ b/chkgrp-ng.c @@ -6,15 +6,12 @@ * License: Apache-2.0 (see LICENSE) */ -#include "version.h" +#include "config.h" #include #include #include -#define MAX_LINE 1024 -#define MAX_NAME 256 - void print_help(const char *prog) { printf("Usage: %s [OPTIONS] \n", prog); diff --git a/chkgrp.c b/chkgrp.c index b1bca2c..35fcd75 100644 --- a/chkgrp.c +++ b/chkgrp.c @@ -6,7 +6,7 @@ * License: Apache-2.0 (see LICENSE) */ -#include "version.h" +#include "config.h" #include #include @@ -17,10 +17,6 @@ #include #include -#ifndef LOGIN_NAME_MAX -#define LOGIN_NAME_MAX 256 -#endif - int main(int argc, char *argv[]) { if (argc != 3) { @@ -34,7 +30,7 @@ int main(int argc, char *argv[]) // 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; + max_name_len = MAX_NAME; } if (strlen(username) > (size_t)max_name_len) { diff --git a/chkpwd.c b/chkpwd.c index 036368d..4273372 100644 --- a/chkpwd.c +++ b/chkpwd.c @@ -8,7 +8,7 @@ */ #include "chkpwd.h" -#include "version.h" +#include "config.h" #include #include @@ -17,22 +17,6 @@ #include #include -#ifndef MAX_PASSWORD_LEN -#define MAX_PASSWORD_LEN 256 -#endif - -#ifndef MAX_USERNAME_LEN -#define MAX_USERNAME_LEN 32 -#endif - -#ifndef MAX_UID -#define MAX_UID 1000 -#endif - -#ifndef MIN_UID -#define MIN_UID 1000 -#endif - // Function to prompt user for input, optionally hiding input void prompt_for_input(char *buffer, size_t size, const char *prompt, bool hide_input) @@ -98,28 +82,28 @@ int main(int argc, char *argv[]) { bool verbose = false; bool version = false; - char password[MAX_PASSWORD_LEN] = {0}; - char username[MAX_USERNAME_LEN] = {0}; + char password[MAX_PASSWORD] = {0}; + char username[MAX_NAME] = {0}; int opt; // Parse command-line arguments while ((opt = getopt(argc, argv, "u:p:hvV")) != -1) { switch (opt) { case 'u': - if (strlen(optarg) >= MAX_USERNAME_LEN) { + if (strlen(optarg) >= MAX_NAME) { fprintf(stderr, "Error: Username is too long (maximum %d characters).\n", - MAX_USERNAME_LEN); + MAX_NAME); exit(1); } - strncpy(username, optarg, MAX_USERNAME_LEN - 1); + strncpy(username, optarg, MAX_NAME - 1); break; case 'p': - if (strlen(optarg) >= MAX_PASSWORD_LEN) { + if (strlen(optarg) >= MAX_PASSWORD) { fprintf(stderr, "Error: Password is too long (maximum %d characters).\n", - MAX_PASSWORD_LEN); + MAX_PASSWORD); exit(1); } - strncpy(password, optarg, MAX_PASSWORD_LEN - 1); + strncpy(password, optarg, MAX_PASSWORD - 1); break; case 'h': print_usage(argv[0]); diff --git a/chkpwr.c b/chkpwr.c new file mode 100644 index 0000000..e6be5eb --- /dev/null +++ b/chkpwr.c @@ -0,0 +1,100 @@ +/** + * checkpwrule is a program that checks if an input string validates a + * password rule. + * + * Author: Johannes Findeisen - 2024 + * License: MIT (see LICENSE) + */ + +#include "config.h" + +#include +#include +#include + +// Return 1 if non-ASCII characters are found, 0 if all are ASCII +int is_valid_ascii(char *str) { + for (int i = 0; i < strlen(str); i++) { + if ((unsigned char)str[i] > 127) { + return 1; // Non-ASCII character detected, return 1 (indicating invalid) + } + } + return 0; // All characters are ASCII +} + +int is_valid(char *str) { + int lowercase_count = 0, uppercase_count = 0, digit_count = 0, special_count = 0; + int i; + int length = strlen(str); + + // Check if the input contains any non-ASCII characters + if (is_valid_ascii(str)) { + printf("Password contains non-ASCII characters.\n"); + return 1; // Invalid input, return 1 to signal failure + } + + // Check if the length is within the allowed range + if (length < MIN_PASSWORD_LENGTH) { + printf("Password is too short! It must be at least %d characters long.\n", MIN_PASSWORD_LENGTH); + return 1; + } + if (length > MAX_PASSWORD_LENGTH) { + printf("Password exceeds the maximum length of %d characters.\n", MAX_PASSWORD_LENGTH); + return 1; + } + + // Check each character of the string and count occurrences + for (i = 0; i < length; i++) { + if (islower(str[i])) { + lowercase_count++; + } else if (isupper(str[i])) { + uppercase_count++; + } else if (isdigit(str[i])) { + digit_count++; + } else if (ispunct(str[i])) { // Checks for special characters like !, @, #, etc. + special_count++; + } + } + + // Check if all counts meet the required minimums + if (lowercase_count < MIN_LOWERCASE) { + printf("Password needs at least %d lowercase letters.\n", MIN_LOWERCASE); + return 1; + } + if (uppercase_count < MIN_UPPERCASE) { + printf("Password needs at least %d uppercase letters.\n", MIN_UPPERCASE); + return 1; + } + if (digit_count < MIN_DIGITS) { + printf("Password needs at least %d digits.\n", MIN_DIGITS); + return 1; + } + if (special_count < MIN_SPECIAL) { + printf("Password needs at least %d special characters.\n", MIN_SPECIAL); + return 1; + } + + // If all conditions are met, return 0 (indicating success) + return 0; +} + +int main(int argc, char *argv[]) { + if (argc != 2) { + printf("Usage: %s \n", argv[0]); + return 1; + } + + if (is_valid(argv[1]) == 0) { + printf("The string is valid.\n"); + return 0; // Success + } else { + printf("The string is invalid. It must contain at least:\n"); + printf("- %d lowercase letters\n", MIN_LOWERCASE); + printf("- %d uppercase letters\n", MIN_UPPERCASE); + printf("- %d digits\n", MIN_DIGITS); + printf("- %d special characters\n", MIN_SPECIAL); + printf("And be between %d and %d characters long.\n", MIN_PASSWORD_LENGTH, MAX_PASSWORD_LENGTH); + return 1; // Invalid password, return 1 + } +} + diff --git a/config.h b/config.h new file mode 100644 index 0000000..5c7db49 --- /dev/null +++ b/config.h @@ -0,0 +1,50 @@ +#define VERSION "1.5.0" + +/* For chkgrp */ +#ifndef MAX_LINE +#define MAX_LINE 1024 +#endif + +/* MAX_NAME is also used by chkpwd */ +#ifndef MAX_NAME +#define MAX_NAME 256 +#endif + +/* For chkpwd */ +#ifndef MAX_PASSWORD +#define MAX_PASSWORD 256 +#endif + +#ifndef MAX_UID +#define MAX_UID 1000 +#endif + +#ifndef MIN_UID +#define MIN_UID 1000 +#endif + +/* For chkpwr */ +#ifndef MAX_PASSWORD_LENGTH +#define MAX_PASSWORD_LENGTH 32 +#endif + +#ifndef MIN_PASSWORD_LENGTH +#define MIN_PASSWORD_LENGTH 12 +#endif + +#ifndef MIN_LOWERCASE +#define MIN_LOWERCASE 2 +#endif + +#ifndef MIN_UPPERCASE +#define MIN_UPPERCASE 2 +#endif + +#ifndef MIN_DIGITS +#define MIN_DIGITS 2 +#endif + +#ifndef MIN_SPECIAL +#define MIN_SPECIAL 2 +#endif + diff --git a/version.h b/version.h deleted file mode 100644 index 7851500..0000000 --- a/version.h +++ /dev/null @@ -1 +0,0 @@ -#define VERSION "1.4.0"