-
Notifications
You must be signed in to change notification settings - Fork 0
/
bftpdutmp.c
203 lines (171 loc) · 4.54 KB
/
bftpdutmp.c
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
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdlib.h>
#include <time.h>
#include "main.h"
#include "bftpdutmp.h"
#include "mypaths.h"
#include "logging.h"
#include "options.h"
FILE *bftpdutmp = NULL;
long bftpdutmp_offset = 0xFFFFFFFF;
void bftpdutmp_create_dir(char *path_to_file)
{
char *dir_name, *index;
if (! path_to_file) return;
dir_name = strdup(path_to_file);
if (! dir_name)
return;
index = strrchr(dir_name, '/');
if (index)
{
index[0] = '\0'; /* wipe out everything after folder name */
mkdir(dir_name, 0755);
}
free(dir_name);
}
void bftpdutmp_init()
{
char *filename = strdup(config_getoption("PATH_BFTPDUTMP"));
if (! filename)
return;
if ((!strcasecmp(filename, "none")) || (!filename[0]))
{
free(filename);
return;
}
/* try to create the directory, if it does not exist yet */
bftpdutmp_create_dir(filename);
/* we have to create the file if it doesn't exist */
bftpdutmp = fopen(filename, "a");
if (bftpdutmp)
fclose(bftpdutmp);
/* Then we can open it for reading and writing */
if (!(bftpdutmp = fopen(filename, "r+"))) {
bftpd_log("Could not open log file %s.\n", filename);
control_printf(SL_FAILURE, "421-Could not open file %s\r\n"
"421 Server disabled for security reasons.", filename);
exit(1);
}
rewind(bftpdutmp);
// clean up memory
free(filename);
}
void bftpdutmp_end()
{
if (bftpdutmp) {
if (bftpdutmp_offset != -1)
bftpdutmp_log(0);
fclose(bftpdutmp);
bftpdutmp = NULL;
}
}
void bftpdutmp_log(char type)
{
struct bftpdutmp ut, tmp;
long i;
int max_length;
if (!bftpdutmp)
return;
memset((void *) &ut, 0, sizeof(ut));
ut.bu_pid = getpid();
if (type) {
ut.bu_type = 1;
strncpy(ut.bu_name, user, sizeof(ut.bu_name));
max_length = sizeof(ut.bu_host);
strncpy(ut.bu_host, remotehostname, max_length);
ut.bu_host[max_length - 1] = '\0'; /* avoid non-null terminated string */
/* Determine offset of first user marked dead */
rewind(bftpdutmp);
i = 0;
while (fread((void *) &tmp, sizeof(tmp), 1, bftpdutmp)) {
if (!tmp.bu_type)
break;
i++;
}
bftpdutmp_offset = i * sizeof(tmp);
}
else
ut.bu_type = 0;
time(&(ut.bu_time));
fseek(bftpdutmp, bftpdutmp_offset, SEEK_SET);
fwrite((void *) &ut, sizeof(ut), 1, bftpdutmp);
fflush(bftpdutmp);
}
char bftpdutmp_pidexists(pid_t pid)
{
struct bftpdutmp tmp;
if (!bftpdutmp)
return 0;
rewind(bftpdutmp);
while (fread((void *) &tmp, sizeof(tmp), 1, bftpdutmp)) {
if ((tmp.bu_pid == pid) && (tmp.bu_type))
return 1;
}
return 0;
}
int bftpdutmp_usercount(char *username)
{
struct bftpdutmp tmp;
int count = 0;
if (!bftpdutmp)
return 0;
rewind(bftpdutmp);
while ( fread((void *) &tmp, sizeof(tmp), 1, bftpdutmp) ) {
/*
Took this out. It seems to just be taking up log space. -- Jesse
bftpd_log("bu_name=%s; username=%s, bu_type=%i\n", tmp.bu_name, username, tmp.bu_type);
*/
if (tmp.bu_type && ( !strcmp(tmp.bu_name, username) || !strcmp(username, "*")))
count++;
}
if (count < 0)
count = 1;
return count;
}
int bftpdutmp_dup_ip_count(char *ip_address)
{
struct bftpdutmp tmp;
int count = 0;
if (! bftpdutmp)
return 0;
rewind(bftpdutmp);
while ( fread( (void *) &tmp, sizeof(tmp), 1, bftpdutmp) )
{
if (tmp.bu_type && (! strcmp(tmp.bu_host, ip_address) ) )
count++;
}
return count;
}
/*
This function removes a log entry of
a dead client. This is called
when the bftpd parent catches a
signal indicating the child/client died.
This makes it look like the child
logged out properly.
-- Jesse
*/
void bftpdutmp_remove_pid(int pid)
{
struct bftpdutmp current;
int index = 0;
if (! bftpdutmp)
return;
rewind(bftpdutmp);
/* search for a matching pid */
while ( fread( (void *) ¤t, sizeof(current), 1, bftpdutmp) )
{
/* over-write the pid */
if ( current.bu_pid == pid )
{
fseek(bftpdutmp, index * sizeof(current), SEEK_SET);
memset(¤t, 0, sizeof(current));
fwrite( (void *) ¤t, sizeof(current), 1, bftpdutmp);
}
index++;
}
}