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