From 2391ca8296478ecf4611adf449af0c8869ce032d Mon Sep 17 00:00:00 2001 From: Peter Avalos Date: Tue, 11 Nov 2008 18:06:12 +0000 Subject: [PATCH] Grab some changes from FreeBSD: -Use getopt. -Use err/warn. While I'm here, fix some whitespace, spelling, and unneeded (void) casts. --- usr.bin/chkey/chkey.c | 130 +++++++++++++++++------------------------- 1 file changed, 52 insertions(+), 78 deletions(-) diff --git a/usr.bin/chkey/chkey.c b/usr.bin/chkey/chkey.c index 5137ad9360..6cb853d520 100644 --- a/usr.bin/chkey/chkey.c +++ b/usr.bin/chkey/chkey.c @@ -1,4 +1,4 @@ -/* +/*- * 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 @@ -8,7 +8,7 @@ * Sun Microsystems, Inc. * * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE - * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR + * WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR * 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 @@ -27,7 +27,7 @@ * 2550 Garcia Avenue * Mountain View, California 94043 * - * $DragonFly: src/usr.bin/chkey/chkey.c,v 1.5 2005/01/11 00:51:11 joerg Exp $ + * $DragonFly: src/usr.bin/chkey/chkey.c,v 1.6 2008/11/11 18:06:12 pavalos Exp $ * @(#)chkey.c 1.7 91/03/11 Copyr 1986 Sun Micro */ /* @@ -38,6 +38,7 @@ * Command to change one's public key in the public key database */ #include +#include #include #include #include @@ -68,7 +69,7 @@ static char PKFILE[] = "/etc/publickey"; #endif /* YP */ static char ROOTKEY[] = "/etc/.rootkey"; -static void usage(char *); +static void usage(void); static int setpublicmap(char *, char *, char *); int @@ -79,56 +80,44 @@ main(int argc, char **argv) char secret[HEXKEYBYTES + 1]; char crypt1[HEXKEYBYTES + KEYCHECKSUMSIZE + 1]; char crypt2[HEXKEYBYTES + KEYCHECKSUMSIZE + 1]; - int status; + int status; char *pass; struct passwd *pw; uid_t uid; int force = 0; - char *self; + int ch; #ifdef YP char *master; #endif - self = argv[0]; - for (argc--, argv++; argc > 0 && **argv == '-'; argc--, argv++) { - if (argv[0][2] != 0) { - usage(self); - } - switch (argv[0][1]) { + while ((ch = getopt(argc, argv, "f")) != -1) + switch(ch) { case 'f': force = 1; break; default: - usage(self); + usage(); } - } - if (argc != 0) { - usage(self); - } + argc -= optind; + argv += optind; + + if (argc != 0) + usage(); #ifdef YP - (void)yp_get_default_domain(&domain); - if (yp_master(domain, PKMAP, &master) != 0) { - (void)fprintf(stderr, - "can't find master of publickey database\n"); - exit(1); - } + yp_get_default_domain(&domain); + if (yp_master(domain, PKMAP, &master) != 0) + errx(1, "can't find master of publickey database"); #endif uid = getuid() /*geteuid()*/; if (uid == 0) { - if (host2netname(name, NULL, NULL) == 0) { - (void)fprintf(stderr, - "chkey: cannot convert hostname to netname\n"); - exit(1); - } + if (host2netname(name, NULL, NULL) == 0) + errx(1, "cannot convert hostname to netname"); } else { - if (user2netname(name, uid, NULL) == 0) { - (void)fprintf(stderr, - "chkey: cannot convert username to netname\n"); - exit(1); - } + if (user2netname(name, uid, NULL) == 0) + errx(1, "cannot convert username to netname"); } - (void)printf("Generating new key for %s.\n", name); + printf("Generating new key for %s.\n", name); if (!force) { if (uid != 0) { @@ -139,35 +128,29 @@ main(int argc, char **argv) #endif if (pw == NULL) { #ifdef YPPASSWD - (void)fprintf(stderr, - "No NIS password entry found: can't change key.\n"); + errx(1, + "no NIS password entry found: can't change key"); #else - (void)fprintf(stderr, - "No password entry found: can't change key.\n"); + errx(1, + "no password entry found: can't change key"); #endif - exit(1); } } else { pw = getpwuid(0); - if (pw == NULL) { - (void)fprintf(stderr, - "No password entry found: can't change key.\n"); - exit(1); - } + if (pw == NULL) + errx(1, "no password entry found: can't change key"); } } pass = getpass("Password:"); #ifdef YPPASSWD if (!force) { - if (strcmp(crypt(pass, pw->pw_passwd), pw->pw_passwd) != 0) { - (void)fprintf(stderr, "Invalid password.\n"); - exit(1); - } + if (strcmp(crypt(pass, pw->pw_passwd), pw->pw_passwd) != 0) + errx(1, "invalid password"); } #else force = 1; /* Make this mandatory */ #endif - genkeys(public, secret, pass); + genkeys(public, secret, pass); memcpy(crypt1, secret, HEXKEYBYTES); memcpy(crypt1 + HEXKEYBYTES, secret, KEYCHECKSUMSIZE); @@ -175,29 +158,24 @@ main(int argc, char **argv) xencrypt(crypt1, pass); if (force) { - memcpy(crypt2, crypt1, HEXKEYBYTES + KEYCHECKSUMSIZE + 1); + memcpy(crypt2, crypt1, HEXKEYBYTES + KEYCHECKSUMSIZE + 1); xdecrypt(crypt2, getpass("Retype password:")); if (memcmp(crypt2, crypt2 + HEXKEYBYTES, KEYCHECKSUMSIZE) != 0 || - memcmp(crypt2, secret, HEXKEYBYTES) != 0) { - (void)fprintf(stderr, "Password incorrect.\n"); - exit(1); - } + memcmp(crypt2, secret, HEXKEYBYTES) != 0) + errx(1, "password incorrect"); } #ifdef YP - (void)printf("Sending key change request to %s...\n", master); + printf("Sending key change request to %s...\n", master); #endif status = setpublicmap(name, public, crypt1); if (status != 0) { #ifdef YP - (void)fprintf(stderr, - "%s: unable to update NIS database (%u): %s\n", - self, status, yperr_string(status)); + errx(1, "unable to update NIS database (%u): %s", + status, yperr_string(status)); #else - (void)fprintf(stderr, - "%s: unable to update publickey database\n", self); + errx(1, "unable to update publickey database"); #endif - exit(1); } if (uid == 0) { @@ -212,31 +190,27 @@ main(int argc, char **argv) fd = open(ROOTKEY, O_WRONLY|O_TRUNC|O_CREAT, 0); if (fd < 0) { - perror(ROOTKEY); + warn("%s", ROOTKEY); } else { char newline = '\n'; if (write(fd, secret, strlen(secret)) < 0 || - write(fd, &newline, sizeof(newline)) < 0) { - (void)fprintf(stderr, "%s: ", ROOTKEY); - perror("write"); - } + write(fd, &newline, sizeof(newline)) < 0) + warn("%s: write", ROOTKEY); } } - if (key_setsecret(secret) < 0) { - (void)printf("Unable to login with new secret key.\n"); - exit(1); - } - (void)printf("Done.\n"); + if (key_setsecret(secret) < 0) + errx(1, "unable to login with new secret key"); + printf("Done.\n"); exit(0); /* NOTREACHED */ } static void -usage(char *name) +usage(void) { - (void)fprintf(stderr, "usage: %s [-f]\n", name); + fprintf(stderr, "usage: chkey [-f]\n"); exit(1); /* NOTREACHED */ } @@ -249,8 +223,8 @@ static int setpublicmap(char *name, char *public, char *secret) { char pkent[1024]; - - (void)sprintf(pkent,"%s:%s", public, secret); + + sprintf(pkent,"%s:%s", public, secret); #ifdef YP return (yp_update(domain, PKMAP, YPOP_STORE, name, strlen(name), pkent, strlen(pkent))); @@ -270,13 +244,13 @@ ypgetpwuid(uid_t uid) static struct passwd pw; char *p; - (void)sprintf(uidstr, "%d", uid); - if (yp_match(domain, "passwd.byuid", uidstr, strlen(uidstr), + sprintf(uidstr, "%d", uid); + if (yp_match(domain, "passwd.byuid", uidstr, strlen(uidstr), &val, &vallen) != 0) { return (NULL); } p = strchr(val, ':'); - if (p == NULL) { + if (p == NULL) { return (NULL); } pw.pw_passwd = p + 1; -- 2.41.0