OpenDNSSEC-enforcer  1.3.16
ksm_keyword.c
Go to the documentation of this file.
1 /*
2  * $Id: ksm_keyword.c 3082 2010-03-24 17:15:20Z sion $
3  *
4  * Copyright (c) 2008-2009 Nominet UK. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in the
13  * documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
19  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
21  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
23  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
25  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  *
27  */
28 
29 /*+
30  * ksm_keyword - Keyword/Value Conversions
31  *
32  * Description:
33  * Some values in the database are numeric but need to be translated to
34  * and from strings. This module does that.
35  *
36  * Although the translations are held in tables, this nmodule hard-codes
37  * the strings in the code.
38 -*/
39 
40 #include <assert.h>
41 #include <stdio.h>
42 #include <string.h>
43 #include <unistd.h>
44 
45 #include "ksm/ksm.h"
46 #include "ksm/string_util.h"
47 #include "ksm/string_util2.h"
48 
49 /* Mapping of keywords to values */
50 
51 static STR_KEYWORD_ELEMENT m_algorithm_keywords[] = {
63  {NULL, -1}
64 };
65 
66 static STR_KEYWORD_ELEMENT m_format_keywords[] = {
70  {NULL, -1}
71 };
72 
73 static STR_KEYWORD_ELEMENT m_state_keywords[] = {
84  {NULL, -1}
85 };
86 
87 static STR_KEYWORD_ELEMENT m_type_keywords[] = {
90  {NULL, -1}
91 };
92 
93 /*
94  * Parameters do not have an associated number; instead, the numeric field
95  * is the default value used if the parameter is not set.
96  */
97 
98 static STR_KEYWORD_ELEMENT m_parameter_keywords[] = {
110  {NULL, -1}
111 };
112 
113 static STR_KEYWORD_ELEMENT m_serial_keywords[] = {
118  {NULL, -1}
119 };
120 
121 static STR_KEYWORD_ELEMENT m_roll_keywords[] = {
124 /* {KSM_ROLL_RRSET_STRING, KSM_ROLL_RRSET}, */
125  {NULL, -1}
126 };
127 
128 /*+
129  * KsmKeywordNameToValue - Convert Name to Value
130  * KsmKeywordValueToName - Convert Value to Name
131  *
132  * Description:
133  * Converts between keywords and associated values for the specific
134  * element.
135  *
136  * When searching for a keyword, the given string need only be an
137  * unambiguous abbreviation of one of the keywords in the list. For
138  * example, given the keywords
139  *
140  * taiwan, tanzania, uganda
141  *
142  * ... then "t" or "ta" are ambiguous but "tai" matches taiwan. "u" (a
143  * single letter) will match uganda.
144  *
145  * Arguments:
146  * STR_KEYWORD_ELEMENT* elements
147  * Element list to search.
148  *
149  * const char* name -or- int value
150  * Name or value to convert.
151  *
152  * Returns:
153  * int -or- const char*
154  * Converted value. The return value is NULL or 0 if no conversion is
155  * found. (This implies that no keyword should have a value of 0.)
156  *
157  * Note that the returned string pointer is a pointer to a static
158  * string in this module. It should not be freed by the caller.
159 -*/
160 
161 static int KsmKeywordNameToValue(STR_KEYWORD_ELEMENT* elements, const char* name)
162 {
163  int status = 1; /* Status return - assume error */
164  int value; /* Return value */
165 
166  if (name) {
167  status = StrKeywordSearch(name, elements, &value);
168  }
169  return (status == 0) ? value : 0;
170 }
171 
172 static const char* KsmKeywordValueToName(STR_KEYWORD_ELEMENT* elements, int value)
173 {
174  int i; /* Loop counter */
175  const char* string = NULL; /* Return value */
176 
177  if (elements == NULL) {
178  return NULL;
179  }
180 
181  for (i = 0; elements[i].string; ++i) {
182  if (value == elements[i].value) {
183  string = elements[i].string;
184  break;
185  }
186  }
187 
188  return string;
189 }
190 
191 /*+
192  * KsmKeyword<type>NameToValue - Convert Name to Value
193  * KsmKeyword<type>ValueToName - Convert Value to Name
194  *
195  * Description:
196  * Converts between keywords and associated values for the specific
197  * element.
198  *
199  * Arguments:
200  * const char* name -or- int value
201  * Name of ID to convert.
202  *
203  * Returns:
204  * int -or- const char*
205  * Converted value. The return value is NULL or 0 if no conversion is
206  * found.
207 -*/
208 
209 int KsmKeywordAlgorithmNameToValue(const char* name)
210 {
211  return KsmKeywordNameToValue(m_algorithm_keywords, name);
212 }
213 
214 int KsmKeywordFormatNameToValue(const char* name)
215 {
216  return KsmKeywordNameToValue(m_format_keywords, name);
217 }
218 
219 int KsmKeywordParameterNameToValue(const char* name)
220 {
221  return KsmKeywordNameToValue(m_parameter_keywords, name);
222 }
223 
224 int KsmKeywordStateNameToValue(const char* name)
225 {
226  return KsmKeywordNameToValue(m_state_keywords, name);
227 }
228 
229 int KsmKeywordTypeNameToValue(const char* name)
230 {
231  return KsmKeywordNameToValue(m_type_keywords, name);
232 }
233 
234 const char* KsmKeywordAlgorithmValueToName(int value)
235 {
236  return KsmKeywordValueToName(m_algorithm_keywords, value);
237 }
238 
239 const char* KsmKeywordFormatValueToName(int value)
240 {
241  return KsmKeywordValueToName(m_format_keywords, value);
242 }
243 
244 const char* KsmKeywordStateValueToName(int value)
245 {
246  return KsmKeywordValueToName(m_state_keywords, value);
247 }
248 
249 const char* KsmKeywordTypeValueToName(int value)
250 {
251  return KsmKeywordValueToName(m_type_keywords, value);
252 }
253 
254 const char* KsmKeywordSerialValueToName(int value)
255 {
256  return KsmKeywordValueToName(m_serial_keywords, value);
257 }
258 
259 int KsmKeywordRollNameToValue(const char* name)
260 {
261  return KsmKeywordNameToValue(m_roll_keywords, name);
262 }
263 
264 const char* KsmKeywordRollValueToName(int value)
265 {
266  return KsmKeywordValueToName(m_roll_keywords, value);
267 }
268 
269 /*+
270  * KsmKeywordParameterExists - Check if Keyword Exists
271  *
272  * Description:
273  * Checks if the keyword is the name of a parameter, returning true (1) if
274  * it is and false (0) if it isn't.
275  *
276  * Unlike the other keyword checks, the match must be exact.
277  *
278  * Arguments:
279  * const char* name
280  * Name of the keyword to check.
281  *
282  * Returns:
283  * int
284  * 1 Keyword exists
285  * 0 Keyword does not exist
286 -*/
287 
288 int KsmKeywordParameterExists(const char* name)
289 {
290  int exists = 0;
291  int i;
292 
293  if (name) {
294  for (i = 0; m_parameter_keywords[i].string; ++i) {
295  if (strcmp(name, m_parameter_keywords[i].string) == 0) {
296  exists = 1;
297  break;
298  }
299  }
300  }
301 
302  return exists;
303 }
#define KSM_ROLL_DS_STRING
Definition: ksm.h:402
#define KSM_STATE_GENERATE_STRING
Definition: ksm.h:367
#define KSM_SERIAL_UNIX_STRING
Definition: ksm.h:387
#define KSM_ALGORITHM_DSA_NSEC3_SHA1_STRING
Definition: ksm.h:340
#define KSM_TYPE_ZSK
Definition: ksm.h:363
#define KSM_PAR_CLOCKSKEW_STRING
Definition: ksm.h:413
#define KSM_STATE_DEAD
Definition: ksm.h:376
#define KSM_ALGORITHM_PRIVOID_STRING
Definition: ksm.h:352
#define KSM_ALGORITHM_PRIVDOM_STRING
Definition: ksm.h:350
int KsmKeywordParameterExists(const char *name)
Definition: ksm_keyword.c:288
#define KSM_PAR_SIGNINT
Definition: ksm.h:427
#define KSM_STATE_RETIRE_STRING
Definition: ksm.h:375
#define KSM_STATE_ACTIVE
Definition: ksm.h:372
#define KSM_STATE_KEYPUBLISH_STRING
Definition: ksm.h:385
#define KSM_PAR_CLOCKSKEW
Definition: ksm.h:412
#define KSM_FORMAT_HSM_STRING
Definition: ksm.h:357
#define KSM_PAR_SOATTL_STRING
Definition: ksm.h:434
int KsmKeywordTypeNameToValue(const char *name)
Definition: ksm_keyword.c:229
#define KSM_ALGORITHM_DSASHA1_STRING
Definition: ksm.h:336
#define KSM_STATE_DSSUB_STRING
Definition: ksm.h:379
#define KSM_SERIAL_DATE_STRING
Definition: ksm.h:391
#define KSM_STATE_READY
Definition: ksm.h:370
#define KSM_TYPE_KSK_STRING
Definition: ksm.h:362
#define KSM_ROLL_DS
Definition: ksm.h:403
#define KSM_ALGORITHM_RSASHA1_STRING
Definition: ksm.h:338
#define KSM_SERIAL_COUNTER_STRING
Definition: ksm.h:389
#define KSM_ALGORITHM_RSAMD5
Definition: ksm.h:331
#define KSM_ALGORITHM_DH
Definition: ksm.h:333
#define KSM_PAR_PROPDELAY_STRING
Definition: ksm.h:419
#define KSM_PAR_STANDBYZSKS_STRING
Definition: ksm.h:425
#define KSM_ALGORITHM_INDIRECT
Definition: ksm.h:347
#define KSM_FORMAT_FILE_STRING
Definition: ksm.h:355
#define KSM_ALGORITHM_DSA_NSEC3_SHA1
Definition: ksm.h:339
#define KSM_STATE_READY_STRING
Definition: ksm.h:371
int KsmKeywordRollNameToValue(const char *name)
Definition: ksm_keyword.c:259
#define KSM_PAR_STANDBYZSKS
Definition: ksm.h:424
#define KSM_PAR_KSKLIFE_STRING
Definition: ksm.h:416
#define KSM_ALGORITHM_RSASHA256_STRING
Definition: ksm.h:344
#define KSM_PAR_KSKLIFE
Definition: ksm.h:415
const char * KsmKeywordRollValueToName(int value)
Definition: ksm_keyword.c:264
#define KSM_ROLL_DNSKEY_STRING
Definition: ksm.h:400
#define KSM_ALGORITHM_DSASHA1
Definition: ksm.h:335
#define KSM_ROLL_DNSKEY
Definition: ksm.h:401
#define KSM_STATE_KEYPUBLISH
Definition: ksm.h:384
const char * KsmKeywordStateValueToName(int value)
Definition: ksm_keyword.c:244
#define KSM_PAR_ZSKLIFE_STRING
Definition: ksm.h:440
#define KSM_STATE_DEAD_STRING
Definition: ksm.h:377
#define KSM_PAR_ZSKSIGLIFE
Definition: ksm.h:436
#define KSM_PAR_ZSKTTL_STRING
Definition: ksm.h:443
#define KSM_STATE_DSREADY_STRING
Definition: ksm.h:383
#define KSM_PAR_ZSKLIFE
Definition: ksm.h:439
#define KSM_STATE_DSPUBLISH
Definition: ksm.h:380
#define KSM_PAR_SOAMIN
Definition: ksm.h:430
#define KSM_SERIAL_COUNTER
Definition: ksm.h:390
#define KSM_ALGORITHM_RSASHA1
Definition: ksm.h:337
int KsmKeywordStateNameToValue(const char *name)
Definition: ksm_keyword.c:224
#define KSM_FORMAT_URI
Definition: ksm.h:358
#define KSM_PAR_SIGNINT_STRING
Definition: ksm.h:428
#define KSM_STATE_ACTIVE_STRING
Definition: ksm.h:373
#define KSM_PAR_PROPDELAY
Definition: ksm.h:418
#define KSM_ALGORITHM_PRIVDOM
Definition: ksm.h:349
const char * string
Definition: string_util2.h:49
#define KSM_SERIAL_UNIX
Definition: ksm.h:388
const char * KsmKeywordSerialValueToName(int value)
Definition: ksm_keyword.c:254
#define KSM_PAR_SOATTL
Definition: ksm.h:433
#define KSM_ALGORITHM_RSASHA512
Definition: ksm.h:345
#define KSM_ALGORITHM_RSASHA1_NSEC3_SHA1_STRING
Definition: ksm.h:342
#define KSM_STATE_PUBLISH_STRING
Definition: ksm.h:369
#define KSM_TYPE_ZSK_STRING
Definition: ksm.h:364
#define KSM_SERIAL_DATE
Definition: ksm.h:392
#define KSM_STATE_RETIRE
Definition: ksm.h:374
#define KSM_STATE_PUBLISH
Definition: ksm.h:368
int StrKeywordSearch(const char *search, STR_KEYWORD_ELEMENT *keywords, int *value)
Definition: string_util2.c:321
#define KSM_FORMAT_HSM
Definition: ksm.h:356
#define KSM_ALGORITHM_PRIVOID
Definition: ksm.h:351
#define KSM_PAR_ZSKSIGLIFE_STRING
Definition: ksm.h:437
#define KSM_PAR_SOAMIN_STRING
Definition: ksm.h:431
#define KSM_FORMAT_FILE
Definition: ksm.h:354
#define KSM_ALGORITHM_INDIRECT_STRING
Definition: ksm.h:348
const char * KsmKeywordTypeValueToName(int value)
Definition: ksm_keyword.c:249
#define KSM_FORMAT_URI_STRING
Definition: ksm.h:359
#define KSM_ALGORITHM_RSAMD5_STRING
Definition: ksm.h:332
int KsmKeywordParameterNameToValue(const char *name)
Definition: ksm_keyword.c:219
#define KSM_ALGORITHM_DH_STRING
Definition: ksm.h:334
const char * KsmKeywordFormatValueToName(int value)
Definition: ksm_keyword.c:239
#define KSM_STATE_DSSUB
Definition: ksm.h:378
int KsmKeywordFormatNameToValue(const char *name)
Definition: ksm_keyword.c:214
#define KSM_TYPE_KSK
Definition: ksm.h:361
int KsmKeywordAlgorithmNameToValue(const char *name)
Definition: ksm_keyword.c:209
#define KSM_PAR_STANDBYKSKS
Definition: ksm.h:421
#define KSM_STATE_DSREADY
Definition: ksm.h:382
#define KSM_STATE_DSPUBLISH_STRING
Definition: ksm.h:381
#define KSM_PAR_STANDBYKSKS_STRING
Definition: ksm.h:422
#define KSM_SERIAL_KEEP
Definition: ksm.h:394
#define KSM_STATE_GENERATE
Definition: ksm.h:366
#define KSM_SERIAL_KEEP_STRING
Definition: ksm.h:393
#define KSM_ALGORITHM_RSASHA256
Definition: ksm.h:343
#define KSM_PAR_ZSKTTL
Definition: ksm.h:442
#define KSM_ALGORITHM_RSASHA1_NSEC3_SHA1
Definition: ksm.h:341
const char * KsmKeywordAlgorithmValueToName(int value)
Definition: ksm_keyword.c:234
#define KSM_ALGORITHM_RSASHA512_STRING
Definition: ksm.h:346