Fix various incorrect error function codes.
[openssl.git] / crypto / conf / conf_def.c
index f7d088d..f14de22 100644 (file)
@@ -59,6 +59,7 @@
 /* Part of the code in here was originally in conf.c, which is now removed */
 
 #include <stdio.h>
+#include <string.h>
 #include <openssl/stack.h>
 #include <openssl/lhash.h>
 #include <openssl/conf.h>
@@ -66,6 +67,7 @@
 #include "conf_def.h"
 #include <openssl/buffer.h>
 #include <openssl/err.h>
+#include "cryptlib.h"
 
 static char *eat_ws(CONF *conf, char *p);
 static char *eat_alpha_numeric(CONF *conf, char *p);
@@ -73,17 +75,18 @@ static void clear_comments(CONF *conf, char *p);
 static int str_copy(CONF *conf,char *section,char **to, char *from);
 static char *scan_quote(CONF *conf, char *p);
 static char *scan_dquote(CONF *conf, char *p);
-#define scan_esc(p)    (((IS_EOF((conf),(p)[1]))?(p+=1):(p+=2)))
+#define scan_esc(conf,p)       (((IS_EOF((conf),(p)[1]))?((p)+1):((p)+2)))
 
 static CONF *def_create(CONF_METHOD *meth);
 static int def_init_default(CONF *conf);
 static int def_init_WIN32(CONF *conf);
 static int def_destroy(CONF *conf);
 static int def_destroy_data(CONF *conf);
-static int def_load(CONF *conf, BIO *bp, long *eline);
-static int def_dump(CONF *conf, BIO *bp);
-static int def_is_number(CONF *conf, char c);
-static int def_to_int(CONF *conf, char c);
+static int def_load(CONF *conf, const char *name, long *eline);
+static int def_load_bio(CONF *conf, BIO *bp, long *eline);
+static int def_dump(const CONF *conf, BIO *bp);
+static int def_is_number(const CONF *conf, char c);
+static int def_to_int(const CONF *conf, char c);
 
 const char *CONF_def_version="CONF_def" OPENSSL_VERSION_PTEXT;
 
@@ -93,10 +96,11 @@ static CONF_METHOD default_method = {
        def_init_default,
        def_destroy,
        def_destroy_data,
-       def_load,
+       def_load_bio,
        def_dump,
        def_is_number,
-       def_to_int
+       def_to_int,
+       def_load
        };
 
 static CONF_METHOD WIN32_method = {
@@ -105,10 +109,11 @@ static CONF_METHOD WIN32_method = {
        def_init_WIN32,
        def_destroy,
        def_destroy_data,
-       def_load,
+       def_load_bio,
        def_dump,
        def_is_number,
-       def_to_int
+       def_to_int,
+       def_load
        };
 
 CONF_METHOD *NCONF_default()
@@ -176,15 +181,41 @@ static int def_destroy_data(CONF *conf)
        return 1;
        }
 
