File: wtmp.c

package info (click to toggle)
rush 1.7%2Bdfsg-4
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 4,716 kB
  • ctags: 3,277
  • sloc: ansic: 7,729; sh: 4,985; perl: 297; makefile: 113; awk: 37; sed: 24
file content (276 lines) | stat: -rw-r--r-- 5,677 bytes parent folder | download | duplicates (2)
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
/* This file is part of GNU Rush.                  
   Copyright (C) 2008, 2009, 2010 Sergey Poznyakoff

   GNU Rush is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3, or (at your option)
   any later version.

   GNU Rush is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with GNU Rush.  If not, see <http://www.gnu.org/licenses/>. */

#ifdef HAVE_CONFIG_H
# include <config.h>
#endif

#include <sys/types.h>
#include <sys/time.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <string.h>

#include "librush.h"

enum rush_wtmp_dir rush_wtmp_dir = rush_wtmp_forward;
static int wtmp_fd = -1;
static size_t wtmp_recsize = 0;

int
rush_wtmp_open(const char *name, int rw)
{
	int fd;
	
	fd = open(name, rw ? O_RDWR|O_CREAT : O_RDONLY, rushdb_file_mode);
	if (fd == -1)
		return -1;
	wtmp_fd = fd;
	return 0;
}

int
rush_wtmp_close()
{
	int rc = close(wtmp_fd);
	wtmp_fd = -1;
	return rc;
}

int
rush_wtmp_rewind(void)
{
	int whence;
	
	switch (rush_wtmp_dir) {
	case rush_wtmp_forward:
		whence = SEEK_SET;
		break;
		
	case rush_wtmp_backward:
		whence = SEEK_END;
	}
	return lseek(wtmp_fd, 0, whence) == -1;
}

void
rush_wtmp_set_dir(enum rush_wtmp_dir dir)
{
	rush_wtmp_dir = dir;
	rush_wtmp_rewind();
}

int
rush_wtmp_seek(off_t off)
{
	off_t rc = lseek(wtmp_fd, off, SEEK_SET);
	if (rc == off) {
		wtmp_recsize = 0;
		return 0;
	} 
	return 1;
}

struct rush_wtmp *
alloc_wtmp(size_t reclen)
{
	struct rush_wtmp *wtmp = malloc(sizeof(*wtmp) + reclen
					- sizeof(size_t));
	if (wtmp) 
		wtmp->reclen = reclen;
	return wtmp;
}

enum rushdb_result
rush_wtmp_read_fwd(struct rush_wtmp **pwtmp)
{
	struct rush_wtmp *wtmprec;
	size_t reclen, left;
	ssize_t size;
	char *p, *s;
	
	if (wtmp_fd == -1) {
		errno = EINVAL;
		return rushdb_result_fail;
	}

	size = read(wtmp_fd, &reclen, sizeof(reclen));
	if (size == 0)
		return rushdb_result_eof; 
	if (size != sizeof(reclen))
		return rushdb_result_fail;
	wtmp_recsize = reclen;
	
	wtmprec = alloc_wtmp(reclen);
	if (!wtmprec)
		return rushdb_result_fail;
	p = RUSH_WTMP_DATA_PTR(wtmprec);
	reclen -= sizeof(reclen);
	left = reclen;
	while (left) {
		ssize_t n = read(wtmp_fd, p, left);
		if (n == -1)
			goto errlab;
		p += n;
		left -= n;
	}
	p = (char*) (wtmprec + 1);
	s = p;
	wtmprec->user = s;
	s += strlen(s) + 1;
	if (s - p > reclen)
		goto errlab;
	wtmprec->rule = s;
	s += strlen(s) + 1;
	if (s - p > reclen)
		goto errlab;
	wtmprec->command = s;

	*pwtmp = wtmprec;
	return rushdb_result_ok;

  errlab:
	free(wtmprec);
	rush_wtmp_close();
	return rushdb_result_fail;
}

