libspf2 1.2.10
|
00001 /* 00002 * This program is free software; you can redistribute it and/or modify 00003 * it under the terms of either: 00004 * 00005 * a) The GNU Lesser General Public License as published by the Free 00006 * Software Foundation; either version 2.1, or (at your option) any 00007 * later version, 00008 * 00009 * OR 00010 * 00011 * b) The two-clause BSD license. 00012 * 00013 * These licenses can be found with the distribution in the file LICENSES 00014 */ 00015 00016 #include "spf_sys_config.h" 00017 00018 00019 #ifdef STDC_HEADERS 00020 # include <stdio.h> /* stdin / stdout */ 00021 # include <stdlib.h> /* malloc / free */ 00022 # include <ctype.h> /* isupper / tolower */ 00023 #endif 00024 00025 #ifdef HAVE_INTTYPES_H 00026 #include <inttypes.h> 00027 #endif 00028 00029 #ifdef HAVE_STRING_H 00030 # include <string.h> /* strstr / strdup */ 00031 #else 00032 # ifdef HAVE_STRINGS_H 00033 # include <strings.h> /* strstr / strdup */ 00034 # endif 00035 #endif 00036 00037 00038 00039 #include "spf.h" 00040 #include "spf_internal.h" 00041 #include "spf_record.h" 00042 00043 00044 #define SPF_MSGSIZE 4096 00045 00046 00047 SPF_record_t * 00048 SPF_record_new(SPF_server_t *spf_server, const char *text) 00049 { 00050 SPF_record_t *rp; 00051 00052 rp = (SPF_record_t *)malloc(sizeof(SPF_record_t)); 00053 if (!rp) 00054 return rp; 00055 memset(rp, 0, sizeof(SPF_record_t)); 00056 00057 rp->spf_server = spf_server; 00058 00059 return rp; 00060 } 00061 00062 void 00063 SPF_record_free(SPF_record_t *rp) 00064 { 00065 if (rp->mech_first) 00066 free(rp->mech_first); 00067 if (rp->mod_first) 00068 free(rp->mod_first); 00069 free(rp); 00070 } 00071 00072 void 00073 SPF_macro_free(SPF_macro_t *mac) 00074 { 00075 free(mac); 00076 } 00077 00078 /* This expects datap and datalenp NOT to be initialised. */ 00079 static SPF_errcode_t 00080 SPF_record_find_mod_data( 00081 SPF_record_t *spf_record, 00082 const char *mod_name, 00083 SPF_data_t **datap, size_t *datalenp) 00084 { 00085 SPF_mod_t *mod; 00086 size_t name_len; 00087 int i; 00088 00089 name_len = strlen( mod_name ); 00090 00091 /* 00092 * make sure we were passed valid data to work with 00093 */ 00094 SPF_ASSERT_NOTNULL(spf_record); 00095 SPF_ASSERT_NOTNULL(mod_name); 00096 SPF_ASSERT_NOTNULL(datap); 00097 SPF_ASSERT_NOTNULL(datalenp); 00098 00099 /* 00100 * find modifier 00101 */ 00102 00103 mod = spf_record->mod_first; 00104 for( i = 0; i < spf_record->num_mod; i++ ) { 00105 if ( name_len == mod->name_len 00106 && strncasecmp( SPF_mod_name( mod ), mod_name, name_len ) == 0 ) 00107 { 00108 *datap = SPF_mod_data( mod ); 00109 *datalenp = mod->data_len; 00110 00111 return 0; 00112 } 00113 00114 mod = SPF_mod_next( mod ); 00115 } 00116 00117 return SPF_E_MOD_NOT_FOUND; 00118 } 00119 00120 /* Nota Bene: *datap and *datalenp MUST BE INITIALIZED, possibly to 00121 * NULL. SPF_record_expand_data requires this. I do not strictly 00122 * approve, but 00123 * I guess it makes things easier on the allocator? It clouds the 00124 * issue of responsibility for memory. */ 00125 SPF_errcode_t 00126 SPF_record_find_mod_value(SPF_server_t *spf_server, 00127 SPF_request_t *spf_request, 00128 SPF_response_t *spf_response, 00129 SPF_record_t *spf_record, 00130 const char *mod_name, 00131 char **bufp, size_t *buflenp) 00132 { 00133 SPF_data_t *data; 00134 size_t data_len; 00135 SPF_errcode_t err; 00136 00137 /* 00138 * make sure we were passed valid data to work with 00139 */ 00140 SPF_ASSERT_NOTNULL(spf_record); 00141 SPF_ASSERT_NOTNULL(mod_name); 00142 SPF_ASSERT_NOTNULL(bufp); 00143 SPF_ASSERT_NOTNULL(buflenp); 00144 00145 err = SPF_record_find_mod_data(spf_record, 00146 mod_name, &data, &data_len); 00147 if (err) 00148 return err; 00149 00150 return SPF_record_expand_data(spf_server, spf_request, spf_response, 00151 data, data_len, bufp, buflenp); 00152 }