1 /*
2  * conf.h
3  *
4  * This file is part of msmtp, an SMTP client.
5  *
6  * Copyright (C) 2000, 2003, 2004, 2005, 2006, 2007, 2008, 2010, 2011, 2014,
7  * 2016, 2018, 2019, 2020
8  * Martin Lambers <marlam@marlam.de>
9  * Martin Stenberg <martin@gnutiken.se> (passwordeval support)
10  * Scott Shumate <sshumate@austin.rr.com> (aliases support)
11  *
12  *   This program is free software; you can redistribute it and/or modify
13  *   it under the terms of the GNU General Public License as published by
14  *   the Free Software Foundation; either version 3 of the License, or
15  *   (at your option) any later version.
16  *
17  *   This program is distributed in the hope that it will be useful,
18  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
19  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  *   GNU General Public License for more details.
21  *
22  *   You should have received a copy of the GNU General Public License
23  *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
24  */
25 
26 #ifndef CONF_H
27 #define CONF_H
28 
29 #include <stddef.h>
30 
31 #include "list.h"
32 
33 /*
34  * If a function with an 'errstr' argument returns a value != CONF_EOK,
35  * '*errstr' either points to an allocates string containing an error
36  * description or is NULL.
37  * If such a function returns CONF_EOK, 'errstr' will not be changed.
38  */
39 #define CONF_EOK        0       /* no error */
40 #define CONF_ECANTOPEN  1       /* Cannot open file */
41 #define CONF_EIO        2       /* Input/output error */
42 #define CONF_EPARSE     3       /* Parse error */
43 #define CONF_ESYNTAX    4       /* Syntax error */
44 #define CONF_EINSECURE  5       /* Insecure permissions */
45 
46 /*
47  * An account
48  */
49 
50 #define ACC_HOST                        (1LL << 0LL)
51 #define ACC_PORT                        (1LL << 1LL)
52 #define ACC_TIMEOUT                     (1LL << 2LL)
53 #define ACC_PROTOCOL                    (1LL << 3LL)
54 #define ACC_AUTO_FROM                   (1LL << 4LL)
55 #define ACC_FROM                        (1LL << 5LL)
56 #define ACC_DOMAIN                      (1LL << 6LL)
57 #define ACC_MAILDOMAIN                  (1LL << 7LL)
58 #define ACC_DSN_RETURN                  (1LL << 8LL)
59 #define ACC_DSN_NOTIFY                  (1LL << 9LL)
60 #define ACC_AUTH_MECH                   (1LL << 10LL)
61 #define ACC_USERNAME                    (1LL << 11LL)
62 #define ACC_PASSWORD                    (1LL << 12LL)
63 #define ACC_PASSWORDEVAL                (1LL << 13LL)
64 #define ACC_NTLMDOMAIN                  (1LL << 14LL)
65 #define ACC_TLS                         (1LL << 15LL)
66 #define ACC_TLS_NOSTARTTLS              (1LL << 16LL)
67 #define ACC_TLS_KEY_FILE                (1LL << 17LL)
68 #define ACC_TLS_CERT_FILE               (1LL << 18LL)
69 #define ACC_TLS_TRUST_FILE              (1LL << 19LL)
70 #define ACC_TLS_CRL_FILE                (1LL << 20LL)
71 #define ACC_TLS_FINGERPRINT             (1LL << 21LL)
72 #define ACC_TLS_NOCERTCHECK             (1LL << 22LL)
73 #define ACC_TLS_MIN_DH_PRIME_BITS       (1LL << 23LL)
74 #define ACC_TLS_PRIORITIES              (1LL << 24LL)
75 #define ACC_TLS_HOST_OVERRIDE           (1LL << 25LL)
76 #define ACC_LOGFILE                     (1LL << 26LL)
77 #define ACC_SYSLOG                      (1LL << 27LL)
78 #define ACC_ALIASES                     (1LL << 28LL)
79 #define ACC_PROXY_HOST                  (1LL << 29LL)
80 #define ACC_PROXY_PORT                  (1LL << 30LL)
81 #define ACC_SET_FROM_HEADER             (1LL << 31LL)
82 #define ACC_SET_DATE_HEADER             (1LL << 32LL)
83 #define ACC_REMOVE_BCC_HEADERS          (1LL << 33LL)
84 #define ACC_UNDISCLOSED_RECIPIENTS      (1LL << 34LL)
85 #define ACC_SOURCE_IP                   (1LL << 35LL)
86 #define ACC_LOGFILE_TIME_FORMAT         (1LL << 36LL)
87 #define ACC_SOCKET                      (1LL << 37LL)
88 
89 typedef struct
90 {
91     /* identification */
92     char *id;                   /* name of this account */
93     char *conffile;             /* name of the configuration file of this
94                                    account or NULL for command line */
95     long long mask;             /* combination of the above ACC_* flags.
96                                    Shows which settings were changed */
97     /* SMTP server */
98     char *host;                 /* hostname of SMTP server */
99     int port;                   /* port number */
100     int timeout;                /* network timeout in seconds */
101     /* SMTP settings */
102     int protocol;               /* which SMTP sub protocol? */
103     char *domain;               /* argument to EHLO command */
104     char *from;                 /* envelope from address */
105     int auto_from;              /* obsolete: automatic envelope-from addresses? */
106     char *maildomain;           /* obsolete: the domain part of generated
107                                    envelope from addresses */
108     char *dsn_return;           /* DSN return request */
109     char *dsn_notify;           /* DSN notify request */
110     /* Authentication */
111     char *auth_mech;            /* authentication mechanism */
112     char *username;             /* username for authentication */
113     char *password;             /* password for authentication */
114     char *passwordeval;         /* command for password evaluation */
115     char *ntlmdomain;           /* domain for NTLM authentication */
116     /* TLS / SSL */
117     int tls;                    /* flag: use TLS? */
118     char *tls_key_file;         /* file in PEM format */
119     char *tls_cert_file;        /* file in PEM format */
120     char *tls_trust_file;       /* file in PEM format */
121     char *tls_crl_file;         /* file in PEM format */
122     unsigned char *tls_sha256_fingerprint; /* certificate fingerprint */
123     unsigned char *tls_sha1_fingerprint;   /* certificate fingerprint */
124     unsigned char *tls_md5_fingerprint;    /* certificate fingerprint */
125     int tls_nostarttls;         /* flag: start TLS immediatly
126                                    (without STARTTLS command)? */
127     int tls_nocertcheck;        /* flag: do not check certificate? */
128     int tls_min_dh_prime_bits;  /* parameter; -1 for default */
129     char *tls_priorities;       /* parameter; NULL for default */
130     char *tls_host_override;    /* overrides 'host' for verification; or NULL */
131     /* logging */
132     char *logfile;              /* NULL or logfile */
133     char *logfile_time_format;  /* NULL or format string for strftime() */
134     char *syslog;               /* NULL or syslog facility */
135     char *aliases;              /* NULL or aliases file */
136     /* proxy */
137     char *proxy_host;           /* NULL or proxy hostname */
138     int proxy_port;             /* port number; 0 for default */
139     /* header modifications */
140     int set_from_header;        /* 0=off, 1=on, 2=auto */
141     int set_date_header;        /* 0=off,       2=auto */
142     int remove_bcc_headers;     /* remove the Bcc headers from the mail? */
143     int undisclosed_recipients; /* remove To, Cc, Bcc, add anonymous To */
144     /* source ip binding */
145     char *source_ip;            /* Source IP to bind the connection to */
146     /* unix domain socket */
147     char *socketname;           /* File name of local socket to connect to */
148 } account_t;
149 
150 
151 /*
152  * account_new()
153  *
154  * Create a new account_t. Built-in default values are filled in, except for
155  * settings whose default values are not yet known. This currently only affects
156  * the port.
157  * Must be freed with account_free().
158  * Both arguments may be NULL.
159  */
160 account_t *account_new(const char *conffile, const char *id);
161 
162 /*
163  * account_copy()
164  *
165  * Create a new account which is a copy of 'acc'.
166  * If 'acc' is NULL, NULL is returned.
167  */
168 account_t *account_copy(account_t *acc);
169 
170 /*
171  * account_free()
172  *
173  * Free an account_t.
174  */
175 void account_free(void *a);
176 
177 /*
178  * find_account()
179  *
180  * Find an account in a list of accounts by its id.
181  * Returns the account or NULL.
182  */
183 account_t *find_account(list_t *acc_list, const char *id);
184 
185 /*
186  * find_account_by_envelope_from()
187  *
188  * Find an account in a list of accounts by its envelope from address.
189  * Returns the *first* account that matches or NULL.
190  */
191 account_t *find_account_by_envelope_from(list_t *acc_list, const char *from);
192 
193 /*
194  * is_on(), is_off(), is_auto()
195  *
196  * Check whether the given string is "on" or "off" or "auto"
197  */
198 int is_on(const char *s);
199 int is_off(const char *s);
200 int is_auto(const char *s);
201 
202 /*
203  * get_pos_int()
204  *
205  * Gets a positive integer. Returns -1 on error.
206  */
207 int get_pos_int(const char *arg);
208 
209 /*
210  * get_fingerprint()
211  *
212  * Gets a fingerprint of the given length and returns it in an allocated array.
213  * Returns NULL on error.
214  */
215 unsigned char *get_fingerprint(const char *arg, size_t len);
216 
217 /*
218  * check_auth_arg()
219  *
220  * checks if the given string is a proper argument to the auth command.
221  * If so, the string is converted to uppercase and 0 is returned. Otherwise, 1
222  * is returned.
223  * Note that you have to check whether the arg is "off" separately, because
224  * that value results in NULL.
225  */
226 int check_auth_arg(char *arg);
227 
228 /*
229  * check_dsn_notify_arg()
230  *
231  * checks if the given string is a proper argument to the dsn_notify command.
232  * If so, the string is converted to uppercase and 0 is returned. Otherwise, 1
233  * is returned.
234  * Note that you have to check whether the arg is "off" separately, because that
235  * value results in NULL.
236  */
237 int check_dsn_notify_arg(char *arg);
238 
239 /*
240  * check_syslog_arg()
241  *
242  * checks if the given string is a proper argument to the syslog command.
243  * If so, the string is converted to uppercase and 0 is returned. Otherwise, 1
244  * is returned.
245  * Note that you have to check whether the arg is "on" or "off" or ""
246  * separately.
247  */
248 int check_syslog_arg(const char *arg);
249 
250 /*
251  * get_default_syslog_facility()
252  *
253  * Returns a pointer to an allocated string containing the default syslog
254  * facility.
255  */
256 char *get_default_syslog_facility(void);
257 
258 /*
259  * override_account()
260  *
261  * Override 'acc1' with 'acc2': those settings of acc2 whose flag is set in
262  * acc2->mask override the corresponding settings of acc1.
263  * The flags from acc2->mask will also be set in acc1->mask.
264  */
265 void override_account(account_t *acc1, account_t *acc2);
266 
267 /*
268  * check_account()
269  *
270  * Check an account_t. 'sendmail_mode' must indicate whether msmtp works
271  * in sendmail mode, because some checks depend on this.
272  * If this function returns CONF_ESYNTAX, *errstr will always point to an
273  * error string.
274  * Used error codes: CONF_ESYNTAX
275  */
276 int check_account(account_t *acc, int sendmail_mode, char **errstr);
277 
278 /*
279  * expand_from()
280  *
281  * Expands the argument of the 'from' command: replaces the substitution
282  * patterns with appropriate values. The 'from' string must be allocated,
283  * and the returned string replaces it. This function returns CONF_EOK
284  * or, if an error occured, one of the CONF_E* error codes, in which case
285  * *errstr will contain an error string.
286  */
287 int expand_from(char **from, char **errstr);
288 
289 /*
290  * expand_domain()
291  *
292  * Expands the argument of the 'domain' command: replaces the substitution
293  * patterns with appropriate values. The 'domain' string must be allocated,
294  * and the returned string replaces it. This function returns CONF_EOK
295  * or, if an error occured, one of the CONF_E* error codes, in which case
296  * *errstr will contain an error string.
297  */
298 int expand_domain(char **domain, char **errstr);
299 
300 /*
301  * get_conf()
302  *
303  * Read 'conffile' and store all account data in 'acc_list'.
304  * If 'securitycheck' is set, the file must not have more permissions than 0600,
305  * must be a regular file and owned by the current user.
306  * Used error codes: CONF_ECANTOPEN, CONF_EIO, CONF_EPARSE, CONF_ESYNTAX
307  */
308 int get_conf(const char *conffile, int securitycheck, list_t **acc_list,
309         char **errstr);
310 
311 #endif
312