1 /* -*- c -*- ------------------------------------------------------------- *
3 * Copyright 2004-2005 Murali Krishnan Ganapathy - All Rights Reserved
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.
11 * ----------------------------------------------------------------------- */
13 #include "passwords.h"
21 // Max line length in a pwdfile
22 p_pwdentry userdb[MAX_USERS]; // Array of pointers
23 int numusers; // Actual number of users
25 // returns true or false, i.e. 1 or 0
26 char authenticate_user(const char * username, const char* pwd)
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);
38 if (strcmp(userdb[i]->pwdhash,crypt(pwd,salt))==0) return 1;
44 // Does user USERNAME have permission PERM
45 char isallowed(const char *username, const char *perm)
51 if (strcmp(username,GUEST_USER) == 0) return 0;
52 dperm = (char *) malloc(strlen(perm)+3);
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
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;
67 // User not found return 0
72 // Initialise the list of of user passwords permissions from file
73 void init_passwords(const char *filename)
76 char line[MAX_LINE], *p,*user,*pwdhash,*perms;
79 for (i=0; i < MAX_USERS; i++) userdb[i] = NULL;
82 if ( !filename ) return; // No filename specified
84 f = fopen(filename,"r");
85 if ( !f ) return; // File does not exist
88 while ( fgets(line, sizeof line, f) ) {
89 // Replace EOLN with \0
90 p = strchr(line, '\r');
92 p = strchr(line, '\n');
95 // If comment line or empty ignore line
97 while (*p==' ') p++; // skip initial spaces
98 if ( (*p == '#') || (*p == '\0')) continue; // Skip comment lines
100 user = p; // This is where username starts
101 p = strchr(user,':');
102 if (p == NULL) continue; // Malformed line skip
105 if (*pwdhash == 0) continue; // Malformed line (no password specified)
106 p = strchr(pwdhash,':');
107 if (p == NULL) { // No perms specified
112 if (*perms == 0) perms = NULL;
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);
119 userdb[numusers]->perms = NULL;
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:"
133 void close_passwords()
137 for (i=0; i < numusers; i++)
138 if (userdb[i] != NULL) free(userdb[i]);