Exemplo n.º 1
0
ProxyContext * proxy_context_init(void)
{
    struct ProxyContext * proxy_context;
    if ((proxy_context = malloc(sizeof(struct ProxyContext))) == NULL)
        return NULL;
	memset(proxy_context,0, sizeof(proxy_context));
	
    proxy_context->keep_alive = 0;
    proxy_context->client_fd = -1;
    proxy_context->server_fd = -1;
    proxy_context->socksize = sizeof(struct sockaddr_in);
    proxy_context->server_buf = linebuf_init(MAXFILENAME);
    proxy_context->client_buf = linebuf_init(MAXFILENAME);

    proxy_context->params   = paramlist_init();

    proxy_context->now = time(NULL);
    proxy_context->noop = 0;
    proxy_context->processed_mail_count = 0;

	proxy_context->protocol = (char*)malloc(32);
	assert(proxy_context->protocol!=NULL);
	
    proxy_context->filename = NULL;



    proxy_context->process_state = INIT;

    proxy_context->account_username = NULL;

    proxy_context->scannerinit = 1;
    proxy_context->bytecount = 0;

    proxy_context->ssl_enable = 0;


    proxy_context->ssl = NULL;
    proxy_context->ssl_server = NULL;

    proxy_context->ctx = NULL;
    proxy_context->ctx_server = NULL;
    proxy_context->sbio = NULL;
    proxy_context->sbio_server = NULL;
    proxy_context->hdrfrom = 0;
    proxy_context->hdrto = 0;
    // added by wfx
    proxy_context->hdrdate = 0;
    proxy_context->action = 0;

    proxy_context->recv_mail_state = 0;

    //proxy_context->mailhdr_fd   = -1;
    return proxy_context;
}
Exemplo n.º 2
0
static int trie_search_f(sd_hwdb *hwdb, const char *search) {
        struct linebuf buf;
        const struct trie_node_f *node;
        size_t i = 0;
        int err;

        linebuf_init(&buf);

        node = trie_node_from_off(hwdb, hwdb->head->nodes_root_off);
        while (node) {
                const struct trie_node_f *child;
                size_t p = 0;

                if (node->prefix_off) {
                        uint8_t c;

                        for (; (c = trie_string(hwdb, node->prefix_off)[p]); p++) {
                                if (IN_SET(c, '*', '?', '['))
                                        return trie_fnmatch_f(hwdb, node, p, &buf, search + i + p);
                                if (c != search[i + p])
                                        return 0;
                        }
                        i += p;
                }

                child = node_lookup_f(hwdb, node, '*');
                if (child) {
                        linebuf_add_char(&buf, '*');
                        err = trie_fnmatch_f(hwdb, child, 0, &buf, search + i);
                        if (err < 0)
                                return err;
                        linebuf_rem_char(&buf);
                }

                child = node_lookup_f(hwdb, node, '?');
                if (child) {
                        linebuf_add_char(&buf, '?');
                        err = trie_fnmatch_f(hwdb, child, 0, &buf, search + i);
                        if (err < 0)
                                return err;
                        linebuf_rem_char(&buf);
                }

                child = node_lookup_f(hwdb, node, '[');
                if (child) {
                        linebuf_add_char(&buf, '[');
                        err = trie_fnmatch_f(hwdb, child, 0, &buf, search + i);
                        if (err < 0)
                                return err;
                        linebuf_rem_char(&buf);
                }

                if (search[i] == '\0') {
                        size_t n;

                        for (n = 0; n < le64toh(node->values_count); n++) {
                                err = hwdb_add_property(hwdb, trie_node_value(hwdb, node, n));
                                if (err < 0)
                                        return err;
                        }
                        return 0;
                }

                child = node_lookup_f(hwdb, node, search[i]);
                node = child;
                i++;
        }
        return 0;
}
Exemplo n.º 3
0
unsigned long send_mailfile(char * mailfile,  ProxyContext *p)
{

    struct LineBuffer *filebuf, *footbuf;
    int mailfd, footfd;
    int res = 0, sendret = 0, gotprd = 0, gottxt = 0, nogo = 0;
    unsigned long len = 0;
    char svrout[1];

    if ((mailfd = r_open2(mailfile, O_RDONLY )) < 0)
    {
        proxy_context_uninit(p);
        // Emergency: send_mailfile: Can't open client mail file.
        g_gateway_config->emergency = make_message("Can't open mailfile (%s)!", mailfile);
        do_log(LOG_EMERG, "ERR: Can't open mailfile (%s)!", mailfile);
        return 0;
    }
    filebuf = linebuf_init(16384);
    footbuf = linebuf_init(512);
    if (!filebuf)
    {
        r_close(mailfd);

        if (p->ssl_enable)
        {
            SSL_shutdown(p->ssl);
            /* 释放SSL */
            SSL_free(p->ssl);
        }

        r_close(p->server_fd);
        proxy_context_uninit(p);
        // Emergency: send_mailfile: Unable to get memory.
        g_gateway_config->emergency = "Could not allocate memory for sending mail!";
        do_log(LOG_EMERG, "ERR: Could not allocate memory for sending mail!");
    }

    gotprd = 0;
    /*  // advance to mailfd pointer to past data already sent:
        if (config->broken)
        {
            if (p->hdroffset && !p->gobogus)
            {
                while (p->hdroffset)
                {
                    res = getlinep3(mailfd, filebuf, NULL);
                    p->hdroffset--;
                }
            }
        }
        else
        {
            if (p->hdroffset)
            {
                lseek(mailfd, p->hdroffset, SEEK_SET);
            }
            // See if bogus headerline sent
            if (p->gobogus)
            {
                if (p->boguspos < 91)
                {
                    svrout[0] = BOGUSX[p->boguspos];
                    secure_write(p->client_fd, svrout, 1, p->ssl_server);
                    p->boguspos++;
                }
                //now close it
                writeline(p->client_fd, WRITELINE_LEADING_RN, PERIOD, p->ssl_server);
                p->gobogus = 0;
            }
        }*/
    while (1)
    {
        /*
        sendret = checktimeout(p);
        if (sendret == GETLINE_PIPE)
        {
            do_log(LOG_CRIT, "ERR: Client disappeared during mail send!");
            linebuf_uninit(filebuf);
            return EPIPE;
        }
        else if (sendret)
        {
            context_uninit(p);
            linebuf_uninit(filebuf);
        // Emergency: send_mailfile: Sending mail to client
            g_gateway_config->emergency = "Sending mail to client";
            do_log(LOG_EMERG, "ERR: Sending mail to client");
            // we are dead now. Should not reach here. But allow it to fall through in case LOG_EMERG is changed in the future.
            return 1;
        }*/


        //to do check timeout

        if ((res = get_line(mailfd, filebuf)) < 0)
        {
            if (res == GETLINE_TOO_LONG)
            {
                // Buffer contains part of line,
                //take care of later
            }
            else
            {
                // Other error, take care of later
                break;
            }
        }
        if (filebuf->linelen >= 0 )
        {
            len += filebuf->linelen;
            //if (config->debug_message)
            do_log(LOG_DEBUG, ">%s", filebuf->line);

            if ((strncmp(filebuf->line, ".", 1 ) == 0 && strlen(filebuf->line) == 1))
                gotprd = 1;
            if ((strncmp(filebuf->line, ".\r", 2) == 0 && strlen(filebuf->line) == 2))
                gotprd = 1;

            /*
            //if ((strncmp(filebuf->line,"Content-Type: application/pgp-signature",39)==0 && strlen(filebuf->line)==39)) gotpgp=1;
            //if (gotpgp) nogo=1;
            if (strncmp(filebuf->line, "Content-Type: ", 14) == 0)
            {
                if ((strncmp(filebuf->line, "Content-Type: text/plain;", 25) == 0 && strlen(filebuf->line) == 25)) gottxt = 1;
                else nogo = 1;
            }
            */

            /*
            if ( gotprd && !nogo)
            {
                if ((footfd = r_open2(FOOTER, O_RDONLY)) >= 0)
                {
                    sendret = writeline(fd, WRITELINE_LEADING_RN, "**********", p->ssl_server);
                    while (1)
                    {
                        if ((sendret = getlinep3(footfd, footbuf, NULL)) < 0) break;
                        if (footbuf->linelen >= 0 ) sendret = writeline(fd, WRITELINE_LEADING_RN, footbuf->line, p->ssl_server);
                    }
                    r_close(footfd);
                    writeline_format(fd, NULL, WRITELINE_LEADING_RN, PROGRAM" "VERSION" running on %s.%s", paramlist_get(p->params, "%HOSTNAME%"), paramlist_get(p->params, "%DOMAINNAME%"));
                    sendret = writeline_format(fd, NULL, WRITELINE_LEADING_RN, "%s", paramlist_get(p->params, "%VDINFO%"));
                    sendret = writeline(fd, WRITELINE_LEADING_RN, "**********", p->ssl_server);
                }
            }*/

            // Take care of buffer here
            if (res == GETLINE_TOO_LONG)
            {

                if (p->ssl_enable)
                    sendret = write_line_ssl(p->ssl, WRITELINE_LEADING_NONE, filebuf->line);
                else
                    sendret = write_line(p->server_fd, WRITELINE_LEADING_NONE, filebuf->line);
            }
            else
            {
                if (!gotprd)
                {

                    if (p->ssl_enable)
                        sendret = write_line_ssl(p->ssl, WRITELINE_LEADING_RN, filebuf->line);
                    else
                        sendret = write_line(p->server_fd, WRITELINE_LEADING_RN, filebuf->line);
                }

            }


            if (sendret == GETLINE_PIPE)
            {
                do_log(LOG_CRIT, "ERR: Client disappeared during mail send!");
                linebuf_uninit(filebuf);
                return EPIPE;
            }
            else if (sendret)
            {
                proxy_context_uninit(p);
                linebuf_uninit(filebuf);
                // Emergency: send_mailfile: Sending mail to client
                g_gateway_config->emergency = "Sending mail to client";
                do_log(LOG_EMERG, "ERR: Sending mail to client");
                // we are dead now. Should not reach here. But allow it
                //to fall through in case LOG_EMERG is changed in the future.
                return 1;
            }
        }


    }

    if (res != GETLINE_EOF)
    {
        do_log(LOG_CRIT, "ERR: reading from mailfile %s, error code: %d", mailfile, res);
        linebuf_uninit(filebuf);
        return 1;
    }

    if (!gotprd)
    {
        do_log(LOG_DEBUG, "Wrote new EOM.");

        if (p->ssl_enable)
            write_line_ssl(p->ssl, WRITELINE_LEADING_RN, ".");
        else
            write_line(p->server_fd, WRITELINE_LEADING_RN, ".");
    }
    linebuf_uninit(filebuf);
    r_close(mailfd);
    return len;

}
Exemplo n.º 4
0
int
main(int argc, char *argv[])
{
	/* Check to see if the user is running us as root, which is a nono */
	if(geteuid() == 0)
	{
		fprintf(stderr, "Don't run ircd as root!!!\n");
		return -1;
	}

	/*
	 * save server boot time right away, so getrusage works correctly
	 */
	set_time();
	/*
	 * Setup corefile size immediately after boot -kre
	 */
	setup_corefile();

	/*
	 * set initialVMTop before we allocate any memory
	 */
	initialVMTop = get_vm_top();

	ServerRunning = 0;
	/* It ain't random, but it ought to be a little harder to guess */
	srand(SystemTime.tv_sec ^ (SystemTime.tv_usec | (getpid() << 20)));
	memset(&me, 0, sizeof(me));
	memset(&meLocalUser, 0, sizeof(meLocalUser));
	me.localClient = &meLocalUser;

	/* Make sure all lists are zeroed */
	memset(&unknown_list, 0, sizeof(unknown_list));
	memset(&lclient_list, 0, sizeof(lclient_list));
	memset(&serv_list, 0, sizeof(serv_list));
	memset(&global_serv_list, 0, sizeof(global_serv_list));
	memset(&oper_list, 0, sizeof(oper_list));

	dlinkAddTail(&me, &me.node, &global_client_list);

	memset((void *) &Count, 0, sizeof(Count));
	memset((void *) &ServerInfo, 0, sizeof(ServerInfo));
	memset((void *) &AdminInfo, 0, sizeof(AdminInfo));

	/* Initialise the channel capability usage counts... */
	init_chcap_usage_counts();

	ConfigFileEntry.dpath = DPATH;
	ConfigFileEntry.configfile = CPATH;	/* Server configuration file */
	ConfigFileEntry.klinefile = KPATH;	/* Server kline file */
	ConfigFileEntry.dlinefile = DLPATH;	/* dline file */
	ConfigFileEntry.xlinefile = XPATH;
	ConfigFileEntry.resvfile = RESVPATH;
	ConfigFileEntry.connect_timeout = 30;	/* Default to 30 */
	myargv = argv;
	umask(077);		/* better safe than sorry --SRB */

	parseargs(&argc, &argv, myopts);

	if(printVersion)
	{
		printf("ircd: version %s\n", ircd_version);
		exit(EXIT_SUCCESS);
	}

	if(chdir(ConfigFileEntry.dpath))
	{
		fprintf(stderr, "Unable to chdir to %s: %s\n", ConfigFileEntry.dpath, strerror(errno));
		exit(EXIT_FAILURE);
	}

	setup_signals();

#ifdef __CYGWIN__
	server_state_foreground = 1;
#endif

	if (testing_conf)
		server_state_foreground = 1;

	/* We need this to initialise the fd array before anything else */
	fdlist_init();
	if(!server_state_foreground)
	{
		comm_close_all();
	}


	/* Check if there is pidfile and daemon already running */
	if(!testing_conf)
	{
		check_pidfile(pidFileName);

		if(!server_state_foreground)
			make_daemon();
		else
			print_startup(getpid());
	}

	init_netio();		/* This needs to be setup early ! -- adrian */

	/* Init the event subsystem */
	eventInit();
	init_sys();

	init_main_logfile();
	initBlockHeap();
	init_dlink_nodes();
	init_patricia();
	newconf_init();
	init_s_conf();
	init_s_newconf();
	linebuf_init();		/* set up some linebuf stuff to control paging */
	init_hash();
	clear_scache_hash_table();	/* server cache name table */
	init_host_hash();
	clear_hash_parse();
	init_client();
	initUser();
	init_channels();
	initclass();
	initwhowas();
	init_stats();
	init_hook();
	init_reject();
	init_cache();
	init_monitor();
	load_all_modules(1);
#ifndef STATIC_MODULES
	load_core_modules(1);
#endif
	init_auth();		/* Initialise the auth code */
	init_resolver();	/* Needs to be setup before the io loop */

	if (testing_conf)
		fprintf(stderr, "\nBeginning config test\n");
	read_conf_files(YES);	/* cold start init conf files */
	rehash_bans(0);
#ifndef STATIC_MODULES

	mod_add_path(MODULE_DIR); 
	mod_add_path(MODULE_DIR "/autoload"); 
#endif

	initialize_server_capabs();	/* Set up default_server_capabs */
	initialize_global_set_options();

	if(ServerInfo.name == NULL)
	{
		fprintf(stderr, "ERROR: No server name specified in serverinfo block.\n");
		ilog(L_MAIN, "No server name specified in serverinfo block.");
		exit(EXIT_FAILURE);
	}
	strlcpy(me.name, ServerInfo.name, sizeof(me.name));

	if(ServerInfo.sid[0] == '\0')
	{
		fprintf(stderr, "ERROR: No server sid specified in serverinfo block.\n");
		ilog(L_MAIN, "No server sid specified in serverinfo block.");
		exit(EXIT_FAILURE);
	}
	strcpy(me.id, ServerInfo.sid);
	init_uid();

	/* serverinfo{} description must exist.  If not, error out. */
	if(ServerInfo.description == NULL)
	{
		fprintf(stderr, "ERROR: No server description specified in serverinfo block.\n");
		ilog(L_MAIN, "ERROR: No server description specified in serverinfo block.");
		exit(EXIT_FAILURE);
	}
	strlcpy(me.info, ServerInfo.description, sizeof(me.info));

	if (testing_conf)
	{
		fprintf(stderr, "\nConfig testing complete.\n");
		fflush(stderr);
		exit(EXIT_SUCCESS);
	}

	me.from = &me;
	me.servptr = &me;
	SetMe(&me);
	make_server(&me);
	me.serv->up = me.name;
	startup_time = CurrentTime;
	add_to_client_hash(me.name, &me);
	add_to_id_hash(me.id, &me);

	dlinkAddAlloc(&me, &global_serv_list);

	check_class();
	write_pidfile(pidFileName);
	load_help();
	open_logfiles();

	ilog(L_MAIN, "Server Ready");

	eventAddIsh("cleanup_glines", cleanup_glines, NULL, CLEANUP_GLINES_TIME);

	/* We want try_connections to be called as soon as possible now! -- adrian */
	/* No, 'cause after a restart it would cause all sorts of nick collides */
	/* um.  by waiting even longer, that just means we have even *more*
	 * nick collisions.  what a stupid idea. set an event for the IO loop --fl
	 */
	eventAddIsh("try_connections", try_connections, NULL, STARTUP_CONNECTIONS_TIME);
	eventAddOnce("try_connections_startup", try_connections, NULL, 0);

	eventAddIsh("collect_zipstats", collect_zipstats, NULL, ZIPSTATS_TIME);

	/* Setup the timeout check. I'll shift it later :)  -- adrian */
	eventAddIsh("comm_checktimeouts", comm_checktimeouts, NULL, 1);

	if(ConfigServerHide.links_delay > 0)
		eventAddIsh("cache_links", cache_links, NULL,
			    ConfigServerHide.links_delay);
	else
		ConfigServerHide.links_disabled = 1;

	if(splitmode)
		eventAdd("check_splitmode", check_splitmode, NULL, 2);

	ServerRunning = 1;

	io_loop();
	return 0;
}