enum rushdb_result
rush_wtmp_read(struct rush_wtmp **pwtmp)
{
	size_t reclen;
	enum rushdb_result res;
		
	switch (rush_wtmp_dir) {
	case rush_wtmp_forward:
		res = rush_wtmp_read_fwd(pwtmp);
		if (lseek(wtmp_fd, sizeof(reclen), SEEK_CUR) == -1)
			res = rushdb_result_fail;
		break;
		
	case rush_wtmp_backward:
		if (lseek(wtmp_fd, 0, SEEK_CUR) == 0)
			return rushdb_result_eof;
		if (lseek(wtmp_fd, -sizeof(reclen), SEEK_CUR) == -1)
			return rushdb_result_fail;
		if (read(wtmp_fd, &reclen, sizeof(reclen)) != sizeof(reclen))
			return rushdb_result_fail;
		if (lseek(wtmp_fd, -(reclen + sizeof(reclen)), SEEK_CUR) == -1)
			return rushdb_result_fail;
		res = rush_wtmp_read_fwd(pwtmp);
		if (res == rushdb_result_ok) {
			if (lseek(wtmp_fd, -reclen, SEEK_CUR) == -1)
				return rushdb_result_fail;
		}
	}
	return res;
}

size_t
rush_wtmp_reclen(struct rush_wtmp *src)
{
	size_t reclen = sizeof(struct rush_wtmp)
		+ strlen(src->user) + 1
		+ strlen(src->rule) + 1
		+ strlen(src->command) + 1;
	return reclen;
}

struct rush_wtmp *
rush_wtmp_copy(struct rush_wtmp *src)
{
	size_t reclen = rush_wtmp_reclen(src);
	struct rush_wtmp *dst = malloc(reclen);
	if (dst) {
		char *p;
		
		dst->reclen = reclen;
		dst->pid = src->pid;
		dst->start = src->start;
		dst->stop = src->stop;
		p = (char*) (dst + 1);
		strcpy(p, src->user);
		dst->user = NULL;
		p += strlen(p) + 1;
		dst->rule = NULL;
		strcpy(p, src->rule);
		p += strlen(p) + 1;
		dst->command = NULL;
		strcpy(p, src->command);
		p += strlen(p) + 1;
	}
	return dst;
}

off_t
rush_wtmp_append(struct rush_wtmp *wtmp)
{
	size_t left;
	char *p;
	off_t off;
	struct rush_wtmp *record;
	
	if (wtmp_fd == -1) {
		errno = EINVAL;
		return -1;
	}

	off = lseek(wtmp_fd, 0, SEEK_END);
	if (off == -1)
		return -1;

	record = rush_wtmp_copy(wtmp);

	rushdb_lock(wtmp_fd, record->reclen, off, SEEK_SET, RUSH_LOCK_WRITE);
	left = record->reclen;
	p = (char*) record;
	while (left) {
		ssize_t n = write(wtmp_fd, p, left);
		if (n == -1)
			goto errlab;
		p += n;
		left -= n;
	}
	if (write(wtmp_fd, &record->reclen, sizeof(record->reclen)) !=
	    sizeof(wtmp->reclen))
		goto errlab;

	rushdb_unlock(wtmp_fd, record->reclen, off, SEEK_SET);
        wtmp_recsize = record->reclen;
	free(record);
	return off;

  errlab:
	rushdb_unlock(wtmp_fd, record->reclen, off, SEEK_SET);
	rush_wtmp_close();
	return -1;
}

int
rush_wtmp_update(struct timeval *tv)
{
	struct rush_wtmp wtmp;
	if (lseek(wtmp_fd, - (wtmp_recsize + sizeof(size_t)), SEEK_CUR) == -1)
		return 1;
	if (read(wtmp_fd, &wtmp, sizeof wtmp) != sizeof wtmp)
		return 1;
	if (lseek(wtmp_fd, - sizeof(wtmp), SEEK_CUR) == -1)
		return 1;
	wtmp.stop = *tv;
	return write(wtmp_fd, &wtmp, sizeof wtmp) != sizeof wtmp;
}