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
29
30#include "spf.h"
31#include "spf_internal.h"
32
33
37void
38SPF_get_lib_version(int *major, int *minor, int *patch)
39{
40 *major = SPF_LIB_VERSION_MAJOR;
41 *minor = SPF_LIB_VERSION_MINOR;
42 *patch = SPF_LIB_VERSION_PATCH;
43}
44
45
46
53char *
54SPF_sanitize(SPF_server_t *spf_server, char *str)
55{
56 char *p;
57
58 SPF_ASSERT_NOTNULL(spf_server);
59
60 if (! spf_server->sanitize)
61 return str;
62
63 if (str == NULL)
64 return str;
65
66 for (p = str; *p != '\0'; p++)
67 if (! isprint( (unsigned char)*p ))
68 *p = '?';
69
70 return str;
71}
72
73
74
75
76
80const char *
82{
83 switch (result) {
85 return "(invalid)";
86 break;
87
88 case SPF_RESULT_PASS: /* + */
89 return "pass";
90 break;
91
92 case SPF_RESULT_FAIL: /* - */
93 return "fail";
94 break;
95
96 case SPF_RESULT_SOFTFAIL: /* ~ */
97 return "softfail";
98 break;
99
100 case SPF_RESULT_NEUTRAL: /* ? */
101 return "neutral";
102 break;
103
104 case SPF_RESULT_PERMERROR: /* permanent error */
105 return "permerror";
106 break;
107
108 case SPF_RESULT_TEMPERROR: /* temporary error */
109 return "temperror";
110 break;
111
112 case SPF_RESULT_NONE: /* no SPF record found */
113 return "none";
114 break;
115
116 default:
117 return "(error: unknown result)";
118 break;
119 }
120}
121
122
123
127const char *
129{
130 switch (reason) {
131 case SPF_REASON_NONE:
132 return "none";
133 break;
134
136 return "localhost";
137 break;
138
140 return "local policy";
141 break;
142
143 case SPF_REASON_MECH:
144 return "mechanism";
145 break;
146
148 return "default";
149 break;
150
151 case SPF_REASON_2MX:
152 return "secondary MX";
153 break;
154
155 default:
156 return "(invalid reason)";
157 break;
158
159 }
160}
161
162const char *
164{
165 switch (rr_type) {
166 case ns_t_a: return "A";
167 case ns_t_aaaa: return "AAAA";
168 case ns_t_any: return "ANY";
169 case ns_t_invalid: return "BAD";
170 case ns_t_mx: return "MX";
171 case ns_t_ptr: return "PTR";
172 case ns_t_spf: return "SPF";
173 case ns_t_txt: return "TXT";
174 default: return "??";
175 }
176}
177
188SPF_recalloc(char **bufp, size_t *buflenp, size_t buflen)
189{
190 char *buf;
191
192 if (*buflenp < buflen) {
193 if (buflen < 64)
194 buflen = 64;
195 buf = realloc(*bufp, buflen);
196 if (buf == NULL)
197 return SPF_E_NO_MEMORY;
198
199 // memset(buf + *buflenp, '\0', buflen - *buflenp);
200 *bufp = buf;
201 *buflenp = buflen;
202 }
203 else {
204 SPF_ASSERT_NOTNULL(*bufp);
205 }
206
207 memset(*bufp, '\0', *buflenp);
208 return SPF_E_SUCCESS;
209}
char * SPF_sanitize(SPF_server_t *spf_server, char *str)
Definition: spf_utils.c:54
SPF_errcode_t SPF_recalloc(char **bufp, size_t *buflenp, size_t buflen)
Definition: spf_utils.c:188
const char * SPF_strrrtype(ns_type rr_type)
Definition: spf_utils.c:163
void SPF_get_lib_version(int *major, int *minor, int *patch)
Definition: spf_utils.c:38
const char * SPF_strresult(SPF_result_t result)
Definition: spf_utils.c:81
const char * SPF_strreason(SPF_reason_t reason)
Definition: spf_utils.c:128
ns_type
Definition: arpa_nameser.h:300
@ ns_t_ptr
Definition: arpa_nameser.h:313
@ ns_t_mx
Definition: arpa_nameser.h:316
@ ns_t_any
Definition: arpa_nameser.h:350
@ ns_t_invalid
Definition: arpa_nameser.h:301
@ ns_t_a
Definition: arpa_nameser.h:302
@ ns_t_txt
Definition: arpa_nameser.h:317
@ ns_t_aaaa
Definition: arpa_nameser.h:329
#define SPF_LIB_VERSION_PATCH
#define SPF_LIB_VERSION_MINOR
#define SPF_LIB_VERSION_MAJOR
SPF_result_t
Definition: spf_response.h:79
@ SPF_RESULT_PERMERROR
Definition: spf_response.h:88
@ SPF_RESULT_NONE
Definition: spf_response.h:86
@ SPF_RESULT_INVALID
Definition: spf_response.h:80
@ SPF_RESULT_PASS
Definition: spf_response.h:82
@ SPF_RESULT_NEUTRAL
Definition: spf_response.h:81
@ SPF_RESULT_TEMPERROR
Definition: spf_response.h:87
@ SPF_RESULT_SOFTFAIL
Definition: spf_response.h:84
@ SPF_RESULT_FAIL
Definition: spf_response.h:83
SPF_reason_t
Definition: spf_response.h:100
@ SPF_REASON_NONE
Definition: spf_response.h:101
@ SPF_REASON_2MX
Definition: spf_response.h:107
@ SPF_REASON_LOCALHOST
Definition: spf_response.h:103
@ SPF_REASON_LOCAL_POLICY
Definition: spf_response.h:104
@ SPF_REASON_DEFAULT
Definition: spf_response.h:106
@ SPF_REASON_MECH
Definition: spf_response.h:105
SPF_errcode_t
Definition: spf_response.h:119
@ SPF_E_NO_MEMORY
Definition: spf_response.h:121
@ SPF_E_SUCCESS
Definition: spf_response.h:120
#define SPF_ASSERT_NOTNULL(x)
Definition: spf_log.h:118
#define NULL
Definition: spf_internal.h:28
#define ns_t_spf
Definition: spf_dns.h:89