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