This repository has been archived by the owner on May 10, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmd_logger.c
647 lines (544 loc) · 20.3 KB
/
cmd_logger.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
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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
/*
* University of Illinois/NCSA Open Source License
*
* Copyright © 2012-2014 NCSA. All rights reserved.
*
* Developed by:
*
* Storage Enabling Technologies (SET)
*
* Nation Center for Supercomputing Applications (NCSA)
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the .Software.),
* to deal with the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* + Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimers.
*
* + Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimers in the
* documentation and/or other materials provided with the distribution.
*
* + Neither the names of SET, NCSA
* nor the names of its contributors may be used to endorse or promote
* products derived from this Software without specific prior written
* permission.
*
* THE SOFTWARE IS PROVIDED .AS IS., WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS WITH THE SOFTWARE.
*/
/*
* System includes.
*/
#include <dlfcn.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <globus_gridftp_server.h>
#include <globus_i_gridftp_server_control.h>
#define ENV_LOGFILE_VAR "CMD_LOGGER_LOGFILE"
#define ENV_LOGFILE_KEEP "CMD_LOGGER_KEEP_LOG"
void __cleanup(void) __attribute__((destructor));
typedef struct
{
gss_cred_id_t cred;
char * sbj;
char * username;
char * pw;
} gfs_l_file_session_t;
#ifdef EOF_DEBUG
typedef struct
{
globus_mutex_t lock;
globus_memory_t mem;
globus_priority_q_t queue;
globus_list_t * buffer_list;
globus_gfs_operation_t op;
char * pathname;
globus_xio_handle_t file_handle;
globus_off_t file_offset;
globus_off_t read_offset;
globus_off_t read_length;
int pending_writes;
int pending_reads;
globus_size_t block_size;
int optimal_count;
int node_ndx;
globus_object_t * error;
globus_bool_t first_read;
globus_bool_t eof;
globus_bool_t aborted;
int concurrency_check;
int concurrency_check_interval;
char * expected_cksm;
char * expected_cksm_alg;
/* added for multicast stuff, but cold be genreally useful */
gfs_l_file_session_t * session;
globus_result_t finish_result;
} globus_l_file_monitor_t;
#endif /* EOF_DEBUG */
static void (*_real_globus_l_gsc_read_cb)(globus_xio_handle_t xio_handle,
globus_result_t result,
globus_byte_t * buffer,
globus_size_t len,
globus_size_t nbytes,
globus_xio_data_descriptor_t data_desc,
void * user_arg) = NULL;
static globus_xio_handle_t _real_xio_handle = NULL;
static int _cmd_logger_initialized = 0;
static FILE * _logfile_fp = NULL;
static char _path_to_logfile[MAXPATHLEN];
#ifdef EOF_DEBUG
static globus_l_file_monitor_t * file_monitor = NULL;
#endif /* EOF_DEBUG */
void __cleanup()
{
if (getenv(ENV_LOGFILE_KEEP))
return;
if (_cmd_logger_initialized && _path_to_logfile[0] != '\0')
unlink(_path_to_logfile);
}
static void
_init_cmd_logger()
{
char * logfile = NULL;
if (_cmd_logger_initialized)
return;
_path_to_logfile[0] = '\0';
logfile = getenv(ENV_LOGFILE_VAR);
if (logfile == NULL)
return;
if ((strlen(logfile) + strlen(".123456") + 1) > sizeof(_path_to_logfile))
return;
snprintf(_path_to_logfile, sizeof(_path_to_logfile), "%s.%d", logfile, getpid());
_logfile_fp = fopen(_path_to_logfile, "w");
if (! _logfile_fp)
return;
_cmd_logger_initialized = 1;
}
static void
_incoming_command_hook(globus_byte_t * buffer,
globus_size_t len,
globus_size_t nbytes,
globus_result_t result)
{
/*
* Save the incoming command.
*/
if (_logfile_fp)
{
fprintf(_logfile_fp,
"CMD (b:%ld l:%d n:%d r:%d)\n%s\n",
buffer,
len,
nbytes,
result,
buffer);
fflush(_logfile_fp);
}
}
static void
_outgoing_reply_hook(globus_byte_t * buffer,
globus_size_t buffer_length)
{
/*
* Save the outgoing reply.
*/
if (_logfile_fp)
{
fprintf(_logfile_fp,
"REPLY (b:%ld l:%d)\n%s\n",
buffer,
buffer_length,
buffer);
fflush(_logfile_fp);
}
}
static void
_fake_globus_l_gsc_read_cb(globus_xio_handle_t xio_handle,
globus_result_t result,
globus_byte_t * buffer,
globus_size_t len,
globus_size_t nbytes,
globus_xio_data_descriptor_t data_desc,
void * user_arg)
{
/* Send the command to the hook. */
_incoming_command_hook(buffer, len, nbytes, result);
/* Call the real function. */
_real_globus_l_gsc_read_cb(xio_handle, result, buffer, len, nbytes, data_desc, user_arg);
}
struct read_callback_args {
globus_gridftp_server_read_cb_t callback;
void * arg;
};
#ifdef EOF_DEBUG
static void
_register_read_callback(globus_gfs_operation_t op,
globus_result_t result,
globus_byte_t * buffer,
globus_size_t nbytes,
globus_off_t offset,
globus_bool_t eof,
void * user_arg)
{
struct read_callback_args * args = user_arg;
if (_logfile_fp)
{
globus_mutex_lock(&file_monitor->lock);
{
fprintf(_logfile_fp, "GridFTP read Callback\n");
fprintf(_logfile_fp, "\tPending writes: %d\n", file_monitor->pending_writes);
fprintf(_logfile_fp, "\tPending reads: %d\n", file_monitor->pending_reads-1);
fprintf(_logfile_fp, "\tEof: %s\n", (eof) ? "Yes": "No");
fprintf(_logfile_fp, "\tError: %s\n", (result || file_monitor->finish_result) ? "Yes": "No");
fflush(_logfile_fp);
}
globus_mutex_unlock(&file_monitor->lock);
}
args->callback(op, result, buffer, nbytes, offset, eof, args->arg);
free(user_arg);
}
globus_result_t
globus_gridftp_server_register_read(
globus_gfs_operation_t op,
globus_byte_t * buffer,
globus_size_t length,
globus_gridftp_server_read_cb_t callback,
void * user_arg)
{
void * module = NULL;
static globus_result_t (*_real_globus_gridftp_server_register_read)(
globus_gfs_operation_t op,
globus_byte_t * buffer,
globus_size_t length,
globus_gridftp_server_read_cb_t callback,
void * user_arg) = NULL;
_init_cmd_logger();
if (_real_globus_gridftp_server_register_read == NULL)
{
module = dlopen("libglobus_gridftp_server.so.6", RTLD_LAZY|RTLD_LOCAL);
if (!module)
exit(1);
/* Clear any previous error. */
dlerror();
_real_globus_gridftp_server_register_read = dlsym(module, "globus_gridftp_server_register_read");
if (dlerror())
exit(1);
}
if (!file_monitor)
file_monitor = user_arg;
if (_logfile_fp)
{
fprintf(_logfile_fp, "Registering GridFTP read\n");
fprintf(_logfile_fp, "\tPending writes: %d\n", file_monitor->pending_writes);
fprintf(_logfile_fp, "\tPending reads: %d\n", file_monitor->pending_reads+1);
fprintf(_logfile_fp, "\tEof: %s\n", (file_monitor->eof) ? "Yes": "No");
fprintf(_logfile_fp, "\tError: %s\n", (file_monitor->finish_result) ? "Yes": "No");
fflush(_logfile_fp);
}
struct read_callback_args * args = malloc(sizeof(struct read_callback_args));
args->callback = callback;
args->arg = user_arg;
return _real_globus_gridftp_server_register_read(op, buffer, length, _register_read_callback, args);
}
struct close_callback_args {
globus_xio_callback_t cb;
void * user_arg;
};
static void
_xio_register_close_cb(globus_xio_handle_t handle,
globus_result_t result,
void * user_arg)
{
struct close_callback_args * args = user_arg;
if (_logfile_fp)
{
if (result)
fprintf(_logfile_fp, "Error in close callback\n");
else
fprintf(_logfile_fp, "Close callback completed\n");
fflush(_logfile_fp);
}
file_monitor = NULL;
args->cb(handle, result, args->user_arg);
free(user_arg);
}
globus_result_t
globus_xio_register_close(
globus_xio_handle_t handle,
globus_xio_attr_t attr,
globus_xio_callback_t cb,
void * user_arg)
{
void * module = NULL;
static globus_result_t (*_real_globus_xio_register_close)(globus_xio_handle_t handle,
globus_xio_attr_t attr,
globus_xio_callback_t cb,
void * user_arg);
_init_cmd_logger();
if (_real_globus_xio_register_close == NULL)
{
module = dlopen("libglobus_xio.so.0", RTLD_LAZY|RTLD_LOCAL);
if (!module)
exit(1);
/* Clear any previous error. */
dlerror();
_real_globus_xio_register_close = dlsym(module, "globus_xio_register_close");
if (dlerror())
exit(1);
}
/* Ignore non-file closing calls. */
if (!file_monitor || file_monitor->file_handle != handle)
return _real_globus_xio_register_close(handle, attr, cb, user_arg);
if (_logfile_fp)
{
fprintf(_logfile_fp, "Registering close \n");
fflush(_logfile_fp);
}
struct close_callback_args * args = malloc(sizeof(struct close_callback_args));
args->cb = cb;
args->user_arg = user_arg;
return _real_globus_xio_register_close(handle, attr, _xio_register_close_cb, args);
}
#endif /* EOF_DEBUG */
/*
* Catch this function so that we can get the value of the xio handle.
*/
globus_result_t
globus_gridftp_server_control_start(globus_gridftp_server_control_t server,
globus_gridftp_server_control_attr_t attr,
globus_xio_system_socket_t system_handle,
globus_gridftp_server_control_cb_t done_cb,
void * user_arg)
{
void * module = NULL;
globus_result_t result = GLOBUS_SUCCESS;
static globus_result_t (*_real_globus_gridftp_server_control_start) (
globus_gridftp_server_control_t server,
globus_gridftp_server_control_attr_t attr,
globus_xio_system_socket_t system_handle,
globus_gridftp_server_control_cb_t done_cb,
void * user_arg) = NULL;
_init_cmd_logger();
if (_real_globus_gridftp_server_control_start == NULL)
{
module = dlopen("libglobus_gridftp_server_control.so.0", RTLD_LAZY|RTLD_LOCAL);
if (!module)
exit(1);
/* Clear any previous error. */
dlerror();
_real_globus_gridftp_server_control_start = dlsym(module, "globus_gridftp_server_control_start");
if (dlerror())
exit(1);
}
/* Call the real function. */
result = _real_globus_gridftp_server_control_start(server, attr, system_handle, done_cb, user_arg);
if (result == GLOBUS_SUCCESS)
_real_xio_handle = server->xio_handle;
return result;
}
/*
* Catch this function and see if it is reading the control channel.
*/
globus_result_t
globus_xio_register_read(globus_xio_handle_t handle,
globus_byte_t * buffer,
globus_size_t buffer_length,
globus_size_t waitforbytes,
globus_xio_data_descriptor_t data_desc,
globus_xio_data_callback_t cb,
void * user_arg)
{
void * module = NULL;
globus_result_t result = GLOBUS_SUCCESS;
static globus_result_t (*_real_globus_xio_register_read)
(globus_xio_handle_t handle,
globus_byte_t * buffer,
globus_size_t buffer_length,
globus_size_t waitforbytes,
globus_xio_data_descriptor_t data_desc,
globus_xio_data_callback_t cb,
void * user_arg) = NULL;
if (_real_globus_xio_register_read == NULL)
{
module = dlopen("libglobus_xio.so.0", RTLD_LAZY|RTLD_LOCAL);
if (!module)
exit(1);
/* Clear any previous error. */
dlerror();
_real_globus_xio_register_read = dlsym(module, "globus_xio_register_read");
if (dlerror())
exit(1);
}
if (handle != _real_xio_handle)
{
return _real_globus_xio_register_read(handle,
buffer,
buffer_length,
waitforbytes,
data_desc,
cb,
user_arg);
}
if (_real_globus_l_gsc_read_cb == NULL)
_real_globus_l_gsc_read_cb = cb;
return _real_globus_xio_register_read(handle,
buffer,
buffer_length,
waitforbytes,
data_desc,
_fake_globus_l_gsc_read_cb,
user_arg);
}
#ifdef EOF_DEBUG
struct xio_register_write_cb {
globus_xio_data_callback_t cb;
void * user_arg;
};
static void
_xio_register_write_cb(
globus_xio_handle_t xio_handle,
globus_result_t result,
globus_byte_t * buffer,
globus_size_t len,
globus_size_t nbytes,
globus_xio_data_descriptor_t data_desc,
void * user_arg)
{
struct xio_register_write_cb * args = user_arg;
globus_l_file_monitor_t * file_monitor = args->user_arg;
if (_logfile_fp)
{
globus_mutex_lock(&file_monitor->lock);
{
fprintf(_logfile_fp, "Buffer written to file\n");
fprintf(_logfile_fp, "\tPending writes: %d\n", file_monitor->pending_writes-1);
fprintf(_logfile_fp, "\tPending reads: %d\n", file_monitor->pending_reads);
fprintf(_logfile_fp, "\tEof: %s\n", (file_monitor->eof) ? "Yes": "No");
fprintf(_logfile_fp, "\tError: %s\n", (result || file_monitor->finish_result) ? "Yes": "No");
fflush(_logfile_fp);
}
globus_mutex_unlock(&file_monitor->lock);
}
return args->cb(xio_handle, result, buffer, len, nbytes, data_desc, args->user_arg);
free(args);
}
#endif /* EOF_DEBUG */
globus_result_t
globus_xio_register_write(
globus_xio_handle_t user_handle,
globus_byte_t * buffer,
globus_size_t buffer_length,
globus_size_t waitforbytes,
globus_xio_data_descriptor_t data_desc,
globus_xio_data_callback_t cb,
void * user_arg)
{
void * module = NULL;
globus_result_t result = GLOBUS_SUCCESS;
static globus_result_t
(*_real_globus_xio_register_write)(globus_xio_handle_t handle,
globus_byte_t * buffer,
globus_size_t buffer_length,
globus_size_t waitforbytes,
globus_xio_data_descriptor_t data_desc,
globus_xio_data_callback_t cb,
void * user_arg) = NULL;
if (_real_globus_xio_register_write == NULL)
{
module = dlopen("libglobus_xio.so.0", RTLD_LAZY|RTLD_LOCAL);
if (!module)
exit(1);
/* Clear any previous error. */
dlerror();
_real_globus_xio_register_write = dlsym(module, "globus_xio_register_write");
if (dlerror())
exit(1);
}
if (user_handle == _real_xio_handle)
_outgoing_reply_hook(buffer, buffer_length);
#ifdef EOF_DEBUG
if (!file_monitor || file_monitor->file_handle != user_handle)
#endif /* EOF_DEBUG */
return _real_globus_xio_register_write(user_handle,
buffer,
buffer_length,
waitforbytes,
data_desc,
cb,
user_arg);
#ifdef EOF_DEBUG
struct xio_register_write_cb * args = malloc(sizeof(struct xio_register_write_cb));
args->cb = cb;
args->user_arg = user_arg;
return _real_globus_xio_register_write(user_handle,
buffer,
buffer_length,
waitforbytes,
data_desc,
_xio_register_write_cb,
args);
#endif /* EOF_DEBUG */
}
int
setuid(uid_t Uid)
{
void * module = NULL;
int retval = 0;
static int (*_real_setuid) (uid_t Uid) = NULL;
if (_real_setuid == NULL)
{
module = dlopen("libc.so.6", RTLD_LAZY|RTLD_LOCAL);
if (!module)
exit(1);
/* Clear any previous error. */
dlerror();
_real_setuid = dlsym(module, "setuid");
if (dlerror())
exit(1);
}
/*
* Change the owner of our log file, this will allow us to delete it later if
* the directory is sticky.
*/
if (_logfile_fp)
fchown(fileno(_logfile_fp), Uid, -1);
return _real_setuid(Uid);
}
int
setgid(gid_t Gid)
{
void * module = NULL;
int retval = 0;
static int (*_real_setgid) (gid_t Gid) = NULL;
if (_real_setgid == NULL)
{
module = dlopen("libc.so.6", RTLD_LAZY|RTLD_LOCAL);
if (!module)
exit(1);
/* Clear any previous error. */
dlerror();
_real_setgid = dlsym(module, "setgid");
if (dlerror())
exit(1);
}
/*
* Change the owner of our log file, this will allow us to delete it later if
* the directory is sticky.
*/
if (_logfile_fp)
fchown(fileno(_logfile_fp), -1, Gid);
return _real_setgid(Gid);
}