New functions
[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
71 static char *eat_ws(CONF *conf, char *p);
72 static char *eat_alpha_numeric(CONF *conf, char *p);
73 static void clear_comments(CONF *conf, char *p);
74 static int str_copy(CONF *conf,char *section,char **to, char *from);
75 static char *scan_quote(CONF *conf, char *p);
76 static char *scan_dquote(CONF *conf, char *p);
77 #define scan_esc(conf,p)        (((IS_EOF((conf),(p)[1]))?((p)+1):((p)+2)))
78
79 static CONF *def_create(CONF_METHOD *meth);
80 static int def_init_default(CONF *conf);
81 static int def_init_WIN32(CONF *conf);
82 static int def_destroy(CONF *conf);
83 static int def_destroy_data(CONF *conf);
84 static int def_load(CONF *conf, const char *name, long *eline);
85 static int def_load_bio(CONF *conf, BIO *bp, long *eline);
86 static int def_dump(const CONF *conf, BIO *bp);
87 static int def_is_number(const CONF *conf, char c);
88 static int def_to_int(const CONF *conf, char c);
89
90 const char *CONF_def_version="CONF_def" OPENSSL_VERSION_PTEXT;
91
92 static CONF_METHOD default_method = {
93         "OpenSSL default",
94         def_create,
95         def_init_default,
96         def_destroy,
97         def_destroy_data,
98         def_load_bio,
99         def_dump,
100         def_is_number,
101         def_to_int,
102         def_load
103         };
104
105 static CONF_METHOD WIN32_method = {
106         "WIN32",
107         def_create,
108         def_init_WIN32,
109         def_destroy,
110         def_destroy_data,
111         def_load_bio,
112         def_dump,
113         def_is_number,
114         def_to_int,
115         def_load
116         };
117
118 CONF_METHOD *NCONF_default()
119         {
120         return &default_method;
121         }
122 CONF_METHOD *NCONF_WIN32()
123         {
124         return &WIN32_method;
125         }
126
127 static CONF *def_create(CONF_METHOD *meth)
128         {
129         CONF *ret;
130
131         ret = (CONF *)OPENSSL_malloc(sizeof(CONF) + sizeof(unsigned short *));
132         if (ret)
133                 if (meth->init(ret) == 0)
134                         {
135                         OPENSSL_free(ret);
136                         ret = NULL;
137                         }
138         return ret;
139         }
140         
141 static int def_init_default(CONF *conf)
142         {
143         if (conf == NULL)
144                 return 0;
145
146         conf->meth = &default_method;
147         conf->meth_data = (void *)CONF_type_default;
148         conf->data = NULL;
149
150         return 1;
151         }
152
153 static int def_init_WIN32(CONF *conf)
154         {
155         if (conf == NULL)
156                 return 0;
157
158         conf->meth = &WIN32_method;
159         conf->meth_data = (void *)CONF_type_win32;
160         conf->data = NULL;
161
162         return 1;
163         }
164
165 static int def_destroy(CONF *conf)
166         {
167         if (def_destroy_data(conf))
168                 {
169                 OPENSSL_free(conf);
170                 return 1;
171                 }
172         return 0;
173         }
174
175 static int def_destroy_data(CONF *conf)
176         {
177         if (conf == NULL)
178                 return 0;
179         _CONF_free_data(conf);
180         return 1;
181         }
182
183 static int def_load(CONF *conf, const char *name, long *line)
184         {
185         int ret;
186         BIO *in=NULL;
187
188 #ifdef OPENSSL_SYS_VMS
189         in=BIO_new_file(name, "r");
190 #else
191         in=BIO_new_file(name, "rb");
192 #endif
193         if (in == NULL)
194                 {
195                 if (ERR_GET_REASON(ERR_peek_last_error()) == BIO_R_NO_SUCH_FILE)
196                         CONFerr(CONF_F_CONF_LOAD,CONF_R_NO_SUCH_FILE);
197                 else
198                         CONFerr(CONF_F_CONF_LOAD,ERR_R_SYS_LIB);
199                 return 0;
200                 }
201
202         ret = def_load_bio(conf, in, line);
203         BIO_free(in);
204
205         return ret;
206         }
207
208 static int def_load_bio(CONF *conf, BIO *in, long *line)
209         {
210 #define BUFSIZE 512
211         char btmp[16];
212         int bufnum=0,i,ii;
213         BUF_MEM *buff=NULL;
214         char *s,*p,*end;
215         int again,n;
216         long eline=0;
217         CONF_VALUE *v=NULL,*tv;
218         CONF_VALUE *sv=NULL;
219         char *section=NULL,*buf;
220         STACK_OF(CONF_VALUE) *section_sk=NULL,*ts;
221         char *start,*psection,*pname;
222         void *h = (void *)(conf->data);
223
224         if ((buff=BUF_MEM_new()) == NULL)
225                 {
226                 CONFerr(CONF_F_CONF_LOAD_BIO,ERR_R_BUF_LIB);
227                 goto err;
228                 }
229
230         section=(char *)OPENSSL_malloc(10);
231         if (section == NULL)
232                 {
233                 CONFerr(CONF_F_CONF_LOAD_BIO,ERR_R_MALLOC_FAILURE);
234                 goto err;
235                 }
236         strcpy(section,"default");
237
238         if (_CONF_new_data(conf) == 0)
239                 {
240                 CONFerr(CONF_F_CONF_LOAD_BIO,ERR_R_MALLOC_FAILURE);
241                 goto err;
242                 }
243
244         sv=_CONF_new_section(conf,section);
245         if (sv == NULL)
246                 {
247                 CONFerr(CONF_F_CONF_LOAD_BIO,
248                                         CONF_R_UNABLE_TO_CREATE_NEW_SECTION);
249                 goto err;
250                 }
251         section_sk=(STACK_OF(CONF_VALUE) *)sv->value;
252
253         bufnum=0;
254         for (;;)
255                 {
256                 again=0;
257                 if (!BUF_MEM_grow(buff,bufnum+BUFSIZE))
258                         {
259                         CONFerr(CONF_F_CONF_LOAD_BIO,ERR_R_BUF_LIB);
260                         goto err;
261                         }
262                 p= &(buff->data[bufnum]);
263                 *p='\0';
264                 BIO_gets(in, p, BUFSIZE-1);
265                 p[BUFSIZE-1]='\0';
266                 ii=i=strlen(p);
267                 if (i == 0) break;
268                 while (i > 0)
269                         {
270                         if ((p[i-1] != '\r') && (p[i-1] != '\n'))
271                                 break;
272                         else
273                                 i--;
274                         }
275                 /* we removed some trailing stuff so there is a new
276                  * line on the end. */
277                 if (i == ii)
278                         again=1; /* long line */
279                 else
280                         {
281                         p[i]='\0';
282                         eline++; /* another input line */
283                         }
284
285                 /* we now have a line with trailing \r\n removed */
286
287                 /* i is the number of bytes */
288                 bufnum+=i;
289
290                 v=NULL;
291                 /* check for line continuation */
292                 if (bufnum >= 1)
293                         {
294                         /* If we have bytes and the last char '\\' and
295                          * second last char is not '\\' */
296                         p= &(buff->data[bufnum-1]);
297                         if (IS_ESC(conf,p[0]) &&
298                                 ((bufnum <= 1) || !IS_ESC(conf,p[-1])))
299                                 {
300                                 bufnum--;
301                                 again=1;
302                                 }
303                         }
304                 if (again) continue;
305                 bufnum=0;
306                 buf=buff->data;
307
308                 clear_comments(conf, buf);
309                 n=strlen(buf);
310                 s=eat_ws(conf, buf);
311                 if (IS_EOF(conf,*s)) continue; /* blank line */
312                 if (*s == '[')
313                         {
314                         char *ss;
315
316                         s++;
317                         start=eat_ws(conf, s);
318                         ss=start;
319 again:
320                         end=eat_alpha_numeric(conf, ss);
321                         p=eat_ws(conf, end);
322                         if (*p != ']')
323                                 {
324                                 if (*p != '\0')
325                                         {
326                                         ss=p;
327                                         goto again;
328                                         }
329                                 CONFerr(CONF_F_CONF_LOAD_BIO,
330                                         CONF_R_MISSING_CLOSE_SQUARE_BRACKET);
331                                 goto err;
332                                 }
333                         *end='\0';
334                         if (!str_copy(conf,NULL,&section,start)) goto err;
335                         if ((sv=_CONF_get_section(conf,section)) == NULL)
336                                 sv=_CONF_new_section(conf,section);
337                         if (sv == NULL)
338                                 {
339                                 CONFerr(CONF_F_CONF_LOAD_BIO,
340                                         CONF_R_UNABLE_TO_CREATE_NEW_SECTION);
341                                 goto err;
342                                 }
343                         section_sk=(STACK_OF(CONF_VALUE) *)sv->value;
344                         continue;
345                         }
346                 else
347                         {
348                         pname=s;
349                         psection=NULL;
350                         end=eat_alpha_numeric(conf, s);
351                         if ((end[0] == ':') && (end[1] == ':'))
352                                 {
353                                 *end='\0';
354                                 end+=2;
355                                 psection=pname;
356                                 pname=end;
357                                 end=eat_alpha_numeric(conf, end);
358                                 }
359                         p=eat_ws(conf, end);
360                         if (*p != '=')
361                                 {
362                                 CONFerr(CONF_F_CONF_LOAD_BIO,
363                                                 CONF_R_MISSING_EQUAL_SIGN);
364                                 goto err;
365                                 }
366                         *end='\0';
367                         p++;
368                         start=eat_ws(conf, p);
369                         while (!IS_EOF(conf,*p))
370                                 p++;
371                         p--;
372                         while ((p != start) && (IS_WS(conf,*p)))
373                                 p--;
374                         p++;
375                         *p='\0';
376
377                         if (!(v=(CONF_VALUE *)OPENSSL_malloc(sizeof(CONF_VALUE))))
378                                 {
379                                 CONFerr(CONF_F_CONF_LOAD_BIO,
380                                                         ERR_R_MALLOC_FAILURE);
381                                 goto err;
382                                 }
383                         if (psection == NULL) psection=section;
384                         v->name=(char *)OPENSSL_malloc(strlen(pname)+1);
385                         v->value=NULL;
386                         if (v->name == NULL)
387                                 {
388                                 CONFerr(CONF_F_CONF_LOAD_BIO,
389                                                         ERR_R_MALLOC_FAILURE);
390                                 goto err;
391                                 }
392                         strcpy(v->name,pname);
393                         if (!str_copy(conf,psection,&(v->value),start)) goto err;
394
395                         if (strcmp(psection,section) != 0)
396                                 {
397                                 if ((tv=_CONF_get_section(conf,psection))
398                                         == NULL)
399                                         tv=_CONF_new_section(conf,psection);
400                                 if (tv == NULL)
401                                         {
402                                         CONFerr(CONF_F_CONF_LOAD_BIO,
403                                            CONF_R_UNABLE_TO_CREATE_NEW_SECTION);
404                                         goto err;
405                                         }
406                                 ts=(STACK_OF(CONF_VALUE) *)tv->value;
407                                 }
408                         else
409                                 {
410                                 tv=sv;
411                                 ts=section_sk;
412                                 }
413 #if 1
414                         if (_CONF_add_string(conf, tv, v) == 0)
415                                 {
416                                 CONFerr(CONF_F_CONF_LOAD_BIO,
417                                                         ERR_R_MALLOC_FAILURE);
418                                 goto err;
419                                 }
420 #else
421                         v->section=tv->section; 
422                         if (!sk_CONF_VALUE_push(ts,v))
423                                 {
424                                 CONFerr(CONF_F_CONF_LOAD_BIO,
425                                                         ERR_R_MALLOC_FAILURE);
426                                 goto err;
427                                 }
428                         vv=(CONF_VALUE *)lh_insert(conf->data,v);
429                         if (vv != NULL)
430                                 {
431                                 sk_CONF_VALUE_delete_ptr(ts,vv);
432                                 OPENSSL_free(vv->name);
433                                 OPENSSL_free(vv->value);
434                                 OPENSSL_free(vv);
435                                 }
436 #endif
437                         v=NULL;
438                         }
439                 }
440         if (buff != NULL) BUF_MEM_free(buff);
441         if (section != NULL) OPENSSL_free(section);
442         return(1);
443 err:
444         if (buff != NULL) BUF_MEM_free(buff);
445         if (section != NULL) OPENSSL_free(section);
446         if (line != NULL) *line=eline;
447         sprintf(btmp,"%ld",eline);
448         ERR_add_error_data(2,"line ",btmp);
449         if ((h != conf->data) && (conf->data != NULL))
450                 {
451                 CONF_free(conf->data);
452                 conf->data=NULL;
453                 }
454         if (v != NULL)
455                 {
456                 if (v->name != NULL) OPENSSL_free(v->name);
457                 if (v->value != NULL) OPENSSL_free(v->value);
458                 if (v != NULL) OPENSSL_free(v);
459                 }
460         return(0);
461         }
462
463 static void clear_comments(CONF *conf, char *p)
464         {
465         char *to;
466
467         to=p;
468         for (;;)
469                 {
470                 if (IS_FCOMMENT(conf,*p))
471                         {
472                         *p='\0';
473                         return;
474                         }
475                 if (!IS_WS(conf,*p))
476                         {
477                         break;
478                         }
479                 p++;
480                 }
481
482         for (;;)
483                 {
484                 if (IS_COMMENT(conf,*p))
485                         {
486                         *p='\0';
487                         return;
488                         }
489                 if (IS_DQUOTE(conf,*p))
490                         {
491                         p=scan_dquote(conf, p);
492                         continue;
493                         }
494                 if (IS_QUOTE(conf,*p))
495                         {
496                         p=scan_quote(conf, p);
497                         continue;
498                         }
499                 if (IS_ESC(conf,*p))
500                         {
501                         p=scan_esc(conf,p);
502                         continue;
503                         }
504                 if (IS_EOF(conf,*p))
505                         return;
506                 else
507                         p++;
508                 }
509         }
510
511 static int str_copy(CONF *conf, char *section, char **pto, char *from)
512         {
513         int q,r,rr=0,to=0,len=0;
514         char *s,*e,*rp,*p,*rrp,*np,*cp,v;
515         BUF_MEM *buf;
516
517         if ((buf=BUF_MEM_new()) == NULL) return(0);
518
519         len=strlen(from)+1;
520         if (!BUF_MEM_grow(buf,len)) goto err;
521
522         for (;;)
523                 {
524                 if (IS_QUOTE(conf,*from))
525                         {
526                         q= *from;
527                         from++;
528                         while (!IS_EOF(conf,*from) && (*from != q))
529                                 {
530                                 if (IS_ESC(conf,*from))
531                                         {
532                                         from++;
533                                         if (IS_EOF(conf,*from)) break;
534                                         }
535                                 buf->data[to++]= *(from++);
536                                 }
537                         if (*from == q) from++;
538                         }
539                 else if (IS_DQUOTE(conf,*from))
540                         {
541                         q= *from;
542                         from++;
543                         while (!IS_EOF(conf,*from))
544                                 {
545                                 if (*from == q)
546                                         {
547                                         if (*(from+1) == q)
548                                                 {
549                                                 from++;
550                                                 }
551                                         else
552                                                 {
553                                                 break;
554                                                 }
555                                         }
556                                 buf->data[to++]= *(from++);
557                                 }
558                         if (*from == q) from++;
559                         }
560                 else if (IS_ESC(conf,*from))
561                         {
562                         from++;
563                         v= *(from++);
564                         if (IS_EOF(conf,v)) break;
565                         else if (v == 'r') v='\r';
566                         else if (v == 'n') v='\n';
567                         else if (v == 'b') v='\b';
568                         else if (v == 't') v='\t';
569                         buf->data[to++]= v;
570                         }
571                 else if (IS_EOF(conf,*from))
572                         break;
573                 else if (*from == '$')
574                         {
575                         /* try to expand it */
576                         rrp=NULL;
577                         s= &(from[1]);
578                         if (*s == '{')
579                                 q='}';
580                         else if (*s == '(')
581                                 q=')';
582                         else q=0;
583
584                         if (q) s++;
585                         cp=section;
586                         e=np=s;
587                         while (IS_ALPHA_NUMERIC(conf,*e))
588                                 e++;
589                         if ((e[0] == ':') && (e[1] == ':'))
590                                 {
591                                 cp=np;
592                                 rrp=e;
593                                 rr= *e;
594                                 *rrp='\0';
595                                 e+=2;
596                                 np=e;
597                                 while (IS_ALPHA_NUMERIC(conf,*e))
598                                         e++;
599                                 }
600                         r= *e;
601                         *e='\0';
602                         rp=e;
603                         if (q)
604                                 {
605                                 if (r != q)
606                                         {
607                                         CONFerr(CONF_F_STR_COPY,CONF_R_NO_CLOSE_BRACE);
608                                         goto err;
609                                         }
610                                 e++;
611                                 }
612                         /* So at this point we have
613                          * ns which is the start of the name string which is
614                          *   '\0' terminated. 
615                          * cs which is the start of the section string which is
616                          *   '\0' terminated.
617                          * e is the 'next point after'.
618                          * r and s are the chars replaced by the '\0'
619                          * rp and sp is where 'r' and 's' came from.
620                          */
621                         p=_CONF_get_string(conf,cp,np);
622                         if (rrp != NULL) *rrp=rr;
623                         *rp=r;
624                         if (p == NULL)
625                                 {
626                                 CONFerr(CONF_F_STR_COPY,CONF_R_VARIABLE_HAS_NO_VALUE);
627                                 goto err;
628                                 }
629                         BUF_MEM_grow(buf,(strlen(p)+len-(e-from)));
630                         while (*p)
631                                 buf->data[to++]= *(p++);
632                         from=e;
633                         }
634                 else
635                         buf->data[to++]= *(from++);
636                 }
637         buf->data[to]='\0';
638         if (*pto != NULL) OPENSSL_free(*pto);
639         *pto=buf->data;
640         OPENSSL_free(buf);
641         return(1);
642 err:
643         if (buf != NULL) BUF_MEM_free(buf);
644         return(0);
645         }
646
647 static char *eat_ws(CONF *conf, char *p)
648         {
649         while (IS_WS(conf,*p) && (!IS_EOF(conf,*p)))
650                 p++;
651         return(p);
652         }
653
654 static char *eat_alpha_numeric(CONF *conf, char *p)
655         {
656         for (;;)
657                 {
658                 if (IS_ESC(conf,*p))
659                         {
660                         p=scan_esc(conf,p);
661                         continue;
662                         }
663                 if (!IS_ALPHA_NUMERIC_PUNCT(conf,*p))
664                         return(p);
665                 p++;
666                 }
667         }
668
669 static char *scan_quote(CONF *conf, char *p)
670         {
671         int q= *p;
672
673         p++;
674         while (!(IS_EOF(conf,*p)) && (*p != q))
675                 {
676                 if (IS_ESC(conf,*p))
677                         {
678                         p++;
679                         if (IS_EOF(conf,*p)) return(p);
680                         }
681                 p++;
682                 }
683         if (*p == q) p++;
684         return(p);
685         }
686
687
688 static char *scan_dquote(CONF *conf, char *p)
689         {
690         int q= *p;
691
692         p++;
693         while (!(IS_EOF(conf,*p)))
694                 {
695                 if (*p == q)
696                         {
697                         if (*(p+1) == q)
698                                 {
699                                 p++;
700                                 }
701                         else
702                                 {
703                                 break;
704                                 }
705                         }
706                 p++;
707                 }
708         if (*p == q) p++;
709         return(p);
710         }
711
712 static void dump_value(CONF_VALUE *a, BIO *out)
713         {
714         if (a->name)
715                 BIO_printf(out, "[%s] %s=%s\n", a->section, a->name, a->value);
716         else
717                 BIO_printf(out, "[[%s]]\n", a->section);
718         }
719
720 static IMPLEMENT_LHASH_DOALL_ARG_FN(dump_value, CONF_VALUE *, BIO *)
721
722 static int def_dump(const CONF *conf, BIO *out)
723         {
724         lh_doall_arg(conf->data, LHASH_DOALL_ARG_FN(dump_value), out);
725         return 1;
726         }
727
728 static int def_is_number(const CONF *conf, char c)
729         {
730         return IS_NUMBER(conf,c);
731         }
732
733 static int def_to_int(const CONF *conf, char c)
734         {
735         return c - '0';
736         }
737