OpenDNSSEC-signer 1.2.1
|
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 "signer/backup.h" 00036 #include "util/duration.h" 00037 #include "util/file.h" 00038 #include "util/log.h" 00039 #include "util/se_malloc.h" 00040 00041 #include <ldns/ldns.h> 00042 00047 char* 00048 backup_read_token(FILE* in) 00049 { 00050 static char buf[4000]; 00051 buf[sizeof(buf)-1]=0; 00052 while (1) { 00053 if (fscanf(in, "%3990s", buf) != 1) { 00054 return 0; 00055 } 00056 if (buf[0] != '#') { 00057 return buf; 00058 } 00059 if (!fgets(buf, sizeof(buf), in)) { 00060 return 0; 00061 } 00062 } 00063 return 0; 00064 } 00065 00070 int 00071 backup_read_check_str(FILE* in, const char* str) 00072 { 00073 char *p = backup_read_token(in); 00074 if (!p) { 00075 se_log_debug("backup: cannot read check string \'%s\'\n", str); 00076 return 0; 00077 } 00078 if (se_strcmp(p, str) != 0) { 00079 se_log_debug("backup: \'%s\' does not match \'%s\'\n", p, str); 00080 return 0; 00081 } 00082 return 1; 00083 } 00084 00085 00090 int 00091 backup_read_str(FILE* in, const char** str) 00092 { 00093 char *p = backup_read_token(in); 00094 if (!p) { 00095 se_log_debug("backup: cannot read string\n"); 00096 return 0; 00097 } 00098 *str = se_strdup(p); 00099 return 1; 00100 } 00101 00102 00107 int 00108 backup_read_time_t(FILE* in, time_t* v) 00109 { 00110 char* p = backup_read_token(in); 00111 if (!p) { 00112 se_log_debug("backup: cannot read time\n"); 00113 return 0; 00114 } 00115 *v=atol(p); 00116 return 1; 00117 } 00118 00119 00124 int 00125 backup_read_duration(FILE* in, duration_type** v) 00126 { 00127 char* p = backup_read_token(in); 00128 if (!p) { 00129 se_log_debug("backup: cannot read duration\n"); 00130 return 0; 00131 } 00132 *v=duration_create_from_string((const char*) p); 00133 return 1; 00134 } 00135 00136 00141 int 00142 backup_read_rr_type(FILE* in, ldns_rr_type* v) 00143 { 00144 char* p = backup_read_token(in); 00145 if (!p) { 00146 se_log_debug("backup: cannot read rr type\n"); 00147 return 0; 00148 } 00149 *v=(ldns_rr_type) atoi(p); 00150 return 1; 00151 } 00152 00153 00158 int 00159 backup_read_int(FILE* in, int* v) 00160 { 00161 char* p = backup_read_token(in); 00162 if (!p) { 00163 se_log_debug("backup: cannot read integer\n"); 00164 return 0; 00165 } 00166 *v=atoi(p); 00167 return 1; 00168 } 00169 00170 00175 int 00176 backup_read_size_t(FILE* in, size_t* v) 00177 { 00178 char* p = backup_read_token(in); 00179 if (!p) { 00180 se_log_debug("backup: cannot read size_t\n"); 00181 return 0; 00182 } 00183 *v=(size_t)atoi(p); 00184 return 1; 00185 } 00186 00187 00192 int 00193 backup_read_uint8_t(FILE* in, uint8_t* v) 00194 { 00195 char* p = backup_read_token(in); 00196 if (!p) { 00197 se_log_debug("backup: cannot read uint8_t\n"); 00198 return 0; 00199 } 00200 *v= (uint8_t)atoi(p); 00201 return 1; 00202 } 00203 00204 00209 int 00210 backup_read_uint16_t(FILE* in, uint16_t* v) 00211 { 00212 char* p = backup_read_token(in); 00213 if (!p) { 00214 se_log_debug("backup: cannot read uint16_t\n"); 00215 return 0; 00216 } 00217 *v= (uint16_t)atoi(p); 00218 return 1; 00219 } 00220 00221 00226 int 00227 backup_read_uint32_t(FILE* in, uint32_t* v) 00228 { 00229 char* p = backup_read_token(in); 00230 if (!p) { 00231 se_log_debug("backup: cannot read uint32_t\n"); 00232 return 0; 00233 } 00234 *v= (uint32_t)atol(p); 00235 return 1; 00236 }