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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
|
/*
Read options from kdmrc
Copyright (C) 2001-2005 Oswald Buddenhagen <ossi@kde.org>
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 2 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, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include <config.h>
#include <stdio.h>
#include <unistd.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <ctype.h>
#include <errno.h>
#include <netdb.h>
#include <netinet/in.h>
#include <grp.h>
#ifdef _POSIX_PRIORITY_SCHEDULING
# include <sched.h>
#endif
#include <X11/X.h>
#ifdef FamilyInternet6
# define IPv6
#endif
#include <greet.h>
#include <config.ci>
/*
* Section/Entry definition structs
*/
typedef struct Ent {
const char *name;
int id;
void *ptr;
const char *def;
} Ent;
typedef struct Sect {
const char *name;
Ent *ents;
int numents;
} Sect;
/*
* Parsed ini file structs
*/
typedef struct Entry {
struct Entry *next;
const char *val;
Ent *ent;
int vallen;
int line;
} Entry;
typedef struct Section {
struct Section *next;
Entry *entries;
Sect *sect;
const char *name, *dname, *dhost, *dnum, *dclass;
int nlen, dlen, dhostl, dnuml, dclassl;
} Section;
/*
* Split up display-name/-class for fast comparison
*/
typedef struct DSpec {
const char *dhost, *dnum, *dclass;
int dhostl, dnuml, dclassl;
} DSpec;
/*
* Config value storage structures
*/
typedef struct Value {
const char *ptr;
int len;
} Value;
typedef struct Val {
Value val;
int id;
} Val;
typedef struct ValArr {
Val *ents;
int nents, esiz, nchars, nptrs;
} ValArr;
static void *Malloc( size_t size );
static void *Realloc( void *ptr, size_t size );
#define PRINT_QUOTES
#define LOG_NAME "kdm_config"
#define LOG_DEBUG_MASK DEBUG_CONFIG
#define LOG_PANIC_EXIT 1
#define STATIC static
#include <printf.c>
static void *
Malloc( size_t size )
{
void *ret;
if (!(ret = malloc( size )))
LogOutOfMem();
return ret;
}
static void *
Realloc( void *ptr, size_t size )
{
void *ret;
if (!(ret = realloc( ptr, size )) && size)
LogOutOfMem();
return ret;
}
static void
MkDSpec( DSpec *spec, const char *dname, const char *dclass )
{
spec->dhost = dname;
for (spec->dhostl = 0; dname[spec->dhostl] != ':'; spec->dhostl++);
spec->dnum = dname + spec->dhostl + 1;
spec->dnuml = strlen( spec->dnum );
spec->dclass = dclass;
spec->dclassl = strlen( dclass );
}
static int rfd, wfd;
static int
Reader( void *buf, int count )
{
int ret, rlen;
for (rlen = 0; rlen < count; ) {
dord:
ret = read( rfd, (void *)((char *)buf + rlen), count - rlen );
if (ret < 0) {
if (errno == EINTR)
goto dord;
if (errno == EAGAIN)
break;
return -1;
}
if (!ret)
break;
rlen += ret;
}
return rlen;
}
static void
GRead( void *buf, int count )
{
if (Reader( buf, count ) != count)
LogPanic( "Can't read from core\n" );
}
static void
GWrite( const void *buf, int count )
{
if (write( wfd, buf, count ) != count)
LogPanic( "Can't write to core\n" );
#ifdef _POSIX_PRIORITY_SCHEDULING
if ((debugLevel & DEBUG_HLPCON))
sched_yield();
#endif
}
static void
GSendInt( int val )
{
GWrite( &val, sizeof(val) );
}
static void
GSendStr( const char *buf )
{
if (buf) {
int len = strlen( buf ) + 1;
GWrite( &len, sizeof(len) );
GWrite( buf, len );
} else
GWrite( &buf, sizeof(int));
}
static void
GSendNStr( const char *buf, int len )
{
int tlen = len + 1;
GWrite( &tlen, sizeof(tlen) );
GWrite( buf, len );
GWrite( "", 1 );
}
#ifdef XDMCP
static void
GSendArr( int len, const char *data )
{
GWrite( &len, sizeof(len) );
GWrite( data, len );
}
#endif
static int
GRecvCmd( int *val )
{
if (Reader( val, sizeof(*val) ) != sizeof(*val))
return 0;
return 1;
}
static int
GRecvInt()
{
int val;
GRead( &val, sizeof(val) );
return val;
}
static char *
GRecvStr()
{
int len;
char *buf;
len = GRecvInt();
if (!len)
return 0;
if (!(buf = malloc( len )))
LogPanic( "No memory for read buffer" );
GRead( buf, len );
return buf;
}
/* #define WANT_CLOSE 1 */
typedef struct File {
char *buf, *eof, *cur;
#if defined(HAVE_MMAP) && defined(WANT_CLOSE)
int ismapped;
#endif
} File;
static int
readFile( File *file, const char *fn, const char *what )
{
int fd;
off_t flen;
if ((fd = open( fn, O_RDONLY )) < 0) {
LogInfo( "Cannot open %s file %s\n", what, fn );
return 0;
}
flen = lseek( fd, 0, SEEK_END );
#ifdef HAVE_MMAP
# ifdef WANT_CLOSE
file->ismapped = 0;
# endif
file->buf = mmap( 0, flen + 1, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0 );
# ifdef WANT_CLOSE
if (file->buf)
file->ismapped = 1;
else
# else
if (!file->buf)
# endif
#endif
{
if (!(file->buf = Malloc( flen + 1 ))) {
close( fd );
return 0;
}
lseek( fd, 0, SEEK_SET );
if (read( fd, file->buf, flen ) != flen) {
free( file->buf );
LogError( "Cannot read %s file %s\n", what, fn );
close( fd );
return 0;
}
}
file->eof = (file->cur = file->buf) + flen;
close( fd );
return 1;
}
#ifdef WANT_CLOSE
static void
freeBuf( File *file )
{
# ifdef HAVE_MMAP
if (file->ismapped)
munmap( file->buf, file->eof - file->buf + 1 );
else
# endif
free( file->buf );
}
#endif
CONF_READ_VARS
#define C_MTYPE_MASK 0x30000000
# define C_PATH 0x10000000 /* C_TYPE_STR is a path spec */
# define C_BOOL 0x10000000 /* C_TYPE_INT is a boolean */
# define C_ENUM 0x20000000 /* C_TYPE_INT is an enum (option) */
# define C_GRP 0x30000000 /* C_TYPE_INT is a group spec */
#define C_INTERNAL 0x40000000 /* don't expose to core */
#define C_CONFIG 0x80000000 /* process only for finding deps */
#ifdef XDMCP
static int
PrequestPort( Value *retval )
{
if (!VxdmcpEnable.ptr) {
retval->ptr = (char *)0;
return 1;
}
return 0;
}
#endif
static Value
emptyStr = { "", 1 },
nullValue = { 0, 0 },
emptyArgv = { (char *)&nullValue, 0 };
static int
PnoPassUsers( Value *retval )
{
if (!VnoPassEnable.ptr) {
*retval = emptyArgv;
return 1;
}
return 0;
}
static int
PautoLoginX( Value *retval )
{
if (!VautoLoginEnable.ptr) {
*retval = emptyStr;
return 1;
}
return 0;
}
CONF_READ_ENTRIES
static const char *kdmrc = KDMCONF "/kdmrc";
static Section *rootsec;
static void
ReadConf()
{
const char *nstr, *dstr, *cstr, *dhost, *dnum, *dclass;
char *s, *e, *st, *en, *ek, *sl, *pt;
Section *cursec;
Entry *curent;
Ent *ce;
int nlen, dlen, clen, dhostl, dnuml, dclassl;
int i, line, sectmoan, restl;
File file;
static int confread;
if (confread)
return;
confread = 1;
Debug( "reading config %s ...\n", kdmrc );
if (!readFile( &file, kdmrc, "master configuration" ))
return;
for (s = file.buf, line = 0, cursec = 0, sectmoan = 1; s < file.eof; s++) {
line++;
while ((s < file.eof) && isspace( *s ) && (*s != '\n'))
s++;
if ((s < file.eof) && ((*s == '\n') || (*s == '#'))) {
sktoeol:
while ((s < file.eof) && (*s != '\n'))
s++;
continue;
}
sl = s;
if (*s == '[') {
sectmoan = 0;
while ((s < file.eof) && (*s != '\n'))
s++;
e = s - 1;
while ((e > sl) && isspace( *e ))
e--;
if (*e != ']') {
cursec = 0;
LogError( "Invalid section header at %s:%d\n", kdmrc, line );
continue;
}
nstr = sl + 1;
nlen = e - nstr;
for (cursec = rootsec; cursec; cursec = cursec->next)
if (nlen == cursec->nlen &&
!memcmp( nstr, cursec->name, nlen ))
{
LogInfo( "Multiple occurrences of section [%.*s] in %s. "
"Consider merging them.\n", nlen, nstr, kdmrc );
goto secfnd;
}
if (nstr[0] == 'X' && nstr[1] == '-') {
cstr = nstr + nlen;
clen = 0;
while (++clen, *--cstr != '-');
if (cstr == nstr + 1)
goto illsec;
dstr = nstr + 2;
dlen = nlen - clen - 2;
dhost = dstr;
dhostl = 0;
for (restl = dlen; restl; restl--) {
if (dhost[dhostl] == ':') {
dnum = dhost + dhostl + 1;
dnuml = 0;
for (restl--; restl; restl--) {
if (dnum[dnuml] == '_') {
dclass = dnum + dnuml + 1;
dclassl = restl;
goto gotall;
}
dnuml++;
}
goto gotnum;
}
dhostl++;
}
dnum = "*";
dnuml = 1;
gotnum:
dclass = "*";
dclassl = 1;
gotall: ;
} else {
if (nstr[0] == '-')
goto illsec;
dstr = 0;
dlen = 0;
dhost = 0;
dhostl = 0;
dnum = 0;
dnuml = 0;
dclass = 0;
dclassl = 0;
cstr = nstr;
clen = nlen;
}
for (i = 0; i < as(allSects); i++)
if ((int)strlen( allSects[i]->name ) == clen &&
!memcmp( allSects[i]->name, cstr, clen ))
goto newsec;
illsec:
cursec = 0;
LogError( "Unrecognized section name [%.*s] at %s:%d\n",
nlen, nstr, kdmrc, line );
continue;
newsec:
if (!(cursec = Malloc( sizeof(*cursec) )))
return;
cursec->name = nstr;
cursec->nlen = nlen;
cursec->dname = dstr;
cursec->dlen = dlen;
cursec->dhost = dhost;
cursec->dhostl = dhostl;
cursec->dnum = dnum;
cursec->dnuml = dnuml;
cursec->dclass = dclass;
cursec->dclassl = dclassl;
cursec->sect = allSects[i];
cursec->entries = 0;
cursec->next = rootsec;
rootsec = cursec;
/*Debug( "now in section [%.*s], dpy '%.*s', core '%.*s'\n",
nlen, nstr, dlen, dstr, clen, cstr );*/
secfnd:
continue;
}
if (!cursec) {
if (sectmoan) {
sectmoan = 0;
LogError( "Entry outside any section at %s:%d", kdmrc, line );
}
goto sktoeol;
}
for (; (s < file.eof) && (*s != '\n'); s++)
if (*s == '=')
goto haveeq;
LogError( "Invalid entry (missing '=') at %s:%d\n", kdmrc, line );
continue;
haveeq:
for (ek = s - 1; ; ek--) {
if (ek < sl) {
LogError( "Invalid entry (empty key) at %s:%d\n", kdmrc, line );
goto sktoeol;
}
if (!isspace( *ek ))
break;
}
s++;
while ((s < file.eof) && isspace( *s ) && (*s != '\n'))
s++;
for (pt = st = en = s; s < file.eof && *s != '\n'; s++) {
if (*s == '\\') {
s++;
if (s >= file.eof || *s == '\n') {
LogError( "Trailing backslash at %s:%d\n", kdmrc, line );
break;
}
switch (*s) {
case 's': *pt++ = ' '; break;
case 't': *pt++ = '\t'; break;
case 'n': *pt++ = '\n'; break;
case 'r': *pt++ = '\r'; break;
case '\\': *pt++ = '\\'; break;
default: *pt++ = '\\'; *pt++ = *s; break;
}
en = pt;
} else {
*pt++ = *s;
if (*s != ' ' && *s != '\t')
en = pt;
}
}
nstr = sl;
nlen = ek - sl + 1;
/*Debug( "read entry '%.*s'='%.*s'\n", nlen, nstr, en - st, st );*/
for (i = 0; i < cursec->sect->numents; i++) {
ce = cursec->sect->ents + i;
if ((int)strlen( ce->name ) == nlen &&
!memcmp( ce->name, nstr, nlen ))
goto keyok;
}
LogError( "Unrecognized key '%.*s' in section [%.*s] at %s:%d\n",
nlen, nstr, cursec->nlen, cursec->name, kdmrc, line );
continue;
keyok:
for (curent = cursec->entries; curent; curent = curent->next)
if (ce == curent->ent) {
LogError( "Multiple occurrences of key '%s' in section [%.*s]"
" of %s\n",
ce->name, cursec->nlen, cursec->name, kdmrc );
goto keyfnd;
}
if (!(curent = Malloc( sizeof(*curent) )))
return;
curent->ent = ce;
curent->line = line;
curent->val = st;
curent->vallen = en - st;
curent->next = cursec->entries;
cursec->entries = curent;
keyfnd:
continue;
}
}
static Entry *
FindGEnt( int id )
{
Section *cursec;
Entry *curent;
for (cursec = rootsec; cursec; cursec = cursec->next)
if (!cursec->dname)
for (curent = cursec->entries; curent; curent = curent->next)
if (curent->ent->id == id) {
Debug( "line %d: %s = %'.*s\n",
curent->line, curent->ent->name,
curent->vallen, curent->val );
return curent;
}
return 0;
}
/* Display name match scoring:
* - class (any/exact) -> 0/1
* - number (any/exact) -> 0/2
* - host (any/nonempty/trail/exact) -> 0/4/8/12
*/
static Entry *
FindDEnt( int id, DSpec *dspec )
{
Section *cursec, *bestsec;
Entry *curent, *bestent;
int score, bestscore;
bestscore = -1, bestent = 0;
for (cursec = rootsec; cursec; cursec = cursec->next)
if (cursec->dname) {
score = 0;
if (cursec->dclassl != 1 || cursec->dclass[0] != '*') {
if (cursec->dclassl == dspec->dclassl &&
!memcmp( cursec->dclass, dspec->dclass, dspec->dclassl ))
score = 1;
else
continue;
}
if (cursec->dnuml != 1 || cursec->dnum[0] != '*') {
if (cursec->dnuml == dspec->dnuml &&
!memcmp( cursec->dnum, dspec->dnum, dspec->dnuml ))
score += 2;
else
continue;
}
if (cursec->dhostl != 1 || cursec->dhost[0] != '*') {
if (cursec->dhostl == 1 && cursec->dhost[0] == '+') {
if (dspec->dhostl)
score += 4;
else
continue;
} else if (cursec->dhost[0] == '.') {
if (cursec->dhostl < dspec->dhostl &&
!memcmp( cursec->dhost,
dspec->dhost + dspec->dhostl - cursec->dhostl,
cursec->dhostl ))
score += 8;
else
continue;
} else {
if (cursec->dhostl == dspec->dhostl &&
!memcmp( cursec->dhost, dspec->dhost, dspec->dhostl ))
score += 12;
else
continue;
}
}
if (score > bestscore) {
for (curent = cursec->entries; curent; curent = curent->next)
if (curent->ent->id == id) {
bestent = curent;
bestsec = cursec;
bestscore = score;
break;
}
}
}
if (bestent)
Debug( "line %d: %.*s:%.*s_%.*s/%s = %'.*s\n", bestent->line,
bestsec->dhostl, bestsec->dhost,
bestsec->dnuml, bestsec->dnum,
bestsec->dclassl, bestsec->dclass,
bestent->ent->name, bestent->vallen, bestent->val );
return bestent;
}
static const char *
CvtValue( Ent *et, Value *retval, int vallen, const char *val, char **eopts )
{
Value *ents;
int i, b, e, tlen, nents, esiz;
char buf[80];
switch (et->id & C_TYPE_MASK) {
case C_TYPE_INT:
for (i = 0; i < vallen && i < (int)sizeof(buf) - 1; i++)
buf[i] = tolower( val[i] );
buf[i] = 0;
if ((et->id & C_MTYPE_MASK) == C_BOOL) {
if (!strcmp( buf, "true" ) ||
!strcmp( buf, "on" ) ||
!strcmp( buf, "yes" ) ||
!strcmp( buf, "1" ))
retval->ptr = (char *)1;
else if (!strcmp( buf, "false" ) ||
!strcmp( buf, "off" ) ||
!strcmp( buf, "no" ) ||
!strcmp( buf, "0" ))
retval->ptr = (char *)0;
else
return "boolean";
return 0;
} else if ((et->id & C_MTYPE_MASK) == C_ENUM) {
for (i = 0; eopts[i]; i++)
if (!memcmp( eopts[i], val, vallen ) && !eopts[i][vallen]) {
retval->ptr = (char *)i;
return 0;
}
return "option";
} else if ((et->id & C_MTYPE_MASK) == C_GRP) {
struct group *ge;
if ((ge = getgrnam( buf ))) {
retval->ptr = (char *)ge->gr_gid;
return 0;
}
}
retval->ptr = 0;
if (sscanf( buf, "%li", &retval->ptr ) != 1)
return "integer";
return 0;
case C_TYPE_STR:
retval->ptr = val;
retval->len = vallen + 1;
if ((et->id & C_MTYPE_MASK) == C_PATH)
if (vallen && val[vallen-1] == '/')
retval->len--;
return 0;
case C_TYPE_ARGV:
if (!(ents = Malloc( sizeof(Value) * (esiz = 10) )))
return 0;
for (nents = 0, tlen = 0, i = 0; ; i++) {
for (; i < vallen && isspace( val[i] ); i++);
for (b = i; i < vallen && val[i] != ','; i++);
if (b == i)
break;
for (e = i; e > b && isspace( val[e - 1] ); e--);
if (esiz < nents + 2) {
Value *entsn = Realloc( ents,
sizeof(Value) * (esiz = esiz * 2 + 1) );
if (!nents)
break;
ents = entsn;
}
ents[nents].ptr = val + b;
ents[nents].len = e - b;
nents++;
tlen += e - b + 1;
}
ents[nents].ptr = 0;
retval->ptr = (char *)ents;
retval->len = tlen;
return 0;
default:
LogError( "Internal error: unknown value type in id %#x\n", et->id );
return 0;
}
}
static void
GetValue( Ent *et, DSpec *dspec, Value *retval, char **eopts )
{
Entry *ent;
const char *errs;
/* Debug( "Getting value %#x\n", et->id );*/
if (dspec)
ent = FindDEnt( et->id, dspec );
else
ent = FindGEnt( et->id );
if (ent) {
if (!(errs = CvtValue( et, retval, ent->vallen, ent->val, eopts )))
return;
LogError( "Invalid %s value '%.*s' at %s:%d\n",
errs, ent->vallen, ent->val, kdmrc, ent->line );
}
Debug( "default: %s = %'s\n", et->name, et->def );
if ((errs = CvtValue( et, retval, strlen( et->def ), et->def, eopts )))
LogError( "Internal error: invalid default %s value '%s' for key %s\n",
errs, et->def, et->name );
}
static int
AddValue( ValArr *va, int id, Value *val )
{
int nu;
/* Debug( "Addig value %#x\n", id );*/
if (va->nents == va->esiz) {
va->ents = Realloc( va->ents, sizeof(Val) * (va->esiz += 50) );
if (!va->ents)
return 0;
}
va->ents[va->nents].id = id;
va->ents[va->nents].val = *val;
va->nents++;
switch (id & C_TYPE_MASK) {
case C_TYPE_INT:
break;
case C_TYPE_STR:
va->nchars += val->len;
break;
case C_TYPE_ARGV:
va->nchars += val->len;
for (nu = 0; ((Value *)val->ptr)[nu++].ptr; );
va->nptrs += nu;
break;
}
return 1;
}
static void
CopyValues( ValArr *va, Sect *sec, DSpec *dspec, int isconfig )
{
Value val;
int i;
Debug( "getting values for section class [%s]\n", sec->name );
for (i = 0; i < sec->numents; i++) {
/*Debug ("value %#x\n", sec->ents[i].id);*/
if ((sec->ents[i].id & (int)C_CONFIG) != isconfig)
;
else if (sec->ents[i].id & C_INTERNAL) {
GetValue( sec->ents + i, dspec, ((Value *)sec->ents[i].ptr), 0 );
} else {
if (((sec->ents[i].id & C_MTYPE_MASK) == C_ENUM) ||
!sec->ents[i].ptr ||
!((int (*)( Value * ))sec->ents[i].ptr)(&val)) {
GetValue( sec->ents + i, dspec, &val,
(char **)sec->ents[i].ptr );
}
if (!AddValue( va, sec->ents[i].id, &val ))
break;
}
}
return;
}
static void
SendValues( ValArr *va )
{
Value *cst;
int i, nu;
GSendInt( va->nents );
GSendInt( va->nptrs );
GSendInt( 0/*va->nints*/ );
GSendInt( va->nchars );
for (i = 0; i < va->nents; i++) {
GSendInt( va->ents[i].id & ~C_PRIVATE );
switch (va->ents[i].id & C_TYPE_MASK) {
case C_TYPE_INT:
GSendInt( (int)va->ents[i].val.ptr );
break;
case C_TYPE_STR:
GSendNStr( va->ents[i].val.ptr, va->ents[i].val.len - 1 );
break;
case C_TYPE_ARGV:
cst = (Value *)va->ents[i].val.ptr;
for (nu = 0; cst[nu].ptr; nu++);
GSendInt( nu );
for (; cst->ptr; cst++)
GSendNStr( cst->ptr, cst->len );
break;
}
}
}
#ifdef XDMCP
static char *
ReadWord( File *file, int *len, int EOFatEOL )
{
char *wordp, *wordBuffer;
int quoted;
char c;
rest:
wordp = wordBuffer = file->cur;
mloop:
quoted = 0;
qloop:
if (file->cur == file->eof) {
doeow:
if (wordp == wordBuffer)
return 0;
retw:
*wordp = '\0';
*len = wordp - wordBuffer;
return wordBuffer;
}
c = *file->cur++;
switch (c) {
case '#':
if (quoted)
break;
do {
if (file->cur == file->eof)
goto doeow;
c = *file->cur++;
} while (c != '\n');
case '\0':
case '\n':
if (EOFatEOL && !quoted) {
file->cur--;
goto doeow;
}
if (wordp != wordBuffer) {
file->cur--;
goto retw;
}
goto rest;
case ' ':
case '\t':
if (wordp != wordBuffer)
goto retw;
goto rest;
case '\\':
if (!quoted) {
quoted = 1;
goto qloop;
}
break;
}
*wordp++ = c;
goto mloop;
}
#define ALIAS_CHARACTER '%'
#define EQUAL_CHARACTER '='
#define NEGATE_CHARACTER '!'
#define CHOOSER_STRING "CHOOSER"
#define BROADCAST_STRING "BROADCAST"
#define NOBROADCAST_STRING "NOBROADCAST"
#define LISTEN_STRING "LISTEN"
#define WILDCARD_STRING "*"
typedef struct hostEntry {
struct hostEntry *next;
int type;
union _hostOrAlias {
char *aliasPattern;
char *hostPattern;
struct _display {
int connectionType;
int hostAddrLen;
char *hostAddress;
} displayAddress;
} entry;
} HostEntry;
typedef struct listenEntry {
struct listenEntry *next;
int iface;
int mcasts;
int nmcasts;
} ListenEntry;
typedef struct aliasEntry {
struct aliasEntry *next;
char *name;
HostEntry **pHosts;
int hosts;
int nhosts;
int hasBad;
} AliasEntry;
typedef struct aclEntry {
struct aclEntry *next;
HostEntry **pEntries;
int entries;
int nentries;
HostEntry **pHosts;
int hosts;
int nhosts;
int flags;
} AclEntry;
static int
HasGlobCharacters( char *s )
{
for (;;)
switch (*s++) {
case '?':
case '*':
return 1;
case '\0':
return 0;
}
}
#define PARSE_ALL 0
#define PARSE_NO_BCAST 1
#define PARSE_NO_PAT 2
#define PARSE_NO_ALIAS 4
static int
ParseHost( int *nHosts, HostEntry ***hostPtr, int *nChars,
char *hostOrAlias, int len, int parse )
{
#if defined(IPv6) && defined(AF_INET6)
struct addrinfo *ai;
#else
struct hostent *hostent;
#endif
void *addr;
int addr_type, addr_len;
if (!(**hostPtr = (HostEntry *)Malloc( sizeof(HostEntry))))
return 0;
if (!(parse & PARSE_NO_BCAST) && !strcmp( hostOrAlias, BROADCAST_STRING ))
{
(**hostPtr)->type = HOST_BROADCAST;
}
else if (!(parse & PARSE_NO_ALIAS) && *hostOrAlias == ALIAS_CHARACTER)
{
(**hostPtr)->type = HOST_ALIAS;
(**hostPtr)->entry.aliasPattern = hostOrAlias + 1;
*nChars += len;
}
else if (!(parse & PARSE_NO_PAT) && HasGlobCharacters( hostOrAlias ))
{
(**hostPtr)->type = HOST_PATTERN;
(**hostPtr)->entry.hostPattern = hostOrAlias;
*nChars += len + 1;
}
else
{
(**hostPtr)->type = HOST_ADDRESS;
#if defined(IPv6) && defined(AF_INET6)
if (getaddrinfo( hostOrAlias, NULL, NULL, &ai ))
#else
if (!(hostent = gethostbyname( hostOrAlias )))
#endif
{
LogWarn( "XDMCP ACL: unresolved host %'s\n", hostOrAlias );
free( (char *)(**hostPtr) );
return 0;
}
#if defined(IPv6) && defined(AF_INET6)
addr_type = ai->ai_addr->sa_family;
if (ai->ai_family == AF_INET) {
addr = &((struct sockaddr_in *)ai->ai_addr)->sin_addr;
addr_len = sizeof(struct in_addr);
} else /*if (ai->ai_addr->sa_family == AF_INET6)*/ {
addr = &((struct sockaddr_in6 *)ai->ai_addr)->sin6_addr;
addr_len = sizeof(struct in6_addr);
}
#else
addr_type = hostent->h_addrtype;
addr = hostent->h_addr;
addr_len = hostent->h_length;
#endif
if (!((**hostPtr)->entry.displayAddress.hostAddress =
Malloc( addr_len )))
{
#if defined(IPv6) && defined(AF_INET6)
freeaddrinfo( ai );
#endif
free( (char *)(**hostPtr) );
return 0;
}
memcpy( (**hostPtr)->entry.displayAddress.hostAddress, addr, addr_len );
*nChars += addr_len;
(**hostPtr)->entry.displayAddress.hostAddrLen = addr_len;
(**hostPtr)->entry.displayAddress.connectionType = addr_type;
#if defined(IPv6) && defined(AF_INET6)
freeaddrinfo( ai );
#endif
}
*hostPtr = &(**hostPtr)->next;
(*nHosts)++;
return 1;
}
/* Returns non-0 if string is matched by pattern. Does case folding. */
static int
patternMatch( const char *string, const char *pattern )
{
int p, s;
if (!string)
string = "";
for (;;) {
s = *string++;
switch (p = *pattern++) {
case '*':
if (!*pattern)
return 1;
for (string--; *string; string++)
if (patternMatch( string, pattern ))
return 1;
return 0;
case '?':
if (s == '\0')
return 0;
break;
case '\0':
return s == '\0';
case '\\':
p = *pattern++;
/* fall through */
default:
if (tolower( p ) != tolower( s ))
return 0;
}
}
}
#define MAX_DEPTH 32
#define CHECK_NOT 1
#define CHECK_NO_PAT 2
static int
checkHostlist( HostEntry **hosts, int nh, AliasEntry *aliases, int na,
int depth, int flags )
{
HostEntry *h;
AliasEntry *a;
int hn, an, am;
for (h = *hosts, hn = 0; hn < nh; hn++, h = h->next)
if (h->type == HOST_ALIAS) {
if (depth == MAX_DEPTH) {
LogError( "XDMCP ACL: alias recursion involving %%%s\n",
h->entry.aliasPattern );
return 1;
}
for (a = aliases, an = 0, am = 0; an < na; an++, a = a->next)
if (patternMatch( a->name, h->entry.aliasPattern )) {
am = 1;
if ((flags & CHECK_NOT) && a->hasBad) {
LogError( "XDMCP ACL: alias %%%s with unresolved hosts "
"in denying rule\n", a->name );
return 1;
}
if (checkHostlist( a->pHosts, a->nhosts, aliases, na,
depth + 1, flags ))
return 1;
}
if (!am) {
if (flags & CHECK_NOT) {
LogError( "XDMCP ACL: unresolved alias pattern %%%s "
"in denying rule\n", h->entry.aliasPattern );
return 1;
} else
LogWarn( "XDMCP ACL: unresolved alias pattern %%%s\n",
h->entry.aliasPattern );
}
} else if (h->type == HOST_PATTERN && (flags & CHECK_NO_PAT))
LogWarn( "XDMCP ACL: wildcarded pattern %'s in host-only context\n",
h->entry.hostPattern );
return 0;
}
static void
ReadAccessFile( const char *fname )
{
HostEntry *hostList, **hostPtr = &hostList;
AliasEntry *aliasList, **aliasPtr = &aliasList;
AclEntry *acList, **acPtr = &acList, *acl;
ListenEntry *listenList, **listenPtr = &listenList;
char *displayOrAlias, *hostOrAlias;
File file;
int nHosts, nAliases, nAcls, nListens, nChars, error, bad;
int i, len;
nHosts = nAliases = nAcls = nListens = nChars = error = 0;
if (!readFile( &file, fname, "XDMCP access control" ))
goto sendacl;
while ((displayOrAlias = ReadWord( &file, &len, FALSE ))) {
if (*displayOrAlias == ALIAS_CHARACTER)
{
if (!(*aliasPtr = (AliasEntry *)Malloc( sizeof(AliasEntry)))) {
error = 1;
break;
}
(*aliasPtr)->name = displayOrAlias + 1;
nChars += len;
(*aliasPtr)->hosts = nHosts;
(*aliasPtr)->pHosts = hostPtr;
(*aliasPtr)->nhosts = 0;
(*aliasPtr)->hasBad = 0;
while ((hostOrAlias = ReadWord( &file, &len, TRUE ))) {
if (ParseHost( &nHosts, &hostPtr, &nChars, hostOrAlias, len,
PARSE_NO_BCAST ))
(*aliasPtr)->nhosts++;
else
(*aliasPtr)->hasBad = 1;
}
aliasPtr = &(*aliasPtr)->next;
nAliases++;
}
else if (!strcmp( displayOrAlias, LISTEN_STRING ))
{
if (!(*listenPtr = (ListenEntry *)Malloc( sizeof(ListenEntry)))) {
error = 1;
break;
}
(*listenPtr)->iface = nHosts;
if (!(hostOrAlias = ReadWord( &file, &len, TRUE )) ||
!strcmp( hostOrAlias, WILDCARD_STRING ) ||
!ParseHost( &nHosts, &hostPtr, &nChars, hostOrAlias, len,
PARSE_NO_BCAST|PARSE_NO_PAT|PARSE_NO_ALIAS ))
{
(*listenPtr)->iface = -1;
}
(*listenPtr)->mcasts = nHosts;
(*listenPtr)->nmcasts = 0;
while ((hostOrAlias = ReadWord( &file, &len, TRUE ))) {
if (ParseHost( &nHosts, &hostPtr, &nChars, hostOrAlias, len,
PARSE_NO_BCAST|PARSE_NO_PAT|PARSE_NO_ALIAS ))
(*listenPtr)->nmcasts++;
}
listenPtr = &(*listenPtr)->next;
nListens++;
}
else
{
if (!(*acPtr = (AclEntry *)Malloc( sizeof(AclEntry)))) {
error = 1;
break;
}
(*acPtr)->flags = 0;
if (*displayOrAlias == NEGATE_CHARACTER) {
(*acPtr)->flags |= a_notAllowed;
displayOrAlias++;
} else if (*displayOrAlias == EQUAL_CHARACTER)
displayOrAlias++;
(*acPtr)->entries = nHosts;
(*acPtr)->pEntries = hostPtr;
(*acPtr)->nentries = 1;
if (!ParseHost( &nHosts, &hostPtr, &nChars, displayOrAlias, len,
PARSE_NO_BCAST ))
{
bad = 1;
if ((*acPtr)->flags & a_notAllowed) {
LogError( "XDMCP ACL: unresolved host in denying rule\n" );
error = 1;
}
} else
bad = 0;
(*acPtr)->hosts = nHosts;
(*acPtr)->pHosts = hostPtr;
(*acPtr)->nhosts = 0;
while ((hostOrAlias = ReadWord( &file, &len, TRUE ))) {
if (!strcmp( hostOrAlias, CHOOSER_STRING ))
(*acPtr)->flags |= a_useChooser;
else if (!strcmp( hostOrAlias, NOBROADCAST_STRING ))
(*acPtr)->flags |= a_notBroadcast;
else {
if (ParseHost( &nHosts, &hostPtr, &nChars,
hostOrAlias, len, PARSE_NO_PAT ))
(*acPtr)->nhosts++;
}
}
if (!bad) {
acPtr = &(*acPtr)->next;
nAcls++;
}
}
}
if (!nListens) {
if (!(*listenPtr = (ListenEntry *)Malloc( sizeof(ListenEntry))))
error = 1;
else {
(*listenPtr)->iface = -1;
(*listenPtr)->mcasts = nHosts;
(*listenPtr)->nmcasts = 0;
#if defined(IPv6) && defined(AF_INET6) && defined(XDM_DEFAULT_MCAST_ADDR6)
if (ParseHost( &nHosts, &hostPtr, &nChars,
XDM_DEFAULT_MCAST_ADDR6,
sizeof(XDM_DEFAULT_MCAST_ADDR6)-1,
PARSE_ALL ))
(*listenPtr)->nmcasts++;
#endif
nListens++;
}
}
for (acl = acList, i = 0; i < nAcls; i++, acl = acl->next)
if (checkHostlist( acl->pEntries, acl->nentries, aliasList, nAliases,
0, (acl->flags & a_notAllowed) ? CHECK_NOT : 0 ) ||
checkHostlist( acl->pHosts, acl->nhosts, aliasList, nAliases,
0, CHECK_NO_PAT ))
error = 1;
if (error) {
nHosts = nAliases = nAcls = nListens = nChars = 0;
sendacl:
LogError( "No XDMCP requests will be granted\n" );
}
GSendInt( nHosts );
GSendInt( nListens );
GSendInt( nAliases );
GSendInt( nAcls );
GSendInt( nChars );
for (i = 0; i < nHosts; i++, hostList = hostList->next) {
GSendInt( hostList->type );
switch (hostList->type) {
case HOST_ALIAS:
GSendStr( hostList->entry.aliasPattern );
break;
case HOST_PATTERN:
GSendStr( hostList->entry.hostPattern );
break;
case HOST_ADDRESS:
GSendArr( hostList->entry.displayAddress.hostAddrLen,
hostList->entry.displayAddress.hostAddress );
GSendInt( hostList->entry.displayAddress.connectionType );
break;
}
}
for (i = 0; i < nListens; i++, listenList = listenList->next) {
GSendInt( listenList->iface );
GSendInt( listenList->mcasts );
GSendInt( listenList->nmcasts );
}
for (i = 0; i < nAliases; i++, aliasList = aliasList->next) {
GSendStr( aliasList->name );
GSendInt( aliasList->hosts );
GSendInt( aliasList->nhosts );
}
for (i = 0; i < nAcls; i++, acList = acList->next) {
GSendInt( acList->entries );
GSendInt( acList->nentries );
GSendInt( acList->hosts );
GSendInt( acList->nhosts );
GSendInt( acList->flags );
}
}
#endif
int main( int argc ATTR_UNUSED, char **argv )
{
DSpec dspec;
ValArr va;
char *ci, *disp, *dcls, *cfgfile;
int what;
if (!(ci = getenv( "CONINFO" ))) {
fprintf( stderr, "This program is part of kdm and should not be run manually.\n" );
return 1;
}
if (sscanf( ci, "%d %d", &rfd, &wfd ) != 2)
return 1;
InitLog();
if ((debugLevel = GRecvInt()) & DEBUG_WCONFIG)
sleep( 100 );
/* Debug ("parsing command line\n");*/
if (**++argv)
kdmrc = *argv;
/*
while (*++argv) {
}
*/
for (;;) {
/* Debug ("Awaiting command ...\n");*/
if (!GRecvCmd( &what ))
break;
switch (what) {
case GC_Files:
/* Debug ("GC_Files\n");*/
ReadConf();
CopyValues( 0, &secGeneral, 0, C_CONFIG );
#ifdef XDMCP
CopyValues( 0, &secXdmcp, 0, C_CONFIG );
GSendInt( 2 );
#else
GSendInt( 1 );
#endif
GSendStr( kdmrc );
GSendInt( -1 );
#ifdef XDMCP
GSendNStr( VXaccess.ptr, VXaccess.len - 1 );
GSendInt( 0 );
#endif
for (; (what = GRecvInt()) != -1; )
switch (what) {
case GC_gGlobal:
case GC_gDisplay:
GSendInt( 0 );
break;
#ifdef XDMCP
case GC_gXaccess:
GSendInt( 1 );
break;
#endif
default:
GSendInt( -1 );
break;
}
break;
case GC_GetConf:
/* Debug( "GC_GetConf\n" );*/
memset( &va, 0, sizeof(va) );
what = GRecvInt();
cfgfile = GRecvStr();
switch (what) {
case GC_gGlobal:
/* Debug( "GC_gGlobal\n" );*/
Debug( "getting global config\n" );
ReadConf();
CopyValues( &va, &secGeneral, 0, 0 );
#ifdef XDMCP
CopyValues( &va, &secXdmcp, 0, 0 );
#endif
CopyValues( &va, &secShutdown, 0, 0 );
SendValues( &va );
break;
case GC_gDisplay:
/* Debug( "GC_gDisplay\n" );*/
disp = GRecvStr();
/* Debug( " Display %s\n", disp );*/
dcls = GRecvStr();
/* Debug( " Class %s\n", dcls );*/
Debug( "getting config for display %s, class %s\n", disp, dcls );
MkDSpec( &dspec, disp, dcls ? dcls : "" );
ReadConf();
CopyValues( &va, &sec_Core, &dspec, 0 );
CopyValues( &va, &sec_Greeter, &dspec, 0 );
free( disp );
if (dcls)
free( dcls );
SendValues( &va );
break;
#ifdef XDMCP
case GC_gXaccess:
ReadAccessFile( cfgfile );
break;
#endif
default:
Debug( "Unsupported config category %#x\n", what );
}
free( cfgfile );
break;
default:
Debug( "Unknown config command %#x\n", what );
}
}
/* Debug( "Config reader exiting ..." );*/
return EX_NORMAL;
}
|