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
40void
41SPF_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
56char *
57SPF_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
83const char *
85{
86 switch (result) {
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
130const char *
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
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
165const 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
191SPF_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}
ns_type
#define SPF_LIB_VERSION_PATCH
#define SPF_LIB_VERSION_MINOR
#define SPF_LIB_VERSION_MAJOR
#define ns_t_spf
Definition spf_dns.h:89
#define ns_t_any
Definition spf_dns.h:83
#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
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 NULL
char * SPF_sanitize(SPF_server_t *spf_server, char *str)
Definition spf_utils.c:57
SPF_errcode_t SPF_recalloc(char **bufp, size_t *buflenp, size_t buflen)
Definition spf_utils.c:191
const char * SPF_strrrtype(ns_type rr_type)
Definition spf_utils.c:166
void SPF_get_lib_version(int *major, int *minor, int *patch)
Definition spf_utils.c:41
const char * SPF_strresult(SPF_result_t result)
Definition spf_utils.c:84
const char * SPF_strreason(SPF_reason_t reason)
Definition spf_utils.c:131