1 /* 2 * jabberd - Jabber Open Source Server 3 * Copyright (c) 2002 Jeremie Miller, Thomas Muldowney, 4 * Ryan Eatmon, Robert Norris 5 * 6 * This program is free software; you can redistribute it and/or drvify 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 /** @file sm/sm.h 22 * @brief data structures and prototypes for the session manager 23 * @author Jeremie Miller 24 * @author Robert Norris 25 * $Date: 2005/09/09 05:34:13 $ 26 * $Revision: 1.62 $ 27 */ 28 29 #ifdef HAVE_CONFIG_H 30 #include <config.h> 31 #endif 32 33 #include "sx/sx.h" 34 #include "mio/mio.h" 35 #include "util/util.h" 36 #include "storage/storage.h" 37 38 #ifdef HAVE_SIGNAL_H 39 #include <signal.h> 40 #endif 41 #ifdef HAVE_SYS_STAT_H 42 #include <sys/stat.h> 43 #endif 44 45 #ifdef _WIN32 46 #ifdef _USRDLL 47 #define DLLEXPORT __declspec(dllexport) 48 #define SM_API __declspec(dllimport) 49 #else 50 #define DLLEXPORT __declspec(dllimport) 51 #define SM_API __declspec(dllexport) 52 #endif 53 #else 54 #define DLLEXPORT 55 #define SM_API 56 #endif 57 58 /* forward declarations */ 59 typedef struct sm_st *sm_t; 60 typedef struct user_st *user_t; 61 typedef struct sess_st *sess_t; 62 typedef struct aci_st *aci_t; 63 typedef struct mm_st *mm_t; 64 65 /* namespace uri strings */ 66 #include "util/uri.h" 67 68 /* indexed known namespace values */ 69 #define ns_AUTH (1) 70 #define ns_REGISTER (2) 71 #define ns_ROSTER (3) 72 #define ns_AGENTS (4) 73 #define ns_DELAY (5) 74 #define ns_BROWSE (6) 75 #define ns_EVENT (7) 76 #define ns_GATEWAY (8) 77 #define ns_EXPIRE (9) 78 #define ns_SEARCH (10) 79 #define ns_DISCO (11) 80 #define ns_DISCO_ITEMS (12) 81 #define ns_DISCO_INFO (13) 82 83 #define ns_AMP (14) 84 #define ns_AMP_ERRORS (15) 85 #define ns_AMP_ACTION_DROP (16) 86 #define ns_AMP_ACTION_ERROR (17) 87 #define ns_AMP_ACTION_NOTIFY (18) 88 #define ns_AMP_CONDITION_DELIVER (19) 89 #define ns_AMP_CONDITION_EXPIREAT (20) 90 #define ns_AMP_CONDITION_MATCHRESOURCE (21) 91 92 /** packet types */ 93 typedef enum { 94 pkt_NONE = 0x00, /**< no packet */ 95 pkt_MESSAGE = 0x10, /**< message */ 96 pkt_MESSAGE_CHAT = 0x11, /**< message (chat) */ 97 pkt_MESSAGE_HEADLINE = 0x12,/**< message (headline) */ 98 pkt_MESSAGE_GROUPCHAT = 0x14,/**< message (groupchat) */ 99 pkt_PRESENCE = 0x20, /**< presence */ 100 pkt_PRESENCE_UN = 0x21, /**< presence (unavailable) */ 101 pkt_PRESENCE_PROBE = 0x24, /**< presence (probe) */ 102 pkt_S10N = 0x40, /**< subscribe request */ 103 pkt_S10N_ED = 0x41, /**< subscribed response */ 104 pkt_S10N_UN = 0x42, /**< unsubscribe request */ 105 pkt_S10N_UNED = 0x44, /**< unsubscribed response */ 106 pkt_IQ = 0x80, /**< info/query (get) */ 107 pkt_IQ_SET = 0x81, /**< info/query (set) */ 108 pkt_IQ_RESULT = 0x82, /**< info/query (result) */ 109 pkt_SESS = 0x100, /**< session start request */ 110 pkt_SESS_END = 0x101, /**< session end request */ 111 pkt_SESS_CREATE = 0x102, /**< session create request */ 112 pkt_SESS_DELETE = 0x104, /**< session delete request */ 113 pkt_SESS_FAILED = 0x08, /**< session request failed (mask) */ 114 pkt_SESS_MASK = 0x10f, /**< session request (mask) */ 115 pkt_ERROR = 0x200 /**< packet error */ 116 } pkt_type_t; 117 118 /** route types */ 119 typedef enum { 120 route_NONE = 0x00, /**< no route */ 121 route_UNICAST = 0x10, /**< unicast */ 122 route_BROADCAST = 0x11, /**< broadcast */ 123 route_ADV = 0x20, /**< advertisement (available) */ 124 route_ADV_UN = 0x21, /**< advertisement (unavailable) */ 125 route_ERROR = 0x40 /**< route error */ 126 } route_type_t; 127 128 /** packet summary data wrapper */ 129 typedef struct pkt_st { 130 sm_t sm; /**< sm context */ 131 132 sess_t source; /**< session this packet came from */ 133 134 jid_t rto, rfrom; /**< addressing of enclosing route */ 135 136 route_type_t rtype; /**< type of enclosing route */ 137 138 pkt_type_t type; /**< packet type */ 139 140 jid_t to, from; /**< packet addressing (not used for routing) */ 141 142 int ns; /**< iq sub-namespace */ 143 144 int pri; /**< presence priority */ 145 146 nad_t nad; /**< nad of the entire packet */ 147 } *pkt_t; 148 149 /** roster items */ 150 typedef struct item_st { 151 jid_t jid; /**< id of this item */ 152 153 const char *name; /**< display name */ 154 155 const char **groups; /**< groups this item is in */ 156 157 int ngroups; /**< number of groups in groups array */ 158 159 int to, from; /**< subscription to this item (they get presence FROM us, they send presence TO us) */ 160 161 int ask; /**< pending subscription (0 == none, 1 == subscribe, 2 == unsubscribe) */ 162 163 int ver; /**< roster item version number */ 164 } *item_t; 165 166 /** session manager global context */ 167 struct sm_st { 168 const char *id; /**< component id */ 169 170 const char *router_ip; /**< ip to connect to the router at */ 171 int router_port; /**< port to connect to the router at */ 172 const char *router_user; /**< username to authenticate to the router as */ 173 const char *router_pass; /**< password to authenticate to the router with */ 174 const char *router_pemfile; /**< name of file containing a SSL certificate & 175 key for channel to the router */ 176 const char *router_private_key_password; /** password for private key if pemfile 177 key is encrypted */ 178 const char *router_ciphers; /** TLS ciphers */ 179 180 mio_t mio; /**< mio context */ 181 182 sx_env_t sx_env; /**< SX environment */ 183 sx_plugin_t sx_sasl; /**< SX SASL plugin */ 184 sx_plugin_t sx_ssl; /**< SX SSL plugin */ 185 186 sx_t router; /**< SX of router connection */ 187 mio_fd_t fd; /**< file descriptor of router connection */ 188 189 xht users; /**< pointers to currently loaded users (key is user@@domain) */ 190 191 xht sessions; /**< pointers to all connected sessions (key is random sm id) */ 192 193 xht xmlns; /**< index of namespaces (for iq sub-namespace in pkt_t) */ 194 xht xmlns_refcount; /**< ref-counting for modules namespaces */ 195 196 xht features; /**< feature index (key is feature string */ 197 198 config_t config; /**< config context */ 199 200 log_t log; /**< log context */ 201 202 log_type_t log_type; /**< log type */ 203 const char *log_facility; /**< syslog facility (local0 - local7) */ 204 const char *log_ident; /**< log identifier */ 205 206 int retry_init; /**< number of times to try connecting to the router at startup */ 207 int retry_lost; /**< number of times to try reconnecting to the router if the connection drops */ 208 int retry_sleep; /**< sleep interval between retries */ 209 int retry_left; /**< number of tries left before failure */ 210 211 storage_t st; /**< storage subsystem */ 212 213 mm_t mm; /**< module subsystem */ 214 215 xht acls; /**< access control lists (key is list name, value is jid_t list) */ 216 217 char signature[2048]; /**< server signature */ 218 int siglen; /**< length of signature */ 219 220 int started; /**< true if we've connected to the router at least once */ 221 222 int online; /**< true if we're currently bound in the router */ 223 224 xht hosts; /**< vHosts map */ 225 226 /** Database query rate limits */ 227 int query_rate_total; 228 int query_rate_seconds; 229 int query_rate_wait; 230 xht query_rates; 231 }; 232 233 /** data for a single user */ 234 struct user_st { 235 pool_t p; /**< memory pool this user is allocated off */ 236 237 sm_t sm; /**< sm context */ 238 239 jid_t jid; /**< user jid (user@@host) */ 240 241 xht roster; /**< roster for this user (key is full jid of item, value is item_t) */ 242 243 sess_t sessions; /**< list of action sessions */ 244 sess_t top; /**< top priority session */ 245 int available; /**< true if this user has any available session */ 246 247 time_t active; /**< time that user first logged in (ever) */ 248 249 void **module_data; /**< per-user module data */ 250 }; 251 252 /** data for a single session */ 253 struct sess_st { 254 pool_t p; /**< memory pool this session is allocated off */ 255 256 user_t user; /**< user this session belongs to */ 257 258 jid_t jid; /**< session jid (user@@host/res) */ 259 260 char c2s[1024]; /**< id of c2s that is handling their connection */ 261 262 char sm_id[41]; /**< local id (for session control) */ 263 char c2s_id[44]; /**< remote id (for session control) */ 264 265 pkt_t pres; /**< copy of the last presence packet we received */ 266 267 int available; /**< true if this session is available */ 268 int pri; /**< current priority of this session */ 269 int fake; /**< true if session is fake (ie. PBX) */ 270 271 jid_t A; /**< list of jids that this session has sent directed presence to */ 272 jid_t E; /**< list of jids that bounced presence updates we sent them */ 273 274 void **module_data; /**< per-session module data */ 275 276 sess_t next; /**< next session (in a list of sessions) */ 277 }; 278 279 extern sig_atomic_t sm_lost_router; 280 281 /* functions */ 282 SM_API xht aci_load(sm_t sm); 283 SM_API int aci_check(xht acls, const char *type, jid_t jid); 284 SM_API void aci_unload(xht acls); 285 286 SM_API int sm_sx_callback(sx_t s, sx_event_t e, void *data, void *arg); 287 SM_API int sm_mio_callback(mio_t m, mio_action_t a, mio_fd_t fd, void *data, void *arg); 288 SM_API void sm_timestamp(time_t t, char timestamp[18]); 289 SM_API void sm_c2s_action(sess_t dest, const char *action, const char *target); 290 SM_API void sm_signature(sm_t sm, const char *str); 291 292 SM_API int sm_register_ns(sm_t sm, const char *uri); 293 SM_API void sm_unregister_ns(sm_t sm, const char *uri); 294 SM_API int sm_get_ns(sm_t sm, const char *uri); 295 296 SM_API int sm_storage_rate_limit(sm_t sm, const char *owner); 297 298 SM_API void dispatch(sm_t sm, pkt_t pkt); 299 300 SM_API pkt_t pkt_error(pkt_t pkt, int err); 301 SM_API pkt_t pkt_tofrom(pkt_t pkt); 302 SM_API pkt_t pkt_dup(pkt_t pkt, const char *to, const char *from); 303 SM_API pkt_t pkt_new(sm_t sm, nad_t nad); 304 SM_API void pkt_free(pkt_t pkt); 305 SM_API pkt_t pkt_create(sm_t sm, const char *elem, const char *type, const char *to, const char *from); 306 SM_API void pkt_id(pkt_t src, pkt_t dest); 307 SM_API void pkt_id_new(pkt_t pkt); 308 SM_API void pkt_delay(pkt_t pkt, time_t t, const char *from); 309 310 SM_API void pkt_router(pkt_t pkt); 311 SM_API void pkt_sess(pkt_t pkt, sess_t sess); 312 313 SM_API int pres_trust(user_t user, jid_t jid); 314 SM_API void pres_roster(sess_t sess, item_t item); 315 SM_API void pres_update(sess_t sess, pkt_t pres); 316 SM_API void pres_error(sess_t sess, jid_t jid); 317 SM_API void pres_deliver(sess_t sess, pkt_t pres); 318 SM_API void pres_in(user_t user, pkt_t pres); 319 SM_API void pres_probe(user_t user); 320 321 SM_API void sess_route(sess_t sess, pkt_t pkt); 322 SM_API sess_t sess_start(sm_t sm, jid_t jid); 323 SM_API void sess_end(sess_t sess); 324 SM_API sess_t sess_match(user_t user, const char *resource); 325 326 SM_API user_t user_load(sm_t sm, jid_t jid); 327 SM_API void user_free(user_t user); 328 SM_API int user_create(sm_t sm, jid_t jid); 329 SM_API void user_delete(sm_t sm, jid_t jid); 330 331 SM_API void feature_register(sm_t sm, const char *feature); 332 SM_API void feature_unregister(sm_t sm, const char *feature); 333 334 335 /* driver module manager */ 336 337 /** module return values */ 338 typedef enum { 339 mod_HANDLED, /**< packet was handled (and freed) */ 340 mod_PASS /**< packet was unhandled, should be passed to the next module */ 341 } mod_ret_t; 342 343 /** module chain types */ 344 typedef enum { 345 chain_SESS_START, /**< session start, load per-session data */ 346 chain_SESS_END, /**< session ended, save & free per-session data */ 347 chain_IN_SESS, /**< packet from an active session */ 348 chain_IN_ROUTER, /**< packet from the router */ 349 chain_OUT_SESS, /**< packet to an active session */ 350 chain_OUT_ROUTER, /**< packet to a router */ 351 chain_PKT_SM, /**< packet for the sm itself */ 352 chain_PKT_USER, /**< packet for a user */ 353 chain_PKT_ROUTER, /**< packet from the router (special purpose) */ 354 chain_USER_LOAD, /**< user loaded, load per-user data */ 355 chain_USER_CREATE, /**< user creation, generate and save per-user data */ 356 chain_USER_DELETE, /**< user deletion, delete saved per-user data */ 357 chain_USER_UNLOAD, /**< user is about to be unloaded */ 358 chain_DISCO_EXTEND /**< disco request, extend sm disco#info */ 359 } mod_chain_t; 360 361 typedef struct module_st *module_t; 362 typedef struct mod_instance_st *mod_instance_t; 363 364 /** module manager data */ 365 struct mm_st { 366 sm_t sm; /**< sm context */ 367 368 xht modules; /**< pointers to module data (key is module name) */ 369 370 int nindex; /**< counter for module instance sequence (!!! should be local to mm_new) */ 371 372 /** sess-start chain */ 373 mod_instance_t *sess_start; int nsess_start; 374 /** sess-end chain */ 375 mod_instance_t *sess_end; int nsess_end; 376 /** in-sess chain */ 377 mod_instance_t *in_sess; int nin_sess; 378 /** in-router chain */ 379 mod_instance_t *in_router; int nin_router; 380 /** out-sess chain */ 381 mod_instance_t *out_sess; int nout_sess; 382 /** out-router chain */ 383 mod_instance_t *out_router; int nout_router; 384 /** pkt-sm chain */ 385 mod_instance_t *pkt_sm; int npkt_sm; 386 /** pkt-user chain */ 387 mod_instance_t *pkt_user; int npkt_user; 388 /** pkt-router chain */ 389 mod_instance_t *pkt_router; int npkt_router; 390 /** user-load chain */ 391 mod_instance_t *user_load; int nuser_load; 392 /** user-create chain */ 393 mod_instance_t *user_create; int nuser_create; 394 /** user-delete chain */ 395 mod_instance_t *user_delete; int nuser_delete; 396 /** disco-extend chain */ 397 mod_instance_t *disco_extend; int ndisco_extend; 398 /** user-unload chain */ 399 mod_instance_t *user_unload; int nuser_unload; 400 }; 401 402 /** data for a single module */ 403 struct module_st { 404 mm_t mm; /**< module manager */ 405 406 const char *name; /**< name of module */ 407 408 int index; /**< module index. this is the index into user->module_data and 409 sess->module_data where the module can store its own 410 per-user/per-session data */ 411 412 void *handle; /**< module handle */ 413 414 int (*module_init_fn)(mod_instance_t); /**< module init function */ 415 416 int init; /**< number of times the module intialiser has been called */ 417 418 void *private; /**< module private data */ 419 420 int (*sess_start)(mod_instance_t mi, sess_t sess); /**< sess-start handler */ 421 void (*sess_end)(mod_instance_t mi, sess_t sess); /**< sess-end handler */ 422 423 mod_ret_t (*in_sess)(mod_instance_t mi, sess_t sess, pkt_t pkt); /**< in-sess handler */ 424 mod_ret_t (*in_router)(mod_instance_t mi, pkt_t pkt); /**< in-router handler */ 425 426 mod_ret_t (*out_sess)(mod_instance_t mi, sess_t sess, pkt_t pkt); /**< out-sess handler */ 427 mod_ret_t (*out_router)(mod_instance_t mi, pkt_t pkt); /**< out-router handler */ 428 429 mod_ret_t (*pkt_sm)(mod_instance_t mi, pkt_t pkt); /**< pkt-sm handler */ 430 mod_ret_t (*pkt_user)(mod_instance_t mi, user_t user, pkt_t pkt); /**< pkt-user handler */ 431 432 mod_ret_t (*pkt_router)(mod_instance_t mi, pkt_t pkt); /**< pkt-router handler */ 433 434 int (*user_load)(mod_instance_t mi, user_t user); /**< user-load handler */ 435 int (*user_unload)(mod_instance_t mi, user_t user); /**< user-load handler */ 436 437 int (*user_create)(mod_instance_t mi, jid_t jid); /**< user-create handler */ 438 void (*user_delete)(mod_instance_t mi, jid_t jid); /**< user-delete handler */ 439 440 void (*disco_extend)(mod_instance_t mi, pkt_t pkt); /**< disco-extend handler */ 441 442 void (*free)(module_t mod); /**< called when module is freed */ 443 }; 444 445 /** single instance of a module in a chain */ 446 struct mod_instance_st { 447 sm_t sm; /**< sm context */ 448 449 module_t mod; /**< module that this is an instance of */ 450 451 int seq; /**< number of this instance */ 452 453 mod_chain_t chain; /**< chain this instance is in */ 454 455 const char *arg; /**< option arg that this instance was started with */ 456 }; 457 458 /** allocate a module manager instance, and loads the modules */ 459 SM_API mm_t mm_new(sm_t sm); 460 /** free a mm instance */ 461 SM_API void mm_free(mm_t mm); 462 463 /** fire sess-start chain */ 464 SM_API int mm_sess_start(mm_t mm, sess_t sess); 465 /** fire sess-end chain */ 466 SM_API void mm_sess_end(mm_t mm, sess_t sess); 467 468 /** fire in-sess chain */ 469 SM_API mod_ret_t mm_in_sess(mm_t mm, sess_t sess, pkt_t pkt); 470 /** fire in-router chain */ 471 SM_API mod_ret_t mm_in_router(mm_t mm, pkt_t pkt); 472 473 /** fire out-sess chain */ 474 SM_API mod_ret_t mm_out_sess(mm_t mm, sess_t sess, pkt_t pkt); 475 /** fire out-router chain */ 476 SM_API mod_ret_t mm_out_router(mm_t mm, pkt_t pkt); 477 478 /** fire pkt-sm chain */ 479 SM_API mod_ret_t mm_pkt_sm(mm_t mm, pkt_t pkt); 480 /** fire pkt-user chain */ 481 SM_API mod_ret_t mm_pkt_user(mm_t mm, user_t user, pkt_t pkt); 482 483 /** fire pkt-router chain */ 484 SM_API mod_ret_t mm_pkt_router(mm_t mm, pkt_t pkt); 485 486 /** fire user-load chain */ 487 SM_API int mm_user_load(mm_t mm, user_t user); 488 489 /** fire user-unload chain */ 490 SM_API int mm_user_unload(mm_t mm, user_t user); 491 492 /** fire user-create chain */ 493 SM_API int mm_user_create(mm_t mm, jid_t jid); 494 /** fire user-delete chain */ 495 SM_API void mm_user_delete(mm_t mm, jid_t jid); 496 497 /** fire disco-extend chain */ 498 SM_API void mm_disco_extend(mm_t mm, pkt_t pkt); 499