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 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 /*
30  * Turning DEBUG on for this library opens a potential security hole.  If
31  * the library is compiled with DEBUG, it should only be done for internal
32  * testing.
33  */
34 #include <stdlib.h>
35 #include <stdio.h>
36 #include <string.h>
37 #include <ctype.h>
38 #include <sys/types.h>
39 #include <sys/stat.h>
40 #include <fcntl.h>
41 #include <syslog.h>
42 
43 
44 #ifdef DEBUG
45 #define	OPT_INT		1
46 #define	OPT_STRING	2
47 #define	OPT_FILE	3
48 
49 int	__ldap_debug_file = 2;
50 int	__ldap_debug_api;
51 int	__ldap_debug_ldap;
52 int	__ldap_debug_servers;
53 
54 struct option {
55 	char	*name;
56 	int	type;
57 	void	*address;
58 };
59 
60 static struct option options[] = {
61 	{ "debug_file", OPT_FILE, &__ldap_debug_file },
62 	{ "debug_api", OPT_INT, &__ldap_debug_api },
63 	{ "debug_ldap", OPT_INT, &__ldap_debug_servers },
64 	{ 0, 0, 0 },
65 };
66 
67 #ifdef NS_NO_STDIO
68 extern int __ns_ldap_raise_fd(int);
69 #endif
70 
71 static void
set_option(char * name,char * val)72 set_option(char *name, char *val)
73 {
74 	struct option *opt;
75 	int		n;
76 	char		*p;
77 	int		fd;
78 
79 	for (opt = options; opt->name; opt++) {
80 		if (strcasecmp(name, opt->name) == 0) {
81 			switch (opt->type) {
82 			    case OPT_STRING:
83 				p = strdup(val);
84 				*((char **)opt->address) = p;
85 				break;
86 			    case OPT_INT:
87 				if (val && *val == '\0')
88 					n = 1;
89 				else
90 					n = atoi(val);
91 				*((int *)opt->address) = n;
92 				break;
93 			    case OPT_FILE:
94 				/* this is a potential security risk    */
95 				/* as setuid programs will create files */
96 				/* owned by root.  This is only to be   */
97 				/* used for internal debugging.		*/
98 				fd = open(val, O_WRONLY | O_CREAT, 0644);
99 #ifdef NS_NO_STDIO
100 				fd = __ns_ldap_raise_fd(fd);
101 #endif
102 				*((int *)opt->address) = fd;
103 				break;
104 			}
105 			break;
106 		}
107 	}
108 }
109 #endif
110 
111 void
get_environment()112 get_environment()
113 {
114 #ifdef DEBUG
115 	char	*p;
116 	char	*base;
117 	char	optname[100];
118 	char	optval[100];
119 
120 	p = getenv("LDAP_OPTIONS");
121 	if (p == NULL)
122 		return;
123 
124 	while (*p) {
125 		while (isspace(*p))
126 			p++;
127 		if (*p == '\0')
128 			break;
129 		base = p;
130 		while (*p && *p != '=' && !isspace(*p))
131 			p++;
132 		(void) strncpy(optname, base, p - base);
133 		optname[p - base] = '\0';
134 		if (*p == '=') {
135 			p++;
136 			base = p;
137 			while (*p && !isspace(*p))
138 				p++;
139 			(void) strncpy(optval, base, p - base);
140 			optval[p - base] = '\0';
141 		} else {
142 			optval[0] = '\0';
143 		}
144 		set_option(optname, optval);
145 	}
146 
147 	(void) fprintf(stderr, "debug_api: %d\n", __ldap_debug_api);
148 	(void) fprintf(stderr, "debug_ldap: %d\n", __ldap_debug_ldap);
149 	(void) fprintf(stderr, "debug_servers: %d\n", __ldap_debug_servers);
150 #endif
151 }
152 
153 /*ARGSUSED*/
154 void
__s_api_debug_pause(int priority,int st,const char * mesg)155 __s_api_debug_pause(int priority, int st, const char *mesg)
156 {
157 	if (mesg)
158 		syslog(priority, "libsldap: Status: %d  Mesg: %s", st, mesg);
159 }
160