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.
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.
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.
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.
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.
26 * Sun Microsystems, Inc.
28 * Mountain View, California 94043
32 * Copyright (C) 1986, Sun Microsystems, Inc.
36 * Administrative tool to add a new user to the publickey database
39 #include <sys/types.h>
41 #include <sys/resource.h>
44 #include <rpc/key_prot.h>
48 #include <rpcsvc/yp_prot.h>
49 #include <rpcsvc/ypclnt.h>
63 #define MAXMAPNAMELEN 256
65 #define YPOP_CHANGE 1 /* change, do not add */
66 #define YPOP_INSERT 2 /* add, do not change */
67 #define YPOP_DELETE 3 /* delete this entry */
68 #define YPOP_STORE 4 /* add, or change */
78 static char YPDBPATH[]="/var/yp";
79 static char PKMAP[] = "publickey.byname";
81 static char PKFILE[] = "/etc/publickey";
82 static const char *err_string(int);
85 static void usage(void) __dead2;
88 main(int argc, char *argv[])
90 char name[MAXNETNAMELEN + 1];
91 char public[HEXKEYBYTES + 1];
92 char secret[HEXKEYBYTES + 1];
93 char crypt1[HEXKEYBYTES + KEYCHECKSUMSIZE + 1];
94 char crypt2[HEXKEYBYTES + KEYCHECKSUMSIZE + 1];
102 if (argc != 3 || !(strcmp(argv[1], "-u") == 0 ||
103 strcmp(argv[1], "-h") == 0)) {
107 errx(1, "must be superuser");
110 if (chdir(YPDBPATH) < 0)
111 warn("cannot chdir to %s", YPDBPATH);
113 if (strcmp(argv[1], "-u") == 0) {
114 pw = getpwnam(argv[2]);
116 errx(1, "unknown user: %s", argv[2]);
117 (void)user2netname(name, (int)pw->pw_uid, (char *)NULL);
120 h = gethostbyname(argv[2]);
122 errx(1, "unknown host: %s", argv[1]);
123 (void)host2netname(name, h->h_name, (char *)NULL);
125 (void)host2netname(name, argv[2], (char *)NULL);
129 (void)printf("Adding new key for %s.\n", name);
130 pass = getpass("New password:");
131 genkeys(public, secret, pass);
133 memcpy(crypt1, secret, HEXKEYBYTES);
134 memcpy(crypt1 + HEXKEYBYTES, secret, KEYCHECKSUMSIZE);
135 crypt1[HEXKEYBYTES + KEYCHECKSUMSIZE] = 0;
136 xencrypt(crypt1, pass);
138 memcpy(crypt2, crypt1, HEXKEYBYTES + KEYCHECKSUMSIZE + 1);
139 xdecrypt(crypt2, getpass("Retype password:"));
140 if (memcmp(crypt2, crypt2 + HEXKEYBYTES, KEYCHECKSUMSIZE) != 0 ||
141 memcmp(crypt2, secret, HEXKEYBYTES) != 0)
142 errx(1, "password incorrect");
145 (void)printf("Please wait for the database to get updated...\n");
147 if ((status = setpublicmap(name, public, crypt1))) {
149 errx(1, "unable to update NIS database (%u): %s",
150 status, yperr_string(status));
152 errx(1, "unable to update publickey database (%u): %s",
153 status, err_string(status));
156 (void)printf("Your new key has been successfully stored away.\n");
164 (void)fprintf(stderr, "%s\n%s\n",
165 "usage: newkey -h hostname",
166 " newkey -u username");
171 * Set the entry in the public key file
174 setpublicmap(char *name, char *public, char *secret)
178 (void)sprintf(pkent, "%s:%s", public, secret);
180 return (mapupdate(name, PKMAP, YPOP_STORE,
181 strlen(name), name, strlen(pkent), pkent));
183 return (localupdate(name, PKFILE, YPOP_STORE,
184 strlen(name), name, strlen(pkent), pkent));
190 * This returns a pointer to an error message string appropriate
191 * to an input error code. An input value of zero will return
201 pmesg = "update operation succeeded";
204 pmesg = "no such key in file";
207 pmesg = "cannot read the database";
210 pmesg = "cannot write to the database";
213 pmesg = "cannot update database";
216 pmesg = "permission denied";
219 pmesg = "malloc failed";
222 pmesg = "unknown error";