aboutsummaryrefslogtreecommitdiff
path: root/src/transmode.c
blob: 0cf12f497a07564adc38b58736446f509ca3fca2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/*
   transmode.c

   This file is part of GNU Anubis.
   Copyright (C) 2003-2014 The Anubis Team.

   GNU Anubis is free software; you can redistribute it and/or modify it
   under the terms of the GNU General Public License as published by the
   Free Software Foundation; either version 3 of the License, or (at your
   option) any later version.

   GNU Anubis is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License along
   with GNU Anubis.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "headers.h"
#include "extern.h"

static unsigned long 
string_to_ipaddr (const char *str)
{
  unsigned long inaddr;
  struct sockaddr_in ad;

  memset (&ad, 0, sizeof (ad));
  inaddr = inet_addr (str);
  if (inaddr != INADDR_NONE)
    memcpy (&ad.sin_addr, &inaddr, sizeof (inaddr));
  else
    {
      struct hostent *hp = 0;
      hp = gethostbyname (str);
      if (hp == 0)
	hostname_error (str);
      else
	{
	  if (hp->h_length != 4 && hp->h_length != 8)
	    {
	      anubis_error (EXIT_FAILURE, 0,
			    _("Illegal address length received for host %s"),
			    str);
	    }
	  else
	    memcpy ((char *) &ad.sin_addr.s_addr, hp->h_addr, hp->h_length);
	}
    }

  return inaddr;
}

void
session_prologue ()
{
  ASSERT_MTA_CONFIG ();
  if (!(topt & T_LOCAL_MTA)
      && session.anubis 
      && string_to_ipaddr (session.mta) == string_to_ipaddr (session.anubis)
      && session.anubis_port == session.mta_port)
    anubis_error (EXIT_FAILURE, 0, _("remote-mta loops back to Anubis"));
  
  alarm (300);
  if (topt & T_LOCAL_MTA)
    {
      remote_server = make_local_connection (session.execpath,
					     session.execargs);
      if (!remote_server)
	{
	  service_unavailable (&remote_client);
	  return;
	}
    }
  else
    {
      remote_server = make_remote_connection (session.mta, session.mta_port);
      if (!remote_server)
	service_unavailable (&remote_client);
    }
  
  alarm (900);
}

int
anubis_transparent_mode (struct sockaddr_in *addr)
{
  int rs = 0;
  int cs = 0;

  rs = auth_ident (addr, &session.clientname);

  if ((topt & T_DROP_UNKNOWN_USER) && !rs)
    {
      service_unavailable (&remote_client);
      return 0;
    }

  parse_transmap (&cs,
		  rs ? session.clientname : 0,
		  inet_ntoa (addr->sin_addr),
		  &session.clientname);

  if (cs == 1)
    {
      anubis_changeowner (session.clientname);
    }
  else if (rs && cs == -1 && ntohl (addr->sin_addr.s_addr) == INADDR_LOOPBACK)
    {
      if (check_username (session.clientname))
	anubis_changeowner (session.clientname);
      else
	set_unprivileged_user ();
    }
  else
    set_unprivileged_user ();

  auth_tunnel ();

  session_prologue ();
  smtp_session_transparent ();
  alarm (0);

  net_close_stream (&remote_server);
  net_close_stream (&remote_client);

  info (NORMAL, _("Connection closed successfully."));

#ifdef HAVE_PAM
  if (pamh)
    {
      int pam_retval = pam_close_session (pamh, 0);
      if (pam_retval == PAM_SUCCESS)
	info (VERBOSE, _("PAM: Session closed."));
      if (pam_end (pamh, pam_retval) != PAM_SUCCESS)
	{
	  pamh = NULL;
	  info (NORMAL, _("PAM: failed to release authenticator."));
	  return EXIT_FAILURE;
	}
    }
#endif /* HAVE_PAM */
  return 0;
}

int
anubis_proxy_mode (struct sockaddr_in *addr)
{
  ASSERT_MTA_CONFIG ();

  set_unprivileged_user ();

  info (NORMAL, _("Initiated proxy mode."));
  session_prologue ();
  smtp_session_transparent ();
  alarm (0);

  net_close_stream (&remote_server);
  net_close_stream (&remote_client);

  info (NORMAL, _("Connection closed successfully."));
  return 0;
}

/* EOF */

Return to:

Send suggestions and report system problems to the System administrator.