aws_24.0.0_2b75fe6d/src/extended/aws-smtp-server.adb

  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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
------------------------------------------------------------------------------
--                              Ada Web Server                              --
--                                                                          --
--                     Copyright (C) 2008-2023, AdaCore                     --
--                                                                          --
--  This library is free software;  you can redistribute it and/or modify   --
--  it under terms of the  GNU General Public License  as published by the  --
--  Free Software  Foundation;  either version 3,  or (at your  option) any --
--  later version. This library 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.                    --
--                                                                          --
--  As a special exception under Section 7 of GPL version 3, you are        --
--  granted additional permissions described in the GCC Runtime Library     --
--  Exception, version 3.1, as published by the Free Software Foundation.   --
--                                                                          --
--  You should have received a copy of the GNU General Public License and   --
--  a copy of the GCC Runtime Library Exception along with this program;    --
--  see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see   --
--  <http://www.gnu.org/licenses/>.                                         --
--                                                                          --
--  As a special exception, if other files instantiate generics from this   --
--  unit, or you link this unit with other files to produce an executable,  --
--  this  unit  does not  by itself cause  the resulting executable to be   --
--  covered by the GNU General Public License. This exception does not      --
--  however invalidate any other reasons why the executable file  might be  --
--  covered by the  GNU Public License.                                     --
------------------------------------------------------------------------------

with Ada.Exceptions;
with Ada.Strings.Fixed;
with Ada.Strings.Maps;
with Ada.Text_IO;

with AWS.Headers;
with AWS.Net.Buffered;
with AWS.Net.SSL;
with AWS.SMTP.Messages.Set;

