OpenDNSSEC-signer 1.3.0
|
00001 /* 00002 * $Id: tools.c 3817 2010-08-27 08:43:00Z matthijs $ 00003 * 00004 * Copyright (c) 2006-2010 NLNet Labs. 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 00034 #include "config.h" 00035 #include "shared/duration.h" 00036 #include "shared/file.h" 00037 #include "shared/log.h" 00038 #include "signer/backup.h" 00039 00040 #include <ldns/ldns.h> 00041 00042 static const char* backup_str = "backup"; 00043 00044 00049 char* 00050 backup_read_token(FILE* in) 00051 { 00052 static char buf[4000]; 00053 buf[sizeof(buf)-1]=0; 00054 00055 while (1) { 00056 if (fscanf(in, "%3990s", buf) != 1) { 00057 return 0; 00058 } 00059 if (buf[0] != '#') { 00060 return buf; 00061 } 00062 if (!fgets(buf, sizeof(buf), in)) { 00063 return 0; 00064 } 00065 } 00066 return 0; 00067 } 00068 00073 int 00074 backup_read_check_str(FILE* in, const char* str) 00075 { 00076 char *p = backup_read_token(in); 00077 if (!p) { 00078 ods_log_debug("[%s] cannot read check string \'%s\'", backup_str, str); 00079 return 0; 00080 } 00081 if (ods_strcmp(p, str) != 0) { 00082 ods_log_debug("[%s] \'%s\' does not match \'%s\'", backup_str, p, str); 00083 return 0; 00084 } 00085 return 1; 00086 } 00087 00088 00093 int 00094 backup_read_str(FILE* in, const char** str) 00095 { 00096 char *p = backup_read_token(in); 00097 if (!p) { 00098 ods_log_debug("[%s] cannot read string", backup_str); 00099 return 0; 00100 } 00101 *str = strdup(p); 00102 return 1; 00103 } 00104 00105 00110 int 00111 backup_read_time_t(FILE* in, time_t* v) 00112 { 00113 char* p = backup_read_token(in); 00114 if (!p) { 00115 ods_log_debug("[%s] cannot read time", backup_str); 00116 return 0; 00117 } 00118 *v=atol(p); 00119 return 1; 00120 } 00121 00122 00127 int 00128 backup_read_duration(FILE* in, duration_type** v) 00129 { 00130 char* p = backup_read_token(in); 00131 if (!p) { 00132 ods_log_debug("[%s] cannot read duration", backup_str); 00133 return 0; 00134 } 00135 *v=duration_create_from_string((const char*) p); 00136 return 1; 00137 } 00138 00139 00144 int 00145 backup_read_rr_type(FILE* in, ldns_rr_type* v) 00146 { 00147 char* p = backup_read_token(in); 00148 if (!p) { 00149 ods_log_debug("[%s] cannot read rr type", backup_str); 00150 return 0; 00151 } 00152 *v=(ldns_rr_type) atoi(p); 00153 return 1; 00154 } 00155 00156 00161 int 00162 backup_read_int(FILE* in, int* v) 00163 { 00164 char* p = backup_read_token(in); 00165 if (!p) { 00166 ods_log_debug("[%s] cannot read integer", backup_str); 00167 return 0; 00168 } 00169 *v=atoi(p); 00170 return 1; 00171 } 00172 00173 00178 int 00179 backup_read_size_t(FILE* in, size_t* v) 00180 { 00181 char* p = backup_read_token(in); 00182 if (!p) { 00183 ods_log_debug("[%s] cannot read size_t", backup_str); 00184 return 0; 00185 } 00186 *v=(size_t)atoi(p); 00187 return 1; 00188 } 00189 00190 00195 int 00196 backup_read_uint8_t(FILE* in, uint8_t* v) 00197 { 00198 char* p = backup_read_token(in); 00199 if (!p) { 00200 ods_log_debug("[%s] cannot read uint8_t", backup_str); 00201 return 0; 00202 } 00203 *v= (uint8_t)atoi(p); 00204 return 1; 00205 } 00206 00207 00212 int 00213 backup_read_uint16_t(FILE* in, uint16_t* v) 00214 { 00215 char* p = backup_read_token(in); 00216 if (!p) { 00217 ods_log_debug("[%s] cannot read uint16_t", backup_str); 00218 return 0; 00219 } 00220 *v= (uint16_t)atoi(p); 00221 return 1; 00222 } 00223 00224 00229 int 00230 backup_read_uint32_t(FILE* in, uint32_t* v) 00231 { 00232 char* p = backup_read_token(in); 00233 if (!p) { 00234 ods_log_debug("[%s] cannot read uint32_t", backup_str); 00235 return 0; 00236 } 00237 *v= (uint32_t)atol(p); 00238 return 1; 00239 }