Merged chkpwr to chkusr and moved defines to config.h.

This commit is contained in:
Johannes Findeisen 2025-09-08 02:34:46 +02:00
commit 71555fc275
12 changed files with 171 additions and 51 deletions

1
.gitignore vendored
View file

@ -8,5 +8,6 @@ chkgrp-native-min-extended
chkgrp-ng
chkpwd
chkpwd.1.gz
chkpwr
test
test.c

View file

@ -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

View file

@ -6,7 +6,7 @@
* License: Apache-2.0 (see LICENSE)
*/
#include "version.h"
#include "config.h"
#include <stdio.h>
#include <stdlib.h>
@ -14,8 +14,6 @@
#include <pwd.h>
#include <grp.h>
#define MAX_NAME 256
int main(int argc, char *argv[])
{
if (argc != 3)

View file

@ -7,16 +7,13 @@
* License: Apache-2.0 (see LICENSE)
*/
#include "version.h"
#include "config.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define MAX_LINE 1024
#define MAX_NAME 256
int main(int argc, char *argv[])
{
if (argc != 3)

View file

@ -6,15 +6,12 @@
* License: Apache-2.0 (see LICENSE)
*/
#include "version.h"
#include "config.h"
#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)

View file

@ -6,15 +6,12 @@
* License: Apache-2.0 (see LICENSE)
*/
#include "version.h"
#include "config.h"
#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) {

View file

@ -6,15 +6,12 @@
* License: Apache-2.0 (see LICENSE)
*/
#include "version.h"
#include "config.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_LINE 1024
#define MAX_NAME 256
void print_help(const char *prog)
{
printf("Usage: %s [OPTIONS] <username> <groupname>\n", prog);

View file

@ -6,7 +6,7 @@
* License: Apache-2.0 (see LICENSE)
*/
#include "version.h"
#include "config.h"
#include <stdio.h>
#include <stdlib.h>
@ -17,10 +17,6 @@
#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) {
@ -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) {

View file

@ -8,7 +8,7 @@
*/
#include "chkpwd.h"
#include "version.h"
#include "config.h"
#include <stdbool.h>
#include <stdio.h>
@ -17,22 +17,6 @@
#include <termios.h>
#include <unistd.h>
#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]);

100
chkpwr.c Normal file
View file

@ -0,0 +1,100 @@
/**
* checkpwrule is a program that checks if an input string validates a
* password rule.
*
* Author: Johannes Findeisen <you@hanez.org> - 2024
* License: MIT (see LICENSE)
*/
#include "config.h"
#include <stdio.h>
#include <ctype.h>
#include <string.h>
// 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 <string>\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
}
}

50
config.h Normal file
View file

@ -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

View file

@ -1 +0,0 @@
#define VERSION "1.4.0"