OpenDNSSEC-enforcer  1.3.4
/build/buildd/opendnssec-1.3.4/enforcer/ksm/ksm_keyword.c
Go to the documentation of this file.
00001 /*
00002  * $Id: ksm_keyword.c 3082 2010-03-24 17:15:20Z sion $
00003  *
00004  * Copyright (c) 2008-2009 Nominet UK. All rights reserved.
00005  *
00006  * Redistribution and use in source and binary forms, with or without
00007  * modification, are permitted provided that the following conditions
00008  * are met:
00009  * 1. Redistributions of source code must retain the above copyright
00010  *    notice, this list of conditions and the following disclaimer.
00011  * 2. Redistributions in binary form must reproduce the above copyright
00012  *    notice, this list of conditions and the following disclaimer in the
00013  *    documentation and/or other materials provided with the distribution.
00014  *
00015  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
00016  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00017  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00018  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
00019  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00020  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
00021  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00022  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
00023  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
00024  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
00025  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00026  *
00027  */
00028 
00029 /*+
00030  * ksm_keyword - Keyword/Value Conversions
00031  *
00032  * Description:
00033  *      Some values in the database are numeric but need to be translated to
00034  *      and from strings.  This module does that.
00035  *
00036  *      Although the translations are held in tables, this nmodule hard-codes
00037  *      the strings in the code.
00038 -*/
00039 
00040 #include <assert.h>
00041 #include <stdio.h>
00042 #include <string.h>
00043 #include <unistd.h>
00044 
00045 #include "ksm/ksm.h"
00046 #include "ksm/string_util.h"
00047 #include "ksm/string_util2.h"
00048 
00049 /* Mapping of keywords to values */
00050 
00051 static STR_KEYWORD_ELEMENT m_algorithm_keywords[] = {
00052     {KSM_ALGORITHM_RSAMD5_STRING,              KSM_ALGORITHM_RSAMD5},
00053     {KSM_ALGORITHM_DH_STRING,                  KSM_ALGORITHM_DH},
00054     {KSM_ALGORITHM_DSASHA1_STRING,             KSM_ALGORITHM_DSASHA1},
00055     {KSM_ALGORITHM_RSASHA1_STRING,             KSM_ALGORITHM_RSASHA1},
00056     {KSM_ALGORITHM_DSA_NSEC3_SHA1_STRING,      KSM_ALGORITHM_DSA_NSEC3_SHA1},
00057     {KSM_ALGORITHM_RSASHA1_NSEC3_SHA1_STRING,  KSM_ALGORITHM_RSASHA1_NSEC3_SHA1},
00058     {KSM_ALGORITHM_RSASHA256_STRING,           KSM_ALGORITHM_RSASHA256},
00059     {KSM_ALGORITHM_RSASHA512_STRING,           KSM_ALGORITHM_RSASHA512},
00060     {KSM_ALGORITHM_INDIRECT_STRING,            KSM_ALGORITHM_INDIRECT},
00061     {KSM_ALGORITHM_PRIVDOM_STRING,             KSM_ALGORITHM_PRIVDOM},
00062     {KSM_ALGORITHM_PRIVOID_STRING,             KSM_ALGORITHM_PRIVOID},
00063     {NULL,                                     -1}
00064 };
00065 
00066 static STR_KEYWORD_ELEMENT m_format_keywords[] = {
00067     {KSM_FORMAT_FILE_STRING,        KSM_FORMAT_FILE},
00068     {KSM_FORMAT_HSM_STRING,         KSM_FORMAT_HSM},
00069     {KSM_FORMAT_URI_STRING,         KSM_FORMAT_URI},
00070     {NULL,                          -1}
00071 };
00072 
00073 static STR_KEYWORD_ELEMENT m_state_keywords[] = {
00074     {KSM_STATE_GENERATE_STRING,     KSM_STATE_GENERATE},
00075     {KSM_STATE_PUBLISH_STRING,      KSM_STATE_PUBLISH},
00076     {KSM_STATE_READY_STRING,        KSM_STATE_READY},
00077     {KSM_STATE_ACTIVE_STRING,       KSM_STATE_ACTIVE},
00078     {KSM_STATE_RETIRE_STRING,       KSM_STATE_RETIRE},
00079     {KSM_STATE_DEAD_STRING,         KSM_STATE_DEAD},
00080     {KSM_STATE_DSSUB_STRING,        KSM_STATE_DSSUB},
00081     {KSM_STATE_DSPUBLISH_STRING,    KSM_STATE_DSPUBLISH},
00082     {KSM_STATE_DSREADY_STRING,      KSM_STATE_DSREADY},
00083     {KSM_STATE_KEYPUBLISH_STRING,   KSM_STATE_KEYPUBLISH},
00084     {NULL,                          -1}
00085 };
00086 
00087 static STR_KEYWORD_ELEMENT m_type_keywords[] = {
00088     {KSM_TYPE_KSK_STRING,           KSM_TYPE_KSK},
00089     {KSM_TYPE_ZSK_STRING,           KSM_TYPE_ZSK},
00090     {NULL,                          -1}
00091 };
00092 
00093 /*
00094  * Parameters do not have an associated number; instead, the numeric field
00095  * is the default value used if the parameter is not set.
00096  */
00097 
00098 static STR_KEYWORD_ELEMENT m_parameter_keywords[] = {
00099     {KSM_PAR_CLOCKSKEW_STRING,  KSM_PAR_CLOCKSKEW},
00100     {KSM_PAR_STANDBYKSKS_STRING,  KSM_PAR_STANDBYKSKS},
00101     {KSM_PAR_STANDBYZSKS_STRING,  KSM_PAR_STANDBYZSKS},
00102     {KSM_PAR_KSKLIFE_STRING,    KSM_PAR_KSKLIFE},
00103     {KSM_PAR_PROPDELAY_STRING,  KSM_PAR_PROPDELAY},
00104     {KSM_PAR_SIGNINT_STRING,    KSM_PAR_SIGNINT},
00105     {KSM_PAR_SOAMIN_STRING,     KSM_PAR_SOAMIN},
00106     {KSM_PAR_SOATTL_STRING,     KSM_PAR_SOATTL},
00107     {KSM_PAR_ZSKSIGLIFE_STRING, KSM_PAR_ZSKSIGLIFE},
00108     {KSM_PAR_ZSKLIFE_STRING,    KSM_PAR_ZSKLIFE},
00109     {KSM_PAR_ZSKTTL_STRING,     KSM_PAR_ZSKTTL},
00110     {NULL,                      -1}
00111 };
00112 
00113 static STR_KEYWORD_ELEMENT m_serial_keywords[] = {
00114     {KSM_SERIAL_UNIX_STRING,        KSM_SERIAL_UNIX},
00115     {KSM_SERIAL_COUNTER_STRING,     KSM_SERIAL_COUNTER},
00116     {KSM_SERIAL_DATE_STRING,        KSM_SERIAL_DATE},
00117     {KSM_SERIAL_KEEP_STRING,        KSM_SERIAL_KEEP},
00118     {NULL,                          -1}
00119 };
00120 
00121 static STR_KEYWORD_ELEMENT m_roll_keywords[] = {
00122     {KSM_ROLL_DNSKEY_STRING,    KSM_ROLL_DNSKEY},
00123     {KSM_ROLL_DS_STRING,        KSM_ROLL_DS},
00124 /*    {KSM_ROLL_RRSET_STRING,     KSM_ROLL_RRSET}, */
00125     {NULL,                          -1}
00126 };
00127 
00128 /*+
00129  * KsmKeywordNameToValue - Convert Name to Value
00130  * KsmKeywordValueToName - Convert Value to Name
00131  *
00132  * Description:
00133  *      Converts between keywords and associated values for the specific
00134  *      element.
00135  *
00136  *      When searching for a keyword, the given string need only be an
00137  *      unambiguous abbreviation of one of the keywords in the list.  For
00138  *      example, given the keywords
00139  *
00140  *              taiwan, tanzania, uganda
00141  *
00142  *      ... then "t" or "ta" are ambiguous but "tai" matches taiwan.  "u" (a
00143  *      single letter) will match uganda.
00144  *
00145  * Arguments:
00146  *      STR_KEYWORD_ELEMENT* elements
00147  *          Element list to search.
00148  *
00149  *      const char* name -or- int value
00150  *          Name or value to convert.
00151  *
00152  * Returns:
00153  *      int -or- const char*
00154  *          Converted value.  The return value is NULL or 0 if no conversion is
00155  *          found. (This implies that no keyword should have a value of 0.)
00156  *
00157  *          Note that the returned string pointer is a pointer to a static
00158  *          string in this module.  It should not be freed by the caller.
00159 -*/
00160 
00161 static int KsmKeywordNameToValue(STR_KEYWORD_ELEMENT* elements, const char* name)
00162 {
00163     int     status = 1;     /* Status return - assume error */
00164     int     value;          /* Return value */
00165 
00166     if (name) {
00167         status = StrKeywordSearch(name, elements, &value);
00168     }
00169     return (status == 0) ? value : 0;
00170 }
00171 
00172 static const char* KsmKeywordValueToName(STR_KEYWORD_ELEMENT* elements, int value)
00173 {
00174     int     i;                  /* Loop counter */
00175     const char* string = NULL;  /* Return value */
00176 
00177     if (elements == NULL) {
00178         return NULL;
00179     }
00180 
00181     for (i = 0; elements[i].string; ++i) {
00182         if (value == elements[i].value) {
00183             string = elements[i].string;
00184             break;
00185         }
00186     }
00187 
00188     return string;
00189 }
00190 
00191 /*+
00192  * KsmKeyword<type>NameToValue - Convert Name to Value
00193  * KsmKeyword<type>ValueToName - Convert Value to Name
00194  *
00195  * Description:
00196  *      Converts between keywords and associated values for the specific
00197  *      element.
00198  *
00199  * Arguments:
00200  *      const char* name -or- int value
00201  *          Name of ID to convert.
00202  *
00203  * Returns:
00204  *      int -or- const char*
00205  *          Converted value.  The return value is NULL or 0 if no conversion is
00206  *          found.
00207 -*/
00208 
00209 int KsmKeywordAlgorithmNameToValue(const char* name)
00210 {
00211     return KsmKeywordNameToValue(m_algorithm_keywords, name);
00212 }
00213 
00214 int KsmKeywordFormatNameToValue(const char* name)
00215 {
00216     return KsmKeywordNameToValue(m_format_keywords, name);
00217 }
00218 
00219 int KsmKeywordParameterNameToValue(const char* name)
00220 {
00221     return KsmKeywordNameToValue(m_parameter_keywords, name);
00222 }
00223 
00224 int KsmKeywordStateNameToValue(const char* name)
00225 {
00226     return KsmKeywordNameToValue(m_state_keywords, name);
00227 }
00228 
00229 int KsmKeywordTypeNameToValue(const char* name)
00230 {
00231     return KsmKeywordNameToValue(m_type_keywords, name);
00232 }
00233 
00234 const char* KsmKeywordAlgorithmValueToName(int value)
00235 {
00236     return KsmKeywordValueToName(m_algorithm_keywords, value);
00237 }
00238 
00239 const char* KsmKeywordFormatValueToName(int value)
00240 {
00241     return KsmKeywordValueToName(m_format_keywords, value);
00242 }
00243 
00244 const char* KsmKeywordStateValueToName(int value)
00245 {
00246     return KsmKeywordValueToName(m_state_keywords, value);
00247 }
00248 
00249 const char* KsmKeywordTypeValueToName(int value)
00250 {
00251     return KsmKeywordValueToName(m_type_keywords, value);
00252 }
00253 
00254 const char* KsmKeywordSerialValueToName(int value)
00255 {
00256     return KsmKeywordValueToName(m_serial_keywords, value);
00257 }
00258 
00259 int KsmKeywordRollNameToValue(const char* name)
00260 {
00261     return KsmKeywordNameToValue(m_roll_keywords, name);
00262 }
00263 
00264 const char* KsmKeywordRollValueToName(int value)
00265 {
00266     return KsmKeywordValueToName(m_roll_keywords, value);
00267 }
00268 
00269 /*+
00270  * KsmKeywordParameterExists - Check if Keyword Exists
00271  *
00272  * Description:
00273  *      Checks if the keyword is the name of a parameter, returning true (1) if
00274  *      it is and false (0) if it isn't.
00275  *
00276  *      Unlike the other keyword checks, the match must be exact.
00277  *
00278  * Arguments:
00279  *      const char* name
00280  *          Name of the keyword to check.
00281  *
00282  * Returns:
00283  *      int
00284  *          1   Keyword exists
00285  *          0   Keyword does not exist
00286 -*/
00287 
00288 int KsmKeywordParameterExists(const char* name)
00289 {
00290     int     exists = 0;
00291     int     i;
00292 
00293     if (name) {
00294         for (i = 0; m_parameter_keywords[i].string; ++i) {
00295             if (strcmp(name, m_parameter_keywords[i].string) == 0) {
00296                 exists = 1;
00297                 break;
00298             }
00299         }
00300     }
00301 
00302     return exists;
00303 }