From: Alejandro Colomar Date: Tue, 10 Dec 2024 02:00:18 +0000 (+0100) Subject: lib/: Use strprefix() instead of its pattern X-Git-Tag: 4.18.0-rc1~76 X-Git-Url: http://git.ipfire.org/gitweb/gitweb.cgi?a=commitdiff_plain;h=68f28eb04474c744ff58f3f205264848260e166c;p=thirdparty%2Fshadow.git lib/: Use strprefix() instead of its pattern Signed-off-by: Alejandro Colomar --- diff --git a/lib/limits.c b/lib/limits.c index 451fdf1c5..43a25967b 100644 --- a/lib/limits.c +++ b/lib/limits.c @@ -474,12 +474,15 @@ void setup_limits (const struct passwd *info) } } for (cp = info->pw_gecos; cp != NULL; cp = strchr (cp, ',')) { + char *val; + cp = strprefix(cp, ",") ?: cp; - if (strncmp (cp, "pri=", 4) == 0) { + val = strprefix(cp, "pri="); + if (val != NULL) { int inc; - if (a2si(&inc, cp + 4, NULL, 0, -20, 20) == 0) { + if (a2si(&inc, val, NULL, 0, -20, 20) == 0) { errno = 0; if ( (nice (inc) != -1) || (0 != errno)) { @@ -494,10 +497,12 @@ void setup_limits (const struct passwd *info) continue; } - if (strncmp (cp, "ulimit=", 7) == 0) { + + val = strprefix(cp, "ulimit="); + if (val != NULL) { int blocks; - if ( (str2si(&blocks, cp + 7) == -1) + if ( (str2si(&blocks, val) == -1) || (set_filesize_limit (blocks) != 0)) { SYSLOG ((LOG_WARN, "Can't set the ulimit for user %s", @@ -505,10 +510,12 @@ void setup_limits (const struct passwd *info) } continue; } - if (strncmp (cp, "umask=", 6) == 0) { + + val = strprefix(cp, "umask="); + if (val != NULL) { mode_t mask; - if (str2i(mode_t, &mask, cp + 6) == -1) { + if (str2i(mode_t, &mask, val) == -1) { SYSLOG ((LOG_WARN, "Can't set umask value for user %s", info->pw_name)); diff --git a/lib/prefix_flag.c b/lib/prefix_flag.c index bcf5d504f..53c13fafb 100644 --- a/lib/prefix_flag.c +++ b/lib/prefix_flag.c @@ -30,6 +30,7 @@ #include "shadowlog.h" #include "string/sprintf/xasprintf.h" #include "string/strcmp/streq.h" +#include "string/strcmp/strprefix.h" static char *passwd_db_file = NULL; @@ -58,10 +59,10 @@ extern const char* process_prefix_flag (const char* short_opt, int argc, char ** for (int i = 0; i < argc; i++) { const char *val; - val = NULL; + val = strprefix(argv[i], "--prefix="); + if ( streq(argv[i], "--prefix") - || ((strncmp (argv[i], "--prefix=", 9) == 0) - && (val = argv[i] + 9)) + || val != NULL || streq(argv[i], short_opt)) { if (NULL != prefix) { diff --git a/lib/root_flag.c b/lib/root_flag.c index 84a659807..e8a5a6d26 100644 --- a/lib/root_flag.c +++ b/lib/root_flag.c @@ -17,6 +17,7 @@ #include "prototypes.h" #include "shadowlog.h" #include "string/strcmp/streq.h" +#include "string/strcmp/strprefix.h" #include @@ -39,11 +40,12 @@ extern void process_root_flag (const char* short_opt, int argc, char **argv) for (int i = 0; i < argc; i++) { const char *val; - val = NULL; + val = strprefix(argv[i], "--root="); + if ( streq(argv[i], "--root") - || ((strncmp (argv[i], "--root=", 7) == 0) - && (val = argv[i] + 7)) - || streq(argv[i], short_opt)) { + || val != NULL + || streq(argv[i], short_opt)) + { if (NULL != newroot) { fprintf (log_get_logfd(), _("%s: multiple --root options\n"), diff --git a/lib/user_busy.c b/lib/user_busy.c index e4b19afe4..d689d34d6 100644 --- a/lib/user_busy.c +++ b/lib/user_busy.c @@ -127,7 +127,7 @@ static int check_status (const char *name, const char *sname, uid_t uid) return 0; } while (fgets (line, sizeof (line), sfile) == line) { - if (strncmp (line, "Uid:\t", 5) == 0) { + if (strprefix(line, "Uid:\t")) { unsigned long ruid, euid, suid; assert (uid == (unsigned long) uid); diff --git a/src/login_nopam.c b/src/login_nopam.c index b4f637baf..09993b627 100644 --- a/src/login_nopam.c +++ b/src/login_nopam.c @@ -61,6 +61,7 @@ #include "sizeof.h" #include "string/strcmp/strcaseeq.h" #include "string/strcmp/streq.h" +#include "string/strcmp/strprefix.h" #include "string/strspn/stprspn.h" #include "string/strtok/stpsep.h" @@ -288,8 +289,6 @@ static const char *resolve_hostname (const char *string) static bool from_match (char *tok, const char *string) { - size_t tok_len; - /* * If a token has the magic value "ALL" the match always succeeds. Return * true if the token fully matches the string. If the token is a domain @@ -306,7 +305,8 @@ static bool from_match (char *tok, const char *string) if (string_match (tok, string)) { /* ALL or exact match */ return true; } else if (tok[0] == '.') { /* domain: match last fields */ - size_t str_len; + size_t str_len, tok_len; + str_len = strlen (string); tok_len = strlen (tok); if ( (str_len > tok_len) @@ -317,8 +317,8 @@ static bool from_match (char *tok, const char *string) if (strchr (string, '.') == NULL) { return true; } - } else if ( (!streq(tok, "") && tok[(tok_len = strlen(tok)) - 1] == '.') /* network */ - && (strncmp (tok, resolve_hostname (string), tok_len) == 0)) { + } else if ( (!streq(tok, "") && tok[strlen(tok) - 1] == '.') /* network */ + && strprefix(resolve_hostname(string), tok)) { return true; } return false; diff --git a/src/newgidmap.c b/src/newgidmap.c index 53c401e75..3463e8508 100644 --- a/src/newgidmap.c +++ b/src/newgidmap.c @@ -169,7 +169,7 @@ int main(int argc, char **argv) */ target_str = argv[1]; - if (strlen(target_str) > 3 && strncmp(target_str, "fd:", 3) == 0) { + if (strlen(target_str) > 3 && strprefix(target_str, "fd:")) { /* the user passed in a /proc/pid fd for the process */ target_str = &target_str[3]; proc_dir_fd = get_pidfd_from_fd(target_str); diff --git a/src/newgrp.c b/src/newgrp.c index effd3d3f6..8b3844cc3 100644 --- a/src/newgrp.c +++ b/src/newgrp.c @@ -769,11 +769,13 @@ int main (int argc, char **argv) } while (NULL != *envp) { - if (strncmp (*envp, "PATH=", 5) == 0 || - strncmp (*envp, "HOME=", 5) == 0 || - strncmp (*envp, "SHELL=", 6) == 0 || - strncmp (*envp, "TERM=", 5) == 0) + if (strprefix(*envp, "PATH=") || + strprefix(*envp, "HOME=") || + strprefix(*envp, "SHELL=") || + strprefix(*envp, "TERM=")) + { addenv (*envp, NULL); + } envp++; } diff --git a/src/newuidmap.c b/src/newuidmap.c index d489de8b3..894c5ec86 100644 --- a/src/newuidmap.c +++ b/src/newuidmap.c @@ -13,12 +13,15 @@ #include #include #include + #include "defines.h" -#include "prototypes.h" -#include "subordinateio.h" #include "getdef.h" #include "idmapping.h" +#include "prototypes.h" #include "shadowlog.h" +#include "string/strcmp/strprefix.h" +#include "subordinateio.h" + /* * Global variables @@ -94,7 +97,7 @@ int main(int argc, char **argv) /* Find the process that needs its user namespace * uid mapping set. */ - if (strlen(target_str) > 3 && strncmp(target_str, "fd:", 3) == 0) { + if (strlen(target_str) > 3 && strprefix(target_str, "fd:")) { /* the user passed in a /proc/pid fd for the process */ target_str = &target_str[3]; proc_dir_fd = get_pidfd_from_fd(target_str);