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 
37 void
38 SPF_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 
53 char *
54 SPF_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 
80 const char *
82 {
83  switch (result) {
84  case SPF_RESULT_INVALID:
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 
127 const 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 
147  case SPF_REASON_DEFAULT:
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 
162 const 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 
188 SPF_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 }
#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:54
#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:38
#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:163
#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:128
#define SPF_LIB_VERSION_MINOR
const char * SPF_strresult(SPF_result_t result)
Definition: spf_utils.c:81
SPF_errcode_t SPF_recalloc(char **bufp, size_t *buflenp, size_t buflen)
Definition: spf_utils.c:188
#define ns_t_mx
Definition: spf_dns.h:79