-static int def_load(CONF *conf, BIO *in, long *line)
+static int def_load(CONF *conf, const char *name, long *line)
        {
-#define BUFSIZE        512
-       char btmp[16];
+       int ret;
+       BIO *in=NULL;
+
+#ifdef OPENSSL_SYS_VMS
+       in=BIO_new_file(name, "r");
+#else
+       in=BIO_new_file(name, "rb");
+#endif
+       if (in == NULL)
+               {
+               if (ERR_GET_REASON(ERR_peek_last_error()) == BIO_R_NO_SUCH_FILE)
+                       CONFerr(CONF_F_DEF_LOAD,CONF_R_NO_SUCH_FILE);
+               else
+                       CONFerr(CONF_F_DEF_LOAD,ERR_R_SYS_LIB);
+               return 0;
+               }
+
+       ret = def_load_bio(conf, in, line);
+       BIO_free(in);
+
+       return ret;
+       }
+
+static int def_load_bio(CONF *conf, BIO *in, long *line)
+       {
+/* The macro BUFSIZE conflicts with a system macro in VxWorks */
+#define CONFBUFSIZE    512
        int bufnum=0,i,ii;
        BUF_MEM *buff=NULL;
        char *s,*p,*end;
        int again,n;
        long eline=0;
+       char btmp[DECIMAL_SIZE(eline)+1];
        CONF_VALUE *v=NULL,*tv;
        CONF_VALUE *sv=NULL;
        char *section=NULL,*buf;
@@ -194,48 +225,49 @@ static int def_load(CONF *conf, BIO *in, long *line)
 
        if ((buff=BUF_MEM_new()) == NULL)
                {
-               CONFerr(CONF_F_CONF_LOAD_BIO,ERR_R_BUF_LIB);
+               CONFerr(CONF_F_DEF_LOAD_BIO,ERR_R_BUF_LIB);
                goto err;
                }
 
        section=(char *)OPENSSL_malloc(10);
        if (section == NULL)
                {
-               CONFerr(CONF_F_CONF_LOAD_BIO,ERR_R_MALLOC_FAILURE);
+               CONFerr(CONF_F_DEF_LOAD_BIO,ERR_R_MALLOC_FAILURE);
                goto err;
                }
-       strcpy(section,"default");
+       BUF_strlcpy(section,"default",10);
 
        if (_CONF_new_data(conf) == 0)
                {
-               CONFerr(CONF_F_CONF_LOAD_BIO,ERR_R_MALLOC_FAILURE);
+               CONFerr(CONF_F_DEF_LOAD_BIO,ERR_R_MALLOC_FAILURE);
                goto err;
                }
 
        sv=_CONF_new_section(conf,section);
        if (sv == NULL)
                {
-               CONFerr(CONF_F_CONF_LOAD_BIO,
+               CONFerr(CONF_F_DEF_LOAD_BIO,
                                        CONF_R_UNABLE_TO_CREATE_NEW_SECTION);
                goto err;
                }
        section_sk=(STACK_OF(CONF_VALUE) *)sv->value;
 
        bufnum=0;
+       again=0;
        for (;;)
                {
-               again=0;
-               if (!BUF_MEM_grow(buff,bufnum+BUFSIZE))
+               if (!BUF_MEM_grow(buff,bufnum+CONFBUFSIZE))
                        {
-                       CONFerr(CONF_F_CONF_LOAD_BIO,ERR_R_BUF_LIB);
+                       CONFerr(CONF_F_DEF_LOAD_BIO,ERR_R_BUF_LIB);
                        goto err;
                        }
                p= &(buff->data[bufnum]);
                *p='\0';
-               BIO_gets(in, p, BUFSIZE-1);
-               p[BUFSIZE-1]='\0';
+               BIO_gets(in, p, CONFBUFSIZE-1);
+               p[CONFBUFSIZE-1]='\0';
                ii=i=strlen(p);
-               if (i == 0) break;
+               if (i == 0 && !again) break;
+               again=0;
                while (i > 0)
                        {
                        if ((p[i-1] != '\r') && (p[i-1] != '\n'))
@@ -245,7 +277,7 @@ static int def_load(CONF *conf, BIO *in, long *line)
                        }
                /* we removed some trailing stuff so there is a new
                 * line on the end. */
-               if (i == ii)
+               if (ii && i == ii)
                        again=1; /* long line */
                else
                        {
@@ -297,7 +329,7 @@ again:
                                        ss=p;
                                        goto again;
                                        }
-                               CONFerr(CONF_F_CONF_LOAD_BIO,
+                               CONFerr(CONF_F_DEF_LOAD_BIO,
                                        CONF_R_MISSING_CLOSE_SQUARE_BRACKET);
                                goto err;
                                }
@@ -307,7 +339,7 @@ again:
                                sv=_CONF_new_section(conf,section);
                        if (sv == NULL)
                                {
-                               CONFerr(CONF_F_CONF_LOAD_BIO,
+                               CONFerr(CONF_F_DEF_LOAD_BIO,
                                        CONF_R_UNABLE_TO_CREATE_NEW_SECTION);
                                goto err;
                                }
@@ -330,7 +362,7 @@ again:
                        p=eat_ws(conf, end);
                        if (*p != '=')
                                {
-                               CONFerr(CONF_F_CONF_LOAD_BIO,
+                               CONFerr(CONF_F_DEF_LOAD_BIO,
                                                CONF_R_MISSING_EQUAL_SIGN);
                                goto err;
                                }
@@ -347,7 +379,7 @@ again:
 
                        if (!(v=(CONF_VALUE *)OPENSSL_malloc(sizeof(CONF_VALUE))))
                                {
-                               CONFerr(CONF_F_CONF_LOAD_BIO,
+                               CONFerr(CONF_F_DEF_LOAD_BIO,
                                                        ERR_R_MALLOC_FAILURE);
                                goto err;
                                }
@@ -356,11 +388,11 @@ again:
                        v->value=NULL;
                        if (v->name == NULL)
                                {
-                               CONFerr(CONF_F_CONF_LOAD_BIO,
+                               CONFerr(CONF_F_DEF_LOAD_BIO,
                                                        ERR_R_MALLOC_FAILURE);
                                goto err;
                                }
-                       strcpy(v->name,pname);
+                       BUF_strlcpy(v->name,pname,strlen(pname)+1);
                        if (!str_copy(conf,psection,&(v->value),start)) goto err;
 
                        if (strcmp(psection,section) != 0)
@@ -370,7 +402,7 @@ again:
                                        tv=_CONF_new_section(conf,psection);
                                if (tv == NULL)
                                        {
-                                       CONFerr(CONF_F_CONF_LOAD_BIO,
+                                       CONFerr(CONF_F_DEF_LOAD_BIO,
                                           CONF_R_UNABLE_TO_CREATE_NEW_SECTION);
                                        goto err;
                                        }
@@ -384,7 +416,7 @@ again:
 #if 1
                        if (_CONF_add_string(conf, tv, v) == 0)
                                {
-                               CONFerr(CONF_F_CONF_LOAD_BIO,
+                               CONFerr(CONF_F_DEF_LOAD_BIO,
                                                        ERR_R_MALLOC_FAILURE);
                                goto err;
                                }
@@ -392,7 +424,7 @@ again:
                        v->section=tv->section; 
                        if (!sk_CONF_VALUE_push(ts,v))
                                {
-                               CONFerr(CONF_F_CONF_LOAD_BIO,
+                               CONFerr(CONF_F_DEF_LOAD_BIO,
                                                        ERR_R_MALLOC_FAILURE);
                                goto err;
                                }
@@ -415,9 +447,13 @@ err:
        if (buff != NULL) BUF_MEM_free(buff);
        if (section != NULL) OPENSSL_free(section);
        if (line != NULL) *line=eline;
-       sprintf(btmp,"%ld",eline);
+       BIO_snprintf(btmp,sizeof btmp,"%ld",eline);
        ERR_add_error_data(2,"line ",btmp);
-       if ((h != conf->data) && (conf->data != NULL)) CONF_free(conf->data);
+       if ((h != conf->data) && (conf->data != NULL))
+               {
+               CONF_free(conf->data);
+               conf->data=NULL;
+               }
        if (v != NULL)
                {
                if (v->name != NULL) OPENSSL_free(v->name);
@@ -465,7 +501,7 @@ static void clear_comments(CONF *conf, char *p)
                        }
                if (IS_ESC(conf,*p))
                        {
-                       p=scan_esc(p);
+                       p=scan_esc(conf,p);
                        continue;
                        }
                if (IS_EOF(conf,*p))
@@ -593,9 +629,14 @@ static int str_copy(CONF *conf, char *section, char **pto, char *from)
                                CONFerr(CONF_F_STR_COPY,CONF_R_VARIABLE_HAS_NO_VALUE);
                                goto err;
                                }
-                       BUF_MEM_grow(buf,(strlen(p)+len-(e-from)));
+                       BUF_MEM_grow_clean(buf,(strlen(p)+buf->length-(e-from)));
                        while (*p)
                                buf->data[to++]= *(p++);
+
+                       /* Since we change the pointer 'from', we also have
+                          to change the perceived length of the string it
+                          points at.  /RL */
+                       len -= e-from;
                        from=e;
                        }
                else
@@ -624,7 +665,7 @@ static char *eat_alpha_numeric(CONF *conf, char *p)
                {
                if (IS_ESC(conf,*p))
                        {
-                       p=scan_esc(p);
+                       p=scan_esc(conf,p);
                        continue;
                        }
                if (!IS_ALPHA_NUMERIC_PUNCT(conf,*p))
@@ -684,18 +725,20 @@ static void dump_value(CONF_VALUE *a, BIO *out)
                BIO_printf(out, "[[%s]]\n", a->section);
        }
 
-static int def_dump(CONF *conf, BIO *out)
+static IMPLEMENT_LHASH_DOALL_ARG_FN(dump_value, CONF_VALUE *, BIO *)
+
+static int def_dump(const CONF *conf, BIO *out)
        {
-       lh_doall_arg(conf->data, (void (*)())dump_value, out);
+       lh_doall_arg(conf->data, LHASH_DOALL_ARG_FN(dump_value), out);
        return 1;
        }
 
-static int def_is_number(CONF *conf, char c)
+static int def_is_number(const CONF *conf, char c)
        {
        return IS_NUMBER(conf,c);
        }
 
-static int def_to_int(CONF *conf, char c)
+static int def_to_int(const CONF *conf, char c)
        {
        return c - '0';
        }