17c478bd9Sstevel@tonic-gate /* 27c478bd9Sstevel@tonic-gate * CDDL HEADER START 37c478bd9Sstevel@tonic-gate * 47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5*36e852a1SRaja Andra * Common Development and Distribution License (the "License"). 6*36e852a1SRaja Andra * You may not use this file except in compliance with the License. 77c478bd9Sstevel@tonic-gate * 87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 117c478bd9Sstevel@tonic-gate * and limitations under the License. 127c478bd9Sstevel@tonic-gate * 137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 187c478bd9Sstevel@tonic-gate * 197c478bd9Sstevel@tonic-gate * CDDL HEADER END 207c478bd9Sstevel@tonic-gate */ 217c478bd9Sstevel@tonic-gate /* 22*36e852a1SRaja Andra * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 237c478bd9Sstevel@tonic-gate * Use is subject to license terms. 247c478bd9Sstevel@tonic-gate */ 257c478bd9Sstevel@tonic-gate 267c478bd9Sstevel@tonic-gate #include <stdio.h> 277c478bd9Sstevel@tonic-gate #include <stdlib.h> 287c478bd9Sstevel@tonic-gate #include <ctype.h> 297c478bd9Sstevel@tonic-gate #include <strings.h> 307c478bd9Sstevel@tonic-gate #include <pwd.h> 317c478bd9Sstevel@tonic-gate #include <shadow.h> 327c478bd9Sstevel@tonic-gate #include <netdb.h> 337c478bd9Sstevel@tonic-gate #include <mp.h> 347c478bd9Sstevel@tonic-gate #include <rpcsvc/nis.h> 357c478bd9Sstevel@tonic-gate #include <rpc/key_prot.h> 367c478bd9Sstevel@tonic-gate #include <nsswitch.h> 377c478bd9Sstevel@tonic-gate #include <ns_sldap.h> 387c478bd9Sstevel@tonic-gate 397c478bd9Sstevel@tonic-gate extern char *crypt(); 407c478bd9Sstevel@tonic-gate extern long random(); 417c478bd9Sstevel@tonic-gate extern char *getpassphrase(); 427c478bd9Sstevel@tonic-gate extern char *program_name; 437c478bd9Sstevel@tonic-gate static const char *CRED_TABLE = "cred.org_dir"; 447c478bd9Sstevel@tonic-gate 457c478bd9Sstevel@tonic-gate #define ROOTKEY_FILE "/etc/.rootkey" 467c478bd9Sstevel@tonic-gate 477c478bd9Sstevel@tonic-gate #ifndef MAXHOSTNAMELEN 487c478bd9Sstevel@tonic-gate #define MAXHOSTNAMELEN 256 497c478bd9Sstevel@tonic-gate #endif 507c478bd9Sstevel@tonic-gate 517c478bd9Sstevel@tonic-gate #define PK_FILES 1 527c478bd9Sstevel@tonic-gate #define PK_YP 2 537c478bd9Sstevel@tonic-gate #define PK_LDAP 4 547c478bd9Sstevel@tonic-gate 557c478bd9Sstevel@tonic-gate #define LDAP_BINDDN_DEFAULT "cn=Directory Manager" 567c478bd9Sstevel@tonic-gate #define PROMPTGET_SUCCESS 1 577c478bd9Sstevel@tonic-gate #define PROMPTGET_FAIL -1 587c478bd9Sstevel@tonic-gate #define PROMPTGET_MEMORY_FAIL -2 597c478bd9Sstevel@tonic-gate #define PASSWD_UNMATCHED -3 607c478bd9Sstevel@tonic-gate 617c478bd9Sstevel@tonic-gate #define FREE_CREDINFO(s) \ 627c478bd9Sstevel@tonic-gate if ((s)) { (void) memset((s), 0, strlen((s))); } 637c478bd9Sstevel@tonic-gate 647c478bd9Sstevel@tonic-gate 657c478bd9Sstevel@tonic-gate /* ************************ switch functions *************************** */ 667c478bd9Sstevel@tonic-gate 677c478bd9Sstevel@tonic-gate /* NSW_NOTSUCCESS NSW_NOTFOUND NSW_UNAVAIL NSW_TRYAGAIN */ 687c478bd9Sstevel@tonic-gate #define DEF_ACTION {__NSW_RETURN, __NSW_RETURN, __NSW_CONTINUE, __NSW_CONTINUE} 697c478bd9Sstevel@tonic-gate 707c478bd9Sstevel@tonic-gate static struct __nsw_lookup lookup_files = {"files", DEF_ACTION, NULL, NULL}, 717c478bd9Sstevel@tonic-gate lookup_nis = {"nis", DEF_ACTION, NULL, &lookup_files}; 727c478bd9Sstevel@tonic-gate static struct __nsw_switchconfig publickey_default = 737c478bd9Sstevel@tonic-gate {0, "publickey", 2, &lookup_nis}; 747c478bd9Sstevel@tonic-gate 757c478bd9Sstevel@tonic-gate static int get_ldap_bindDN(char **); 767c478bd9Sstevel@tonic-gate static int get_ldap_bindPassword(char **); 777c478bd9Sstevel@tonic-gate 787c478bd9Sstevel@tonic-gate /* 797c478bd9Sstevel@tonic-gate * Prompt the users for a ldap bind DN. If users do not enter a value but just 807c478bd9Sstevel@tonic-gate * simply hit the return key, the default bindDN "cn=Directory Manager" 817c478bd9Sstevel@tonic-gate * will be used. 827c478bd9Sstevel@tonic-gate */ 837c478bd9Sstevel@tonic-gate static int 847c478bd9Sstevel@tonic-gate get_ldap_bindDN(char **ret_bindDN) { 857c478bd9Sstevel@tonic-gate 867c478bd9Sstevel@tonic-gate char bindDN[BUFSIZ]; 877c478bd9Sstevel@tonic-gate char prompt[BUFSIZ]; 887c478bd9Sstevel@tonic-gate int blen, pos; 897c478bd9Sstevel@tonic-gate 907c478bd9Sstevel@tonic-gate /* set the initial value for bindDN buffer */ 917c478bd9Sstevel@tonic-gate (void) memset(bindDN, 0, BUFSIZ); 927c478bd9Sstevel@tonic-gate 937c478bd9Sstevel@tonic-gate (void) snprintf(prompt, BUFSIZ, 947c478bd9Sstevel@tonic-gate "\nThe LDAP bind DN and password are required for this update.\n" 957c478bd9Sstevel@tonic-gate "If you are not sure what values to enter, please contact your\n" 967c478bd9Sstevel@tonic-gate "LDAP administrator.\n\nPlease enter LDAP bind DN [%s]: ", 977c478bd9Sstevel@tonic-gate LDAP_BINDDN_DEFAULT); 987c478bd9Sstevel@tonic-gate 997c478bd9Sstevel@tonic-gate printf(prompt); 1007c478bd9Sstevel@tonic-gate 1017c478bd9Sstevel@tonic-gate if (fgets(bindDN, sizeof (bindDN), stdin) == NULL) { 1027c478bd9Sstevel@tonic-gate (void) strlcpy(bindDN, LDAP_BINDDN_DEFAULT, BUFSIZ); 1037c478bd9Sstevel@tonic-gate } 1047c478bd9Sstevel@tonic-gate 1057c478bd9Sstevel@tonic-gate blen = strlen(bindDN); 1067c478bd9Sstevel@tonic-gate 1077c478bd9Sstevel@tonic-gate /* Check if the buffer ends with a newline */ 1087c478bd9Sstevel@tonic-gate if ((blen > 0) && (bindDN[blen - 1] == '\n')) { 1097c478bd9Sstevel@tonic-gate bindDN[blen - 1] = '\0'; 1107c478bd9Sstevel@tonic-gate blen -= 1; 1117c478bd9Sstevel@tonic-gate } 1127c478bd9Sstevel@tonic-gate 1137c478bd9Sstevel@tonic-gate /* Remove the white spaces */ 1147c478bd9Sstevel@tonic-gate if (blen > 0) { 1157c478bd9Sstevel@tonic-gate for (pos = blen - 1; pos >= 0; pos--) { 1167c478bd9Sstevel@tonic-gate if (isspace(bindDN[pos])) 1177c478bd9Sstevel@tonic-gate bindDN[pos] = '\0'; 1187c478bd9Sstevel@tonic-gate else 1197c478bd9Sstevel@tonic-gate break; 1207c478bd9Sstevel@tonic-gate } 1217c478bd9Sstevel@tonic-gate } 1227c478bd9Sstevel@tonic-gate 1237c478bd9Sstevel@tonic-gate /* Use the default bindDN, if the buffer contains no characters */ 1247c478bd9Sstevel@tonic-gate if (strlen(bindDN) == 0) 1257c478bd9Sstevel@tonic-gate (void) strlcpy(bindDN, LDAP_BINDDN_DEFAULT, BUFSIZ); 1267c478bd9Sstevel@tonic-gate 1277c478bd9Sstevel@tonic-gate if ((*ret_bindDN = (char *)malloc(strlen(bindDN)+1)) == NULL) { 1287c478bd9Sstevel@tonic-gate (void) memset(bindDN, 0, BUFSIZ); 1297c478bd9Sstevel@tonic-gate return (PROMPTGET_MEMORY_FAIL); 1307c478bd9Sstevel@tonic-gate } 1317c478bd9Sstevel@tonic-gate 1327c478bd9Sstevel@tonic-gate (void) strlcpy(*ret_bindDN, bindDN, strlen(bindDN)+1); 1337c478bd9Sstevel@tonic-gate 1347c478bd9Sstevel@tonic-gate /* Clean up and erase the credential info */ 1357c478bd9Sstevel@tonic-gate (void) memset(bindDN, 0, BUFSIZ); 1367c478bd9Sstevel@tonic-gate 1377c478bd9Sstevel@tonic-gate return (PROMPTGET_SUCCESS); 1387c478bd9Sstevel@tonic-gate } 1397c478bd9Sstevel@tonic-gate 1407c478bd9Sstevel@tonic-gate 1417c478bd9Sstevel@tonic-gate /* 1427c478bd9Sstevel@tonic-gate * Prompt the user for a ldap bind password. 1437c478bd9Sstevel@tonic-gate */ 1447c478bd9Sstevel@tonic-gate static int 1457c478bd9Sstevel@tonic-gate get_ldap_bindPassword(char **ret_bindPass) { 1467c478bd9Sstevel@tonic-gate 1477c478bd9Sstevel@tonic-gate char bindPassword[BUFSIZ]; 1487c478bd9Sstevel@tonic-gate char prompt[BUFSIZ]; 1497c478bd9Sstevel@tonic-gate char *bindPass = NULL; 1507c478bd9Sstevel@tonic-gate 1517c478bd9Sstevel@tonic-gate /* set the initial value for bindPassword buffer */ 1527c478bd9Sstevel@tonic-gate (void) memset(bindPassword, 0, BUFSIZ); 1537c478bd9Sstevel@tonic-gate *ret_bindPass = NULL; 1547c478bd9Sstevel@tonic-gate 1557c478bd9Sstevel@tonic-gate (void) snprintf(prompt, BUFSIZ, 1567c478bd9Sstevel@tonic-gate "Please enter LDAP bind password: "); 1577c478bd9Sstevel@tonic-gate 1587c478bd9Sstevel@tonic-gate bindPass = getpassphrase(prompt); 1597c478bd9Sstevel@tonic-gate 1607c478bd9Sstevel@tonic-gate if (bindPass == NULL) 1617c478bd9Sstevel@tonic-gate return (PROMPTGET_FAIL); 1627c478bd9Sstevel@tonic-gate 1637c478bd9Sstevel@tonic-gate (void) strlcpy(bindPassword, bindPass, BUFSIZ); 1647c478bd9Sstevel@tonic-gate 1657c478bd9Sstevel@tonic-gate /* clean the static buffer returned from getpassphrase call */ 1667c478bd9Sstevel@tonic-gate (void) memset(bindPass, 0, strlen(bindPass)); 1677c478bd9Sstevel@tonic-gate bindPass = NULL; 1687c478bd9Sstevel@tonic-gate 1697c478bd9Sstevel@tonic-gate /* 1707c478bd9Sstevel@tonic-gate * Re-enter the bind passowrd and compare it with the one 1717c478bd9Sstevel@tonic-gate * from previous entered. 1727c478bd9Sstevel@tonic-gate */ 1737c478bd9Sstevel@tonic-gate (void) snprintf(prompt, BUFSIZ, 1747c478bd9Sstevel@tonic-gate "Re-enter LDAP bind password to confirm: "); 1757c478bd9Sstevel@tonic-gate 1767c478bd9Sstevel@tonic-gate bindPass = getpassphrase(prompt); 1777c478bd9Sstevel@tonic-gate 1787c478bd9Sstevel@tonic-gate if (bindPass == NULL) { 1797c478bd9Sstevel@tonic-gate (void) memset(bindPassword, 0, BUFSIZ); 1807c478bd9Sstevel@tonic-gate return (PASSWD_UNMATCHED); 1817c478bd9Sstevel@tonic-gate } 1827c478bd9Sstevel@tonic-gate 1837c478bd9Sstevel@tonic-gate if (strcmp(bindPass, bindPassword) != 0) { 1847c478bd9Sstevel@tonic-gate (void) memset(bindPassword, 0, BUFSIZ); 1857c478bd9Sstevel@tonic-gate (void) memset(bindPass, 0, strlen(bindPass)); 1867c478bd9Sstevel@tonic-gate return (PASSWD_UNMATCHED); 1877c478bd9Sstevel@tonic-gate } else { 1887c478bd9Sstevel@tonic-gate (void) memset(bindPass, 0, strlen(bindPass)); 1897c478bd9Sstevel@tonic-gate if ((*ret_bindPass = (char *)malloc(strlen(bindPassword)+1)) 1907c478bd9Sstevel@tonic-gate == NULL) { 1917c478bd9Sstevel@tonic-gate (void) memset(bindPassword, 0, BUFSIZ); 1927c478bd9Sstevel@tonic-gate return (PROMPTGET_MEMORY_FAIL); 1937c478bd9Sstevel@tonic-gate } 1947c478bd9Sstevel@tonic-gate 1957c478bd9Sstevel@tonic-gate (void) strlcpy(*ret_bindPass, bindPassword, 1967c478bd9Sstevel@tonic-gate strlen(bindPassword)+1); 1977c478bd9Sstevel@tonic-gate 1987c478bd9Sstevel@tonic-gate /* Clean up and erase the credential info */ 1997c478bd9Sstevel@tonic-gate (void) memset(bindPassword, 0, BUFSIZ); 2007c478bd9Sstevel@tonic-gate 2017c478bd9Sstevel@tonic-gate return (PROMPTGET_SUCCESS); 2027c478bd9Sstevel@tonic-gate } 2037c478bd9Sstevel@tonic-gate } 2047c478bd9Sstevel@tonic-gate 2057c478bd9Sstevel@tonic-gate 2067c478bd9Sstevel@tonic-gate 2077c478bd9Sstevel@tonic-gate char * 2087c478bd9Sstevel@tonic-gate switch_policy_str(struct __nsw_switchconfig *conf) 2097c478bd9Sstevel@tonic-gate { 2107c478bd9Sstevel@tonic-gate struct __nsw_lookup *look; 2117c478bd9Sstevel@tonic-gate static char policy[256]; /* 256 is enough for (nis, files...etc) */ 2127c478bd9Sstevel@tonic-gate int previous = 0; 2137c478bd9Sstevel@tonic-gate 2147c478bd9Sstevel@tonic-gate memset((char *)policy, 0, 256); 2157c478bd9Sstevel@tonic-gate 2167c478bd9Sstevel@tonic-gate for (look = conf->lookups; look; look = look->next) { 2177c478bd9Sstevel@tonic-gate if (previous) 2187c478bd9Sstevel@tonic-gate strcat(policy, " "); 2197c478bd9Sstevel@tonic-gate strcat(policy, look->service_name); 2207c478bd9Sstevel@tonic-gate previous = 1; 2217c478bd9Sstevel@tonic-gate } 2227c478bd9Sstevel@tonic-gate return (policy); 2237c478bd9Sstevel@tonic-gate } 2247c478bd9Sstevel@tonic-gate 2257c478bd9Sstevel@tonic-gate int 2267c478bd9Sstevel@tonic-gate no_switch_policy(struct __nsw_switchconfig *conf) 2277c478bd9Sstevel@tonic-gate { 2287c478bd9Sstevel@tonic-gate return (conf == NULL || conf->lookups == NULL); 2297c478bd9Sstevel@tonic-gate } 2307c478bd9Sstevel@tonic-gate 23149e7ca49Speteh int 2327c478bd9Sstevel@tonic-gate is_switch_policy(struct __nsw_switchconfig *conf, char *target) 2337c478bd9Sstevel@tonic-gate { 2347c478bd9Sstevel@tonic-gate return (conf && 2357c478bd9Sstevel@tonic-gate conf->lookups && 2367c478bd9Sstevel@tonic-gate strcmp(conf->lookups->service_name, target) == 0 && 2377c478bd9Sstevel@tonic-gate conf->lookups->next == NULL); 2387c478bd9Sstevel@tonic-gate } 2397c478bd9Sstevel@tonic-gate 2407c478bd9Sstevel@tonic-gate char * 2417c478bd9Sstevel@tonic-gate first_and_only_switch_policy(char *policy, 2427c478bd9Sstevel@tonic-gate struct __nsw_switchconfig *default_conf, 2437c478bd9Sstevel@tonic-gate char *head_msg) 2447c478bd9Sstevel@tonic-gate { 2457c478bd9Sstevel@tonic-gate struct __nsw_switchconfig *conf; 2467c478bd9Sstevel@tonic-gate enum __nsw_parse_err perr; 2477c478bd9Sstevel@tonic-gate int policy_correct = 1; 2487c478bd9Sstevel@tonic-gate char *target_service = 0; 2497c478bd9Sstevel@tonic-gate int use_default = 0; 2507c478bd9Sstevel@tonic-gate 2517c478bd9Sstevel@tonic-gate if (default_conf == 0) 2527c478bd9Sstevel@tonic-gate default_conf = &publickey_default; 2537c478bd9Sstevel@tonic-gate 2547c478bd9Sstevel@tonic-gate conf = __nsw_getconfig(policy, &perr); 2557c478bd9Sstevel@tonic-gate if (no_switch_policy(conf)) { 2567c478bd9Sstevel@tonic-gate use_default = 1; 2577c478bd9Sstevel@tonic-gate conf = default_conf; 2587c478bd9Sstevel@tonic-gate } 2597c478bd9Sstevel@tonic-gate 2607c478bd9Sstevel@tonic-gate target_service = conf->lookups->service_name; 2617c478bd9Sstevel@tonic-gate 2627c478bd9Sstevel@tonic-gate if (conf->lookups->next != NULL) { 2637c478bd9Sstevel@tonic-gate policy_correct = 0; 2647c478bd9Sstevel@tonic-gate if (use_default) { 2657c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 2667c478bd9Sstevel@tonic-gate "\n%s\n There is no publickey entry in %s.\n", 2677c478bd9Sstevel@tonic-gate head_msg, __NSW_CONFIG_FILE); 2687c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 2697c478bd9Sstevel@tonic-gate "The default publickey policy is \"publickey: %s\".\n", 2707c478bd9Sstevel@tonic-gate switch_policy_str(default_conf)); 2717c478bd9Sstevel@tonic-gate } else 2727c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 2737c478bd9Sstevel@tonic-gate "\n%s\nThe publickey entry in %s is \"publickey: %s\".\n", 2747c478bd9Sstevel@tonic-gate head_msg, __NSW_CONFIG_FILE, 2757c478bd9Sstevel@tonic-gate switch_policy_str(conf)); 2767c478bd9Sstevel@tonic-gate } 2777c478bd9Sstevel@tonic-gate 2787c478bd9Sstevel@tonic-gate if (policy_correct == 0) 2797c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 2807c478bd9Sstevel@tonic-gate "I cannot figure out which publickey database you want to update.\n"); 2817c478bd9Sstevel@tonic-gate if (!use_default && conf) 2827c478bd9Sstevel@tonic-gate __nsw_freeconfig(conf); 2837c478bd9Sstevel@tonic-gate 2847c478bd9Sstevel@tonic-gate if (policy_correct) 2857c478bd9Sstevel@tonic-gate return (target_service); 2867c478bd9Sstevel@tonic-gate else 2877c478bd9Sstevel@tonic-gate return (0); 2887c478bd9Sstevel@tonic-gate } 2897c478bd9Sstevel@tonic-gate 2907c478bd9Sstevel@tonic-gate 2917c478bd9Sstevel@tonic-gate 2927c478bd9Sstevel@tonic-gate int 2937c478bd9Sstevel@tonic-gate check_switch_policy(char *policy, char *target_service, 2947c478bd9Sstevel@tonic-gate struct __nsw_switchconfig *default_conf, 2957c478bd9Sstevel@tonic-gate char *head_msg, char *tail_msg) 2967c478bd9Sstevel@tonic-gate { 2977c478bd9Sstevel@tonic-gate struct __nsw_switchconfig *conf; 2987c478bd9Sstevel@tonic-gate enum __nsw_parse_err perr; 2997c478bd9Sstevel@tonic-gate int policy_correct = 1; 3007c478bd9Sstevel@tonic-gate 3017c478bd9Sstevel@tonic-gate if (default_conf == 0) 3027c478bd9Sstevel@tonic-gate default_conf = &publickey_default; 3037c478bd9Sstevel@tonic-gate 3047c478bd9Sstevel@tonic-gate conf = __nsw_getconfig(policy, &perr); 3057c478bd9Sstevel@tonic-gate if (no_switch_policy(conf)) { 3067c478bd9Sstevel@tonic-gate if (!is_switch_policy(default_conf, target_service)) { 3077c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 3087c478bd9Sstevel@tonic-gate "\n%s\nThere is no publickey entry in %s.\n", 3097c478bd9Sstevel@tonic-gate head_msg, __NSW_CONFIG_FILE); 3107c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 3117c478bd9Sstevel@tonic-gate "The default publickey policy is \"publickey: %s\".\n", 3127c478bd9Sstevel@tonic-gate switch_policy_str(default_conf)); 3137c478bd9Sstevel@tonic-gate policy_correct = 0; 3147c478bd9Sstevel@tonic-gate } 3157c478bd9Sstevel@tonic-gate } else if (!is_switch_policy(conf, target_service)) { 3167c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 3177c478bd9Sstevel@tonic-gate "\n%s\nThe publickey entry in %s is \"publickey: %s\".\n", 3187c478bd9Sstevel@tonic-gate head_msg, __NSW_CONFIG_FILE, 3197c478bd9Sstevel@tonic-gate switch_policy_str(conf)); 3207c478bd9Sstevel@tonic-gate policy_correct = 0; 3217c478bd9Sstevel@tonic-gate } 3227c478bd9Sstevel@tonic-gate /* should we exit ? */ 3237c478bd9Sstevel@tonic-gate if (policy_correct == 0) 3247c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 3257c478bd9Sstevel@tonic-gate "It should be \"publickey: %s\"%s\n\n", 3267c478bd9Sstevel@tonic-gate target_service, tail_msg); 3277c478bd9Sstevel@tonic-gate if (conf) 3287c478bd9Sstevel@tonic-gate __nsw_freeconfig(conf); 3297c478bd9Sstevel@tonic-gate 3307c478bd9Sstevel@tonic-gate return (policy_correct); 3317c478bd9Sstevel@tonic-gate } 3327c478bd9Sstevel@tonic-gate 3337c478bd9Sstevel@tonic-gate int 3347c478bd9Sstevel@tonic-gate get_pk_source(char *pk_service) 3357c478bd9Sstevel@tonic-gate { 3367c478bd9Sstevel@tonic-gate int db = 0, got_from_switch = 0; 3377c478bd9Sstevel@tonic-gate 3387c478bd9Sstevel@tonic-gate /* No service specified, try to figure out from switch */ 3397c478bd9Sstevel@tonic-gate if (pk_service == 0) { 3407c478bd9Sstevel@tonic-gate pk_service = first_and_only_switch_policy("publickey", 0, 3417c478bd9Sstevel@tonic-gate "ERROR:"); 3427c478bd9Sstevel@tonic-gate if (pk_service == 0) 3437c478bd9Sstevel@tonic-gate return (0); 3447c478bd9Sstevel@tonic-gate (void) fprintf(stdout, 3457c478bd9Sstevel@tonic-gate "Updating %s publickey database.\n", 3467c478bd9Sstevel@tonic-gate pk_service); 3477c478bd9Sstevel@tonic-gate got_from_switch = 1; 3487c478bd9Sstevel@tonic-gate } 3497c478bd9Sstevel@tonic-gate 3507c478bd9Sstevel@tonic-gate if (strcmp(pk_service, "ldap") == 0) 3517c478bd9Sstevel@tonic-gate db = PK_LDAP; 3527c478bd9Sstevel@tonic-gate else if (strcmp(pk_service, "nis") == 0) 3537c478bd9Sstevel@tonic-gate db = PK_YP; 3547c478bd9Sstevel@tonic-gate else if (strcmp(pk_service, "files") == 0) 3557c478bd9Sstevel@tonic-gate db = PK_FILES; 3567c478bd9Sstevel@tonic-gate else return (0); 3577c478bd9Sstevel@tonic-gate 3587c478bd9Sstevel@tonic-gate /* 3597c478bd9Sstevel@tonic-gate * If we didn't get service name from switch, check switch 3607c478bd9Sstevel@tonic-gate * and print warning about it source of publickeys if not unique 3617c478bd9Sstevel@tonic-gate */ 3627c478bd9Sstevel@tonic-gate if (got_from_switch == 0) 3637c478bd9Sstevel@tonic-gate check_switch_policy("publickey", pk_service, 0, "WARNING:", 3647c478bd9Sstevel@tonic-gate db == PK_FILES ? "" : 3657c478bd9Sstevel@tonic-gate "; add 'files' if you want the 'nobody' key."); 3667c478bd9Sstevel@tonic-gate 3677c478bd9Sstevel@tonic-gate 3687c478bd9Sstevel@tonic-gate return (db); /* all passed */ 3697c478bd9Sstevel@tonic-gate } 3707c478bd9Sstevel@tonic-gate 3717c478bd9Sstevel@tonic-gate 3727c478bd9Sstevel@tonic-gate /* ***************************** keylogin stuff *************************** */ 3737c478bd9Sstevel@tonic-gate int 3747c478bd9Sstevel@tonic-gate keylogin(char *netname, char *secret) 3757c478bd9Sstevel@tonic-gate { 3767c478bd9Sstevel@tonic-gate struct key_netstarg netst; 3777c478bd9Sstevel@tonic-gate 3787c478bd9Sstevel@tonic-gate netst.st_pub_key[0] = 0; 3797c478bd9Sstevel@tonic-gate memcpy(netst.st_priv_key, secret, HEXKEYBYTES); 3807c478bd9Sstevel@tonic-gate netst.st_netname = netname; 3817c478bd9Sstevel@tonic-gate 3827c478bd9Sstevel@tonic-gate #ifdef NFS_AUTH 3837c478bd9Sstevel@tonic-gate nra.authtype = AUTH_DES; /* only revoke DES creds */ 3847c478bd9Sstevel@tonic-gate nra.uid = getuid(); /* use the real uid */ 3857c478bd9Sstevel@tonic-gate if (_nfssys(NFS_REVAUTH, &nra) < 0) { 3867c478bd9Sstevel@tonic-gate perror("Warning: NFS credentials not destroyed"); 3877c478bd9Sstevel@tonic-gate err = 1; 3887c478bd9Sstevel@tonic-gate } 3897c478bd9Sstevel@tonic-gate #endif 3907c478bd9Sstevel@tonic-gate 3917c478bd9Sstevel@tonic-gate 3927c478bd9Sstevel@tonic-gate /* do actual key login */ 3937c478bd9Sstevel@tonic-gate if (key_setnet(&netst) < 0) { 3947c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 3957c478bd9Sstevel@tonic-gate "Could not set %s's secret key\n", netname); 3967c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "May be the keyserv is down?\n"); 3977c478bd9Sstevel@tonic-gate return (0); 3987c478bd9Sstevel@tonic-gate } 3997c478bd9Sstevel@tonic-gate 4007c478bd9Sstevel@tonic-gate return (1); 4017c478bd9Sstevel@tonic-gate } 4027c478bd9Sstevel@tonic-gate 4037c478bd9Sstevel@tonic-gate nis_object * 4047c478bd9Sstevel@tonic-gate init_entry() 4057c478bd9Sstevel@tonic-gate { 4067c478bd9Sstevel@tonic-gate static nis_object obj; 4077c478bd9Sstevel@tonic-gate static entry_col cred_data[10]; 4087c478bd9Sstevel@tonic-gate entry_obj *eo; 4097c478bd9Sstevel@tonic-gate 4107c478bd9Sstevel@tonic-gate memset((char *)(&obj), 0, sizeof (obj)); 4117c478bd9Sstevel@tonic-gate memset((char *)(cred_data), 0, sizeof (entry_col) * 10); 4127c478bd9Sstevel@tonic-gate 4137c478bd9Sstevel@tonic-gate obj.zo_name = "cred"; 4147c478bd9Sstevel@tonic-gate obj.zo_group = ""; 4157c478bd9Sstevel@tonic-gate obj.zo_ttl = 43200; 4167c478bd9Sstevel@tonic-gate obj.zo_data.zo_type = NIS_ENTRY_OBJ; 4177c478bd9Sstevel@tonic-gate eo = &(obj.EN_data); 4187c478bd9Sstevel@tonic-gate eo->en_type = "cred_tbl"; 4197c478bd9Sstevel@tonic-gate eo->en_cols.en_cols_val = cred_data; 4207c478bd9Sstevel@tonic-gate eo->en_cols.en_cols_len = 5; 4217c478bd9Sstevel@tonic-gate cred_data[4].ec_flags |= EN_CRYPT; 4227c478bd9Sstevel@tonic-gate return (&obj); 4237c478bd9Sstevel@tonic-gate } 4247c478bd9Sstevel@tonic-gate 4257c478bd9Sstevel@tonic-gate 4267c478bd9Sstevel@tonic-gate static char *attrFilter[] = { 4277c478bd9Sstevel@tonic-gate "objectclass", 4287c478bd9Sstevel@tonic-gate "nispublickey", 4297c478bd9Sstevel@tonic-gate "nissecretkey", 4307c478bd9Sstevel@tonic-gate (char *)NULL 4317c478bd9Sstevel@tonic-gate }; 4327c478bd9Sstevel@tonic-gate 4337c478bd9Sstevel@tonic-gate 4347c478bd9Sstevel@tonic-gate /* Determines if there is a NisKeyObject objectclass in a given entry */ 4357c478bd9Sstevel@tonic-gate static int 4367c478bd9Sstevel@tonic-gate ldap_keyobj_exist(ns_ldap_entry_t *entry) 4377c478bd9Sstevel@tonic-gate { 4387c478bd9Sstevel@tonic-gate char **fattrs; 4397c478bd9Sstevel@tonic-gate 4407c478bd9Sstevel@tonic-gate fattrs = __ns_ldap_getAttr(entry, "objectClass"); 4417c478bd9Sstevel@tonic-gate 4427c478bd9Sstevel@tonic-gate if (fattrs == NULL) 4437c478bd9Sstevel@tonic-gate return (1); 4447c478bd9Sstevel@tonic-gate 4457c478bd9Sstevel@tonic-gate while (*fattrs) { 4467c478bd9Sstevel@tonic-gate if (strcasecmp("NisKeyObject", *fattrs) == 0) 4477c478bd9Sstevel@tonic-gate return (1); 4487c478bd9Sstevel@tonic-gate fattrs++; 4497c478bd9Sstevel@tonic-gate } 4507c478bd9Sstevel@tonic-gate 4517c478bd9Sstevel@tonic-gate return (0); 4527c478bd9Sstevel@tonic-gate } 4537c478bd9Sstevel@tonic-gate 4547c478bd9Sstevel@tonic-gate 4557c478bd9Sstevel@tonic-gate static char *keyAttrs[] = { 4567c478bd9Sstevel@tonic-gate "nispublickey", 4577c478bd9Sstevel@tonic-gate "nissecretkey", 4587c478bd9Sstevel@tonic-gate NULL 4597c478bd9Sstevel@tonic-gate }; 4607c478bd9Sstevel@tonic-gate 4617c478bd9Sstevel@tonic-gate /* 4627c478bd9Sstevel@tonic-gate * Replace or append new attribute value(s) to an attribute. 4637c478bd9Sstevel@tonic-gate * Don't care about memory leaks, because program is short running. 4647c478bd9Sstevel@tonic-gate */ 4657c478bd9Sstevel@tonic-gate 4667c478bd9Sstevel@tonic-gate static int 4677c478bd9Sstevel@tonic-gate ldap_attr_mod(ns_ldap_entry_t *entry, 4687c478bd9Sstevel@tonic-gate char *mechname, 4697c478bd9Sstevel@tonic-gate char *public, 4707c478bd9Sstevel@tonic-gate ns_ldap_attr_t **pkeyattrs, 4717c478bd9Sstevel@tonic-gate char *crypt, 4727c478bd9Sstevel@tonic-gate ns_ldap_attr_t **ckeyattrs) 4737c478bd9Sstevel@tonic-gate { 4747c478bd9Sstevel@tonic-gate char **alist[2]; 4757c478bd9Sstevel@tonic-gate char *keys[2]; 4767c478bd9Sstevel@tonic-gate 4777c478bd9Sstevel@tonic-gate char *mechfilter; 4787c478bd9Sstevel@tonic-gate int mechfilterlen; 4797c478bd9Sstevel@tonic-gate int q = 0; 4807c478bd9Sstevel@tonic-gate int i, j; 4817c478bd9Sstevel@tonic-gate int keycount[] = {0, 0}; 4827c478bd9Sstevel@tonic-gate ns_ldap_attr_t *attrs; 4837c478bd9Sstevel@tonic-gate 4847c478bd9Sstevel@tonic-gate keys[0] = public; 4857c478bd9Sstevel@tonic-gate keys[1] = crypt; 4867c478bd9Sstevel@tonic-gate 4877c478bd9Sstevel@tonic-gate mechfilter = (char *)malloc(strlen(mechname) + 3); 4887c478bd9Sstevel@tonic-gate if (mechfilter == NULL) 4897c478bd9Sstevel@tonic-gate return (0); 4907c478bd9Sstevel@tonic-gate sprintf(mechfilter, "{%s}", mechname); 4917c478bd9Sstevel@tonic-gate mechfilterlen = strlen(mechfilter); 4927c478bd9Sstevel@tonic-gate 4937c478bd9Sstevel@tonic-gate for (q = 0; keyAttrs[q] != NULL; q++) { 4947c478bd9Sstevel@tonic-gate int found = 0; 4957c478bd9Sstevel@tonic-gate 4967c478bd9Sstevel@tonic-gate for (i = 0; i < entry->attr_count; i++) { 4977c478bd9Sstevel@tonic-gate int rep = 0; 4987c478bd9Sstevel@tonic-gate ns_ldap_attr_t *attr = entry->attr_pair[i]; 4997c478bd9Sstevel@tonic-gate char *name = attr->attrname; 5007c478bd9Sstevel@tonic-gate int count = 0; 5017c478bd9Sstevel@tonic-gate 5027c478bd9Sstevel@tonic-gate if (strcasecmp(keyAttrs[q], name) == 0) { 5037c478bd9Sstevel@tonic-gate found++; 5047c478bd9Sstevel@tonic-gate count = attr->value_count; 5057c478bd9Sstevel@tonic-gate alist[q] = (char **)malloc(sizeof (char *) * (count + 1)); 5067c478bd9Sstevel@tonic-gate if (alist[q] == NULL) 5077c478bd9Sstevel@tonic-gate return (0); 5087c478bd9Sstevel@tonic-gate alist[q][attr->value_count] = NULL; 5097c478bd9Sstevel@tonic-gate for (j = 0; j < attr->value_count; j++) { 5107c478bd9Sstevel@tonic-gate char *val = attr->attrvalue[j]; 5117c478bd9Sstevel@tonic-gate if (strncasecmp(val, mechfilter, 5127c478bd9Sstevel@tonic-gate mechfilterlen) == 0) { 5137c478bd9Sstevel@tonic-gate /* Replace entry */ 5147c478bd9Sstevel@tonic-gate rep++; 5157c478bd9Sstevel@tonic-gate alist[q][j] = keys[q]; 5167c478bd9Sstevel@tonic-gate } else 5177c478bd9Sstevel@tonic-gate alist[q][j] = val; 5187c478bd9Sstevel@tonic-gate ++keycount[q]; 5197c478bd9Sstevel@tonic-gate } 5207c478bd9Sstevel@tonic-gate if (!rep) { 5217c478bd9Sstevel@tonic-gate /* Add entry to list */ 5227c478bd9Sstevel@tonic-gate alist[q] = (char **)realloc(alist[q], 5237c478bd9Sstevel@tonic-gate sizeof (char *) * (count + 2)); 5247c478bd9Sstevel@tonic-gate if (alist[q] == NULL) 5257c478bd9Sstevel@tonic-gate return (0); 5267c478bd9Sstevel@tonic-gate alist[q][attr->value_count + 1] = NULL; 5277c478bd9Sstevel@tonic-gate alist[q][attr->value_count] = keys[q]; 5287c478bd9Sstevel@tonic-gate ++keycount[q]; 5297c478bd9Sstevel@tonic-gate } 5307c478bd9Sstevel@tonic-gate } 5317c478bd9Sstevel@tonic-gate } 5327c478bd9Sstevel@tonic-gate if (!found) { 5337c478bd9Sstevel@tonic-gate /* Attribute does not exist, add entry anyways */ 5347c478bd9Sstevel@tonic-gate alist[q] = (char **)malloc(sizeof (char *) * 2); 5357c478bd9Sstevel@tonic-gate if (alist[q] == NULL) 5367c478bd9Sstevel@tonic-gate return (0); 5377c478bd9Sstevel@tonic-gate alist[q][0] = keys[q]; 5387c478bd9Sstevel@tonic-gate alist[q][1] = NULL; 5397c478bd9Sstevel@tonic-gate ++keycount[q]; 5407c478bd9Sstevel@tonic-gate } 5417c478bd9Sstevel@tonic-gate } 5427c478bd9Sstevel@tonic-gate if ((attrs = (ns_ldap_attr_t *)calloc(1, 5437c478bd9Sstevel@tonic-gate sizeof (ns_ldap_attr_t))) == NULL) 5447c478bd9Sstevel@tonic-gate return (0); 5457c478bd9Sstevel@tonic-gate attrs->attrname = "nisPublicKey"; 5467c478bd9Sstevel@tonic-gate attrs->attrvalue = alist[0]; 5477c478bd9Sstevel@tonic-gate attrs->value_count = keycount[0]; 5487c478bd9Sstevel@tonic-gate *pkeyattrs = attrs; 5497c478bd9Sstevel@tonic-gate 5507c478bd9Sstevel@tonic-gate if ((attrs = (ns_ldap_attr_t *)calloc(1, 5517c478bd9Sstevel@tonic-gate sizeof (ns_ldap_attr_t))) == NULL) 5527c478bd9Sstevel@tonic-gate return (0); 5537c478bd9Sstevel@tonic-gate attrs->attrname = "nisSecretKey"; 5547c478bd9Sstevel@tonic-gate attrs->attrvalue = alist[1]; 5557c478bd9Sstevel@tonic-gate attrs->value_count = keycount[1]; 5567c478bd9Sstevel@tonic-gate *ckeyattrs = attrs; 5577c478bd9Sstevel@tonic-gate return (1); 5587c478bd9Sstevel@tonic-gate } 5597c478bd9Sstevel@tonic-gate 5607c478bd9Sstevel@tonic-gate 5617c478bd9Sstevel@tonic-gate /* 5627c478bd9Sstevel@tonic-gate * Do the actual Add or update of attributes in attrs. 5637c478bd9Sstevel@tonic-gate * The parameter 'update4host' is a flag that tells the function which 5647c478bd9Sstevel@tonic-gate * DN and password should be used to bind to ldap. If it is an update 5657c478bd9Sstevel@tonic-gate * for a host (update4host > 0), the two parameters "bindDN" and 5667c478bd9Sstevel@tonic-gate * "bindPasswd" would be used to bind as the directory manager, 5677c478bd9Sstevel@tonic-gate * otherwise "dn" and "passwd" would be used to bind as an individual 5687c478bd9Sstevel@tonic-gate * user. 5697c478bd9Sstevel@tonic-gate */ 5707c478bd9Sstevel@tonic-gate static void 5717c478bd9Sstevel@tonic-gate update_ldap_attr(const char *dn, 5727c478bd9Sstevel@tonic-gate ns_ldap_attr_t **attrs, 5737c478bd9Sstevel@tonic-gate const char *passwd, 5747c478bd9Sstevel@tonic-gate int add, 5757c478bd9Sstevel@tonic-gate int update4host, 5767c478bd9Sstevel@tonic-gate const char *bindDN, 5777c478bd9Sstevel@tonic-gate const char *bindPasswd) 5787c478bd9Sstevel@tonic-gate { 5797c478bd9Sstevel@tonic-gate int ldaprc; 5807c478bd9Sstevel@tonic-gate int authstried = 0; 5817c478bd9Sstevel@tonic-gate char *msg; 5827c478bd9Sstevel@tonic-gate char *ldap_pw; 5837c478bd9Sstevel@tonic-gate char **certpath = NULL; 5847c478bd9Sstevel@tonic-gate ns_auth_t **app; 5857c478bd9Sstevel@tonic-gate ns_auth_t **authpp = NULL; 5867c478bd9Sstevel@tonic-gate ns_auth_t *authp = NULL; 5877c478bd9Sstevel@tonic-gate ns_cred_t *credp; 5887c478bd9Sstevel@tonic-gate ns_ldap_error_t *errorp = NULL; 5897c478bd9Sstevel@tonic-gate int status; 5907c478bd9Sstevel@tonic-gate 5917c478bd9Sstevel@tonic-gate if ((credp = (ns_cred_t *)calloc(1, sizeof (ns_cred_t))) == NULL) { 5927c478bd9Sstevel@tonic-gate fprintf(stderr, "Can not allocate cred buffer.\n"); 5937c478bd9Sstevel@tonic-gate goto out; 5947c478bd9Sstevel@tonic-gate } 5957c478bd9Sstevel@tonic-gate 5967c478bd9Sstevel@tonic-gate /* 5977c478bd9Sstevel@tonic-gate * if this is an update for host, use the bindDN from the 5987c478bd9Sstevel@tonic-gate * command prompt, otherwise use user's DN directly. 5997c478bd9Sstevel@tonic-gate */ 6007c478bd9Sstevel@tonic-gate if (update4host) 6017c478bd9Sstevel@tonic-gate credp->cred.unix_cred.userID = strdup(bindDN); 6027c478bd9Sstevel@tonic-gate else 6037c478bd9Sstevel@tonic-gate credp->cred.unix_cred.userID = strdup(dn); 6047c478bd9Sstevel@tonic-gate 6057c478bd9Sstevel@tonic-gate if (credp->cred.unix_cred.userID == NULL) { 6067c478bd9Sstevel@tonic-gate fprintf(stderr, "Memory allocation failure (userID)\n"); 6077c478bd9Sstevel@tonic-gate goto out; 6087c478bd9Sstevel@tonic-gate } 6097c478bd9Sstevel@tonic-gate 6107c478bd9Sstevel@tonic-gate if (update4host) { 6117c478bd9Sstevel@tonic-gate credp->cred.unix_cred.passwd = strdup(bindPasswd); 6127c478bd9Sstevel@tonic-gate } else { 6137c478bd9Sstevel@tonic-gate if (passwd) 6147c478bd9Sstevel@tonic-gate credp->cred.unix_cred.passwd = strdup(passwd); 6157c478bd9Sstevel@tonic-gate else { 6167c478bd9Sstevel@tonic-gate /* Make sure a valid password is received. */ 6177c478bd9Sstevel@tonic-gate status = get_ldap_bindPassword(&ldap_pw); 6187c478bd9Sstevel@tonic-gate 6197c478bd9Sstevel@tonic-gate if (status != PROMPTGET_SUCCESS) { 6207c478bd9Sstevel@tonic-gate if (!ldap_pw) 6217c478bd9Sstevel@tonic-gate free(ldap_pw); 6227c478bd9Sstevel@tonic-gate goto out; 6237c478bd9Sstevel@tonic-gate } 6247c478bd9Sstevel@tonic-gate credp->cred.unix_cred.passwd = ldap_pw; 6257c478bd9Sstevel@tonic-gate } 6267c478bd9Sstevel@tonic-gate } 6277c478bd9Sstevel@tonic-gate 6287c478bd9Sstevel@tonic-gate if (credp->cred.unix_cred.passwd == NULL) { 6297c478bd9Sstevel@tonic-gate fprintf(stderr, "Memory allocation failure (passwd)\n"); 6307c478bd9Sstevel@tonic-gate goto out; 6317c478bd9Sstevel@tonic-gate } 6327c478bd9Sstevel@tonic-gate 6337c478bd9Sstevel@tonic-gate /* get host certificate path, if one is configured */ 6347c478bd9Sstevel@tonic-gate if (__ns_ldap_getParam(NS_LDAP_HOST_CERTPATH_P, 6357c478bd9Sstevel@tonic-gate (void ***)&certpath, &errorp) != NS_LDAP_SUCCESS) 6367c478bd9Sstevel@tonic-gate goto out; 6377c478bd9Sstevel@tonic-gate 6387c478bd9Sstevel@tonic-gate if (certpath && *certpath) 6397c478bd9Sstevel@tonic-gate credp->hostcertpath = *certpath; 6407c478bd9Sstevel@tonic-gate 6417c478bd9Sstevel@tonic-gate /* Load the service specific authentication method */ 6427c478bd9Sstevel@tonic-gate if (__ns_ldap_getServiceAuthMethods("keyserv", &authpp, &errorp) != 6437c478bd9Sstevel@tonic-gate NS_LDAP_SUCCESS) 6447c478bd9Sstevel@tonic-gate goto out; 6457c478bd9Sstevel@tonic-gate 6467c478bd9Sstevel@tonic-gate /* 6477c478bd9Sstevel@tonic-gate * if authpp is null, there is no serviceAuthenticationMethod 6487c478bd9Sstevel@tonic-gate * try default authenticationMethod 6497c478bd9Sstevel@tonic-gate */ 6507c478bd9Sstevel@tonic-gate if (authpp == NULL) { 6517c478bd9Sstevel@tonic-gate if (__ns_ldap_getParam(NS_LDAP_AUTH_P, (void ***)&authpp, 6527c478bd9Sstevel@tonic-gate &errorp) != NS_LDAP_SUCCESS) 6537c478bd9Sstevel@tonic-gate goto out; 6547c478bd9Sstevel@tonic-gate } 6557c478bd9Sstevel@tonic-gate 6567c478bd9Sstevel@tonic-gate /* 6577c478bd9Sstevel@tonic-gate * if authpp is still null, then can not authenticate, log 6587c478bd9Sstevel@tonic-gate * error message and return error 6597c478bd9Sstevel@tonic-gate */ 6607c478bd9Sstevel@tonic-gate if (authpp == NULL) { 6617c478bd9Sstevel@tonic-gate fprintf(stderr, "No LDAP authentication method configured.\n" 6627c478bd9Sstevel@tonic-gate " configured.\n"); 6637c478bd9Sstevel@tonic-gate goto out; 6647c478bd9Sstevel@tonic-gate } 6657c478bd9Sstevel@tonic-gate 6667c478bd9Sstevel@tonic-gate /* 6677c478bd9Sstevel@tonic-gate * Walk the array and try all authentication methods in order except 6687c478bd9Sstevel@tonic-gate * for "none". 6697c478bd9Sstevel@tonic-gate */ 6707c478bd9Sstevel@tonic-gate for (app = authpp; *app; app++) { 6717c478bd9Sstevel@tonic-gate authp = *app; 6727c478bd9Sstevel@tonic-gate /* what about disabling other mechanisms? "tls:sasl/EXTERNAL" */ 6737c478bd9Sstevel@tonic-gate if (authp->type == NS_LDAP_AUTH_NONE) 6747c478bd9Sstevel@tonic-gate continue; 6757c478bd9Sstevel@tonic-gate authstried++; 6767c478bd9Sstevel@tonic-gate credp->auth.type = authp->type; 6777c478bd9Sstevel@tonic-gate credp->auth.tlstype = authp->tlstype; 6787c478bd9Sstevel@tonic-gate credp->auth.saslmech = authp->saslmech; 6797c478bd9Sstevel@tonic-gate credp->auth.saslopt = authp->saslopt; 6807c478bd9Sstevel@tonic-gate 6817c478bd9Sstevel@tonic-gate if (add == TRUE) 6827c478bd9Sstevel@tonic-gate ldaprc = __ns_ldap_addAttr("publickey", dn, 6837c478bd9Sstevel@tonic-gate (const ns_ldap_attr_t * const *)attrs, 6847c478bd9Sstevel@tonic-gate credp, NULL, &errorp); 6857c478bd9Sstevel@tonic-gate else 6867c478bd9Sstevel@tonic-gate ldaprc = __ns_ldap_repAttr("publickey", dn, 6877c478bd9Sstevel@tonic-gate (const ns_ldap_attr_t * const *)attrs, 6887c478bd9Sstevel@tonic-gate credp, NULL, &errorp); 6897c478bd9Sstevel@tonic-gate if (ldaprc == NS_LDAP_SUCCESS) { 6907c478bd9Sstevel@tonic-gate /* clean up ns_cred_t structure in memory */ 6917c478bd9Sstevel@tonic-gate if (credp != NULL) 6927c478bd9Sstevel@tonic-gate (void) __ns_ldap_freeCred(&credp); 6937c478bd9Sstevel@tonic-gate return; 6947c478bd9Sstevel@tonic-gate } 6957c478bd9Sstevel@tonic-gate 6967c478bd9Sstevel@tonic-gate /* XXX add checking for cases of authentication errors */ 6977c478bd9Sstevel@tonic-gate if ((ldaprc == NS_LDAP_INTERNAL) && 6987c478bd9Sstevel@tonic-gate ((errorp->status == LDAP_INAPPROPRIATE_AUTH) || 6997c478bd9Sstevel@tonic-gate (errorp->status == LDAP_INVALID_CREDENTIALS))) { 7007c478bd9Sstevel@tonic-gate fprintf(stderr, "LDAP authentication failed.\n"); 7017c478bd9Sstevel@tonic-gate goto out; 7027c478bd9Sstevel@tonic-gate } 7037c478bd9Sstevel@tonic-gate } 7047c478bd9Sstevel@tonic-gate if (authstried == 0) 7057c478bd9Sstevel@tonic-gate fprintf(stderr, "No legal authentication method configured.\n"); 7067c478bd9Sstevel@tonic-gate 7077c478bd9Sstevel@tonic-gate out: 7087c478bd9Sstevel@tonic-gate /* clean up ns_cred_t structure in memory */ 7097c478bd9Sstevel@tonic-gate if (credp != NULL) { 7107c478bd9Sstevel@tonic-gate (void) __ns_ldap_freeCred(&credp); 7117c478bd9Sstevel@tonic-gate } 7127c478bd9Sstevel@tonic-gate 7137c478bd9Sstevel@tonic-gate if (errorp) { 7147c478bd9Sstevel@tonic-gate __ns_ldap_err2str(errorp->status, &msg); 7157c478bd9Sstevel@tonic-gate fprintf(stderr, "LDAP error: %s.\n", msg); 7167c478bd9Sstevel@tonic-gate } 7177c478bd9Sstevel@tonic-gate fprintf(stderr, "%s: key-pair(s) unchanged.\n", program_name); 7187c478bd9Sstevel@tonic-gate exit(1); 7197c478bd9Sstevel@tonic-gate } 7207c478bd9Sstevel@tonic-gate 7217c478bd9Sstevel@tonic-gate 7227c478bd9Sstevel@tonic-gate /* 7237c478bd9Sstevel@tonic-gate * Update LDAP nisplublickey entry with new key information via SLDAP. 7247c478bd9Sstevel@tonic-gate * Free and clean up memory that stores credential data soon after 7257c478bd9Sstevel@tonic-gate * they are not used or an error comes up. 7267c478bd9Sstevel@tonic-gate */ 7277c478bd9Sstevel@tonic-gate int 7287c478bd9Sstevel@tonic-gate ldap_update(char *mechname, 7297c478bd9Sstevel@tonic-gate char *netname, 7307c478bd9Sstevel@tonic-gate char *public, 7317c478bd9Sstevel@tonic-gate char *crypt, 7327c478bd9Sstevel@tonic-gate char *passwd) 7337c478bd9Sstevel@tonic-gate { 7347c478bd9Sstevel@tonic-gate char *netnamecpy; 7357c478bd9Sstevel@tonic-gate char *id; 7367c478bd9Sstevel@tonic-gate char *domain; 7377c478bd9Sstevel@tonic-gate char *dn; 7387c478bd9Sstevel@tonic-gate char *db; 7397c478bd9Sstevel@tonic-gate char *filter; 7407c478bd9Sstevel@tonic-gate ns_ldap_error_t *errorp; 7417c478bd9Sstevel@tonic-gate char *pkeyatval, *ckeyatval; 7427c478bd9Sstevel@tonic-gate ns_ldap_result_t *res; 7437c478bd9Sstevel@tonic-gate ns_ldap_attr_t *pattrs, *cattrs; 7447c478bd9Sstevel@tonic-gate int update4host = FALSE; 7457c478bd9Sstevel@tonic-gate char *bindDN = NULL; 7467c478bd9Sstevel@tonic-gate char *bindPasswd = NULL; 7477c478bd9Sstevel@tonic-gate int status; 7487c478bd9Sstevel@tonic-gate 7497c478bd9Sstevel@tonic-gate /* Generate DN */ 7507c478bd9Sstevel@tonic-gate if ((netnamecpy = strdup(netname)) == NULL) 7517c478bd9Sstevel@tonic-gate return (0); 7527c478bd9Sstevel@tonic-gate if (((id = strchr(netnamecpy, '.')) == NULL) || 7537c478bd9Sstevel@tonic-gate ((domain = strchr(netnamecpy, '@')) == NULL)) 7547c478bd9Sstevel@tonic-gate return (0); 7557c478bd9Sstevel@tonic-gate else { 7567c478bd9Sstevel@tonic-gate *domain++ = '\0'; 7577c478bd9Sstevel@tonic-gate *id++ = '\0'; 7587c478bd9Sstevel@tonic-gate 7597c478bd9Sstevel@tonic-gate id = strdup(id); 7607c478bd9Sstevel@tonic-gate if (id == NULL) { 7617c478bd9Sstevel@tonic-gate free(netnamecpy); 7627c478bd9Sstevel@tonic-gate fprintf(stderr, "LDAP memory error (id)\n"); 7637c478bd9Sstevel@tonic-gate return (0); 7647c478bd9Sstevel@tonic-gate } 7657c478bd9Sstevel@tonic-gate domain = strdup(domain); 7667c478bd9Sstevel@tonic-gate if (domain == NULL) { 7677c478bd9Sstevel@tonic-gate free(netnamecpy); 7687c478bd9Sstevel@tonic-gate free(id); 7697c478bd9Sstevel@tonic-gate fprintf(stderr, "LDAP memory error (domain)\n"); 7707c478bd9Sstevel@tonic-gate return (0); 7717c478bd9Sstevel@tonic-gate } 7727c478bd9Sstevel@tonic-gate free(netnamecpy); 7737c478bd9Sstevel@tonic-gate } 7747c478bd9Sstevel@tonic-gate 7757c478bd9Sstevel@tonic-gate if (isdigit(*id)) { 7767c478bd9Sstevel@tonic-gate /* We be user. */ 7777c478bd9Sstevel@tonic-gate __ns_ldap_uid2dn(id, &dn, NULL, &errorp); 7787c478bd9Sstevel@tonic-gate if (dn == NULL) { 7797c478bd9Sstevel@tonic-gate fprintf(stderr, "Could not obtain LDAP dn\n"); 7807c478bd9Sstevel@tonic-gate fprintf(stderr, "%s: key-pair(s) unchanged.\n", 7817c478bd9Sstevel@tonic-gate program_name); 7827c478bd9Sstevel@tonic-gate exit(1); 7837c478bd9Sstevel@tonic-gate } 7847c478bd9Sstevel@tonic-gate db = "passwd"; 7857c478bd9Sstevel@tonic-gate filter = (char *)malloc(strlen(id) + 13); 7867c478bd9Sstevel@tonic-gate if (filter) 7877c478bd9Sstevel@tonic-gate sprintf(filter, "(uidnumber=%s)", id); 7887c478bd9Sstevel@tonic-gate else { 7897c478bd9Sstevel@tonic-gate fprintf(stderr, "Can not allocate filter buffer.\n"); 7907c478bd9Sstevel@tonic-gate fprintf(stderr, "%s: key-pair(s) unchanged.\n", 7917c478bd9Sstevel@tonic-gate program_name); 7927c478bd9Sstevel@tonic-gate exit(1); 7937c478bd9Sstevel@tonic-gate } 7947c478bd9Sstevel@tonic-gate } else { 7957c478bd9Sstevel@tonic-gate /* We be host. */ 7967c478bd9Sstevel@tonic-gate update4host = TRUE; 7977c478bd9Sstevel@tonic-gate 7987c478bd9Sstevel@tonic-gate __ns_ldap_host2dn(id, NULL, &dn, NULL, &errorp); 7997c478bd9Sstevel@tonic-gate if (dn == NULL) { 8007c478bd9Sstevel@tonic-gate fprintf(stderr, "Could not obtain LDAP dn\n"); 8017c478bd9Sstevel@tonic-gate fprintf(stderr, "%s: key-pair(s) unchanged.\n", 8027c478bd9Sstevel@tonic-gate program_name); 8037c478bd9Sstevel@tonic-gate exit(1); 8047c478bd9Sstevel@tonic-gate } 8057c478bd9Sstevel@tonic-gate 8067c478bd9Sstevel@tonic-gate db = "hosts"; 8077c478bd9Sstevel@tonic-gate filter = (char *)malloc(strlen(id) + 6); 8087c478bd9Sstevel@tonic-gate if (filter) 8097c478bd9Sstevel@tonic-gate sprintf(filter, "(cn=%s)", id); 8107c478bd9Sstevel@tonic-gate else { 8117c478bd9Sstevel@tonic-gate fprintf(stderr, "Can not allocate filter buffer.\n"); 8127c478bd9Sstevel@tonic-gate fprintf(stderr, "%s: key-pair(s) unchanged.\n", 8137c478bd9Sstevel@tonic-gate program_name); 8147c478bd9Sstevel@tonic-gate exit(1); 8157c478bd9Sstevel@tonic-gate } 8167c478bd9Sstevel@tonic-gate 8177c478bd9Sstevel@tonic-gate /* Prompt for ldap bind DN for entry udpates */ 8187c478bd9Sstevel@tonic-gate status = get_ldap_bindDN(&bindDN); 8197c478bd9Sstevel@tonic-gate 8207c478bd9Sstevel@tonic-gate if (status != PROMPTGET_SUCCESS) { 8217c478bd9Sstevel@tonic-gate FREE_CREDINFO(bindDN); 8227c478bd9Sstevel@tonic-gate fprintf(stderr, 8237c478bd9Sstevel@tonic-gate "Failed to get a valid LDAP bind DN.\n" 8247c478bd9Sstevel@tonic-gate "%s: key-pair(s) unchanged.\n", 8257c478bd9Sstevel@tonic-gate program_name); 8267c478bd9Sstevel@tonic-gate exit(1); 8277c478bd9Sstevel@tonic-gate } 8287c478bd9Sstevel@tonic-gate 8297c478bd9Sstevel@tonic-gate /* Prompt for ldap bind password */ 8307c478bd9Sstevel@tonic-gate status = get_ldap_bindPassword(&bindPasswd); 8317c478bd9Sstevel@tonic-gate 8327c478bd9Sstevel@tonic-gate if (status != PROMPTGET_SUCCESS) { 8337c478bd9Sstevel@tonic-gate FREE_CREDINFO(bindPasswd); 8347c478bd9Sstevel@tonic-gate FREE_CREDINFO(bindDN); 8357c478bd9Sstevel@tonic-gate 8367c478bd9Sstevel@tonic-gate fprintf(stderr, 8377c478bd9Sstevel@tonic-gate "Failed to get a valid LDAP bind password." 8387c478bd9Sstevel@tonic-gate "\n%s: key-pair(s) unchanged.\n", 8397c478bd9Sstevel@tonic-gate program_name); 8407c478bd9Sstevel@tonic-gate exit(1); 8417c478bd9Sstevel@tonic-gate } 8427c478bd9Sstevel@tonic-gate } 8437c478bd9Sstevel@tonic-gate 8447c478bd9Sstevel@tonic-gate /* Construct attribute values */ 8457c478bd9Sstevel@tonic-gate pkeyatval = (char *)malloc(strlen(mechname) + strlen(public) + 3); 8467c478bd9Sstevel@tonic-gate if (pkeyatval == NULL) { 8477c478bd9Sstevel@tonic-gate FREE_CREDINFO(bindPasswd); 8487c478bd9Sstevel@tonic-gate FREE_CREDINFO(bindDN); 8497c478bd9Sstevel@tonic-gate fprintf(stderr, "LDAP memory error (pkeyatval)\n"); 8507c478bd9Sstevel@tonic-gate fprintf(stderr, "%s: key-pair(s) unchanged.\n", program_name); 8517c478bd9Sstevel@tonic-gate exit(1); 8527c478bd9Sstevel@tonic-gate } 8537c478bd9Sstevel@tonic-gate sprintf(pkeyatval, "{%s}%s", mechname, public); 8547c478bd9Sstevel@tonic-gate ckeyatval = (char *)malloc(strlen(mechname) + strlen(crypt) + 3); 8557c478bd9Sstevel@tonic-gate if (ckeyatval == NULL) { 8567c478bd9Sstevel@tonic-gate FREE_CREDINFO(pkeyatval); 8577c478bd9Sstevel@tonic-gate FREE_CREDINFO(bindPasswd); 8587c478bd9Sstevel@tonic-gate FREE_CREDINFO(bindDN); 8597c478bd9Sstevel@tonic-gate fprintf(stderr, "LDAP memory error (pkeyatval)\n"); 8607c478bd9Sstevel@tonic-gate fprintf(stderr, "%s: key-pair(s) unchanged.\n", program_name); 8617c478bd9Sstevel@tonic-gate exit(1); 8627c478bd9Sstevel@tonic-gate } 8637c478bd9Sstevel@tonic-gate sprintf(ckeyatval, "{%s}%s", mechname, crypt); 8647c478bd9Sstevel@tonic-gate 8657c478bd9Sstevel@tonic-gate /* Does entry exist? */ 8667c478bd9Sstevel@tonic-gate if ((__ns_ldap_list(db, filter, NULL, (const char **)attrFilter, 8677c478bd9Sstevel@tonic-gate NULL, 0, &res, &errorp, 8687c478bd9Sstevel@tonic-gate NULL, NULL) == NS_LDAP_SUCCESS) && res == NULL) { 8697c478bd9Sstevel@tonic-gate FREE_CREDINFO(ckeyatval); 8707c478bd9Sstevel@tonic-gate FREE_CREDINFO(pkeyatval); 8717c478bd9Sstevel@tonic-gate FREE_CREDINFO(bindPasswd); 8727c478bd9Sstevel@tonic-gate FREE_CREDINFO(bindDN); 8737c478bd9Sstevel@tonic-gate fprintf(stderr, "LDAP entry does not exist.\n"); 8747c478bd9Sstevel@tonic-gate fprintf(stderr, "%s: key-pair(s) unchanged.\n", program_name); 8757c478bd9Sstevel@tonic-gate exit(1); 8767c478bd9Sstevel@tonic-gate } 8777c478bd9Sstevel@tonic-gate 8787c478bd9Sstevel@tonic-gate /* Entry exists, modify attributes for public and secret keys */ 8797c478bd9Sstevel@tonic-gate 8807c478bd9Sstevel@tonic-gate /* Is there a NisKeyObject in entry? */ 8817c478bd9Sstevel@tonic-gate if (!ldap_keyobj_exist(&res->entry[0])) { 8827c478bd9Sstevel@tonic-gate /* Add NisKeyObject objectclass and the keys */ 8837c478bd9Sstevel@tonic-gate char **newattr; 8847c478bd9Sstevel@tonic-gate ns_ldap_attr_t *attrs[4]; /* objectclass, pk, sk, NULL */ 8857c478bd9Sstevel@tonic-gate 8867c478bd9Sstevel@tonic-gate /* set objectclass */ 8877c478bd9Sstevel@tonic-gate newattr = (char **)calloc(2, sizeof (char *)); 8887c478bd9Sstevel@tonic-gate newattr[0] = "NisKeyObject"; 8897c478bd9Sstevel@tonic-gate newattr[1] = NULL; 8907c478bd9Sstevel@tonic-gate if ((attrs[0] = (ns_ldap_attr_t *)calloc(1, 8917c478bd9Sstevel@tonic-gate sizeof (ns_ldap_attr_t))) == NULL) { 8927c478bd9Sstevel@tonic-gate FREE_CREDINFO(ckeyatval); 8937c478bd9Sstevel@tonic-gate FREE_CREDINFO(pkeyatval); 8947c478bd9Sstevel@tonic-gate FREE_CREDINFO(bindPasswd); 8957c478bd9Sstevel@tonic-gate FREE_CREDINFO(bindDN); 8967c478bd9Sstevel@tonic-gate fprintf(stderr, "Memory allocation failed\n"); 8977c478bd9Sstevel@tonic-gate fprintf(stderr, "%s: key-pair(s) unchanged.\n", 8987c478bd9Sstevel@tonic-gate program_name); 8997c478bd9Sstevel@tonic-gate exit(1); 9007c478bd9Sstevel@tonic-gate } 9017c478bd9Sstevel@tonic-gate attrs[0]->attrname = "objectClass"; 9027c478bd9Sstevel@tonic-gate attrs[0]->attrvalue = newattr; 9037c478bd9Sstevel@tonic-gate attrs[0]->value_count = 1; 9047c478bd9Sstevel@tonic-gate 9057c478bd9Sstevel@tonic-gate /* set publickey */ 9067c478bd9Sstevel@tonic-gate newattr = (char **)calloc(2, sizeof (char *)); 9077c478bd9Sstevel@tonic-gate newattr[0] = pkeyatval; 9087c478bd9Sstevel@tonic-gate newattr[1] = NULL; 9097c478bd9Sstevel@tonic-gate if ((attrs[1] = (ns_ldap_attr_t *)calloc(1, 9107c478bd9Sstevel@tonic-gate sizeof (ns_ldap_attr_t))) == NULL) { 9117c478bd9Sstevel@tonic-gate FREE_CREDINFO(ckeyatval); 9127c478bd9Sstevel@tonic-gate FREE_CREDINFO(pkeyatval); 9137c478bd9Sstevel@tonic-gate FREE_CREDINFO(bindPasswd); 9147c478bd9Sstevel@tonic-gate FREE_CREDINFO(bindDN); 9157c478bd9Sstevel@tonic-gate fprintf(stderr, "Memory allocation failed\n"); 9167c478bd9Sstevel@tonic-gate fprintf(stderr, "%s: key-pair(s) unchanged.\n", 9177c478bd9Sstevel@tonic-gate program_name); 9187c478bd9Sstevel@tonic-gate exit(1); 9197c478bd9Sstevel@tonic-gate } 9207c478bd9Sstevel@tonic-gate attrs[1]->attrname = "nisPublicKey"; 9217c478bd9Sstevel@tonic-gate attrs[1]->attrvalue = newattr; 9227c478bd9Sstevel@tonic-gate attrs[1]->value_count = 1; 9237c478bd9Sstevel@tonic-gate 9247c478bd9Sstevel@tonic-gate /* set privatekey */ 9257c478bd9Sstevel@tonic-gate newattr = (char **)calloc(2, sizeof (char *)); 9267c478bd9Sstevel@tonic-gate newattr[0] = ckeyatval; 9277c478bd9Sstevel@tonic-gate newattr[1] = NULL; 9287c478bd9Sstevel@tonic-gate if ((attrs[2] = (ns_ldap_attr_t *)calloc(1, 9297c478bd9Sstevel@tonic-gate sizeof (ns_ldap_attr_t))) == NULL) { 9307c478bd9Sstevel@tonic-gate FREE_CREDINFO(ckeyatval); 9317c478bd9Sstevel@tonic-gate FREE_CREDINFO(pkeyatval); 9327c478bd9Sstevel@tonic-gate FREE_CREDINFO(bindPasswd); 9337c478bd9Sstevel@tonic-gate FREE_CREDINFO(bindDN); 9347c478bd9Sstevel@tonic-gate fprintf(stderr, "Memory allocation failed\n"); 9357c478bd9Sstevel@tonic-gate fprintf(stderr, "%s: key-pair(s) unchanged.\n", 9367c478bd9Sstevel@tonic-gate program_name); 9377c478bd9Sstevel@tonic-gate exit(1); 9387c478bd9Sstevel@tonic-gate } 9397c478bd9Sstevel@tonic-gate attrs[2]->attrname = "nisSecretKey"; 9407c478bd9Sstevel@tonic-gate attrs[2]->attrvalue = newattr; 9417c478bd9Sstevel@tonic-gate attrs[2]->value_count = 1; 9427c478bd9Sstevel@tonic-gate 9437c478bd9Sstevel@tonic-gate /* terminator */ 9447c478bd9Sstevel@tonic-gate attrs[3] = NULL; 9457c478bd9Sstevel@tonic-gate 9467c478bd9Sstevel@tonic-gate update_ldap_attr(dn, attrs, passwd, TRUE, update4host, 9477c478bd9Sstevel@tonic-gate bindDN, bindPasswd); 9487c478bd9Sstevel@tonic-gate } else { 9497c478bd9Sstevel@tonic-gate /* object class already exists, replace keys */ 9507c478bd9Sstevel@tonic-gate ns_ldap_attr_t *attrs[4]; /* objectclass, pk, sk, NULL */ 9517c478bd9Sstevel@tonic-gate 9527c478bd9Sstevel@tonic-gate if (!ldap_attr_mod(&res->entry[0], mechname, 9537c478bd9Sstevel@tonic-gate pkeyatval, &pattrs, 9547c478bd9Sstevel@tonic-gate ckeyatval, &cattrs)) { 9557c478bd9Sstevel@tonic-gate FREE_CREDINFO(ckeyatval); 9567c478bd9Sstevel@tonic-gate FREE_CREDINFO(pkeyatval); 9577c478bd9Sstevel@tonic-gate FREE_CREDINFO(bindPasswd); 9587c478bd9Sstevel@tonic-gate FREE_CREDINFO(bindDN); 9597c478bd9Sstevel@tonic-gate fprintf(stderr, 9607c478bd9Sstevel@tonic-gate "Could not generate LDAP attribute list.\n"); 9617c478bd9Sstevel@tonic-gate fprintf(stderr, 9627c478bd9Sstevel@tonic-gate "%s: key-pair(s) unchanged.\n", program_name); 9637c478bd9Sstevel@tonic-gate exit(1); 9647c478bd9Sstevel@tonic-gate } 9657c478bd9Sstevel@tonic-gate 9667c478bd9Sstevel@tonic-gate attrs[0] = pattrs; 9677c478bd9Sstevel@tonic-gate attrs[1] = cattrs; 9687c478bd9Sstevel@tonic-gate attrs[2] = NULL; 9697c478bd9Sstevel@tonic-gate 9707c478bd9Sstevel@tonic-gate update_ldap_attr(dn, attrs, passwd, FALSE, update4host, 9717c478bd9Sstevel@tonic-gate bindDN, bindPasswd); 9727c478bd9Sstevel@tonic-gate } 9737c478bd9Sstevel@tonic-gate 9747c478bd9Sstevel@tonic-gate FREE_CREDINFO(ckeyatval); 9757c478bd9Sstevel@tonic-gate FREE_CREDINFO(pkeyatval); 9767c478bd9Sstevel@tonic-gate FREE_CREDINFO(bindPasswd); 9777c478bd9Sstevel@tonic-gate FREE_CREDINFO(bindDN); 9787c478bd9Sstevel@tonic-gate 9797c478bd9Sstevel@tonic-gate return (0); 9807c478bd9Sstevel@tonic-gate } 981