1 /*
2  * conf.h
3  *
4  * This file is part of mpop, a POP3 client.
5  *
6  * Copyright (C) 2000, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2014,
7  * 2016, 2018, 2020
8  * Martin Lambers <marlam@marlam.de>
9  * Martin Stenberg <martin@gnutiken.se> (passwordeval support)
10  *
11  *   This program is free software; you can redistribute it and/or modify
12  *   it under the terms of the GNU General Public License as published by
13  *   the Free Software Foundation; either version 3 of the License, or
14  *   (at your option) any later version.
15  *
16  *   This program is distributed in the hope that it will be useful,
17  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
18  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  *   GNU General Public License for more details.
20  *
21  *   You should have received a copy of the GNU General Public License
22  *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
23  */
24 
25 #ifndef CONF_H
26 #define CONF_H
27 
28 #include <stddef.h>
29 
30 #include "list.h"
31 
32 /*
33  * If a function with an 'errstr' argument returns a value != CONF_EOK,
34  * '*errstr' either points to an allocates string containing an error
35  * description or is NULL.
36  * If such a function returns CONF_EOK, 'errstr' will not be changed.
37  */
38 #define CONF_EOK        0       /* no error */
39 #define CONF_EIO        1       /* Input/output error */
40 #define CONF_EPARSE     2       /* Parse error */
41 #define CONF_ESYNTAX    3       /* Syntax error */
42 #define CONF_EINSECURE  4       /* Insecure permissions */
43 
44 /*
45  * An account
46  */
47 #define ACC_HOST                        (1LL << 0LL)
48 #define ACC_PORT                        (1LL << 1LL)
49 #define ACC_TIMEOUT                     (1LL << 2LL)
50 #define ACC_PIPELINING                  (1LL << 3LL)
51 #define ACC_DELIVERY                    (1LL << 4LL)
52 #define ACC_RECEIVED_HEADER             (1LL << 5LL)
53 #define ACC_UIDLS_FILE                  (1LL << 6LL)
54 #define ACC_ONLY_NEW                    (1LL << 7LL)
55 #define ACC_KEEP                        (1LL << 8LL)
56 #define ACC_KILLSIZE                    (1LL << 9LL)
57 #define ACC_SKIPSIZE                    (1LL << 10LL)
58 #define ACC_FILTER                      (1LL << 11LL)
59 #define ACC_AUTH_MECH                   (1LL << 12LL)
60 #define ACC_USERNAME                    (1LL << 13LL)
61 #define ACC_PASSWORD                    (1LL << 14LL)
62 #define ACC_PASSWORDEVAL                (1LL << 15LL)
63 #define ACC_NTLMDOMAIN                  (1LL << 16LL)
64 #define ACC_TLS                         (1LL << 17LL)
65 #define ACC_TLS_KEY_FILE                (1LL << 18LL)
66 #define ACC_TLS_CERT_FILE               (1LL << 19LL)
67 #define ACC_TLS_TRUST_FILE              (1LL << 20LL)
68 #define ACC_TLS_CRL_FILE                (1LL << 21LL)
69 #define ACC_TLS_FINGERPRINT             (1LL << 22LL)
70 #define ACC_TLS_NOCERTCHECK             (1LL << 23LL)
71 #define ACC_TLS_NOSTARTTLS              (1LL << 24LL)
72 #define ACC_TLS_MIN_DH_PRIME_BITS       (1LL << 25LL)
73 #define ACC_TLS_PRIORITIES              (1LL << 26LL)
74 #define ACC_TLS_HOST_OVERRIDE           (1LL << 27LL)
75 #define ACC_PROXY_HOST                  (1LL << 28LL)
76 #define ACC_PROXY_PORT                  (1LL << 29LL)
77 #define ACC_SOURCE_IP                   (1LL << 30LL)
78 #define ACC_SOCKET                      (1LL << 31LL)
79 
80 typedef struct
81 {
82     /* identification */
83     char *id;                   /* name of this account */
84     char *conffile;             /* name of the configuration file of this
85                                    account or NULL for command line */
86     long long mask;             /* combination of the above ACC_* flags.
87                                    Shows which settings were changed */
88     /* POP3 server */
89     char *host;                 /* hostname of POP3 server */
90     int port;                   /* port number */
91     int timeout;                /* connect/input/output timeout in seconds */
92     /* POP3 settings */
93     int pipelining;             /* use pipelining? 0=off, 1=on, 2=auto */
94     int received_header;        /* flag: add Received: header? */
95     int delivery_method;        /* number of the method, from delivery.h */
96     char *delivery_args;        /* arguments for the delivery method */
97     char *uidls_file;           /* file to store UIDLs */
98     int only_new;               /* flag: retrieve only new messages? */
99     int keep;                   /* flag: keep messages on the server? */
100     long long killsize;         /* killsize, -1 when disabled */
101     long long skipsize;         /* skipsize, -1 when disabled */
102     char *filter;               /* a program to filter the mail headers
103                                    through */
104     /* Authentication */
105     char *auth_mech;            /* authentication mechanism */
106     char *username;             /* username for authentication */
107     char *password;             /* password for authentication */
108     char *passwordeval;         /* command for password evaluation */
109     char *ntlmdomain;           /* domain for NTLM authentication */
110     /* TLS / SSL */
111     int tls;                    /* flag: use TLS? */
112     int tls_nostarttls;         /* flag: start TLS immediatly
113                                    (without STARTTLS command)? */
114     char *tls_key_file;         /* file in PEM format */
115     char *tls_cert_file;        /* file in PEM format */
116     char *tls_trust_file;       /* file in PEM format */
117     char *tls_crl_file;         /* file in PEM format */
118     unsigned char *tls_sha256_fingerprint; /* certificate fingerprint */
119     unsigned char *tls_sha1_fingerprint;   /* certificate fingerprint */
120     unsigned char *tls_md5_fingerprint;    /* certificate fingerprint */
121     int tls_nocertcheck;        /* flag: do not check certificate? */
122     int tls_min_dh_prime_bits;  /* parameter; -1 for default */
123     char *tls_priorities;       /* parameter; NULL for default */
124     char *tls_host_override;    /* overrides 'host' for verification; or NULL */
125     /* proxy */
126     char *proxy_host;           /* NULL or proxy hostname */
127     int proxy_port;             /* port number; 0 for default */
128     /* source ip binding */
129     char *source_ip;            /* Source IP to bind the connection to */
130     /* unix domain socket */
131     char *socketname;           /* File name of local socket to connect to */
132 } account_t;
133 
134 /*
135  * account_new()
136  *
137  * Create a new account_t. Built-in default values are filled in, except for
138  * settings whose default values are not yet known. These are port and
139  * uidls_file at the moment.
140  * Must be freed with account_free().
141  * Both arguments may be NULL.
142  */
143 account_t *account_new(const char *conffile, const char *id);
144 
145 /*
146  * account_copy()
147  *
148  * Create a new account which is a copy of 'acc'.
149  * If 'acc' is NULL, NULL is returned.
150  */
151 account_t *account_copy(account_t *acc);
152 
153 /*
154  * account_free()
155  *
156  * Free an account_t.
157  */
158 void account_free(void *a);
159 
160 /*
161  * find_account()
162  *
163  * Find an account in a list of accounts by its id.
164  * Returns the account or NULL.
165  */
166 account_t *find_account(list_t *acc_list, const char *id);
167 
168 /*
169  * is_on(), is_off()
170  *
171  * Check whether the given string is "on" or "off"
172  */
173 int is_on(char *s);
174 int is_off(char *s);
175 
176 /*
177  * get_fingerprint()
178  *
179  * Gets a fingerprint of the given length and returns it in an allocated array.
180  * Returns NULL on error.
181  */
182 unsigned char *get_fingerprint(const char *arg, size_t len);
183 
184 /*
185  * check_auth_arg()
186  *
187  * checks if the given string is a proper argument to the auth command.
188  * If so, the string is converted to uppercase and 0 is returned. Otherwise, 1
189  * is returned.
190  * Note that you have to check whether the arg is "off" separately, because
191  * that value results in NULL.
192  */
193 int check_auth_arg(char *arg);
194 
195 /*
196  * get_non_neg_int()
197  *
198  * Gets a non-negative integer. Returns -1 on error.
199  */
200 int get_non_neg_int(const char *arg);
201 
202 /*
203  * get_size_arg()
204  *
205  * Gets a size argument. Returns -1 on error.
206  */
207 long long get_size_arg(const char *arg);
208 
209 /*
210  * override_account()
211  *
212  * Override the settings of 'acc1' with the settings of 'acc2' when the
213  * appropriate flag is set in acc2->mask.
214  * The flags from acc2->mask will also be set in acc1->mask.
215  */
216 void override_account(account_t *acc1, account_t *acc2);
217 
218 /*
219  * check_account()
220  *
221  * Check an account_t. 'retrmail' must indicate whether mpop works in mail
222  * retrieval mode, because some checks depend on this.
223  * If this function returns CONF_ESYNTAX, *errstr will always point to an
224  * error string.
225  * Used error codes: CONF_ESYNTAX
226  */
227 int check_account(account_t *acc, int retrmail, char **errstr);
228 
229 /*
230  * get_conf()
231  *
232  * Read 'conffile' and store all account data in 'acc_list'.
233  * If 'securitycheck' is set, the file must not have more permissions than 0600,
234  * must be a regular file and owned by the current user.
235  * Used error codes: CONF_EIO, CONF_EPARSE, CONF_ESYNTAX
236  */
237 int get_conf(const char *conffile, int securitycheck, list_t **acc_list,
238         char **errstr);
239 
240 #endif
241