1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
|
/* $OpenBSD: tokendb.c,v 1.7 2003/07/29 18:39:23 deraadt Exp $ */
/*-
* Copyright (c) 1995 Migration Associates Corp. All Rights Reserved
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by Berkeley Software Design,
* Inc.
* 4. The name of Berkeley Software Design, Inc. may not be used to endorse
* or promote products derived from this software without specific prior
* written permission.
*
* THIS SOFTWARE IS PROVIDED BY BERKELEY SOFTWARE DESIGN, INC. ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL BERKELEY SOFTWARE DESIGN, INC. BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* BSDI $From: tokendb.c,v 1.1 1996/08/26 20:13:10 prb Exp $
*/
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/param.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <ctype.h>
#include <db.h>
#include <errno.h>
#include <fcntl.h>
#include <grp.h>
#include <limits.h>
#include <stdio.h>
#include <syslog.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "token.h"
#include "tokendb.h"
static DB *tokendb;
/*
* Static function prototypes
*/
static int tokendb_open(void);
static void tokendb_close(void);
/*
* Retrieve a user record from the token database file
*/
int
tokendb_getrec(char *username, TOKENDB_Rec *tokenrec)
{
DBT key;
DBT data;
int status = 0;
key.data = username;
key.size = strlen(username) + 1;
memset(&data, 0, sizeof(data));
if (tokendb_open())
return(-1);
status = (tokendb->get)(tokendb, &key, &data, 0);
switch (status) {
case 1:
tokendb_close();
return(ENOENT);
case -1:
tokendb_close();
return(-1);
}
memcpy(tokenrec, data.data, sizeof(TOKENDB_Rec));
if ((tokenrec->flags & TOKEN_USEMODES) == 0)
tokenrec->mode = tt->modes & ~TOKEN_RIM;
tokendb_close();
return (0);
}
/*
* Put a user record to the token database file.
*/
int
tokendb_putrec(char *username, TOKENDB_Rec *tokenrec)
{
DBT key;
DBT data;
int status = 0;
key.data = username;
key.size = strlen(username) + 1;
if (tokenrec->mode)
tokenrec->flags |= TOKEN_USEMODES;
data.data = tokenrec;
data.size = sizeof(TOKENDB_Rec);
if (!tokendb_open()) {
if (flock((tokendb->fd)(tokendb), LOCK_EX)) {
tokendb_close();
return (-1);
}
status = (tokendb->put)(tokendb, &key, &data, 0);
}
tokendb_close();
return (status);
}
/*
* Remove a user record from the token database file.
*/
int
tokendb_delrec(char *username)
{
DBT key;
DBT data;
int status = 0;
key.data = username;
key.size = strlen(username) + 1;
memset(&data, 0, sizeof(data));
if (!tokendb_open()) {
if (flock((tokendb->fd)(tokendb), LOCK_EX)) {
tokendb_close();
return (-1);
}
status = (tokendb->del)(tokendb, &key, 0);
}
tokendb_close();
return (status);
}
/*
* Open the token database. In order to expedite access in
* heavily loaded conditions, we employ a N1 lock method.
* Updates should be brief, so all locks wait infinitely.
* Wait for a read (shared) lock as all updates read first.
*/
static int
tokendb_open(void)
{
int must_set_perms = 0;
int must_set_mode = 0;
struct group *grp;
struct stat statb;
if ((grp = getgrnam(TOKEN_GROUP)) == NULL) {
printf("Missing %s group, authentication disabled\n",
TOKEN_GROUP);
fflush(stdout);
syslog(LOG_ALERT,
"the %s group is missing, token authentication disabled",
TOKEN_GROUP);
return (-1);
}
if (stat(tt->db, &statb) < 0) {
if (errno != ENOENT)
return (-1);
must_set_perms++;
} else {
if (statb.st_uid != 0 || statb.st_gid != grp->gr_gid) {
#ifdef PARANOID
printf("Authentication disabled\n");
fflush(stdout);
syslog(LOG_ALERT,
"POTENTIAL COMPROMISE of %s. Owner was %u, "
"Group was %u", tt->db, statb.st_uid, statb.st_gid);
return (-1);
#else
must_set_perms++;
#endif
}
if ((statb.st_mode & 0777) != 0640) {
#ifdef PARANOID
printf("Authentication disabled\n");
fflush(stdout);
syslog(LOG_ALERT,
"POTENTIAL COMPROMISE of %s. Mode was %o",
tt->db, statb.st_mode);
return (-1);
#else
must_set_mode++;
#endif
}
}
if (!(tokendb =
dbopen(tt->db, O_CREAT | O_RDWR, 0640, DB_BTREE, 0)) )
return (-1);
if (flock((tokendb->fd)(tokendb), LOCK_SH)) {
(tokendb->close)(tokendb);
return (-1);
}
if (must_set_perms && fchown((tokendb->fd)(tokendb), 0, grp->gr_gid))
syslog(LOG_INFO,
"Can't set owner/group of %s errno=%m", tt->db);
if (must_set_mode && fchmod((tokendb->fd)(tokendb), 0640))
syslog(LOG_INFO,
"Can't set mode of %s errno=%m", tt->db);
return (0);
}
/*
* Close the token database. We are holding an unknown lock.
* Release it, then close the db. Since little can be done
* about errors, we ignore them.
*/
static void
tokendb_close(void)
{
(void)flock((tokendb->fd)(tokendb), LOCK_UN);
(tokendb->close)(tokendb);
}
/*
* Retrieve the first user record from the database, leaving the
* database open for the next retrieval. If the march thru the
* the database is aborted before end-of-file, the caller should
* call tokendb_close to release the read lock.
*/
int
tokendb_firstrec(int reverse_flag, TOKENDB_Rec *tokenrec)
{
DBT key;
DBT data;
int status = 0;
memset(&data, 0, sizeof(data));
if (!tokendb_open()) {
status = (tokendb->seq)(tokendb, &key, &data,
reverse_flag ? R_LAST : R_FIRST);
}
if (status) {
tokendb_close();
return (status);
}
if (!data.data) {
tokendb_close();
return (ENOENT);
}
memcpy(tokenrec, data.data, sizeof(TOKENDB_Rec));
if ((tokenrec->flags & TOKEN_USEMODES) == 0)
tokenrec->mode = tt->modes & ~TOKEN_RIM;
return (0);
}
/*
* Retrieve the next sequential user record from the database. Close
* the database only on end-of-file or error.
*/
int
tokendb_nextrec(int reverse_flag, TOKENDB_Rec *tokenrec)
{
DBT key;
DBT data;
int status;
memset(&data, 0, sizeof(data));
status = (tokendb->seq)(tokendb, &key, &data,
reverse_flag ? R_PREV : R_NEXT);
if (status) {
tokendb_close();
return (status);
}
if (!data.data) {
tokendb_close();
return (ENOENT);
}
memcpy(tokenrec, data.data, sizeof(TOKENDB_Rec));
if ((tokenrec->flags & TOKEN_USEMODES) == 0)
tokenrec->mode = tt->modes & ~TOKEN_RIM;
return (0);
}
/*
* Retrieve and lock a user record. Since there are no facilities in
* BSD for record locking, we hack a bit lock into the user record.
*/
int
tokendb_lockrec(char *username, TOKENDB_Rec *tokenrec, unsigned recflags)
{
DBT key;
DBT data;
int status;
key.data = username;
key.size = strlen(username) + 1;
memset(&data, 0, sizeof(data));
if (tokendb_open())
return(-1);
if (flock((tokendb->fd)(tokendb), LOCK_EX)) {
tokendb_close();
return(-1);
}
switch ((tokendb->get)(tokendb, &key, &data, 0)) {
case 1:
tokendb_close();
return (ENOENT);
case -1:
tokendb_close();
return(-1);
}
memcpy(tokenrec, data.data, sizeof(TOKENDB_Rec));
if ((tokenrec->flags & TOKEN_LOCKED)||(tokenrec->flags & recflags)) {
tokendb_close();
return(1);
}
data.data = tokenrec;
data.size = sizeof(TOKENDB_Rec);
time(&tokenrec->lock_time);
tokenrec->flags |= recflags;
status = (tokendb->put)(tokendb, &key, &data, 0);
tokendb_close();
if (status)
return(-1);
if ((tokenrec->flags & TOKEN_USEMODES) == 0)
tokenrec->mode = tt->modes & ~TOKEN_RIM;
return(0);
}
|