conf_def.c revision 279265
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 "cryptlib.h"
64#include <openssl/stack.h>
65#include <openssl/lhash.h>
66#include <openssl/conf.h>
67#include <openssl/conf_api.h>
68#include "conf_def.h"
69#include <openssl/buffer.h>
70#include <openssl/err.h>
71
72static char *eat_ws(CONF *conf, char *p);
73static char *eat_alpha_numeric(CONF *conf, char *p);
74static void clear_comments(CONF *conf, char *p);
75static int str_copy(CONF *conf,char *section,char **to, char *from);
76static char *scan_quote(CONF *conf, char *p);
77static char *scan_dquote(CONF *conf, char *p);
78#define scan_esc(conf,p)	(((IS_EOF((conf),(p)[1]))?((p)+1):((p)+2)))
79
80static CONF *def_create(CONF_METHOD *meth);
81static int def_init_default(CONF *conf);
82static int def_init_WIN32(CONF *conf);
83static int def_destroy(CONF *conf);
84static int def_destroy_data(CONF *conf);
85static int def_load(CONF *conf, const char *name, long *eline);
86static int def_load_bio(CONF *conf, BIO *bp, long *eline);
87static int def_dump(const CONF *conf, BIO *bp);
88static int def_is_number(const CONF *conf, char c);
89static int def_to_int(const CONF *conf, char c);
90
91const char CONF_def_version[]="CONF_def" OPENSSL_VERSION_PTEXT;
92
93static 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
106static 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
119CONF_METHOD *NCONF_default()
120	{
121	return &default_method;
122	}
123CONF_METHOD *NCONF_WIN32()
124	{
125	return &WIN32_method;
126	}
127
128static 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
142static 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
154static 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
166static 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
176static int def_destroy_data(CONF *conf)
177	{
178	if (conf == NULL)
179		return 0;
180	_CONF_free_data(conf);
181	return 1;
182	}
183
184static 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_DEF_LOAD,CONF_R_NO_SUCH_FILE);
198		else
199			CONFerr(CONF_F_DEF_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
209static 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;
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;*/
223/*	STACK_OF(CONF_VALUE) *ts=NULL;*/
224	char *start,*psection,*pname;
225	void *h = (void *)(conf->data);
226
227	if ((buff=BUF_MEM_new()) == NULL)
228		{
229		CONFerr(CONF_F_DEF_LOAD_BIO,ERR_R_BUF_LIB);
230		goto err;
231		}
232
233	section=(char *)OPENSSL_malloc(10);
234	if (section == NULL)
235		{
236		CONFerr(CONF_F_DEF_LOAD_BIO,ERR_R_MALLOC_FAILURE);
237		goto err;
238		}
239	BUF_strlcpy(section,"default",10);
240
241	if (_CONF_new_data(conf) == 0)
242		{
243		CONFerr(CONF_F_DEF_LOAD_BIO,ERR_R_MALLOC_FAILURE);
244		goto err;
245		}
246
247	sv=_CONF_new_section(conf,section);
248	if (sv == NULL)
249		{
250		CONFerr(CONF_F_DEF_LOAD_BIO,
251					CONF_R_UNABLE_TO_CREATE_NEW_SECTION);
252		goto err;
253		}
254/*	section_sk=(STACK_OF(CONF_VALUE) *)sv->value;*/
255
256	bufnum=0;
257	again=0;
258	for (;;)
259		{
260		if (!BUF_MEM_grow(buff,bufnum+CONFBUFSIZE))
261			{
262			CONFerr(CONF_F_DEF_LOAD_BIO,ERR_R_BUF_LIB);
263			goto err;
264			}
265		p= &(buff->data[bufnum]);
266		*p='\0';
267		BIO_gets(in, p, CONFBUFSIZE-1);
268		p[CONFBUFSIZE-1]='\0';
269		ii=i=strlen(p);
270		if (i == 0 && !again) break;
271		again=0;
272		while (i > 0)
273			{
274			if ((p[i-1] != '\r') && (p[i-1] != '\n'))
275				break;
276			else
277				i--;
278			}
279		/* we removed some trailing stuff so there is a new
280		 * line on the end. */
281		if (ii && i == ii)
282			again=1; /* long line */
283		else
284			{
285			p[i]='\0';
286			eline++; /* another input line */
287			}
288
289		/* we now have a line with trailing \r\n removed */
290
291		/* i is the number of bytes */
292		bufnum+=i;
293
294		v=NULL;
295		/* check for line continuation */
296		if (bufnum >= 1)
297			{
298			/* If we have bytes and the last char '\\' and
299			 * second last char is not '\\' */
300			p= &(buff->data[bufnum-1]);
301			if (IS_ESC(conf,p[0]) &&
302				((bufnum <= 1) || !IS_ESC(conf,p[-1])))
303				{
304				bufnum--;
305				again=1;
306				}
307			}
308		if (again) continue;
309		bufnum=0;
310		buf=buff->data;
311
312		clear_comments(conf, buf);
313		s=eat_ws(conf, buf);
314		if (IS_EOF(conf,*s)) continue; /* blank line */
315		if (*s == '[')
316			{
317			char *ss;
318
319			s++;
320			start=eat_ws(conf, s);
321			ss=start;
322again:
323			end=eat_alpha_numeric(conf, ss);
324			p=eat_ws(conf, end);
325			if (*p != ']')
326				{
327				if (*p != '\0' && ss != p)
328					{
329					ss=p;
330					goto again;
331					}
332				CONFerr(CONF_F_DEF_LOAD_BIO,
333					CONF_R_MISSING_CLOSE_SQUARE_BRACKET);
334				goto err;
335				}
336			*end='\0';
337			if (!str_copy(conf,NULL,&section,start)) goto err;
338			if ((sv=_CONF_get_section(conf,section)) == NULL)
339				sv=_CONF_new_section(conf,section);
340			if (sv == NULL)
341				{
342				CONFerr(CONF_F_DEF_LOAD_BIO,
343					CONF_R_UNABLE_TO_CREATE_NEW_SECTION);
344				goto err;
345				}
346/*			section_sk=(STACK_OF(CONF_VALUE) *)sv->value;*/
347			continue;
348			}
349		else
350			{
351			pname=s;
352			psection=NULL;
353			end=eat_alpha_numeric(conf, s);
354			if ((end[0] == ':') && (end[1] == ':'))
355				{
356				*end='\0';
357				end+=2;
358				psection=pname;
359				pname=end;
360				end=eat_alpha_numeric(conf, end);
361				}
362			p=eat_ws(conf, end);
363			if (*p != '=')
364				{
365				CONFerr(CONF_F_DEF_LOAD_BIO,
366						CONF_R_MISSING_EQUAL_SIGN);
367				goto err;
368				}
369			*end='\0';
370			p++;
371			start=eat_ws(conf, p);
372			while (!IS_EOF(conf,*p))
373				p++;
374			p--;
375			while ((p != start) && (IS_WS(conf,*p)))
376				p--;
377			p++;
378			*p='\0';
379
380			if (!(v=(CONF_VALUE *)OPENSSL_malloc(sizeof(CONF_VALUE))))
381				{
382				CONFerr(CONF_F_DEF_LOAD_BIO,
383							ERR_R_MALLOC_FAILURE);
384				goto err;
385				}
386			if (psection == NULL) psection=section;
387			v->name=(char *)OPENSSL_malloc(strlen(pname)+1);
388			v->value=NULL;
389			if (v->name == NULL)
390				{
391				CONFerr(CONF_F_DEF_LOAD_BIO,
392							ERR_R_MALLOC_FAILURE);
393				goto err;
394				}
395			BUF_strlcpy(v->name,pname,strlen(pname)+1);
396			if (!str_copy(conf,psection,&(v->value),start)) goto err;
397
398			if (strcmp(psection,section) != 0)
399				{
400				if ((tv=_CONF_get_section(conf,psection))
401					== NULL)
402					tv=_CONF_new_section(conf,psection);
403				if (tv == NULL)
404					{
405					CONFerr(CONF_F_DEF_LOAD_BIO,
406					   CONF_R_UNABLE_TO_CREATE_NEW_SECTION);
407					goto err;
408					}
409/*				ts=(STACK_OF(CONF_VALUE) *)tv->value;*/
410				}
411			else
412				{
413				tv=sv;
414/*				ts=section_sk;*/
415				}
416#if 1
417			if (_CONF_add_string(conf, tv, v) == 0)
418				{
419				CONFerr(CONF_F_DEF_LOAD_BIO,
420							ERR_R_MALLOC_FAILURE);
421				goto err;
422				}
423#else
424			v->section=tv->section;
425			if (!sk_CONF_VALUE_push(ts,v))
426				{
427				CONFerr(CONF_F_DEF_LOAD_BIO,
428							ERR_R_MALLOC_FAILURE);
429				goto err;
430				}
431			vv=(CONF_VALUE *)lh_insert(conf->data,v);
432			if (vv != NULL)
433				{
434				sk_CONF_VALUE_delete_ptr(ts,vv);
435				OPENSSL_free(vv->name);
436				OPENSSL_free(vv->value);
437				OPENSSL_free(vv);
438				}
439#endif
440			v=NULL;
441			}
442		}
443	if (buff != NULL) BUF_MEM_free(buff);
444	if (section != NULL) OPENSSL_free(section);
445	return(1);
446err:
447	if (buff != NULL) BUF_MEM_free(buff);
448	if (section != NULL) OPENSSL_free(section);
449	if (line != NULL) *line=eline;
450	BIO_snprintf(btmp,sizeof btmp,"%ld",eline);
451	ERR_add_error_data(2,"line ",btmp);
452	if ((h != conf->data) && (conf->data != NULL))
453		{
454		CONF_free(conf->data);
455		conf->data=NULL;
456		}
457	if (v != NULL)
458		{
459		if (v->name != NULL) OPENSSL_free(v->name);
460		if (v->value != NULL) OPENSSL_free(v->value);
461		if (v != NULL) OPENSSL_free(v);
462		}
463	return(0);
464	}
465
466static void clear_comments(CONF *conf, char *p)
467	{
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
511static 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			 * np which is the start of the name string which is
614			 *   '\0' terminated.
615			 * cp which is the start of the section string which is
616			 *   '\0' terminated.
617			 * e is the 'next point after'.
618			 * r and rr are the chars replaced by the '\0'
619			 * rp and rrp is where 'r' and 'rr' 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_clean(buf,(strlen(p)+buf->length-(e-from)));
630			while (*p)
631				buf->data[to++]= *(p++);
632
633			/* Since we change the pointer 'from', we also have
634			   to change the perceived length of the string it
635			   points at.  /RL */
636			len -= e-from;
637			from=e;
638
639			/* In case there were no braces or parenthesis around
640			   the variable reference, we have to put back the
641			   character that was replaced with a '\0'.  /RL */
642			*rp = r;
643			}
644		else
645			buf->data[to++]= *(from++);
646		}
647	buf->data[to]='\0';
648	if (*pto != NULL) OPENSSL_free(*pto);
649	*pto=buf->data;
650	OPENSSL_free(buf);
651	return(1);
652err:
653	if (buf != NULL) BUF_MEM_free(buf);
654	return(0);
655	}
656
657static char *eat_ws(CONF *conf, char *p)
658	{
659	while (IS_WS(conf,*p) && (!IS_EOF(conf,*p)))
660		p++;
661	return(p);
662	}
663
664static char *eat_alpha_numeric(CONF *conf, char *p)
665	{
666	for (;;)
667		{
668		if (IS_ESC(conf,*p))
669			{
670			p=scan_esc(conf,p);
671			continue;
672			}
673		if (!IS_ALPHA_NUMERIC_PUNCT(conf,*p))
674			return(p);
675		p++;
676		}
677	}
678
679static char *scan_quote(CONF *conf, char *p)
680	{
681	int q= *p;
682
683	p++;
684	while (!(IS_EOF(conf,*p)) && (*p != q))
685		{
686		if (IS_ESC(conf,*p))
687			{
688			p++;
689			if (IS_EOF(conf,*p)) return(p);
690			}
691		p++;
692		}
693	if (*p == q) p++;
694	return(p);
695	}
696
697
698static char *scan_dquote(CONF *conf, char *p)
699	{
700	int q= *p;
701
702	p++;
703	while (!(IS_EOF(conf,*p)))
704		{
705		if (*p == q)
706			{
707			if (*(p+1) == q)
708				{
709				p++;
710				}
711			else
712				{
713				break;
714				}
715			}
716		p++;
717		}
718	if (*p == q) p++;
719	return(p);
720	}
721
722static void dump_value(CONF_VALUE *a, BIO *out)
723	{
724	if (a->name)
725		BIO_printf(out, "[%s] %s=%s\n", a->section, a->name, a->value);
726	else
727		BIO_printf(out, "[[%s]]\n", a->section);
728	}
729
730static IMPLEMENT_LHASH_DOALL_ARG_FN(dump_value, CONF_VALUE *, BIO *)
731
732static int def_dump(const CONF *conf, BIO *out)
733	{
734	lh_doall_arg(conf->data, LHASH_DOALL_ARG_FN(dump_value), out);
735	return 1;
736	}
737
738static int def_is_number(const CONF *conf, char c)
739	{
740	return IS_NUMBER(conf,c);
741	}
742
743static int def_to_int(const CONF *conf, char c)
744	{
745	return c - '0';
746	}
747
748