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 /*
28*e1dd0a2fSth  * Helper functions for standalone functionality
29*e1dd0a2fSth  */
30*e1dd0a2fSth 
31*e1dd0a2fSth #include <assert.h>
32*e1dd0a2fSth #include <libintl.h>
33*e1dd0a2fSth #include <strings.h>
34*e1dd0a2fSth #include "ns_sldap.h"
35*e1dd0a2fSth #include "ns_internal.h"
36*e1dd0a2fSth 
37*e1dd0a2fSth ns_standalone_conf_t standaloneDefaults =
38*e1dd0a2fSth 	    { {NULL,		/* A directory server's IP/name. No default. */
39*e1dd0a2fSth 	    0,			/* A directory server's port. No default. */
40*e1dd0a2fSth 	    NULL,		/* A domain name. */
41*e1dd0a2fSth 				/* libsldap uses its own default. */
42*e1dd0a2fSth 	    "default",		/* A DUAProfile's name. */
43*e1dd0a2fSth 	    NULL,		/* Authentication information used. */
44*e1dd0a2fSth 				/* If not specified by the user, */
45*e1dd0a2fSth 				/* libsldap will use its own data */
46*e1dd0a2fSth 	    NULL,		/* A credential level to be used */
47*e1dd0a2fSth 				/* along with the authentication info. */
48*e1dd0a2fSth 				/* See the previous comment. */
49*e1dd0a2fSth 	    NSLDAPDIRECTORY,	/* The default path to */
50*e1dd0a2fSth 				/* the certificate database. */
51*e1dd0a2fSth 	    NULL,		/* A bind DN to be used during */
52*e1dd0a2fSth 				/* subsequent LDAP Bind requests */
53*e1dd0a2fSth 	    NULL},		/* A bind password to be used during */
54*e1dd0a2fSth 				/* subsequent LDAP Bind requests */
55*e1dd0a2fSth 	    NS_CACHEMGR};	/* If the -H option is not given, libsldap */
56*e1dd0a2fSth 				/* will obtain all the configuration */
57*e1dd0a2fSth 				/* information from ldap_cachemgr. */
58*e1dd0a2fSth 
59*e1dd0a2fSth int
separatePort(char * peer,char ** name,uint16_t * port)60*e1dd0a2fSth separatePort(char *peer, char **name, uint16_t *port)
61*e1dd0a2fSth {
62*e1dd0a2fSth 	char	*chr, *portStr = NULL;
63*e1dd0a2fSth 
64*e1dd0a2fSth 	chr = strchr(peer, '[');
65*e1dd0a2fSth 	if (chr != NULL) {
66*e1dd0a2fSth 		/* An IPv6 address */
67*e1dd0a2fSth 		*name = chr + 1;
68*e1dd0a2fSth 
69*e1dd0a2fSth 		chr = strchr(peer, ']');
70*e1dd0a2fSth 		if (chr == NULL) {
71*e1dd0a2fSth 			(void) fprintf(stderr,
72*e1dd0a2fSth 			    gettext("Server address is wrong: "
73*e1dd0a2fSth 			    "unbalanced [\n"));
74*e1dd0a2fSth 			return (1);
75*e1dd0a2fSth 		}
76*e1dd0a2fSth 
77*e1dd0a2fSth 		*chr++ = '\0';
78*e1dd0a2fSth 
79*e1dd0a2fSth 		chr = strchr(chr, ':');
80*e1dd0a2fSth 		if (chr != NULL && *(chr + 1) != '\0') {
81*e1dd0a2fSth 			portStr = chr + 1;
82*e1dd0a2fSth 		}
83*e1dd0a2fSth 	} else {
84*e1dd0a2fSth 		/* An IPv4 address */
85*e1dd0a2fSth 		chr = strchr(peer, ']');
86*e1dd0a2fSth 		if (chr != NULL) {
87*e1dd0a2fSth 			(void) fprintf(stderr,
88*e1dd0a2fSth 			    gettext("Server address is wrong: "
89*e1dd0a2fSth 			    "unbalanced ]\n"));
90*e1dd0a2fSth 			return (1);
91*e1dd0a2fSth 		}
92*e1dd0a2fSth 
93*e1dd0a2fSth 		chr = strchr(peer, ':');
94*e1dd0a2fSth 		if (chr != NULL && *(chr + 1) != '\0') {
95*e1dd0a2fSth 			*chr++ = '\0';
96*e1dd0a2fSth 			portStr = chr;
97*e1dd0a2fSth 		}
98*e1dd0a2fSth 
99*e1dd0a2fSth 		*name = peer;
100*e1dd0a2fSth 	}
101*e1dd0a2fSth 
102*e1dd0a2fSth 	if ((*name)[0] == '\0') {
103*e1dd0a2fSth 		(void) fprintf(stderr,
104*e1dd0a2fSth 		    gettext("Server address or name must be"
105*e1dd0a2fSth 		    " specified.\n"));
106*e1dd0a2fSth 		return (1);
107*e1dd0a2fSth 	}
108*e1dd0a2fSth 
109*e1dd0a2fSth 	if (portStr && sscanf(portStr, "%hu", port) != 1) {
110*e1dd0a2fSth 		(void) fprintf(stderr,
111*e1dd0a2fSth 		    gettext("Server port is wrong. "
112*e1dd0a2fSth 		    "The default port 389/636 "
113*e1dd0a2fSth 		    "will be used.\n"));
114*e1dd0a2fSth 	}
115*e1dd0a2fSth 	return (0);
116*e1dd0a2fSth }
117*e1dd0a2fSth 
118*e1dd0a2fSth char *
readPwd(char * pwd_file)119*e1dd0a2fSth readPwd(char *pwd_file)
120*e1dd0a2fSth {
121*e1dd0a2fSth 	FILE	*f;
122*e1dd0a2fSth 	char	*pwd;
123*e1dd0a2fSth 	char	passwdBuf[BUFSIZE];
124*e1dd0a2fSth 
125*e1dd0a2fSth 	if ((f = fopen(pwd_file, "r")) == NULL) {
126*e1dd0a2fSth 		(void) fprintf(stderr,
127*e1dd0a2fSth 		    gettext("Unable to open '%s' file\n"), pwd_file);
128*e1dd0a2fSth 		return (NULL);
129*e1dd0a2fSth 	}
130*e1dd0a2fSth 	if (fgets(passwdBuf, BUFSIZE, f) == NULL) {
131*e1dd0a2fSth 		(void) fprintf(stderr,
132*e1dd0a2fSth 		    gettext("Unable to read '%s' file\n"), pwd_file);
133*e1dd0a2fSth 		(void) fclose(f);
134*e1dd0a2fSth 		return (NULL);
135*e1dd0a2fSth 	}
136*e1dd0a2fSth 
137*e1dd0a2fSth 	(void) fclose(f);
138*e1dd0a2fSth 
139*e1dd0a2fSth 	if (passwdBuf[strlen(passwdBuf) - 1] == '\n') {
140*e1dd0a2fSth 		passwdBuf[strlen(passwdBuf) - 1] = '\0';
141*e1dd0a2fSth 	}
142*e1dd0a2fSth 	if ((pwd = strdup(passwdBuf)) == NULL) {
143*e1dd0a2fSth 		(void) fprintf(stderr,
144*e1dd0a2fSth 		    gettext("Memory allocation error\n"));
145*e1dd0a2fSth 		return (NULL);
146*e1dd0a2fSth 	}
147*e1dd0a2fSth 
148*e1dd0a2fSth 	return (pwd);
149*e1dd0a2fSth }
150