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