void aim_transport(instance i, xmlnode x) { ati ti; xmlnode config; char *eightbitcode, *utf8code = "UTF-8", *latin1code = "CP1252"; ti = pmalloco(i->p, sizeof(_ati)); ti->i = i; ti->xc = xdb_cache(i); log_notice(i->id, "AIM-Transport starting up for instance %s...", i->id); config = xdb_get(ti->xc, jid_new(xmlnode_pool(x), "config@-internal"), "jabber:config:aimtrans"); ti->vcard = xmlnode_new_tag_pool(i->p,"vCard"); xmlnode_put_attrib(ti->vcard,"xmlns",NS_VCARD); xmlnode_insert_node(ti->vcard,xmlnode_get_firstchild(xmlnode_get_tag(config,"vCard"))); ti->start_time = time(NULL); ti->session__list=xhash_new(101); ti->iq__callbacks = xhash_new(23); ti->pending__buddies = xhash_new(101); /* The aim.exe binary should not be necessary any more. */ ti->aimbinarydir = pstrdup(i->p, xmlnode_get_tag_data(config, "aimbinarydir")); eightbitcode = pstrdup(i->p, xmlnode_get_tag_data(config, "charset")); if( eightbitcode == NULL ) { log_notice( i->id, "Charset is not specified, using CP1252" ); eightbitcode = latin1code; } xmlnode_free(config); fromutf8 = iconv_open(eightbitcode, utf8code); if(fromutf8 == (iconv_t)(-1)) { log_error(i->id, "Conversion from %s to %s is not supported", utf8code, eightbitcode); raise(SIGINT); } toutf8 = iconv_open(utf8code, eightbitcode); if(toutf8 == (iconv_t)(-1)) { log_error(i->id, "Conversion from %s to %s is not supported", eightbitcode, utf8code); raise(SIGINT); } ti->send_buf = NULL; ti->modname = NULL; pth_mutex_init(&ti->buddies_mutex); at_init_iqcbs(ti); register_phandler(i, o_DELIVER, at_phandler, ti); pool_cleanup(i->p, at_shutdown, (void*)i); }
/* bounces packet for unknown users with the appropriate error */ void mt_unknown_bounce(void *arg) { jpacket jp = (jpacket) arg; mti ti = (mti) jp->aux1; xmlnode reg; lowercase(jp->from->user); lowercase(jp->from->server); if ((reg = xdb_get(ti->xc,mt_xdb_id(jp->p,jp->from,jp->to->server),NS_REGISTER)) != NULL) { xmlnode p = xmlnode_new_tag("presence"); xmlnode_put_attrib(p,"to",jid_full(jp->from)); xmlnode_put_attrib(p,"from",jp->to->server); xmlnode_put_attrib(p,"type","probe"); mt_deliver(ti,p); jutil_error(jp->x,TERROR_NOTFOUND); xmlnode_free(reg); } else jutil_error(jp->x,TERROR_REGISTER); mt_deliver(ti,jp->x); }
mreturn mod_browse_reply(mapi m, void *arg) { xmlnode browse, ns, cur; session s; if (m->packet->type != JPACKET_IQ) return M_IGNORE; if (!NSCHECK(m->packet->iq, NS_BROWSE)) return M_PASS; /* first, is this a valid request? */ switch (jpacket_subtype(m->packet)) { case JPACKET__RESULT: case JPACKET__ERROR: return M_PASS; case JPACKET__SET: js_bounce(m->si, m->packet->x, TERROR_NOTALLOWED); return M_HANDLED; } log_debug("handling query for user %s", m->user->user); /* get this dudes browse info */ browse = mod_browse_get(m, m->packet->to); /* insert the namespaces */ ns = xdb_get(m->si->xc, m->packet->to, NS_XDBNSLIST); for (cur = xmlnode_get_firstchild(ns); cur != NULL; cur = xmlnode_get_nextsibling(cur)) if (xmlnode_get_attrib(cur, "type") == NULL) xmlnode_insert_tag_node(browse, cur); /* only include the generic <ns>foo</ns> */ xmlnode_free(ns); /* include any connected resources if there's a s10n from them */ if (js_trust(m->user, m->packet->from)) { SEM_LOCK(m->user->sem); for (s = m->user->sessions; s != NULL; s = s->next) { /* if(s->priority < 0) continue; *** include all resources I guess */ if (xmlnode_get_tag (browse, spools(m->packet->p, "?jid=", jid_full(s->id), m->packet->p)) != NULL) continue; /* already in the browse result */ cur = xmlnode_insert_tag(browse, "user"); xmlnode_put_attrib(cur, "type", "client"); xmlnode_put_attrib(cur, "jid", jid_full(s->id)); } SEM_UNLOCK(m->user->sem); } /* XXX include iq:filter forwards */ jutil_iqresult(m->packet->x); jpacket_reset(m->packet); xmlnode_insert_tag_node(m->packet->x, browse); js_deliver(m->si, m->packet); xmlnode_free(browse); return M_HANDLED; }
xmlnode mod_browse_get(mapi m, jid id) { xmlnode browse, x; if (id == NULL) /* use the user id as a backup */ id = m->user->id; /* get main account browse */ if ((browse = xdb_get(m->si->xc, id, NS_BROWSE)) == NULL) { /* no browse is set up yet, we must create one for this user! */ if (id->resource == NULL) { /* a user is only the user@host */ browse = xmlnode_new_tag("user"); /* get the friendly name for this user from somewhere */ if ((x = xdb_get(m->si->xc, m->user->id, NS_VCARD)) != NULL) xmlnode_put_attrib(browse, "name", xmlnode_get_tag_data(x, "FN")); else if ((x = xdb_get(m->si->xc, m->user->id, NS_REGISTER)) != NULL) xmlnode_put_attrib(browse, "name", xmlnode_get_tag_data(x, "name")); xmlnode_free(x); } else { /* everything else is generic unless set by the user */ browse = xmlnode_new_tag("item"); } xmlnode_put_attrib(browse, "xmlns", NS_BROWSE); xmlnode_put_attrib(browse, "jid", jid_full(id)); xdb_set(m->si->xc, id, NS_BROWSE, browse); } return browse; }
/** * A replacement for xdb_set * returns 0 if the set succeeded, 1 if not */ int xdb_set(void *noop, jid fn, char *ns, xmlnode x) { extern jcr_instance jcr; char buf[512]; xmlnode n, s, xdb; int rc; n = xdb_get(noop, fn, NULL); if (n == NULL) { memset(buf, 0, 512); snprintf(buf, 511, "%s/%s.xml", jcr->spool_dir, fn->user); xdb = xmlnode_new_tag("xdb"); if (ns != NULL) xmlnode_put_attrib(x, "xdbns", ns); // log_debug(JDBG, "node not found, creating %s", xmlnode2str(x)); xmlnode_insert_node(xdb, x); // log_debug(JDBG, "\nwriting '%s'\n\n", xmlnode2str(xdb)); rc = xmlnode2file(buf, xdb); xmlnode_free(xdb); return rc; } else { s = NULL; // log_debug(JDBG, "node found '%s'", xmlnode2str(n)); if (ns == NULL) { s = xmlnode_get_tag(n, xmlnode_get_name(x)); } else { memset(buf, 0, 512); snprintf(buf, 511, "%s?xdbns=%s", xmlnode_get_name(x), ns); // log_debug(JDBG, "search for '%s'", buf); s = xmlnode_get_tag(n, buf); } // log_debug(JDBG, "removing '%s'", xmlnode2str(s)); xmlnode_hide(s); if (ns != NULL) xmlnode_put_attrib(x, "xdbns", ns); xmlnode_insert_tag_node(n, x); } snprintf(buf, 511, "%s/%s.xml", jcr->spool_dir, fn->user); rc = xmlnode2file(buf, n); xmlnode_free(n); log_debug(JDBG, "xdb_set: rc == %d", rc); return 0; }
char *moby_get_syn(DB * db, char *tok, int opt) { dlist_t *dl = NULL, *dptr = NULL; xdb_pair_t *pair = NULL; char *str = NULL; debug(NULL, "moby_get_syn: Entered\n"); if (!tok || !moby_info.initialized) return NULL; str_apply(tok, tolower); pair = xdb_get(db, tok); if (!pair) return NULL; if (opt == MOBY_SINGLE) { dl = tokenize(NULL, pair->value, TOKENIZE_NORMAL | TOKENIZE_EATWHITESPACE, ","); if (!dl) goto cleanup; dptr = dlist_node_rand(dl); if (!dptr) goto cleanup; str = str_unite_static("%s", (char *)dlist_data(dptr)); } else if (opt == MOBY_LIST) { str = str_unite_static("%s,%s", pair->key, pair->value); } cleanup: if (dl) tokenize_destroy(NULL, &dl); xdb_pair_destroy(pair); return str; }
/** Start up transport. Read configuration, register callbacks. */ void icqtrans(instance i, xmlnode x) { iti ti; pool p = i->p; xmlnode config; xmlnode cur; int check; log_debug(ZONE,"ICQ Transport, initializing for section '%s'",i->id); /* create new transport instance */ ti = pmalloco(p,sizeof(_iti)); ti->i = i; ti->xc = xdb_cache(i); config = xdb_get(ti->xc,jid_new(xmlnode_pool(x),"config@-internal"),"jabber:config:icqtrans"); if (config == NULL) { log_error(i->id,"Configuration not found!"); return; } ti->registration_instructions = pstrdup(p,xmlnode_get_tag_data(config,"instructions")); if (ti->registration_instructions == NULL) { log_debug(i->id,"Registration instructions not found"); } ti->search_instructions = pstrdup(p,xmlnode_get_tag_data(config,"search")); if (ti->search_instructions == NULL) { log_debug(i->id,"Search instructions not found"); } ti->charset = pstrdup(p,xmlnode_get_tag_data(config,"charset")); if (ti->charset == NULL) { log_debug(i->id,"Charset not specified, set default to %s ",DEFAULT_CHARSET); ti->charset = pstrdup(p,DEFAULT_CHARSET); } _ucs2utf = iconv_open("UTF-8","UCS-2BE"); _win2utf = iconv_open("UTF-8",ti->charset); if (_win2utf==(iconv_t)-1) { ti->charset = pstrdup(p,DEFAULT_CHARSET); _win2utf = iconv_open("UTF-8",ti->charset); if (_win2utf==(iconv_t)-1) { log_error(i->id,"Charset error!"); return; } } _utf2win = iconv_open(ti->charset,"UTF-8"); if (_utf2win ==(iconv_t)-1) { ti->charset = pstrdup(p,DEFAULT_CHARSET); _utf2win = iconv_open(ti->charset,"UTF-8"); if (_utf2win ==(iconv_t)-1) { log_error(i->id,"Charset error!"); return; } } log_notice("config","charset %s",ti->charset); ti->msg_chat = xmlnode_get_tag(config,"chat") ? 1 : 0; if (ti->msg_chat) { log_notice("config","chat messages enabled"); } ti->web_aware = xmlnode_get_tag(config,"web") ? 1 : 0; if (ti->web_aware) { log_notice("config","web presence enabled"); } ti->own_roster = xmlnode_get_tag(config,"own_roster") ? 1 : 0; if (ti->own_roster) { log_notice("config","JIT will use own roster"); } ti->no_jabber_roster = xmlnode_get_tag(config,"no_jabber_roster") ? 1 : 0; if (ti->no_jabber_roster) { log_notice("config","JIT willn't get users from jabber roster"); } ti->no_x_data = xmlnode_get_tag(config,"no_xdata") ? 1 : 0; if (ti->no_x_data) { log_notice("config","JIT will not use xdata"); } cur = xmlnode_get_tag(config,"sms"); if (cur) { ti->sms_id = pstrdup(p,xmlnode_get_tag_data(cur,"host")); if (ti->sms_id) { ti->sms_show = jit_show2status(xmlnode_get_tag_data(cur,"show")); if (ti->sms_show==ICQ_STATUS_NOT_IN_LIST) { ti->sms_show = ICQ_STATUS_ONLINE; } log_notice("config","sms host %s show: %d",ti->sms_id,ti->sms_show); ti->sms_status = pstrdup(p,xmlnode_get_tag_data(cur,"status")); if (ti->sms_status) { log_debug(ZONE,"sms st %s ",ti->sms_status); } ti->sms_name = pstrdup(p,xmlnode_get_tag_data(cur,"name")); if (ti->sms_name) { log_debug(ZONE,"sms name %s",ti->sms_name); } } } ti->count_file = pstrdup(p,xmlnode_get_tag_data(config,"user_count_file")); if (ti->count_file == NULL) { ti->count_file = "icqcount"; } log_notice("config","Using %s as count log file",ti->count_file); for (cur = xmlnode_get_firstchild(xmlnode_get_tag(config,"server")); cur != NULL; cur = xmlnode_get_nextsibling(cur)) { char * port; char * host; if (xmlnode_get_type(cur) != NTYPE_TAG) continue; if ((port = xmlnode_get_attrib(cur,"port")) == NULL) continue; if ((host = xmlnode_get_data(cur)) == NULL) continue; ti->auth_hosts[ti->auth_hosts_count] = pstrdup(p,host); ti->auth_ports[ti->auth_hosts_count] = j_atoi(port,5190); log_debug(ZONE,"Host %s port %d at pos %d", ti->auth_hosts[ti->auth_hosts_count], ti->auth_ports[ti->auth_hosts_count], ti->auth_hosts_count); ti->auth_hosts_count++; if (ti->auth_hosts_count >= MAX_AUTH_HOSTS) break; } if (ti->auth_hosts_count == 0) { log_alert("err","No hosts to auth icq client !. Using default"); ti->auth_hosts[ti->auth_hosts_count] = pstrdup(p,"205.188.179.233"); ti->auth_ports[ti->auth_hosts_count] = 5190; ti->auth_hosts_count++; } /* add queue for unknown packets */ ti->q = mtq_new(i->p); ti->sessions = wpxhash_new(j_atoi(xmlnode_get_tag_data(config,"prime"),509)); ti->sessions_alt = wpxhash_new(j_atoi(xmlnode_get_tag_data(config,"prime"),509)); SEM_INIT(ti->sessions_sem); ti->vcard = xmlnode_new_tag_pool(p,"vCard"); xmlnode_put_attrib(ti->vcard,"xmlns",NS_VCARD); xmlnode_insert_node(ti->vcard,xmlnode_get_firstchild(xmlnode_get_tag(config,"vCard"))); /* default 5 hours */ ti->session_timeout = j_atoi(xmlnode_get_tag_data(config,"session_timeout"),18000); log_notice("config","session_timeout in sec : %d",ti->session_timeout); ti->reconnect = j_atoi(xmlnode_get_tag_data(config,"reconnects"),0); log_notice("config","Number of reconnects for session %d",ti->reconnect); check = j_atoi(xmlnode_get_tag_data(config,"session_check"),10); log_notice("config","JIT will check session every %d sec",check); // ti->admin = xmlnode_dup(xmlnode_get_tag(config,"admin")); ti->start = time(NULL); /* Register callbacks */ register_phandler(i,o_DELIVER,it_receive,(void *) ti); register_shutdown(it_shutdown,(void *) ti); /* Start up heartbeat thread */ register_beat(check,it_sessions_check,(void *) ti); xmlnode_free(config); }
void dnsrv(instance i, xmlnode x) { xdbcache xc = NULL; xmlnode config = NULL; xmlnode iternode = NULL; dns_resend_list tmplist = NULL; /* Setup a struct to hold dns_io handles */ dns_io di; di = pmalloco(i->p, sizeof(_dns_io)); di->mempool = i->p; /* Load config from xdb */ xc = xdb_cache(i); config = xdb_get(xc, jid_new(xmlnode_pool(x), "config@-internal"), "jabber:config:dnsrv"); /* Build a list of services/resend hosts */ iternode = xmlnode_get_lastchild(config); while (iternode != NULL) { if (j_strcmp("resend", xmlnode_get_name(iternode)) != 0) { iternode = xmlnode_get_prevsibling(iternode); continue; } /* Allocate a new list node */ tmplist = pmalloco(di->mempool, sizeof(_dns_resend_list)); tmplist->service = pstrdup(di->mempool, xmlnode_get_attrib(iternode, "service")); tmplist->host = pstrdup(di->mempool, xmlnode_get_data(iternode)); /* Insert this node into the list */ tmplist->next = di->svclist; di->svclist = tmplist; /* Move to next child */ iternode = xmlnode_get_prevsibling(iternode); } log_debug(ZONE, "dnsrv debug: %s\n", xmlnode2str(config)); /* Setup the hash of dns_packet_list */ di->packet_table = xhash_new(j_atoi(xmlnode_get_attrib(config,"queuemax"),101)); di->packet_timeout = j_atoi(xmlnode_get_attrib(config,"queuetimeout"),60); register_beat(di->packet_timeout, dnsrv_beat_packets, (void *)di); /* Setup the internal hostname cache */ di->cache_table = xhash_new(j_atoi(xmlnode_get_attrib(config,"cachemax"),1999)); di->cache_timeout = j_atoi(xmlnode_get_attrib(config,"cachetimeout"),3600); /* 1 hour dns cache? XXX would be nice to get the right value from dns! */ xmlnode_free(config); /* spawn a thread that get's forked, and wait for it since it sets up the fd's */ pth_join(pth_spawn(PTH_ATTR_DEFAULT,(void*)dnsrv_thread,(void*)di),NULL); if(di->pid < 0) { log_error(i->id,"dnsrv failed to start, unable to fork and/or create pipes"); return; } /* Start IO thread */ pth_spawn(PTH_ATTR_DEFAULT, dnsrv_process_io, di); /* Register an incoming packet handler */ register_phandler(i, o_DELIVER, dnsrv_deliver, (void*)di); /* register a cleanup function */ pool_cleanup(i->p, dnsrv_shutdown, (void*)di); }