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
)
31 for (i
= 0; i
< numusers
; i
++) {
32 if (userdb
[i
] == NULL
)
34 if (strcmp(username
, userdb
[i
]->username
) == 0) {
35 strcpy(salt
, userdb
[i
]->pwdhash
);
37 if (strcmp(userdb
[i
]->pwdhash
, crypt(pwd
, salt
)) == 0)
44 // Does user USERNAME have permission PERM
45 char isallowed(const char *username
, const char *perm
)
51 // If no users, then everybody is allowed to do everything
54 if (strcmp(username
, GUEST_USER
) == 0)
56 dperm
= (char *)malloc(strlen(perm
) + 3);
57 strcpy(dperm
+ 1, perm
);
59 dperm
[strlen(perm
) + 1] = ':';
60 dperm
[strlen(perm
) + 2] = 0;
61 // Now dperm = ":perm:"
62 for (i
= 0; i
< numusers
; i
++) {
63 if (strcmp(userdb
[i
]->username
, username
) == 0) // Found the user
65 if (userdb
[i
]->perms
== NULL
)
66 return 0; // No permission
67 tmp
= strstr(userdb
[i
]->perms
, dperm
); // Search for permission
68 free(dperm
); // Release memory
75 // User not found return 0
80 // Initialise the list of of user passwords permissions from file
81 void init_passwords(const char *filename
)
84 char line
[MAX_LINE
], *p
, *user
, *pwdhash
, *perms
;
87 for (i
= 0; i
< MAX_USERS
; i
++)
92 return; // No filename specified
94 f
= fopen(filename
, "r");
96 return; // File does not exist
99 while (fgets(line
, sizeof line
, f
)) {
100 // Replace EOLN with \0
101 p
= strchr(line
, '\r');
104 p
= strchr(line
, '\n');
108 // If comment line or empty ignore line
111 p
++; // skip initial spaces
112 if ((*p
== '#') || (*p
== '\0'))
113 continue; // Skip comment lines
115 user
= p
; // This is where username starts
116 p
= strchr(user
, ':');
118 continue; // Malformed line skip
122 continue; // Malformed line (no password specified)
123 p
= strchr(pwdhash
, ':');
124 if (p
== NULL
) { // No perms specified
132 // At this point we have user,pwdhash and perms setup
133 userdb
[numusers
] = (p_pwdentry
) malloc(sizeof(pwdentry
));
134 strcpy(userdb
[numusers
]->username
, user
);
135 strcpy(userdb
[numusers
]->pwdhash
, pwdhash
);
137 userdb
[numusers
]->perms
= NULL
;
139 userdb
[numusers
]->perms
= (char *)malloc(strlen(perms
) + 3);
140 (userdb
[numusers
]->perms
)[0] = ':';
141 strcpy(userdb
[numusers
]->perms
+ 1, perms
);
142 (userdb
[numusers
]->perms
)[strlen(perms
) + 1] = ':';
143 (userdb
[numusers
]->perms
)[strlen(perms
) + 2] = 0;
144 // Now perms field points to ":perms:"
151 void close_passwords(void)
155 for (i
= 0; i
< numusers
; i
++)
156 if (userdb
[i
] != NULL
)