Sync usage with man page.
[netbsd-mini2440.git] / dist / ntp / libntp / authreadkeys.c
blobe691b2626c1855aa2fffa57e837e6fb524de99bb
1 /* $NetBSD$ */
3 /*
4 * authreadkeys.c - routines to support the reading of the key file
5 */
6 #include <stdio.h>
7 #include <ctype.h>
9 #include "ntp_fp.h"
10 #include "ntp.h"
11 #include "ntp_syslog.h"
12 #include "ntp_stdlib.h"
15 * Arbitrary long string of ASCII characters.
17 #define KEY_TYPE_MD5 4
19 /* Forwards */
20 static char *nexttok P((char **));
23 * nexttok - basic internal tokenizing routine
25 static char *
26 nexttok(
27 char **str
30 register char *cp;
31 char *starttok;
33 cp = *str;
36 * Space past white space
38 while (*cp == ' ' || *cp == '\t')
39 cp++;
42 * Save this and space to end of token
44 starttok = cp;
45 while (*cp != '\0' && *cp != '\n' && *cp != ' '
46 && *cp != '\t' && *cp != '#')
47 cp++;
50 * If token length is zero return an error, else set end of
51 * token to zero and return start.
53 if (starttok == cp)
54 return 0;
56 if (*cp == ' ' || *cp == '\t')
57 *cp++ = '\0';
58 else
59 *cp = '\0';
61 *str = cp;
62 return starttok;
67 * authreadkeys - (re)read keys from a file.
69 int
70 authreadkeys(
71 const char *file
74 FILE *fp;
75 char *line;
76 char *token;
77 u_long keyno;
78 int keytype;
79 char buf[512]; /* lots of room for line */
82 * Open file. Complain and return if it can't be opened.
84 fp = fopen(file, "r");
85 if (fp == NULL) {
86 msyslog(LOG_ERR, "can't open key file %s: %m", file);
87 return 0;
91 * Remove all existing keys
93 auth_delkeys();
96 * Now read lines from the file, looking for key entries
98 while ((line = fgets(buf, sizeof buf, fp)) != NULL) {
99 token = nexttok(&line);
100 if (token == 0)
101 continue;
104 * First is key number. See if it is okay.
106 keyno = atoi(token);
107 if (keyno == 0) {
108 msyslog(LOG_ERR,
109 "cannot change keyid 0, key entry `%s' ignored",
110 token);
111 continue;
114 if (keyno > NTP_MAXKEY) {
115 msyslog(LOG_ERR,
116 "keyid's > %d reserved for autokey, key entry `%s' ignored",
117 NTP_MAXKEY, token);
118 continue;
122 * Next is keytype. See if that is all right.
124 token = nexttok(&line);
125 if (token == 0) {
126 msyslog(LOG_ERR,
127 "no key type for key number %ld, entry ignored",
128 keyno);
129 continue;
131 switch (*token) {
132 case 'M':
133 case 'm':
134 keytype = KEY_TYPE_MD5; break;
135 default:
136 msyslog(LOG_ERR,
137 "invalid key type for key number %ld, entry ignored",
138 keyno);
139 continue;
143 * Finally, get key and insert it
145 token = nexttok(&line);
146 if (token == 0) {
147 msyslog(LOG_ERR,
148 "no key for number %ld entry, entry ignored",
149 keyno);
150 } else {
151 switch(keytype) {
152 case KEY_TYPE_MD5:
153 if (!authusekey(keyno, keytype,
154 (u_char *)token))
155 msyslog(LOG_ERR,
156 "format/parity error for MD5 key %ld, not used",
157 keyno);
158 break;
162 (void) fclose(fp);
163 return 1;