1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 1999-2003 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 /*
28  * Turning DEBUG on for this library opens a potential security hole.  If
29  * the library is compiled with DEBUG, it should only be done for internal
30  * testing.
31  */
32 #include <stdlib.h>
33 #include <stdio.h>
34 #include <string.h>
35 #include <ctype.h>
36 #include <sys/types.h>
37 #include <sys/stat.h>
38 #include <fcntl.h>
39 #include <syslog.h>
40 
41 
42 #ifdef DEBUG
43 #define	OPT_INT		1
44 #define	OPT_STRING	2
45 #define	OPT_FILE	3
46 
47 int	__ldap_debug_file = 2;
48 int	__ldap_debug_api;
49 int	__ldap_debug_ldap;
50 int	__ldap_debug_servers;
51 
52 struct option {
53 	char	*name;
54 	int	type;
55 	void	*address;
56 };
57 
58 static struct option options[] = {
59 	{ "debug_file", OPT_FILE, &__ldap_debug_file },
60 	{ "debug_api", OPT_INT, &__ldap_debug_api },
61 	{ "debug_ldap", OPT_INT, &__ldap_debug_servers },
62 	{ 0, 0, 0 },
63 };
64 
65 #ifdef NS_NO_STDIO
66 extern int __ns_ldap_raise_fd(int);
67 #endif
68 
69 static void
set_option(char * name,char * val)70 set_option(char *name, char *val)
71 {
72 	struct option *opt;
73 	int		n;
74 	char		*p;
75 	int		fd;
76 
77 	for (opt = options; opt->name; opt++) {
78 		if (strcasecmp(name, opt->name) == 0) {
79 			switch (opt->type) {
80 			    case OPT_STRING:
81 				p = strdup(val);
82 				*((char **)opt->address) = p;
83 				break;
84 			    case OPT_INT:
85 				if (val && *val == '\0')
86 					n = 1;
87 				else
88 					n = atoi(val);
89 				*((int *)opt->address) = n;
90 				break;
91 			    case OPT_FILE:
92 				/* this is a potential security risk    */
93 				/* as setuid programs will create files */
94 				/* owned by root.  This is only to be   */
95 				/* used for internal debugging.		*/
96 				fd = open(val, O_WRONLY | O_CREAT, 0644);
97 #ifdef NS_NO_STDIO
98 				fd = __ns_ldap_raise_fd(fd);
99 #endif
100 				*((int *)opt->address) = fd;
101 				break;
102 			}
103 			break;
104 		}
105 	}
106 }
107 #endif
108 
109 void
get_environment()110 get_environment()
111 {
112 #ifdef DEBUG
113 	char	*p;
114 	char	*base;
115 	char	optname[100];
116 	char	optval[100];
117 
118 	p = getenv("LDAP_OPTIONS");
119 	if (p == NULL)
120 		return;
121 
122 	while (*p) {
123 		while (isspace(*p))
124 			p++;
125 		if (*p == '\0')
126 			break;
127 		base = p;
128 		while (*p && *p != '=' && !isspace(*p))
129 			p++;
130 		(void) strncpy(optname, base, p - base);
131 		optname[p - base] = '\0';
132 		if (*p == '=') {
133 			p++;
134 			base = p;
135 			while (*p && !isspace(*p))
136 				p++;
137 			(void) strncpy(optval, base, p - base);
138 			optval[p - base] = '\0';
139 		} else {
140 			optval[0] = '\0';
141 		}
142 		set_option(optname, optval);
143 	}
144 
145 	(void) fprintf(stderr, "debug_api: %d\n", __ldap_debug_api);
146 	(void) fprintf(stderr, "debug_ldap: %d\n", __ldap_debug_ldap);
147 	(void) fprintf(stderr, "debug_servers: %d\n", __ldap_debug_servers);
148 #endif
149 }
150 
151 /*ARGSUSED*/
152 void
__s_api_debug_pause(int priority,int st,const char * mesg)153 __s_api_debug_pause(int priority, int st, const char *mesg)
154 {
155 	if (mesg)
156 		syslog(priority, "libsldap: Status: %d  Mesg: %s", st, mesg);
157 }
158