pop3d.h
changeset 4 cb3d0d99955c
child 5 9e370734cb36
equal deleted inserted replaced
-1:000000000000 4:cb3d0d99955c
       
     1 /*
       
     2  * Copyright (c) 2014 Sunil Nimmagadda <sunil@nimmagadda.net>
       
     3  *
       
     4  * Permission to use, copy, modify, and distribute this software for any
       
     5  * purpose with or without fee is hereby granted, provided that the above
       
     6  * copyright notice and this permission notice appear in all copies.
       
     7  *
       
     8  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
       
     9  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
       
    10  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
       
    11  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
       
    12  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
       
    13  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
       
    14  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
       
    15  */
       
    16 
       
    17 #include <sys/tree.h>
       
    18 
       
    19 #include <sha1.h>
       
    20 
       
    21 #include "imsgev.h"
       
    22 #include "iobuf.h"
       
    23 #include "ioev.h"
       
    24 
       
    25 #define	ARGLEN		40
       
    26 #define POP3S		0x01
       
    27 #define	F_DELE		0x01
       
    28 
       
    29 struct passwd;
       
    30 
       
    31 enum imsg_type {
       
    32 	IMSG_AUTH,
       
    33 	IMSG_MAILDROP_INIT,
       
    34 	IMSG_MAILDROP_RETR,
       
    35 	IMSG_MAILDROP_DELE,
       
    36 	IMSG_MAILDROP_RSET,
       
    37 	IMSG_MAILDROP_LIST,
       
    38 	IMSG_MAILDROP_LISTALL,
       
    39 	IMSG_MAILDROP_UIDLALL,
       
    40 	IMSG_MAILDROP_UPDATE
       
    41 };
       
    42 
       
    43 enum m_type {
       
    44 	M_MBOX,
       
    45 	M_MAILDIR
       
    46 };
       
    47 
       
    48 struct msg {
       
    49 	union {
       
    50 		SIMPLEQ_ENTRY(msg)	q_entry;
       
    51 		RB_ENTRY(msg)		t_entry;
       
    52 	}				e;
       
    53 	char				hash[SHA1_DIGEST_STRING_LENGTH];
       
    54 	size_t				sz;
       
    55 	size_t				nlines;
       
    56 	union {
       
    57 		long			offset;
       
    58 		const char		*fname;
       
    59 	}				u;
       
    60 	int				flags;
       
    61 };
       
    62 
       
    63 struct mdrop {
       
    64 	union {
       
    65 		SIMPLEQ_HEAD(, msg)	q_msgs;
       
    66 		RB_HEAD(msgtree, msg)	t_msgs;
       
    67 	}				e;
       
    68 	size_t				nmsgs;
       
    69 	size_t				sz;
       
    70 	struct msg			**msgs_index; /* random access to msgs */
       
    71 	int				fd;
       
    72 };
       
    73 
       
    74 struct stats {
       
    75 	size_t	nmsgs;
       
    76 	size_t	sz;
       
    77 };
       
    78 
       
    79 struct retr_req {
       
    80 	unsigned int	idx;
       
    81 	unsigned int	ntop;
       
    82 	int		top;
       
    83 };
       
    84 
       
    85 struct retr_res {
       
    86 	size_t		nlines;
       
    87 	long		offset;
       
    88 	unsigned int	ntop;
       
    89 	int		top;
       
    90 };
       
    91 
       
    92 struct list_req {
       
    93 	unsigned int	idx;
       
    94 	int		uidl;
       
    95 };
       
    96 
       
    97 struct list_res {
       
    98 	unsigned int	idx;
       
    99 	union {
       
   100 		size_t	sz;
       
   101 		char	hash[SHA1_DIGEST_STRING_LENGTH];
       
   102 	}		u;
       
   103 	int		uidl;
       
   104 };
       
   105 
       
   106 struct m_backend {
       
   107 	int (*init)(struct mdrop *, size_t *, size_t *);
       
   108 	int (*retr)(struct mdrop *, unsigned int, size_t *, size_t *);
       
   109 	int (*update)(struct mdrop *);
       
   110 };
       
   111 
       
   112 struct auth_req {
       
   113 	char	user[ARGLEN];
       
   114 	char	pass[ARGLEN];
       
   115 };
       
   116 
       
   117 struct listener {
       
   118 	struct sockaddr_storage	ss;
       
   119 	struct event		ev;
       
   120 	struct event		pause;
       
   121 	int			flags;
       
   122 	int			sock;
       
   123 };
       
   124 
       
   125 enum state {
       
   126 	AUTH,
       
   127 	TRANSACTION,
       
   128 	UPDATE
       
   129 };
       
   130 
       
   131 struct session {
       
   132 	SPLAY_ENTRY(session)	entry;
       
   133 	struct imsgev		iev_maildrop;
       
   134 	struct iobuf		iobuf;
       
   135 	struct io		io;
       
   136 	char			user[ARGLEN];
       
   137 	char			pass[ARGLEN];
       
   138 	size_t			m_sz;
       
   139 	size_t			nmsgs;
       
   140 	struct listener		*l;
       
   141 	uint32_t		id;
       
   142 	int			sock;
       
   143 	int			flags;
       
   144 	enum state		state;
       
   145 };
       
   146 
       
   147 /* pop3e.c */
       
   148 pid_t pop3_main(int [2], struct passwd *);
       
   149 
       
   150 /* session.c */
       
   151 void session_init(struct listener *, int);
       
   152 void session_close(struct session *, int);
       
   153 void session_reply(struct session *, char *, ...);
       
   154 void session_set_state(struct session *, enum state);
       
   155 void session_imsgev_init(struct session *, int);
       
   156 SPLAY_HEAD(session_tree, session);
       
   157 int session_cmp(struct session *, struct session *);
       
   158 SPLAY_PROTOTYPE(session_tree, session, entry, session_cmp);
       
   159 
       
   160 /* maildrop.c */
       
   161 pid_t maildrop_init(uint32_t, int [2], struct passwd *, int, const char *);
       
   162 
       
   163 /* util.c */
       
   164 void set_nonblocking(int);
       
   165 void log_init(int);
       
   166 void logit(int, const char *, ...);
       
   167 void vlog(int, const char *, va_list);
       
   168 void fatal(const char *);
       
   169 void fatalx(const char *);
       
   170 void *xcalloc(size_t, size_t, const char *);
       
   171 void iobuf_xfqueue(struct iobuf *, const char *, const char *, ...);
       
   172 void iobuf_xqueue(struct iobuf *, const char *, const void *, size_t);
       
   173 int imsgev_xcompose(struct imsgev *, u_int16_t, u_int32_t,
       
   174     uint32_t, int, void *, u_int16_t, const char *);
       
   175 int get_index(struct session *, const char *, unsigned int *);
       
   176 void log_connect(uint32_t, struct sockaddr_storage *, socklen_t);