]> git.ipfire.org Git - thirdparty/ntp.git/commitdiff
[Bug 3689] Extension for MD5, SHA-1 and other keys
authorJuergen Perlinger <perlinger@ntp.org>
Mon, 19 Oct 2020 18:27:38 +0000 (20:27 +0200)
committerJuergen Perlinger <perlinger@ntp.org>
Mon, 19 Oct 2020 18:27:38 +0000 (20:27 +0200)
 - enum for password encoding, symdef for secret size
 - update docs

bk: 5f8dda9ap5taVSmvX0Cuc0okzeKfIw

html/authentic.html
html/ntpdc.html
html/ntpq.html
include/ntp_stdlib.h
libntp/authkeys.c
libntp/authreadkeys.c
libntp/authusekey.c

index 52703e4566e5f9a8f909a6b579aa6861e345bb11..62066c3b54446f22fed07b143d7ab9f84435bbd5 100644 (file)
       Ordinarily, the <tt>ntp.keys</tt> file is generated by
       the <tt><a href="keygen.html">ntp-keygen</a></tt> program, but it can
       be constructed and edited using an ordinary text editor.</p>
-    <p> Each line of the keys file consists of three or four fields: a key
-      ID in the range 1 to 65,535, inclusive, a key type, a message digest
-      key consisting of a printable ASCII string less than 40 characters or
-      a 40-character hex digit string, and an optional comma-separated list
-      of IPs that are allowed to serve time.  If the OpenSSL library is
-      installed, the key type can be any message digest algorithm supported
-      by the library.  If the OpenSSL library is not installed, the only
+    <p> Each line of the keys file consists of three or four fields: a
+      key ID in the range 1 to 65,535, inclusive, a key type, a
+      message digest key consisting of a printable ASCII string up to
+      20 characters or a hex digit string with more than 20
+      characters, and an optional comma-separated list of IPs that are
+      allowed to serve time.  If the OpenSSL library is installed, the
+      key type can be any message digest algorithm supported by the
+      library.  If the OpenSSL library is not installed, the only
       permitted key type is MD5.</p>
     <table>
       <caption style="caption-side: bottom;">
index 7a68dd2f79061ef81774d937dc757be473397f6b..8a66a0d01118bd698105c16999808f4de475ea0f 100644 (file)
@@ -75,7 +75,8 @@
   <dt><tt>quit</tt></dt>
   <dd>Exit <tt>ntpdc</tt>.</dd>
   <dt><tt>passwd</tt></dt>
-  <dd>This command prompts you to type in a password (which will not be echoed) which will be used to authenticate configuration requests. The password must correspond to the key configured for use by the NTP server for this purpose if such requests are to be successful.</dd>
+  <dd>This command prompts you to type in a password (which will not be echoed) which will be used to authenticate configuration requests. The password must correspond to the key configured for use by the NTP server for this purpose if such requests are to be successful.
+  It follows the same rules as a password entry in the keys file of <tt>ntpd</tt>.</dd>
   <dt><tt>timeout <i>milliseconds</i></tt></dt>
   <dd>Specify a timeout period for responses to server queries. The default is about 8000 milliseconds. Note that since <tt>ntpdc</tt> retries each query once after a timeout, the total waiting time for a timeout will be twice the timeout value set.</dd>
 </dl>
index 4789c8ba9cdcfc292b383f7af919614054d01b0f..e143d8596755f984284c36606b03deacb00ef7c4 100644 (file)
@@ -75,7 +75,8 @@
   <dt id="ntpversion"><tt>ntpversion 1 | 2 | 3 | 4</tt></dt>
   <dd>Sets the NTP version number which <tt>ntpq</tt> claims in packets. Defaults to 2, Note that mode-6 control messages (and modes, for that matter) didn't exist in NTP version 1.</dd>
   <dt id="passwd"><tt>passwd</tt></dt>
-  <dd>This command prompts for a password to authenticate requests. The password must correspond to the key ID configured in <tt>ntp.conf</tt> for this purpose.</dd>
+  <dd>This command prompts for a password to authenticate requests. The password must correspond to the key ID configured in <tt>ntp.conf</tt> for this purpose.
+  It follows the same rules as a password entry in the keys file of <tt>ntpd</tt>.</dd>
   <dt id="quit"><tt>quit</tt></dt>
   <dd>Exit <tt>ntpq</tt>.</dd>
   <dt id="raw"><tt>raw</tt></dt>
index 873e9d90fa3461369f6770b94c84190cc62f4e41..d655286474338068ca201b2658e44b63116e62fa 100644 (file)
@@ -67,13 +67,17 @@ extern      int     xsbprintf(char**, char* const, char const*, ...) NTP_PRINTF(3, 4);
 typedef void (*ctrl_c_fn)(void);
 
 /* authkeys.c */
-#define AUTHPWD_UNSPEC 0
-#define AUTHPWD_PLAIN  1
-#define AUTHPWD_HEX    2
+#define AUTHPWD_MAXSECLEN      64      /* max. length of secret blob */
+
+enum AuthPwdEnc {
+       AUTHPWD_UNSPEC, /* format unspecified, length used for discrimination */
+       AUTHPWD_PLAIN,  /* plain text, used as is */
+       AUTHPWD_HEX     /* hex-encoded string */
+};
 
 extern void    auth_delkeys    (void);
 extern int     auth_havekey    (keyid_t);