package body AWS.SMTP.Server is

   use Ada;

   --  Messages definitions

   DATA     : constant String := "DATA";
   HELO     : constant String := "HELO";
   MAIL     : constant String := "MAIL";
   QUIT     : constant String := "QUIT";
   RCPT     : constant String := "RCPT";
   STARTTLS : constant String := "STARTTLS";

   function Get_Message (Sock : Net.Socket_Type'Class) return Messages.Data;
   --  Get an incoming message from the server

   ------------------
   -- Mail_Handler --
   ------------------

   task body Mail_Handler is
      Message : Messages.Data;
   begin
      accept Start;

      --  Server.Host.Secure could be used only after accept Start because
      --  task created before Server.Host initialized.

      declare
         Sock : Net.Socket_Type'Class :=
                  Net.Socket (Security => Server.Host.Security = TLS);
      begin
         loop
            AWS.Net.Accept_Socket (Server.Host.Sock.all, Sock);

            Message := Get_Message (Sock);

            Server.Action (Message);

            Sock.Shutdown;
         end loop;
      end;

   exception
      when E : others =>
         if not Server.Shutdown then
            --  When server is shutdown we do not want to report errors. In
            --  some cases the socket layer won't end silently as the server
            --  socket is shutdown. See Shutdown routine below.
            Text_IO.Put_Line ("SMTP Server Fatal error");
            Text_IO.Put_Line (Exceptions.Exception_Information (E));
            raise;
         end if;
   end Mail_Handler;

   -----------------
   -- Get_Message --
   -----------------

   function Get_Message
     (Sock : Net.Socket_Type'Class) return Messages.Data
   is

      S : Net.Socket_Access := new Net.Socket_Type'Class'(Sock);

      Empty_Line : constant String := "";

      procedure Read_Message_Body;
      --  Read message body from the current socket and set the Message_Body
      --  Unbounded String below. A message body is terminated by the sequence
      --  CRLF & '.' & CRLF.

      Message      : Messages.Data;
      Message_Body : Unbounded_String;
      Headers      : AWS.Headers.List;

      -----------------------
      -- Read_Message_Body --
      -----------------------

      procedure Read_Message_Body is
      begin
         Read_Message_Header : loop
            declare
               Line        : constant String :=
                               Net.Buffered.Get_Line (S.all);
               Split_Point : Natural;
            begin
               exit Read_Message_Header when Line = Empty_Line;
               Split_Point := Strings.Fixed.Index (Line, ":");

               Headers.Add
                 (Name  => Line (Line'First .. Split_Point - 1),
                  Value => Line (Split_Point + 2 .. Line'Last));
            end;
         end loop Read_Message_Header;

         --  https://tools.ietf.org/html/rfc5321#section-4.5.2

         Read_Message : loop
            declare
               Line  : constant String := Net.Buffered.Get_Line (S.all);
               First : Natural := Line'First;
            begin
               exit Read_Message when Line = ".";

               if Line'Length > 0 and then Line (Line'First) = '.' then
                  First := First + 1;
               end if;

               Append (Message_Body, Line (First .. Line'Last) & CRLF);
            end;
         end loop Read_Message;
      end Read_Message_Body;

   begin
      Net.Buffered.Put_Line (S.all, SMTP.Message (220));
      --  ok, service is ready

      Read_Message : loop
         declare
            Line : constant String := Net.Buffered.Get_Line (S.all);
            Spc  : constant Natural :=
                     Strings.Fixed.Index
                       (Line, Strings.Maps.To_Set (" "));
            Last : constant Positive :=
                     (if Spc = 0 then Line'Last else Spc - 1);
            Cmd  : constant String :=
                     Line (Line'First .. Last);
         begin
            if Cmd = DATA then
               Net.Buffered.Put_Line
                 (S.all, SMTP.Message (Start_Mail_Input));

               Read_Message_Body;
               Net.Buffered.Put_Line
                 (S.all, SMTP.Message (Requested_Action_Ok));

            elsif Cmd = HELO then
               Net.Buffered.Put_Line
                 (S.all, SMTP.Message (Requested_Action_Ok));

            elsif Cmd = MAIL then
               Net.Buffered.Put_Line
                 (S.all, SMTP.Message (Requested_Action_Ok));

            elsif Cmd = RCPT then
               Net.Buffered.Put_Line
                 (S.all, SMTP.Message (Requested_Action_Ok));

            elsif Cmd = QUIT then
               Net.Buffered.Put_Line
                 (S.all, SMTP.Message (Service_Closing));
               exit Read_Message;

            elsif Cmd = STARTTLS then
               --  And upgrade socket to secure one

               declare
                  S_Sock : constant Net.SSL.Socket_Type :=
                             Net.SSL.Secure_Server (S.all);
               begin
                  Net.Buffered.Put_Line
                    (S.all, SMTP.Message (Service_Ready));
                  Net.Buffered.Flush (S.all);

                  Net.Free (S);
                  S := new Net.Socket_Type'Class'
                    (Net.Socket_Type'Class (S_Sock));
               end;
            else
               --  Command is not known

               Net.Buffered.Put_Line (S.all, SMTP.Message (Syntax_Error));

            end if;
         end;
      end loop Read_Message;

      Net.Buffered.Flush (S.all);

      Messages.Set.Set_Body (Message, To_String (Message_Body));
      Messages.Set.Headers (Message, Headers);

      return Message;
   end Get_Message;

   --------------
   -- Shutdown --
   --------------

   procedure Shutdown (Server : in out Handle) is
      Try : Natural := 5;
   begin
      --  Shutdown the socket, this should raise socket error on the
      --  Mail_Handler task.

      Server.Shutdown := True;

      Net.Shutdown (Server.Host.Sock.all);

      --  Now wait for the task to terminate

      while not Server.Server_Handler'Terminated and then Try > 0 loop
         delay 1.0;
         Try := Try - 1;
      end loop;

      --  If not yet terminated abort

      if not Server.Server_Handler'Terminated then
         abort Server.Server_Handler;
      end if;

      Net.Free (Server.Host.Sock);
   end Shutdown;

   -----------
   -- Start --
   -----------

   procedure Start
     (Server : in out Handle;
      Host   : Receiver;
      Action : Callback) is
   begin
      --  Initialize server listening port

      Server.Host := Host;

      Server.Host.Sock := Net.Socket (Security => False);

      Server.Host.Sock.Set_Timeout (Server.Host.Timeout);
      Server.Host.Sock.Bind (Port => Host.Port, Family => Host.Family);
      Server.Host.Sock.Listen;

      if Host.Port = 0 then
         Server.Host.Port := Server.Host.Sock.Get_Port;
      end if;

      Server.Action := Action;

      Server.Server_Handler.Start;
   end Start;

end AWS.SMTP.Server;