libspf2  1.2.11
spf_utils.c
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or modify
3  * it under the terms of either:
4  *
5  * a) The GNU Lesser General Public License as published by the Free
6  * Software Foundation; either version 2.1, or (at your option) any
7  * later version,
8  *
9  * OR
10  *
11  * b) The two-clause BSD license.
12  *
13  * These licenses can be found with the distribution in the file LICENSES
14  */
15 
16 
17 #include "spf_sys_config.h"
18 
19 #ifdef STDC_HEADERS
20 # include <stdlib.h> /* malloc / free */
21 # include <ctype.h> /* isupper / tolower */
22 #endif
23 
24 #ifdef HAVE_MEMORY_H
25 #include <memory.h>
26 #endif
27 
28 #ifdef HAVE_STRING_H
29 # include <string.h> /* memset */
30 #endif
31 
32 
33 #include "spf.h"
34 #include "spf_internal.h"
35 
36 
40 void
41 SPF_get_lib_version(int *major, int *minor, int *patch)
42 {
43  *major = SPF_LIB_VERSION_MAJOR;
44  *minor = SPF_LIB_VERSION_MINOR;
45  *patch = SPF_LIB_VERSION_PATCH;
46 }
47 
48 
49 
56 char *
57 SPF_sanitize(SPF_server_t *spf_server, char *str)
58 {
59  char *p;
60 
61  SPF_ASSERT_NOTNULL(spf_server);
62 
63  if (! spf_server->sanitize)
64  return str;
65 
66  if (str == NULL)
67  return str;
68 
69  for (p = str; *p != '\0'; p++)
70  if (! isprint( (unsigned char)*p ))
71  *p = '?';
72 
73  return str;
74 }
75 
76 
77 
78 
79 
83 const char *
84 SPF_strresult(SPF_result_t result)
85 {
86  switch (result) {
87  case SPF_RESULT_INVALID:
88  return "(invalid)";
89  break;
90 
91  case SPF_RESULT_PASS: /* + */
92  return "pass";
93  break;
94 
95  case SPF_RESULT_FAIL: /* - */
96  return "fail";
97  break;
98 
99  case SPF_RESULT_SOFTFAIL: /* ~ */
100  return "softfail";
101  break;
102 
103  case SPF_RESULT_NEUTRAL: /* ? */
104  return "neutral";
105  break;
106 
107  case SPF_RESULT_PERMERROR: /* permanent error */
108  return "permerror";
109  break;
110 
111  case SPF_RESULT_TEMPERROR: /* temporary error */
112  return "temperror";
113  break;
114 
115  case SPF_RESULT_NONE: /* no SPF record found */
116  return "none";
117  break;
118 
119  default:
120  return "(error: unknown result)";
121  break;
122  }
123 }
124 
125 
126 
130 const char *
131 SPF_strreason(SPF_reason_t reason)
132 {
133  switch (reason) {
134  case SPF_REASON_NONE:
135  return "none";
136  break;
137 
139  return "localhost";
140  break;
141 
143  return "local policy";
144  break;
145 
146  case SPF_REASON_MECH:
147  return "mechanism";
148  break;
149 
150  case SPF_REASON_DEFAULT:
151  return "default";
152  break;
153 
154  case SPF_REASON_2MX:
155  return "secondary MX";
156  break;
157 
158  default:
159  return "(invalid reason)";
160  break;
161 
162  }
163 }
164 
165 const char *
167 {
168  switch (rr_type) {
169  case ns_t_a: return "A";
170  case ns_t_aaaa: return "AAAA";
171  case ns_t_any: return "ANY";
172  case ns_t_invalid: return "BAD";
173  case ns_t_mx: return "MX";
174  case ns_t_ptr: return "PTR";
175  case ns_t_spf: return "SPF";
176  case ns_t_txt: return "TXT";
177  default: return "??";
178  }
179 }
180 
191 SPF_recalloc(char **bufp, size_t *buflenp, size_t buflen)
192 {
193  char *buf;
194 
195  if (*buflenp < buflen) {
196  if (buflen < 64)
197  buflen = 64;
198  buf = realloc(*bufp, buflen);
199  if (buf == NULL)
200  return SPF_E_NO_MEMORY;
201 
202  // memset(buf + *buflenp, '\0', buflen - *buflenp);
203  *bufp = buf;
204  *buflenp = buflen;
205  }
206  else {
207  SPF_ASSERT_NOTNULL(*bufp);
208  }
209 
210  memset(*bufp, '\0', *buflenp);
211  return SPF_E_SUCCESS;
212 }
SPF_RESULT_FAIL
@ SPF_RESULT_FAIL
Definition: spf_response.h:83
SPF_REASON_DEFAULT
@ SPF_REASON_DEFAULT
Definition: spf_response.h:106
SPF_REASON_MECH
@ SPF_REASON_MECH
Definition: spf_response.h:105
SPF_errcode_t
SPF_errcode_t
Definition: spf_response.h:118
ns_t_a
@ ns_t_a
Definition: arpa_nameser.h:302
ns_t_invalid
@ ns_t_invalid
Definition: arpa_nameser.h:301
SPF_RESULT_NONE
@ SPF_RESULT_NONE
Definition: spf_response.h:86
SPF_REASON_2MX
@ SPF_REASON_2MX
Definition: spf_response.h:107
spf.h
SPF_RESULT_INVALID
@ SPF_RESULT_INVALID
Definition: spf_response.h:80
SPF_LIB_VERSION_PATCH
#define SPF_LIB_VERSION_PATCH
Definition: spf_lib_version.h:10
SPF_RESULT_NEUTRAL
@ SPF_RESULT_NEUTRAL
Definition: spf_response.h:81
SPF_strresult
const char * SPF_strresult(SPF_result_t result)
Definition: spf_utils.c:84
SPF_REASON_LOCAL_POLICY
@ SPF_REASON_LOCAL_POLICY
Definition: spf_response.h:104
ns_t_aaaa
@ ns_t_aaaa
Definition: arpa_nameser.h:329
SPF_strrrtype
const char * SPF_strrrtype(ns_type rr_type)
Definition: spf_utils.c:166
SPF_LIB_VERSION_MINOR
#define SPF_LIB_VERSION_MINOR
Definition: spf_lib_version.h:9
SPF_E_SUCCESS
@ SPF_E_SUCCESS
Definition: spf_response.h:120
SPF_recalloc
SPF_errcode_t SPF_recalloc(char **bufp, size_t *buflenp, size_t buflen)
Definition: spf_utils.c:191
ns_t_spf
#define ns_t_spf
Definition: spf_dns.h:89
SPF_REASON_LOCALHOST
@ SPF_REASON_LOCALHOST
Definition: spf_response.h:103
ns_t_any
@ ns_t_any
Definition: arpa_nameser.h:350
SPF_RESULT_PERMERROR
@ SPF_RESULT_PERMERROR
Definition: spf_response.h:88
ns_type
int ns_type
Definition: spf_dns.h:85
spf_internal.h
SPF_E_NO_MEMORY
@ SPF_E_NO_MEMORY
Definition: spf_response.h:121
ns_t_txt
@ ns_t_txt
Definition: arpa_nameser.h:317
SPF_RESULT_PASS
@ SPF_RESULT_PASS
Definition: spf_response.h:82
SPF_strreason
const char * SPF_strreason(SPF_reason_t reason)
Definition: spf_utils.c:131
SPF_sanitize
char * SPF_sanitize(SPF_server_t *spf_server, char *str)
Definition: spf_utils.c:57
SPF_RESULT_SOFTFAIL
@ SPF_RESULT_SOFTFAIL
Definition: spf_response.h:84
SPF_RESULT_TEMPERROR
@ SPF_RESULT_TEMPERROR
Definition: spf_response.h:87
NULL
#define NULL
Definition: spf_internal.h:28
SPF_ASSERT_NOTNULL
#define SPF_ASSERT_NOTNULL(x)
Definition: spf_log.h:118
SPF_REASON_NONE
@ SPF_REASON_NONE
Definition: spf_response.h:101
SPF_LIB_VERSION_MAJOR
#define SPF_LIB_VERSION_MAJOR
Definition: spf_lib_version.h:8
ns_t_mx
@ ns_t_mx
Definition: arpa_nameser.h:316
ns_t_ptr
@ ns_t_ptr
Definition: arpa_nameser.h:313
spf_sys_config.h
SPF_get_lib_version
void SPF_get_lib_version(int *major, int *minor, int *patch)
Definition: spf_utils.c:41