update to 9.7.1-P2
[tridge/bind9.git] / contrib / zkt / dki.c
index c6320294bd2088292981c6ddb8947ed40318d0f1..b6a68db4bcb5059cc6b2ab6f5c6d8e3127df9595 100644 (file)
@@ -400,7 +400,7 @@ int dki_readdir (const char *dir, dki_t **listp, int recursive)
 
        while ( (dentp = readdir (dirp)) != NULL )
        {
-               if ( is_dotfile (dentp->d_name) )
+               if ( is_dotfilename (dentp->d_name) )
                        continue;
 
                dbg_val ("directory: check %s\n", dentp->d_name);
@@ -679,7 +679,6 @@ int dki_prt_dnskeyttl (const dki_t *dkp, FILE *fp, int ttl)
 {
        char    *p;
 
-       dki_estr[0] = '\0';
        if ( dkp == NULL )
                return 0;
 
@@ -711,7 +710,6 @@ int dki_prt_dnskey_raw (const dki_t *dkp, FILE *fp)
 {
        int     days;
 
-       dki_estr[0] = '\0';
        if ( dkp == NULL )
                return 0;
 
@@ -741,7 +739,6 @@ int dki_prt_comment (const dki_t *dkp, FILE *fp)
 {
        int     len = 0;
 
-       dki_estr[0] = '\0';
        if ( dkp == NULL )
                return len;
        len += fprintf (fp, "; %s  ", dkp->name);
@@ -761,7 +758,6 @@ int dki_prt_trustedkey (const dki_t *dkp, FILE *fp)
        int     spaces;
        int     len = 0;
 
-       dki_estr[0] = '\0';
        if ( dkp == NULL )
                return len;
        len += fprintf (fp, "\"%s\"  ", dkp->name);
@@ -792,7 +788,6 @@ int dki_cmp (const dki_t *a, const dki_t *b)
 {
        int     res;
 
-       dki_estr[0] = '\0';
        if ( a == NULL ) return -1;
        if ( b == NULL ) return 1;
 
@@ -816,7 +811,6 @@ int dki_allcmp (const dki_t *a, const dki_t *b)
 {
        int     res;
 
-       dki_estr[0] = '\0';
        if ( a == NULL ) return -1;
        if ( b == NULL ) return 1;
 
@@ -842,7 +836,6 @@ int dki_allcmp (const dki_t *a, const dki_t *b)
 *****************************************************************/
 int    dki_namecmp (const dki_t *a, const dki_t *b)
 {
-       dki_estr[0] = '\0';
        if ( a == NULL ) return -1;
        if ( b == NULL ) return 1;
 
@@ -853,7 +846,6 @@ int dki_namecmp (const dki_t *a, const dki_t *b)
 *****************************************************************/
 int    dki_tagcmp (const dki_t *a, const dki_t *b)
 {
-       dki_estr[0] = '\0';
        if ( a == NULL ) return -1;
        if ( b == NULL ) return 1;
 
@@ -866,19 +858,26 @@ int       dki_tagcmp (const dki_t *a, const dki_t *b)
 *****************************************************************/
 int    dki_timecmp (const dki_t *a, const dki_t *b)
 {
-       dki_estr[0] = '\0';
        if ( a == NULL ) return -1;
        if ( b == NULL ) return 1;
 
        return ((ulong)a->time - (ulong)b->time);
 }
 
+/*****************************************************************
+**     dki_algo ()     return the algorithm of the key
+*****************************************************************/
+time_t dki_algo (const dki_t *dkp)
+{
+       assert (dkp != NULL);
+       return (dkp->algo);
+}
+
 /*****************************************************************
 **     dki_time ()     return the timestamp of the key
 *****************************************************************/
 time_t dki_time (const dki_t *dkp)
 {
-       dki_estr[0] = '\0';
        assert (dkp != NULL);
        return (dkp->time);
 }
@@ -888,7 +887,6 @@ time_t      dki_time (const dki_t *dkp)
 *****************************************************************/
 time_t dki_exptime (const dki_t *dkp)
 {
-       dki_estr[0] = '\0';
        assert (dkp != NULL);
        return (dkp->exptime);
 }
@@ -898,7 +896,6 @@ time_t      dki_exptime (const dki_t *dkp)
 *****************************************************************/
 time_t dki_lifetime (const dki_t *dkp)
 {
-       dki_estr[0] = '\0';
        assert (dkp != NULL);
        return (dkp->lifetime);
 }
@@ -908,7 +905,6 @@ time_t      dki_lifetime (const dki_t *dkp)
 *****************************************************************/
 ushort dki_lifetimedays (const dki_t *dkp)
 {
-       dki_estr[0] = '\0';
        assert (dkp != NULL);
        return (dkp->lifetime / DAYSEC);
 }
@@ -918,7 +914,6 @@ ushort      dki_lifetimedays (const dki_t *dkp)
 *****************************************************************/
 time_t dki_gentime (const dki_t *dkp)
 {
-       dki_estr[0] = '\0';
        assert (dkp != NULL);
        return (dkp->gentime > 0L ? dkp->gentime: dkp->time);
 }
@@ -933,7 +928,6 @@ ushort      dki_setlifetime (dki_t *dkp, int days)
        ulong   lifetsec;
        char    path[MAX_PATHSIZE+1];
 
-       dki_estr[0] = '\0';
        assert (dkp != NULL);
 
        lifetsec = dkp->lifetime;               /* old lifetime */
@@ -959,7 +953,6 @@ time_t      dki_setexptime (dki_t *dkp, time_t sec)
        char    path[MAX_PATHSIZE+1];
        time_t  oldexptime;
 
-       dki_estr[0] = '\0';
        assert (dkp != NULL);
 
        dbg_val1 ("dki_setexptime (%ld)\n", sec);
@@ -980,7 +973,6 @@ time_t      dki_setexptime (dki_t *dkp, time_t sec)
 *****************************************************************/
 int    dki_age (const dki_t *dkp, time_t curr)
 {
-       dki_estr[0] = '\0';
        assert (dkp != NULL);
        return ((ulong)curr - (ulong)dkp->time);
 }
@@ -990,7 +982,6 @@ int dki_age (const dki_t *dkp, time_t curr)
 *****************************************************************/
 dk_flag_t      dki_getflag (const dki_t *dkp, time_t curr)
 {
-       dki_estr[0] = '\0';
        return dkp->flags;
 }
 
@@ -999,7 +990,6 @@ dk_flag_t   dki_getflag (const dki_t *dkp, time_t curr)
 *****************************************************************/
 dk_flag_t      dki_setflag (dki_t *dkp, dk_flag_t flag)
 {
-       dki_estr[0] = '\0';
        return dkp->flags |= (ushort)flag;
 }
 
@@ -1008,7 +998,6 @@ dk_flag_t  dki_setflag (dki_t *dkp, dk_flag_t flag)
 *****************************************************************/
 dk_flag_t      dki_unsetflag (dki_t *dkp, dk_flag_t flag)
 {
-       dki_estr[0] = '\0';
        return dkp->flags &= ~((ushort)flag);
 }
 
@@ -1017,7 +1006,6 @@ dk_flag_t dki_unsetflag (dki_t *dkp, dk_flag_t flag)
 *****************************************************************/
 int    dki_isksk (const dki_t *dkp)
 {
-       dki_estr[0] = '\0';
        assert (dkp != NULL);
        return (dkp->flags & DK_FLAG_KSK) == DK_FLAG_KSK;
 }
@@ -1027,7 +1015,6 @@ int       dki_isksk (const dki_t *dkp)
 *****************************************************************/
 int    dki_isrevoked (const dki_t *dkp)
 {
-       dki_estr[0] = '\0';
        assert (dkp != NULL);
        return (dkp->flags & DK_FLAG_REVOKE) == DK_FLAG_REVOKE;
 }
@@ -1037,7 +1024,6 @@ int       dki_isrevoked (const dki_t *dkp)
 *****************************************************************/
 int    dki_isdepreciated (const dki_t *dkp)
 {
-       dki_estr[0] = '\0';
        return dki_status (dkp) == DKI_DEPRECIATED;
 }
 
@@ -1046,7 +1032,6 @@ int       dki_isdepreciated (const dki_t *dkp)
 *****************************************************************/
 int    dki_isactive (const dki_t *dkp)
 {
-       dki_estr[0] = '\0';
        return dki_status (dkp) == DKI_ACTIVE;
 }
 
@@ -1055,7 +1040,6 @@ int       dki_isactive (const dki_t *dkp)
 *****************************************************************/
 int    dki_ispublished (const dki_t *dkp)
 {
-       dki_estr[0] = '\0';
        return dki_status (dkp) == DKI_PUBLISHED;
 }
 
@@ -1065,7 +1049,6 @@ int       dki_ispublished (const dki_t *dkp)
 *****************************************************************/
 dk_status_t    dki_status (const dki_t *dkp)
 {
-       dki_estr[0] = '\0';
        assert (dkp != NULL);
        return (dkp->status);
 }
@@ -1075,7 +1058,6 @@ dk_status_t       dki_status (const dki_t *dkp)
 *****************************************************************/
 const  char    *dki_statusstr (const dki_t *dkp)
 {
-       dki_estr[0] = '\0';
        assert (dkp != NULL);
        switch ( dkp->status )
        {
@@ -1099,7 +1081,6 @@ dki_t     *dki_add (dki_t **list, dki_t *new)
        dki_t   *curr;
        dki_t   *last;
 
-       dki_estr[0] = '\0';
        if ( list == NULL )
                return NULL;
        if ( new == NULL )
@@ -1129,7 +1110,6 @@ const dki_t       *dki_search (const dki_t *list, int tag, const char *name)
 {
        const dki_t     *curr;
 
-       dki_estr[0] = '\0';
        curr = list;
        if ( tag )
                while ( curr && (tag != curr->tag ||
@@ -1152,7 +1132,6 @@ dki_t     *dki_tadd (dki_t **tree, dki_t *new)
 {
        dki_t   **p;
 
-       dki_estr[0] = '\0';
        p = tsearch (new, tree, dki_namecmp);
        if ( *p == new )
                dbg_val ("dki_tadd: New entry %s added\n", new->name);
@@ -1174,7 +1153,6 @@ const dki_t       *dki_tsearch (const dki_t *tree, int tag, const char *name)
        dki_t   search;
        dki_t   **p;
 
-       dki_estr[0] = '\0';
        search.tag = tag;
        snprintf (search.name, sizeof (search.name), "%s", name);
        p = tfind (&search, &tree, dki_namecmp);
@@ -1193,7 +1171,6 @@ const dki_t       *dki_find (const dki_t *list, int ksk, int status, int no)
        const   dki_t   *dkp;
        const   dki_t   *last;
 
-       dki_estr[0] = '\0';
        last = NULL;
        for ( dkp = list; no > 0 && dkp; dkp = dkp->next )
                if ( dki_isksk (dkp) == ksk && dki_status (dkp) == status )
@@ -1204,3 +1181,24 @@ const dki_t      *dki_find (const dki_t *list, int ksk, int status, int no)
 
        return last;
 }
+
+/*****************************************************************
+**     dki_findalgo () find the n'th ksk or zsk key with given
+**                     algorithm and status
+*****************************************************************/
+const dki_t    *dki_findalgo (const dki_t *list, int ksk, int alg, int status, int no)
+{
+       const   dki_t   *dkp;
+       const   dki_t   *last;
+
+       last = NULL;
+       for ( dkp = list; no > 0 && dkp; dkp = dkp->next )
+               if ( dki_isksk (dkp) == ksk && dki_algo (dkp) == alg &&
+                                               dki_status (dkp) == status )
+               {
+                       no--;
+                       last = dkp;
+               }
+
+       return last;
+}