]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
agetty: add cred_read_num() for credential numeric parsing
authorKarel Zak <kzak@redhat.com>
Wed, 22 Apr 2026 10:21:50 +0000 (12:21 +0200)
committerKarel Zak <kzak@redhat.com>
Wed, 22 Apr 2026 10:21:50 +0000 (12:21 +0200)
Introduce cred_read_num() helper that reads a systemd credential
file and parses the value as a number. The type argument selects
between unsigned ('u') and signed ('d') parsing, making it easy
to extend for other numeric types in the future.

Addresses: https://github.com/util-linux/util-linux/issues/2255
Signed-off-by: Karel Zak <kzak@redhat.com>
term-utils/agetty.c

index de5215d484ab268ee38863268e674de58575d771..959386706ac2ae68f65aebd2dd65015f9baab04c 100644 (file)
@@ -3108,6 +3108,43 @@ static int cred_read_str(struct path_cxt *pc, const char *name,
        return 0;
 }
 
+static int cred_read_num(struct path_cxt *pc, const char *name,
+                        struct options *op, size_t offset, int type)
+{
+       char *str = NULL;
+       int rc;
+
+       if (ul_path_read_string(pc, &str, name) < 0)
+               return -1;
+
+       switch (type) {
+       case 'u':
+       {
+               uint32_t num;
+               rc = ul_strtou32(str, &num, 10);
+               if (rc == 0)
+                       *((unsigned int *) ((char *) op + offset)) = num;
+               break;
+       }
+       case 'd':
+       {
+               int32_t num;
+               rc = ul_strtos32(str, &num, 10);
+               if (rc == 0)
+                       *((int *) ((char *) op + offset)) = num;
+               break;
+       }
+       default:
+               rc = -EINVAL;
+               break;
+       }
+
+       if (rc)
+               log_warn(_("invalid '%s' credential value"), name);
+       free(str);
+       return rc;
+}
+
 static void load_credentials(struct options *op)
 {
        char *env;