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