chkey.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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, MERCHANTABILITY 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. #include <sys/cdefs.h>
  31. /*
  32. * Copyright (C) 1986, Sun Microsystems, Inc.
  33. */
  34. /*
  35. * Command to change one's public key in the public key database
  36. */
  37. #include <rpc/rpc.h>
  38. #include <rpc/key_prot.h>
  39. #ifdef YP
  40. #include <rpcsvc/yp_prot.h>
  41. #include <rpcsvc/ypclnt.h>
  42. #else
  43. #define YPOP_STORE 4
  44. #endif
  45. #include <sys/fcntl.h>
  46. #include <err.h>
  47. #include <pwd.h>
  48. #include <stdio.h>
  49. #include <stdlib.h>
  50. #include <string.h>
  51. #include <unistd.h>
  52. #include "extern.h"
  53. #ifdef YPPASSWD
  54. struct passwd *ypgetpwuid(uid_t);
  55. #endif
  56. #ifdef YP
  57. static char *domain;
  58. static char PKMAP[] = "publickey.byname";
  59. #else
  60. static char PKFILE[] = "/etc/publickey";
  61. #endif /* YP */
  62. static char ROOTKEY[] = "/etc/.rootkey";
  63. static void usage(void) __dead2;
  64. extern int yp_update(char *, char *, int, char *, size_t, char *, size_t);
  65. int
  66. main(int argc, char **argv)
  67. {
  68. char name[MAXNETNAMELEN+1];
  69. char public[HEXKEYBYTES + 1];
  70. char secret[HEXKEYBYTES + 1];
  71. char crypt1[HEXKEYBYTES + KEYCHECKSUMSIZE + 1];
  72. char crypt2[HEXKEYBYTES + KEYCHECKSUMSIZE + 1];
  73. int status;
  74. char *pass;
  75. struct passwd *pw;
  76. uid_t uid;
  77. int force = 0;
  78. int ch;
  79. #ifdef YP
  80. char *master;
  81. #endif
  82. #ifdef YPPASSWD
  83. char *cryptpw;
  84. #endif
  85. while ((ch = getopt(argc, argv, "f")) != -1)
  86. switch(ch) {
  87. case 'f':
  88. force = 1;
  89. break;
  90. default:
  91. usage();
  92. }
  93. argc -= optind;
  94. argv += optind;
  95. if (argc != 0)
  96. usage();
  97. #ifdef YP
  98. (void)yp_get_default_domain(&domain);
  99. if (yp_master(domain, PKMAP, &master) != 0)
  100. errx(1, "can't find master of publickey database");
  101. #endif
  102. uid = getuid() /*geteuid()*/;
  103. if (uid == 0) {
  104. if (host2netname(name, NULL, NULL) == 0)
  105. errx(1, "cannot convert hostname to netname");
  106. } else {
  107. if (user2netname(name, uid, NULL) == 0)
  108. errx(1, "cannot convert username to netname");
  109. }
  110. (void)printf("Generating new key for %s.\n", name);
  111. if (!force) {
  112. if (uid != 0) {
  113. #ifdef YPPASSWD
  114. pw = ypgetpwuid(uid);
  115. #else
  116. pw = getpwuid(uid);
  117. #endif
  118. if (pw == NULL) {
  119. #ifdef YPPASSWD
  120. errx(1,
  121. "no NIS password entry found: can't change key");
  122. #else
  123. errx(1,
  124. "no password entry found: can't change key");
  125. #endif
  126. }
  127. } else {
  128. pw = getpwuid(0);
  129. if (pw == NULL)
  130. errx(1, "no password entry found: can't change key");
  131. }
  132. }
  133. pass = getpass("Password:");
  134. #ifdef YPPASSWD
  135. if (!force) {
  136. cryptpw = crypt(pass, pw->pw_passwd);
  137. if (cryptpw == NULL || strcmp(cryptpw, pw->pw_passwd) != 0)
  138. errx(1, "invalid password");
  139. }
  140. #else
  141. force = 1; /* Make this mandatory */
  142. #endif
  143. genkeys(public, secret, pass);
  144. memcpy(crypt1, secret, HEXKEYBYTES);
  145. memcpy(crypt1 + HEXKEYBYTES, secret, KEYCHECKSUMSIZE);
  146. crypt1[HEXKEYBYTES + KEYCHECKSUMSIZE] = 0;
  147. xencrypt(crypt1, pass);
  148. if (force) {
  149. memcpy(crypt2, crypt1, HEXKEYBYTES + KEYCHECKSUMSIZE + 1);
  150. xdecrypt(crypt2, getpass("Retype password:"));
  151. if (memcmp(crypt2, crypt2 + HEXKEYBYTES, KEYCHECKSUMSIZE) != 0
  152. || memcmp(crypt2, secret, HEXKEYBYTES) != 0)
  153. errx(1, "password incorrect");
  154. }
  155. #ifdef YP
  156. (void)printf("Sending key change request to %s...\n", master);
  157. #endif
  158. status = setpublicmap(name, public, crypt1);
  159. if (status != 0) {
  160. #ifdef YP
  161. errx(1, "unable to update NIS database (%u): %s",
  162. status, yperr_string(status));
  163. #else
  164. errx(1, "unable to update publickey database");
  165. #endif
  166. }
  167. if (uid == 0) {
  168. /*
  169. * Root users store their key in /etc/$ROOTKEY so
  170. * that they can auto reboot without having to be
  171. * around to type a password. Storing this in a file
  172. * is rather dubious: it should really be in the EEPROM
  173. * so it does not go over the net.
  174. */
  175. int fd;
  176. fd = open(ROOTKEY, O_WRONLY|O_TRUNC|O_CREAT, 0);
  177. if (fd < 0) {
  178. warn("%s", ROOTKEY);
  179. } else {
  180. char newline = '\n';
  181. if (write(fd, secret, strlen(secret)) < 0 ||
  182. write(fd, &newline, sizeof(newline)) < 0)
  183. warn("%s: write", ROOTKEY);
  184. }
  185. close(fd);
  186. }
  187. if (key_setsecret(secret) < 0)
  188. errx(1, "unable to login with new secret key");
  189. (void)printf("Done.\n");
  190. exit(0);
  191. /* NOTREACHED */
  192. }
  193. static void
  194. usage(void)
  195. {
  196. (void)fprintf(stderr, "usage: chkey [-f]\n");
  197. exit(1);
  198. /* NOTREACHED */
  199. }
  200. /*
  201. * Set the entry in the public key file
  202. */
  203. int
  204. setpublicmap(char *name, char *public, char *secret)
  205. {
  206. char pkent[1024];
  207. (void)sprintf(pkent,"%s:%s", public, secret);
  208. #ifdef YP
  209. return (yp_update(domain, PKMAP, YPOP_STORE,
  210. name, strlen(name), pkent, strlen(pkent)));
  211. #else
  212. return (localupdate(name, PKFILE, YPOP_STORE,
  213. strlen(name), name, strlen(pkent), pkent));
  214. #endif
  215. }
  216. #ifdef YPPASSWD
  217. struct passwd *
  218. ypgetpwuid(uid_t uid)
  219. {
  220. char uidstr[10];
  221. char *val;
  222. int vallen;
  223. static struct passwd pw;
  224. char *p;
  225. (void)sprintf(uidstr, "%d", uid);
  226. if (yp_match(domain, "passwd.byuid", uidstr, strlen(uidstr),
  227. &val, &vallen) != 0) {
  228. return (NULL);
  229. }
  230. p = strchr(val, ':');
  231. if (p == NULL) {
  232. return (NULL);
  233. }
  234. pw.pw_passwd = p + 1;
  235. p = strchr(pw.pw_passwd, ':');
  236. if (p == NULL) {
  237. return (NULL);
  238. }
  239. *p = 0;
  240. return (&pw);
  241. }
  242. #endif /* YPPASSWD */