1 #ifndef MAIL_STORAGE_SERVICE_H
2 #define MAIL_STORAGE_SERVICE_H
3 
4 #include "net.h"
5 
6 struct master_service;
7 struct mail_user;
8 struct setting_parser_context;
9 struct setting_parser_info;
10 struct mail_storage_service_user;
11 
12 enum mail_storage_service_flags {
13 	/* Allow not dropping root privileges */
14 	MAIL_STORAGE_SERVICE_FLAG_ALLOW_ROOT		= 0x01,
15 	/* Lookup user from userdb */
16 	MAIL_STORAGE_SERVICE_FLAG_USERDB_LOOKUP		= 0x02,
17 	/* Force mail_debug=yes */
18 	MAIL_STORAGE_SERVICE_FLAG_DEBUG			= 0x04,
19 	/* Keep the current process permissions */
20 	MAIL_STORAGE_SERVICE_FLAG_NO_RESTRICT_ACCESS	= 0x08,
21 	/* Don't chdir() to user's home */
22 	MAIL_STORAGE_SERVICE_FLAG_NO_CHDIR		= 0x10,
23 	/* Drop privileges only temporarily (keep running as setuid-root) */
24 	MAIL_STORAGE_SERVICE_FLAG_TEMP_PRIV_DROP	= 0x20,
25 	/* Enable core dumps even when dropping privileges temporarily */
26 	MAIL_STORAGE_SERVICE_FLAG_ENABLE_CORE_DUMPS	= 0x40,
27 	/* Don't initialize logging or change log prefixes */
28 	MAIL_STORAGE_SERVICE_FLAG_NO_LOG_INIT		= 0x80,
29 	/* Don't load plugins in _service_lookup() */
30 	MAIL_STORAGE_SERVICE_FLAG_NO_PLUGINS		= 0x100,
31 	/* Don't close auth connections because of idling. */
32 	MAIL_STORAGE_SERVICE_FLAG_NO_IDLE_TIMEOUT	= 0x200,
33 	/* When executing doveconf, tell it to use sysexits codes */
34 	MAIL_STORAGE_SERVICE_FLAG_USE_SYSEXITS		= 0x400,
35 	/* Don't create namespaces, only the user. */
36 	MAIL_STORAGE_SERVICE_FLAG_NO_NAMESPACES		= 0x800,
37 	/* Disable reading ssl_ca setting to save memory. */
38 	MAIL_STORAGE_SERVICE_FLAG_NO_SSL_CA		= 0x1000,
39 };
40 
41 struct mail_storage_service_input {
42 	struct event *event_parent;
43 
44 	const char *module;
45 	const char *service;
46 	const char *username;
47 	/* If set, use this string as the session ID */
48 	const char *session_id;
49 	/* If set, use this string as the session ID prefix, but also append
50 	   a unique session ID suffix to it. */
51 	const char *session_id_prefix;
52 	/* If non-zero, override timestamp when session was created and set
53 	   mail_user.session_restored=TRUE */
54 	time_t session_create_time;
55 
56 	struct ip_addr local_ip, remote_ip;
57 	in_port_t local_port, remote_port;
58 
59 	const char *const *userdb_fields;
60 
61 	const char *forward_fields;
62 
63 	/* Override specified global flags */
64 	enum mail_storage_service_flags flags_override_add;
65 	enum mail_storage_service_flags flags_override_remove;
66 
67 	/* override MAIL_STORAGE_SERVICE_FLAG_USERDB_LOOKUP for this lookup */
68 	bool no_userdb_lookup:1;
69 	/* Enable auth_debug=yes for this lookup */
70 	bool debug:1;
71 	/* Connection is secure (SSL or just trusted) */
72 	bool conn_secured:1;
73 	/* Connection is secured using SSL specifically */
74 	bool conn_ssl_secured:1;
75 };
76 
77 extern struct module *mail_storage_service_modules;
78 
79 struct mail_storage_service_ctx *
80 mail_storage_service_init(struct master_service *service,
81 			  const struct setting_parser_info *set_roots[],
82 			  enum mail_storage_service_flags flags) ATTR_NULL(2);
83 struct auth_master_connection *
84 mail_storage_service_get_auth_conn(struct mail_storage_service_ctx *ctx);
85 /* Set auth connection (instead of creating a new one automatically). */
86 void mail_storage_service_set_auth_conn(struct mail_storage_service_ctx *ctx,
87 					struct auth_master_connection *conn);
88 int mail_storage_service_read_settings(struct mail_storage_service_ctx *ctx,
89 				       const struct mail_storage_service_input *input,
90 				       pool_t pool,
91 				       const struct setting_parser_info **user_info_r,
92 				       const struct setting_parser_context **parser_r,
93 				       const char **error_r) ATTR_NULL(2);
94 /* Read settings and initialize context to use them. Do nothing if service is
95    already initialized. This is mainly necessary when calling _get_auth_conn()
96    or _all_init(). */
97 void mail_storage_service_init_settings(struct mail_storage_service_ctx *ctx,
98 					const struct mail_storage_service_input *input)
99 	ATTR_NULL(2);
100 /* Returns 1 if ok, 0 if user wasn't found, -1 if fatal error,
101    -2 if error is user-specific (e.g. invalid settings). */
102 int mail_storage_service_lookup(struct mail_storage_service_ctx *ctx,
103 				const struct mail_storage_service_input *input,
104 				struct mail_storage_service_user **user_r,
105 				const char **error_r);
106 /* The next mail_storage_service_lookup() will save the userdb fields into the
107    given pointer, allocated from the given pool. */
108 void mail_storage_service_save_userdb_fields(struct mail_storage_service_ctx *ctx,
109 					     pool_t pool, const char *const **userdb_fields_r);
110 /* Returns 0 if ok, -1 if fatal error, -2 if error is user-specific. */
111 int mail_storage_service_next(struct mail_storage_service_ctx *ctx,
112 			      struct mail_storage_service_user *user,
113 			      struct mail_user **mail_user_r,
114 			      const char **error_r);
115 /* Returns 0 if ok, -1 if fatal error, -2 if error is user-specific. */
116 int mail_storage_service_next_with_session_suffix(struct mail_storage_service_ctx *ctx,
117 						  struct mail_storage_service_user *user,
118 						  const char *session_id_postfix,
119 						  struct mail_user **mail_user_r,
120 						   const char **error_r);
121 void mail_storage_service_restrict_setenv(struct mail_storage_service_ctx *ctx,
122 					  struct mail_storage_service_user *user);
123 /* Combine lookup() and next() into one call. */
124 int mail_storage_service_lookup_next(struct mail_storage_service_ctx *ctx,
125 				     const struct mail_storage_service_input *input,
126 				     struct mail_storage_service_user **user_r,
127 				     struct mail_user **mail_user_r,
128 				     const char **error_r);
129 void mail_storage_service_user_ref(struct mail_storage_service_user *user);
130 void mail_storage_service_user_unref(struct mail_storage_service_user **user);
131 /* Initialize iterating through all users. */
132 void mail_storage_service_all_init(struct mail_storage_service_ctx *ctx);
133 /* Initialize iterating through all users with a user mask hint to the
134    userdb iteration lookup. This itself isn't yet guaranteed to filter out any
135    usernames. */
136 void mail_storage_service_all_init_mask(struct mail_storage_service_ctx *ctx,
137 					const char *user_mask_hint);
138 /* Iterate through all usernames. Returns 1 if username was returned, 0 if
139    there are no more users, -1 if error. */
140 int mail_storage_service_all_next(struct mail_storage_service_ctx *ctx,
141 				  const char **username_r);
142 void mail_storage_service_deinit(struct mail_storage_service_ctx **ctx);
143 /* Returns the first created service context. If it gets freed, NULL is
144    returned until the next time mail_storage_service_init() is called. */
145 struct mail_storage_service_ctx *mail_storage_service_get_global(void);
146 
147 /* Activate user context. Normally this is called automatically by the ioloop,
148    but e.g. during loops at deinit where all users are being destroyed, it's
149    useful to call this to set the correct user-specific log prefix. */
150 void mail_storage_service_io_activate_user(struct mail_storage_service_user *user);
151 /* Deactivate user context. This only switches back to non-user-specific
152    log prefix. */
153 void mail_storage_service_io_deactivate_user(struct mail_storage_service_user *user);
154 
155 /* Return the settings pointed to by set_root parameter in _init().
156    The settings contain all the changes done by userdb lookups. */
157 void **mail_storage_service_user_get_set(struct mail_storage_service_user *user);
158 const struct mail_storage_settings *
159 mail_storage_service_user_get_mail_set(struct mail_storage_service_user *user);
160 const struct mail_storage_service_input *
161 mail_storage_service_user_get_input(struct mail_storage_service_user *user);
162 struct setting_parser_context *
163 mail_storage_service_user_get_settings_parser(struct mail_storage_service_user *user);
164 const struct master_service_ssl_settings *
165 mail_storage_service_user_get_ssl_settings(struct mail_storage_service_user *user);
166 struct mail_storage_service_ctx *
167 mail_storage_service_user_get_service_ctx(struct mail_storage_service_user *user);
168 pool_t mail_storage_service_user_get_pool(struct mail_storage_service_user *user);
169 const char *
170 mail_storage_service_user_get_log_prefix(struct mail_storage_service_user *user);
171 
172 const char *
173 mail_storage_service_get_log_prefix(struct mail_storage_service_ctx *ctx);
174 const struct var_expand_table *
175 mail_storage_service_get_var_expand_table(struct mail_storage_service_ctx *ctx,
176 					  struct mail_storage_service_input *input);
177 const char *mail_storage_service_fields_var_expand(const char *data,
178 						   const char *const *fields);
179 /* Return the settings pointed to by set_root parameter in _init() */
180 void *mail_storage_service_get_settings(struct master_service *service);
181 /* Updates settings for storage service user, forwards return value of settings_parse_keyvalue() */
182 int mail_storage_service_user_set_setting(struct mail_storage_service_user *user,
183 					  const char *key,
184 					  const char *value,
185 					  const char **error_r);
186 
187 #endif
188