|
|
1.1 root 1: /*
2: ******************************************************************************
3: *
4: * Module: bb_passwd.c
5: *
6: * Functions:
7: * bb_read_passwords() - Read or create the password file.
8: * bb_check_passwd() - Check a password against the passwd list.
9: * bb_passwd_set_1() - Set a password in the passwd list.
10: * bb_passwd_update() - Update the password list on disk.
11: *
12: *
13: ******************************************************************************
14: */
15:
16: /*
17: ******************************************************************************
18: * Include Files
19: ******************************************************************************
20: */
21: #include <stdio.h>
22: #include <rpc/rpc.h>
23: #include "common.h"
24: #include "protocol.h"
25: #include "server.h"
26:
27:
28: /*************************************************************************
29: ** **
30: ** bb_read_passwords() - If the password file exists read it and store **
31: ** each line in the password array. If it does not exist then create **
32: ** it and fill in a blank entry for each implementation. **
33: ** **
34: *************************************************************************/
35: BB_passwd passwds[BB_MAX_IMP]; /* The password array. */
36:
37: int
38: bb_read_passwords()
39: {
40: FILE *passwd_file; /* Pointer to the passwd file. */
41: int i = 0; /* Index into passwd array. */
42: int num_imps; /* The number of implementations*/
43: BB_passwd empty_pass; /* An empty password record. */
44:
45: num_imps = bb_get_imp_cnt();
46:
47: /*
48: ** If the file exists read it into the password array.
49: */
50: if ( (passwd_file = fopen( BB_PASSWD_FILE, "r")) != NULL )
51: {
52: while ( fread( passwds[i], BB_PASSWD_LEN, 1, passwd_file) != 0 )
53: {
54: passwds[i][BB_PASSWD_LEN-1] = NULL;
55: i++;
56: if ( i > num_imps )
57: {
58: fprintf( stderr, "ERROR: Too many passwords in file.\n");
59: fclose( passwd_file);
60: return BB_FAILURE;
61: }
62: }
63:
64: fclose( passwd_file);
65: return BB_SUCCESS;
66: }
67:
68: /*
69: ** The file did not exist so create it.
70: */
71: if ( (passwd_file = fopen( BB_PASSWD_FILE, "w")) == NULL )
72: {
73: fprintf( stderr, "FAILED opening the password data file '%s'.\n",
74: BB_PASSWD_FILE);
75: fclose( passwd_file);
76: return BB_FAILURE;
77: }
78:
79: /*
80: ** Fill the file with empty passwords.
81: */
82: memset( empty_pass, NUL, BB_PASSWD_LEN);
83: for( i = 0; i < num_imps; i++ )
84: {
85: if ( fwrite( empty_pass, BB_PASSWD_LEN, 1, passwd_file) != 1 )
86: {
87: fprintf( stderr, "ERROR: Could not create password file.\n");
88: fclose( passwd_file);
89: return BB_FAILURE;
90: }
91: memset( passwds[i], NUL, BB_PASSWD_LEN);
92: }
93:
94: fclose( passwd_file);
95: return BB_SUCCESS;
96: }
97:
98:
99:
100: /*************************************************************************
101: ** **
102: ** bb_check_passwd() - Check to see if the password matches the id. **
103: ** **
104: *************************************************************************/
105: int
106: bb_check_passwd( id, passwd)
107: BB_id id; /* The identifier of the passwd owner. */
108: BB_passwd passwd; /* The password to verify. */
109: {
110: int index; /* The index of the identifier. */
111:
112: if ( (index = bb_get_hash( id)) == BB_HASH_ID_NOT_FOUND )
113: {
114: return BB_BAD_CLIENT;
115: }
116:
117: /*
118: ** If no password exists return success.
119: */
120: if ( passwds[index][0] == NUL )
121: return BB_SUCCESS;
122:
123: if ( strncmp( passwd, passwds[index], BB_PASSWD_LEN) != 0 )
124: {
125: return BB_BAD_PASSWD;
126: }
127:
128: return BB_SUCCESS;
129: }
130:
131:
132:
133: /*************************************************************************
134: ** **
135: ** bb_passwd_set_1() - Set the password for the client in the database.**
136: ** check to make sure the old password is correct first. **
137: ** **
138: *************************************************************************/
139: int *
140: bb_passwd_set_1( p_passwd)
141: BB_passwd_in *p_passwd; /* The passwd_in structure. */
142: {
143: static int result; /* The result of the set operation. */
144: int index; /* The index of the identifier. */
145:
146: /*
147: ** bb_check_passwd() returns the reason for failure, so use that
148: ** as the result of this function call.
149: */
150: if ( (result = bb_check_passwd( p_passwd->client, p_passwd->old))
151: != BB_SUCCESS )
152: {
153: return &result;
154: }
155:
156: if ( (index = bb_get_hash( p_passwd->client)) == BB_HASH_ID_NOT_FOUND )
157: {
158: result = BB_BAD_CLIENT;
159: return &result;
160: }
161:
162: /*
163: ** Copy the password to the data structure and update the data file.
164: */
165: strncpy( passwds[index], p_passwd->new, BB_PASSWD_LEN);
166: result = bb_passwd_update( index);
167: return &result;
168: }
169:
170:
171:
172: /*************************************************************************
173: ** **
174: ** bb_passwd_update() - Write the password which has changed out to **
175: ** password data file. **
176: ** **
177: *************************************************************************/
178: int
179: bb_passwd_update( index)
180: int index; /* The index of the changed password. */
181: {
182: int num_imps; /* The number of implementations. */
183: FILE *passwd_file; /* Pointer to the passwd file. */
184: long seek_to; /* The position to seek to in the file. */
185:
186: num_imps = bb_get_imp_cnt();
187:
188: if ( index >= num_imps )
189: {
190: return BB_FAILURE;
191: }
192:
193: /*
194: ** The password that we want to write is at location index
195: ** passwords into the file. Multiply index by the size of
196: ** a password and that is the offset.
197: */
198: seek_to = index * BB_PASSWD_LEN;
199:
200: /*
201: ** Open the password file.
202: */
203: if ( (passwd_file = fopen( BB_PASSWD_FILE, "r+")) == NULL )
204: {
205: fprintf( stderr, "ERROR: Could not open password file for update.\n");
206: return BB_FAILURE;
207: }
208:
209: if ( fseek( passwd_file, seek_to, 0) != 0 )
210: {
211: fprintf( stderr, "ERROR: Could not seek in password file.\n");
212: fclose( passwd_file);
213: return BB_FAILURE;
214: }
215:
216: if ( fwrite( passwds[index], BB_PASSWD_LEN, 1, passwd_file) != 1 )
217: {
218: fprintf( stderr, "ERROR: Could not update password file.\n");
219: fclose( passwd_file);
220: return BB_FAILURE;
221: }
222:
223: fclose( passwd_file);
224: return BB_SUCCESS;
225: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.