srp-entry.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. /*
  2. * Utility program for generating entries in /etc/ppp/srp-secrets
  3. *
  4. * Copyright (c) 2001 by Sun Microsystems, Inc.
  5. * All rights reserved.
  6. *
  7. * Non-exclusive rights to redistribute, modify, translate, and use
  8. * this software in source and binary forms, in whole or in part, is
  9. * hereby granted, provided that the above copyright notice is
  10. * duplicated in any source form, and that neither the name of the
  11. * copyright holder nor the author is used to endorse or promote
  12. * products derived from this software.
  13. *
  14. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
  15. * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
  16. * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  17. *
  18. * Original version by James Carlson
  19. *
  20. * Usage:
  21. * srp-entry [-i index] [clientname]
  22. *
  23. * Index, if supplied, is the modulus/generator index from
  24. * /etc/tpasswd.conf. If not supplied, then the last (highest
  25. * numbered) entry from that file is used. If the file doesn't exist,
  26. * then the default "well known" EAP SRP-SHA1 modulus/generator is
  27. * used.
  28. *
  29. * The default modulus/generator can be requested as index 0.
  30. */
  31. #include <stdio.h>
  32. #include <stdlib.h>
  33. #include <unistd.h>
  34. #include <string.h>
  35. #include <ctype.h>
  36. #include <t_pwd.h>
  37. #ifndef SOL2
  38. #define getpassphrase getpass
  39. #endif
  40. #define HAS_SPACE 1
  41. #define HAS_DQUOTE 2
  42. #define HAS_SQUOTE 4
  43. #define HAS_BACKSLASH 8
  44. static const u_char wkmodulus[] = {
  45. 0xAC, 0x6B, 0xDB, 0x41, 0x32, 0x4A, 0x9A, 0x9B,
  46. 0xF1, 0x66, 0xDE, 0x5E, 0x13, 0x89, 0x58, 0x2F,
  47. 0xAF, 0x72, 0xB6, 0x65, 0x19, 0x87, 0xEE, 0x07,
  48. 0xFC, 0x31, 0x92, 0x94, 0x3D, 0xB5, 0x60, 0x50,
  49. 0xA3, 0x73, 0x29, 0xCB, 0xB4, 0xA0, 0x99, 0xED,
  50. 0x81, 0x93, 0xE0, 0x75, 0x77, 0x67, 0xA1, 0x3D,
  51. 0xD5, 0x23, 0x12, 0xAB, 0x4B, 0x03, 0x31, 0x0D,
  52. 0xCD, 0x7F, 0x48, 0xA9, 0xDA, 0x04, 0xFD, 0x50,
  53. 0xE8, 0x08, 0x39, 0x69, 0xED, 0xB7, 0x67, 0xB0,
  54. 0xCF, 0x60, 0x95, 0x17, 0x9A, 0x16, 0x3A, 0xB3,
  55. 0x66, 0x1A, 0x05, 0xFB, 0xD5, 0xFA, 0xAA, 0xE8,
  56. 0x29, 0x18, 0xA9, 0x96, 0x2F, 0x0B, 0x93, 0xB8,
  57. 0x55, 0xF9, 0x79, 0x93, 0xEC, 0x97, 0x5E, 0xEA,
  58. 0xA8, 0x0D, 0x74, 0x0A, 0xDB, 0xF4, 0xFF, 0x74,
  59. 0x73, 0x59, 0xD0, 0x41, 0xD5, 0xC3, 0x3E, 0xA7,
  60. 0x1D, 0x28, 0x1E, 0x44, 0x6B, 0x14, 0x77, 0x3B,
  61. 0xCA, 0x97, 0xB4, 0x3A, 0x23, 0xFB, 0x80, 0x16,
  62. 0x76, 0xBD, 0x20, 0x7A, 0x43, 0x6C, 0x64, 0x81,
  63. 0xF1, 0xD2, 0xB9, 0x07, 0x87, 0x17, 0x46, 0x1A,
  64. 0x5B, 0x9D, 0x32, 0xE6, 0x88, 0xF8, 0x77, 0x48,
  65. 0x54, 0x45, 0x23, 0xB5, 0x24, 0xB0, 0xD5, 0x7D,
  66. 0x5E, 0xA7, 0x7A, 0x27, 0x75, 0xD2, 0xEC, 0xFA,
  67. 0x03, 0x2C, 0xFB, 0xDB, 0xF5, 0x2F, 0xB3, 0x78,
  68. 0x61, 0x60, 0x27, 0x90, 0x04, 0xE5, 0x7A, 0xE6,
  69. 0xAF, 0x87, 0x4E, 0x73, 0x03, 0xCE, 0x53, 0x29,
  70. 0x9C, 0xCC, 0x04, 0x1C, 0x7B, 0xC3, 0x08, 0xD8,
  71. 0x2A, 0x56, 0x98, 0xF3, 0xA8, 0xD0, 0xC3, 0x82,
  72. 0x71, 0xAE, 0x35, 0xF8, 0xE9, 0xDB, 0xFB, 0xB6,
  73. 0x94, 0xB5, 0xC8, 0x03, 0xD8, 0x9F, 0x7A, 0xE4,
  74. 0x35, 0xDE, 0x23, 0x6D, 0x52, 0x5F, 0x54, 0x75,
  75. 0x9B, 0x65, 0xE3, 0x72, 0xFC, 0xD6, 0x8E, 0xF2,
  76. 0x0F, 0xA7, 0x11, 0x1F, 0x9E, 0x4A, 0xFF, 0x73
  77. };
  78. static const char *myname;
  79. static void
  80. usage(void)
  81. {
  82. (void) fprintf(stderr, "Usage:\n\t%s [-i index] [clientname]\n",
  83. myname);
  84. exit(1);
  85. }
  86. int
  87. main(int argc, char **argv)
  88. {
  89. struct t_conf *tc;
  90. struct t_confent *tcent, mytce;
  91. struct t_pw pwval;
  92. char *name;
  93. char pname[256];
  94. char *pass1, *pass2;
  95. int flags, idx;
  96. char *cp;
  97. char delimit;
  98. char strbuf[MAXB64PARAMLEN];
  99. char saltbuf[MAXB64SALTLEN];
  100. if ((myname = *argv) == NULL)
  101. myname = "srp-entry";
  102. else
  103. argv++;
  104. idx = -1;
  105. if (*argv != NULL && strcmp(*argv, "-i") == 0) {
  106. if (*++argv == NULL)
  107. usage();
  108. idx = atoi(*argv++);
  109. }
  110. tcent = NULL;
  111. if (idx != 0 && (tc = t_openconf(NULL)) != NULL) {
  112. if (idx == -1)
  113. tcent = t_getconflast(tc);
  114. else
  115. tcent = t_getconfbyindex(tc, idx);
  116. }
  117. if (idx <= 0 && tcent == NULL) {
  118. mytce.index = 0;
  119. mytce.modulus.data = (u_char *)wkmodulus;
  120. mytce.modulus.len = sizeof (wkmodulus);
  121. mytce.generator.data = (u_char *)"\002";
  122. mytce.generator.len = 1;
  123. tcent = &mytce;
  124. }
  125. if (tcent == NULL) {
  126. (void) fprintf(stderr, "SRP modulus/generator %d not found\n",
  127. idx);
  128. exit(1);
  129. }
  130. if ((name = *argv) == NULL) {
  131. (void) printf("Client name: ");
  132. if (fgets(pname, sizeof (pname), stdin) == NULL)
  133. exit(1);
  134. if ((cp = strchr(pname, '\n')) != NULL)
  135. *cp = '\0';
  136. name = pname;
  137. }
  138. for (;;) {
  139. if ((pass1 = getpassphrase("Pass phrase: ")) == NULL)
  140. exit(1);
  141. pass1 = strdup(pass1);
  142. if ((pass2 = getpassphrase("Re-enter phrase: ")) == NULL)
  143. exit(1);
  144. if (strcmp(pass1, pass2) == 0)
  145. break;
  146. free(pass1);
  147. (void) printf("Phrases don't match; try again.\n");
  148. }
  149. memset(&pwval, 0, sizeof (pwval));
  150. t_makepwent(&pwval, name, pass1, NULL, tcent);
  151. flags = 0;
  152. for (cp = name; *cp != '\0'; cp++)
  153. if (isspace(*cp))
  154. flags |= HAS_SPACE;
  155. else if (*cp == '"')
  156. flags |= HAS_DQUOTE;
  157. else if (*cp == '\'')
  158. flags |= HAS_SQUOTE;
  159. else if (*cp == '\\')
  160. flags |= HAS_BACKSLASH;
  161. delimit = flags == 0 ? '\0' : (flags & HAS_DQUOTE) ? '\'' : '"';
  162. if (delimit != '\0')
  163. (void) putchar(delimit);
  164. for (cp = name; *cp != '\0'; cp++) {
  165. if (*cp == delimit || *cp == '\\')
  166. (void) putchar('\\');
  167. (void) putchar(*cp);
  168. }
  169. if (delimit != '\0')
  170. (void) putchar(delimit);
  171. (void) printf(" * %d:%s:%s *\n",
  172. pwval.pebuf.index, t_tob64(strbuf,
  173. (char *)pwval.pebuf.password.data, pwval.pebuf.password.len),
  174. t_tob64(saltbuf, (char *)pwval.pebuf.salt.data,
  175. pwval.pebuf.salt.len));
  176. return 0;
  177. }