Source File modules/au/AU_util.c

    1    | /***************************************
    2    |   $Revision: 1.11 $
    3    |
    4    |   Authentication utilities
    5    |
    6    |   Status: NOT REVIEWED, NOT TESTED
    7    |
    8    |   Author(s):      Engin Gunduz
    9    |
    10   |   ******************/ /******************
    11   |   Modification History:
    12   |         engin (05/04/2000) Created.
    13   |   ******************/ /******************
    14   |   Copyright (c) 2000                              RIPE NCC
    15   |
    16   |   All Rights Reserved
    17   |
    18   |   Permission to use, copy, modify, and distribute this software and its
    19   |   documentation for any purpose and without fee is hereby granted,
    20   |   provided that the above copyright notice appear in all copies and that
    21   |   both that copyright notice and this permission notice appear in
    22   |   supporting documentation, and that the name of the author not be
    23   |   used in advertising or publicity pertaining to distribution of the
    24   |   software without specific, written prior permission.
    25   |
    26   |   THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
    27   |   ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS; IN NO EVENT SHALL
    28   |   AUTHOR BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY
    29   |   DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
    30   |   AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
    31   |   OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
    32   |  ***************************************/
    33   |
    34   | #include "AU_util.h"
    35   |
    36   | /* AU_crypt is a wrapper around crypt(3) */
    37   | char * AU_crypt(const char *key, const char *setting){
    38   |
    39   |   return crypt(key, setting);
    40   |
    41   | }
    42   |
    43   | /* takes a list of passwords and a crypted password. If any
    44   |    of the passwords in the list is the plaintext of crypted
    45   |    text, then it immediately returns 1. Otherwise, it returns
    46   |    0 */
    47   | int au_check_password(char * crypted_password, GSList * password_list){
    48   |
    49   |   GSList * next = NULL;
    50   |
    51   |   for(next = password_list; next != NULL; next = g_slist_next(next)){
    52   |     /* if the password is correct, return 1 */
    53   |     if(strcmp(crypt((char *)next->data, crypted_password), crypted_password) == 0){
    54   |       //printf("DEBUG: au_check_password returning 1\n");
    55   |       return(1);
    56   |     }
    57   |   }
    58   |   /* we couldn't find any correct password. So, return 0 */
    59   |   //printf("DEBUG: au_check_password returning 0\n");
    60   |   return(0);
    61   | }
    62   |
    63   |
    64   |
    65   |
    66   | /* simply compares auth_pgpkeyID & mesg_pgpkeyID and
    67   |    returns 1 if they are the same. */
    68   | int au_check_PGPkey(char * auth_pgpkeyID, char * mesg_pgpkeyID){
    69   |
    70   |   /* if auth_pgpkeyID & mesg_pgpkeyID are the same, return 1 */
    71   |   if(strcmp(auth_pgpkeyID, mesg_pgpkeyID) == 0){
    72   |     return(1);
    73   |   }else{
    74   |     return(0);
    75   |   }
    76   | }
    77   |
    78   |
    79   |
    80   | /* Compares the 'From' address of the message to the regular
    81   |    expression in the 'auth' attribute of the maintainer*/
    82   | int au_check_from_address(char * regexp, char * from_address){
    83   |
    84   |    int status;
    85   |    regex_t re;
    86   |
    87   |    if(from_address == NULL){
    88   |      return(0);
    89   |    }
    90   |    if (regcomp(&re, regexp, REG_EXTENDED|REG_NOSUB) != 0) {
    91   |      //printf("DEBUG: au_check_from_address  returns 0 (couldn't compile)\n");
    92   |      return(0);      /* couldn't compile the regexp, return false */
    93   |    }
    94   |
    95   |    status = regexec(&re, from_address, (size_t) 0, NULL, 0);
    96   |    regfree(&re);
    97   |    if (status != 0) {
    98   |      //printf("DEBUG: au_check_from_address returns 0 (regexp doesn't match)\n\t[regexp:%s][from:%s]\n",
    99   |      //       regexp, from_address);
    100  |      return(0);      /* failed */
    101  |    }
    102  |    /* OK, the regexp matches */
    103  |    //printf("DEBUG: au_check_from_address returns 1\n");
    104  |    return(1);
    105  | }
    106  |
    107  |
    108  |
    109  |
    110  |
    111  |
    112  |
    113  |
    114  | /* Gets a auth_vector, and  credentials_struct (which is extracted
    115  |    from the update message) and returns 0 if all of the auth
    116  |    methods fail, and returns the index of the succeeding auth_struct in the auth_vector
    117  |    if any one of them succeeds. */
    118  | int AU_authorise(GSList * auth_vector, credentials_struct credentials){
    119  |
    120  |   GSList * next = NULL;
    121  |   auth_struct * temp = NULL;
    122  |   int result = 0;
    123  |
    124  |   /* if the linked list contains no members, then return 1*/
    125  |   if(g_slist_length(auth_vector) == 0){
    126  |     return(1);
    127  |   }
    128  |
    129  |   for(next = auth_vector; next != NULL; next = g_slist_next(next)){
    130  |     temp = (auth_struct *)next->data;
    131  |     if( temp != NULL ){
    132  |       switch (temp->type){
    133  |         case AU_NONE: return temp->index; /* NONE, immediately returns true */
    134  |         case AU_MAIL_FROM: if(au_check_from_address(temp->auth, credentials.from)){
    135  |                              result = temp->index;
    136  |                            }
    137  |                            break;
    138  |         case AU_CRYPT_PW:  if(au_check_password(temp->auth, credentials.password_list)){
    139  |                              result = temp->index;
    140  |                            }
    141  |                            break;
    142  |         case AU_PGP: //printf("DEBUG: AU_authorise: will call au_check_PGPkey\n");
    143  |                      //printf("DEBUG: AU_authorise:   with temp->auth=[%s]\n", temp->auth);
    144  |                      //printf("DEBUG: AU_authorise:   and credentials.pgp_struct=[%s]\n", credentials.pgp_struct);
    145  |                      if(au_check_PGPkey(temp->auth, credentials.pgp_struct)){
    146  |                        result = temp->index;
    147  |                      }
    148  |                      /*result = 0; *//* not yet implemented */
    149  |                      break;
    150  |         default: ;/* this mustn't happen */
    151  |       }
    152  |       if(result > 0){
    153  |         return(result);
    154  |       }
    155  |     }
    156  |   }
    157  |   /* we couldn't find any credential which passes, so returning 0 */
    158  |   return 0;
    159  |
    160  | }