1 /*
2  * jabberd - Jabber Open Source Server
3  * Copyright (c) 2002-2007 Jeremie Miller, Thomas Muldowney,
4  *                         Ryan Eatmon, Robert Norris, Tomasz Sterna
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA02111-1307USA
19  */
20 
21 #ifndef INCL_SX_PLUGINS_H
22 #define INCL_SX_PLUGINS_H
23 
24 /** sx stream flags */
25 #define SX_SSL_WRAPPER          (1<<0)    /** SSL wrapper on legacy 5223 port */
26 #define SX_SSL_STARTTLS_OFFER   (1<<1)    /** don't offer starttls without this */
27 #define SX_SSL_STARTTLS_REQUIRE (1<<2)    /** starttls is required on the stream */
28 
29 #define SX_SASL_OFFER           (1<<3)    /** don't offer sasl without this */
30 
31 #define SX_COMPRESS_WRAPPER     (1<<4)
32 #define SX_COMPRESS_OFFER       (1<<5)
33 
34 #define SX_WEBSOCKET_WRAPPER    (1<<6)    /** indicates stream over WebSocket connection */
35 
36 /** magic numbers, so plugins can find each other */
37 #define SX_SSL_MAGIC        (0x01)
38 
39 
40 /** error codes */
41 /* prefix 0x0. is taken by sx core errors in sx.h */
42 #define SX_ERR_SSL              (0x010)
43 #define SX_ERR_STARTTLS_FAILURE (0x011)
44 
45 #define SX_ERR_COMPRESS         (0x020)
46 #define SX_ERR_COMPRESS_FAILURE (0x021)
47 
48 
49 #define SX_CONN_EXTERNAL_ID_MAX_COUNT 8
50 
51 #ifdef __cplusplus
52 extern "C" {
53 #endif
54 
55 
56 /* SSL plugin */
57 #ifdef HAVE_SSL
58 
59 #include <openssl/md5.h>
60 #include <openssl/ssl.h>
61 #include <openssl/err.h>
62 #include <openssl/x509v3.h>
63 
64 
65 /** init function */
66 JABBERD2_API int                         sx_ssl_init(sx_env_t env, sx_plugin_t p, va_list args);
67 
68 /** add cert function */
69 JABBERD2_API int                         sx_ssl_server_addcert(sx_plugin_t p, const char *name, const char *pemfile, const char *cachain, int mode, const char *private_key_password, const char *ciphers);
70 
71 /** trigger for client starttls */
72 JABBERD2_API int                         sx_ssl_client_starttls(sx_plugin_t p, sx_t s, const char *pemfile, const char *private_key_password);
73 
74 /* previous states */
75 #define SX_SSL_STATE_NONE       (0)
76 #define SX_SSL_STATE_WANT_READ  (1)
77 #define SX_SSL_STATE_WANT_WRITE (2)
78 #define SX_SSL_STATE_ERROR      (3)
79 
80 /** a single conn */
81 typedef struct _sx_ssl_conn_st {
82     /* id and ssf for sasl external auth */
83     char        *external_id[SX_CONN_EXTERNAL_ID_MAX_COUNT];
84 
85     SSL         *ssl;
86 
87     BIO         *wbio, *rbio;
88 
89     jqueue_t    wq;
90 
91     int         last_state;
92 
93     char        *pemfile;
94 
95     char        *private_key_password;
96 } *_sx_ssl_conn_t;
97 
98 #endif /* HAVE_SSL */
99 
100 
101 /* SASL plugin */
102 
103 /** init function */
104 JABBERD2_API int                         sx_sasl_init(sx_env_t env, sx_plugin_t p, va_list args);
105 
106 /** the callback function */
107 typedef int                 (*sx_sasl_callback_t)(int cb, void *arg, void **res, sx_t s, void *cbarg);
108 
109 /* callbacks */
110 #define sx_sasl_cb_GET_REALM        (0x00)
111 #define sx_sasl_cb_GET_PASS         (0x01)
112 #define sx_sasl_cb_CHECK_PASS       (0x02)
113 #define sx_sasl_cb_CHECK_AUTHZID    (0x03)
114 #define sx_sasl_cb_GEN_AUTHZID      (0x04)
115 #define sx_sasl_cb_CHECK_MECH       (0x05)
116 
117 /* error codes */
118 #define sx_sasl_ret_OK		    (0)
119 #define sx_sasl_ret_FAIL	    (1)
120 
121 /** trigger for client auth */
122 JABBERD2_API int                         sx_sasl_auth(sx_plugin_t p, sx_t s, const char *appname, const char *mech, const char *user, const char *pass);
123 
124 /* for passing auth data to callback */
125 typedef struct sx_sasl_creds_st {
126     const char                  *authnid;
127     const char                  *realm;
128     const char                  *authzid;
129     const char                  *pass;
130 } *sx_sasl_creds_t;
131 
132 
133 /* Stream Compression plugin */
134 #ifdef HAVE_LIBZ
135 
136 #include <zlib.h>
137 
138 /** init function */
139 JABBERD2_API int                         sx_compress_init(sx_env_t env, sx_plugin_t p, va_list args);
140 
141 /* allocation chunk for decompression */
142 #define SX_COMPRESS_CHUNK       16384
143 
144 /** a single conn */
145 typedef struct _sx_compress_conn_st {
146     /* zlib streams for deflate() and inflate() */
147     z_stream    wstrm, rstrm;
148 
149     /* buffers for compressed and decompressed data */
150     sx_buf_t    wbuf, rbuf;
151 
152 } *_sx_compress_conn_t;
153 
154 #endif /* HAVE_LIBZ */
155 
156 
157 /* Stanza Acknowledgements plugin */
158 /** init function */
159 JABBERD2_API int sx_ack_init(sx_env_t env, sx_plugin_t p, va_list args);
160 
161 /* websocket wrapper plugin */
162 #ifdef USE_WEBSOCKET
163 #include <http_parser.h>
164 #include <util/util.h>
165 
166 JABBERD2_API int sx_websocket_init(sx_env_t env, sx_plugin_t p, va_list args);
167 
168 /** websocket state */
169 typedef enum {
170     websocket_PRE,
171     websocket_HEADERS,      /* parsing HTTP headers */
172     websocket_ACTIVE,       /* active websocket connection */
173     websocket_CLOSING       /* shutdown in progress */
174 } _sx_websocket_state_t;
175 
176 /** a single conn */
177 typedef struct _sx_websocket_conn_st {
178     http_parser             parser;
179     _sx_websocket_state_t   state;
180     int                     header_value;
181     pool_t                  p;
182     spool                   field, value;
183     xht                     headers;
184     void                    *frame;
185     unsigned int            opcode;
186     char                    *buf;
187     size_t                  buf_len;
188 } *_sx_websocket_conn_t;
189 #endif
190 
191 #ifdef __cplusplus
192 }
193 #endif
194 
195 
196 #endif /* INCL_SX_PLUGINS_H */
197