syslinux-3.08-2 sources from FC4
[bootcd.git] / syslinux / menu / libmenu / passwords.c
1 /* -*- c -*- ------------------------------------------------------------- *
2  *   
3  *   Copyright 2004-2005 Murali Krishnan Ganapathy - All Rights Reserved
4  *
5  *   This program is free software; you can redistribute it and/or modify
6  *   it under the terms of the GNU General Public License as published by
7  *   the Free Software Foundation, Inc., 53 Temple Place Ste 330,
8  *   Bostom MA 02111-1307, USA; either version 2 of the License, or
9  *   (at your option) any later version; incorporated herein by reference.
10  *
11  * ----------------------------------------------------------------------- */
12
13 #include "passwords.h"
14 #include "des.h"
15 #include "string.h"
16 #include <stdlib.h>
17 #include <stdio.h>
18 #include "tui.h"
19
20 #define MAX_LINE 512 
21 // Max line length in a pwdfile
22 p_pwdentry userdb[MAX_USERS]; // Array of pointers
23 int numusers; // Actual number of users
24
25 // returns true or false, i.e. 1 or 0
26 char authenticate_user(const char * username, const char* pwd)
27 {
28   char salt[12];
29   int  i, password_ok;
30
31   password_ok=0;
32
33   for (i=0; i< numusers; i++) {
34     if (userdb[i] == NULL) continue;
35     if (strcmp(username,userdb[i]->username)==0) {
36       strcpy(salt, userdb[i]->pwdhash);
37       salt[2] = '\0';
38       if (strcmp(userdb[i]->pwdhash,crypt(pwd,salt))==0) return 1;
39     }
40   }
41   return 0;
42 }
43
44 // Does user USERNAME  have permission PERM
45 char isallowed(const char *username, const char *perm)
46 {
47   int i;
48   char *dperm;
49   char *tmp;
50
51   if (strcmp(username,GUEST_USER) == 0) return 0;
52   dperm = (char *) malloc(strlen(perm)+3);
53   strcpy(dperm+1,perm);
54   dperm[0] = ':';
55   dperm[strlen(perm)+1]=':';
56   dperm[strlen(perm)+2]=0;
57   // Now dperm = ":perm:"
58   for (i=0; i < numusers; i++) {
59      if (strcmp(userdb[i]->username,username)==0) // Found the user
60      {
61         if (userdb[i]->perms == NULL) return 0; // No permission
62         tmp = strstr(userdb[i]->perms,dperm); // Search for permission
63         free (dperm); // Release memory
64         if (tmp == NULL) return 0; else return 1;
65      }
66   }
67   // User not found return 0
68   free (dperm);
69   return 0;
70 }
71
72 // Initialise the list of of user passwords permissions from file
73 void init_passwords(const char *filename)
74 {
75   int i;
76   char line[MAX_LINE], *p,*user,*pwdhash,*perms;
77   FILE *f;
78
79   for (i=0; i < MAX_USERS; i++) userdb[i] = NULL;
80   numusers = 0;
81  
82   if ( !filename ) return; // No filename specified
83
84   f = fopen(filename,"r");
85   if ( !f ) return; // File does not exist
86
87   // Process each line
88   while ( fgets(line, sizeof line, f) ) {
89     // Replace EOLN with \0
90     p = strchr(line, '\r');
91     if ( p ) *p = '\0';
92     p = strchr(line, '\n');
93     if ( p ) *p = '\0';
94
95     // If comment line or empty ignore line
96     p = line;
97     while (*p==' ') p++; // skip initial spaces
98     if ( (*p == '#') || (*p == '\0')) continue; // Skip comment lines
99
100     user = p; // This is where username starts
101     p = strchr(user,':');
102     if (p == NULL) continue; // Malformed line skip
103     *p = '\0';
104     pwdhash = p+1;
105     if (*pwdhash == 0) continue; // Malformed line (no password specified)
106     p = strchr(pwdhash,':'); 
107     if (p == NULL) { // No perms specified
108        perms = NULL;
109     } else {
110        *p = '\0';
111        perms = p+1;
112        if (*perms == 0) perms = NULL;
113     }
114     // At this point we have user,pwdhash and perms setup 
115     userdb[numusers] = (p_pwdentry)malloc(sizeof(pwdentry));
116     strcpy(userdb[numusers]->username,user);
117     strcpy(userdb[numusers]->pwdhash,pwdhash);
118     if (perms == NULL)
119       userdb[numusers]->perms = NULL;
120     else {
121       userdb[numusers]->perms = (char *)malloc(strlen(perms)+3);
122       (userdb[numusers]->perms)[0] = ':';
123       strcpy(userdb[numusers]->perms + 1,perms);
124       (userdb[numusers]->perms)[strlen(perms)+1] = ':';
125       (userdb[numusers]->perms)[strlen(perms)+2] = 0;
126       // Now perms field points to ":perms:"
127     }
128     numusers++;
129   }
130   fclose(f);
131 }
132
133 void close_passwords()
134 {
135   int i;
136
137   for (i=0; i < numusers; i++)
138     if (userdb[i] != NULL) free(userdb[i]);
139   numusers = 0;
140 }