This file is indexed.

/usr/share/ada/adainclude/aws/aws-server-hotplug.adb is in libaws2.10.2-dev 2.10.2-4.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
------------------------------------------------------------------------------
--                              Ada Web Server                              --
--                                                                          --
--                     Copyright (C) 2000-2009, AdaCore                     --
--                                                                          --
--  This library 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 2 of the License, 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.  See the GNU       --
--  General Public License for more details.                                --
--                                                                          --
--  You should have received a copy of the GNU General Public License       --
--  along with this library; if not, write to the Free Software Foundation, --
--  Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.          --
--                                                                          --
------------------------------------------------------------------------------

with Ada.Characters.Handling;
with Ada.Containers.Indefinite_Hashed_Maps;
with Ada.Strings.Hash;
with Ada.Strings.Unbounded;
with Ada.Text_IO;

with Strings_Cutter;

with AWS.Communication;
with AWS.Communication.Server;
with AWS.Digest;
with AWS.Messages;
with AWS.MIME;
with AWS.URL;

package body AWS.Server.Hotplug is

   use Ada.Strings.Unbounded;

   Authorization_Error : exception;

   function Message
     (Server     : String;
      Name       : String;
      Web_Server : HTTP_Access;
      Parameters : Communication.Parameter_Set
        := Communication.Null_Parameter_Set)
      return Response.Data;
   --  Handle incoming message to register/unregister a module

   package Hotplug_Server is
     new Communication.Server (HTTP, HTTP_Access, Message);

   type Client_Data is record
      Password : Unbounded_String;
      Host     : Unbounded_String;
      Port     : Positive;
      Nonce    : Digest.Nonce;
   end record;

   package Client_Table is new Ada.Containers.Indefinite_Hashed_Maps
     (String, Client_Data, Ada.Strings.Hash, "=");

   Null_Nonce : constant Digest.Nonce := (others => ' ');

   protected Client_Handler is

      procedure Add
        (Client : String;
         Data   : Client_Data);
      --  Add this client to the list of trusted clients

      procedure Get_Nonce
        (Client : String;
         Nonce  : out Digest.Nonce);
      --  Returns a new Nonce string

      function Get (Client : String) return Client_Data;
      --  Returns data for specified client

      procedure Delete_All;
      --  Removes all client from the handler

   private
      Clients : Client_Table.Map;
   end Client_Handler;

   ----------------------
   -- Activate_Hotplug --
   ----------------------

   procedure Activate
     (Web_Server         : HTTP_Access;
      Port               : Positive;
      Authorization_File : String;
      Register_Mode      : AWS.Hotplug.Register_Mode := AWS.Hotplug.Add)
   is
      use Ada;
      use Ada.Characters.Handling;

      function "+"
        (Str : String)
         return Unbounded_String renames To_Unbounded_String;

      File   : Text_IO.File_Type;
      Buffer : String (1 .. 1_024);
      Last   : Natural;
      Line   : Strings_Cutter.Cut_String;
      N      : Natural := 0;
   begin
      Hotplug_Server.Start (Port, Web_Server);
      AWS.Hotplug.Set_Mode (Web_Server.Filters, Register_Mode);

      Text_IO.Open (File, Text_IO.In_File, Authorization_File);

      while not Text_IO.End_Of_File (File) loop
         Text_IO.Get_Line (File, Buffer, Last);
         N := N + 1;
         Strings_Cutter.Create (Line, Buffer (1 .. Last), Separators => ":");

         if Strings_Cutter.Field_Count (Line) /= 4 then
            declare
               Error_Message : constant String
                 := Authorization_File & ": format error in line "
                    & Natural'Image (N);
            begin
               Log.Write (Web_Server.Error_Log, Error_Message);
               raise Constraint_Error with Error_Message;
            end;
         end if;

         Client_Handler.Add
           (Client => Strings_Cutter.Field (Line, 1),
            Data   =>
              (Password => +Strings_Cutter.Field (Line, 2),
               Host     => +To_Lower (Strings_Cutter.Field (Line, 3)),
               Port     => Positive'Value (Strings_Cutter.Field (Line, 4)),
               Nonce    => Null_Nonce));
      end loop;

      Text_IO.Close (File);
   exception
      when Text_IO.Name_Error =>
         Log.Write
           (Web_Server.Error_Log,
            "Can't open authorization file " & Authorization_File);
   end Activate;

   -------------
   -- Message --
   -------------

   function Message
     (Server     : String;
      Name       : String;
      Web_Server : HTTP_Access;
      Parameters : Communication.Parameter_Set
        := Communication.Null_Parameter_Set)
      return Response.Data
   is
      pragma Unreferenced (Server);
      use Ada.Characters.Handling;

      function Get_Nonce (Client_Name : String) return String;
      --  Returns a Nonce string

      function Check_Auth
        (Client_Name, Digest, Regexp : String;
         URL                         : String := "") return Boolean;
      --  Returns True if the Digest string is ok. If URL is specified, checks
      --  also that this redirection is authorized.

      ----------------
      -- Check_Auth --
      ----------------

      function Check_Auth
        (Client_Name, Digest, Regexp : String;
         URL                         : String := "") return Boolean
      is
         procedure Log_Error;
         --  Log an error message into the server error log file

         CD     : constant Client_Data := Client_Handler.Get (Client_Name);
         D      : AWS.Digest.Digest_String;
         Result : Boolean;

         ---------------
         -- Log_Error --
         ---------------

         procedure Log_Error is
         begin
            Log.Write
              (Web_Server.Error_Log,
               "Wrong authorization "
               & Client_Name & '|' & Regexp & '|' & URL);
         end Log_Error;

      begin
         D := AWS.Digest.Create
           (Client_Name, "hotplug",
            To_String (CD.Password), String (CD.Nonce), "hotplug", Regexp);

         if URL = "" then
            Result := D = Digest;
            return D = Digest;
         else
            declare
               U : AWS.URL.Object;
            begin
               U := AWS.URL.Parse (URL);
               Result := D = Digest
                 and then To_Lower (AWS.URL.Host (U)) = To_String (CD.Host)
                 and then AWS.URL.Port (U) = CD.Port;
            exception
               when AWS.URL.URL_Error =>
                  Result := False;
            end;
         end if;

         if not Result then
            Log_Error;
         end if;

         return Result;
      end Check_Auth;

      ---------------
      -- Get_Nonce --
      ---------------

      function Get_Nonce (Client_Name : String) return String is
         Nonce : Digest.Nonce;
      begin
         Client_Handler.Get_Nonce (Client_Name, Nonce);
         return String (Nonce);
      end Get_Nonce;

   begin
      --  There is two kind of message REGISTER and UNREGISTER. The formats
      --  are (parameters are between <>):
      --
      --  REGISTER <name> <digest> <regexp> <URL>
      --  UNREGISTER <name> <digest> <regexp>
      --  REQUEST_NONCE <name>

      if Name = Register_Message and then Parameters'Length = 4 then
         if Check_Auth
           (Client_Name => To_String (Parameters (1)),
            Digest      => To_String (Parameters (2)),
            Regexp      => To_String (Parameters (3)),
            URL         => To_String (Parameters (4)))
         then
            --  Now check that the URL host for this client is authorized
            AWS.Hotplug.Register
              (Web_Server.Filters,
               To_String (Parameters (3)),
               To_String (Parameters (4)));
            return Response.Acknowledge (Messages.S200, "OK");
         else
            return Response.Acknowledge (Messages.S401, "Wrong authorization");
         end if;

      elsif Name = Unregister_Message and then Parameters'Length = 3 then
         if Check_Auth
           (Client_Name => To_String (Parameters (1)),
            Digest      => To_String (Parameters (2)),
            Regexp      => To_String (Parameters (3)))
         then
            AWS.Hotplug.Unregister
              (Web_Server.Filters,
               To_String (Parameters (3)));
            return Response.Acknowledge (Messages.S200, "OK");
         else
            return Response.Acknowledge (Messages.S401, "Wrong authorization");
         end if;

      elsif Name = Request_Nonce_Message and then Parameters'Length = 1 then
         return Response.Build
           (MIME.Text_Plain, Get_Nonce (To_String (Parameters (1))));

      else
         return Response.Acknowledge (Messages.S400, "Unknown message");
      end if;

   exception
      when AWS.Hotplug.Register_Error =>
         --  Exception sent because a duplicate client name has been found
         --  on this server.
         return Response.Acknowledge (Messages.S409, "Cannot register");

      when Authorization_Error =>
         return Response.Acknowledge (Messages.S401, "Cannot register");
   end Message;

   -------------------
   -- Nonce_Handler --
   -------------------

   protected body Client_Handler is

      ---------
      -- Add --
      ---------

      procedure Add
        (Client : String;
         Data   : Client_Data)
      is
         Cursor  : Client_Table.Cursor;
         Success : Boolean;
      begin
         Client_Table.Insert (Clients, Client, Data, Cursor, Success);

         if not Success then
            raise Authorization_Error;
         end if;
      end Add;

      ----------------
      -- Delete_All --
      ----------------

      procedure Delete_All is
      begin
         Client_Table.Clear (Clients);
      end Delete_All;

      ---------
      -- Get --
      ---------

      function Get (Client : String) return Client_Data is
         Cursor : Client_Table.Cursor;
      begin
         Cursor := Client_Table.Find (Clients, Client);

         if Client_Table.Has_Element (Cursor) then
            return Client_Table.Element (Cursor);
         else
            raise Authorization_Error;
         end if;
      end Get;

      ---------------
      -- Get_Nonce --
      ---------------

      procedure Get_Nonce
        (Client : String;
         Nonce  : out Digest.Nonce)
      is
         Cursor : Client_Table.Cursor;
      begin
         Cursor := Client_Table.Find (Clients, Client);

         if Client_Table.Has_Element (Cursor) then
            declare
               CD : Client_Data := Client_Table.Element (Cursor);
            begin
               CD.Nonce := Digest.Create_Nonce;
               Nonce    := CD.Nonce;
               Client_Table.Replace_Element (Clients, Cursor, CD);
            end;

         else
            raise Authorization_Error;
         end if;
      end Get_Nonce;

   end Client_Handler;

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

   procedure Shutdown is
   begin
      Hotplug_Server.Shutdown;
      Client_Handler.Delete_All;
   end Shutdown;

end AWS.Server.Hotplug;