-
Notifications
You must be signed in to change notification settings - Fork 15
/
lfdiff.c
447 lines (391 loc) · 10.4 KB
/
lfdiff.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
/*
* Copyright (c) 2003 Regents of The University of Michigan.
* All Rights Reserved. See COPYRIGHT.
*/
#include "config.h"
#include <sys/types.h>
#include <sys/time.h>
#include <sys/stat.h>
#include <sys/param.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <fcntl.h>
#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <openssl/ssl.h>
#include <openssl/err.h>
#include <openssl/evp.h>
#ifdef HAVE_ZLIB
#include <zlib.h>
#endif /* HAVE_ZLIB */
#include <snet.h>
#include "applefile.h"
#include "connect.h"
#include "argcargv.h"
#include "list.h"
#include "pathcmp.h"
#include "tls.h"
#include "transcript.h"
#include "code.h"
#include "wildcard.h"
void (*logger)( char * ) = NULL;
extern struct timeval timeout;
int verbose = 0;
int dodots = 0;
int linenum = 0;
int cksum = 0;
int case_sensitive = 1;
int tran_format = -1;
int create_prefix = 0;
int quiet = 1;
int excluded = 0;
const EVP_MD *md;
SSL_CTX *ctx;
extern char *caFile, *caDir, *cert, *privatekey;
static struct transcript *
precedent_transcript( char *kfile, char *file, int where )
{
extern struct transcript *tran_head;
struct stat st;
struct transcript *tran;
int cmp = 0;
/* verify that file exists on the local system */
if ( lstat( file, &st ) < 0 ) {
perror( file );
exit( 2 );
}
/* initialize important transcript bits */
edit_path = APPLICABLE;
transcript_init( kfile, where );
outtran = stdout;
/* check exclude list */
if ( t_exclude( file )) {
excluded = 1;
return( NULL );
}
for ( tran = tran_head; !tran->t_eof; tran = tran->t_next ) {
while (( cmp = pathcasecmp( tran->t_pinfo.pi_name, file,
case_sensitive )) < 0 ) {
transcript_parse( tran );
if ( tran->t_eof ) {
break;
}
}
if ( tran->t_eof ) {
continue;
}
if ( cmp > 0 ) {
continue;
}
if ( cmp == 0 ) {
return( tran );
}
}
return( NULL );
}
/*
* exit codes:
* 0 No differences were found.
* 1 Differences were found.
* >1 An error occurred.
*/
int
main( int argc, char **argv, char **envp )
{
int c, i, tac, err = 0, len;
int special = 0, diffargc = 0;
int fd;
unsigned short port = 0;
extern int optind;
extern char *version;
char *host = _RADMIND_HOST;
char *transcript = NULL;
char *file = NULL;
char *kfile = _RADMIND_COMMANDFILE;
char *diff = _PATH_GNU_DIFF;
char **diffargv;
char **argcargv;
char pathdesc[ 2 * MAXPATHLEN ];
char *path = "/tmp/lfdiff";
char temppath[ MAXPATHLEN ];
char opt[ 3 ];
char *epath; /* encoded path */
char **capa = NULL; /* capabilities */
SNET *sn;
int authlevel = _RADMIND_AUTHLEVEL;
int use_randfile = 0;
struct transcript *tran;
/* create argv to pass to diff */
if (( diffargv = (char **)malloc( 1 * sizeof( char * ))) == NULL ) {
perror( "malloc" );
exit( 2 );
}
diffargc = 0;
diffargv[ diffargc++ ] = diff;
while (( c = getopt ( argc, argv,
"h:IK:p:P:rST:u:Vvw:x:y:z:Z:bitcefnC:D:sX:" )) != EOF ) {
switch( c ) {
case 'I':
case_sensitive = 0;
break;
case 'h':
host = optarg;
break;
case 'K':
kfile = optarg;
break;
case 'p':
/* connect.c handles things if atoi returns 0 */
port = htons( atoi( optarg ));
break;
case 'P' : /* ca dir */
caDir = optarg;
break;
case 'r':
use_randfile = 1;
break;
case 'S':
special = 1;
break;
case 'T':
transcript = optarg;
break;
case 'u' : /* umask */
umask( (mode_t)strtol( optarg, (char **)NULL, 0 ));
break;
case 'V':
printf( "%s\n", version );
exit( 0 );
case 'v':
verbose = 1;
logger = v_logger;
if ( isatty( fileno( stdout ))) {
dodots = 1;
}
break;
case 'w' : /* authlevel 0:none, 1:serv, 2:client & serv */
authlevel = atoi( optarg );
if (( authlevel < 0 ) || ( authlevel > 2 )) {
fprintf( stderr, "%s: invalid authorization level\n",
optarg );
exit( 1 );
}
break;
case 'x' : /* ca file */
caFile = optarg;
break;
case 'y' : /* cert file */
cert = optarg;
break;
case 'z' : /* private key */
privatekey = optarg;
break;
case 'Z':
#ifdef HAVE_ZLIB
zlib_level = atoi(optarg);
if (( zlib_level < 0 ) || ( zlib_level > 9 )) {
fprintf( stderr, "Invalid compression level\n" );
exit( 1 );
}
break;
#else /* HAVE_ZLIB */
fprintf( stderr, "Zlib not supported.\n" );
exit( 1 );
#endif /* HAVE_ZLIB */
/* diff options */
case 'b': case 'i': case 't':
case 'c': case 'e': case 'f': case 'n':
case 's':
if (( diffargv = (char **)realloc( diffargv, ( sizeof( *diffargv )
+ ( 2 * sizeof( char * ))))) == NULL ) {
perror( "malloc" );
exit( 2 );
}
if ( snprintf( opt, sizeof( opt ), "-%c", c ) > sizeof( opt )) {
fprintf( stderr, "-%c: too large\n", c );
exit( 2 );
}
if (( diffargv[ diffargc++ ] = strdup( opt )) == NULL ) {
perror( "strdup" );
exit( 2 );
};
break;
case 'C':
case 'D':
if (( diffargv = (char **)realloc( diffargv, ( sizeof( *diffargv )
+ ( 3 * sizeof( char * ))))) == NULL ) {
perror( "malloc" );
exit( 2 );
}
if ( snprintf( opt, sizeof( opt ), "-%c", c ) > sizeof( opt )) {
fprintf( stderr, "-%c: too large\n", c );
exit( 2 );
}
if (( diffargv[ diffargc++ ] = strdup( opt )) == NULL ) {
perror( "strdup" );
exit( 2 );
};
diffargv[ diffargc++ ] = optarg;
break;
case 'X':
if (( tac = argcargv( opt, &argcargv )) < 0 ) {
err++;
}
if (( diffargv = (char **)realloc( diffargv, ( sizeof( *diffargv )
+ ( tac * sizeof( char * ))))) == NULL ) {
perror( "malloc" );
exit( 2 );
}
for ( i = 0; i < tac; i++ ) {
diffargv[ diffargc++ ] = argcargv[ i ];
}
break;
case '?':
err++;
break;
default:
err++;
break;
}
}
if (( transcript == NULL ) && ( !special )) {
if (( file = argv[ argc - 1 ] ) == NULL ) {
err++;
} else {
if (( tran = precedent_transcript( kfile,
file, K_CLIENT )) == NULL ) {
if ( excluded ) {
fprintf( stderr, "%s: excluded\n", file );
} else {
fprintf( stderr, "%s not found in any transcript\n", file );
}
exit( 2 );
}
/* check for special */
if ( strcmp( tran->t_shortname, "special.T" ) == 0 ) {
special = 1;
} else {
transcript = tran->t_shortname;
}
}
}
if ((( transcript == NULL ) && ( !special ))
|| (( special ) && ( transcript != NULL ))
|| ( host == NULL )) {
err++;
}
if ( err || ( argc - optind != 1 )) {
fprintf( stderr, "usage: %s ", argv[ 0 ] );
fprintf( stderr, "[ -IrvV ] " );
fprintf( stderr, "[ -T transcript | -S ] " );
fprintf( stderr, "[ -h host ] [ -p port ] [ -P ca-pem-directory ] " );
fprintf( stderr, "[ -u umask ] " );
fprintf( stderr, "[ -w auth-level ] [ -x ca-pem-file ] " );
fprintf( stderr, "[ -y cert-pem-file] [ -z key-pem-file ] " );
fprintf( stderr, "[ -Z compression-level ] " );
fprintf( stderr, "[ supported diff options ] " );
fprintf( stderr, "[ -X \"unsupported diff options\" ] " );
fprintf( stderr, "file\n" );
exit( 2 );
}
file = argv[ optind ];
len = strlen( file );
/* Determine if called with relative or absolute pathing. Path is relative
* if it's just '.' or starts with './'. File names that start with a '.'
* are absolute.
*/
if ( file[ 0 ] == '.' ) {
if ( len == 1 ) {
tran_format = T_RELATIVE;
} else if ( file[ 1 ] == '/' ) {
tran_format = T_RELATIVE;
}
} else {
tran_format = T_ABSOLUTE;
}
if ( authlevel != 0 ) {
if ( tls_client_setup( use_randfile, authlevel, caFile, caDir, cert,
privatekey ) != 0 ) {
/* error message printed in tls_setup */
exit( 2 );
}
}
if (( sn = connectsn( host, port )) == NULL ) {
exit( 2 );
}
if (( capa = get_capabilities( sn )) == NULL ) {
exit( 2 );
}
if ( authlevel != 0 ) {
if ( tls_client_start( sn, host, authlevel ) != 0 ) {
/* error message printed in tls_cleint_starttls */
exit( 2 );
}
}
#ifdef HAVE_ZLIB
/* Enable compression */
if ( zlib_level > 0 ) {
if ( negotiate_compression( sn, capa ) != 0 ) {
exit( 2 );
}
}
#endif /* HAVE_ZLIB */
/* encode path */
if (( epath = encode( file )) == NULL ) {
fprintf( stderr, "filename too long: %s\n", file );
exit( 2 );
}
/* create path description */
if ( special ) {
if ( snprintf( pathdesc, ( MAXPATHLEN * 2 ), "SPECIAL %s",
epath ) >= ( MAXPATHLEN * 2 )) {
fprintf( stderr, "RETR SPECIAL %s: path description too long\n",
file );
exit( 2 );
}
} else {
if ( snprintf( pathdesc, ( MAXPATHLEN * 2 ), "FILE %s %s",
transcript, epath ) >= ( MAXPATHLEN * 2 )) {
fprintf( stderr, "RETR FILE %s %s: path description too long\n",
transcript, epath );
exit( 2 );
}
}
if ( retr( sn, pathdesc, path, temppath, 0600, -1, "-" ) != 0 ) {
exit( 2 );
}
if (( closesn( sn )) != 0 ) {
fprintf( stderr, "can not close sn\n" );
exit( 2 );
}
#ifdef HAVE_ZLIB
if ( verbose && zlib_level > 0 ) print_stats( sn );
#endif /* HAVE_ZLIB */
if (( fd = open( temppath, O_RDONLY )) < 0 ) {
perror( temppath );
exit( 2 );
}
if ( unlink( temppath ) != 0 ) {
perror( temppath );
exit( 2 );
}
if ( dup2( fd, 0 ) < 0 ) {
perror( temppath );
exit( 2 );
}
if (( diffargv = (char **)realloc( diffargv, ( sizeof( *diffargv )
+ ( 4 * sizeof( char * ))))) == NULL ) {
perror( "malloc" );
exit( 2 );
}
diffargv[ diffargc++ ] = "--";
diffargv[ diffargc++ ] = "-";
diffargv[ diffargc++ ] = file;
diffargv[ diffargc++ ] = NULL;
execve( diff, diffargv, envp );
perror( diff );
exit( 2 );
}