/contrib/ntp/libntp/authreadkeys.c

https://bitbucket.org/freebsd/freebsd-head/ · C · 162 lines · 102 code · 19 blank · 41 comment · 37 complexity · 26bd13ecc702daf9a98ef1dc6a0e8571 MD5 · raw file

  1. /*
  2. * authreadkeys.c - routines to support the reading of the key file
  3. */
  4. #include <stdio.h>
  5. #include <ctype.h>
  6. #include "ntp_fp.h"
  7. #include "ntp.h"
  8. #include "ntp_syslog.h"
  9. #include "ntp_stdlib.h"
  10. /*
  11. * Arbitrary long string of ASCII characters.
  12. */
  13. #define KEY_TYPE_MD5 4
  14. /* Forwards */
  15. static char *nexttok P((char **));
  16. /*
  17. * nexttok - basic internal tokenizing routine
  18. */
  19. static char *
  20. nexttok(
  21. char **str
  22. )
  23. {
  24. register char *cp;
  25. char *starttok;
  26. cp = *str;
  27. /*
  28. * Space past white space
  29. */
  30. while (*cp == ' ' || *cp == '\t')
  31. cp++;
  32. /*
  33. * Save this and space to end of token
  34. */
  35. starttok = cp;
  36. while (*cp != '\0' && *cp != '\n' && *cp != ' '
  37. && *cp != '\t' && *cp != '#')
  38. cp++;
  39. /*
  40. * If token length is zero return an error, else set end of
  41. * token to zero and return start.
  42. */
  43. if (starttok == cp)
  44. return 0;
  45. if (*cp == ' ' || *cp == '\t')
  46. *cp++ = '\0';
  47. else
  48. *cp = '\0';
  49. *str = cp;
  50. return starttok;
  51. }
  52. /*
  53. * authreadkeys - (re)read keys from a file.
  54. */
  55. int
  56. authreadkeys(
  57. const char *file
  58. )
  59. {
  60. FILE *fp;
  61. char *line;
  62. char *token;
  63. u_long keyno;
  64. int keytype;
  65. char buf[512]; /* lots of room for line */
  66. /*
  67. * Open file. Complain and return if it can't be opened.
  68. */
  69. fp = fopen(file, "r");
  70. if (fp == NULL) {
  71. msyslog(LOG_ERR, "can't open key file %s: %m", file);
  72. return 0;
  73. }
  74. /*
  75. * Remove all existing keys
  76. */
  77. auth_delkeys();
  78. /*
  79. * Now read lines from the file, looking for key entries
  80. */
  81. while ((line = fgets(buf, sizeof buf, fp)) != NULL) {
  82. token = nexttok(&line);
  83. if (token == 0)
  84. continue;
  85. /*
  86. * First is key number. See if it is okay.
  87. */
  88. keyno = atoi(token);
  89. if (keyno == 0) {
  90. msyslog(LOG_ERR,
  91. "cannot change keyid 0, key entry `%s' ignored",
  92. token);
  93. continue;
  94. }
  95. if (keyno > NTP_MAXKEY) {
  96. msyslog(LOG_ERR,
  97. "keyid's > %d reserved for autokey, key entry `%s' ignored",
  98. NTP_MAXKEY, token);
  99. continue;
  100. }
  101. /*
  102. * Next is keytype. See if that is all right.
  103. */
  104. token = nexttok(&line);
  105. if (token == 0) {
  106. msyslog(LOG_ERR,
  107. "no key type for key number %ld, entry ignored",
  108. keyno);
  109. continue;
  110. }
  111. switch (*token) {
  112. case 'M':
  113. case 'm':
  114. keytype = KEY_TYPE_MD5; break;
  115. default:
  116. msyslog(LOG_ERR,
  117. "invalid key type for key number %ld, entry ignored",
  118. keyno);
  119. continue;
  120. }
  121. /*
  122. * Finally, get key and insert it
  123. */
  124. token = nexttok(&line);
  125. if (token == 0) {
  126. msyslog(LOG_ERR,
  127. "no key for number %ld entry, entry ignored",
  128. keyno);
  129. } else {
  130. switch(keytype) {
  131. case KEY_TYPE_MD5:
  132. if (!authusekey(keyno, keytype,
  133. (u_char *)token))
  134. msyslog(LOG_ERR,
  135. "format/parity error for MD5 key %ld, not used",
  136. keyno);
  137. break;
  138. }
  139. }
  140. }
  141. (void) fclose(fp);
  142. return 1;
  143. }