]> git.ipfire.org Git - thirdparty/shadow.git/commitdiff
lib/: Use strprefix() instead of its pattern
authorAlejandro Colomar <alx@kernel.org>
Tue, 10 Dec 2024 02:00:18 +0000 (03:00 +0100)
committerSerge Hallyn <serge@hallyn.com>
Mon, 26 May 2025 16:29:26 +0000 (11:29 -0500)
Signed-off-by: Alejandro Colomar <alx@kernel.org>
lib/limits.c
lib/prefix_flag.c
lib/root_flag.c
lib/user_busy.c
src/login_nopam.c
src/newgidmap.c
src/newgrp.c
src/newuidmap.c

index 451fdf1c580bee5938da7d21c569dd14d31f50ad..43a25967b462ac282c74b60b40aece0593e59026 100644 (file)
@@ -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));
index bcf5d504fbd34c969ef9c513d3a2158a9e8f13c2..53c13fafb304046c4a349f8d632420d6689adda4 100644 (file)
@@ -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) {
index 84a659807aafd49ab8fe82d4a5ce24f2721d263e..e8a5a6d260b9f8125165bcd1070d0d5bab7678ed 100644 (file)
@@ -17,6 +17,7 @@
 #include "prototypes.h"
 #include "shadowlog.h"
 #include "string/strcmp/streq.h"
+#include "string/strcmp/strprefix.h"
 
 #include <assert.h>
 
@@ -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"),
index e4b19afe494be602b5e0fe686838de3578116167..d689d34d60b37e19b1c8beec81189270f67e0d08 100644 (file)
@@ -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);
index b4f637baf728148cfdc72cfd860e053e28a30a6c..09993b627efb0bb7f2cfda0c2f9dab1510c82a31 100644 (file)
@@ -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;
index 53c401e75ec88d2fab4d2d77aca92e6049c15a68..3463e85085de797e18576b272b8ced67dabca549 100644 (file)
@@ -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);
index effd3d3f63cdb96e61200689829072cb7488b300..8b3844cc3530ddf09dd11ada77a6edc578e25593 100644 (file)
@@ -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++;
                }
index d489de8b31c86b934f0db7e8b41e9534fecc8fe7..894c5ec8619816130c0709fd299e6c9696823af3 100644 (file)
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <fcntl.h>
+
 #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);