1997-05-28 15:54:04 +00:00
|
|
|
/*
|
|
|
|
* Sun RPC is a product of Sun Microsystems, Inc. and is provided for
|
|
|
|
* unrestricted use provided that this legend is included on all tape
|
|
|
|
* media and as a part of the software program in whole or part. Users
|
|
|
|
* may copy or modify Sun RPC without charge, but are not authorized
|
|
|
|
* to license or distribute it to anyone else except as part of a product or
|
|
|
|
* program developed by the user or with the express written consent of
|
|
|
|
* Sun Microsystems, Inc.
|
|
|
|
*
|
|
|
|
* SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
|
2002-08-27 19:06:44 +00:00
|
|
|
* WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
1997-05-28 15:54:04 +00:00
|
|
|
* PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
|
|
|
|
*
|
|
|
|
* Sun RPC is provided with no support and without any obligation on the
|
|
|
|
* part of Sun Microsystems, Inc. to assist in its use, correction,
|
|
|
|
* modification or enhancement.
|
|
|
|
*
|
|
|
|
* SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
|
|
|
|
* INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
|
|
|
|
* OR ANY PART THEREOF.
|
|
|
|
*
|
|
|
|
* In no event will Sun Microsystems, Inc. be liable for any lost revenue
|
|
|
|
* or profits or other special, indirect and consequential damages, even if
|
|
|
|
* Sun has been advised of the possibility of such damages.
|
|
|
|
*
|
|
|
|
* Sun Microsystems, Inc.
|
|
|
|
* 2550 Garcia Avenue
|
|
|
|
* Mountain View, California 94043
|
|
|
|
*/
|
2001-12-02 23:12:33 +00:00
|
|
|
|
2001-12-10 21:13:08 +00:00
|
|
|
#if 0
|
1997-05-28 15:54:04 +00:00
|
|
|
#ifndef lint
|
2001-12-10 21:13:08 +00:00
|
|
|
static char sccsid[] = "@(#)chkey.c 1.7 91/03/11 Copyr 1986 Sun Micro";
|
1997-05-28 15:54:04 +00:00
|
|
|
#endif
|
2001-12-10 21:13:08 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <sys/cdefs.h>
|
|
|
|
__FBSDID("$FreeBSD$");
|
2001-12-02 23:12:33 +00:00
|
|
|
|
1997-05-28 15:54:04 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 1986, Sun Microsystems, Inc.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Command to change one's public key in the public key database
|
|
|
|
*/
|
|
|
|
#include <rpc/rpc.h>
|
|
|
|
#include <rpc/key_prot.h>
|
|
|
|
#ifdef YP
|
|
|
|
#include <rpcsvc/yp_prot.h>
|
|
|
|
#include <rpcsvc/ypclnt.h>
|
|
|
|
#else
|
|
|
|
#define YPOP_STORE 4
|
|
|
|
#endif
|
2001-12-02 23:12:33 +00:00
|
|
|
#include <sys/fcntl.h>
|
2002-08-27 19:06:44 +00:00
|
|
|
#include <err.h>
|
1997-05-28 15:54:04 +00:00
|
|
|
#include <pwd.h>
|
2001-12-02 23:12:33 +00:00
|
|
|
#include <stdio.h>
|
2002-07-28 15:02:24 +00:00
|
|
|
#include <stdlib.h>
|
1997-05-28 15:54:04 +00:00
|
|
|
#include <string.h>
|
2001-12-02 23:12:33 +00:00
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include "extern.h"
|
1997-05-28 15:54:04 +00:00
|
|
|
|
|
|
|
#ifdef YPPASSWD
|
2001-12-02 23:12:33 +00:00
|
|
|
struct passwd *ypgetpwuid(uid_t);
|
1997-05-28 15:54:04 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef YP
|
|
|
|
static char *domain;
|
|
|
|
static char PKMAP[] = "publickey.byname";
|
|
|
|
#else
|
|
|
|
static char PKFILE[] = "/etc/publickey";
|
|
|
|
#endif /* YP */
|
|
|
|
static char ROOTKEY[] = "/etc/.rootkey";
|
|
|
|
|
2002-08-27 19:06:44 +00:00
|
|
|
static void usage(void);
|
2001-12-02 23:12:33 +00:00
|
|
|
extern int yp_update(char *, char *, int, char *, size_t, char *, size_t);
|
|
|
|
|
|
|
|
int
|
2002-07-28 15:02:24 +00:00
|
|
|
main(int argc, char **argv)
|
1997-05-28 15:54:04 +00:00
|
|
|
{
|
|
|
|
char name[MAXNETNAMELEN+1];
|
|
|
|
char public[HEXKEYBYTES + 1];
|
|
|
|
char secret[HEXKEYBYTES + 1];
|
|
|
|
char crypt1[HEXKEYBYTES + KEYCHECKSUMSIZE + 1];
|
|
|
|
char crypt2[HEXKEYBYTES + KEYCHECKSUMSIZE + 1];
|
|
|
|
int status;
|
|
|
|
char *pass;
|
|
|
|
struct passwd *pw;
|
|
|
|
uid_t uid;
|
|
|
|
int force = 0;
|
2002-08-27 19:06:44 +00:00
|
|
|
int ch;
|
1997-05-28 15:54:04 +00:00
|
|
|
#ifdef YP
|
|
|
|
char *master;
|
|
|
|
#endif
|
|
|
|
|
2002-08-27 19:06:44 +00:00
|
|
|
while ((ch = getopt(argc, argv, "f")) != -1)
|
|
|
|
switch(ch) {
|
1997-05-28 15:54:04 +00:00
|
|
|
case 'f':
|
|
|
|
force = 1;
|
|
|
|
break;
|
|
|
|
default:
|
2002-08-27 19:06:44 +00:00
|
|
|
usage();
|
1997-05-28 15:54:04 +00:00
|
|
|
}
|
2002-08-27 19:06:44 +00:00
|
|
|
argc -= optind;
|
|
|
|
argv += optind;
|
|
|
|
|
|
|
|
if (argc != 0)
|
|
|
|
usage();
|
1997-05-28 15:54:04 +00:00
|
|
|
|
|
|
|
#ifdef YP
|
|
|
|
(void)yp_get_default_domain(&domain);
|
2002-08-27 19:06:44 +00:00
|
|
|
if (yp_master(domain, PKMAP, &master) != 0)
|
|
|
|
errx(1, "can't find master of publickey database");
|
1997-05-28 15:54:04 +00:00
|
|
|
#endif
|
|
|
|
uid = getuid() /*geteuid()*/;
|
|
|
|
if (uid == 0) {
|
2002-08-27 19:06:44 +00:00
|
|
|
if (host2netname(name, NULL, NULL) == 0)
|
|
|
|
errx(1, "cannot convert hostname to netname");
|
1997-05-28 15:54:04 +00:00
|
|
|
} else {
|
2002-08-27 19:06:44 +00:00
|
|
|
if (user2netname(name, uid, NULL) == 0)
|
|
|
|
errx(1, "cannot convert username to netname");
|
1997-05-28 15:54:04 +00:00
|
|
|
}
|
|
|
|
(void)printf("Generating new key for %s.\n", name);
|
|
|
|
|
|
|
|
if (!force) {
|
|
|
|
if (uid != 0) {
|
|
|
|
#ifdef YPPASSWD
|
|
|
|
pw = ypgetpwuid(uid);
|
|
|
|
#else
|
|
|
|
pw = getpwuid(uid);
|
|
|
|
#endif
|
|
|
|
if (pw == NULL) {
|
|
|
|
#ifdef YPPASSWD
|
2002-08-27 19:06:44 +00:00
|
|
|
errx(1,
|
|
|
|
"no NIS password entry found: can't change key");
|
1997-05-28 15:54:04 +00:00
|
|
|
#else
|
2002-08-27 19:06:44 +00:00
|
|
|
errx(1,
|
|
|
|
"no password entry found: can't change key");
|
1997-05-28 15:54:04 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
pw = getpwuid(0);
|
2002-08-27 19:06:44 +00:00
|
|
|
if (pw == NULL)
|
|
|
|
errx(1, "no password entry found: can't change key");
|
1997-05-28 15:54:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
pass = getpass("Password:");
|
|
|
|
#ifdef YPPASSWD
|
|
|
|
if (!force) {
|
2002-08-27 19:06:44 +00:00
|
|
|
if (strcmp(crypt(pass, pw->pw_passwd), pw->pw_passwd) != 0)
|
|
|
|
errx(1, "invalid password");
|
1997-05-28 15:54:04 +00:00
|
|
|
}
|
|
|
|
#else
|
|
|
|
force = 1; /* Make this mandatory */
|
|
|
|
#endif
|
|
|
|
genkeys(public, secret, pass);
|
|
|
|
|
|
|
|
memcpy(crypt1, secret, HEXKEYBYTES);
|
|
|
|
memcpy(crypt1 + HEXKEYBYTES, secret, KEYCHECKSUMSIZE);
|
|
|
|
crypt1[HEXKEYBYTES + KEYCHECKSUMSIZE] = 0;
|
|
|
|
xencrypt(crypt1, pass);
|
|
|
|
|
|
|
|
if (force) {
|
|
|
|
memcpy(crypt2, crypt1, HEXKEYBYTES + KEYCHECKSUMSIZE + 1);
|
|
|
|
xdecrypt(crypt2, getpass("Retype password:"));
|
2002-08-27 19:06:44 +00:00
|
|
|
if (memcmp(crypt2, crypt2 + HEXKEYBYTES, KEYCHECKSUMSIZE) != 0
|
|
|
|
|| memcmp(crypt2, secret, HEXKEYBYTES) != 0)
|
|
|
|
errx(1, "password incorrect");
|
1997-05-28 15:54:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef YP
|
|
|
|
(void)printf("Sending key change request to %s...\n", master);
|
|
|
|
#endif
|
|
|
|
status = setpublicmap(name, public, crypt1);
|
|
|
|
if (status != 0) {
|
|
|
|
#ifdef YP
|
2002-08-27 19:06:44 +00:00
|
|
|
errx(1, "unable to update NIS database (%u): %s",
|
|
|
|
status, yperr_string(status));
|
1997-05-28 15:54:04 +00:00
|
|
|
#else
|
2002-08-27 19:06:44 +00:00
|
|
|
errx(1, "unable to update publickey database");
|
1997-05-28 15:54:04 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
if (uid == 0) {
|
|
|
|
/*
|
|
|
|
* Root users store their key in /etc/$ROOTKEY so
|
|
|
|
* that they can auto reboot without having to be
|
|
|
|
* around to type a password. Storing this in a file
|
|
|
|
* is rather dubious: it should really be in the EEPROM
|
|
|
|
* so it does not go over the net.
|
|
|
|
*/
|
|
|
|
int fd;
|
|
|
|
|
|
|
|
fd = open(ROOTKEY, O_WRONLY|O_TRUNC|O_CREAT, 0);
|
|
|
|
if (fd < 0) {
|
2002-08-27 19:06:44 +00:00
|
|
|
warn("%s", ROOTKEY);
|
1997-05-28 15:54:04 +00:00
|
|
|
} else {
|
|
|
|
char newline = '\n';
|
|
|
|
|
|
|
|
if (write(fd, secret, strlen(secret)) < 0 ||
|
2002-08-27 19:06:44 +00:00
|
|
|
write(fd, &newline, sizeof(newline)) < 0)
|
|
|
|
warn("%s: write", ROOTKEY);
|
1997-05-28 15:54:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-08-27 19:06:44 +00:00
|
|
|
if (key_setsecret(secret) < 0)
|
|
|
|
errx(1, "unable to login with new secret key");
|
1997-05-28 15:54:04 +00:00
|
|
|
(void)printf("Done.\n");
|
|
|
|
exit(0);
|
|
|
|
/* NOTREACHED */
|
|
|
|
}
|
|
|
|
|
2001-12-02 23:12:33 +00:00
|
|
|
static void
|
2002-08-27 19:06:44 +00:00
|
|
|
usage(void)
|
1997-05-28 15:54:04 +00:00
|
|
|
{
|
2002-08-27 19:06:44 +00:00
|
|
|
(void)fprintf(stderr, "usage: chkey [-f]\n");
|
1997-05-28 15:54:04 +00:00
|
|
|
exit(1);
|
|
|
|
/* NOTREACHED */
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Set the entry in the public key file
|
|
|
|
*/
|
2001-12-02 23:12:33 +00:00
|
|
|
int
|
2002-07-28 15:02:24 +00:00
|
|
|
setpublicmap(char *name, char *public, char *secret)
|
1997-05-28 15:54:04 +00:00
|
|
|
{
|
|
|
|
char pkent[1024];
|
|
|
|
|
|
|
|
(void)sprintf(pkent,"%s:%s", public, secret);
|
|
|
|
#ifdef YP
|
|
|
|
return (yp_update(domain, PKMAP, YPOP_STORE,
|
|
|
|
name, strlen(name), pkent, strlen(pkent)));
|
|
|
|
#else
|
|
|
|
return (localupdate(name, PKFILE, YPOP_STORE,
|
|
|
|
strlen(name), name, strlen(pkent), pkent));
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef YPPASSWD
|
|
|
|
struct passwd *
|
2002-07-28 15:02:24 +00:00
|
|
|
ypgetpwuid(uid_t uid)
|
1997-05-28 15:54:04 +00:00
|
|
|
{
|
|
|
|
char uidstr[10];
|
|
|
|
char *val;
|
|
|
|
int vallen;
|
|
|
|
static struct passwd pw;
|
|
|
|
char *p;
|
|
|
|
|
|
|
|
(void)sprintf(uidstr, "%d", uid);
|
|
|
|
if (yp_match(domain, "passwd.byuid", uidstr, strlen(uidstr),
|
|
|
|
&val, &vallen) != 0) {
|
|
|
|
return (NULL);
|
|
|
|
}
|
2002-08-27 19:06:44 +00:00
|
|
|
p = strchr(val, ':');
|
1997-05-28 15:54:04 +00:00
|
|
|
if (p == NULL) {
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
pw.pw_passwd = p + 1;
|
2002-08-27 19:06:44 +00:00
|
|
|
p = strchr(pw.pw_passwd, ':');
|
1997-05-28 15:54:04 +00:00
|
|
|
if (p == NULL) {
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
*p = 0;
|
|
|
|
return (&pw);
|
|
|
|
}
|
|
|
|
#endif /* YPPASSWD */
|