|
jabberd14 1.6.2
|
00001 /* 00002 * Copyrights 00003 * 00004 * Portions created by or assigned to Jabber.com, Inc. are 00005 * Copyright (c) 1999-2002 Jabber.com, Inc. All Rights Reserved. Contact 00006 * information for Jabber.com, Inc. is available at http://www.jabber.com/. 00007 * 00008 * Portions Copyright (c) 1998-1999 Jeremie Miller. 00009 * 00010 * Portions Copyright (c) 2006-2007 Matthias Wimmer 00011 * 00012 * This file is part of jabberd14. 00013 * 00014 * This software is free software; you can redistribute it and/or 00015 * modify it under the terms of the GNU General Public License as 00016 * published by the Free Software Foundation; either version 2 of the 00017 * License, or (at your option) any later version. 00018 * 00019 * This software is distributed in the hope that it will be useful, but 00020 * WITHOUT ANY WARRANTY; without even the implied warranty of 00021 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00022 * General Public License for more details. 00023 * 00024 * You should have received a copy of the GNU General Public License 00025 * along with this software; if not, write to the Free Software 00026 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 00027 * 02110-1301, USA. 00028 * 00029 */ 00030 00099 #ifdef HAVE_CONFIG_H 00100 # include <config.h> 00101 #endif 00102 00103 #include <jabberdlib.h> 00104 #include <gnutls/gnutls.h> 00105 #include <gnutls/x509.h> 00106 00107 #ifdef HAVE_GNUTLS_EXTRA 00108 # include <gnutls/extra.h> 00109 # include <gnutls/openpgp.h> 00110 #endif 00111 00113 typedef enum { p_NONE, p_NORM, p_XDB, p_LOG, p_ROUTE } ptype; 00114 00120 typedef enum { 00121 o_PRECOND, 00122 o_COND, 00123 o_PREDELIVER, 00124 o_DELIVER 00125 } order; 00126 00134 typedef enum { 00135 r_UNREG, 00136 r_NONE, 00137 r_PASS, 00138 r_LAST, 00139 r_ERR, 00140 r_DONE 00141 } result; 00142 00143 typedef struct instance_struct *instance, _instance; 00144 00146 typedef struct dpacket_struct 00147 { 00148 char *host; 00149 jid id; 00150 jid from_jid; 00151 jid to_jid; 00152 ptype type; 00153 pool p; 00154 xmlnode x; 00155 } *dpacket, _dpacket; 00156 00158 typedef result (*phandler)(instance id, dpacket p, void *arg); 00159 00161 typedef struct handel_struct 00162 { 00163 pool p; 00164 phandler f; 00165 void *arg; 00166 order o; 00167 struct handel_struct *next; 00168 } *handel, _handel; 00169 00171 typedef void (*register_notify)(instance i, const char *destination, int is_register, void *arg); 00172 00174 typedef struct register_notifier_struct { 00175 register_notify callback; 00176 void *arg; 00177 struct register_notifier_struct *next; 00178 } *register_notifier, _register_notifier; 00179 00181 struct instance_struct 00182 { 00183 char *id; 00184 pool p; 00185 xmlnode x; 00186 ptype type; 00187 handel hds; 00188 register_notifier routing_update_callbacks; 00189 std::map<std::string, void*>* module_init_funcs; 00190 std::set<Glib::ustring>* static_hosts; 00191 }; 00192 00194 typedef result (*cfhandler)(instance id, xmlnode x, void *arg); 00195 00197 typedef result (*beathandler)(void *arg); 00198 00199 /*** public functions for base modules ***/ 00200 void register_config(pool p, char const* node, cfhandler f, void *arg); /* register a function to handle that node in the config file */ 00201 void register_beat(int freq, beathandler f, void *arg); /* register the function to be called from the heartbeat, freq is how often, <= 0 is ignored */ 00202 typedef void(*shutdown_func)(void*arg); 00203 void register_shutdown(shutdown_func f,void *arg); /* register to be notified when the server is shutting down */ 00204 00205 // functions in deliver.cc 00206 void register_instance(instance i, char const* host); /* associate an id with a hostname for that packet type */ 00207 void unregister_instance(instance i, char const* host); /* disassociate an id with a hostname for that packet type */ 00208 void register_routing_update_callback(instance i, register_notify f, void *arg); 00209 void register_phandler(instance id, order o, phandler f, void *arg); /* register a function to handle delivery for this instance */ 00210 dpacket dpacket_new(xmlnode x); /* create a new delivery packet from source xml */ 00211 dpacket dpacket_copy(dpacket p); /* copy a packet (and it's flags) */ 00212 void deliver(dpacket p, instance i); /* deliver packet from sending instance */ 00213 void deliver_fail(dpacket p, const char *err); /* bounce a packet intelligently */ 00214 // void deliver_instance(instance i, dpacket p); /* deliver packet TO the instance, if the result != r_DONE, you have to handle the packet! */ 00215 bool deliver_is_delivered_to(Glib::ustring const& host, _instance const* i); /* util that returns the instance handling this hostname for normal packets */ 00216 bool deliver_is_uplink(instance i); // checks if an instance is configured to be the uplink 00217 std::set<Glib::ustring> deliver_routed_hosts(ptype type, instance i); 00218 void deliver_config_filter(xmlnode greymatter); 00219 00220 /*** global logging/signal symbols ***/ 00221 #define LOGT_LEGACY 1 00222 #define LOGT_DELIVER 2 00223 #define LOGT_REGISTER 4 00224 #define LOGT_STATUS 8 00225 #define LOGT_EVENT 16 00226 #define LOGT_CONFIG 32 00227 #define LOGT_DYNAMIC 64 00228 #define LOGT_IO 128 00229 #define LOGT_INIT 256 00230 #define LOGT_EXECFLOW 512 00231 #define LOGT_CLEANUP 1024 00232 #define LOGT_STRANGE 2048 00233 #define LOGT_XML 4096 00234 #define LOGT_THREAD 8192 00235 #define LOGT_STORAGE 16384 00236 #define LOGT_AUTH 32768 00237 #define LOGT_SESSION 65536 00238 #define LOGT_ROSTER 131072 00239 #define LOGT_BYTES 262144 00240 00241 int log_get_facility(char const* facility); 00242 int log_get_level(char const* level); 00243 #define MAX_LOG_SIZE 1024 00244 extern int debug_flag; 00245 inline int get_debug_flag(void); 00246 void set_debug_flag(int v); 00247 void set_cmdline_debug_flag(int v); 00248 void set_debug_facility(int facility); 00249 #ifdef __CYGWIN__ 00250 #define log_debug if(get_debug_flag()&1) debug_log 00251 #define log_debug2 if(get_debug_flag()) debug_log2 00252 #else 00253 #define log_debug if(debug_flag&1) debug_log 00254 #define log_debug2 if(debug_flag) debug_log2 00255 #endif 00256 void debug_log(char const* zone, char const* msgfmt, ...); 00257 void debug_log2(char const* zone, int type, char const* msgfmt, ...); 00258 void log_notice(char const* host, char const* msgfmt, ...); 00259 void log_warn(char const* host, char const* msgfmt, ...); 00260 void log_alert(char const* host, char const* msgfmt, ...); 00261 #define log_error log_alert 00262 void logger(char const* type, char const* host, char const* message); /* actually creates and delivers the log message */ 00263 void log_record(char const* id, char const* type, char const* action, char const* msgfmt, ...); /* for generic logging support, like log_record("jer@jabber.org","session","end","...") */ 00264 void log_generic(char const* logtype, char const* id, char const* type, char const* action, char const* msgfmt, ...); 00265 00266 /*** xdb utilities ***/ 00267 00269 typedef struct xdbcache_struct { 00270 instance i; 00271 int id; 00272 const char *ns; 00273 int set; 00274 char const* act; 00275 char const* match; 00276 char const* matchpath; 00277 xht namespaces; 00278 xmlnode data; 00279 jid owner; 00280 int sent; 00281 int preblock; 00282 pth_cond_t cond; 00283 pth_mutex_t mutex; 00284 struct xdbcache_struct *prev; 00285 struct xdbcache_struct *next; 00286 } *xdbcache, _xdbcache; 00287 00288 xdbcache xdb_cache(instance i); 00289 xmlnode xdb_get(xdbcache xc, jid owner, const char *ns); 00290 int xdb_act(xdbcache xc, jid owner, const char *ns, char *act, char const* match, xmlnode data); 00291 int xdb_act_path(xdbcache xc, jid owner, const char *ns, char const *act, char const* matchpath, xht namespaces, xmlnode data); 00292 int xdb_set(xdbcache xc, jid owner, const char *ns, xmlnode data); 00294 /* Error messages */ 00295 #define SERROR_NAMESPACE "<stream:error><invalid-namespace xmlns='urn:ietf:params:xml:ns:xmpp-streams'/><text xmlns='urn:ietf:params:xml:ns:xmpp-streams' xml:lang='en'>Invalid namespace specified.</text></stream:error>" 00296 #define SERROR_INVALIDHOST "<stream:error><invalid-from xmlns='urn:ietf:params:xml:ns:xmpp-streams'/><text xmlns='urn:ietf:params:xml:ns:xmpp-streams' xml:lang='en'>Invalid hostname used.</text></stream:error>" 00297 00298 /* ------------------------------------ 00299 * Managed Thread Queue (MTQ) utilities 00300 * ------------------------------------*/ 00301 00302 /* default waiting threads */ 00303 #define MTQ_THREADS 10 00304 00306 typedef void (*mtq_callback)(void *arg); 00307 00309 typedef struct mtqueue_struct 00310 { 00311 struct mth_struct *t; 00312 pth_msgport_t mp; 00313 int routed; 00314 } *mtq, _mtq; 00315 00317 typedef struct mth_struct 00318 { 00319 mtq q; 00320 pth_msgport_t mp; 00321 pool p; 00322 pth_t id; 00323 int busy; 00324 } *mth, _mth; 00325 00326 mtq mtq_new(pool p); 00328 void mtq_send(mtq q, pool p, mtq_callback f, void *arg); 00330 /* MIO - Managed I/O - TCP functions */ 00331 00333 typedef enum { queue_XMLNODE, queue_CDATA } mio_queue_type; 00334 typedef struct mio_wb_q_st 00335 { 00336 pth_message_t head; /* for compatibility */ 00337 pool p; 00338 mio_queue_type type; 00339 xmlnode x; 00340 void *data; 00341 void *cur; 00342 int len; 00343 struct mio_wb_q_st *next; 00344 } _mio_wbq,*mio_wbq; 00345 00346 struct mio_handlers_st; 00347 00348 /* the mio data type */ 00349 typedef enum { state_ACTIVE, state_CLOSE } mio_state; 00350 typedef enum { type_LISTEN, type_NORMAL, type_NUL, type_HTTP } mio_type; 00351 00352 /* standard i/o callback function definition */ 00353 struct mio_st; 00354 typedef void (*mio_std_cb)(mio_st* m, int state, void *arg, xmlnode x, char *buffer, int bufsz); 00355 00359 typedef struct mio_st { 00360 pool p; 00361 int fd; 00362 mio_type type; 00363 mio_state state; 00365 mio_wbq queue; 00366 mio_wbq tail; 00368 struct mio_st *prev,*next; 00370 void *cb_arg; 00371 mio_std_cb cb; 00372 struct mio_handlers_st *mh; 00374 xstream xs; /* XXX kill me, I suck */ 00375 XML_Parser parser; 00376 xmlnode stacknode; 00377 void *ssl; 00378 struct { 00379 int root:1; 00380 int rated:1; 00381 int reset_stream:1; 00382 int recall_read_when_readable:1; 00383 int recall_read_when_writeable:1; 00384 int recall_write_when_readable:1; 00385 int recall_write_when_writeable:1; 00386 int recall_handshake_when_readable:1; 00387 int recall_handshake_when_writeable:1; 00388 } flags; 00389 00390 struct karma k; 00391 jlimit rate; 00392 char *peer_ip; 00393 uint16_t peer_port; 00394 char *our_ip; 00395 uint16_t our_port; 00396 char *connect_errmsg; 00397 char *authed_other_side; 00399 xmppd::ns_decl_list* out_ns; 00400 xmppd::ns_decl_list* in_root; 00401 xmppd::ns_decl_list* in_stanza; 00402 const char *root_lang; 00403 } *mio, _mio; 00404 00410 typedef struct mio_main_st { 00411 pool p; 00412 mio master__list; 00413 pth_t t; 00414 int shutdown; 00415 int zzz[2]; 00416 int zzz_active; 00417 struct karma *k; 00418 int rate_t, rate_p; 00419 char const* bounce_uri; 00420 char const* webserver_path; 00421 char const* flash_policy; 00423 } _ios,*ios; 00424 00425 /* MIO SOCKET HANDLERS */ 00426 typedef ssize_t (*mio_read_func) (mio m, void* buf, size_t count); 00427 typedef ssize_t (*mio_write_func) (mio m, void const* buf, size_t count); 00428 typedef void (*mio_parser_func) (mio m, void const* buf, size_t bufsz); 00429 typedef int (*mio_accepted_func) (mio m); 00430 typedef int (*mio_handshake_func) (mio m); 00431 typedef void (*mio_close_func) (mio m, bool close_read); 00432 00434 typedef struct mio_handlers_st { 00435 pool p; 00436 mio_read_func read; 00437 mio_write_func write; 00438 mio_accepted_func accepted; 00439 mio_parser_func parser; 00440 mio_handshake_func handshake; 00441 mio_close_func close; 00442 } _mio_handlers, *mio_handlers; 00443 00444 /* standard read/write/accept/connect functions */ 00445 ssize_t _mio_raw_read(mio m, void *buf, size_t count); 00446 ssize_t _mio_raw_write(mio m, void *buf, size_t count); 00447 void _mio_raw_parser(mio m, const void *buf, size_t bufsz); 00448 #define MIO_RAW_READ (mio_read_func)&_mio_raw_read 00449 #define MIO_RAW_WRITE (mio_write_func)&_mio_raw_write 00450 #define MIO_RAW_ACCEPTED (mio_accepted_func)NULL 00451 #define MIO_RAW_PARSER (mio_parser_func)&_mio_raw_parser 00452 00453 void mio_xml_reset(mio m); 00454 int mio_xml_starttls(mio m, int originator, const char *identity); 00455 void _mio_xml_parser(mio m, const void *buf, size_t bufsz); 00456 #define MIO_XML_PARSER (mio_parser_func)&_mio_xml_parser 00457 00458 /* function helpers */ 00459 #define MIO_LISTEN_RAW mio_handlers_new(NULL, NULL, NULL) 00460 #define MIO_CONNECT_RAW mio_handlers_new(NULL, NULL, NULL) 00461 #define MIO_LISTEN_XML mio_handlers_new(NULL, NULL, MIO_XML_PARSER) 00462 #define MIO_CONNECT_XML mio_handlers_new(NULL, NULL, MIO_XML_PARSER) 00463 00464 /* TLS functions */ 00465 void mio_ssl_init (xmlnode x); 00466 bool mio_tls_early_init(); 00467 int mio_ssl_starttls (mio m, int originator, const char* identity); 00468 int mio_ssl_starttls_possible (mio m, const char* identity); 00469 int mio_ssl_verify(mio m, const char *id_on_xmppAddr); 00470 ssize_t _mio_ssl_read (mio m, void *buf, size_t count); 00471 ssize_t _mio_ssl_write (mio m, const void* buf, size_t count); 00472 int _mio_ssl_accepted(mio m); 00473 void mio_tls_get_characteristics(mio m, char* buffer, size_t len); 00474 void mio_tls_get_certtype(mio m, char* buffer, size_t len); 00475 void mio_tls_get_compression(mio m, char* buffer, size_t len); 00476 #define MIO_SSL_READ _mio_ssl_read 00477 #define MIO_SSL_WRITE _mio_ssl_write 00478 #define MIO_SSL_ACCEPTED _mio_ssl_accepted 00479 00480 int mio_is_encrypted(mio m); 00481 00482 /* MIO handlers helper functions */ 00483 mio_handlers mio_handlers_new(mio_read_func rf, mio_write_func wf, mio_parser_func pf); 00484 void mio_handlers_free(mio_handlers mh); 00485 void mio_set_handlers(mio m, mio_handlers mh); 00486 00487 /* callback state flags */ 00488 #define MIO_NEW 0 00489 #define MIO_BUFFER 1 00490 #define MIO_XML_ROOT 2 00491 #define MIO_XML_NODE 3 00492 #define MIO_CLOSED 4 00493 #define MIO_ERROR 5 00494 00495 /* Initializes the MIO subsystem */ 00496 void mio_init(void); 00497 00498 /* Stops the MIO system */ 00499 void mio_stop(void); 00500 00501 /* Create a new mio object from a file descriptor */ 00502 mio mio_new(int fd, mio_std_cb cb, void *cb_arg, mio_handlers mh); 00503 00504 /* Reset the callback and argument for an mio object */ 00505 void mio_reset(mio m, mio_std_cb cb, void *arg); 00506 00507 /* Request the mio socket be closed */ 00508 void mio_close(mio m); 00509 00510 /* Writes an xmlnode to the socket */ 00511 void mio_write(mio m, xmlnode stanza, char const* buffer, int len); 00512 00513 /* write the root element to a mio stream */ 00514 void mio_write_root(mio m, xmlnode root, int stream_type); 00515 00516 /* Sets the karma values for a socket */ 00517 void mio_karma(mio m, int val, int max, int inc, int dec, int penalty, int restore); 00518 void mio_karma2(mio m, struct karma *k); 00519 00520 /* Sets connection based rate limits */ 00521 void mio_rate(mio m, int rate_time, int max_points); 00522 00523 /* Pops the next xmlnode from the queue, or NULL if no more nodes */ 00524 xmlnode mio_cleanup(mio m); 00525 00526 /* Connects to an ip */ 00527 void mio_connect(char *host, int port, mio_std_cb cb, void *cb_arg, int timeout, mio_handlers mh); 00528 00529 /* Starts listening on a port/ip, returns NULL if failed to listen */ 00530 mio mio_listen(int port, char const* sourceip, mio_std_cb cb, void *cb_arg, mio_handlers mh); 00531 00532 int _mio_write_dump(mio m); 00533 00534 /* some nice api utilities */ 00535 #define mio_pool(m) (m->p) 00536 #define mio_ip(m) (m ? m->peer_ip : NULL) 00537 #define mio_connect_errmsg(m) (m->connect_errmsg) 00538 00539 // where to bounce a HTTP GET request to if not otherwise configured 00540 #define HTTP_BOUNCE_URI "http://jabberd.org/get-bounce" 00541 00542 /*----------------- 00543 * Access controll 00544 *-----------------*/ 00545 00546 int acl_check_access(xdbcache xdb, const char *function, const jid user); 00547 jid acl_get_users(xdbcache xdb, const char *function); 00548 00549 namespace xmppd { 00550 00551 /* ******************** Logging ******************** */ 00552 00553 /* forward declaration */ 00554 class logging; 00555 00559 enum loglevel { 00560 notice, 00561 warn, 00562 error, 00563 alert 00564 }; 00565 00571 class logmessage : public std::ostringstream { 00572 public: 00579 logmessage(logging& log_entity, loglevel level); 00580 00584 ~logmessage(); 00585 00592 std::ostream& operator<<(const char* text); 00593 00600 std::ostream& operator<<(const std::string& text); 00601 00605 logmessage(const logmessage& orig); 00606 00607 private: 00611 logging& log_entity; 00612 00616 loglevel level; 00617 }; 00618 00622 class logging { 00623 public: 00629 logging(Glib::ustring ident); 00630 00634 ~logging(); 00635 00641 logmessage level(loglevel level_to_use); 00642 private: 00649 void write(loglevel level_to_use, Glib::ustring log_message); 00650 00654 friend class logmessage; 00655 00659 Glib::ustring identity; 00660 }; 00661 00662 /*-------------------------- 00663 * OOP-instances base class 00664 *--------------------------*/ 00665 00669 class instance_base { 00670 public: 00677 instance_base(instance i, xmlnode x); 00678 00679 protected: 00688 virtual result on_packet(dpacket dp); 00689 00698 virtual result on_stanza_packet(dpacket dp); 00699 00708 virtual result on_xdb_packet(dpacket dp); 00709 00718 virtual result on_log_packet(dpacket dp); 00719 00728 virtual result on_route_packet(dpacket dp); 00729 00738 virtual result on_message_stanza(jpacket p); 00739 00750 virtual result on_presence_stanza(jpacket p); 00751 00760 virtual result on_iq_stanza(jpacket p); 00761 00770 virtual result on_subscription_stanza(jpacket p); 00771 00775 virtual void on_heartbeat(); 00776 00784 void set_heartbeat_interval(int interval); 00785 00791 void deliver(dpacket p); 00792 00798 void deliver(xmlnode x); 00799 00806 void deliver_fail(dpacket p, const std::string& reason_text); 00807 00818 void bounce_stanza(xmlnode x, xterror xterr); 00819 00825 std::string get_instance_id(); 00826 00833 logmessage log(loglevel level); 00834 00840 xmlnode get_instance_config(); 00841 00842 private: 00846 instance i; 00847 00851 pointer<logging> logger; 00852 00858 instance_base(instance_base& ref); 00859 00863 result beathandler_wrapper(); 00864 00872 static result phandler_helper(instance id, dpacket p, void* arg); 00873 00879 static result beathandler_helper(void* arg); 00880 00888 int current_heartbeat_frequency; 00889 00897 int requested_heartbeat_frequency; 00898 }; 00899 }
1.7.4