mirror of
https://github.com/vincentmli/bpfire.git
synced 2026-04-11 19:55:52 +02:00
misc-progs: Add functions to sanitise input arguments
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
This commit is contained in:
@@ -199,3 +199,42 @@ int initsetuid(void) {
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* Checks if a string only contains alphanumerical characters, dash or underscore */
|
||||
int is_valid_argument_alnum(const char* arg) {
|
||||
size_t l = strlen(arg);
|
||||
|
||||
for (unsigned int i = 0; i < l; i++) {
|
||||
char c = arg[i];
|
||||
|
||||
// Dash or underscore
|
||||
if (c == '-' || c == '_')
|
||||
continue;
|
||||
|
||||
// Any alphanumerical character
|
||||
if (isalnum(c))
|
||||
continue;
|
||||
|
||||
// Invalid
|
||||
return 0;
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
int is_valid_argument_num(const char* arg) {
|
||||
size_t l = strlen(arg);
|
||||
|
||||
for (unsigned int i = 0; i < l; i++) {
|
||||
char c = arg[i];
|
||||
|
||||
// Any digit
|
||||
if (isdigit(c))
|
||||
continue;
|
||||
|
||||
// Invalid
|
||||
return 0;
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
@@ -33,6 +33,9 @@ int safe_system(char* command);
|
||||
int unpriv_system(char* command, uid_t uid, gid_t gid);
|
||||
int initsetuid(void);
|
||||
|
||||
int is_valid_argument_alnum(const char* arg);
|
||||
int is_valid_argument_num(const char* arg);
|
||||
|
||||
/* Compatibility for the local copy of strlcat,
|
||||
* which has been removed. */
|
||||
#define strlcat(src, dst, size) strncat(src, dst, size)
|
||||
|
||||
Reference in New Issue
Block a user