-extern size_t  authdecodepw    (u_char *dst, size_t dstlen, const char *src, int fmt);
+extern size_t  authdecodepw    (u_char *dst, size_t dstlen, const char *src, enum AuthPwdEnc);
 extern int     authdecrypt     (keyid_t, u_int32 *, size_t, size_t);
 extern size_t  authencrypt     (keyid_t, u_int32 *, size_t);
 extern int     authhavekey     (keyid_t);
index bf9628b738d87a00cbd40055ac166046b0c51053..3d16228af0594c93cbbcab85f446c0c099caaea8 100644 (file)
@@ -119,7 +119,7 @@ keyacc_new_push(
        )
 {
        KeyAccT *       node = emalloc(sizeof(KeyAccT));
-       
+
        memcpy(&node->addr, addr, sizeof(sockaddr_u));
        node->subnetbits = subnetbits;
        node->next = head;
@@ -219,7 +219,7 @@ keyacc_amatch(
         * other checks if we find object identity. But that use case is
         * too rare to care for it.
         */
-       
+
        /* 2nd check: Address families must be the same. */
        if (AF(a1) != AF(a2))
                return FALSE;
@@ -317,7 +317,7 @@ free_auth_mem(void)
        cache_keyacclist = NULL;
        for (alloc = authallocs; alloc != NULL; alloc = next_alloc) {
                next_alloc = alloc->link;
-               free(alloc->mem);       
+               free(alloc->mem);
        }
        authfreekeys = NULL;
        authnumfreekeys = 0;
@@ -525,7 +525,7 @@ freesymkey(
 
        authcache_flush_id(sk->keyid);
        keyacc_all_free(sk->keyacclist);
-       
+
        bucket = &key_hash[KEYHASH(sk->keyid)];
        if (sk->secret != NULL) {
                memset(sk->secret, '\0', sk->secretsize);
@@ -649,7 +649,7 @@ authtrust(
         * There are two conditions remaining. Either it does not
         * exist and is to be trusted or it does exist and is or is
         * not to be trusted.
-        */     
+        */
        if (sk != NULL) {
                /*
                 * Key exists. If it is to be trusted, say so and update
@@ -727,9 +727,9 @@ authistrusted(
                return (KEY_TRUSTED & sk->flags) &&
                    keyacc_contains(sk->keyacclist, sau, TRUE);
        }
-       
+
        authkeynotfound++;
-       return FALSE;    
+       return FALSE;
 }
 
 /* Note: There are two locations below where 'strncpy()' is used. While
@@ -754,7 +754,7 @@ MD5auth_setkey(
 {
        symkey *        sk;
        u_char *        secret;
-       
+
        DEBUG_ENSURE(keytype <= USHRT_MAX);
        DEBUG_ENSURE(secretsize < 4 * 1024);
        /*
@@ -809,7 +809,7 @@ MD5auth_setkey(
                        printf("%02x", secret[j]);
                }
                printf("\n");
-       }       
+       }
 #endif
 }
 
@@ -953,7 +953,7 @@ pwdecode_hex(
        )
 {
        static const char hex[] = "00112233445566778899AaBbCcDdEeFf";
-       
+
        size_t          srclen = strlen(src);
        size_t          reslen = (srclen >> 1) + (srclen & 1);
        u_char          tmp;
@@ -963,7 +963,7 @@ pwdecode_hex(
        if (reslen > dstlen) {
                errno = ENOMEM;
                reslen = (size_t)-1;
-       } else {        
+       } else {
                for (j = 0; j < srclen; ++j) {
                        tmp = *(const unsigned char*)(src + j);
                        ptr = strchr(hex, tmp);
@@ -990,16 +990,16 @@ authdecodepw(
        u_char *        dst,
        size_t          dstlen,
        const char *    src,
-       int             fmt
+       enum AuthPwdEnc enc
        )
 {
        size_t          reslen;
-       
+
        if ( !(dst && dstlen && src)) {
                errno  = EINVAL;
                reslen = (size_t)-1;
        } else {
-               switch (fmt) {
+               switch (enc) {
                case AUTHPWD_UNSPEC:
                        if (strlen(src) <= 20)
                                reslen = pwdecode_plain(dst, dstlen, src);
index adf53beaea8023a2a470c61d2447185d107bf0ca..da91bd0d3443fdf3f4f317d71d3a6bb38047f5bc 100644 (file)
@@ -142,7 +142,7 @@ authreadkeys(
        keyid_t keyno;
        int     keytype;
        char    buf[512];               /* lots of room for line */
-       u_char  keystr[64];             /* Bug 2537 */
+       u_char  keystr[AUTHPWD_MAXSECLEN];
        size_t  len;
        u_int   nerr;
        KeyDataT *list = NULL;
index abc762188057b615499e0393a6484aeb6412134e..3c8da1d3c7c7da54ca8a50edbed21820cfbf2a24 100644 (file)
@@ -21,13 +21,13 @@ authusekey(
        )
 {
        size_t  len;
-       u_char  buf[64];
-       
+       u_char  buf[AUTHPWD_MAXSECLEN];
+
        len = authdecodepw(buf, sizeof(buf), (const char*)str,
                           AUTHPWD_UNSPEC);
        if (len < 1 || len > sizeof(buf))
                return 0;
-       
+
        MD5auth_setkey(keyno, keytype, buf, len, NULL);
        memset(buf, 0, sizeof(buf));
        return 1;