]> git.ipfire.org Git - thirdparty/gnutls.git/commitdiff
certtool: removed limits in interactive input
authorNikos Mavrogiannopoulos <nmav@redhat.com>
Tue, 28 Feb 2017 07:24:30 +0000 (08:24 +0100)
committerNikos Mavrogiannopoulos <nmav@redhat.com>
Tue, 28 Feb 2017 07:35:27 +0000 (08:35 +0100)
That removes the limits when reading most of the interactive input.
The read_str() function due to its dependence on static variable remains
with a limit, but will output an error if the input string exceeds size.

Resolves #179

Signed-off-by: Nikos Mavrogiannopoulos <nmav@redhat.com>
src/certtool-cfg.c

index 73f273d43f43e164276d408a317b61cce3c5eecd..6415ee8cf3136c8f02437b8e5d3b91261b635c80 100644 (file)
@@ -556,48 +556,86 @@ int template_parse(const char *template)
 #define IS_NEWLINE(x) ((x[0] == '\n') || (x[0] == '\r'))
 #define MAX_INPUT_SIZE 512
 
+static size_t strip_nl(char *str, size_t str_size)
+{
+       if ((str_size > 0) && (str[str_size - 1] == '\n')) {
+               str_size--;
+               str[str_size] = 0;
+       }
+       if ((str_size > 0) && (str[str_size - 1] == '\r')) {
+               str_size--;
+               str[str_size] = 0;
+       }
+       return str_size;
+}
+
+static int copystr_without_nl(char *out, size_t out_size, const char *in, size_t in_size)
+{
+       if (in_size+1 >= out_size) {
+               fprintf(stderr, "Too long line to parse in interactive mode; please use templates.\n");
+               exit(1);
+       }
+       memcpy(out, in, in_size+1); /* copy terminating null */
+       strip_nl(out, in_size);
+       return 0;
+}
+
 void
 read_crt_set(gnutls_x509_crt_t crt, const char *input_str, const char *oid)
 {
-       char input[MAX_INPUT_SIZE];
-       int ret;
+       ssize_t ret;
+       char *lineptr = NULL;
+       size_t linesize = 0;
 
        fputs(input_str, stderr);
-       if (fgets(input, sizeof(input), stdin) == NULL)
+       ret = getline(&lineptr, &linesize, stdin);
+       if (ret == -1)
                return;
 
-       if (IS_NEWLINE(input))
+       if (IS_NEWLINE(lineptr)) {
+               free(lineptr);
                return;
+       }
+
+       linesize = strip_nl(lineptr, ret);
 
        ret =
-           gnutls_x509_crt_set_dn_by_oid(crt, oid, 0, input,
-                                         strlen(input) - 1);
+           gnutls_x509_crt_set_dn_by_oid(crt, oid, 0, lineptr,
+                                         linesize);
        if (ret < 0) {
                fprintf(stderr, "set_dn: %s\n", gnutls_strerror(ret));
                exit(1);
        }
+       free(lineptr);
 }
 
 void
 read_crq_set(gnutls_x509_crq_t crq, const char *input_str, const char *oid)
 {
-       char input[MAX_INPUT_SIZE];
-       int ret;
+       ssize_t ret;
+       char *lineptr = NULL;
+       size_t linesize = 0;
 
        fputs(input_str, stderr);
-       if (fgets(input, sizeof(input), stdin) == NULL)
+       ret = getline(&lineptr, &linesize, stdin);
+       if (ret == -1)
                return;
 
-       if (IS_NEWLINE(input))
+       if (IS_NEWLINE(lineptr)) {
+               free(lineptr);
                return;
+       }
+
+       linesize = strip_nl(lineptr, ret);
 
        ret =
-           gnutls_x509_crq_set_dn_by_oid(crq, oid, 0, input,
-                                         strlen(input) - 1);
+           gnutls_x509_crq_set_dn_by_oid(crq, oid, 0, lineptr,
+                                         linesize);
        if (ret < 0) {
                fprintf(stderr, "set_dn: %s\n", gnutls_strerror(ret));
                exit(1);
        }
+       free(lineptr);
 }
 
 /* The input_str should contain %d or %u to print the default.
@@ -663,18 +701,23 @@ int64_t read_int(const char *input_str)
 const char *read_str(const char *input_str)
 {
        static char input[MAX_INPUT_SIZE];
-       int len;
+       ssize_t ret;
+       char *lineptr = NULL;
+       size_t linesize = 0;
 
        fputs(input_str, stderr);
-       if (fgets(input, sizeof(input), stdin) == NULL)
+       ret = getline(&lineptr, &linesize, stdin);
+       if (ret == -1)
+               return NULL;
+
+       ret = copystr_without_nl(input, sizeof(input), lineptr, ret);
+       free(lineptr);
+       if (ret < 0)
                return NULL;
 
        if (IS_NEWLINE(input))
                return NULL;
 
-       len = strlen(input);
-       if ((len > 0) && (input[len - 1] == '\n'))
-               input[len - 1] = 0;
        if (input[0] == 0)
                return NULL;