xref: /titanic_52/usr/src/cmd/keyserv/newkey.c (revision 0eb822a1c0c2bea495647510b75f77f0e57633eb)
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, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
28 /*	  All Rights Reserved  	*/
29 
30 /*
31  * University Copyright- Copyright (c) 1982, 1986, 1988
32  * The Regents of the University of California
33  * All Rights Reserved
34  *
35  * University Acknowledgment- Portions of this document are derived from
36  * software developed by the University of California, Berkeley, and its
37  * contributors.
38  */
39 
40 #pragma ident	"%Z%%M%	%I%	%E% SMI"
41 
42 /*
43  * Administrative tool to add a new user to the publickey database
44  */
45 #include <stdio.h>
46 #include <stdlib.h>
47 #include <rpc/rpc.h>
48 #include <rpc/key_prot.h>
49 #include <rpcsvc/ypclnt.h>
50 #include <sys/wait.h>
51 #include <netdb.h>
52 #include <pwd.h>
53 #include <shadow.h>
54 #include <crypt.h>
55 #include <string.h>
56 #include <sys/resource.h>
57 #include <netdir.h>
58 #include <rpcsvc/nis.h>
59 
60 #define	MAXMAPNAMELEN	256
61 #define	MAXPASSWD	256	/* max significant characters in password */
62 
63 #define	PK_FILES	1
64 #define	PK_YP		2
65 #define	PK_NISPLUS	3
66 #define	PK_LDAP		4
67 
68 extern	int optind;
69 extern	char *optarg;
70 extern	char *get_nisplus_principal();
71 extern	int __getnetnamebyuid();
72 extern  int self_check(char *name);
73 
74 #define	local_host(host_name)	self_check(host_name)
75 
76 char	*program_name;
77 int		pk_database;
78 static	char	*get_password();
79 static	char *basename();
80 static	char SHELL[] = "/bin/sh";
81 static	char YPDBPATH[] = "/var/yp";
82 static	char PKMAP[] = "publickey.byname";
83 static	char UPDATEFILE[] = "updaters";
84 static	char PKFILE[] = "/etc/publickey";
85 static	void usage(void);
86 
87 int
88 main(int argc, char *argv[])
89 {
90 	char	name[MAXNETNAMELEN + 1];
91 	char	public[HEXKEYBYTES + 1];
92 	char	secret[HEXKEYBYTES + 1];
93 	char	crypt1[HEXKEYBYTES + KEYCHECKSUMSIZE + 1];
94 	int	status;
95 	char	*pass, *target_host = NULL,
96 		*username = NULL, *pk_service = NULL;
97 	struct passwd	*pw;
98 	NCONF_HANDLE	*nc_handle;
99 	struct	netconfig *nconf;
100 	struct	nd_hostserv service;
101 	struct	nd_addrlist *addrs;
102 	bool_t	validhost;
103 	uid_t	uid;
104 	int	c;
105 	char	*nprinc = NULL;  /* nisplus principal name */
106 	char	host_pname[NIS_MAXNAMELEN];
107 
108 	program_name = argv[0];
109 	while ((c = getopt(argc, argv, "s:u:h:")) != -1) {
110 		switch (c) {
111 		case 's':
112 			if (pk_service == NULL)
113 				pk_service = optarg;
114 			else
115 				usage();
116 			break;
117 		case 'u':
118 			if (username || target_host)
119 				usage();
120 			username = optarg;
121 			break;
122 		case 'h':
123 			if (username || target_host)
124 				usage();
125 			target_host = optarg;
126 			break;
127 		default:
128 			usage();
129 		}
130 	}
131 
132 	if (optind < argc || (username == 0 && target_host == 0)) {
133 		usage();
134 	}
135 
136 	if ((pk_database = get_pk_source(pk_service)) == 0)
137 		usage();
138 
139 	if (geteuid() != 0) {
140 		(void) fprintf(stderr, "Must be superuser to run %s\n",
141 		    program_name);
142 		exit(1);
143 	}
144 
145 	if (username) {
146 		pw = getpwnam(username);
147 		if (pw == NULL) {
148 			(void) fprintf(stderr, "%s: unknown user: '%s'\n",
149 				program_name, username);
150 			exit(1);
151 		}
152 		uid = pw->pw_uid;
153 		if (uid == 0) {
154 			if (! getnetname(name)) {
155 				(void) fprintf(stderr,
156 			"%s: could not get the equivalent netname for %s\n",
157 				program_name, username);
158 				usage();
159 			}
160 			if (pk_database == PK_NISPLUS)
161 				target_host = nis_local_host();
162 			else {
163 				if (gethostname(host_pname, NIS_MAXNAMELEN)
164 					< 0) {
165 					(void) fprintf(stderr,
166 				"%s: could not get the hostname for %s\n",
167 					program_name, username);
168 					usage();
169 				}
170 				target_host = host_pname;
171 			}
172 		}
173 		if (__getnetnamebyuid(name, uid) == 0) {
174 			(void) fprintf(stderr,
175 			"%s: could not get the equivalent netname for %s\n",
176 				program_name, username);
177 			usage();
178 		}
179 		if (pk_database == PK_NISPLUS)
180 			nprinc = get_nisplus_principal(nis_local_directory(),
181 					uid);
182 	} else {
183 		/* -h hostname option */
184 		service.h_host = target_host;
185 		service.h_serv = NULL;
186 		validhost = FALSE;
187 		/* verify if this is a valid hostname */
188 		nc_handle = setnetconfig();
189 		if (nc_handle == NULL) {
190 			/* fails to open netconfig file */
191 			(void) fprintf(stderr,
192 				"%s: failed in routine setnetconfig()\n",
193 				program_name);
194 			exit(2);
195 		}
196 		while (nconf = getnetconfig(nc_handle)) {
197 			/* check to see if hostname exists for this transport */
198 			if ((netdir_getbyname(nconf, &service, &addrs) == 0) &&
199 			    (addrs->n_cnt != 0)) {
200 				/* at least one valid address */
201 				validhost = TRUE;
202 				break;
203 			}
204 		}
205 		endnetconfig(nc_handle);
206 		if (!validhost) {
207 			(void) fprintf(stderr, "%s: unknown host: %s\n",
208 				program_name, target_host);
209 			exit(1);
210 		}
211 		(void) host2netname(name, target_host, (char *)NULL);
212 		if (pk_database == PK_NISPLUS) {
213 			if (target_host[strlen(target_host) - 1] != '.') {
214 				sprintf(host_pname, "%s.%s",
215 					target_host, nis_local_directory());
216 				nprinc = host_pname;
217 			} else
218 				nprinc = target_host;
219 		}
220 		uid = 0;
221 	}
222 
223 	(void) fprintf(stdout, "Adding new key for %s.\n", name);
224 	pass = get_password(uid, target_host, username);
225 
226 	if (pass == NULL)
227 		exit(1);
228 
229 	(void) __gen_dhkeys(public, secret, pass);
230 
231 	memcpy(crypt1, secret, HEXKEYBYTES);
232 	memcpy(crypt1 + HEXKEYBYTES, secret, KEYCHECKSUMSIZE);
233 	crypt1[HEXKEYBYTES + KEYCHECKSUMSIZE] = 0;
234 	xencrypt(crypt1, pass);
235 
236 	if (status = setpublicmap(name, public, crypt1, pk_database,
237 				nprinc, pass)) {
238 		switch (pk_database) {
239 		case PK_YP:
240 			(void) fprintf(stderr,
241 				"%s: unable to update NIS database (%u): %s\n",
242 				program_name, status,
243 				yperr_string(status));
244 			break;
245 		case PK_FILES:
246 			(void) fprintf(stderr,
247 			"%s: hence, unable to update publickey database\n",
248 				program_name);
249 			break;
250 		case PK_NISPLUS:
251 			(void) fprintf(stderr,
252 				"%s: unable to update nisplus database\n",
253 				program_name);
254 			break;
255 		default:
256 			(void) fprintf(stderr,
257 				"%s: could not update unknown database: %d\n",
258 				program_name, pk_database);
259 		}
260 		exit(1);
261 	}
262 	return (0);
263 }
264 
265 /*
266  * Set the entry in the public key file
267  */
268 int
269 setpublicmap(name, public, secret, database, nis_princ, pw)
270 	int database;
271 	char *name;
272 	char *public;
273 	char *secret;
274 	nis_name nis_princ;
275 	char *pw;
276 {
277 	char pkent[HEXKEYBYTES + HEXKEYBYTES + KEYCHECKSUMSIZE + 2];
278 	char *domain = NULL;
279 	char *master = NULL;
280 	char hostname[MAXHOSTNAMELEN+1];
281 
282 	(void) sprintf(pkent, "%s:%s", public, secret);
283 	switch (database) {
284 	case PK_YP:
285 		/* check that we're on the master server */
286 		(void) yp_get_default_domain(&domain);
287 		if (yp_master(domain, PKMAP, &master) != 0) {
288 			(void) fprintf(stderr,
289 			"%s: cannot find master of NIS publickey database\n",
290 				program_name);
291 			exit(1);
292 		}
293 		if (gethostname(hostname, MAXHOSTNAMELEN) < 0) {
294 			(void) fprintf(stderr,
295 				"%s: cannot find my own host name\n",
296 				program_name);
297 			exit(1);
298 		}
299 		if (strcmp(master, hostname) != 0) {
300 			(void) fprintf(stderr,
301 			"%s: can only be used on NIS master machine '%s'\n",
302 				program_name, master);
303 			exit(1);
304 		}
305 
306 		if (chdir(YPDBPATH) < 0) {
307 			(void) fprintf(stderr, "%s: cannot chdir to %s",
308 			program_name, YPDBPATH);
309 		}
310 		(void) fprintf(stdout,
311 			"Please wait for the database to get updated ...\n");
312 		return (mapupdate(name, PKMAP, YPOP_STORE, pkent));
313 	case PK_FILES:
314 		return (localupdate(name, PKFILE, YPOP_STORE, pkent));
315 	case PK_NISPLUS:
316 		return (nisplus_update(name, public, secret, nis_princ));
317 	case PK_LDAP:
318 		return (ldap_update("dh192-0", name, public, secret, pw));
319 	default:
320 		break;
321 	}
322 	return (1);
323 }
324 
325 void
326 usage(void)
327 {
328 	(void) fprintf(stderr,
329 		"usage:\t%s -u username [-s ldap | nisplus | nis | files]\n",
330 		program_name);
331 	(void) fprintf(stderr,
332 		"\t%s -h hostname [-s ldap | nisplus | nis | files]\n",
333 		program_name);
334 	exit(1);
335 }
336 
337 /*
338  * The parameters passed into the routine get_password and the
339  * return values are as follows:
340  * If the -h flag was specified on the command line:
341  * (a) username is null
342  * (b) target_host is non-null
343  * (c) uid is 0
344  * (d) the login password of root on target_host is returned
345  *
346  * If the -u flag was specified on the command line:
347  * (a) username is non-null
348  * (b) target_host is null in all cases except when username is root;
349  *	in that case target_host is set to the local host
350  * (c) uid is set to the username's uid
351  * (d) the login password of the user <username> is returned
352  */
353 static char *
354 get_password(uid, target_host, username)
355 uid_t	uid;
356 char	*target_host;
357 char	*username;
358 {
359 	static	char	password[MAXPASSWD+1];
360 	char		prompt[MAXPASSWD+MAXHOSTNAMELEN+64];
361 	char		*encrypted_password,
362 			*login_password = NULL,
363 			*pass = NULL;
364 	struct	passwd	*pw;
365 	struct	spwd	*spw;
366 
367 	if ((username != 0) ||
368 	    (target_host != 0) && (local_host(target_host))) {
369 
370 	/*
371 	 * "-u username" or "-h localhost" was specified on the
372 	 * command line
373 	 */
374 
375 	pw = getpwuid(uid);
376 
377 	if (! pw) {
378 		(void) fprintf(stderr,
379 			"%s: unable to locate password record for uid %d\n",
380 			program_name, uid);
381 		return (0);
382 	}
383 	spw = getspnam(pw->pw_name);
384 	if (spw)
385 		login_password = spw->sp_pwdp;
386 
387 	if (! login_password || (strlen(login_password) == 0)) {
388 		(void) fprintf(stderr,
389 			"%s: unable to locate shadow password record for %s\n",
390 			program_name, pw->pw_name);
391 		return (0);
392 	}
393 
394 	if (uid == 0) {
395 		(void) sprintf(prompt, "Enter local root login password:");
396 	} else
397 	    (void) sprintf(prompt, "Enter %s's login password:",
398 		pw->pw_name);
399 
400 	pass = getpass(prompt);
401 	if (pass && strlen(pass) == 0) {
402 		(void) fprintf(stderr, "%s: Invalid password.\n",
403 			program_name);
404 		return (0);
405 	}
406 	strcpy(password, pass);
407 	encrypted_password = crypt(password, login_password);
408 
409 	/* Verify that password supplied matches login password */
410 	if (strcmp(encrypted_password, login_password) != 0) {
411 		/*
412 		 * Give another chance for typo
413 		 */
414 		pass = getpass("Please retype password:");
415 	    if (pass && strlen(pass) == 0) {
416 		(void) fprintf(stderr, "%s: Invalid password.\n",
417 			program_name);
418 		return (0);
419 	    }
420 	    strcpy(password, pass);
421 	    encrypted_password = crypt(password, login_password);
422 	    if (strcmp(encrypted_password, login_password) != 0) {
423 		    (void) fprintf(stderr,
424 			"%s: ERROR, invalid password.\n",
425 			program_name);
426 		    return (0);
427 	    }
428 	}
429 	} else {
430 		/*
431 		 * "-h remotehost" was specified on the command line
432 		 *
433 		 * Since we cannot verify the root password of the remote
434 		 * host we have to trust what the user inputs. We can,
435 		 * however, reduce the possibility of an  error by prompting
436 		 * the user to enter the target host's password twice and
437 		 * comparing those two. We can also authenticate the
438 		 * user to be root by checking the real uid.
439 		 */
440 
441 		if (getuid() != 0) {
442 			(void) fprintf(stderr, "Must be superuser to run %s\n",
443 			    program_name);
444 			return (0);
445 		}
446 
447 		(void) sprintf(prompt,
448 		    "Enter %s's root login password:",
449 		    target_host);
450 		pass = getpass(prompt);
451 		if (!pass) {
452 			(void) fprintf(stderr,
453 			    "%s: getpass failed.\n",
454 			    program_name);
455 			return (0);
456 		}
457 		if (!*pass) {
458 			(void) fprintf(stderr,
459 			    "%s: Invalid root password.\n",
460 			    program_name);
461 			return (0);
462 		}
463 		strcpy(password, pass);
464 
465 		/*
466 		 * Now re-enter the password and compare it to the
467 		 * one just read.
468 		 */
469 		(void) sprintf(prompt,
470 		    "Please confirm %s's root login password:",
471 		    target_host);
472 		pass = getpass(prompt);
473 		if (!pass) {
474 			(void) fprintf(stderr,
475 			    "%s: getpass failed.\n",
476 			    program_name);
477 			return (0);
478 		}
479 		if (!*pass) {
480 			(void) fprintf(stderr,
481 			    "%s: Invalid root password.\n",
482 			    program_name);
483 			return (0);
484 		}
485 		if (strcmp(pass, password) != 0) {
486 			(void) fprintf(stderr,
487 			    "%s: Password Incorrect.\n",
488 			    program_name);
489 			return (0);
490 		}
491 	}
492 
493 	return (password);
494 }
495