]> git.ipfire.org Git - thirdparty/shadow.git/commitdiff
lib/, src/: Use strprefix() instead of its pattern
authorAlejandro Colomar <alx@kernel.org>
Tue, 10 Dec 2024 13:27:02 +0000 (14:27 +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>
22 files changed:
contrib/adduser.c
lib/commonio.c
lib/encrypt.c
lib/getdef.c
lib/limits.c
lib/nss.c
lib/port.c
lib/setupenv.c
lib/ttytype.c
lib/yesno.c
src/check_subid_range.c
src/grpck.c
src/login.c
src/login_nopam.c
src/newgrp.c
src/passwd.c
src/pwck.c
src/su.c
src/suauth.c
src/sulogin.c
src/useradd.c
src/usermod.c

index 8061c76a4460464f91983b8e0e8a61ce71a8cc55..8efec7aa3f583739325a88d9154c6c31fd8e91f1 100644 (file)
 #include <syslog.h>
 
 #include "string/strcmp/streq.h"
+#include "string/strcmp/strprefix.h"
 
 
 #define IMMEDIATE_CHANGE       /* Expire newly created password, must be changed
@@ -389,7 +390,7 @@ main (void)
       fflush (stdout);
       safeget (foo, sizeof (foo));
 
-      done = bad = correct = (foo[0] == 'y' || foo[0] == 'Y');
+      done = bad = correct = (strprefix(foo, "y") || strprefix(foo, "Y"));
 
       if (bad != 1)
        printf ("\nUser [%s] not added\n", usrname);
index fc8ad7a57b51da3454f0ed0059f76751655b8a77..38bf65c59dcf55c6479879188c8d1547a0986a84 100644 (file)
@@ -37,6 +37,7 @@
 #include "string/memset/memzero.h"
 #include "string/sprintf/snprintf.h"
 #include "string/strcmp/streq.h"
+#include "string/strcmp/strprefix.h"
 #include "string/strtok/stpsep.h"
 
 
@@ -515,7 +516,7 @@ static void add_one_entry (struct commonio_db *db,
 
 static bool name_is_nis (const char *name)
 {
-       return (('+' == name[0]) || ('-' == name[0]));
+       return strprefix(name, "+") || strprefix(name, "-");
 }
 
 
index 9c1cb40677df9a857c6e9fc93d526d828fd4ecea..1abe0a6232d6822102f25dae71a1abf73a8557e0 100644 (file)
@@ -17,6 +17,8 @@
 #include "prototypes.h"
 #include "defines.h"
 #include "shadowlog_internal.h"
+#include "string/strcmp/strprefix.h"
+
 
 /*@exposed@*//*@null@*/char *pw_encrypt (const char *clear, const char *salt)
 {
@@ -35,7 +37,7 @@
 
        /* Some crypt() do not return NULL if the algorithm is not
         * supported, and return a DES encrypted password. */
-       if ((NULL != salt) && (salt[0] == '$') && (strlen (cp) <= 13))
+       if ((NULL != salt) && strprefix(salt, "$") && (strlen (cp) <= 13))
        {
                /*@observer@*/const char *method;
                switch (salt[1])
index 8883a29cccbdf04aba4c0f778daaae49f85b112d..f963e5bc8a0b15ca85435b4817d4fdea04ca764e 100644 (file)
@@ -32,6 +32,7 @@
 #include "string/sprintf/xasprintf.h"
 #include "string/strcmp/strcaseeq.h"
 #include "string/strcmp/streq.h"
+#include "string/strcmp/strprefix.h"
 #include "string/strspn/stpspn.h"
 #include "string/strspn/stprspn.h"
 #include "string/strtok/stpsep.h"
@@ -569,7 +570,7 @@ static void def_load (void)
                 * Break the line into two fields.
                 */
                name = stpspn(buf, " \t");      /* first nonwhite */
-               if (streq(name, "") || *name == '#')
+               if (streq(name, "") || strprefix(name, "#"))
                        continue;       /* comment or empty */
 
                s = stpsep(name, " \t");  /* next field */
index 43a25967b462ac282c74b60b40aece0593e59026..55a5fa9d9ae3ce08ac0fbcfe1a307dddd2d48f99 100644 (file)
@@ -63,7 +63,7 @@ static int setrlimit_value (unsigned int resource,
        /* The "-" is special, not belonging to a strange negative limit.
         * It is infinity, in a controlled way.
         */
-       if ('-' == value[0]) {
+       if (strprefix(value, "-")) {
                limit = RLIM_INFINITY;
 
        } else {
@@ -371,7 +371,7 @@ static int setup_user_limits (const char *uname)
         * FIXME: A better (smarter) checking should be done
         */
        while (fgets (buf, 1024, fil) != NULL) {
-               if (('#' == buf[0]) || ('\n' == buf[0])) {
+               if (strprefix(buf, "#") || strprefix(buf, "\n")) {
                        continue;
                }
                MEMZERO(tempbuf);
@@ -402,7 +402,7 @@ static int setup_user_limits (const char *uname)
                                break;
                        } else if (streq(name, "*")) {
                                strcpy (deflimits, tempbuf);
-                       } else if (name[0] == '@') {
+                       } else if (strprefix(name, "@")) {
                                /* If the user is in the group, the group
                                 * limits apply unless later a line for
                                 * the specific user is found.
index d76cef8707cdb6130d6a411729fb76acfacc961e..a2eb9e006373f4d9c9d7b44440484f7c5c828f8b 100644 (file)
--- a/lib/nss.c
+++ b/lib/nss.c
@@ -16,6 +16,7 @@
 #include "shadowlog.h"
 #include "string/sprintf/snprintf.h"
 #include "string/strcmp/streq.h"
+#include "string/strcmp/strprefix.h"
 #include "string/strspn/stpspn.h"
 #include "string/strtok/stpsep.h"
 
@@ -79,7 +80,7 @@ nss_init(const char *nsswitch_path) {
        }
        p = NULL;
        while (getline(&line, &len, nssfp) != -1) {
-               if (line[0] == '#')
+               if (strprefix(line, "#"))
                        continue;
                if (strlen(line) < 8)
                        continue;
index a30e681897e887af77d5303f7fb8844a577b8736..7e1a7b70e7704cae015cde27f14f9eb158392db2 100644 (file)
@@ -20,6 +20,7 @@
 #include "port.h"
 #include "prototypes.h"
 #include "string/strcmp/streq.h"
+#include "string/strcmp/strprefix.h"
 #include "string/strtok/stpsep.h"
 
 
@@ -140,7 +141,7 @@ next:
                errno = saveerr;
                return NULL;
        }
-       if ('#' == buf[0])
+       if (strprefix(buf, "#"))
                goto next;
 
        stpsep(buf, "\n");
index b7dcb3fa182c3191a62745ad12179574a598fc5a..1bc3da314ca835bdac2022c0b0593431a17153b1 100644 (file)
@@ -28,6 +28,7 @@
 #include "shadowlog.h"
 #include "string/sprintf/xasprintf.h"
 #include "string/strcmp/streq.h"
+#include "string/strcmp/strprefix.h"
 #include "string/strdup/xstrdup.h"
 #include "string/strspn/stpspn.h"
 #include "string/strtok/stpsep.h"
@@ -61,7 +62,7 @@ static void read_env_file (const char *filename)
                cp = buf;
                /* ignore whitespace and comments */
                cp = stpspn(cp, " \t");
-               if (streq(cp, "") || ('#' == *cp)) {
+               if (streq(cp, "") || strprefix(cp, "#")) {
                        continue;
                }
                /*
index 740510421be350f62ade072c366b733e411dc45d..3514f2941b4ce48bdbbaf4f2be43ab3a882193a5 100644 (file)
@@ -18,6 +18,7 @@
 #include "getdef.h"
 #include "prototypes.h"
 #include "string/strcmp/streq.h"
+#include "string/strcmp/strprefix.h"
 #include "string/strtok/stpsep.h"
 
 
@@ -47,7 +48,7 @@ void ttytype (const char *line)
                return;
        }
        while (fgets (buf, sizeof buf, fp) == buf) {
-               if (buf[0] == '#') {
+               if (strprefix(buf, "#")) {
                        continue;
                }
 
index 029cd815e1c88213934ec72566a8634e8712751a..27f156bca32c26a5371e8e10920d5d5009e9a18a 100644 (file)
@@ -14,7 +14,9 @@
 #include <stdbool.h>
 #include <stdio.h>
 #include <stdlib.h>
+
 #include "prototypes.h"
+#include "string/strcmp/strprefix.h"
 
 
 /*
@@ -76,10 +78,9 @@ yes_or_no(bool read_only)
 static int
 rpmatch(const char *response)
 {
-       if (response[0] == 'y' || response[0] == 'Y')
+       if (strprefix(response, "y") || strprefix(response, "Y"))
                return 1;
-
-       if (response[0] == 'n' || response[0] == 'n')
+       if (strprefix(response, "n") || strprefix(response, "N"))
                return 0;
 
        return -1;
index 482267581fee442e8e0848f164f3d56c2b87821d..a33f92238111e0006a41ae1d7d68987318031a28 100644 (file)
 #include "atoi/getnum.h"
 #include "atoi/str2i/str2u.h"
 #include "defines.h"
-#include "prototypes.h"
-#include "subordinateio.h"
 #include "idmapping.h"
+#include "prototypes.h"
 #include "shadowlog.h"
+#include "string/strcmp/strprefix.h"
+#include "subordinateio.h"
 
 
 static const char Prog[] = "check_subid_range";
@@ -39,7 +40,7 @@ main(int argc, char **argv)
                exit(1);
 
        owner = argv[1];
-       check_uids = argv[2][0] == 'u';
+       check_uids = strprefix(argv[2], "u");
        if (get_uid(argv[3], &start) == -1)
                exit(1);
        if (str2ul(&count, argv[4]) == -1)
index d3f2baee6a453ff569e7b505c67c3cc7ed8b87c1..5fcf6fadfbc13775c37bd16bbb9534b4c680eb49 100644 (file)
@@ -25,6 +25,7 @@
 #include "shadowlog.h"
 #include "sssd.h"
 #include "string/strcmp/streq.h"
+#include "string/strcmp/strprefix.h"
 
 #ifdef SHADOWGRP
 #include "sgroupio.h"
@@ -470,7 +471,7 @@ static void check_grp_file (bool *errors, bool *changed)
                 * Skip all NIS entries.
                 */
 
-               if ((gre->line[0] == '+') || (gre->line[0] == '-')) {
+               if (strprefix(gre->line, "+") || strprefix(gre->line, "-")) {
                        continue;
                }
 
index 2866b1523ccd40af75af04dbd47901497c5a5d45..57e106b828a83087c0f19e4c8b12c01101eab803 100644 (file)
@@ -41,6 +41,7 @@
 #include "string/memset/memzero.h"
 #include "string/sprintf/snprintf.h"
 #include "string/strcmp/streq.h"
+#include "string/strcmp/strprefix.h"
 #include "string/strcpy/strtcpy.h"
 #include "string/strdup/xstrdup.h"
 #include "string/strftime.h"
@@ -267,7 +268,7 @@ static void process_flags (int argc, char *const *argv)
         * clever telnet, and getty holes.
         */
        for (arg = 1; arg < argc; arg++) {
-               if (argv[arg][0] == '-' && strlen (argv[arg]) > 2) {
+               if (strprefix(argv[arg], "-") && strlen(argv[arg]) > 2) {
                        usage ();
                }
                if (streq(argv[arg], "--")) {
@@ -348,7 +349,7 @@ static void init_env (void)
        else {
                cp = getdef_str ("ENV_TZ");
                if (NULL != cp) {
-                       addenv (('/' == *cp) ? tz (cp) : cp, NULL);
+                       addenv(strprefix(cp, "/") ? tz(cp) : cp, NULL);
                }
        }
 #endif                         /* !USE_PAM */
@@ -856,8 +857,8 @@ int main (int argc, char **argv)
                         * login, even if they have been
                         * "pre-authenticated."
                         */
-                       if (   ('!' == user_passwd[0])
-                           || ('*' == user_passwd[0])) {
+                       if (   strprefix(user_passwd, "!")
+                           || strprefix(user_passwd, "*")) {
                                failed = true;
                        }
 
@@ -1015,7 +1016,7 @@ int main (int argc, char **argv)
                addenv ("IFS= \t\n", NULL);     /* ... instead, set a safe IFS */
        }
 
-       if (pwd->pw_shell[0] == '*') {  /* subsystem root */
+       if (strprefix(pwd->pw_shell, "*")) {  /* subsystem root */
                pwd->pw_shell++;        /* skip the '*' */
                subsystem (pwd);        /* figure out what to execute */
                subroot = true; /* say I was here again */
index 09993b627efb0bb7f2cfda0c2f9dab1510c82a31..b7a7bd54f8c8002180d6de6d431bd7850ec37c8e 100644 (file)
@@ -110,7 +110,7 @@ login_access(const char *user, const char *from)
                                         TABLE, lineno));
                                continue;
                        }
-                       if (line[0] == '#') {
+                       if (strprefix(line, "#")) {
                                continue;       /* comment line */
                        }
                        stpcpy(stprspn(line, " \t"), "");
@@ -141,7 +141,7 @@ login_access(const char *user, const char *from)
                int err = errno;
                SYSLOG ((LOG_ERR, "cannot open %s: %s", TABLE, strerror (err)));
        }
-       return (!match || (line[0] == '+'))?1:0;
+       return (!match || strprefix(line, "+"))?1:0;
 }
 
 /* list_match - match an item against a list of tokens with exceptions */
@@ -225,7 +225,7 @@ static bool user_match (char *tok, const char *string)
        if (host != NULL) {
                return user_match(tok, string) && from_match(host, myhostname());
 #if HAVE_INNETGR
-       } else if (tok[0] == '@') {     /* netgroup */
+       } else if (strprefix(tok, "@")) {       /* netgroup */
                return (netgroup_match (tok + 1, NULL, string));
 #endif
        } else if (string_match (tok, string)) {        /* ALL or exact match */
@@ -298,13 +298,13 @@ static bool from_match (char *tok, const char *string)
         * if it matches the head of the string.
         */
 #if HAVE_INNETGR
-       if (tok[0] == '@') {    /* netgroup */
+       if (strprefix(tok, "@")) {  /* netgroup */
                return (netgroup_match (tok + 1, string, NULL));
        } else
 #endif
        if (string_match (tok, string)) {       /* ALL or exact match */
                return true;
-       } else if (tok[0] == '.') {     /* domain: match last fields */
+       } else if (strprefix(tok, ".")) {  /* domain: match last fields */
                size_t  str_len, tok_len;
 
                str_len = strlen (string);
index 8b3844cc3530ddf09dd11ada77a6edc578e25593..94340bd1ceff9142ac4f254981b9397f66d04dbf 100644 (file)
@@ -510,7 +510,7 @@ int main (int argc, char **argv)
                 * Do the command line for "newgrp". It's just making sure
                 * there aren't any flags and getting the new group name.
                 */
-               if ((argc > 0) && (argv[0][0] == '-')) {
+               if ((argc > 0) && strprefix(argv[0], "-")) {
                        usage ();
                        goto failure;
                } else if (argv[0] != NULL) {
index c545550fdf4ab160deeead9a2aa547081b9da86d..df3394a04f59afbffd13f89a3f95a0c857c682ac 100644 (file)
@@ -35,6 +35,7 @@
 #include "string/memset/memzero.h"
 #include "string/sprintf/xasprintf.h"
 #include "string/strcmp/streq.h"
+#include "string/strcmp/strprefix.h"
 #include "string/strcpy/strtcpy.h"
 #include "string/strdup/xstrdup.h"
 #include "time/day_to_str.h"
@@ -402,7 +403,7 @@ static void check_password (const struct passwd *pw, const struct spwd *sp)
         * changed. Passwords which have been inactive too long cannot be
         * changed.
         */
-       if (   (sp->sp_pwdp[0] == '!')
+       if (   strprefix(sp->sp_pwdp, "!")
            || (exp_status > 1)
            || (   (sp->sp_max >= 0)
                && (sp->sp_min > sp->sp_max))) {
@@ -439,7 +440,7 @@ static void check_password (const struct passwd *pw, const struct spwd *sp)
 
 static /*@observer@*/const char *pw_status (const char *pass)
 {
-       if (*pass == '*' || *pass == '!') {
+       if (strprefix(pass, "*") || strprefix(pass, "!")) {
                return "L";
        }
        if (streq(pass, "")) {
@@ -520,7 +521,7 @@ static char *update_crypt_pw (char *cp)
        if (dflg)
                strcpy(cp, "");
 
-       if (uflg && *cp == '!') {
+       if (uflg && strprefix(cp, "!")) {
                if (cp[1] == '\0') {
                        (void) fprintf (stderr,
                                        _("%s: unlocking the password would result in a passwordless account.\n"
index b485a5a87f9aeece2797eb2e4a0b92aa22a0d83a..6272839fdc85efce42b8ee3cbe6bb9985a1f453d 100644 (file)
@@ -29,6 +29,7 @@
 #include "shadowlog.h"
 #include "sssd.h"
 #include "string/strcmp/streq.h"
+#include "string/strcmp/strprefix.h"
 #ifdef WITH_TCB
 #include "tcbfuncs.h"
 #endif                         /* WITH_TCB */
@@ -383,7 +384,7 @@ static void check_pw_file (bool *errors, bool *changed)
                 * If this is a NIS line, skip it. You can't "know" what NIS
                 * is going to do without directly asking NIS ...
                 */
-               if (('+' == pfe->line[0]) || ('-' == pfe->line[0])) {
+               if (strprefix(pfe->line, "+") || strprefix(pfe->line, "-")) {
                        continue;
                }
 
@@ -708,7 +709,7 @@ static void check_spw_file (bool *errors, bool *changed)
                 * If this is a NIS line, skip it. You can't "know" what NIS
                 * is going to do without directly asking NIS ...
                 */
-               if (('+' == spe->line[0]) || ('-' == spe->line[0])) {
+               if (strprefix(spe->line, "+") || strprefix(spe->line, "-")) {
                        continue;
                }
 
index 970ff1b3cd7bd5b8defb80bbb3b9dfbd8448f1ff..0902c49a6322d48e621d1c6d31f6e4a0dfd50a0f 100644 (file)
--- a/src/su.c
+++ b/src/su.c
@@ -62,6 +62,7 @@
 #include "string/sprintf/snprintf.h"
 #include "string/sprintf/xasprintf.h"
 #include "string/strcmp/streq.h"
+#include "string/strcmp/strprefix.h"
 #include "string/strcpy/strtcpy.h"
 #include "string/strdup/xstrdup.h"
 
@@ -714,7 +715,7 @@ static /*@only@*/struct passwd * do_check_perms (void)
         * the shell specified in /etc/passwd (not the one specified with
         * --shell, which will be the one executed in the chroot later).
         */
-       if ('*' == pw->pw_shell[0]) {   /* subsystem root required */
+       if (strprefix(pw->pw_shell, "*")) {  /* subsystem root required */
                subsystem (pw); /* change to the subsystem root */
                endpwent ();            /* close the old password databases */
                endspent ();
@@ -915,7 +916,7 @@ static void set_environment (struct passwd *pw)
 #ifndef USE_PAM
                cp = getdef_str ("ENV_TZ");
                if (NULL != cp) {
-                       addenv (('/' == *cp) ? tz (cp) : cp, NULL);
+                       addenv(strprefix(cp, "/") ? tz(cp) : cp, NULL);
                }
 
                /*
index cd2f87b657e9212d78986dd37f3a211dc45b4335..2a5affb24c47ede16caa916fe05a662bf5db9038 100644 (file)
@@ -20,6 +20,7 @@
 #include "defines.h"
 #include "prototypes.h"
 #include "string/strcmp/streq.h"
+#include "string/strcmp/strprefix.h"
 #include "string/strspn/stpspn.h"
 #include "string/strspn/stprspn.h"
 #include "string/strtok/stpsep.h"
@@ -87,7 +88,7 @@ check_su_auth(const char *actual_id, const char *wanted_id, bool su_to_root)
                stpcpy(stprspn(temp, " \t"), "");
 
                p = stpspn(temp, " \t");
-               if (*p == '#' || streq(p, ""))
+               if (strprefix(p, "#") || streq(p, ""))
                        continue;
 
                to_users = strsep(&p, ":");
index 655a583abe6035a4bf92ace28b9f1fb6e8209ab4..e2e491aec07e7f5428b772cd562bf065c41c6cd4 100644 (file)
@@ -28,6 +28,7 @@
 #include "exitcodes.h"
 #include "shadowlog.h"
 #include "string/strcmp/streq.h"
+#include "string/strcmp/strprefix.h"
 #include "string/strdup/xstrdup.h"
 
 
@@ -117,7 +118,7 @@ main(int argc, char *argv[])
 #ifndef USE_PAM
        env = getdef_str ("ENV_TZ");
        if (NULL != env) {
-               addenv (('/' == *env) ? tz (env) : env, NULL);
+               addenv(strprefix(env, "/") ? tz(env) : env, NULL);
        }
        env = getdef_str ("ENV_HZ");
        if (NULL != env) {
index 84647ee1160dd67909e9fc7a57511c4226c9cdf8..258a6cbbba33a809ff7ce31089325b1511309070 100644 (file)
@@ -69,6 +69,7 @@
 #include "string/sprintf/xasprintf.h"
 #include "string/strcmp/strcaseeq.h"
 #include "string/strcmp/streq.h"
+#include "string/strcmp/strprefix.h"
 #include "string/strdup/xstrdup.h"
 #include "string/strtok/stpsep.h"
 
@@ -2218,7 +2219,7 @@ static void create_home (void)
         */
        for (cp = strtok(bhome, "/"); cp != NULL; cp = strtok(NULL, "/")) {
                /* Avoid turning a relative path into an absolute path. */
-               if (bhome[0] == '/' || !streq(path, ""))
+               if (strprefix(bhome, "/") || !streq(path, ""))
                        strcat(path, "/");
 
                strcat(path, cp);
index fc29729a917727246a17a3e32752fd9191588cce..b4cf2e705e699d917f770b9577cb49aa08da8436 100644 (file)
@@ -65,6 +65,7 @@
 #include "string/memset/memzero.h"
 #include "string/sprintf/xasprintf.h"
 #include "string/strcmp/streq.h"
+#include "string/strcmp/strprefix.h"
 #include "string/strdup/xstrdup.h"
 #include "time/day_to_str.h"
 
@@ -436,7 +437,7 @@ static char *new_pw_passwd (char *pw_pass)
                SYSLOG ((LOG_INFO, "lock user '%s' password", user_newname));
                xasprintf(&buf, "!%s", pw_pass);
                pw_pass = buf;
-       } else if (Uflg && pw_pass[0] == '!') {
+       } else if (Uflg && strprefix(pw_pass, "!")) {
                if (pw_pass[1] == '\0') {
                        fprintf (stderr,
                                 _("%s: unlocking the user's password would result in a passwordless account.\n"