1 /*
2  *	binkleyforce -- unix FTN mailer project
3  *
4  *	Copyright (c) 1998-2000 Alexander Belkin, 2:5020/1398.11
5  *
6  *	This program is free software; you can redistribute it and/or modify
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  *	$Id: sess_answ.c,v 1.1.1.1 2004/09/09 09:52:39 kstepanenkov Exp $
12  */
13 
14 #include "includes.h"
15 #include "confread.h"
16 #include "logger.h"
17 #include "util.h"
18 #include "nodelist.h"
19 #include "io.h"
20 #include "session.h"
21 
answ_system(e_session type,char * connstr,int inetd)22 int answ_system(e_session type, char *connstr, int inetd)
23 {
24 	TIO oldtio;
25 	struct sockaddr_in client;
26 	int clientlen = sizeof(client);
27 	int rc = 0;
28 	char *p;
29 
30 	init_state(&state);
31 	state.session  = type;
32 	state.caller   = FALSE;
33 	state.valid    = TRUE;
34 
35 	/*
36 	 * Set verbal line name
37 	 */
38 	if( inetd )
39 	{
40 		state.linename = xstrcpy("tcpip");
41 		state.inet = TRUE;
42 	}
43 	else
44 		state.linename = isatty(0) ? port_get_name(ttyname(0)) : NULL;
45 
46 	if( !inetd )
47 	{
48 		if( tio_get_dcd(0) == 0 )
49 			bf_log("warning: DCD line is not active");
50 
51 		if( (p = getenv("CALLER_ID")) && *p && strcmp(p, "none") )
52 			state.cidstr = (char*)xstrcpy(p);
53 
54 		if( connstr && *connstr )
55 			state.connstr = (char*)xstrcpy(connstr);
56 		else if( (p = getenv("CONNECT")) && *p )
57 			state.connstr = (char*)xstrcpy(p);
58 
59 		if( state.connstr )
60 			state.connspeed = modem_getconnspeed(state.connstr);
61 	}
62 
63 	/*
64 	 * Open new log file with current line name as extension
65 	 */
66 	if( log_reopen(log_getfilename(LOG_FILE_SESSION), state.linename, NULL) )
67 	{
68 		bf_log("can't continue without logging");
69 		gotoexit(BFERR_FATALERROR);
70 	}
71 
72 #ifdef DEBUG
73 	(void)debug_setfilename(log_getfilename(LOG_FILE_DEBUG));
74 #endif
75 
76 	if( inetd )
77 	{
78 		if( connstr && *connstr )
79 			state.connstr = (char*)xstrcpy(connstr);
80 		else if( getpeername(0, (struct sockaddr*)&client, &clientlen) == -1 )
81 			logerr("can't get client address");
82 		else
83 		{
84 			state.peername = (char*)xstrcpy(inet_ntoa(client.sin_addr));
85 			state.peerport = (long)ntohs(client.sin_port);
86 		}
87 	}
88 
89 	if( inetd == 0 && state.cidstr )
90 	{
91 		setproctitle("bforce answering, CID: %.32s",
92 			string_printable(state.cidstr));
93 		bf_log("Caller-ID: \"%s\"",
94 			string_printable(state.cidstr));
95 	}
96 	else if( inetd && state.peername )
97 	{
98 		setproctitle("bforce answering, host %.32s:%ld",
99 			string_printable(state.peername), state.peerport);
100 		bf_log("TCP/IP connect from %s on port %ld",
101 			string_printable(state.peername), state.peerport);
102 	}
103 	else
104 	{
105 		setproctitle("bforce answering");
106 	}
107 
108 	if( state.connstr )
109 		bf_log("connect \"%s\" (%ld)", state.connstr, state.connspeed);
110 
111 	if( (inetd == 0 && (rc = port_init(0, 0, &oldtio, FALSE)) == 0)
112 	 || (inetd == 1 && (rc = tcpip_init()) == 0) )
113 	{
114 		port_carrier(0, TRUE);
115 
116 		rc = session();
117 
118 		if( !inetd )
119 		{
120 			port_deinit(0, &oldtio);
121 			port_close();
122 		}
123 	}
124 	else
125 		rc = BFERR_FATALERROR;
126 
127 exit:
128 	out_bsy_unlockall();
129 
130 	bf_log("session rc = %d (\"%s\")", rc, BFERR_NAME(rc));
131 
132 	if( state.node.addr.zone > 0 )
133 		(void)session_stat_update(&state.node.addr,
134 				&state.sess_stat, FALSE, rc);
135 
136 	deinit_state(&state);
137 	return(rc);
138 }
139