1*e1dd0a2fSth /*
2*e1dd0a2fSth  * CDDL HEADER START
3*e1dd0a2fSth  *
4*e1dd0a2fSth  * The contents of this file are subject to the terms of the
5*e1dd0a2fSth  * Common Development and Distribution License (the "License").
6*e1dd0a2fSth  * You may not use this file except in compliance with the License.
7*e1dd0a2fSth  *
8*e1dd0a2fSth  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9*e1dd0a2fSth  * or http://www.opensolaris.org/os/licensing.
10*e1dd0a2fSth  * See the License for the specific language governing permissions
11*e1dd0a2fSth  * and limitations under the License.
12*e1dd0a2fSth  *
13*e1dd0a2fSth  * When distributing Covered Code, include this CDDL HEADER in each
14*e1dd0a2fSth  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15*e1dd0a2fSth  * If applicable, add the following below this CDDL HEADER, with the
16*e1dd0a2fSth  * fields enclosed by brackets "[]" replaced with your own identifying
17*e1dd0a2fSth  * information: Portions Copyright [yyyy] [name of copyright owner]
18*e1dd0a2fSth  *
19*e1dd0a2fSth  * CDDL HEADER END
20*e1dd0a2fSth  */
21*e1dd0a2fSth 
22*e1dd0a2fSth /*
23*e1dd0a2fSth  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
24*e1dd0a2fSth  * Use is subject to license terms.
25*e1dd0a2fSth  */
26*e1dd0a2fSth 
27*e1dd0a2fSth #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*e1dd0a2fSth 
29*e1dd0a2fSth /*
30*e1dd0a2fSth  * Helper functions for standalone functionality
31*e1dd0a2fSth  */
32*e1dd0a2fSth 
33*e1dd0a2fSth #include <assert.h>
34*e1dd0a2fSth #include <libintl.h>
35*e1dd0a2fSth #include <strings.h>
36*e1dd0a2fSth #include "ns_sldap.h"
37*e1dd0a2fSth #include "ns_internal.h"
38*e1dd0a2fSth 
39*e1dd0a2fSth ns_standalone_conf_t standaloneDefaults =
40*e1dd0a2fSth 	    { {NULL,		/* A directory server's IP/name. No default. */
41*e1dd0a2fSth 	    0,			/* A directory server's port. No default. */
42*e1dd0a2fSth 	    NULL,		/* A domain name. */
43*e1dd0a2fSth 				/* libsldap uses its own default. */
44*e1dd0a2fSth 	    "default",		/* A DUAProfile's name. */
45*e1dd0a2fSth 	    NULL,		/* Authentication information used. */
46*e1dd0a2fSth 				/* If not specified by the user, */
47*e1dd0a2fSth 				/* libsldap will use its own data */
48*e1dd0a2fSth 	    NULL,		/* A credential level to be used */
49*e1dd0a2fSth 				/* along with the authentication info. */
50*e1dd0a2fSth 				/* See the previous comment. */
51*e1dd0a2fSth 	    NSLDAPDIRECTORY,	/* The default path to */
52*e1dd0a2fSth 				/* the certificate database. */
53*e1dd0a2fSth 	    NULL,		/* A bind DN to be used during */
54*e1dd0a2fSth 				/* subsequent LDAP Bind requests */
55*e1dd0a2fSth 	    NULL},		/* A bind password to be used during */
56*e1dd0a2fSth 				/* subsequent LDAP Bind requests */
57*e1dd0a2fSth 	    NS_CACHEMGR};	/* If the -H option is not given, libsldap */
58*e1dd0a2fSth 				/* will obtain all the configuration */
59*e1dd0a2fSth 				/* information from ldap_cachemgr. */
60*e1dd0a2fSth 
61*e1dd0a2fSth int
separatePort(char * peer,char ** name,uint16_t * port)62*e1dd0a2fSth separatePort(char *peer, char **name, uint16_t *port)
63*e1dd0a2fSth {
64*e1dd0a2fSth 	char	*chr, *portStr = NULL;
65*e1dd0a2fSth 
66*e1dd0a2fSth 	chr = strchr(peer, '[');
67*e1dd0a2fSth 	if (chr != NULL) {
68*e1dd0a2fSth 		/* An IPv6 address */
69*e1dd0a2fSth 		*name = chr + 1;
70*e1dd0a2fSth 
71*e1dd0a2fSth 		chr = strchr(peer, ']');
72*e1dd0a2fSth 		if (chr == NULL) {
73*e1dd0a2fSth 			(void) fprintf(stderr,
74*e1dd0a2fSth 			    gettext("Server address is wrong: "
75*e1dd0a2fSth 			    "unbalanced [\n"));
76*e1dd0a2fSth 			return (1);
77*e1dd0a2fSth 		}
78*e1dd0a2fSth 
79*e1dd0a2fSth 		*chr++ = '\0';
80*e1dd0a2fSth 
81*e1dd0a2fSth 		chr = strchr(chr, ':');
82*e1dd0a2fSth 		if (chr != NULL && *(chr + 1) != '\0') {
83*e1dd0a2fSth 			portStr = chr + 1;
84*e1dd0a2fSth 		}
85*e1dd0a2fSth 	} else {
86*e1dd0a2fSth 		/* An IPv4 address */
87*e1dd0a2fSth 		chr = strchr(peer, ']');
88*e1dd0a2fSth 		if (chr != NULL) {
89*e1dd0a2fSth 			(void) fprintf(stderr,
90*e1dd0a2fSth 			    gettext("Server address is wrong: "
91*e1dd0a2fSth 			    "unbalanced ]\n"));
92*e1dd0a2fSth 			return (1);
93*e1dd0a2fSth 		}
94*e1dd0a2fSth 
95*e1dd0a2fSth 		chr = strchr(peer, ':');
96*e1dd0a2fSth 		if (chr != NULL && *(chr + 1) != '\0') {
97*e1dd0a2fSth 			*chr++ = '\0';
98*e1dd0a2fSth 			portStr = chr;
99*e1dd0a2fSth 		}
100*e1dd0a2fSth 
101*e1dd0a2fSth 		*name = peer;
102*e1dd0a2fSth 	}
103*e1dd0a2fSth 
104*e1dd0a2fSth 	if ((*name)[0] == '\0') {
105*e1dd0a2fSth 		(void) fprintf(stderr,
106*e1dd0a2fSth 		    gettext("Server address or name must be"
107*e1dd0a2fSth 		    " specified.\n"));
108*e1dd0a2fSth 		return (1);
109*e1dd0a2fSth 	}
110*e1dd0a2fSth 
111*e1dd0a2fSth 	if (portStr && sscanf(portStr, "%hu", port) != 1) {
112*e1dd0a2fSth 		(void) fprintf(stderr,
113*e1dd0a2fSth 		    gettext("Server port is wrong. "
114*e1dd0a2fSth 		    "The default port 389/636 "
115*e1dd0a2fSth 		    "will be used.\n"));
116*e1dd0a2fSth 	}
117*e1dd0a2fSth 	return (0);
118*e1dd0a2fSth }
119*e1dd0a2fSth 
120*e1dd0a2fSth char *
readPwd(char * pwd_file)121*e1dd0a2fSth readPwd(char *pwd_file)
122*e1dd0a2fSth {
123*e1dd0a2fSth 	FILE	*f;
124*e1dd0a2fSth 	char	*pwd;
125*e1dd0a2fSth 	char	passwdBuf[BUFSIZE];
126*e1dd0a2fSth 
127*e1dd0a2fSth 	if ((f = fopen(pwd_file, "r")) == NULL) {
128*e1dd0a2fSth 		(void) fprintf(stderr,
129*e1dd0a2fSth 		    gettext("Unable to open '%s' file\n"), pwd_file);
130*e1dd0a2fSth 		return (NULL);
131*e1dd0a2fSth 	}
132*e1dd0a2fSth 	if (fgets(passwdBuf, BUFSIZE, f) == NULL) {
133*e1dd0a2fSth 		(void) fprintf(stderr,
134*e1dd0a2fSth 		    gettext("Unable to read '%s' file\n"), pwd_file);
135*e1dd0a2fSth 		(void) fclose(f);
136*e1dd0a2fSth 		return (NULL);
137*e1dd0a2fSth 	}
138*e1dd0a2fSth 
139*e1dd0a2fSth 	(void) fclose(f);
140*e1dd0a2fSth 
141*e1dd0a2fSth 	if (passwdBuf[strlen(passwdBuf) - 1] == '\n') {
142*e1dd0a2fSth 		passwdBuf[strlen(passwdBuf) - 1] = '\0';
143*e1dd0a2fSth 	}
144*e1dd0a2fSth 	if ((pwd = strdup(passwdBuf)) == NULL) {
145*e1dd0a2fSth 		(void) fprintf(stderr,
146*e1dd0a2fSth 		    gettext("Memory allocation error\n"));
147*e1dd0a2fSth 		return (NULL);
148*e1dd0a2fSth 	}
149*e1dd0a2fSth 
150*e1dd0a2fSth 	return (pwd);
151*e1dd0a2fSth }
152