jabberd.h

Go to the documentation of this file.
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     ptype type;
00151     pool p;
00152     xmlnode x;
00153 } *dpacket, _dpacket;
00154 
00156 typedef result (*phandler)(instance id, dpacket p, void *arg);
00157 
00159 typedef struct handel_struct
00160 {
00161     pool p;
00162     phandler f;  
00163     void *arg;
00164     order o;     
00165     struct handel_struct *next;
00166 } *handel, _handel;
00167 
00169 typedef void (*register_notify)(instance i, const char *destination, int is_register, void *arg);
00170 
00172 typedef struct register_notifier_struct {
00173     register_notify callback;   
00174     void *arg;                  
00175     struct register_notifier_struct *next;      
00176 } *register_notifier, _register_notifier;
00177 
00179 struct instance_struct
00180 {
00181     char *id;   
00182     pool p;
00183     xmlnode x;  
00184     ptype type; 
00185     handel hds; 
00186     register_notifier routing_update_callbacks; 
00187     std::map<std::string, void*>* module_init_funcs;    
00188 };
00189 
00191 typedef result (*cfhandler)(instance id, xmlnode x, void *arg);
00192 
00194 typedef result (*beathandler)(void *arg);
00195 
00196 /*** public functions for base modules ***/
00197 void register_config(pool p, char *node, cfhandler f, void *arg); /* register a function to handle that node in the config file */
00198 void register_instance(instance i, char const* host); /* associate an id with a hostname for that packet type */
00199 void unregister_instance(instance i, char const* host); /* disassociate an id with a hostname for that packet type */
00200 void register_routing_update_callback(instance i, register_notify f, void *arg); 
00201 void register_phandler(instance id, order o, phandler f, void *arg); /* register a function to handle delivery for this instance */
00202 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 */
00203 typedef void(*shutdown_func)(void*arg);
00204 void register_shutdown(shutdown_func f,void *arg); /* register to be notified when the server is shutting down */
00205 
00206 dpacket dpacket_new(xmlnode x); /* create a new delivery packet from source xml */
00207 dpacket dpacket_copy(dpacket p); /* copy a packet (and it's flags) */
00208 void deliver(dpacket p, instance i); /* deliver packet from sending instance */
00209 void deliver_fail(dpacket p, const char *err); /* bounce a packet intelligently */
00210 void deliver_instance(instance i, dpacket p); /* deliver packet TO the instance, if the result != r_DONE, you have to handle the packet! */
00211 instance deliver_hostcheck(char const* host); /* util that returns the instance handling this hostname for normal packets */
00212 bool deliver_is_uplink(instance i); // checks if an instance is configured to be the uplink
00213 std::set<Glib::ustring> deliver_routed_hosts(ptype type, instance i);
00214 
00215 /*** global logging/signal symbols ***/
00216 #define LOGT_LEGACY 1
00217 #define LOGT_DELIVER 2
00218 #define LOGT_REGISTER 4
00219 #define LOGT_STATUS 8
00220 #define LOGT_EVENT 16
00221 #define LOGT_CONFIG 32
00222 #define LOGT_DYNAMIC 64
00223 #define LOGT_IO 128
00224 #define LOGT_INIT 256
00225 #define LOGT_EXECFLOW 512
00226 #define LOGT_CLEANUP 1024
00227 #define LOGT_STRANGE 2048
00228 #define LOGT_XML 4096
00229 #define LOGT_THREAD 8192
00230 #define LOGT_STORAGE 16384
00231 #define LOGT_AUTH 32768
00232 #define LOGT_SESSION 65536
00233 #define LOGT_ROSTER 131072
00234 #define LOGT_BYTES 262144
00235 
00236 int log_get_facility(char const* facility);
00237 int log_get_level(char const* level);
00238 #define MAX_LOG_SIZE 1024
00239 extern int debug_flag;
00240 inline int get_debug_flag(void);
00241 void set_debug_flag(int v);
00242 void set_cmdline_debug_flag(int v);
00243 void set_debug_facility(int facility);
00244 #ifdef __CYGWIN__
00245 #define log_debug if(get_debug_flag()&1) debug_log
00246 #define log_debug2 if(get_debug_flag()) debug_log2
00247 #else
00248 #define log_debug if(debug_flag&1) debug_log
00249 #define log_debug2 if(debug_flag) debug_log2
00250 #endif
00251 void debug_log(char const* zone, char const* msgfmt, ...);
00252 void debug_log2(char const* zone, int type, char const* msgfmt, ...);
00253 void log_notice(char const* host, char const* msgfmt, ...);
00254 void log_warn(char const* host, char const* msgfmt, ...);
00255 void log_alert(char const* host, char const* msgfmt, ...);
00256 #define log_error log_alert
00257 void logger(char const* type, char const* host, char const* message); /* actually creates and delivers the log message */
00258 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","...") */
00259 void log_generic(char const* logtype, char const* id, char const* type, char const* action, char const* msgfmt, ...);
00260 
00261 /*** xdb utilities ***/
00262 
00264 typedef struct xdbcache_struct {
00265     instance i;
00266     int id;
00267     const char *ns;
00268     int set; 
00269     char const* act; 
00270     char const* match; 
00271     char const* matchpath; 
00272     xht namespaces; 
00273     xmlnode data; 
00274     jid owner;
00275     int sent;
00276     int preblock;               
00277     pth_cond_t cond;
00278     pth_mutex_t mutex;
00279     struct xdbcache_struct *prev;
00280     struct xdbcache_struct *next;
00281 } *xdbcache, _xdbcache;
00282 
00283 xdbcache xdb_cache(instance i); 
00284 xmlnode xdb_get(xdbcache xc,  jid owner, const char *ns); 
00285 int xdb_act(xdbcache xc, jid owner, const char *ns, char *act, char *match, xmlnode data); 
00286 int xdb_act_path(xdbcache xc, jid owner, const char *ns, char const *act, char const* matchpath, xht namespaces, xmlnode data); 
00287 int xdb_set(xdbcache xc, jid owner, const char *ns, xmlnode data); 
00289 /* Error messages */
00290 #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>"
00291 #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>"
00292 
00293 /* ------------------------------------
00294  * Managed Thread Queue (MTQ) utilities 
00295  * ------------------------------------*/
00296 
00297 /* default waiting threads */
00298 #define MTQ_THREADS 10
00299 
00301 typedef void (*mtq_callback)(void *arg);
00302 
00304 typedef struct mtqueue_struct
00305 {
00306     struct mth_struct *t;
00307     pth_msgport_t mp;
00308     int routed;
00309 } *mtq, _mtq;
00310 
00312 typedef struct mth_struct
00313 {
00314     mtq q;
00315     pth_msgport_t mp;
00316     pool p;
00317     pth_t id;
00318     int busy;
00319 } *mth, _mth;
00320 
00321 mtq mtq_new(pool p); 
00323 void mtq_send(mtq q, pool p, mtq_callback f, void *arg); 
00325 /* MIO - Managed I/O - TCP functions */
00326 
00328 typedef enum { queue_XMLNODE, queue_CDATA } mio_queue_type;
00329 typedef struct mio_wb_q_st
00330 {
00331     pth_message_t head;  /* for compatibility */
00332     pool p;
00333     mio_queue_type type;
00334     xmlnode x;
00335     void *data;
00336     void *cur;
00337     int len;
00338     struct mio_wb_q_st *next;
00339 } _mio_wbq,*mio_wbq;
00340 
00341 struct mio_handlers_st;
00342 
00343 /* the mio data type */
00344 typedef enum { state_ACTIVE, state_CLOSE } mio_state;
00345 typedef enum { type_LISTEN, type_NORMAL, type_NUL, type_HTTP } mio_type;
00346 
00347 /* standard i/o callback function definition */
00348 struct mio_st;
00349 typedef void (*mio_std_cb)(mio_st* m, int state, void *arg, xmlnode x, char *buffer, int bufsz);
00350 
00354 typedef struct mio_st {
00355     pool p;                             
00356     int fd;                             
00357     mio_type type;                      
00358     mio_state state;                    
00360     mio_wbq queue;                      
00361     mio_wbq tail;                       
00363     struct mio_st *prev,*next;          
00365     void *cb_arg;                       
00366     mio_std_cb cb;                      
00367     struct mio_handlers_st *mh;         
00369     xstream    xs;   /* XXX kill me, I suck */
00370     XML_Parser parser;                  
00371     xmlnode    stacknode;               
00372     void       *ssl;                    
00373     struct {
00374         int     root:1;                 
00375         int     rated:1;                
00376         int     reset_stream:1;         
00377         int     recall_read_when_readable:1;    
00378         int     recall_read_when_writeable:1;   
00379         int     recall_write_when_readable:1;   
00380         int     recall_write_when_writeable:1;  
00381         int     recall_handshake_when_readable:1; 
00382         int     recall_handshake_when_writeable:1; 
00383     } flags;
00384 
00385     struct karma k;                     
00386     jlimit rate;                        
00387     char *peer_ip;                      
00388     uint16_t peer_port;                 
00389     char *our_ip;                       
00390     uint16_t our_port;                  
00391     char *connect_errmsg;               
00392     char *authed_other_side;            
00394     xmppd::ns_decl_list* out_ns;        
00395     xmppd::ns_decl_list* in_root;       
00396     xmppd::ns_decl_list* in_stanza;     
00397     const char *root_lang;              
00398 } *mio, _mio;
00399 
00405 typedef struct mio_main_st {
00406     pool p;             
00407     mio master__list;   
00408     pth_t t;            
00409     int shutdown;       
00410     int zzz[2];         
00411     int zzz_active;     
00412     struct karma *k;    
00413     int rate_t, rate_p; 
00414     char *bounce_uri;   
00415 } _ios,*ios;
00416 
00417 /* MIO SOCKET HANDLERS */
00418 typedef ssize_t (*mio_read_func)        (mio m, void* buf, size_t count);
00419 typedef ssize_t (*mio_write_func)       (mio m, void const* buf, size_t count); 
00420 typedef void    (*mio_parser_func)      (mio m, void const* buf, size_t bufsz);
00421 typedef int     (*mio_accepted_func)    (mio m);
00422 typedef int     (*mio_handshake_func)   (mio m);
00423 typedef void    (*mio_close_func)       (mio m, bool close_read);
00424 
00426 typedef struct mio_handlers_st {
00427     pool  p;
00428     mio_read_func       read;
00429     mio_write_func      write;
00430     mio_accepted_func   accepted;
00431     mio_parser_func     parser;
00432     mio_handshake_func  handshake;
00433     mio_close_func      close;
00434 } _mio_handlers, *mio_handlers; 
00435 
00436 /* standard read/write/accept/connect functions */
00437 ssize_t _mio_raw_read(mio m, void *buf, size_t count);
00438 ssize_t _mio_raw_write(mio m, void *buf, size_t count);
00439 void _mio_raw_parser(mio m, const void *buf, size_t bufsz);
00440 #define MIO_RAW_READ     (mio_read_func)&_mio_raw_read
00441 #define MIO_RAW_WRITE    (mio_write_func)&_mio_raw_write
00442 #define MIO_RAW_ACCEPTED (mio_accepted_func)NULL
00443 #define MIO_RAW_PARSER   (mio_parser_func)&_mio_raw_parser
00444 
00445 void mio_xml_reset(mio m);
00446 int  mio_xml_starttls(mio m, int originator, const char *identity);
00447 void _mio_xml_parser(mio m, const void *buf, size_t bufsz);
00448 #define MIO_XML_PARSER  (mio_parser_func)&_mio_xml_parser
00449 
00450 /* function helpers */
00451 #define MIO_LISTEN_RAW mio_handlers_new(NULL, NULL, NULL)
00452 #define MIO_CONNECT_RAW mio_handlers_new(NULL, NULL, NULL)
00453 #define MIO_LISTEN_XML mio_handlers_new(NULL, NULL, MIO_XML_PARSER)
00454 #define MIO_CONNECT_XML mio_handlers_new(NULL, NULL, MIO_XML_PARSER)
00455 
00456 /* TLS functions */
00457 void    mio_ssl_init     (xmlnode x);
00458 bool    mio_tls_early_init();
00459 int     mio_ssl_starttls (mio m, int originator, const char* identity);
00460 int     mio_ssl_starttls_possible (mio m, const char* identity);
00461 int     mio_ssl_verify(mio m, const char *id_on_xmppAddr);
00462 ssize_t _mio_ssl_read    (mio m, void *buf, size_t count);
00463 ssize_t _mio_ssl_write   (mio m, const void*      buf,       size_t     count);
00464 int     _mio_ssl_accepted(mio m);
00465 void    mio_tls_get_characteristics(mio m, char* buffer, size_t len);
00466 void    mio_tls_get_certtype(mio m, char* buffer, size_t len);
00467 void    mio_tls_get_compression(mio m, char* buffer, size_t len);
00468 #define MIO_SSL_READ     _mio_ssl_read
00469 #define MIO_SSL_WRITE    _mio_ssl_write
00470 #define MIO_SSL_ACCEPTED _mio_ssl_accepted
00471 
00472 int     mio_is_encrypted(mio m);
00473 
00474 /* MIO handlers helper functions */
00475 mio_handlers mio_handlers_new(mio_read_func rf, mio_write_func wf, mio_parser_func pf);
00476 void         mio_handlers_free(mio_handlers mh);
00477 void         mio_set_handlers(mio m, mio_handlers mh);
00478 
00479 /* callback state flags */
00480 #define MIO_NEW       0
00481 #define MIO_BUFFER    1
00482 #define MIO_XML_ROOT  2
00483 #define MIO_XML_NODE  3 
00484 #define MIO_CLOSED    4
00485 #define MIO_ERROR     5
00486 
00487 /* Initializes the MIO subsystem */
00488 void mio_init(void);
00489 
00490 /* Stops the MIO system */
00491 void mio_stop(void);
00492 
00493 /* Create a new mio object from a file descriptor */
00494 mio mio_new(int fd, mio_std_cb cb, void *cb_arg, mio_handlers mh);
00495 
00496 /* Reset the callback and argument for an mio object */
00497 void mio_reset(mio m, mio_std_cb cb, void *arg);
00498 
00499 /* Request the mio socket be closed */
00500 void mio_close(mio m);
00501 
00502 /* Writes an xmlnode to the socket */
00503 void mio_write(mio m, xmlnode stanza, char const* buffer, int len);
00504 
00505 /* write the root element to a mio stream */
00506 void mio_write_root(mio m, xmlnode root, int stream_type);
00507 
00508 /* Sets the karma values for a socket */
00509 void mio_karma(mio m, int val, int max, int inc, int dec, int penalty, int restore);
00510 void mio_karma2(mio m, struct karma *k);
00511 
00512 /* Sets connection based rate limits */
00513 void mio_rate(mio m, int rate_time, int max_points);
00514 
00515 /* Pops the next xmlnode from the queue, or NULL if no more nodes */
00516 xmlnode mio_cleanup(mio m);
00517 
00518 /* Connects to an ip */
00519 void mio_connect(char *host, int port, mio_std_cb cb, void *cb_arg, int timeout, mio_handlers mh);
00520 
00521 /* Starts listening on a port/ip, returns NULL if failed to listen */
00522 mio mio_listen(int port, char const* sourceip, mio_std_cb cb, void *cb_arg, mio_handlers mh);
00523 
00524 int _mio_write_dump(mio m);
00525 
00526 /* some nice api utilities */
00527 #define mio_pool(m) (m->p)
00528 #define mio_ip(m) (m ? m->peer_ip : NULL)
00529 #define mio_connect_errmsg(m) (m->connect_errmsg)
00530 
00531 /*-----------------
00532  * Access controll 
00533  *-----------------*/
00534 
00535 int acl_check_access(xdbcache xdb, const char *function, const jid user);
00536 jid acl_get_users(xdbcache xdb, const char *function);
00537 
00538 namespace xmppd {
00539 
00540     /* ******************** Logging ******************** */
00541 
00542     /* forward declaration */
00543     class logging;
00544 
00548     enum loglevel {
00549         notice,
00550         warn,
00551         error,
00552         alert
00553     };
00554 
00560     class logmessage : public std::ostringstream {
00561         public:
00568             logmessage(logging& log_entity, loglevel level);
00569 
00573             ~logmessage();
00574 
00581             std::ostream& operator<<(const char* text);
00582 
00589             std::ostream& operator<<(const std::string& text);
00590 
00594             logmessage(const logmessage& orig);
00595 
00596         private:
00600             logging& log_entity;
00601 
00605             loglevel level;
00606     };
00607 
00611     class logging {
00612         public:
00618             logging(Glib::ustring ident);
00619 
00623             ~logging();
00624 
00630             logmessage level(loglevel level_to_use);
00631         private:
00638             void write(loglevel level_to_use, Glib::ustring log_message);
00639 
00643             friend class logmessage;
00644 
00648             Glib::ustring identity;
00649     };
00650 
00651 /*--------------------------
00652  * OOP-instances base class
00653  *--------------------------*/
00654 
00658     class instance_base {
00659         public:
00666             instance_base(instance i, xmlnode x);
00667 
00668         protected:
00677             virtual result on_packet(dpacket dp);
00678 
00687             virtual result on_stanza_packet(dpacket dp);
00688 
00697             virtual result on_xdb_packet(dpacket dp);
00698 
00707             virtual result on_log_packet(dpacket dp);
00708 
00717             virtual result on_route_packet(dpacket dp);
00718 
00727             virtual result on_message_stanza(jpacket p);
00728 
00739             virtual result on_presence_stanza(jpacket p);
00740 
00749             virtual result on_iq_stanza(jpacket p);
00750 
00759             virtual result on_subscription_stanza(jpacket p);
00760 
00764             virtual void on_heartbeat();
00765 
00773             void set_heartbeat_interval(int interval);
00774 
00780             void deliver(dpacket p);
00781 
00787             void deliver(xmlnode x);
00788 
00795             void deliver_fail(dpacket p, const std::string& reason_text);
00796 
00807             void bounce_stanza(xmlnode x, xterror xterr);
00808 
00814             std::string get_instance_id();
00815 
00822             logmessage log(loglevel level);
00823 
00829             xmlnode get_instance_config();
00830 
00831         private:
00835             instance i;
00836 
00840             pointer<logging> logger;
00841 
00847             instance_base(instance_base& ref);
00848 
00852             result beathandler_wrapper();
00853 
00861             static result phandler_helper(instance id, dpacket p, void* arg);
00862 
00868             static result beathandler_helper(void* arg);
00869 
00877             int current_heartbeat_frequency;
00878 
00886             int requested_heartbeat_frequency;
00887     };
00888 }

Generated on Wed Sep 10 01:59:47 2008 for jabberd14 by  doxygen 1.5.5