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