Adding debian version 3.35-1.
[syslinux-debian/hramrach.git] / menu / libmenu / passwords.c
blob40b5c49fee76ed442ae5f0e0b02a9b116cafcc7d
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"
14 #include "des.h"
15 #include "string.h"
16 #include <stdlib.h>
17 #include <stdio.h>
18 #include "tui.h"
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
25 // returns true or false, i.e. 1 or 0
26 char authenticate_user(const char * username, const char* pwd)
28 char salt[12];
29 int i;
31 for (i=0; i< numusers; i++) {
32 if (userdb[i] == NULL) continue;
33 if (strcmp(username,userdb[i]->username)==0) {
34 strcpy(salt, userdb[i]->pwdhash);
35 salt[2] = '\0';
36 if (strcmp(userdb[i]->pwdhash,crypt(pwd,salt))==0) return 1;
39 return 0;
42 // Does user USERNAME have permission PERM
43 char isallowed(const char *username, const char *perm)
45 int i;
46 char *dperm;
47 char *tmp;
49 // If no users, then everybody is allowed to do everything
50 if (numusers == 0) return 1;
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
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
68 free (dperm);
69 return 0;
72 // Initialise the list of of user passwords permissions from file
73 void init_passwords(const char *filename)
75 int i;
76 char line[MAX_LINE], *p,*user,*pwdhash,*perms;
77 FILE *f;
79 for (i=0; i < MAX_USERS; i++) userdb[i] = NULL;
80 numusers = 0;
82 if ( !filename ) return; // No filename specified
84 f = fopen(filename,"r");
85 if ( !f ) return; // File does not exist
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';
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
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;
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:"
128 numusers++;
130 fclose(f);
133 void close_passwords()
135 int i;
137 for (i=0; i < numusers; i++)
138 if (userdb[i] != NULL) free(userdb[i]);
139 numusers = 0;