When build as dynamic engines, the loading functions should be defined
[openssl.git] / crypto / conf / conf_def.c
1 /* crypto/conf/conf.c */
2 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3  * All rights reserved.
4  *
5  * This package is an SSL implementation written
6  * by Eric Young (eay@cryptsoft.com).
7  * The implementation was written so as to conform with Netscapes SSL.
8  * 
9  * This library is free for commercial and non-commercial use as long as
10  * the following conditions are aheared to.  The following conditions
11  * apply to all code found in this distribution, be it the RC4, RSA,
12  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
13  * included with this distribution is covered by the same copyright terms
14  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15  * 
16  * Copyright remains Eric Young's, and as such any Copyright notices in
17  * the code are not to be removed.
18  * If this package is used in a product, Eric Young should be given attribution
19  * as the author of the parts of the library used.
20  * This can be in the form of a textual message at program startup or
21  * in documentation (online or textual) provided with the package.
22  * 
23  * Redistribution and use in source and binary forms, with or without
24  * modification, are permitted provided that the following conditions
25  * are met:
26  * 1. Redistributions of source code must retain the copyright
27  *    notice, this list of conditions and the following disclaimer.
28  * 2. Redistributions in binary form must reproduce the above copyright
29  *    notice, this list of conditions and the following disclaimer in the
30  *    documentation and/or other materials provided with the distribution.
31  * 3. All advertising materials mentioning features or use of this software
32  *    must display the following acknowledgement:
33  *    "This product includes cryptographic software written by
34  *     Eric Young (eay@cryptsoft.com)"
35  *    The word 'cryptographic' can be left out if the rouines from the library
36  *    being used are not cryptographic related :-).
37  * 4. If you include any Windows specific code (or a derivative thereof) from 
38  *    the apps directory (application code) you must include an acknowledgement:
39  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40  * 
41  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51  * SUCH DAMAGE.
52  * 
53  * The licence and distribution terms for any publically available version or
54  * derivative of this code cannot be changed.  i.e. this code cannot simply be
55  * copied and put under another distribution licence
56  * [including the GNU Public Licence.]
57  */
58
59 /* Part of the code in here was originally in conf.c, which is now removed */
60
61 #include <stdio.h>
62 #include <string.h>
63 #include <openssl/stack.h>
64 #include <openssl/lhash.h>
65 #include <openssl/conf.h>
66 #include <openssl/conf_api.h>
67 #include "conf_def.h"
68 #include <openssl/buffer.h>
69 #include <openssl/err.h>
70 #include "cryptlib.h"
71
72 static char *eat_ws(CONF *conf, char *p);
73 static char *eat_alpha_numeric(CONF *conf, char *p);
74 static void clear_comments(CONF *conf, char *p);
75 static int str_copy(CONF *conf,char *section,char **to, char *from);
76 static char *scan_quote(CONF *conf, char *p);
77 static char *scan_dquote(CONF *conf, char *p);
78 #define scan_esc(conf,p)        (((IS_EOF((conf),(p)[1]))?((p)+1):((p)+2)))
79
80 static CONF *def_create(CONF_METHOD *meth);
81 static int def_init_default(CONF *conf);
82 static int def_init_WIN32(CONF *conf);
83 static int def_destroy(CONF *conf);
84 static int def_destroy_data(CONF *conf);
85 static int def_load(CONF *conf, const char *name, long *eline);
86 static int def_load_bio(CONF *conf, BIO *bp, long *eline);
87 static int def_dump(const CONF *conf, BIO *bp);
88 static int def_is_number(const CONF *conf, char c);
89 static int def_to_int(const CONF *conf, char c);
90
91 const char *CONF_def_version="CONF_def" OPENSSL_VERSION_PTEXT;
92
93 static CONF_METHOD default_method = {
94         "OpenSSL default",
95         def_create,
96         def_init_default,
97         def_destroy,
98         def_destroy_data,
99         def_load_bio,
100         def_dump,
101         def_is_number,
102         def_to_int,
103         def_load
104         };
105
106 static CONF_METHOD WIN32_method = {
107         "WIN32",
108         def_create,
109         def_init_WIN32,
110         def_destroy,
111         def_destroy_data,
112         def_load_bio,
113         def_dump,
114         def_is_number,
115         def_to_int,
116         def_load
117         };
118
119 CONF_METHOD *NCONF_default()
120         {
121         return &default_method;
122         }
123 CONF_METHOD *NCONF_WIN32()
124         {
125         return &WIN32_method;
126         }
127
128 static CONF *def_create(CONF_METHOD *meth)
129         {
130         CONF *ret;
131
132         ret = (CONF *)OPENSSL_malloc(sizeof(CONF) + sizeof(unsigned short *));
133         if (ret)
134                 if (meth->init(ret) == 0)
135                         {
136                         OPENSSL_free(ret);
137                         ret = NULL;
138                         }
139         return ret;
140         }
141         
142 static int def_init_default(CONF *conf)
143         {
144         if (conf == NULL)
145                 return 0;
146
147         conf->meth = &default_method;
148         conf->meth_data = (void *)CONF_type_default;
149         conf->data = NULL;
150
151         return 1;
152         }
153
154 static int def_init_WIN32(CONF *conf)
155         {
156         if (conf == NULL)
157                 return 0;
158
159         conf->meth = &WIN32_method;
160         conf->meth_data = (void *)CONF_type_win32;
161         conf->data = NULL;
162
163         return 1;
164         }
165
166 static int def_destroy(CONF *conf)
167         {
168         if (def_destroy_data(conf))
169                 {
170                 OPENSSL_free(conf);
171                 return 1;
172                 }
173         return 0;
174         }
175
176 static int def_destroy_data(CONF *conf)
177         {
178         if (conf == NULL)
179                 return 0;
180         _CONF_free_data(conf);
181         return 1;
182         }
183
184 static int def_load(CONF *conf, const char *name, long *line)
185         {
186         int ret;
187         BIO *in=NULL;
188
189 #ifdef OPENSSL_SYS_VMS
190         in=BIO_new_file(name, "r");
191 #else
192         in=BIO_new_file(name, "rb");
193 #endif
194         if (in == NULL)
195                 {
196                 if (ERR_GET_REASON(ERR_peek_last_error()) == BIO_R_NO_SUCH_FILE)
197                         CONFerr(CONF_F_CONF_LOAD,CONF_R_NO_SUCH_FILE);
198                 else
199                         CONFerr(CONF_F_CONF_LOAD,ERR_R_SYS_LIB);
200                 return 0;
201                 }
202
203         ret = def_load_bio(conf, in, line);
204         BIO_free(in);
205
206         return ret;
207         }
208
209 static int def_load_bio(CONF *conf, BIO *in, long *line)
210         {
211 /* The macro BUFSIZE conflicts with a system macro in VxWorks */
212 #define CONFBUFSIZE     512
213         int bufnum=0,i,ii;
214         BUF_MEM *buff=NULL;
215         char *s,*p,*end;
216         int again,n;
217         long eline=0;
218         char btmp[DECIMAL_SIZE(eline)+1];
219         CONF_VALUE *v=NULL,*tv;
220         CONF_VALUE *sv=NULL;
221         char *section=NULL,*buf;
222         STACK_OF(CONF_VALUE) *section_sk=NULL,*ts;
223         char *start,*psection,*pname;
224         void *h = (void *)(conf->data);
225
226         if ((buff=BUF_MEM_new()) == NULL)
227                 {
228                 CONFerr(CONF_F_CONF_LOAD_BIO,ERR_R_BUF_LIB);
229                 goto err;
230                 }
231
232         section=(char *)OPENSSL_malloc(10);
233         if (section == NULL)
234                 {
235                 CONFerr(CONF_F_CONF_LOAD_BIO,ERR_R_MALLOC_FAILURE);
236                 goto err;
237                 }
238         strcpy(section,"default");
239
240         if (_CONF_new_data(conf) == 0)
241                 {
242                 CONFerr(CONF_F_CONF_LOAD_BIO,ERR_R_MALLOC_FAILURE);
243                 goto err;
244                 }
245
246         sv=_CONF_new_section(conf,section);
247         if (sv == NULL)
248                 {
249                 CONFerr(CONF_F_CONF_LOAD_BIO,
250                                         CONF_R_UNABLE_TO_CREATE_NEW_SECTION);
251                 goto err;
252                 }
253         section_sk=(STACK_OF(CONF_VALUE) *)sv->value;
254
255         bufnum=0;
256         for (;;)
257                 {
258                 again=0;
259                 if (!BUF_MEM_grow(buff,bufnum+CONFBUFSIZE))
260                         {
261                         CONFerr(CONF_F_CONF_LOAD_BIO,ERR_R_BUF_LIB);
262                         goto err;
263                         }
264                 p= &(buff->data[bufnum]);
265                 *p='\0';
266                 BIO_gets(in, p, CONFBUFSIZE-1);
267                 p[CONFBUFSIZE-1]='\0';
268                 ii=i=strlen(p);
269                 if (i == 0) break;
270                 while (i > 0)
271                         {
272                         if ((p[i-1] != '\r') && (p[i-1] != '\n'))
273                                 break;
274                         else
275                                 i--;
276                         }
277                 /* we removed some trailing stuff so there is a new
278                  * line on the end. */
279                 if (i == ii)
280                         again=1; /* long line */
281                 else
282                         {
283                         p[i]='\0';
284                         eline++; /* another input line */
285                         }
286
287                 /* we now have a line with trailing \r\n removed */
288
289                 /* i is the number of bytes */
290                 bufnum+=i;
291
292                 v=NULL;
293                 /* check for line continuation */
294                 if (bufnum >= 1)
295                         {
296                         /* If we have bytes and the last char '\\' and
297                          * second last char is not '\\' */
298                         p= &(buff->data[bufnum-1]);
299                         if (IS_ESC(conf,p[0]) &&
300                                 ((bufnum <= 1) || !IS_ESC(conf,p[-1])))
301                                 {
302                                 bufnum--;
303                                 again=1;
304                                 }
305                         }
306                 if (again) continue;
307                 bufnum=0;
308                 buf=buff->data;
309
310                 clear_comments(conf, buf);
311                 n=strlen(buf);
312                 s=eat_ws(conf, buf);
313                 if (IS_EOF(conf,*s)) continue; /* blank line */
314                 if (*s == '[')
315                         {
316                         char *ss;
317
318                         s++;
319                         start=eat_ws(conf, s);
320                         ss=start;
321 again:
322                         end=eat_alpha_numeric(conf, ss);
323                         p=eat_ws(conf, end);
324                         if (*p != ']')
325                                 {
326                                 if (*p != '\0')
327                                         {
328                                         ss=p;
329                                         goto again;
330                                         }
331                                 CONFerr(CONF_F_CONF_LOAD_BIO,
332                                         CONF_R_MISSING_CLOSE_SQUARE_BRACKET);
333                                 goto err;
334                                 }
335                         *end='\0';
336                         if (!str_copy(conf,NULL,&section,start)) goto err;
337                         if ((sv=_CONF_get_section(conf,section)) == NULL)
338                                 sv=_CONF_new_section(conf,section);
339                         if (sv == NULL)
340                                 {
341                                 CONFerr(CONF_F_CONF_LOAD_BIO,
342                                         CONF_R_UNABLE_TO_CREATE_NEW_SECTION);
343                                 goto err;
344                                 }
345                         section_sk=(STACK_OF(CONF_VALUE) *)sv->value;
346                         continue;
347                         }
348                 else
349                         {
350                         pname=s;
351                         psection=NULL;
352                         end=eat_alpha_numeric(conf, s);
353                         if ((end[0] == ':') && (end[1] == ':'))
354                                 {
355                                 *end='\0';
356                                 end+=2;
357                                 psection=pname;
358                                 pname=end;
359                                 end=eat_alpha_numeric(conf, end);
360                                 }
361                         p=eat_ws(conf, end);
362                         if (*p != '=')
363                                 {
364                                 CONFerr(CONF_F_CONF_LOAD_BIO,
365                                                 CONF_R_MISSING_EQUAL_SIGN);
366                                 goto err;
367                                 }
368                         *end='\0';
369                         p++;
370                         start=eat_ws(conf, p);
371                         while (!IS_EOF(conf,*p))
372                                 p++;
373                         p--;
374                         while ((p != start) && (IS_WS(conf,*p)))
375                                 p--;
376                         p++;
377                         *p='\0';
378
379                         if (!(v=(CONF_VALUE *)OPENSSL_malloc(sizeof(CONF_VALUE))))
380                                 {
381                                 CONFerr(CONF_F_CONF_LOAD_BIO,
382                                                         ERR_R_MALLOC_FAILURE);
383                                 goto err;
384                                 }
385                         if (psection == NULL) psection=section;
386                         v->name=(char *)OPENSSL_malloc(strlen(pname)+1);
387                         v->value=NULL;
388                         if (v->name == NULL)
389                                 {
390                                 CONFerr(CONF_F_CONF_LOAD_BIO,
391                                                         ERR_R_MALLOC_FAILURE);
392                                 goto err;
393                                 }
394                         strcpy(v->name,pname);
395                         if (!str_copy(conf,psection,&(v->value),start)) goto err;
396
397                         if (strcmp(psection,section) != 0)
398                                 {
399                                 if ((tv=_CONF_get_section(conf,psection))
400                                         == NULL)
401                                         tv=_CONF_new_section(conf,psection);
402                                 if (tv == NULL)
403                                         {
404                                         CONFerr(CONF_F_CONF_LOAD_BIO,
405                                            CONF_R_UNABLE_TO_CREATE_NEW_SECTION);
406                                         goto err;
407                                         }
408                                 ts=(STACK_OF(CONF_VALUE) *)tv->value;
409                                 }
410                         else
411                                 {
412                                 tv=sv;
413                                 ts=section_sk;
414                                 }
415 #if 1
416                         if (_CONF_add_string(conf, tv, v) == 0)
417                                 {
418                                 CONFerr(CONF_F_CONF_LOAD_BIO,
419                                                         ERR_R_MALLOC_FAILURE);
420                                 goto err;
421                                 }
422 #else
423                         v->section=tv->section; 
424                         if (!sk_CONF_VALUE_push(ts,v))
425                                 {
426                                 CONFerr(CONF_F_CONF_LOAD_BIO,
427                                                         ERR_R_MALLOC_FAILURE);
428                                 goto err;
429                                 }
430                         vv=(CONF_VALUE *)lh_insert(conf->data,v);
431                         if (vv != NULL)
432                                 {
433                                 sk_CONF_VALUE_delete_ptr(ts,vv);
434                                 OPENSSL_free(vv->name);
435                                 OPENSSL_free(vv->value);
436                                 OPENSSL_free(vv);
437                                 }
438 #endif
439                         v=NULL;
440                         }
441                 }
442         if (buff != NULL) BUF_MEM_free(buff);
443         if (section != NULL) OPENSSL_free(section);
444         return(1);
445 err:
446         if (buff != NULL) BUF_MEM_free(buff);
447         if (section != NULL) OPENSSL_free(section);
448         if (line != NULL) *line=eline;
449         sprintf(btmp,"%ld",eline);
450         ERR_add_error_data(2,"line ",btmp);
451         if ((h != conf->data) && (conf->data != NULL))
452                 {
453                 CONF_free(conf->data);
454                 conf->data=NULL;
455                 }
456         if (v != NULL)
457                 {
458                 if (v->name != NULL) OPENSSL_free(v->name);
459                 if (v->value != NULL) OPENSSL_free(v->value);
460                 if (v != NULL) OPENSSL_free(v);
461                 }
462         return(0);
463         }
464
465 static void clear_comments(CONF *conf, char *p)
466         {
467         char *to;
468
469         to=p;
470         for (;;)
471                 {
472                 if (IS_FCOMMENT(conf,*p))
473                         {
474                         *p='\0';
475                         return;
476                         }
477                 if (!IS_WS(conf,*p))
478                         {
479                         break;
480                         }
481                 p++;
482                 }
483
484         for (;;)
485                 {
486                 if (IS_COMMENT(conf,*p))
487                         {
488                         *p='\0';
489                         return;
490                         }
491                 if (IS_DQUOTE(conf,*p))
492                         {
493                         p=scan_dquote(conf, p);
494                         continue;
495                         }
496                 if (IS_QUOTE(conf,*p))
497                         {
498                         p=scan_quote(conf, p);
499                         continue;
500                         }
501                 if (IS_ESC(conf,*p))
502                         {
503                         p=scan_esc(conf,p);
504                         continue;
505                         }
506                 if (IS_EOF(conf,*p))
507                         return;
508                 else
509                         p++;
510                 }
511         }
512
513 static int str_copy(CONF *conf, char *section, char **pto, char *from)
514         {
515         int q,r,rr=0,to=0,len=0;
516         char *s,*e,*rp,*p,*rrp,*np,*cp,v;
517         BUF_MEM *buf;
518
519         if ((buf=BUF_MEM_new()) == NULL) return(0);
520
521         len=strlen(from)+1;
522         if (!BUF_MEM_grow(buf,len)) goto err;
523
524         for (;;)
525                 {
526                 if (IS_QUOTE(conf,*from))
527                         {
528                         q= *from;
529                         from++;
530                         while (!IS_EOF(conf,*from) && (*from != q))
531                                 {
532                                 if (IS_ESC(conf,*from))
533                                         {
534                                         from++;
535                                         if (IS_EOF(conf,*from)) break;
536                                         }
537                                 buf->data[to++]= *(from++);
538                                 }
539                         if (*from == q) from++;
540                         }
541                 else if (IS_DQUOTE(conf,*from))
542                         {
543                         q= *from;
544                         from++;
545                         while (!IS_EOF(conf,*from))
546                                 {
547                                 if (*from == q)
548                                         {
549                                         if (*(from+1) == q)
550                                                 {
551                                                 from++;
552                                                 }
553                                         else
554                                                 {
555                                                 break;
556                                                 }
557                                         }
558                                 buf->data[to++]= *(from++);
559                                 }
560                         if (*from == q) from++;
561                         }
562                 else if (IS_ESC(conf,*from))
563                         {
564                         from++;
565                         v= *(from++);
566                         if (IS_EOF(conf,v)) break;
567                         else if (v == 'r') v='\r';
568                         else if (v == 'n') v='\n';
569                         else if (v == 'b') v='\b';
570                         else if (v == 't') v='\t';
571                         buf->data[to++]= v;
572                         }
573                 else if (IS_EOF(conf,*from))
574                         break;
575                 else if (*from == '$')
576                         {
577                         /* try to expand it */
578                         rrp=NULL;
579                         s= &(from[1]);
580                         if (*s == '{')
581                                 q='}';
582                         else if (*s == '(')
583                                 q=')';
584                         else q=0;
585
586                         if (q) s++;
587                         cp=section;
588                         e=np=s;
589                         while (IS_ALPHA_NUMERIC(conf,*e))
590                                 e++;
591                         if ((e[0] == ':') && (e[1] == ':'))
592                                 {
593                                 cp=np;
594                                 rrp=e;
595                                 rr= *e;
596                                 *rrp='\0';
597                                 e+=2;
598                                 np=e;
599                                 while (IS_ALPHA_NUMERIC(conf,*e))
600                                         e++;
601                                 }
602                         r= *e;
603                         *e='\0';
604                         rp=e;
605                         if (q)
606                                 {
607                                 if (r != q)
608                                         {
609                                         CONFerr(CONF_F_STR_COPY,CONF_R_NO_CLOSE_BRACE);
610                                         goto err;
611                                         }
612                                 e++;
613                                 }
614                         /* So at this point we have
615                          * ns which is the start of the name string which is
616                          *   '\0' terminated. 
617                          * cs which is the start of the section string which is
618                          *   '\0' terminated.
619                          * e is the 'next point after'.
620                          * r and s are the chars replaced by the '\0'
621                          * rp and sp is where 'r' and 's' came from.
622                          */
623                         p=_CONF_get_string(conf,cp,np);
624                         if (rrp != NULL) *rrp=rr;
625                         *rp=r;
626                         if (p == NULL)
627                                 {
628                                 CONFerr(CONF_F_STR_COPY,CONF_R_VARIABLE_HAS_NO_VALUE);
629                                 goto err;
630                                 }
631                         BUF_MEM_grow(buf,(strlen(p)+len-(e-from)));
632                         while (*p)
633                                 buf->data[to++]= *(p++);
634                         from=e;
635                         }
636                 else
637                         buf->data[to++]= *(from++);
638                 }
639         buf->data[to]='\0';
640         if (*pto != NULL) OPENSSL_free(*pto);
641         *pto=buf->data;
642         OPENSSL_free(buf);
643         return(1);
644 err:
645         if (buf != NULL) BUF_MEM_free(buf);
646         return(0);
647         }
648
649 static char *eat_ws(CONF *conf, char *p)
650         {
651         while (IS_WS(conf,*p) && (!IS_EOF(conf,*p)))
652                 p++;
653         return(p);
654         }
655
656 static char *eat_alpha_numeric(CONF *conf, char *p)
657         {
658         for (;;)
659                 {
660                 if (IS_ESC(conf,*p))
661                         {
662                         p=scan_esc(conf,p);
663                         continue;
664                         }
665                 if (!IS_ALPHA_NUMERIC_PUNCT(conf,*p))
666                         return(p);
667                 p++;
668                 }
669         }
670
671 static char *scan_quote(CONF *conf, char *p)
672         {
673         int q= *p;
674
675         p++;
676         while (!(IS_EOF(conf,*p)) && (*p != q))
677                 {
678                 if (IS_ESC(conf,*p))
679                         {
680                         p++;
681                         if (IS_EOF(conf,*p)) return(p);
682                         }
683                 p++;
684                 }
685         if (*p == q) p++;
686         return(p);
687         }
688
689
690 static char *scan_dquote(CONF *conf, char *p)
691         {
692         int q= *p;
693
694         p++;
695         while (!(IS_EOF(conf,*p)))
696                 {
697                 if (*p == q)
698                         {
699                         if (*(p+1) == q)
700                                 {
701                                 p++;
702                                 }
703                         else
704                                 {
705                                 break;
706                                 }
707                         }
708                 p++;
709                 }
710         if (*p == q) p++;
711         return(p);
712         }
713
714 static void dump_value(CONF_VALUE *a, BIO *out)
715         {
716         if (a->name)
717                 BIO_printf(out, "[%s] %s=%s\n", a->section, a->name, a->value);
718         else
719                 BIO_printf(out, "[[%s]]\n", a->section);
720         }
721
722 static IMPLEMENT_LHASH_DOALL_ARG_FN(dump_value, CONF_VALUE *, BIO *)
723
724 static int def_dump(const CONF *conf, BIO *out)
725         {
726         lh_doall_arg(conf->data, LHASH_DOALL_ARG_FN(dump_value), out);
727         return 1;
728         }
729
730 static int def_is_number(const CONF *conf, char c)
731         {
732         return IS_NUMBER(conf,c);
733         }
734
735 static int def_to_int(const CONF *conf, char c)
736         {
737         return c - '0';
738         }
739