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 57d1e8394SAshok Kumar T * Common Development and Distribution License (the "License"). 67d1e8394SAshok Kumar T * 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 /* 227d1e8394SAshok Kumar T * 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 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */ 277c478bd9Sstevel@tonic-gate /* All Rights Reserved */ 287c478bd9Sstevel@tonic-gate 297c478bd9Sstevel@tonic-gate /* 307c478bd9Sstevel@tonic-gate * University Copyright- Copyright (c) 1982, 1986, 1988 317c478bd9Sstevel@tonic-gate * The Regents of the University of California 327c478bd9Sstevel@tonic-gate * All Rights Reserved 337c478bd9Sstevel@tonic-gate * 347c478bd9Sstevel@tonic-gate * University Acknowledgment- Portions of this document are derived from 357c478bd9Sstevel@tonic-gate * software developed by the University of California, Berkeley, and its 367c478bd9Sstevel@tonic-gate * contributors. 377c478bd9Sstevel@tonic-gate */ 387c478bd9Sstevel@tonic-gate 397c478bd9Sstevel@tonic-gate /* 407c478bd9Sstevel@tonic-gate * Administrative tool to add a new user to the publickey database 417c478bd9Sstevel@tonic-gate */ 427c478bd9Sstevel@tonic-gate #include <stdio.h> 437c478bd9Sstevel@tonic-gate #include <stdlib.h> 447c478bd9Sstevel@tonic-gate #include <rpc/rpc.h> 457c478bd9Sstevel@tonic-gate #include <rpc/key_prot.h> 467c478bd9Sstevel@tonic-gate #include <rpcsvc/ypclnt.h> 477c478bd9Sstevel@tonic-gate #include <sys/wait.h> 487c478bd9Sstevel@tonic-gate #include <netdb.h> 497c478bd9Sstevel@tonic-gate #include <pwd.h> 507c478bd9Sstevel@tonic-gate #include <shadow.h> 517c478bd9Sstevel@tonic-gate #include <crypt.h> 527c478bd9Sstevel@tonic-gate #include <string.h> 537c478bd9Sstevel@tonic-gate #include <sys/resource.h> 547c478bd9Sstevel@tonic-gate #include <netdir.h> 557c478bd9Sstevel@tonic-gate #include <rpcsvc/nis.h> 567c478bd9Sstevel@tonic-gate 577c478bd9Sstevel@tonic-gate #define MAXMAPNAMELEN 256 587c478bd9Sstevel@tonic-gate #define MAXPASSWD 256 /* max significant characters in password */ 597c478bd9Sstevel@tonic-gate 607c478bd9Sstevel@tonic-gate #define PK_FILES 1 617c478bd9Sstevel@tonic-gate #define PK_YP 2 627c478bd9Sstevel@tonic-gate #define PK_LDAP 4 63*36e852a1SRaja Andra #define DESCREDPASSLEN sizeof (des_block) 647c478bd9Sstevel@tonic-gate 657c478bd9Sstevel@tonic-gate extern int optind; 667c478bd9Sstevel@tonic-gate extern char *optarg; 677c478bd9Sstevel@tonic-gate extern int __getnetnamebyuid(); 687c478bd9Sstevel@tonic-gate extern int self_check(char *name); 697c478bd9Sstevel@tonic-gate 707c478bd9Sstevel@tonic-gate #define local_host(host_name) self_check(host_name) 717c478bd9Sstevel@tonic-gate 727c478bd9Sstevel@tonic-gate char *program_name; 737c478bd9Sstevel@tonic-gate int pk_database; 747c478bd9Sstevel@tonic-gate static char *get_password(); 757c478bd9Sstevel@tonic-gate static char *basename(); 767c478bd9Sstevel@tonic-gate static char SHELL[] = "/bin/sh"; 777c478bd9Sstevel@tonic-gate static char YPDBPATH[] = "/var/yp"; 787c478bd9Sstevel@tonic-gate static char PKMAP[] = "publickey.byname"; 797c478bd9Sstevel@tonic-gate static char UPDATEFILE[] = "updaters"; 807c478bd9Sstevel@tonic-gate static char PKFILE[] = "/etc/publickey"; 8149e7ca49Speteh static void usage(void); 827c478bd9Sstevel@tonic-gate 8349e7ca49Speteh int 8449e7ca49Speteh main(int argc, char *argv[]) 857c478bd9Sstevel@tonic-gate { 867c478bd9Sstevel@tonic-gate char name[MAXNETNAMELEN + 1]; 877c478bd9Sstevel@tonic-gate char public[HEXKEYBYTES + 1]; 887c478bd9Sstevel@tonic-gate char secret[HEXKEYBYTES + 1]; 897c478bd9Sstevel@tonic-gate char crypt1[HEXKEYBYTES + KEYCHECKSUMSIZE + 1]; 907c478bd9Sstevel@tonic-gate int status; 917c478bd9Sstevel@tonic-gate char *pass, *target_host = NULL, 927c478bd9Sstevel@tonic-gate *username = NULL, *pk_service = NULL; 937d1e8394SAshok Kumar T char short_pass[DESCREDPASSLEN + 1]; 947c478bd9Sstevel@tonic-gate struct passwd *pw; 957c478bd9Sstevel@tonic-gate NCONF_HANDLE *nc_handle; 967c478bd9Sstevel@tonic-gate struct netconfig *nconf; 977c478bd9Sstevel@tonic-gate struct nd_hostserv service; 987c478bd9Sstevel@tonic-gate struct nd_addrlist *addrs; 997c478bd9Sstevel@tonic-gate bool_t validhost; 1007c478bd9Sstevel@tonic-gate uid_t uid; 1017c478bd9Sstevel@tonic-gate int c; 1027c478bd9Sstevel@tonic-gate char host_pname[NIS_MAXNAMELEN]; 1037c478bd9Sstevel@tonic-gate 1047c478bd9Sstevel@tonic-gate program_name = argv[0]; 1057c478bd9Sstevel@tonic-gate while ((c = getopt(argc, argv, "s:u:h:")) != -1) { 1067c478bd9Sstevel@tonic-gate switch (c) { 1077c478bd9Sstevel@tonic-gate case 's': 1087c478bd9Sstevel@tonic-gate if (pk_service == NULL) 1097c478bd9Sstevel@tonic-gate pk_service = optarg; 1107c478bd9Sstevel@tonic-gate else 1117c478bd9Sstevel@tonic-gate usage(); 1127c478bd9Sstevel@tonic-gate break; 1137c478bd9Sstevel@tonic-gate case 'u': 1147c478bd9Sstevel@tonic-gate if (username || target_host) 1157c478bd9Sstevel@tonic-gate usage(); 1167c478bd9Sstevel@tonic-gate username = optarg; 1177c478bd9Sstevel@tonic-gate break; 1187c478bd9Sstevel@tonic-gate case 'h': 1197c478bd9Sstevel@tonic-gate if (username || target_host) 1207c478bd9Sstevel@tonic-gate usage(); 1217c478bd9Sstevel@tonic-gate target_host = optarg; 1227c478bd9Sstevel@tonic-gate break; 1237c478bd9Sstevel@tonic-gate default: 1247c478bd9Sstevel@tonic-gate usage(); 1257c478bd9Sstevel@tonic-gate } 1267c478bd9Sstevel@tonic-gate } 1277c478bd9Sstevel@tonic-gate 1287c478bd9Sstevel@tonic-gate if (optind < argc || (username == 0 && target_host == 0)) { 1297c478bd9Sstevel@tonic-gate usage(); 1307c478bd9Sstevel@tonic-gate } 1317c478bd9Sstevel@tonic-gate 1327c478bd9Sstevel@tonic-gate if ((pk_database = get_pk_source(pk_service)) == 0) 1337c478bd9Sstevel@tonic-gate usage(); 1347c478bd9Sstevel@tonic-gate 1357c478bd9Sstevel@tonic-gate if (geteuid() != 0) { 1367c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "Must be superuser to run %s\n", 1377c478bd9Sstevel@tonic-gate program_name); 1387c478bd9Sstevel@tonic-gate exit(1); 1397c478bd9Sstevel@tonic-gate } 1407c478bd9Sstevel@tonic-gate 1417c478bd9Sstevel@tonic-gate if (username) { 1427c478bd9Sstevel@tonic-gate pw = getpwnam(username); 1437c478bd9Sstevel@tonic-gate if (pw == NULL) { 1447c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "%s: unknown user: '%s'\n", 1457c478bd9Sstevel@tonic-gate program_name, username); 1467c478bd9Sstevel@tonic-gate exit(1); 1477c478bd9Sstevel@tonic-gate } 1487c478bd9Sstevel@tonic-gate uid = pw->pw_uid; 1497c478bd9Sstevel@tonic-gate if (uid == 0) { 1507c478bd9Sstevel@tonic-gate if (! getnetname(name)) { 1517c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 1527c478bd9Sstevel@tonic-gate "%s: could not get the equivalent netname for %s\n", 1537c478bd9Sstevel@tonic-gate program_name, username); 1547c478bd9Sstevel@tonic-gate usage(); 1557c478bd9Sstevel@tonic-gate } 1567c478bd9Sstevel@tonic-gate if (gethostname(host_pname, NIS_MAXNAMELEN) 1577c478bd9Sstevel@tonic-gate < 0) { 1587c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 1597c478bd9Sstevel@tonic-gate "%s: could not get the hostname for %s\n", 1607c478bd9Sstevel@tonic-gate program_name, username); 1617c478bd9Sstevel@tonic-gate usage(); 1627c478bd9Sstevel@tonic-gate } 1637c478bd9Sstevel@tonic-gate target_host = host_pname; 1647c478bd9Sstevel@tonic-gate } 1657c478bd9Sstevel@tonic-gate if (__getnetnamebyuid(name, uid) == 0) { 1667c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 1677c478bd9Sstevel@tonic-gate "%s: could not get the equivalent netname for %s\n", 1687c478bd9Sstevel@tonic-gate program_name, username); 1697c478bd9Sstevel@tonic-gate usage(); 1707c478bd9Sstevel@tonic-gate } 1717c478bd9Sstevel@tonic-gate } else { 1727c478bd9Sstevel@tonic-gate /* -h hostname option */ 1737c478bd9Sstevel@tonic-gate service.h_host = target_host; 1747c478bd9Sstevel@tonic-gate service.h_serv = NULL; 1757c478bd9Sstevel@tonic-gate validhost = FALSE; 1767c478bd9Sstevel@tonic-gate /* verify if this is a valid hostname */ 1777c478bd9Sstevel@tonic-gate nc_handle = setnetconfig(); 1787c478bd9Sstevel@tonic-gate if (nc_handle == NULL) { 1797c478bd9Sstevel@tonic-gate /* fails to open netconfig file */ 1807c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 1817c478bd9Sstevel@tonic-gate "%s: failed in routine setnetconfig()\n", 1827c478bd9Sstevel@tonic-gate program_name); 1837c478bd9Sstevel@tonic-gate exit(2); 1847c478bd9Sstevel@tonic-gate } 1857c478bd9Sstevel@tonic-gate while (nconf = getnetconfig(nc_handle)) { 1867c478bd9Sstevel@tonic-gate /* check to see if hostname exists for this transport */ 1877c478bd9Sstevel@tonic-gate if ((netdir_getbyname(nconf, &service, &addrs) == 0) && 1887c478bd9Sstevel@tonic-gate (addrs->n_cnt != 0)) { 1897c478bd9Sstevel@tonic-gate /* at least one valid address */ 1907c478bd9Sstevel@tonic-gate validhost = TRUE; 1917c478bd9Sstevel@tonic-gate break; 1927c478bd9Sstevel@tonic-gate } 1937c478bd9Sstevel@tonic-gate } 1947c478bd9Sstevel@tonic-gate endnetconfig(nc_handle); 1957c478bd9Sstevel@tonic-gate if (!validhost) { 1967c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "%s: unknown host: %s\n", 1977c478bd9Sstevel@tonic-gate program_name, target_host); 1987c478bd9Sstevel@tonic-gate exit(1); 1997c478bd9Sstevel@tonic-gate } 2007c478bd9Sstevel@tonic-gate (void) host2netname(name, target_host, (char *)NULL); 2017c478bd9Sstevel@tonic-gate uid = 0; 2027c478bd9Sstevel@tonic-gate } 2037c478bd9Sstevel@tonic-gate 2047c478bd9Sstevel@tonic-gate (void) fprintf(stdout, "Adding new key for %s.\n", name); 2057c478bd9Sstevel@tonic-gate pass = get_password(uid, target_host, username); 2067c478bd9Sstevel@tonic-gate 2077c478bd9Sstevel@tonic-gate if (pass == NULL) 2087c478bd9Sstevel@tonic-gate exit(1); 2097c478bd9Sstevel@tonic-gate 2107d1e8394SAshok Kumar T (void) strlcpy(short_pass, pass, sizeof (short_pass)); 2117d1e8394SAshok Kumar T (void) __gen_dhkeys(public, secret, short_pass); 2127c478bd9Sstevel@tonic-gate 2137d1e8394SAshok Kumar T (void) memcpy(crypt1, secret, HEXKEYBYTES); 2147d1e8394SAshok Kumar T (void) memcpy(crypt1 + HEXKEYBYTES, secret, KEYCHECKSUMSIZE); 2157c478bd9Sstevel@tonic-gate crypt1[HEXKEYBYTES + KEYCHECKSUMSIZE] = 0; 2167d1e8394SAshok Kumar T xencrypt(crypt1, short_pass); 2177c478bd9Sstevel@tonic-gate 2187c478bd9Sstevel@tonic-gate if (status = setpublicmap(name, public, crypt1, pk_database, 219*36e852a1SRaja Andra short_pass)) { 2207c478bd9Sstevel@tonic-gate switch (pk_database) { 2217c478bd9Sstevel@tonic-gate case PK_YP: 2227c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 2237c478bd9Sstevel@tonic-gate "%s: unable to update NIS database (%u): %s\n", 2247c478bd9Sstevel@tonic-gate program_name, status, 2257c478bd9Sstevel@tonic-gate yperr_string(status)); 2267c478bd9Sstevel@tonic-gate break; 2277c478bd9Sstevel@tonic-gate case PK_FILES: 2287c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 2297c478bd9Sstevel@tonic-gate "%s: hence, unable to update publickey database\n", 2307c478bd9Sstevel@tonic-gate program_name); 2317c478bd9Sstevel@tonic-gate break; 2327c478bd9Sstevel@tonic-gate default: 2337c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 2347c478bd9Sstevel@tonic-gate "%s: could not update unknown database: %d\n", 2357c478bd9Sstevel@tonic-gate program_name, pk_database); 2367c478bd9Sstevel@tonic-gate } 2377c478bd9Sstevel@tonic-gate exit(1); 2387c478bd9Sstevel@tonic-gate } 23949e7ca49Speteh return (0); 2407c478bd9Sstevel@tonic-gate } 2417c478bd9Sstevel@tonic-gate 2427c478bd9Sstevel@tonic-gate /* 2437c478bd9Sstevel@tonic-gate * Set the entry in the public key file 2447c478bd9Sstevel@tonic-gate */ 24549e7ca49Speteh int 246*36e852a1SRaja Andra setpublicmap(name, public, secret, database, pw) 2477c478bd9Sstevel@tonic-gate int database; 2487c478bd9Sstevel@tonic-gate char *name; 2497c478bd9Sstevel@tonic-gate char *public; 2507c478bd9Sstevel@tonic-gate char *secret; 2517c478bd9Sstevel@tonic-gate char *pw; 2527c478bd9Sstevel@tonic-gate { 2537c478bd9Sstevel@tonic-gate char pkent[HEXKEYBYTES + HEXKEYBYTES + KEYCHECKSUMSIZE + 2]; 2547c478bd9Sstevel@tonic-gate char *domain = NULL; 2557c478bd9Sstevel@tonic-gate char *master = NULL; 2567c478bd9Sstevel@tonic-gate char hostname[MAXHOSTNAMELEN+1]; 2577c478bd9Sstevel@tonic-gate 2587c478bd9Sstevel@tonic-gate (void) sprintf(pkent, "%s:%s", public, secret); 2597c478bd9Sstevel@tonic-gate switch (database) { 2607c478bd9Sstevel@tonic-gate case PK_YP: 2617c478bd9Sstevel@tonic-gate /* check that we're on the master server */ 2627c478bd9Sstevel@tonic-gate (void) yp_get_default_domain(&domain); 2637c478bd9Sstevel@tonic-gate if (yp_master(domain, PKMAP, &master) != 0) { 2647c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 2657c478bd9Sstevel@tonic-gate "%s: cannot find master of NIS publickey database\n", 2667c478bd9Sstevel@tonic-gate program_name); 2677c478bd9Sstevel@tonic-gate exit(1); 2687c478bd9Sstevel@tonic-gate } 2697c478bd9Sstevel@tonic-gate if (gethostname(hostname, MAXHOSTNAMELEN) < 0) { 2707c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 2717c478bd9Sstevel@tonic-gate "%s: cannot find my own host name\n", 2727c478bd9Sstevel@tonic-gate program_name); 2737c478bd9Sstevel@tonic-gate exit(1); 2747c478bd9Sstevel@tonic-gate } 2757c478bd9Sstevel@tonic-gate if (strcmp(master, hostname) != 0) { 2767c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 2777c478bd9Sstevel@tonic-gate "%s: can only be used on NIS master machine '%s'\n", 2787c478bd9Sstevel@tonic-gate program_name, master); 2797c478bd9Sstevel@tonic-gate exit(1); 2807c478bd9Sstevel@tonic-gate } 2817c478bd9Sstevel@tonic-gate 2827c478bd9Sstevel@tonic-gate if (chdir(YPDBPATH) < 0) { 2837c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "%s: cannot chdir to %s", 2847c478bd9Sstevel@tonic-gate program_name, YPDBPATH); 2857c478bd9Sstevel@tonic-gate } 2867c478bd9Sstevel@tonic-gate (void) fprintf(stdout, 2877c478bd9Sstevel@tonic-gate "Please wait for the database to get updated ...\n"); 2887c478bd9Sstevel@tonic-gate return (mapupdate(name, PKMAP, YPOP_STORE, pkent)); 2897c478bd9Sstevel@tonic-gate case PK_FILES: 2907c478bd9Sstevel@tonic-gate return (localupdate(name, PKFILE, YPOP_STORE, pkent)); 2917c478bd9Sstevel@tonic-gate case PK_LDAP: 2927c478bd9Sstevel@tonic-gate return (ldap_update("dh192-0", name, public, secret, pw)); 2937c478bd9Sstevel@tonic-gate default: 2947c478bd9Sstevel@tonic-gate break; 2957c478bd9Sstevel@tonic-gate } 2967c478bd9Sstevel@tonic-gate return (1); 2977c478bd9Sstevel@tonic-gate } 2987c478bd9Sstevel@tonic-gate 29949e7ca49Speteh void 30049e7ca49Speteh usage(void) 3017c478bd9Sstevel@tonic-gate { 3027c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 303*36e852a1SRaja Andra "usage:\t%s -u username [-s ldap | nis | files]\n", 3047c478bd9Sstevel@tonic-gate program_name); 3057c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 306*36e852a1SRaja Andra "\t%s -h hostname [-s ldap | nis | files]\n", 3077c478bd9Sstevel@tonic-gate program_name); 3087c478bd9Sstevel@tonic-gate exit(1); 3097c478bd9Sstevel@tonic-gate } 3107c478bd9Sstevel@tonic-gate 3117c478bd9Sstevel@tonic-gate /* 3127c478bd9Sstevel@tonic-gate * The parameters passed into the routine get_password and the 3137c478bd9Sstevel@tonic-gate * return values are as follows: 3147c478bd9Sstevel@tonic-gate * If the -h flag was specified on the command line: 3157c478bd9Sstevel@tonic-gate * (a) username is null 3167c478bd9Sstevel@tonic-gate * (b) target_host is non-null 3177c478bd9Sstevel@tonic-gate * (c) uid is 0 3187c478bd9Sstevel@tonic-gate * (d) the login password of root on target_host is returned 3197c478bd9Sstevel@tonic-gate * 3207c478bd9Sstevel@tonic-gate * If the -u flag was specified on the command line: 3217c478bd9Sstevel@tonic-gate * (a) username is non-null 3227c478bd9Sstevel@tonic-gate * (b) target_host is null in all cases except when username is root; 3237c478bd9Sstevel@tonic-gate * in that case target_host is set to the local host 3247c478bd9Sstevel@tonic-gate * (c) uid is set to the username's uid 3257c478bd9Sstevel@tonic-gate * (d) the login password of the user <username> is returned 3267c478bd9Sstevel@tonic-gate */ 3277c478bd9Sstevel@tonic-gate static char * 3287c478bd9Sstevel@tonic-gate get_password(uid, target_host, username) 3297c478bd9Sstevel@tonic-gate uid_t uid; 3307c478bd9Sstevel@tonic-gate char *target_host; 3317c478bd9Sstevel@tonic-gate char *username; 3327c478bd9Sstevel@tonic-gate { 3337c478bd9Sstevel@tonic-gate static char password[MAXPASSWD+1]; 3347c478bd9Sstevel@tonic-gate char prompt[MAXPASSWD+MAXHOSTNAMELEN+64]; 3357c478bd9Sstevel@tonic-gate char *encrypted_password, 3367c478bd9Sstevel@tonic-gate *login_password = NULL, 3377c478bd9Sstevel@tonic-gate *pass = NULL; 3387c478bd9Sstevel@tonic-gate struct passwd *pw; 3397c478bd9Sstevel@tonic-gate struct spwd *spw; 3407c478bd9Sstevel@tonic-gate 3417c478bd9Sstevel@tonic-gate if ((username != 0) || 3427c478bd9Sstevel@tonic-gate (target_host != 0) && (local_host(target_host))) { 3437c478bd9Sstevel@tonic-gate 3447c478bd9Sstevel@tonic-gate /* 3457c478bd9Sstevel@tonic-gate * "-u username" or "-h localhost" was specified on the 3467c478bd9Sstevel@tonic-gate * command line 3477c478bd9Sstevel@tonic-gate */ 3487c478bd9Sstevel@tonic-gate 3497c478bd9Sstevel@tonic-gate pw = getpwuid(uid); 3507c478bd9Sstevel@tonic-gate 3517c478bd9Sstevel@tonic-gate if (! pw) { 3527c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 3537c478bd9Sstevel@tonic-gate "%s: unable to locate password record for uid %d\n", 3547c478bd9Sstevel@tonic-gate program_name, uid); 3557c478bd9Sstevel@tonic-gate return (0); 3567c478bd9Sstevel@tonic-gate } 3577c478bd9Sstevel@tonic-gate spw = getspnam(pw->pw_name); 3587c478bd9Sstevel@tonic-gate if (spw) 3597c478bd9Sstevel@tonic-gate login_password = spw->sp_pwdp; 3607c478bd9Sstevel@tonic-gate 3617c478bd9Sstevel@tonic-gate if (! login_password || (strlen(login_password) == 0)) { 3627c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 3637c478bd9Sstevel@tonic-gate "%s: unable to locate shadow password record for %s\n", 3647c478bd9Sstevel@tonic-gate program_name, pw->pw_name); 3657c478bd9Sstevel@tonic-gate return (0); 3667c478bd9Sstevel@tonic-gate } 3677c478bd9Sstevel@tonic-gate 3687c478bd9Sstevel@tonic-gate if (uid == 0) { 3697c478bd9Sstevel@tonic-gate (void) sprintf(prompt, "Enter local root login password:"); 3707c478bd9Sstevel@tonic-gate } else 3717c478bd9Sstevel@tonic-gate (void) sprintf(prompt, "Enter %s's login password:", 3727c478bd9Sstevel@tonic-gate pw->pw_name); 3737c478bd9Sstevel@tonic-gate 3747d1e8394SAshok Kumar T pass = getpassphrase(prompt); 3757c478bd9Sstevel@tonic-gate if (pass && strlen(pass) == 0) { 3767c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "%s: Invalid password.\n", 3777c478bd9Sstevel@tonic-gate program_name); 3787c478bd9Sstevel@tonic-gate return (0); 3797c478bd9Sstevel@tonic-gate } 3807c478bd9Sstevel@tonic-gate strcpy(password, pass); 3817c478bd9Sstevel@tonic-gate encrypted_password = crypt(password, login_password); 3827c478bd9Sstevel@tonic-gate 3837c478bd9Sstevel@tonic-gate /* Verify that password supplied matches login password */ 3847c478bd9Sstevel@tonic-gate if (strcmp(encrypted_password, login_password) != 0) { 3857c478bd9Sstevel@tonic-gate /* 3867c478bd9Sstevel@tonic-gate * Give another chance for typo 3877c478bd9Sstevel@tonic-gate */ 3887d1e8394SAshok Kumar T pass = getpassphrase("Please retype password:"); 3897c478bd9Sstevel@tonic-gate if (pass && strlen(pass) == 0) { 3907c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "%s: Invalid password.\n", 3917c478bd9Sstevel@tonic-gate program_name); 3927c478bd9Sstevel@tonic-gate return (0); 3937c478bd9Sstevel@tonic-gate } 3947c478bd9Sstevel@tonic-gate strcpy(password, pass); 3957c478bd9Sstevel@tonic-gate encrypted_password = crypt(password, login_password); 3967c478bd9Sstevel@tonic-gate if (strcmp(encrypted_password, login_password) != 0) { 3977c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 3987c478bd9Sstevel@tonic-gate "%s: ERROR, invalid password.\n", 3997c478bd9Sstevel@tonic-gate program_name); 4007c478bd9Sstevel@tonic-gate return (0); 4017c478bd9Sstevel@tonic-gate } 4027c478bd9Sstevel@tonic-gate } 4037c478bd9Sstevel@tonic-gate } else { 4047c478bd9Sstevel@tonic-gate /* 4057c478bd9Sstevel@tonic-gate * "-h remotehost" was specified on the command line 4067c478bd9Sstevel@tonic-gate * 4077c478bd9Sstevel@tonic-gate * Since we cannot verify the root password of the remote 4087c478bd9Sstevel@tonic-gate * host we have to trust what the user inputs. We can, 4097c478bd9Sstevel@tonic-gate * however, reduce the possibility of an error by prompting 4107c478bd9Sstevel@tonic-gate * the user to enter the target host's password twice and 4117c478bd9Sstevel@tonic-gate * comparing those two. We can also authenticate the 4127c478bd9Sstevel@tonic-gate * user to be root by checking the real uid. 4137c478bd9Sstevel@tonic-gate */ 4147c478bd9Sstevel@tonic-gate 4157c478bd9Sstevel@tonic-gate if (getuid() != 0) { 4167c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "Must be superuser to run %s\n", 4177c478bd9Sstevel@tonic-gate program_name); 4187c478bd9Sstevel@tonic-gate return (0); 4197c478bd9Sstevel@tonic-gate } 4207c478bd9Sstevel@tonic-gate 4217c478bd9Sstevel@tonic-gate (void) sprintf(prompt, 4227c478bd9Sstevel@tonic-gate "Enter %s's root login password:", 4237c478bd9Sstevel@tonic-gate target_host); 4247d1e8394SAshok Kumar T pass = getpassphrase(prompt); 4257c478bd9Sstevel@tonic-gate if (!pass) { 4267c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 4277c478bd9Sstevel@tonic-gate "%s: getpass failed.\n", 4287c478bd9Sstevel@tonic-gate program_name); 4297c478bd9Sstevel@tonic-gate return (0); 4307c478bd9Sstevel@tonic-gate } 4317c478bd9Sstevel@tonic-gate if (!*pass) { 4327c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 4337c478bd9Sstevel@tonic-gate "%s: Invalid root password.\n", 4347c478bd9Sstevel@tonic-gate program_name); 4357c478bd9Sstevel@tonic-gate return (0); 4367c478bd9Sstevel@tonic-gate } 4377c478bd9Sstevel@tonic-gate strcpy(password, pass); 4387c478bd9Sstevel@tonic-gate 4397c478bd9Sstevel@tonic-gate /* 4407c478bd9Sstevel@tonic-gate * Now re-enter the password and compare it to the 4417c478bd9Sstevel@tonic-gate * one just read. 4427c478bd9Sstevel@tonic-gate */ 4437c478bd9Sstevel@tonic-gate (void) sprintf(prompt, 4447c478bd9Sstevel@tonic-gate "Please confirm %s's root login password:", 4457c478bd9Sstevel@tonic-gate target_host); 4467d1e8394SAshok Kumar T pass = getpassphrase(prompt); 4477c478bd9Sstevel@tonic-gate if (!pass) { 4487c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 4497c478bd9Sstevel@tonic-gate "%s: getpass failed.\n", 4507c478bd9Sstevel@tonic-gate program_name); 4517c478bd9Sstevel@tonic-gate return (0); 4527c478bd9Sstevel@tonic-gate } 4537c478bd9Sstevel@tonic-gate if (!*pass) { 4547c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 4557c478bd9Sstevel@tonic-gate "%s: Invalid root password.\n", 4567c478bd9Sstevel@tonic-gate program_name); 4577c478bd9Sstevel@tonic-gate return (0); 4587c478bd9Sstevel@tonic-gate } 4597c478bd9Sstevel@tonic-gate if (strcmp(pass, password) != 0) { 4607c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 4617c478bd9Sstevel@tonic-gate "%s: Password Incorrect.\n", 4627c478bd9Sstevel@tonic-gate program_name); 4637c478bd9Sstevel@tonic-gate return (0); 4647c478bd9Sstevel@tonic-gate } 4657c478bd9Sstevel@tonic-gate } 4667c478bd9Sstevel@tonic-gate 4677c478bd9Sstevel@tonic-gate return (password); 4687c478bd9Sstevel@tonic-gate } 469