-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathuser_BM.c
2376 lines (2297 loc) · 91.8 KB
/
user_BM.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
// file user_BM.c; management of European GDPR related personal data
// for Bismon contributors; see also userlogin.md
// SPDX-License-Identifier: GPL-3.0-or-later
/***
BISMON
Copyright © 2018 - 2021 CEA (Commissariat à l'énergie atomique et
aux énergies alternatives)
contributed by Basile Starynkevitch (working at CEA, LIST, France)
This program 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 of the License, or
(at your option) any later version.
This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
----
Contact me (Basile Starynkevitch) by email
***/
#include "bismon.h"
#include "user_BM.const.h"
static const char *quickfindcontriboid_bm;
static void
write_password_file_BM (FILE * passfil, objectval_tyBM * assocobarg,
struct stackframe_stBM *stkf);
////////////////////////////////////////////////////////////////
//// user support; might be relevant to GDPR
void
usergcmark_BM (struct garbcoll_stBM *gc,
struct user_stBM *us, objectval_tyBM * fromob, int depth)
{
ASSERT_BM (gc && gc->gc_magic == GCMAGIC_BM);
ASSERT_BM (valtype_BM ((value_tyBM) us) == typayl_user_BM);
ASSERT_BM (fromob == NULL || isobject_BM (fromob));
ASSERT_BM (!fromob || !us->user_ownobj || us->user_ownobj == fromob);
ASSERT_BM (depth >= 0);
uint8_t oldmark = ((typedhead_tyBM *) us)->hgc;
if (oldmark)
return;
((typedhead_tyBM *) us)->hgc = MARKGC_BM;
gc->gc_nbmarks++;
DBGPRINTF_BM
("usergcmark_BM us@%p, usname %s, fromob %s, usownobj %s, depth %d", us,
bytstring_BM (us->user_namev), objectdbg_BM (fromob),
objectdbg1_BM (us->user_ownobj), depth);
if (us->user_ownobj)
gcobjmark_BM (gc, us->user_ownobj);
if (us->user_namev)
EXTENDEDGCPROC_BM (gc, us->user_namev, fromob, depth + 1);
if (us->user_emailv)
EXTENDEDGCPROC_BM (gc, us->user_emailv, fromob, depth + 1);
} /* end usergcmark_BM */
void
usergcdestroy_BM (struct garbcoll_stBM *gc, struct user_stBM *us)
{
ASSERT_BM (gc && gc->gc_magic == GCMAGIC_BM);
ASSERT_BM (valtype_BM ((value_tyBM) us) == typayl_user_BM);
DBGPRINTF_BM ("usergcdestroy_BM us@%p, usname %s, usemail %s, usownobj %s",
us, bytstring_BM (us->user_namev),
bytstring_BM (us->user_emailv),
objectdbg1_BM (us->user_ownobj));
if (us->user_ownobj)
{
objectval_tyBM *ownerob = us->user_ownobj;
us->user_ownobj = NULL;
objlock_BM (ownerob);
if (objpayload_BM (ownerob) == (extendedval_tyBM) us)
objclearpayload_BM (ownerob);
objunlock_BM (ownerob);
}
memset ((void *) us, 0, sizeof (*us));
free (us);
gc->gc_freedbytes += sizeof (*us);
} /* end usergcdestroy_BM */
void
usergckeep_BM (struct garbcoll_stBM *gc, struct user_stBM *us)
{
ASSERT_BM (gc && gc->gc_magic == GCMAGIC_BM);
ASSERT_BM (valtype_BM ((value_tyBM) us) == typayl_user_BM);
DBGPRINTF_BM ("usergckeep_BM us@%p, usname %s usemail %s, usownobj %s",
us, bytstring_BM (us->user_namev),
bytstring_BM (us->user_emailv),
objectdbg1_BM (us->user_ownobj));
gc->gc_keptbytes += sizeof (*us);
} /* end usergckeep_BM */
const stringval_tyBM *
objcontributornamepayl_BM (const objectval_tyBM * obj)
{
if (!objhascontributorpayl_BM (obj))
return NULL;
struct user_stBM *usp = objpayload_BM (obj);
if (valtype_BM ((extendedval_tyBM) usp) != typayl_user_BM)
return NULL;
return usp->user_namev;
} /* end objcontributornamepayl_BM */
const stringval_tyBM *
objcontributoremailpayl_BM (const objectval_tyBM * obj)
{
if (!objhascontributorpayl_BM (obj))
return NULL;
struct user_stBM *usp = objpayload_BM (obj);
if (valtype_BM ((extendedval_tyBM) usp) != typayl_user_BM)
return NULL;
return usp->user_emailv;
} /* end objcontributoremailpayl_BM */
static void
overwrite_contributor_file_BM (FILE * fil,
objectval_tyBM * assocobarg,
struct stackframe_stBM *stkf)
{
LOCALFRAME_BM ( /*prev: */ stkf, /*descr: */ NULL,
objectval_tyBM * contribob; //current contributor object
objectval_tyBM * assocob; // assoc-object
// mapping
// contributors-objects to
// *contributors(<email>,<alias>)
// nodes
value_tyBM namev; // the name string
value_tyBM emailv; // the email string value
value_tyBM aliasv; // the alias string value
value_tyBM nodev; // the node
value_tyBM keysetv; // the set of keys
value_tyBM tmpv; // temporary, for debugging
);
_.assocob = assocobarg;
ASSERT_BM (isobject_BM (assocobarg) && objhasassocpayl_BM (assocobarg));
rewind (fil);
int nbcontrib = 0;
_.keysetv = (value_tyBM) objassocsetattrspayl_BM (_.assocob);
if (showdebugmsg_BM)
{
char contidbuf[32];
memset (contidbuf, 0, sizeof (contidbuf));
idtocbuf32_BM (objid_BM (BMP_contributors), contidbuf);
_.tmpv = (value_tyBM) objhashsettosetpayl_BM (BMP_contributors);
DBGPRINTF_BM
("overwrite_contributor_file assocob %s set-contrib %s keyset %s",
objectdbg_BM (_.assocob), OUTSTRVALUE_BM (_.tmpv),
OUTSTRVALUE_BM (_.keysetv));
_.tmpv = NULL;
}
{
nbcontrib = objassocnbkeyspayl_BM (_.assocob);
ASSERT_BM ((int) setcardinal_BM (_.keysetv) == nbcontrib);
char nowtimbuf[80];
memset (nowtimbuf, 0, sizeof (nowtimbuf));
time_t nowt = 0;
time (&nowt);
struct tm nowtm = { };
memset (&nowtm, 0, sizeof (nowtm));
localtime_r (&nowt, &nowtm);
strftime (nowtimbuf, sizeof (nowtimbuf), "%c", &nowtm);
char *baspath = basename (contributors_filepath_BM);
if (strcmp (baspath, CONTRIBUTORS_FILE_BM))
fprintf (fil, "## BISMON contributors file %s (really %s)\n",
CONTRIBUTORS_FILE_BM, contributors_filepath_BM);
else
fprintf (fil, "## BISMON contributors file %s\n", CONTRIBUTORS_FILE_BM);
fprintf (fil,
"## when BISMON is running, don't edit manually this file; it could be flock-ed.\n");
fprintf (fil,
"## use preferably the --contributor or --remove-contributor BISMON ...\n");
fprintf (fil,
"## ... program options of BISMON to change that file at startup\n");
fprintf (fil, "###############################################\n");
fprintf (fil, "## written by BISMON built at %s\n", bismon_timestamp);
fprintf (fil, "## BISMON lastgitcommit %s\n", bismon_lastgitcommit);
fprintf (fil, "## BISMON checksum %s\n", bismon_checksum);
fprintf (fil, "##- emitted at %s on %s for %d contributors.\n", nowtimbuf,
myhostname_BM, nbcontrib);
fprintf (fil,
"## format: one login line per user or contributor like:\n");
fprintf (fil, "## <user-name>;<oid>;<email>;<alias>\n");
for (int cix = 0; cix < nbcontrib; cix++)
{
_.contribob = setelemnth_BM (_.keysetv, cix);
_.nodev = objassocgetattrpayl_BM (_.assocob, _.contribob);
ASSERT_BM (nodeconn_BM (_.nodev) == BMP_contributors
&& nodewidth_BM (_.nodev) == 2);
_.emailv = nodenthson_BM (_.nodev, 0);
_.aliasv = nodenthson_BM (_.nodev, 1);
objlock_BM (_.contribob);
_.namev = (value_tyBM) objcontributornamepayl_BM (_.contribob);
objunlock_BM (_.contribob);
char idbuf[32];
memset (idbuf, 0, sizeof (idbuf));
idtocbuf32_BM (objid_BM (_.contribob), idbuf);
ASSERT_BM (isstring_BM (_.namev));
ASSERT_BM (isstring_BM (_.emailv));
ASSERT_BM (!_.aliasv || isstring_BM (_.aliasv));
ASSERT_BM (valid_contributor_name_BM (bytstring_BM (_.namev), NULL));
ASSERT_BM (valid_email_BM
(bytstring_BM (_.emailv), DONTCHECKDNS_BM, NULL));
ASSERT_BM (!_.aliasv
|| valid_email_BM (bytstring_BM (_.aliasv),
DONTCHECKDNS_BM, NULL));
if (!_.aliasv)
fprintf (fil, "%s;%s;%s;\n", bytstring_BM (_.namev), idbuf,
bytstring_BM (_.emailv));
else
fprintf (fil, "%s;%s;%s;%s\n", bytstring_BM (_.namev), idbuf,
bytstring_BM (_.emailv), bytstring_BM (_.aliasv));
}
fprintf (fil, "#### end of file %s with %d contributors sorted by oid\n",
CONTRIBUTORS_FILE_BM, nbcontrib);
}
} /* end overwrite_contributor_file_BM */
static objectval_tyBM *add_contributor_name_email_alias_BM
(const char *name,
const char *email,
const char *alias, char **perrmsg, struct stackframe_stBM *stkf);
bool
valid_email_BM (const char *email, bool checkdns, char **perrmsg)
{
if (perrmsg)
*perrmsg = NULL;
if (!email)
{
if (perrmsg)
{
if (asprintf (perrmsg, "no email") < 0)
FATAL_BM ("asprintf failure for email - %m");
return false;
}
}
const char *end = NULL;
if (!g_utf8_validate (email, -1, &end) && end && *end)
{
if (perrmsg)
if (asprintf (perrmsg, "invalid utf8 email %s", email) < 0)
FATAL_BM ("asprintf failure for utf8 email - %m");
return false;
}
if (!isalpha (email[0]))
{
if (perrmsg)
if (asprintf
(perrmsg, "mail address %s don't start with letter", email) < 0)
FATAL_BM ("asprintf failure for mail non-letter - %m");
return false;
}
const char *at = strchr (email, '@');
if (!at || !at[1])
{
if (perrmsg)
if (asprintf (perrmsg, "mail address %s don't have at-sign", email) <
0)
FATAL_BM ("asprintf failure for mail without at - %m");
return false;
}
if (strchr (at + 1, '@'))
{
if (perrmsg)
if (asprintf (perrmsg, "mail address %s has more than one at-sign",
email) < 0)
FATAL_BM ("asprintf failure for bad mail - %m");;
return false;
}
for (const char *pc = email; pc < at; pc++)
{
if (isalnum (*pc))
continue;
if (*pc == '.' || *pc == '+' || *pc == '-' || *pc == '_')
{
if (!isalnum (pc[-1]) || !isalnum (pc[1]))
{
if (perrmsg)
if (asprintf (perrmsg,
"mail address %s with bad . + - or _ occurrences before at-sign",
email) < 0)
FATAL_BM ("asprintf fail for bad punctuation in email");
return false;
}
else
continue;
}
if (perrmsg)
if (asprintf (perrmsg,
"mail address %s with unexpected characters before at-sign\n",
email) < 0)
FATAL_BM
("asprintf failure for unexpected characters before at-sign in email %m");
return false;
}
for (const char *pc = at + 1; pc < at; pc++)
{
if (isalnum (*pc))
continue;
if (*pc == '.' || *pc == '+' || *pc == '-' || *pc == '_')
{
if (!isalnum (pc[-1]) || !isalnum (pc[1]))
{
if (perrmsg)
if (asprintf (perrmsg,
"mail address %s with bad . + - or _ occurrences after at-sign",
email) < 0)
FATAL_BM
("asprintf failure for bad punctuation in email - %m");
return false;
}
else
continue;
}
if (perrmsg)
if (asprintf (perrmsg,
"mail address %s with unexpected characters after at-sign",
email) < 0)
FATAL_BM
("asprintf failure for unexpected characters after at-sign in email - %m");
return false;
}
if (!strcmp (at + 1, "fake.email") || !strcmp (at + 1, "localhost"))
return true;
if (checkdns)
{
struct addrinfo *res = NULL;
int err = getaddrinfo (at + 1, "mail", NULL, &res);
if (err)
{
if (perrmsg)
if (asprintf (perrmsg,
"mail address %s domain %s failed on getaddrinfo: %s",
email, at + 1, gai_strerror (err)) < 0)
FATAL_BM ("asprintf failure on mail address - %m");
return false;
}
if (res)
freeaddrinfo (res);
else
{
if (perrmsg)
if (asprintf (perrmsg,
"mail address %s domain %s invalid\n", email,
at + 1) < 0)
FATAL_BM ("asprintf failure for invalid mail & domain - %m");
return false;
}
}
return true;
} /* end valid_email_BM */
bool
valid_contributor_name_BM (const char *name, char **perrmsg)
{
if (perrmsg)
*perrmsg = NULL;
if (!name)
{
if (perrmsg)
if (asprintf (perrmsg, "no contributor name") < 0)
FATAL_BM ("asprintf failure for no contributor name - %m");
return false;
}
const char *end = NULL;
if (!g_utf8_validate (name, -1, &end) && end && *end)
{
if (perrmsg)
if (asprintf (perrmsg, "invalid utf8 contributor name %s", name) < 0)
FATAL_BM
("asprintf failure for invalid utf8 contributor name - %m");
return false;
}
// validate the name:
gunichar uc = 0;
gunichar prevuc = 0;
for (const char *p = name;
*p && (uc = g_utf8_get_char (p)) != 0;
(p = g_utf8_next_char (p)), (prevuc = uc))
{
if (g_unichar_isalpha (uc))
continue;
else if (g_unichar_isalnum (uc) && p > name)
continue;
else
if ((uc == '_' || uc == '-' || uc == '+' || uc == ' ')
&& g_unichar_isalnum (prevuc) && *g_utf8_next_char (p))
continue;
if (perrmsg)
if (asprintf (perrmsg, "invalid contributor name '%s'", name) < 0)
FATAL_BM ("asprintf failure for invalid contributor name - %m");
return false;
}
return true;
} /* end valid_contributor_name_BM */
objectval_tyBM *
find_contributor_BM (const char *str, struct stackframe_stBM *stkf)
{
LOCALFRAME_BM ( /*prev: */ stkf, /*descr: */ NULL,
objectval_tyBM * contribob; //current contributor object
);
if (!str || !str[0])
return NULL;
bool withemail = valid_email_BM (str, DONTCHECKDNS_BM, NULL);
DBGPRINTF_BM ("find_contributor str='%s' %s email", str,
withemail ? "with" : "without");
if (!withemail)
{
char *errmsg = NULL;
if (!valid_contributor_name_BM (str, &errmsg))
{
if (errmsg)
{
WARNPRINTF_BM ("contributor name %s is not valid: %s", str,
errmsg);
free (errmsg), errmsg = NULL;
}
else
WARNPRINTF_BM ("invalid contributor name %s", str);
return NULL;
}
}
FILE *fil = fopen (contributors_filepath_BM, "r+");
if (!fil)
FATAL_BM ("find_contributor_BM cannot open contributors file %s : %m",
contributors_filepath_BM);
int fd = fileno (fil);
if (flock (fd, LOCK_EX))
FATAL_BM ("failed to flock fd#%d for contributors file %s", fd,
contributors_filepath_BM);
size_t linsiz = 128;
char *linbuf = calloc (linsiz, 1);
int lincnt = 0;
if (!linbuf)
FATAL_BM ("find_contributor_BM can't alloc line of %zd bytes", linsiz);
/// read loop
for (;;)
{
ssize_t linlen = getline (&linbuf, &linsiz, fil);
if (linlen < 0)
break;
lincnt++;
if (linbuf[0] == '#' || linbuf[0] == '\n' || !linbuf[0])
continue;
if (linlen > 0 && linbuf[linlen - 1] == '\n')
linbuf[linlen - 1] = (char) 0;
const char *end = NULL;
if (!g_utf8_validate (linbuf, -1, &end) && end && *end)
FATAL_BM ("in %s line#%d is invalid UTF8: %s",
contributors_filepath_BM, lincnt, linbuf);
// linbuf should be like: <username>;<oid>;<email>;<alias>
char *semcol1 = strchr (linbuf, ';');
char *semcol2 = semcol1 ? strchr (semcol1 + 1, ';') : NULL;
char *semcol3 = semcol2 ? strchr (semcol2 + 1, ';') : NULL;
if (!semcol3)
FATAL_BM
("in %s line#%d should be like <username>;<oid>;<email>;<alias> but is %s",
contributors_filepath_BM, lincnt, linbuf);
*semcol1 = (char) 0;
*semcol2 = (char) 0;
*semcol3 = (char) 0;
const char *curcontrib = linbuf;
const char *curoidstr = semcol1 + 1;
const char *curemail = semcol2 + 1;
const char *curalias = semcol3 + 1;
DBGPRINTF_BM
("find_contributor line#%d curcontrib '%s' curoidstr '%s' curemail '%s' curalias '%s'",
lincnt, curcontrib, curoidstr, curemail, curalias);
if ((withemail && !strcmp (curemail, str))
|| (!withemail && !strcmp (curcontrib, str)))
{
const char *endid = NULL;
rawid_tyBM curid = parse_rawid_BM (curoidstr, &endid);
if (!endid || *endid || !curid.id_hi || !curid.id_lo)
FATAL_BM ("in %s line#%d has invalid oid %s for contributor %s",
contributors_filepath_BM, lincnt, curoidstr,
curcontrib);
_.contribob = findobjofid_BM (curid);
DBGPRINTF_BM ("find_contributor str '%s' contribob %s", str,
objectdbg_BM (_.contribob));
break;
}
} // end forloop
if (flock (fd, LOCK_UN))
FATAL_BM ("failed to un-flock fd#%d for %s", fd,
contributors_filepath_BM);
DBGPRINTF_BM ("find_contributor str '%s' contribob %s", str,
objectdbg_BM (_.contribob));
if (_.contribob)
{
bool okcontrib = false;
objlock_BM (_.contribob);
okcontrib = objhascontributorpayl_BM (_.contribob);
objunlock_BM (_.contribob);
DBGPRINTF_BM
("find_contributor contribob %s has %s contributor payload",
objectdbg_BM (_.contribob), okcontrib ? "a" : "no");
if (!okcontrib)
_.contribob = NULL;
}
DBGPRINTF_BM ("find_contributor str '%s' contribob %s", str,
objectdbg_BM (_.contribob));
if (_.contribob)
{
objlock_BM (BMP_contributors);
if (!objhashsetcontainspayl_BM (BMP_contributors, _.contribob))
{
DBGPRINTF_BM ("find_contributor contribob %s not in `contributors`",
objectdbg_BM (_.contribob));
_.contribob = NULL;
}
objunlock_BM (BMP_contributors);
}
// sleep a tiny random amount of time, to make this call more
// unpredictable to outside and a bit costly
usleep (100 + g_random_int () % 1024);
DBGPRINTF_BM ("find_contributor_BM str '%s' gives %s",
str, objectdbg_BM (_.contribob));
return _.contribob;
} /* end find_contributor_BM */
#define LOADEDCONTRIBDATAMAGIC_BM 0x3d10b193 /*1024504211 */
struct loadedcontribdata_bm
{
unsigned lcda_magic;
const char *lcda_curcontrib;
const char *lcda_rcpath;
rawid_tyBM lcda_curid;
int lcda_lincnt;
int lcda_curlineno;
objectval_tyBM **lcda_pcontribob;
value_tyBM *lcda_pnamev;
objectval_tyBM **lcda_phsetob;
const char *lcda_curoidstr;
const char *lcda_curemail;
const char *lcda_curalias;
};
static void
handle_loaded_contributor_bm (struct stackframe_stBM *stkf,
struct loadedcontribdata_bm *ldat);
// this check is done only once, during load...
void
check_and_load_contributors_file_BM (struct loader_stBM *ld,
struct stackframe_stBM *stkf)
{
struct stat mystat = { };
LOCALFRAME_BM ( /*prev: */ stkf, /*descr: */ NULL,
objectval_tyBM * contribob; //current contributor object
objectval_tyBM * hsetob; //hash set of parsed contributors
value_tyBM namev; /* string value, name of contributor object */
value_tyBM contribsetv; /* set of keys in hsetob */
value_tyBM tmpv; /* temporary */
);
ASSERT_BM (valtype_BM ((const value_tyBM) ld) == typayl_loader_BM);
ASSERT_BM (ld->ld_magic == LOADERMAGIC_BM);
if (!ld || ld->ld_magic != LOADERMAGIC_BM)
FATAL_BM
("check_and_load_contributors_file_BM invalid loader for path %s",
contributors_filepath_BM);
const char *rcpath = contributors_filepath_BM;
ASSERT_BM (rcpath != NULL && rcpath[0] == '/');
FILE *fil = fopen (rcpath, "r+");
if (!fil)
{
FATAL_BM ("cannot open contributors file %s : %m", rcpath);
return;
}
if (showdebugmsg_BM)
{
char contidbuf[32];
memset (contidbuf, 0, sizeof (contidbuf));
idtocbuf32_BM (objid_BM (BMP_contributors), contidbuf);
_.tmpv = (value_tyBM) objhashsettosetpayl_BM (BMP_contributors);
DBGBACKTRACEPRINTF_BM
("check_and_load_contributors_file_BM rcpath='%s'\n"
"... contributors=%s/%s with set %s",
rcpath, objectdbg_BM (BMP_contributors), contidbuf,
OUTSTRVALUE_BM (_.tmpv));
}
_.tmpv = NULL;
_.hsetob = makeobj_BM ();
int fd = fileno (fil);
memset (&mystat, 0, sizeof (mystat));
if (fstat (fd, &mystat))
FATAL_BM ("failed to fstat fd#%d for %s", fd, rcpath);
// the file size gives a guess on the initial size of hashset
objputhashsetpayl_BM (_.hsetob, prime_above_BM (mystat.st_size / 64 + 10));
if (flock (fd, LOCK_EX))
FATAL_BM ("failed to flock fd#%d for %s", fd, rcpath);
size_t linsiz = 128;
char *linbuf = calloc (linsiz, 1);
int lincnt = 0;
int nbcontrib = 0;
if (!linbuf)
FATAL_BM
("check_and_load_contributors_file_BM can't alloc line of %zd bytes",
linsiz);
/// read loop
for (;;)
{
ASSERT_BM (objhashashsetpayl_BM (_.hsetob));
_.contribob = NULL;
_.namev = NULL;
ssize_t linlen = getline (&linbuf, &linsiz, fil);
if (linlen < 0)
break;
lincnt++;
if (linbuf[0] == '#' || linbuf[0] == '\n' || !linbuf[0])
continue;
if (linlen > 0 && linbuf[linlen - 1] == '\n')
linbuf[linlen - 1] = (char) 0;
const char *end = NULL;
if (!g_utf8_validate (linbuf, -1, &end) && end && *end)
FATAL_BM ("in %s line#%d is invalid UTF8: %s", rcpath,
lincnt, linbuf);
DBGPRINTF_BM ("contributor line#%d: %s", lincnt, linbuf);
// linbuf should be like: <username>;<oid>;<email>;<alias>
char *semcol1 = strchr (linbuf, ';');
char *semcol2 = semcol1 ? strchr (semcol1 + 1, ';') : NULL;
char *semcol3 = semcol2 ? strchr (semcol2 + 1, ';') : NULL;
if (!semcol3)
FATAL_BM
("in %s line#%d should be like <username>;<oid>;<email>;<alias> but is %s",
rcpath, lincnt, linbuf);
*semcol1 = (char) 0;
*semcol2 = (char) 0;
*semcol3 = (char) 0;
const char *curcontrib = linbuf;
const char *curoidstr = semcol1 + 1;
const char *curemail = semcol2 + 1;
const char *curalias = semcol3 + 1;
DBGPRINTF_BM
("check_and_load_contributors_file line#%d curcontrib '%s' curoidstr '%s' curemail '%s' curalias '%s'",
lincnt, curcontrib, curoidstr, curemail, curalias);
char *errmsg = NULL;
if (!valid_contributor_name_BM (curcontrib, &errmsg))
FATAL_BM ("in %s line#%d has invalid contributor name %s : %s",
rcpath, lincnt, curcontrib, errmsg);
const char *endid = NULL;
rawid_tyBM curid = parse_rawid_BM (curoidstr, &endid);
if (!endid || *endid || !curid.id_hi || !curid.id_lo)
FATAL_BM ("in %s line#%d has invalid oid %s for contributor %s",
rcpath, lincnt, curoidstr, curcontrib);
if (!valid_email_BM (curemail, DONTCHECKDNS_BM, &errmsg))
FATAL_BM
("in %s line#%d has invalid email %s for contributor %s : %s",
rcpath, lincnt, curemail, curcontrib, errmsg);
if (curalias[0] && !isspace (curalias[0])
&& !valid_email_BM (curalias, DONTCHECKDNS_BM, &errmsg))
FATAL_BM
("in %s line#%d has invalid alias %s for contributor %s : %s",
rcpath, lincnt, curalias, curcontrib, errmsg);
if (quickfindcontriboid_bm)
{
if (!strcmp (quickfindcontriboid_bm, curoidstr))
{
printf ("%s\t%s\t%s\t%s\n",
/*full name: */ curcontrib,
/*objid: */ curoidstr,
/*email: */ curemail,
/*alias: */ curalias ? curalias : "");
fflush (NULL);
exit (EXIT_SUCCESS);
}
}
else
{
struct loadedcontribdata_bm lcdata =
{.lcda_magic = LOADEDCONTRIBDATAMAGIC_BM,
.lcda_rcpath = rcpath,
.lcda_curcontrib = curcontrib,
.lcda_curid = curid,
.lcda_lincnt = lincnt,
.lcda_curlineno = __LINE__,
.lcda_pcontribob = &_.contribob,
.lcda_pnamev = &_.namev,
.lcda_phsetob = &_.hsetob,
.lcda_curoidstr = curoidstr,
.lcda_curemail = curemail,
.lcda_curalias = curalias,
};
handle_loaded_contributor_bm (CURFRAME_BM, &lcdata);
};
_.namev = NULL;
_.contribob = NULL;
nbcontrib++;
}
if (flock (fd, LOCK_UN))
FATAL_BM ("failed to un-flock fd#%d for %s", fd, rcpath);
DBGPRINTF_BM ("check_and_load_contributors_file nbcontrib=%d", nbcontrib);
// check that all the contributors in BMP_contributors are also in hsetob
_.contribob = NULL;
_.contribsetv = (value_tyBM) objhashsettosetpayl_BM (BMP_contributors);
if (!isset_BM (_.contribsetv))
FATAL_BM ("the `contributors` object has no hashset payload as expected");
DBGPRINTF_BM ("check_and_load_contributors_file contribsetv=%s",
OUTSTRVALUE_BM (_.contribsetv));
int csetsiz = setcardinal_BM (_.contribsetv);
ASSERT_BM (objhashashsetpayl_BM (_.hsetob));
{
_.tmpv = (value_tyBM) objhashsettosetpayl_BM (_.hsetob);
DBGPRINTF_BM ("check_and_load_contributors_file hsetob %s with %s", objectdbg_BM (_.hsetob), //
debug_outstr_value_BM (_.tmpv, CURFRAME_BM, 0));
_.tmpv = NULL;
}
for (int cix = 0; cix < csetsiz; cix++)
{
_.contribob = setelemnth_BM (_.contribsetv, cix);
if (!objhashsetcontainspayl_BM (_.hsetob, _.contribob))
FATAL_BM
("contributor object %s (of name %s) is in `contributors` but don't appear in %s",
objectdbg_BM (_.contribob),
bytstring_BM (objcontributornamepayl_BM (_.contribob)), rcpath);
}
if (fclose (fil))
FATAL_BM ("failed to fclose %s", rcpath);
fil = NULL;
INFOPRINTF_BM
("check and load of %d contributors in file %s completed successfully\n",
nbcontrib, rcpath);
} /* end check_and_load_contributors_file_BM */
static void
handle_loaded_contributor_bm (struct stackframe_stBM *stkf,
struct loadedcontribdata_bm *pd)
{
objectval_tyBM *k_contributor_class = BMK_5BAqWtmxAH6_9rCGuxiNbfc;
LOCALFRAME_BM ( /*prev: */ stkf, /*descr: */ NULL,
objectval_tyBM * contribob; //current contributor object
objectval_tyBM * hsetob; //hash set of parsed contributors
value_tyBM namev; /* string value, name of contributor object */
value_tyBM mailv; /* string value, mail of contributor object */
value_tyBM contribsetv; /* set of keys in hsetob */
);
ASSERT_BM (pd && pd->lcda_magic == LOADEDCONTRIBDATAMAGIC_BM);
const char *rcpath = pd->lcda_rcpath;
rawid_tyBM curid = pd->lcda_curid;
const char *curcontrib = pd->lcda_curcontrib;
const char *curoidstr = pd->lcda_curoidstr;
const char *curemail = pd->lcda_curemail;
//const char* curalias = pd->lcda_curalias;
int lincnt = pd->lcda_lincnt;
_.contribob = findobjofid_BM (curid);
_.hsetob = pd->lcda_phsetob ? (*pd->lcda_phsetob) : NULL;
if (!_.contribob)
FATAL_BM
("in %s line#%d contributor %s of oid %s is missing in persistent heap,"
" so remove or comment out that line, then add that contributor",
rcpath, lincnt, curcontrib, curoidstr);
if (objhashsetcontainspayl_BM (_.hsetob, _.contribob))
FATAL_BM
("in %s line#%d contributor %s of oid %s and object %s is duplicated",
rcpath, lincnt, curcontrib, curoidstr, objectdbg_BM (_.contribob));
if (!objhashsetcontainspayl_BM (BMP_contributors, _.contribob))
FATAL_BM
("in %s line#%d contributor %s of oid %s and object %s in not in `contributors` hashset-object",
rcpath, lincnt, curcontrib, curoidstr, objectdbg_BM (_.contribob));
objlock_BM (_.contribob);
if (!objectisinstance_BM (_.contribob, k_contributor_class))
FATAL_BM
("in %s line#%d for contributor %s of oid %s the object %s is not of `contributor_class`",
rcpath, lincnt, curcontrib, curoidstr, objectdbg_BM (_.contribob));
objclearpayload_BM (_.contribob);
{
struct user_stBM *us =
allocgcty_BM (typayl_user_BM, sizeof (struct user_stBM));
us->user_ownobj = _.contribob;
us->user_namev = _.namev = (value_tyBM) makestring_BM (curcontrib);
us->user_emailv = _.mailv = (value_tyBM) makestring_BM (curemail);
objputpayload_BM (_.contribob, us);
DBGPRINTF_BM
("check_and_load_contributors_file lincnt#%d contribob %s with name %s email %s, us@%p",
lincnt, objectdbg_BM (_.contribob), curcontrib, curemail, us);
ASSERT_BM (objpayload_BM (_.contribob) == us);
ASSERT_BM (curcontrib != NULL);
}
objunlock_BM (_.contribob);
objhashsetaddpayl_BM (_.hsetob, _.contribob);
if (!isstring_BM (_.namev))
FATAL_BM
("in %s line#%d contributor %s of oid %s and object %s has no user-name",
rcpath, lincnt, curcontrib, curoidstr, objectdbg_BM (_.contribob));
if (strcmp (bytstring_BM (_.namev), curcontrib))
FATAL_BM
("in %s line#%d contributor %s of oid %s and object %s which has unexpected name %s",
rcpath, lincnt, curcontrib, curoidstr, objectdbg_BM (_.contribob),
bytstring_BM (_.namev));
*(pd->lcda_pnamev) = _.namev;
*(pd->lcda_phsetob) = _.hsetob;
*(pd->lcda_pcontribob) = _.contribob;
} /* end of handle_loaded_contributor_bm */
////////////////
void
check_passwords_file_BM (struct loader_stBM *ld, struct stackframe_stBM *stkf)
{
LOCALFRAME_BM ( /*prev: */ stkf, /*descr: */ NULL,
objectval_tyBM * contribob; //current contributor object
value_tyBM namev; /* string value, name of contributor object */
);
ASSERT_BM (ld->ld_magic == LOADERMAGIC_BM);
if (!ld || ld->ld_magic != LOADERMAGIC_BM)
FATAL_BM
("check_passwords_file_BM invalid loader for path %s",
passwords_filepath_BM);
ASSERT_BM (passwords_filepath_BM && passwords_filepath_BM[0] == '/');
FILE *passfil = fopen (passwords_filepath_BM, "r");
if (!passfil)
FATAL_BM ("check_contributor_password fopen %s failed: %m",
passwords_filepath_BM);
if (flock (fileno (passfil), LOCK_EX))
FATAL_BM ("check_contributor_password failed to flock fd#%d for %s",
fileno (passfil), passwords_filepath_BM);
size_t linsiz = 128;
char *linbuf = calloc (linsiz, 1);
int lincnt = 0;
if (!linbuf)
FATAL_BM ("check_contributor_password failed to calloc line of %zd",
linsiz);
/// read password file loop
for (;;)
{
ssize_t linlen = getline (&linbuf, &linsiz, passfil);
if (linlen < 0)
break;
if (linbuf[linlen] == '\n')
linbuf[linlen] = (char) 0;
lincnt++;
if (linbuf[0] == '#' || linbuf[0] == '\n' || !linbuf[0])
continue;
const char *end = NULL;
if (!g_utf8_validate (linbuf, -1, &end) && end && *end)
FATAL_BM ("in password file %s line#%d is invalid UTF8: %s",
passwords_filepath_BM, lincnt, linbuf);
// linbuf should be like: <contribname>;<oid>;<encrypted-password>
char *semcol1 = strchr (linbuf, ';');
char *semcol2 = semcol1 ? strchr (semcol1 + 1, ';') : NULL;
if (!semcol2)
FATAL_BM
("in password file %s line#%d should be like <contributor-name>;<oid>;<encrypted-password> but is %s",
passwords_filepath_BM, lincnt, linbuf);
*semcol1 = (char) 0;
*semcol2 = (char) 0;
const char *curcontrib = linbuf;
const char *curoidstr = semcol1 + 1;
const char *curcryptpass = semcol2 + 1;
char *errmsg = NULL;
if (!valid_contributor_name_BM (curcontrib, &errmsg))
FATAL_BM
("in password file %s line#%d has invalid contributor name %s : %s",
passwords_filepath_BM, lincnt, curcontrib, errmsg);
const char *endid = NULL;
rawid_tyBM curid = parse_rawid_BM (curoidstr, &endid);
if (!endid || *endid || !curid.id_hi || !curid.id_lo)
FATAL_BM
("in password file %s line#%d has invalid oid %s for contributor %s",
passwords_filepath_BM, lincnt, curoidstr, curcontrib);
_.contribob = findobjofid_BM (curid);
if (!_.contribob)
{
WARNPRINTF_BM
("in password file %s line#%d has unknown oid %s for contributor %s, skipping...\n",
passwords_filepath_BM, lincnt, curoidstr, curcontrib);
continue;
}
objlock_BM (_.contribob);
if (!objhascontributorpayl_BM (_.contribob))
FATAL_BM
("in password file %s line#%d contributor %s of oid %s and object %s is not a contributor-object",
passwords_filepath_BM, lincnt, curcontrib, curoidstr,
objectdbg_BM (_.contribob));
_.namev = (value_tyBM) objcontributornamepayl_BM (_.contribob);
objunlock_BM (_.contribob);
if (!isstring_BM (_.namev)
|| strcmp (bytstring_BM (_.namev), curcontrib))
FATAL_BM
("in password file %s line#%d contributor %s of oid %s and object %s which has unexpected name %s",
passwords_filepath_BM, lincnt, curcontrib, curoidstr,
objectdbg_BM (_.contribob), bytstring_BM (_.namev) ? : "**none**");
if (strncmp (curcryptpass, "$6$", 3))
FATAL_BM
("in password file %s line#%d contributor %s of oid %s and object %s has bad encrypted password %s",
passwords_filepath_BM, lincnt, curcontrib, curoidstr,
objectdbg_BM (_.contribob), curcryptpass);
}
if (linbuf)
free (linbuf), linbuf = NULL;
if (passfil)
{
if (flock (fileno (passfil), LOCK_UN))
FATAL_BM ("failed to un-flock fd#%d for %s", fileno (passfil),
passwords_filepath_BM);
if (fclose (passfil))
FATAL_BM ("failed to fclose %s", passwords_filepath_BM);
passfil = NULL;
}
} /* end check_passwords_file_BM */
////////////////
objectval_tyBM *add_contributor_name_email_alias_BM
(const char *name, const char *email, const char *alias,
char **perrmsg, struct stackframe_stBM *stkf)
{
objectval_tyBM *k_contributor_class = BMK_5BAqWtmxAH6_9rCGuxiNbfc;
LOCALFRAME_BM ( /*prev: */ stkf, /*descr: */ NULL,
objectval_tyBM * newcontribob; //returned contributor object
objectval_tyBM * contribob; //current contributor object
objectval_tyBM * assocob; // temporary assoc-object
// mapping
// contributors-objects to
// *contributors(<email>,<alias>)
// nodes
value_tyBM namev; // the name string
value_tyBM emailv; // the email string value
value_tyBM aliasv; // the alias string value
value_tyBM nodev; // the node
);
bool knowncontrib = false;
if (!alias)
alias = "";
if (perrmsg)
*perrmsg = NULL;
DBGPRINTF_BM ("add_contributor_name_email_alias start name='%s'"
" email='%s' alias='%s' perrmsg@%p",
name, email, alias, (void *) perrmsg);
ASSERT_BM (name && name[0]);
ASSERT_BM (email && email[0]);
if (!valid_contributor_name_BM (name, perrmsg))
LOCALRETURN_BM (NULL);
if (!valid_email_BM (email, CHECKDNS_BM, perrmsg))
LOCALRETURN_BM (NULL);
if (alias && alias[0] && !valid_email_BM (alias, CHECKDNS_BM, perrmsg))
LOCALRETURN_BM (NULL);
// the contributor files should exist and contain only valid
// entries. Otherwise fatal error!
FILE *fil = fopen (contributors_filepath_BM, "r+");
if (!fil)
{
FATAL_BM ("cannot open contributors file %s : %m",
CONTRIBUTORS_FILE_BM);
LOCALRETURN_BM (NULL);
}
const char *rcpath = contributors_filepath_BM;
struct stat mystat = { };
if (!rcpath)
FATAL_BM ("cannot get real path of contributor file %s: %m",
CONTRIBUTORS_FILE_BM);
int fd = fileno (fil);
memset (&mystat, 0, sizeof (mystat));
if (fstat (fd, &mystat))
FATAL_BM ("failed to fstat fd#%d for %s", fd, rcpath);
// the file size gives a guess on the initial size of assocob
_.assocob = makeobj_BM ();
objputassocpayl_BM (_.assocob, prime_above_BM (mystat.st_size / 64 + 10));
if (flock (fd, LOCK_EX))
FATAL_BM ("failed to flock fd#%d for %s", fd, rcpath);
size_t linsiz = 128;
char *linbuf = calloc (linsiz, 1);
int lincnt = 0;
int nbcontrib = 0;
if (!linbuf)
FATAL_BM
("add_contributor_name_email_alias can't alloc line of %zd bytes",
linsiz);
/// reading loop
for (;;)
{
_.contribob = NULL;
_.namev = NULL;