diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..64263f1 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +checkpwrule +checkpwrule.o diff --git a/LICENSE b/LICENSE index 48a33c7..6e58503 100644 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,6 @@ MIT License -Copyright (c) 2024 xw3 +Copyright (c) 2024 hanez Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in 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: diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..9cdaec9 --- /dev/null +++ b/Makefile @@ -0,0 +1,12 @@ +all: + $(CC) -Wall -o checkpwrule checkpwrule.c + +clean: + rm -f ./checkpwrule + +install: + cp ./checkpwrule /usr/bin/ + +uninstall: + rm -f /usr/bin/checkpwrule + diff --git a/README.md b/README.md index 59ac5c4..f8f03f6 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,4 @@ # checkpwrule +More to come... + diff --git a/checkpwrule.c b/checkpwrule.c new file mode 100644 index 0000000..5e5c459 --- /dev/null +++ b/checkpwrule.c @@ -0,0 +1,105 @@ +/** + * checkpwrule is a program that checks if an input string validates a + * password rule. + * + * Author: Johannes Findeisen - 2024 + * License: MIT (see LICENSE) + */ + +#include +#include +#include + +#define MAX_PASSWORD_LENGTH 32 +#define MIN_PASSWORD_LENGTH 12 +#define MIN_LOWERCASE 2 +#define MIN_UPPERCASE 2 +#define MIN_DIGITS 2 +#define MIN_SPECIAL 2 + +// 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 + } +} +