1 /* 2 * Sun RPC is a product of Sun Microsystems, Inc. and is provided for 3 * unrestricted use provided that this legend is included on all tape 4 * media and as a part of the software program in whole or part. Users 5 * may copy or modify Sun RPC without charge, but are not authorized 6 * to license or distribute it to anyone else except as part of a product or 7 * program developed by the user or with the express written consent of 8 * Sun Microsystems, Inc. 9 * 10 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE 11 * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR 12 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE. 13 * 14 * Sun RPC is provided with no support and without any obligation on the 15 * part of Sun Microsystems, Inc. to assist in its use, correction, 16 * modification or enhancement. 17 * 18 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE 19 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC 20 * OR ANY PART THEREOF. 21 * 22 * In no event will Sun Microsystems, Inc. be liable for any lost revenue 23 * or profits or other special, indirect and consequential damages, even if 24 * Sun has been advised of the possibility of such damages. 25 * 26 * Sun Microsystems, Inc. 27 * 2550 Garcia Avenue 28 * Mountain View, California 94043 29 */ 30 31 #if !defined(lint) && defined(SCCSIDS) 32 #if 0 33 static char sccsid[] = "@(#)newkey.c 1.8 91/03/11 Copyr 1986 Sun Micro"; 34 #endif 35 #endif 36 37 /* 38 * Copyright (C) 1986, Sun Microsystems, Inc. 39 */ 40 41 /* 42 * Administrative tool to add a new user to the publickey database 43 */ 44 #include <sys/cdefs.h> 45 __FBSDID("$FreeBSD$"); 46 47 #include <sys/types.h> 48 #include <sys/time.h> 49 #include <sys/resource.h> 50 51 #include <rpc/rpc.h> 52 #include <rpc/key_prot.h> 53 54 #ifdef YP 55 #include <sys/wait.h> 56 #include <rpcsvc/yp_prot.h> 57 #include <rpcsvc/ypclnt.h> 58 #include <netdb.h> 59 #endif /* YP */ 60 61 #include <err.h> 62 #include <pwd.h> 63 #include <stdio.h> 64 #include <string.h> 65 #include <unistd.h> 66 67 #include "extern.h" 68 69 #ifdef YP 70 #define MAXMAPNAMELEN 256 71 #else 72 #define YPOP_CHANGE 1 /* change, do not add */ 73 #define YPOP_INSERT 2 /* add, do not change */ 74 #define YPOP_DELETE 3 /* delete this entry */ 75 #define YPOP_STORE 4 /* add, or change */ 76 #define ERR_ACCESS 1 77 #define ERR_MALLOC 2 78 #define ERR_READ 3 79 #define ERR_WRITE 4 80 #define ERR_DBASE 5 81 #define ERR_KEY 6 82 #endif 83 84 #ifdef YP 85 static char YPDBPATH[]="/var/yp"; 86 static char PKMAP[] = "publickey.byname"; 87 #else 88 static char PKFILE[] = "/etc/publickey"; 89 static char *err_string(); 90 #endif /* YP */ 91 92 static void usage(void); 93 94 int 95 main(int argc, char *argv[]) 96 { 97 char name[MAXNETNAMELEN + 1]; 98 char public[HEXKEYBYTES + 1]; 99 char secret[HEXKEYBYTES + 1]; 100 char crypt1[HEXKEYBYTES + KEYCHECKSUMSIZE + 1]; 101 char crypt2[HEXKEYBYTES + KEYCHECKSUMSIZE + 1]; 102 int status; 103 char *pass; 104 struct passwd *pw; 105 #ifdef undef 106 struct hostent *h; 107 #endif 108 109 if (argc != 3 || !(strcmp(argv[1], "-u") == 0 || 110 strcmp(argv[1], "-h") == 0)) { 111 usage(); 112 } 113 if (geteuid() != 0) 114 errx(1, "must be superuser"); 115 116 #ifdef YP 117 if (chdir(YPDBPATH) < 0) 118 warn("cannot chdir to %s", YPDBPATH); 119 #endif /* YP */ 120 if (strcmp(argv[1], "-u") == 0) { 121 pw = getpwnam(argv[2]); 122 if (pw == NULL) 123 errx(1, "unknown user: %s", argv[2]); 124 (void)user2netname(name, (int)pw->pw_uid, (char *)NULL); 125 } else { 126 #ifdef undef 127 h = gethostbyname(argv[2]); 128 if (h == NULL) 129 errx(1, "unknown host: %s", argv[1]); 130 (void)host2netname(name, h->h_name, (char *)NULL); 131 #else 132 (void)host2netname(name, argv[2], (char *)NULL); 133 #endif 134 } 135 136 (void)printf("Adding new key for %s.\n", name); 137 pass = getpass("New password:"); 138 genkeys(public, secret, pass); 139 140 memcpy(crypt1, secret, HEXKEYBYTES); 141 memcpy(crypt1 + HEXKEYBYTES, secret, KEYCHECKSUMSIZE); 142 crypt1[HEXKEYBYTES + KEYCHECKSUMSIZE] = 0; 143 xencrypt(crypt1, pass); 144 145 memcpy(crypt2, crypt1, HEXKEYBYTES + KEYCHECKSUMSIZE + 1); 146 xdecrypt(crypt2, getpass("Retype password:")); 147 if (memcmp(crypt2, crypt2 + HEXKEYBYTES, KEYCHECKSUMSIZE) != 0 || 148 memcmp(crypt2, secret, HEXKEYBYTES) != 0) 149 errx(1, "password incorrect"); 150 151 #ifdef YP 152 (void)printf("Please wait for the database to get updated...\n"); 153 #endif 154 if ((status = setpublicmap(name, public, crypt1))) { 155 #ifdef YP 156 errx(1, "unable to update NIS database (%u): %s", 157 status, yperr_string(status)); 158 #else 159 errx(1, "unable to update publickey database (%u): %s", 160 status, err_string(status)); 161 #endif 162 } 163 (void)printf("Your new key has been successfully stored away.\n"); 164 exit(0); 165 /* NOTREACHED */ 166 } 167 168 static void 169 usage(void) 170 { 171 (void)fprintf(stderr, "%s\n%s\n", 172 "usage: newkey [-u username]", 173 " newkey [-h hostname]"); 174 exit(1); 175 } 176 177 /* 178 * Set the entry in the public key file 179 */ 180 int 181 setpublicmap(char *name, char *public, char *secret) 182 { 183 char pkent[1024]; 184 185 (void)sprintf(pkent, "%s:%s", public, secret); 186 #ifdef YP 187 return (mapupdate(name, PKMAP, YPOP_STORE, 188 strlen(name), name, strlen(pkent), pkent)); 189 #else 190 return (localupdate(name, PKFILE, YPOP_STORE, 191 strlen(name), name, strlen(pkent), pkent)); 192 #endif 193 } 194 195 #ifndef YP 196 /* 197 * This returns a pointer to an error message string appropriate 198 * to an input error code. An input value of zero will return 199 * a success message. 200 */ 201 static char * 202 err_string(int code) 203 { 204 char *pmesg; 205 206 switch (code) { 207 case 0: 208 pmesg = "update operation succeeded"; 209 break; 210 case ERR_KEY: 211 pmesg = "no such key in file"; 212 break; 213 case ERR_READ: 214 pmesg = "cannot read the database"; 215 break; 216 case ERR_WRITE: 217 pmesg = "cannot write to the database"; 218 break; 219 case ERR_DBASE: 220 pmesg = "cannot update database"; 221 break; 222 case ERR_ACCESS: 223 pmesg = "permission denied"; 224 break; 225 case ERR_MALLOC: 226 pmesg = "malloc failed"; 227 break; 228 default: 229 pmesg = "unknown error"; 230 break; 231 } 232 return (pmesg); 233 } 234 #endif 235