-
Notifications
You must be signed in to change notification settings - Fork 1
/
sr_util.c
1066 lines (906 loc) · 26.2 KB
/
sr_util.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
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <dirent.h>
#include <libgen.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/xattr.h>
#include <stdbool.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <openssl/evp.h>
#include <openssl/md5.h>
#include <string.h>
#include <time.h>
#include <sys/time.h>
#include <stdarg.h>
#include <linux/limits.h>
#include <ctype.h>
/* following three are for sr_has_vip */
#include <sys/socket.h>
#include <ifaddrs.h>
#include <netdb.h>
#include "sr_util.h"
static struct timespec ts;
static struct tm tc;
static bool sr_default_log_initialized=false;
static struct sr_log_context_s null_ctx;
void sr_log_init_context(struct sr_log_context_s *ctx) {
if (ctx == &null_ctx) {
sr_default_log_initialized=true;
}
//ctx->logbase;
ctx->logfd = STDERR_FILENO;
//ctx->logfn[PATH_MAX];
//ctx->metricsfn[PATH_MAX];
ctx->logMetrics = false;
//ctx->logfn_ts[PATH_MAX];
//ctx->metricsfn_ts[PATH_MAX];
ctx->loglevel = LOG_INFO;
ctx->logmode = 0600;
ctx->logRotateCount = 5;
ctx->logRotateInterval = 24 * 60 * 60;
}
char *log_set_fnts(struct sr_log_context_s* ctx, char *basefilename);
void sr_log_delete_rotations(struct sr_log_context_s* ctx, char *basefilename)
/* look at all the files that match the basefilename,
* keep logRotateCount of them, delete the others.
*/
{
char *baseDirBuf=strdup(basefilename);
char *thedir = dirname(baseDirBuf);
DIR *dir;
struct dirent* de;
char *baseFileBuf=strdup(basefilename);
char *thefile = basename(baseFileBuf);
int maxAge = ctx->logRotateCount * ctx->logRotateInterval;
char fullpath[PATH_MAX];
struct stat sb;
struct timespec ts;
int age;
clock_gettime(CLOCK_REALTIME, &ts);
//fprintf( stderr, "directory is: %s, filename: %s\n", thedir, thefile );
//fprintf( stderr, "oldest possible: %d\n", maxAge );
dir = opendir(thedir);
if (dir)
{
while ( (de = readdir(dir)) != NULL ) {
if (!strncmp( thefile, de->d_name, strlen(thefile) ) )
{
sprintf(fullpath,"%s/%s", thedir, de->d_name );
//fprintf( stderr, "this is a matching log: %s\n", fullpath );
if (!stat(fullpath, &sb))
{
age = ts.tv_sec - sb.st_mtime;
//fprintf( stderr, "and %s is %d seconds old\n", fullpath, age );
if (age > maxAge) {
fprintf( stderr, "%s too old! (%d seconds) Removing.\n", fullpath, age );
remove(fullpath);
}
}
}
}
closedir(dir);
} else {
fprintf(stderr, "failed to open %s!\n", thedir );
};
free(baseDirBuf);
free(baseFileBuf);
}
#ifndef SR_DEBUG_LOGS
void sr_log_msg(struct sr_log_context_s* ctx, int prio, const char *format, ...)
{
char *level;
va_list ap;
if (ctx==NULL) {
if (!(sr_default_log_initialized))
sr_log_init_context( &null_ctx);
ctx=&null_ctx;
}
if (prio > ctx->loglevel)
return;
switch (prio) {
case LOG_DEBUG:
level = "DEBUG";
break;
case LOG_INFO:
level = "INFO";
break;
case LOG_NOTICE:
level = "NOTICE";
break;
case LOG_WARNING:
level = "WARNING";
break;
case LOG_ERROR:
level = "ERROR";
break;
case LOG_CRITICAL:
level = "CRITICAL";
break;
default:
level = "UNKNOWN";
}
clock_gettime(CLOCK_REALTIME, &ts);
localtime_r(&ts.tv_sec, &tc);
/* log (message & metrics) rotation */
if ((ctx->logfd != STDERR_FILENO) && ((ts.tv_sec - ctx->logbase) > ctx->logRotateInterval)) {
ctx->logbase = ts.tv_sec;
strcpy(ctx->logfn_ts,log_set_fnts(ctx, ctx->logfn));
close(ctx->logfd);
rename(ctx->logfn, ctx->logfn_ts);
strcpy(ctx->metricsfn_ts,log_set_fnts(ctx, ctx->metricsfn));
sr_log_delete_rotations( ctx, ctx->metricsfn );
rename(ctx->metricsfn, ctx->metricsfn_ts);
ctx->logfd = open(ctx->logfn, O_WRONLY | O_CREAT | O_APPEND, ctx->logmode);
/* delete outdated logs */
if (ctx->logRotateCount > 0) {
sr_log_delete_rotations( ctx, ctx->logfn );
if (ctx->ltab.fns[ctx->ltab.i]) {
remove(ctx->ltab.fns[ctx->ltab.i]);
free(ctx->ltab.fns[ctx->ltab.i]);
ctx->ltab.fns[ctx->ltab.i] = NULL;
}
ctx->ltab.fns[ctx->ltab.i] = strdup(ctx->logfn_ts);
ctx->ltab.i = (ctx->ltab.i + 1) % ctx->ltab.size;
if (ctx->mtab.fns[ctx->mtab.i]) {
remove(ctx->mtab.fns[ctx->mtab.i]);
free(ctx->mtab.fns[ctx->mtab.i]);
ctx->mtab.fns[ctx->mtab.i] = NULL;
}
ctx->mtab.fns[ctx->mtab.i] = strdup(ctx->metricsfn_ts);
ctx->mtab.i = (ctx->mtab.i + 1) % ctx->mtab.size;
}
}
/* logging */
dprintf(ctx->logfd, "%04d-%02d-%02d %02d:%02d:%02d,%03d [%s] ",
tc.tm_year + 1900, tc.tm_mon + 1, tc.tm_mday,
tc.tm_hour, tc.tm_min, tc.tm_sec, (int)(ts.tv_nsec / 1e6), level);
va_start(ap, format);
vdprintf(ctx->logfd, format, ap);
va_end(ap);
}
#endif
struct sr_log_context_s* sr_log_setup(const char *fn, const char *mfn, bool logMetricsFlag, mode_t mode, int level, int lr, int lri)
{
#ifndef SR_DEBUG_LOGS
struct sr_log_context_s *ctx = NULL;
ctx = (struct sr_log_context_s*)malloc(sizeof(struct sr_log_context_s));
strcpy(ctx->logfn, fn);
strcpy(ctx->metricsfn, mfn);
ctx->logmode = mode;
ctx->logRotateCount = lr;
ctx->logRotateInterval = lri;
ctx->loglevel = level;
ctx->logMetrics = logMetricsFlag;
clock_gettime(CLOCK_REALTIME, &ts);
localtime_r(&ts.tv_sec, &tc);
ctx->logbase = ts.tv_sec;
ctx->logfd = open(ctx->logfn, O_WRONLY | O_CREAT | O_APPEND, ctx->logmode);
if (ctx->logRotateCount > 0) {
ctx->ltab.fns = (char **)malloc(sizeof(char *) * ctx->logRotateCount);
for (ctx->ltab.i = 0; ctx->ltab.i < ctx->logRotateCount; ++ctx->ltab.i)
ctx->ltab.fns[ctx->ltab.i] = NULL;
ctx->ltab.i = 0;
ctx->ltab.size = ctx->logRotateCount;
// metrics files.
ctx->mtab.fns = (char **)malloc(sizeof(char *) * ctx->logRotateCount);
for (ctx->mtab.i = 0; ctx->mtab.i < ctx->logRotateCount; ++ctx->mtab.i)
ctx->mtab.fns[ctx->mtab.i] = NULL;
ctx->mtab.i = 0;
ctx->mtab.size = ctx->logRotateCount;
}
return(ctx);
#endif
}
/* global accessor for loglevel, ugly but better than using a global variable... */
void sr_set_loglevel(struct sr_log_context_s* ctx, int level)
{
if (ctx==NULL) {
if (!(sr_default_log_initialized))
sr_log_init_context( &null_ctx);
ctx=&null_ctx;
}
ctx->loglevel = level;
}
void sr_log_cleanup(struct sr_log_context_s* ctx)
{
if (ctx==NULL) {
if (!(sr_default_log_initialized))
sr_log_init_context( &null_ctx);
ctx=&null_ctx;
}
#ifndef SR_DEBUG_LOGS
/* (logfd != STDERR_FILENO) <> sr_log_setup called previously */
if (ctx->logfd != STDERR_FILENO) {
close(ctx->logfd);
ctx->logfd = STDERR_FILENO;
if (ctx->logRotateCount > 0) {
for (ctx->ltab.i = 0; ctx->ltab.i < ctx->logRotateCount; ++ctx->ltab.i)
if (ctx->ltab.fns[ctx->ltab.i])
free(ctx->ltab.fns[ctx->ltab.i]);
free(ctx->ltab.fns);
ctx->ltab.i = 0;
ctx->ltab.size = 0;
for (ctx->mtab.i = 0; ctx->mtab.i < ctx->logRotateCount; ++ctx->mtab.i)
if (ctx->mtab.fns[ctx->mtab.i])
free(ctx->mtab.fns[ctx->mtab.i]);
free(ctx->mtab.fns);
ctx->mtab.i = 0;
ctx->mtab.size = 0;
}
}
#endif
}
char *log_set_fnts(struct sr_log_context_s *ctx, char *basefilename)
/* set logging file name time stamp.
*/
{
static char logfn_ts[PATH_MAX];
char *p;
char b[PATH_MAX];
strcpy(b, basefilename);
p = b + strlen(basefilename);
*p++ = '.';
int lri = ctx->logRotateInterval;
if (!(lri % (24 * 60 * 60))) {
/* daily resolution */
strcpy(p, "%04d-%02d-%02d");
sprintf(logfn_ts, b, tc.tm_year + 1900, tc.tm_mon + 1, tc.tm_mday);
} else if (!(lri % (60 * 60))) {
/* hourly resolution */
strcpy(p, "%04d-%02d-%02d_%02d");
sprintf(logfn_ts, b, tc.tm_year + 1900, tc.tm_mon + 1, tc.tm_mday, tc.tm_hour);
} else if (!(lri % (60))) {
/* minute resolution */
strcpy(p, "%04d-%02d-%02d_%02d-%02d");
sprintf(logfn_ts, b, tc.tm_year + 1900, tc.tm_mon + 1,
tc.tm_mday, tc.tm_hour, tc.tm_min);
} else {
/* second resolution */
strcpy(p, "%04d-%02d-%02d_%02d-%02d-%02d");
sprintf(logfn_ts, b, tc.tm_year + 1900, tc.tm_mon + 1,
tc.tm_mday, tc.tm_hour, tc.tm_min, tc.tm_sec);
}
return(logfn_ts);
}
/* sr_is_utf8 routine to confirm that a field is utf8 encoded, taken verbatim from:
* https://stackoverflow.com/questions/1031645/how-to-detect-utf-8-in-plain-c
* considered CC-BY-SA. Is this a sufficient attribution?
* https://legalict.com/2016/01/07/what-is-the-license-status-of-stackoverflow-code-snippets/
*/
int sr_is_utf8(const char *string)
{
if (!string)
return 0;
const unsigned char *bytes = (const unsigned char *)string;
while (*bytes) {
if (( // ASCII
// use bytes[0] <= 0x7F to allow ASCII control characters
bytes[0] == 0x09 ||
bytes[0] == 0x0A ||
bytes[0] == 0x0D || (0x20 <= bytes[0] && bytes[0] <= 0x7E)
)
) {
bytes += 1;
continue;
}
if (( // non-overlong 2-byte
(0xC2 <= bytes[0] && bytes[0] <= 0xDF) &&
(0x80 <= bytes[1] && bytes[1] <= 0xBF)
)
) {
bytes += 2;
continue;
}
if (( // excluding overlongs
bytes[0] == 0xE0 &&
(0xA0 <= bytes[1] && bytes[1] <= 0xBF) &&
(0x80 <= bytes[2] && bytes[2] <= 0xBF)
) || ( // straight 3-byte
((0xE1 <= bytes[0] && bytes[0] <= 0xEC) ||
bytes[0] == 0xEE ||
bytes[0] == 0xEF) &&
(0x80 <= bytes[1] && bytes[1] <= 0xBF) &&
(0x80 <= bytes[2] && bytes[2] <= 0xBF)
) || ( // excluding surrogates
bytes[0] == 0xED &&
(0x80 <= bytes[1] && bytes[1] <= 0x9F) &&
(0x80 <= bytes[2] && bytes[2] <= 0xBF)
)
) {
bytes += 3;
continue;
}
if (( // planes 1-3
bytes[0] == 0xF0 &&
(0x90 <= bytes[1] && bytes[1] <= 0xBF) &&
(0x80 <= bytes[2] && bytes[2] <= 0xBF) &&
(0x80 <= bytes[3] && bytes[3] <= 0xBF)
) || ( // planes 4-15
(0xF1 <= bytes[0] && bytes[0] <= 0xF3) &&
(0x80 <= bytes[1] && bytes[1] <= 0xBF) &&
(0x80 <= bytes[2] && bytes[2] <= 0xBF) &&
(0x80 <= bytes[3] && bytes[3] <= 0xBF)
) || ( // plane 16
bytes[0] == 0xF4 &&
(0x80 <= bytes[1] && bytes[1] <= 0x8F) &&
(0x80 <= bytes[2] && bytes[2] <= 0xBF) &&
(0x80 <= bytes[3] && bytes[3] <= 0xBF)
)
) {
bytes += 4;
continue;
}
return 0;
}
return 1;
}
/* end of code from stack exchange */
/* ok, sr_has_vip is inspired by stack exchange, but changed beyond all recognition.
returns 1 if this host has the indicated virtual internet protocol (vip) address.
returns 0 if this host doesn't have it.
returns -1 on error.
supports ipv6, and string can be either a hostname or a numeric one.
*/
int sr_has_vip(char const *vip, struct sr_log_context_s *logctx)
{
struct ifaddrs *ifaddr, *ifa;
char host[NI_MAXHOST];
char addr[NI_MAXHOST];
int family;
if (getifaddrs(&ifaddr) == -1) {
perror("getifaddrs");
return -1;
}
for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) {
if (ifa->ifa_addr == NULL)
continue;
family = ifa->ifa_addr->sa_family;
if ((family != AF_INET) && (family != AF_INET6))
continue;
if (family == AF_INET) {
getnameinfo(ifa->ifa_addr, sizeof(struct sockaddr_in), host,
NI_MAXHOST, NULL, 0, 0);
getnameinfo(ifa->ifa_addr, sizeof(struct sockaddr_in), addr,
NI_MAXHOST, NULL, 0, NI_NUMERICHOST);
} else {
getnameinfo(ifa->ifa_addr, sizeof(struct sockaddr_in6), host,
NI_MAXHOST, NULL, 0, 0);
getnameinfo(ifa->ifa_addr, sizeof(struct sockaddr_in6), addr,
NI_MAXHOST, NULL, 0, NI_NUMERICHOST);
}
//sr_log_msg(logctx,LOG_DEBUG, "sr_has_vip: checking interface %s host=%s addr=%s\n",
// ifa->ifa_name, host, addr);
if (vip && (!strcmp(host, vip) || !strcmp(addr, vip))) {
//sr_log_msg(logctx,LOG_DEBUG, "sr_has_vip: Matched!\n");
return 1;
}
}
sr_log_msg(logctx,LOG_DEBUG, "sr_has_vip: we don't have the vip\n");
freeifaddrs(ifaddr);
return 0;
}
void sr_daemonize(int close_stdout, struct sr_log_context_s *logctx)
/*
fork child, parent then exits. child returns with proper daemon prep done.
*/
{
pid_t pid;
pid_t sid;
pid = fork();
if (pid < 0) {
sr_log_msg(logctx,LOG_CRITICAL, "fork failed, cannot launch as daemon\n");
exit(1);
}
if (pid > 0) {
fprintf(stderr, "parent exiting normally, started child with pid: %d\n", pid);
exit(0);
}
// child processing.
sr_log_msg(logctx,LOG_DEBUG, "child daemonizing start\n");
sid = setsid();
if (sid < 0) {
sr_log_msg(logctx,LOG_WARNING,
"daemonizing, setsid errord, failed to completely dissociate from login process\n");
}
if (!(sr_default_log_initialized))
sr_log_init_context( &null_ctx);
if (logctx->logfd == 2) {
sr_log_msg(logctx,LOG_CRITICAL, "to run as daemon log option must be set.\n");
exit(1);
}
close(0);
if (close_stdout) {
close(1);
dup2(logctx->logfd, STDOUT_FILENO);
}
close(2);
dup2(logctx->logfd, STDERR_FILENO);
sr_log_msg(logctx,LOG_DEBUG, "child daemonizing complete.\n");
}
/* v03 conversion code for base64
*/
static char b64rep(char i)
{
if (i > 64)
fprintf(stderr,
"error in representation: %i should not be input to b64encode from hex\n",
i);
if (i == 63)
return ('/');
if (i == 62)
return ('+');
if (i >= 52)
return (i + '0' - 52);
if (i >= 26)
return (i + 'a' - 26);
return (i + 'A');
}
static char h2b(char i)
{
if (i > 'f')
fprintf(stderr,
"error in representation: %i should not be input to h2b from hex\n", i);
if (i >= 'a')
return (i - 'a' + 10);
if (i > 'F')
fprintf(stderr,
"errror in representation: %i should not be input to h2b from hex\n", i);
if (i >= 'A')
return (i - 'A' + 10);
if (i > '9')
fprintf(stderr,
"errror in representation: %i should not be input to h2b from hex\n", i);
return (i - '0');
}
char *sr_hex2base64(const char *hstr)
{
static char buf[1024];
int hxlen;
unsigned int h, b;
char pad[2];
hxlen = strlen(hstr);
b = 0;
for (h = 0; h < hxlen - 2; h += 3) {
//base64 encoding requires line feed after every 76 chars...
/*
if (!((b-1)%77))
buf[b++]='\n';
*/
// but Sarracenia expects fake line feed...
if ((b > 10) && !((b % 78))) {
buf[b++] = '\\';
buf[b++] = 'n';
}
pad[0] = (h2b(hstr[h]) << 2) | (h2b(hstr[h + 1]) >> 2);
pad[1] = ((h2b(hstr[h + 1]) & 0x03) << 4) | (h2b(hstr[h + 2]));
buf[b++] = b64rep(pad[0]);
buf[b++] = b64rep(pad[1]);
}
if ((hxlen - h) >= 2) {
pad[0] = (h2b(hstr[h]) << 2) | (h2b(hstr[h + 1]) >> 2);
buf[b++] = b64rep(pad[0]);
pad[1] = ((h2b(hstr[h + 1]) & 0x03) << 4);
buf[b++] = b64rep(pad[1]);
} else if ((hxlen - h) == 1) {
pad[0] = (h2b(hstr[h]) << 2);
buf[b++] = b64rep(pad[0]);
}
while (h < hxlen) {
buf[b++] = '=';
h++;
}
buf[b] = '\0';
return (buf);
}
static char raw2hex(char i)
{
if (i > 15)
fprintf(stderr, "error in represenation. hex range only 0-15: %d\n", i);
if (i < 10)
return (i + '0');
return (i + 'a' - 10);
}
static char b642raw(char i)
{
if (i > 'z')
fprintf(stderr, "error in representation: %d invalid \n", i);
if (i >= 'a')
return (i - 'a' + 26);
if (i >= 'A')
return (i - 'A');
if (i == '=')
return (0); // padding.
if (i >= '0')
return (i - '0' + 52);
if (i == '/')
return (63);
if (i == '+')
return (62);
fprintf(stderr, "invalid character in base64 representation: %d\n", i);
return (0);
}
char *sr_base642hex(const char *bstr)
{
static char buf[1024];
int b64len;
int h, b;
b64len = strlen(bstr);
h = 0;
for (b = 0; b < b64len; b += 2) {
if ((bstr[b] == '\\') && (bstr[b + 1] == 'n')) {
b += 2;
}
while (isspace(bstr[b])) {
b++;
}
if (bstr[b] == '=') {
h--;
break;
}
buf[h++] = raw2hex(b642raw(bstr[b]) >> 2);
buf[h++] = raw2hex(((b642raw(bstr[b]) & 0x03) << 2) | b642raw(bstr[b + 1]) >> 4);
buf[h++] = raw2hex(b642raw(bstr[b + 1]) & 0x0f);
}
buf[h] = '\0';
return (buf);
}
/* size of buffer used to read the file content in calculating checksums.
*/
#define SUMBUFSIZE (4096*1024)
// SHA512 being the longest digest...
//static char sumstr[SR_SUMSTRLEN];
//static unsigned char sumhash[SR_SUMHASHLEN];
int sr_get_sumhashlen(char algo)
{
switch (algo) {
case 'd':
case 'n':
return (MD5_DIGEST_LENGTH + 1);
case '0':
return (4 + 1);
case 'p':
case 's':
case 'L':
case 'R':
return (SHA512_DIGEST_LENGTH + 1);
case 'z':
return (2);
default:
return (0);
}
}
char *sr_set_sumstr(char algo, char algoz, const char *fn, const char *partstr,
char *linkstr, unsigned long block_size,
unsigned long block_count, unsigned long block_rem,
unsigned long block_num, int xattr_cc)
/*
return a correct sumstring (assume it is big enough) as per sr_post(7)
algo =
'0' - no checksum, value is random. -> now same as N.
'd' - md5sum of block.
'n' - md5sum of filename (fn).
'L' - now sha512 sum of link value.
'm' - mkdir
'p' - md5sum of filename (fn) + partstr.
'r' - rmdir
'R' - no checksum, value is random. -> now same as N.
's' - sha512 sum of block.
'z' - downstream should recalculate with algo that is argument.
block starts at block_size * block_num, and ends
*/
{
EVP_MD_CTX *ctx;
const EVP_MD *md;
char *sumstrptr;
//static char sumstr[SR_SUMSTRLEN];
unsigned int hashlen = 0;
static int fd;
static char buf[SUMBUFSIZE];
static unsigned char sumhash[SR_SUMHASHLEN];
long bytes_read;
long how_many_to_read;
const char *just_the_name = NULL;
unsigned long start = block_size * block_num;
unsigned long end;
end = start + ((block_num < (block_count - (block_rem != 0))) ? block_size : block_rem);
memset(sumhash, 0, SR_SUMHASHLEN);
sumhash[0] = algo;
/* xattr check for checksum caching optimization */
struct stat attr;
time_t stat_mtime;
char cache_mtime[SR_TIMESTRLEN];
stat(fn, &attr);
stat_mtime = attr.st_mtime;
sumstrptr = (char*)malloc(SR_SUMSTRLEN);
memset(cache_mtime, 0, SR_TIMESTRLEN);
// are xattrs set?
if (xattr_cc && (getxattr(fn, "user.sr_mtime", cache_mtime, SR_TIMESTRLEN) > 0)) {
// is the checksum valid? (i.e. is (cache_mtime >= stat_mtime)? )
if (sr_str2time(cache_mtime)->tv_sec >= stat_mtime) {
memset(sumstrptr, 0, SR_SUMSTRLEN);
getxattr(fn, "user.sr_sum", sumstrptr, SR_SUMSTRLEN);
// is it the right checksum algorithm?
if (algo == sumstrptr[0])
return (sumstrptr);
}
}
/* end of xattr check */
switch (algo) {
case '0':
sprintf(sumstrptr, "%c,%03ld", algo, random() % 1000);
break;
case 'd':
ctx = EVP_MD_CTX_create();
md = EVP_md5();
EVP_DigestInit_ex(ctx, md, NULL);
// keep file open through repeated calls.
//fprintf( stderr, "opening %s to checksum\n", fn );
fd = open(fn, O_RDONLY);
if (fd < 0) {
fprintf(stderr, "unable to read file for checksumming\n");
strcpy(sumstrptr + 3, "deadbeef0");
return (NULL);
}
lseek(fd, start, SEEK_SET);
//fprintf( stderr, "checksumming start: %lu to %lu\n", start, end );
while (start < end) {
how_many_to_read =
(SUMBUFSIZE < (end - start)) ? SUMBUFSIZE : (end - start);
bytes_read = read(fd, buf, how_many_to_read);
if (bytes_read > 0) {
EVP_DigestUpdate(ctx, buf, bytes_read);
start += bytes_read;
} else {
fprintf(stderr, "error reading %s for MD5\n", fn);
close(fd);
return (NULL);
}
}
close(fd);
EVP_DigestFinal_ex(ctx, sumhash + 1, &hashlen);
sr_hash2sumstr(sumstrptr, sumhash);
break;
case 'm': // mkdir
ctx = EVP_MD_CTX_create();
md = EVP_md5();
EVP_DigestInit_ex(ctx, md, NULL);
just_the_name = just_the_name ? just_the_name + 1 : fn;
EVP_DigestUpdate(ctx, just_the_name, strlen(just_the_name));
EVP_DigestFinal_ex(ctx, sumhash + 1, &hashlen);
sr_hash2sumstr(sumstrptr, sumhash);
break;
case 'r': // rmdir
ctx = EVP_MD_CTX_create();
md = EVP_md5();
EVP_DigestInit_ex(ctx, md, NULL);
just_the_name = just_the_name ? just_the_name + 1 : fn;
EVP_DigestUpdate(ctx, just_the_name, strlen(just_the_name));
EVP_DigestFinal_ex(ctx, sumhash + 1, &hashlen);
sr_hash2sumstr(sumstrptr, sumhash);
break;
case 'n':
ctx = EVP_MD_CTX_create();
md = EVP_md5();
EVP_DigestInit_ex(ctx, md, NULL);
just_the_name = just_the_name ? just_the_name + 1 : fn;
EVP_DigestUpdate(ctx, just_the_name, strlen(just_the_name));
EVP_DigestFinal_ex(ctx, sumhash + 1, &hashlen);
sr_hash2sumstr(sumstrptr, sumhash);
break;
case 'L': // symlink case
just_the_name = linkstr;
ctx = EVP_MD_CTX_create();
md = EVP_sha512();
EVP_DigestInit_ex(ctx, md, NULL);
EVP_DigestUpdate(ctx, linkstr, strlen(linkstr));
EVP_DigestFinal_ex(ctx, sumhash + 1, &hashlen);
sr_hash2sumstr(sumstrptr, sumhash);
break;
case 'R': // null, or removal.
just_the_name = rindex(fn, '/') + 1;
just_the_name = just_the_name ? just_the_name + 1 : fn;
ctx = EVP_MD_CTX_create();
md = EVP_sha512();
EVP_DigestInit_ex(ctx, md, NULL);
EVP_DigestUpdate(ctx, just_the_name, strlen(just_the_name));
EVP_DigestFinal_ex(ctx, sumhash + 1, &hashlen);
sr_hash2sumstr(sumstrptr, sumhash);
break;
case 'p':
ctx = EVP_MD_CTX_create();
md = EVP_sha512();
EVP_DigestInit_ex(ctx, md, NULL);
just_the_name = rindex(fn, '/') + 1;
just_the_name = just_the_name ? just_the_name + 1 : fn;
strcpy(buf, just_the_name);
sprintf(buf, "%s%c,%lu,%lu,%lu,%lu", just_the_name, algo,
block_size, block_count, block_rem, block_num);
EVP_DigestUpdate(ctx, buf, strlen(buf));
EVP_DigestFinal_ex(ctx, sumhash + 1, &hashlen);
sr_hash2sumstr(sumstrptr, sumhash);
break;
case 's':
ctx = EVP_MD_CTX_create();
md = EVP_sha512();
EVP_DigestInit_ex(ctx, md, NULL);
fd = open(fn, O_RDONLY);
if (fd < 0) {
fprintf(stderr, "unable to read file for SHA checksumming\n");
return (NULL);
}
lseek(fd, start, SEEK_SET);
//fprintf( stderr, "DBG checksumming start: %lu to %lu\n", start, end );
while (start < end) {
how_many_to_read =
(SUMBUFSIZE < (end - start)) ? SUMBUFSIZE : (end - start);
bytes_read = read(fd, buf, how_many_to_read);
//fprintf( stderr, "checksumming how_many_to_read: %lu bytes_read: %lu\n",
// how_many_to_read, bytes_read );
if (bytes_read >= 0) {
EVP_DigestUpdate(ctx, buf, bytes_read);
start += bytes_read;
} else {
fprintf(stderr, "error reading %s for SHA\n", fn);
close(fd);
return (NULL);
}
}
close(fd);
EVP_DigestFinal_ex(ctx, sumhash + 1, &hashlen);
sr_hash2sumstr(sumstrptr, sumhash);
break;
case 'z':
sumhash[1] = algoz;
sumhash[2] = '\0';
sr_hash2sumstr(sumstrptr, sumhash);
break;
default:
fprintf(stderr, "sum algorithm %c unimplemented\n", algo);
return (NULL);
}
/* xattr set for checksum caching optimization */
if (xattr_cc) {
// can we set xattrs? let's try and find out!
setxattr(fn, "user.sr_sum", sumstrptr, strlen(sumstrptr), 0);
char *t2s = sr_time2str(&attr.st_mtim);
setxattr(fn, "user.sr_mtime", t2s, strlen(t2s), 0);
// if the calls above fail, ignore and proceed
}
/* end of xattr set */
return (sumstrptr);
}
static char nibble2hexchr(int i)
{
unsigned char c = (unsigned char)(i & 0xf);
return ((char)((c < 10) ? (c + '0') : (c - 10 + 'a')));
}
static int hexchr2nibble(char c)
/* return ordinal value of digit assuming a character set that has a-f sequential in both lower and upper case.
kind of based on ASCII, because numbers are assumed to be lower in collation than upper and lower case letters.
*/
{
if (c < ':')
return (c - '0');
if (c < 'G')
return (c - 'A' + 10);
if (c < 'g')
return (c - 'a' + 10);
return (-1);
}
unsigned char *sr_sumstr2hash(unsigned char *sumhash, const char *s)
{
int i;
//static unsigned char sumhash[SR_SUMHASHLEN];
if (!s)
return (NULL);
memset(sumhash, 0, SR_SUMHASHLEN);
sumhash[0] = s[0];
if (s[0] == 'z') {
sumhash[1] = s[2];
return (sumhash);
}
for (i = 1; (i < sr_get_sumhashlen(s[0])); i++) {
sumhash[i] =
(unsigned char)((hexchr2nibble(s[i << 1]) << 4) +
hexchr2nibble(s[(i << 1) + 1]));
}
return (sumhash);
}
char *sr_hash2sumstr(char *sumstr, const unsigned char *h)
{
int i;
//static char sumstr[SR_SUMSTRLEN];
memset(sumstr, 0, SR_SUMSTRLEN);
sumstr[0] = h[0];
sumstr[1] = ',';
if (sumstr[0] == 'z') {
sumstr[2] = h[1];
sumstr[3] = '\0';;
return (sumstr);
}
for (i = 1; i < sr_get_sumhashlen(h[0]); i++) {
sumstr[i * 2] = nibble2hexchr(h[i] >> 4);
sumstr[i * 2 + 1] = nibble2hexchr(h[i]);
}
sumstr[2 * i] = '\0';
return (sumstr);
}
char *sr_time2str(struct timespec *tin)
{
/* turn a timespec into an 18 character sr_post(7) conformant time stamp string.
if argument is NULL, then the string should correspond to the current system time.
*/
static char time2str_result[SR_TIMESTRLEN + 30];
struct tm s;
time_t when;
struct timespec ts;
long nsec;
char nsstr[10];
int nsl;
memset(&s, 0, sizeof(struct tm));
memset(&ts, 0, sizeof(struct timespec));
if (tin) {
when = tin->tv_sec;
nsec = tin->tv_nsec;
} else {
clock_gettime(CLOCK_REALTIME, &ts);
when = ts.tv_sec;
nsec = ts.tv_nsec;