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