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 
38 void
39 SPF_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 
54 char *
55 SPF_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 
81 const char *
83 {
84  switch (result) {
85  case SPF_RESULT_INVALID:
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 
128 const 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 
148  case SPF_REASON_DEFAULT:
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 
163 const 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 
189 SPF_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 }
#define ns_t_invalid
Definition: spf_dns.h:93
SPF_reason_t
Definition: spf_response.h:99
#define ns_t_txt
Definition: spf_dns.h:80
char * SPF_sanitize(SPF_server_t *spf_server, char *str)
Definition: spf_utils.c:55
#define ns_t_any
Definition: spf_dns.h:83
SPF_result_t
Definition: spf_response.h:78
void SPF_get_lib_version(int *major, int *minor, int *patch)
Definition: spf_utils.c:39
#define SPF_ASSERT_NOTNULL(x)
Definition: spf_log.h:118
#define SPF_LIB_VERSION_PATCH
SPF_errcode_t
Definition: spf_response.h:118
#define NULL
Definition: spf_internal.h:28
#define ns_t_spf
Definition: spf_dns.h:89
#define ns_t_ptr
Definition: spf_dns.h:78
int ns_type
Definition: spf_dns.h:85
#define ns_t_aaaa
Definition: spf_dns.h:81
const char * SPF_strrrtype(ns_type rr_type)
Definition: spf_utils.c:164
#define SPF_LIB_VERSION_MAJOR
#define ns_t_a
Definition: spf_dns.h:75
const char * SPF_strreason(SPF_reason_t reason)
Definition: spf_utils.c:129
#define SPF_LIB_VERSION_MINOR
const char * SPF_strresult(SPF_result_t result)
Definition: spf_utils.c:82
SPF_errcode_t SPF_recalloc(char **bufp, size_t *buflenp, size_t buflen)
Definition: spf_utils.c:189
#define ns_t_mx
Definition: spf_dns.h:79