Пример #1
0
void debug_configure() // this function should be called after configuration is settled
{
    char *debugfile = conf_string(cf_debug_file);
    char *debuglevel =  conf_string(cf_debug_level);
    unsigned long n_debuglevel;

    if (debugfile!=NULL) {
	if( strcmp(debug_name, debugfile)!=0 ) {
	
            if( debug_isopened() ) debug_close();
	
	/* Reset ignore flag */
	    if( debug_invalid_name )
		debug_invalid_name = FALSE;
	
	    strncpy(debug_name, debugfile, sizeof(debug_name));
	}
    }

    _debug_parsestring(debuglevel, &n_debuglevel);

    if( n_debuglevel != debug_current_debuglevel ) {
		log("changing debug level from 0x%08x to 0x%08x",
			debug_current_debuglevel, n_debuglevel);
		debug_current_debuglevel = n_debuglevel;
    }
}
Пример #2
0
static void terminate(void)
{
	/* got SIGINT, cleanup and exit */
	syslog(LOG_INFO, "terminated (SIGINT)");
	dbg("got SIGINT, exiting\n");
	debug_close();
	pthread_exit(NULL);
}
Пример #3
0
void
debug_mprintf (mchar* fmt, ...)
{
    int was_open = 1;
    va_list argptr;
    int rc;
    mchar mbuf[DEBUG_BUF_LEN];
    char cbuf[DEBUG_BUF_LEN];

    if (!debug_on) return;

    if (!debug_initialized) {
        m_debug_lock = threadlib_create_sem();
        threadlib_signal_sem(&m_debug_lock);
    }
    threadlib_waitfor_sem (&m_debug_lock);

    va_start (argptr, fmt);
    if (!gcsfp) {
	was_open = 0;
	debug_open();
	if (!gcsfp) return;
    }
    if (!debug_initialized) {
	debug_initialized = 1;
	fprintf (gcsfp, "=========================\n");
	fprintf (gcsfp, "STREAMRIPPER " SRPLATFORM " " SRVERSION "\n");
    }

#if defined HAVE_WCHAR_SUPPORT
    rc = vswprintf (mbuf, DEBUG_BUF_LEN, fmt, argptr);
    debug_on = 0;   /* Avoid recursive call which hangs on semaphore */
    rc = string_from_mstring (cbuf, DEBUG_BUF_LEN, mbuf, CODESET_LOCALE);
    debug_on = 1;
#else
    rc = vsnprintf (cbuf, DEBUG_BUF_LEN, fmt, argptr);
#endif

    fwrite (cbuf, 1, rc, gcsfp);

    fflush (gcsfp);

    va_end (argptr);
    if (!was_open) {
	debug_close ();
    }
    threadlib_signal_sem (&m_debug_lock);
}
Пример #4
0
debug_printf (char* fmt, ...)
#endif
{
    int was_open = 1;
    va_list argptr;

    if (!debug_on) {
	/* Uncomment to debug debug_mprintf() */
#if defined (commentout)
	va_start (argptr, fmt);
        vprintf (fmt, argptr);
	va_end (argptr);
#endif
	return;
    }

    if (!debug_initialized) {
        m_debug_lock = threadlib_create_sem();
        threadlib_signal_sem(&m_debug_lock);
    }
    threadlib_waitfor_sem (&m_debug_lock);

    va_start (argptr, fmt);
    if (!gcsfp) {
	was_open = 0;
	debug_open();
	if (!gcsfp) return;
    }
    if (!debug_initialized) {
	debug_initialized = 1;
	fprintf (gcsfp, "=========================\n");
	fprintf (gcsfp, "STREAMRIPPER " SRPLATFORM " " SRVERSION "\n");
    }

    vfprintf (gcsfp, fmt, argptr);
    fflush (gcsfp);

    va_end (argptr);
    if (!was_open) {
	debug_close ();
    }
    threadlib_signal_sem (&m_debug_lock);
}
Пример #5
0
static void
debug_backtrace(int signum)
{
	switch (signum) {
		case SIGSEGV:
			SYS_FATAL("Received SIGSEGV:\n");
			break;
		case SIGABRT:
			SYS_FATAL("Received SIGABRT:\n");
			break;
		default:
			SYS_FATAL("Received signal %d\n", signum);
			break;
	}
	print_backtrace(fdebug_out);

	debug_close();

	signal(signum, SIG_DFL);
	raise(signum);
}
Пример #6
0
void debug_printf(char *fmt, ...)
{
	int was_open = 1;
	va_list argptr;

	if (!debug_on) {
		return;
	}

	if (!debug_initialized) {
		m_debug_lock = threadlib_create_sem();
		threadlib_signal_sem(&m_debug_lock);
	}
	threadlib_waitfor_sem(&m_debug_lock);

	va_start(argptr, fmt);
	if (!gcsfp) {
		was_open = 0;
		debug_open();
		if (!gcsfp)
			return;
	}
	if (!debug_initialized) {
		debug_initialized = 1;
		fprintf(gcsfp, "=========================\n");
		fprintf(gcsfp, "STREAMRIPPER " SRPLATFORM " " SRVERSION "\n");
	}

	vfprintf(gcsfp, fmt, argptr);
	fflush(gcsfp);

	va_end(argptr);
	if (!was_open) {
		debug_close();
	}
	threadlib_signal_sem(&m_debug_lock);
}
Пример #7
0
int main(int argc, char *argv[], char *envp[])
{
	s_bforce_opts opts;
	int rc = 0;
	int ch = 0;
	opts.runmode = MODE_UNDEFINED;
	
	memset(&opts, '\0', sizeof(s_bforce_opts));
	
	// parsing
	
	while( (ch=getopt(argc, argv, "hfI:p:n:l:a:u:oiC:S:dq")) != EOF )
	{
		switch( ch ) {
		case 'h':
			usage();
			exit(BFERR_NOERROR);
		case 'f':
			if( opts.runmode == MODE_UNDEFINED ) opts.runmode = MODE_CALL_MODEM;
			if( opts.runmode != MODE_CALL_MODEM || opts.force ) { usage(); exit(BFERR_FATALERROR); }
			opts.force = 1;
			break;
		case 'I':
			if( opts.incname || !optarg ) { usage(); exit(BFERR_FATALERROR); }  //free(opts.incname);
			opts.incname = (char *)xstrcpy(optarg);
			break;
		case 'p':
			if( opts.runmode == MODE_UNDEFINED ) opts.runmode = MODE_CALL_MODEM;
			if( opts.runmode != MODE_CALL_MODEM || opts.device || !optarg ) { usage(); exit(BFERR_FATALERROR); }
			opts.device = (char *)xstrcpy(optarg);
			break;
		case 'n':
			if( opts.runmode == MODE_UNDEFINED ) opts.runmode = MODE_CALL_MODEM;
			if( opts.runmode != MODE_CALL_MODEM || opts.phone || !optarg ) { usage(); exit(BFERR_FATALERROR); }
			//if( opts.phone ) free(opts.phone);
			opts.phone = (char *)xstrcpy(optarg);
			break;
		case 'l':
			if( opts.runmode == MODE_UNDEFINED ) opts.runmode = MODE_CALL_MODEM;
			if( opts.runmode != MODE_CALL_MODEM || opts.hiddline || !optarg || ISDEC(optarg) ) { usage(); exit(BFERR_FATALERROR); }
			opts.hiddline = atoi(optarg);
			break;
		case 'a':
			if( opts.runmode == MODE_UNDEFINED ) opts.runmode = MODE_CALL_IP;
			if( opts.runmode != MODE_CALL_IP || opts.iphost || !optarg ) { usage(); exit(BFERR_FATALERROR); }
			opts.iphost = (char *)xstrcpy(optarg);
			break;
		case 'u':
			if( opts.runmode == MODE_UNDEFINED ) opts.runmode = MODE_CALL_IP;
			if( opts.runmode != MODE_CALL_IP || opts.ipproto || !optarg ) { usage(); exit(BFERR_FATALERROR); }
			opts.ipproto = (char *)xstrcpy(optarg);
			break;
		case 'o':
			if( opts.runmode == MODE_UNDEFINED ) opts.runmode = MODE_CALL_STDIO;
			if( opts.runmode != MODE_CALL_STDIO || opts.usestdio ) { usage(); exit(BFERR_FATALERROR); }
			opts.usestdio = TRUE;
			break;
		case 'i':
			//if( opts.runmode == MODE_UNDEFINED ) opts.runmode = MODE_ANSWER;
			//if( opts.runmode != MODE_ANSWER || opts.inetd ) { usage(); exit(BFERR_FATALERROR); }
			opts.inetd = 1;
			break;
		case 'C':
			if( opts.confname || !optarg ) { usage(); exit(BFERR_FATALERROR); }
			opts.confname = (char *)xstrcpy(optarg);
			break;
		case 'S':
			if( opts.runmode == MODE_UNDEFINED ) opts.runmode = MODE_ANSWER;
			if( opts.runmode != MODE_ANSWER || opts.connect || !optarg ) { usage(); exit(BFERR_FATALERROR); }
			opts.connect = (char *)xstrcpy(optarg);
			break;
		case 'd':
			if( opts.runmode == MODE_UNDEFINED ) opts.runmode = MODE_DAEMON;
			if( opts.runmode != MODE_DAEMON || opts.daemon || opts.quit ) { usage(); exit(BFERR_FATALERROR); }
			opts.daemon = 1;
			break;
		case 'q':
			if( opts.runmode == MODE_UNDEFINED ) opts.runmode = MODE_DAEMON;
			if( opts.runmode != MODE_DAEMON || opts.quit ) { usage(); exit(BFERR_FATALERROR); }
			opts.quit = 1;
			opts.daemon = 1;
			break;
		default :
			usage();
			exit(BFERR_FATALERROR);
		}
	}
	
	
	/* Expression checker use it, so init first */
	init_state(&state);
	
	/* Set space available for process title */
#ifndef HAVE_SETPROCTITLE
	setargspace(argv, envp);
#endif
	
	/* Initialise random number generation */
	(void)srand((unsigned)time(NULL));
	
	/* Initialise current locale */
	(void)setlocale(LC_ALL, "");
	
	/* Set secure process umask */
	(void)umask(~(S_IRUSR|S_IWUSR));
	
	/* Now process non-option arguments */
	if( opts.daemon == FALSE )
	{
		const char *p;
		s_faddr addr;
		s_falist **alist = &opts.addrlist;
		
		while( (optind < argc) && (p = argv[optind++]) )
		{
			for( ; *p == '*'; p++ );
		
			if( strcasecmp(p, "tsync") == 0 )
			{
				if( opts.runmode == MODE_UNDEFINED ) opts.runmode = MODE_ANSWER;
				if( opts.runmode != MODE_ANSWER || opts.stype ) { usage(); exit(BFERR_FATALERROR); }
				opts.stype = SESSION_FTSC;
			}
			else if( strcasecmp(p, "yoohoo") == 0 )
			{
				if( opts.runmode == MODE_UNDEFINED ) opts.runmode = MODE_ANSWER;
				if( opts.runmode != MODE_ANSWER || opts.stype ) { usage(); exit(BFERR_FATALERROR); }
				opts.stype = SESSION_YOOHOO;
			}
			else if( strcasecmp(p, "**EMSI_INQC816") == 0 )
			{
				if( opts.runmode == MODE_UNDEFINED ) opts.runmode = MODE_ANSWER;
				if( opts.runmode != MODE_ANSWER || opts.stype ) { usage(); exit(BFERR_FATALERROR); }
				opts.stype = SESSION_EMSI;
			}
			else if( strncasecmp(p, "emsi", 4) == 0 )
			{
				if( opts.runmode == MODE_UNDEFINED ) opts.runmode = MODE_ANSWER;
				if( opts.runmode != MODE_ANSWER || opts.stype ) { usage(); exit(BFERR_FATALERROR); }
				opts.stype = SESSION_EMSI;
			}
			else if( strcasecmp(p, "binkp") == 0 )
			{
				if( opts.runmode == MODE_UNDEFINED ) opts.runmode = MODE_ANSWER;
				if( opts.runmode != MODE_ANSWER || opts.stype ) { usage(); exit(BFERR_FATALERROR); }
				opts.stype = SESSION_BINKP;
			}
			else if( strcasecmp(p, "auto") == 0 )
			{
				if( opts.runmode == MODE_UNDEFINED ) opts.runmode = MODE_ANSWER;
				if( opts.runmode != MODE_ANSWER || opts.stype ) { usage(); exit(BFERR_FATALERROR); }
				opts.stype = SESSION_UNKNOWN;
			}
			else if( ftn_addrparse(&addr, p, FALSE) == 0 )
			{
				if( opts.runmode == MODE_UNDEFINED ) opts.runmode = MODE_CALL_DEFAULT;
				if( opts.runmode != MODE_CALL_DEFAULT && opts.runmode != MODE_CALL_IP &&
				    opts.runmode != MODE_CALL_MODEM && opts.runmode != MODE_CALL_STDIO ) { usage(); exit(BFERR_FATALERROR); }
				(*alist) = (s_falist*)xmalloc(sizeof(s_falist));
				memset(*alist, '\0', sizeof(s_falist));
				(*alist)->addr = addr;
				alist = &(*alist)->next;
			}
			else
			{
				printf("invalid address \"%s\"", p);
				usage();
				exit(BFERR_FATALERROR);
			}
		}
	}

	/*
	  if( opts.inetd && opts.runmode != MODE_ANSWER && opts.runmode != MODE_CALL_STDIO )
	  {
	  usage();
	  exit(BFERR_FATALERROR);
	  }
	*/


/*	if( (rc = log_open(BFORCE_LOGFILE, NULL, NULL)) ) //compiled in
	{
		log("can't continue without logging");
		gotoexit(BFERR_FATALERROR);
	}*/
	
	/* Process primary config file */
	if( opts.confname && *opts.confname )
		rc = conf_readconf(opts.confname, 0);
	else
		rc = conf_readconf(conf_getconfname(), 0);
	
	if( rc ) gotoexit(BFERR_FATALERROR);
	
	/* Process additional config file, ignore errors */
	if( opts.incname && *opts.incname )
		(void)conf_readconf(opts.incname, 1);
	
	/* Reopen log file if it was defined in config */
	if( log_reopen(log_getfilename(LOG_FILE_SESSION), NULL, NULL) )
	{
		log("can't continue without logging");
		gotoexit(BFERR_FATALERROR);
	}
	
	//char runmode_str[21];
	//snprintf(runmode_str, 20, "Run mode: %d", opts.runmode);
	//log(runmode_str);
	
	switch( opts.runmode )
	{
case MODE_DAEMON:
		log("Daemon mode");
		rc = bforce_daemon(&opts);
		break;
case MODE_CALL_DEFAULT:
case MODE_CALL_IP:
case MODE_CALL_MODEM:
case MODE_CALL_STDIO:
		log("Outgoing call");
		rc = bforce_master(&opts);
		break;
case MODE_ANSWER:
		log("Start answer");
		rc = bforce_slave(&opts);
		break;
default:
		log("Could not determine run mode");
	}

exit:
	
	deinit_conf();
	deinit_opts(&opts);
	
	/* Shutdown logging services */
	if( log_isopened() ) log_close();
        DEB((D_FREE, "good exit"));
#ifdef DEBUG
	if( debug_isopened() ) debug_close();
#endif
	exit(rc);
}
Пример #8
0
int main(int argc, char **argv)
{
	pthread_t sigth;
	sigset_t sigblock;
	int logflags = 0;
	int ret = 1;

	debug_init();

	sigemptyset(&sigblock);
	sigaddset(&sigblock, SIGHUP);
	sigaddset(&sigblock, SIGINT);
	sigaddset(&sigblock, SIGTERM);
#ifdef ENABLE_VT
	sigaddset(&sigblock, SIGPIPE);
#endif
	pthread_sigmask(SIG_BLOCK, &sigblock, NULL);

	if (conf_parse(&conf, argc, argv))
		return 1;

	if (conf.debug_level > 0)
		logflags = LOG_PERROR;

	openlog(basename(argv[0]), LOG_PID|logflags, LOG_DAEMON);

	syslog(LOG_INFO, "%s v%s started (%s)", PACKAGE_NAME, PACKAGE_VERSION,
	       entity_string[conf.mip6_entity]);
#ifdef ENABLE_VT
	if (vt_init() < 0)
		goto vt_failed;
#endif

	/* if not debugging, detach from tty */
	if (conf.debug_level == 0)
		daemon_start(1);
	else {
		/* if debugging with debug log file, detach from tty */
		if (conf.debug_log_file) {
			daemon_start(1);

			ret = debug_open(conf.debug_log_file);
			if (ret < 0) {
				fprintf(stderr, "can't init debug log:%s\n",
					strerror(-ret));
				goto debug_failed;
			}
			dbg("%s started in debug mode\n", PACKAGE_NAME);
		} else {
			dbg("%s started in debug mode, not detaching from terminal\n",
			    PACKAGE_NAME);
		}
		conf_show(&conf);
	}

	srandom(time(NULL));

	if (rr_cn_init() < 0)
		goto rr_cn_failed;
	if (policy_init() < 0)
		goto policy_failed;
	if (taskqueue_init() < 0)
		goto taskqueue_failed;
	if (bcache_init() < 0)
		goto bcache_failed;
	if (mh_init() < 0)
		goto mh_failed;
	if (icmp6_init() < 0)
		goto icmp6_failed;
	if (xfrm_init() < 0)
		goto xfrm_failed;
	cn_init();
	if ((is_ha() || is_mn()) && tunnelctl_init() < 0)
		goto tunnelctl_failed;
	if (is_ha() && ha_init() < 0) 
		goto ha_failed;
	if (is_mn() && mn_init() < 0)
		goto mn_failed;
#ifdef ENABLE_VT
	if (vt_start(conf.vt_hostname, conf.vt_service) < 0)
		goto vt_start_failed;
#endif
	if (pthread_create(&sigth, NULL, sigh, NULL))
		goto sigth_failed;
	pthread_join(sigth, NULL);
	ret = 0;
sigth_failed:
#ifdef ENABLE_VT
	vt_fini();
vt_start_failed:
#endif
	if (is_mn())
		mn_cleanup();
mn_failed:
	if (is_ha())
		ha_cleanup();
ha_failed:
	if (is_ha() || is_mn())
		tunnelctl_cleanup();
tunnelctl_failed:
	cn_cleanup();
	xfrm_cleanup();
xfrm_failed:
	icmp6_cleanup();
icmp6_failed:
	mh_cleanup();
mh_failed:
	bcache_cleanup();
bcache_failed:
	taskqueue_destroy();
taskqueue_failed:
	policy_cleanup();
policy_failed:
rr_cn_failed:
	debug_close();
debug_failed:
#ifdef ENABLE_VT
vt_failed:
#endif
	syslog(LOG_INFO, "%s v%s stopped (%s)", PACKAGE_NAME, PACKAGE_VERSION,
	       entity_string[conf.mip6_entity]);
	closelog();
	return ret;
}