aboutsummaryrefslogtreecommitdiff
path: root/src/Authorisation.c
blob: 3cde03d83b205f829237842d3f2e2dc0075c45e0 (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
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
 /*@@
   @file      Authorisation.c
   @date      Fri Sep 15 12:34:59 2000
   @author    Tom Goodale
   @desc 
   Authorisation stuff for webserver
   @enddesc
   @version $Header
 @@*/

#include "cctk.h"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif

#ifdef HAVE_CRYPT_H
#include <crypt.h>
#endif

#include "httpd_Map.h"
#include "util_String.h"

#include "httpRequest.h"
#include "Auth.h"
#include "SString_Namespace.h"

#include "base64.h"

static const char *rcsid = "$Header$";

CCTK_FILEVERSION(CactusConnect_HTTPD_Authorisation_c)

/********************************************************************
 *********************     Local Data Types   ***********************
 ********************************************************************/

struct httpUserData
{
  char *password;
  char *encryption_scheme;
};

/********************************************************************
 ********************* Local Routine Prototypes *********************
 ********************************************************************/

static int AddUser(uMap database, 
                   const char *name,
                   const char *password,
                   const char *encryption_scheme);

static int VerifyPassword(const char *database, 
                          const char *user, 
                          const char *password);

/********************************************************************
 ********************* Other Routine Prototypes *********************
 ********************************************************************/

/********************************************************************
 *********************     Local Data   *****************************
 ********************************************************************/

static uMap AuthDatabase = NULL;

#define INITIAL_SIZE 32
#define DECODED_SIZE 100

/********************************************************************
 *********************     External Routines   **********************
 ********************************************************************/

 /*@@
   @routine    HTTP_AuthAddUser
   @date       Fri Sep 15 12:52:09 2000
   @author     Tom Goodale
   @desc 
   Adds a user to a http authentication database.
   @enddesc 
   @calls     
   @calledby   
   @history 
 
   @endhistory 

@@*/
int HTTP_AuthAddUser(const char *database, 
                     const char *name,
                     const char *password,
                     const char *encryption_scheme)
{
  int retcode = -1;
  uMap this_database = NULL;

  /* Create the master database if necessary */
  if(!AuthDatabase)
  {
    AuthDatabase = Httpd_MapCreate();
  }

  if(AuthDatabase)
  {
    /* Does this database exist ? */
    this_database = (uMap)Httpd_MapData(AuthDatabase, strlen(database), database);

    if(!this_database)
    {
      this_database = Httpd_MapCreate();

      if(this_database)
      {
        Httpd_MapStore(AuthDatabase, strlen(database), database, (void *)this_database);
      }
      else
      {
        retcode = -2;
      }
    }
  }

  /* Now add the user to the database */
  if(this_database)
  {
    retcode = AddUser(this_database, name, password, encryption_scheme);
  }

  return retcode;
}


 /*@@
   @routine    HTTP_AuthenticateBasic
   @date       Fri Sep 15 13:12:43 2000
   @author     Tom Goodale
   @desc 
   Authenticates an HTTP request against 
   a particular database.
   @enddesc 
   @calls     
   @calledby   
   @history 
 
   @endhistory 

   @returntype int
   @returndesc
   The authorisation status.
   +1 means that there was no Authorization header.
   0  succesful authentication
   -1 failed authentication
   @endreturndesc

@@*/
int HTTP_AuthenticateBasic(httpRequest *request,
                           const char *database,
                           char *user,
                           int length)
{
  int retval = -1;

  char *auth_string = NULL;
  char *token;
  
  int decoded_size = 0;
  char decoded[DECODED_SIZE+1] = {'\0'};
  
  char *password = NULL;

  int authorised = 0;

  const char *value = HTTP_HeaderValue(request, "Authorization");

  /* Null terminate the user string */ 
  if(user && length > 0)
  {
    *user = 0;
  }

  /* Ok, there's an authentication string here. */
  if(value)
  {
    auth_string = Util_Strdup(value);
    
    token = strtok(auth_string, " ");

    if(token)
    {
      if(CCTK_Equals(token, "Basic"))
      {
        token = strtok(NULL, " ,\t");
        decoded_size = HTTP_b64_pton(token, (unsigned char *) decoded,
                                     DECODED_SIZE);
        
        /* Null terminate string */
        decoded[decoded_size] = 0;

        password = strchr(decoded, ':');

        if(password)
        {
          *password = 0;
          password++;

          authorised = VerifyPassword(database, decoded, password);
          if(user && (int) strlen(user) < decoded_size)
          {
            sprintf(user,"%s", decoded);
          }
        }
      }
    }

    if(auth_string)
    {
      free(auth_string);
    }

    if(authorised)
    {
      retval = 0;
    }
  }
  else
  {
    /* There's no authentication string here */

    retval = 1;
    
  }

  return retval;
}

/********************************************************************
 *********************     Local Routines   *************************
 ********************************************************************/

 /*@@
   @routine    AddUser
   @date       Fri Sep 15 12:52:37 2000
   @author     Tom Goodale
   @desc 
   Adds a user to a particular database.
   @enddesc 
   @calls     
   @calledby   
   @history 
 
   @endhistory 

@@*/
static int AddUser(uMap database, 
                   const char *name,
                   const char *password,
                   const char *encryption_scheme)
{
  int retcode = -1;

  /* Does this user already exist ? */
  struct httpUserData * this_user = (struct httpUserData *)Httpd_MapData(
                                              database, strlen(name), name);

  if(!this_user)
  {
    /* New user */

    this_user = (struct httpUserData *)malloc(sizeof(struct httpUserData));

    if(this_user)
    {
      this_user->password          = Util_Strdup(password);
      this_user->encryption_scheme = Util_Strdup(encryption_scheme);

      retcode = Httpd_MapStore(database, strlen(name), name, (void *)this_user);
    }
  }
  else
  {
    /* Replace user's current data */
    free(this_user->password);
    free(this_user->encryption_scheme);

    this_user->password          = Util_Strdup(password);
    this_user->encryption_scheme = Util_Strdup(encryption_scheme);

    retcode = 0;
  }

  return retcode;
}

 /*@@
   @routine    VerifyPassword
   @date       Fri Sep 15 13:28:50 2000
   @author     Tom Goodale
   @desc 
   Verifies a user and password against a database.
   @enddesc 
   @calls     
   @calledby   
   @history 
 
   @endhistory 

@@*/
static int VerifyPassword(const char *database, 
                          const char *user, 
                          const char *password)
{
  int retcode = 0;

  if(AuthDatabase)
  {
    /* Does this database exist ? */
    uMap this_database = (uMap)Httpd_MapData(AuthDatabase,
                                              strlen(database), database);

    if(this_database)
    {
      struct httpUserData *data = (struct httpUserData *) Httpd_MapData(
                                         this_database, strlen(user), user);

      if(data)
      {
        /* Ok, now verify the password. */
        if(CCTK_Equals(data->encryption_scheme, "none"))
        {
          if(!strcmp(data->password, password))
          {
            retcode = 1;
          }
        }
        else if(CCTK_Equals(data->encryption_scheme, "crypt"))
        {
#ifdef HAVE_CRYPT
          if(!strcmp(data->password, crypt(password, data->password)))
          {
            retcode = 1;
          }
#else
	  CCTK_WARN( 1, "Sorry, crypt(3) not supported in this configuration." );
#endif
        }
        else
        {
          CCTK_VWarn(1, __LINE__,__FILE__,CCTK_THORNSTRING,
                     "Unknown encryption algorithm: %s",
                     data->encryption_scheme  );
        } 
      }
    }
  }

  return retcode;
}