OpenDNSSEC-signer 1.3.0
|
00001 /* 00002 * $Id$ 00003 * 00004 * Copyright (c) 2009-2011 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 "adapter/adutil.h" 00036 #include "shared/file.h" 00037 #include "shared/log.h" 00038 00039 #include <ldns/ldns.h> 00040 00041 static const char* adapter_str = "adapter"; 00042 00043 00048 ldns_rr* 00049 adutil_lookup_soa_rr(FILE* fd) 00050 { 00051 ldns_rr *cur_rr = NULL; 00052 char line[SE_ADFILE_MAXLINE]; 00053 ldns_status status = LDNS_STATUS_OK; 00054 int line_len = 0; 00055 unsigned int l = 0; 00056 00057 while (line_len >= 0) { 00058 line_len = adutil_readline_frm_file(fd, (char*) line, &l); 00059 adutil_rtrim_line(line, &line_len); 00060 00061 if (line_len > 0) { 00062 if (line[0] != ';') { 00063 status = ldns_rr_new_frm_str(&cur_rr, line, 0, NULL, NULL); 00064 if (status == LDNS_STATUS_OK) { 00065 if (ldns_rr_get_type(cur_rr) == LDNS_RR_TYPE_SOA) { 00066 return cur_rr; 00067 } else { 00068 ldns_rr_free(cur_rr); 00069 cur_rr = NULL; 00070 } 00071 } 00072 } 00073 } 00074 } 00075 return NULL; 00076 } 00077 00078 00083 int 00084 adutil_readline_frm_file(FILE* fd, char* line, unsigned int* l) 00085 { 00086 int i = 0; 00087 int li = 0; 00088 int in_string = 0; 00089 int depth = 0; 00090 int comments = 0; 00091 char c = 0; 00092 char lc = 0; 00093 00094 for (i = 0; i < SE_ADFILE_MAXLINE; i++) { 00095 c = (char) ods_fgetc(fd, l); 00096 if (comments) { 00097 while (c != EOF && c != '\n') { 00098 c = (char) ods_fgetc(fd, l); 00099 } 00100 } 00101 00102 if (c == EOF) { 00103 if (depth != 0) { 00104 ods_log_error("[%s] read line: bracket mismatch discovered at " 00105 "line %i, missing ')'", adapter_str, l&&*l?*l:0); 00106 } 00107 if (li > 0) { 00108 line[li] = '\0'; 00109 return li; 00110 } else { 00111 return -1; 00112 } 00113 } else if (c == '"' && lc != '\\') { 00114 in_string = 1 - in_string; /* swap status */ 00115 line[li] = c; 00116 li++; 00117 } else if (c == '(') { 00118 if (in_string) { 00119 line[li] = c; 00120 li++; 00121 } else if (lc != '\\') { 00122 depth++; 00123 line[li] = ' '; 00124 li++; 00125 } else { 00126 line[li] = c; 00127 li++; 00128 } 00129 } else if (c == ')') { 00130 if (in_string) { 00131 line[li] = c; 00132 li++; 00133 } else if (lc != '\\') { 00134 if (depth < 1) { 00135 ods_log_error("[%s] read line: bracket mismatch " 00136 "discovered at line %i, missing '('", adapter_str, 00137 l&&*l?*l:0); 00138 line[li] = '\0'; 00139 return li; 00140 } 00141 depth--; 00142 line[li] = ' '; 00143 li++; 00144 } else { 00145 line[li] = c; 00146 li++; 00147 } 00148 } else if (c == ';') { 00149 if (in_string) { 00150 line[li] = c; 00151 li++; 00152 } else if (lc != '\\') { 00153 comments = 1; 00154 } else { 00155 line[li] = c; 00156 li++; 00157 } 00158 } else if (c == '\n' && lc != '\\') { 00159 comments = 0; 00160 /* if no depth issue, we are done */ 00161 if (depth == 0) { 00162 break; 00163 } 00164 line[li] = ' '; 00165 li++; 00166 } else if (c == '\t' && lc != '\\') { 00167 line[li] = ' '; 00168 li++; 00169 } else { 00170 line[li] = c; 00171 li++; 00172 } 00173 /* continue with line */ 00174 lc = c; 00175 } 00176 00177 /* done */ 00178 if (depth != 0) { 00179 ods_log_error("[%s] read line: bracket mismatch discovered at line %i," 00180 " missing ')'", adapter_str, l&&*l?*l:0); 00181 return li; 00182 } 00183 line[li] = '\0'; 00184 return li; 00185 } 00186 00187 00188 /* 00189 * Trim trailing whitespace. 00190 * 00191 */ 00192 void 00193 adutil_rtrim_line(char* line, int* line_len) 00194 { 00195 int i = strlen(line), nl = 0; 00196 int trimmed = 0; 00197 00198 while (i>0) { 00199 --i; 00200 if (line[i] == '\n') { 00201 nl = 1; 00202 } 00203 if (line[i] == ' ' || line[i] == '\t' || line[i] == '\n') { 00204 line[i] = '\0'; 00205 trimmed++; 00206 } else { 00207 break; 00208 } 00209 } 00210 if (nl) { 00211 line[++i] = '\n'; 00212 } 00213 *line_len -= trimmed; 00214 return; 00215 } 00216 00217 00222 int 00223 adutil_whitespace_line(char* line, int line_len) 00224 { 00225 int i; 00226 for (i = 0; i < line_len; i++) { 00227 if (!isspace((int)line[i])) { 00228 return 0; 00229 } 00230 } 00231 return 1; 00232 }