Revision bump
[mmondor.git] / mmsoftware / mmmail / src / mmpop3d / mmpop3d.h
CommitLineData
1099fea4 1/* $Id: mmpop3d.h,v 1.29 2009/01/17 03:48:20 mmondor Exp $ */
47071c2b
MM
2
3/*
667273fa 4 * Copyright (C) 2001-2008, Matthew Mondor
47071c2b
MM
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software written by Matthew Mondor.
18 * 4. The name of Matthew Mondor may not be used to endorse or promote
19 * products derived from this software without specific prior written
20 * permission.
667273fa
MM
21 * 5. Redistribution of source code may not be released under the terms of
22 * any GNU Public License derivate.
47071c2b
MM
23 *
24 * THIS SOFTWARE IS PROVIDED BY MATTHEW MONDOR ``AS IS'' AND ANY EXPRESS OR
25 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
26 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
27 * IN NO EVENT SHALL MATTHEW MONDOR BE LIABLE FOR ANY DIRECT, INDIRECT,
28 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
29 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
30 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
31 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
33 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 */
35
36
37
38
39#ifndef MMPOP3D_H
40#define MMPOP3D_H
41
42
43
44
45/* HEADERS */
46
47#include <sys/types.h>
b232bd02
MM
48#include <stdbool.h>
49#include <stdint.h>
47071c2b 50
3dd832e3 51#include <pthread.h>
5eb34fba 52
47071c2b 53#include <mmtypes.h>
399db776 54#include <mmhash.h>
7a56f31f 55#include <mmpool.h>
47071c2b
MM
56#include <mmserver.h>
57#include <mmlist.h>
58#include <mmfd.h>
59#include <mmstat.h>
60
3dd832e3
MM
61#include <mm_pthread_sleep.h>
62
46173fd8
MM
63#include <libpq-fe.h>
64
47071c2b
MM
65
66
67
68/* DEFINITIONS */
69
70#define DAEMON_NAME "mmpop3d"
1099fea4 71#define DAEMON_VERSION "mmmail-0.3.2/mmondor"
47071c2b
MM
72
73/* Negative states are used by the state swapper, others are real states */
74#define STATE_ERROR -3
75#define STATE_END -2
76#define STATE_CURRENT -1
77#define STATE_AUTH 0
78#define STATE_MAIN 1
79
80/* Asynchroneous functions we attach */
8a21b7a2 81#define ASYNC_CHECKPW 1
47071c2b 82
e334174e 83/* Error registration macro */
46173fd8
MM
84#define REGISTER_ERROR(x) do { \
85 (x)->errors++; \
86 if (CONF.DELAY_ON_ERROR) \
87 pthread_sleep((x)->errors); \
88} while (/* CONSTCOND */0)
e334174e 89
47071c2b
MM
90
91
92
93/* STRUCTURES */
94
95/* We store config file read results in this structure */
96typedef struct config {
7fd2c069 97 char LOCK_PATH[256], CHROOT_DIR[256], PID_PATH[256], USER[32], GROUPS[256],
46173fd8
MM
98 LOG_FACILITY[32], LISTEN_IPS[1024], SERVER_NAMES[1024], DB_INFO[1024],
99 MAIL_DIR[256];
47071c2b
MM
100 long ALLOC_BUFFERS, LOG_LEVEL, LISTEN_PORT, MAX_ERRORS, MAX_IPS,
101 MAX_PER_IP, CONNECTION_RATE, CONNECTION_PERIOD, INPUT_TIMEOUT,
102 BANDWIDTH_IN, BANDWIDTH_OUT, GBANDWIDTH_IN, GBANDWIDTH_OUT,
103 ASYNC_PROCESSES;
1aa70b4f 104 bool RESOLVE_HOSTS, DELAY_ON_ERROR, STATFAIL_LOGIN, STATFAIL_PASSWORD;
47071c2b
MM
105} CONFIG;
106
107/* For the messages list during the session */
108typedef struct msgnode {
497d62ac
MM
109 char id[24]; /* ID in the database */
110 char file[256]; /* Fullpath to message file */
111 char h_from[64], h_to[64], h_subject[64]; /* Headers preview */
112 long size; /* Message size in bytes */
113 bool retreived, deleted, read; /* Flags */
47071c2b
MM
114} msgnode;
115
6c40aa27
MM
116/* For a loaded message body */
117typedef struct msgdata {
118 void *internal;
119 char *body;
120 size_t size;
121} msgdata;
122
123/* A user environment, one copy per connected client exists */
47071c2b 124typedef struct clientenv {
7a56f31f 125 pnode_t node;
47071c2b
MM
126 fdbuf *fdb; /* Buffered handler around our fd */
127 char *buffer; /* Buffer that points to last command line */
128 char *c_hostname; /* Pointer to client's hostname */
129 char *c_ipaddr; /* Pointer to client's IP address string */
130 char *mailbox; /* Mailbox, this is if USER was specified */
131 msgnode *index; /* Available messages with flags */
47071c2b
MM
132 long size; /* Size of total messages in index */
133 long newsize; /* Current size of undeleted messages */
5eb34fba
MM
134 long messages; /* Number of messages, to know index size */
135 long newmessages; /* Current number of messages after deleted */
47071c2b
MM
136 long last; /* Last message read in current session */
137 long errors; /* Total number of errors that occured */
138 int timeout; /* Timeout in ms */
139 unsigned long id; /* Our connection ID */
140 unsigned long retreived, deleted; /* Number of msgs retrieved & deleted */
141 bool login; /* For all_quit() */
142 struct iface *iface; /* Current interface user connected through */
143 struct async_clenv *aclenv; /* Thread context for async_call() */
46173fd8
MM
144 mmstat_t vstat, pstat; /* Persistent mmstat(3) handles */
145 PGconn *pgconn; /* Persistent db connection */
47071c2b
MM
146} clientenv;
147
5eb34fba
MM
148/* Used for mmfd thread support delegation/abstraction */
149struct mutexnode {
7a56f31f 150 pnode_t node;
3dd832e3 151 pthread_mutex_t mutex;
5eb34fba
MM
152};
153
47071c2b
MM
154/* This defines a state */
155typedef struct state {
156 int (**functions)(clientenv *);
157 int errcode;
158 char *errtext;
159} state;
160
161/* A command of a state */
162typedef struct command {
47071c2b
MM
163 int loglevel;
164 char *name;
165} command;
166
399db776
MM
167/* Used for fast command lookup */
168struct commandnode {
169 hashnode_t node;
b232bd02 170 uint32_t hash;
399db776
MM
171 int index;
172 struct command *command;
173};
174
47071c2b 175/* Used by our asynchoneous functions */
8a21b7a2 176struct async_checkpw_msg {
47071c2b
MM
177 struct async_msg msg;
178 union {
8a21b7a2
MM
179 struct {
180 bool matching;
47071c2b
MM
181 } res;
182 struct {
8a21b7a2 183 char hash[48];
47071c2b
MM
184 char passwd[256];
185 } args;
186 } un;
187};
188
189
190
191
192/* PROTOTYPES */
193
194int main(int, char **);
195
196static int all_noop(clientenv *);
197static int all_quit(clientenv *);
198static int all_beer(clientenv *);
199static int auth_user(clientenv *);
200static int auth_pass(clientenv *);
201static int main_rset(clientenv *);
202static int main_stat(clientenv *);
203static int main_list(clientenv *);
204static int main_retr(clientenv *);
205static int main_dele(clientenv *);
206static int main_last(clientenv *);
207static int main_top(clientenv *);
2c55d0fe 208static int main_uidl(clientenv *);
497d62ac 209static int main_head(clientenv *);
47071c2b
MM
210static int main_page(clientenv *);
211
7fd2c069 212static int lock_check(const char *);
399db776 213static bool hash_commands(struct command *, size_t);
b232bd02 214static uint32_t commandnode_keyhash(const void *, size_t);
399db776 215static int commandnode_keycmp(const void *, const void *, size_t);
47071c2b 216static bool do_buildindex(clientenv *);
6c40aa27
MM
217static bool do_message_load(msgdata *, msgnode *);
218static void do_message_free(msgdata *);
47071c2b 219static bool do_retreive(fdbuf *, msgnode *);
5eb34fba
MM
220static bool do_top(fdbuf *, msgnode *, long);
221static bool do_page(fdbuf *, msgnode *, long, long);
47071c2b
MM
222static bool do_update(clientenv *);
223static bool msgwrite(fdbuf *, const void *, size_t);
224static bool reply(fdbuf *, bool, const char *, ...);
46173fd8
MM
225static bool clenv_constructor(pnode_t *);
226static void clenv_destructor(pnode_t *);
52122f72
MM
227static void *utdata_constructor(void);
228static void utdata_destructor(void *);
47071c2b
MM
229static clientenv *alloc_clientenv(void);
230static bool init_clientenv(clientenv *);
231static clientenv *free_clientenv(clientenv *);
232static bool valid_address(char *, char *);
233static bool valid_host(char *);
e89b4e26 234inline static bool valid_char(char);
47071c2b
MM
235
236static int handleclient(unsigned long, int, clientlistnode *, struct iface *,
52122f72 237 struct async_clenv *, void *);
47071c2b 238
3dd832e3
MM
239static void thread_init(void);
240static void *thread_mutex_create(void);
241static void *thread_mutex_destroy(void *);
242static void thread_mutex_lock(void *);
243static void thread_mutex_unlock(void *);
244static bool thread_eintr(void);
5eb34fba 245
8a21b7a2
MM
246static void async_checkpw(struct async_msg *);
247static bool checkpw(clientenv *, const char *, const char *);
47071c2b
MM
248
249
250
251
252#endif