summaryrefslogtreecommitdiff
path: root/server/realm.c
blob: 494c2e4af2ff982a1b19ad36c5aa491fa974f1bf (plain)
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
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
#include "zserver.h"
#include <sys/socket.h>

Unacked *rlm_nacklist = NULL;   /* not acked list for realm-realm
                                   packets */
ZRealm **otherrealms = NULL;    /* points to an array of the known
                                   servers */
int nrealms = 0;                /* number of other realms */
int n_realm_slots = 0;          /* size of malloc'd otherrealms */

/*
 * External Routines:
 *
 * ZRealm *realm_which_realm(struct sockaddr_in *who)
 * figures out if this packet came from another realm's server
 *
 * ZRealm *realm_get_realm_by_pid(int pid)
 * figures out which realm a child handler was for
 *
 * void kill_realm_pids()
 * kills all ticket getting childen
 *
 * char *realm_expand_realm(char *realmname)
 * figures out what an abbreviated realm expands to
 *
 * Code_t realm_send_realms()
 * loops through all realms for a brain dump
 *
 * int realm_bound_for_realm(char *realm, char *recip)
 * figures out if recip is in realm, expanding recip's realm
 *
 * int realm_sender_in_realm(char *realm, char *sender)
 * figures out if sender is in realm
 *
 * ZRealm *realm_get_realm_by_name(char *name)
 * finds a realm struct from the realm array by name, tries expansion
 *
 * Code_t realm_dispatch(ZNotice_t *notice, int auth, struct sockaddr_in *who,
 *                       Server *server)
 * dispatches a message from a foreign realm
 *
 * void realm_init()
 * sets up the realm module
 *
 * void realm_deathgram()
 * tells other realms this server is going down
 *
 * Code_t realm_control_dispatch(ZNotice_t *notice, int auth,
 *                               struct sockaddr_in *who, Server *server,
 *				 ZRealm *realm)
 * dispatches a foreign realm control message
 *
 * void realm_handoff(ZNotice_t *notice, int auth, struct sockaddr_in *who,
 *                    ZRealm *realm, int ack_to_sender)
 * hands off a message to another realm
 *
 * void realm_dump_realms(File *fp)
 * do a database dump of foreign realm info
 *
 */
static int realm_next_idx_by_idx(ZRealm *realm, int idx);
static void realm_sendit(ZNotice_t *notice, struct sockaddr_in *who, int auth, ZRealm *realm, int ack_to_sender);
#ifdef HAVE_KRB5
static Code_t realm_sendit_auth(ZNotice_t *notice, struct sockaddr_in *who, int auth, ZRealm *realm, int ack_to_sender);
#endif
static void rlm_ack(ZNotice_t *notice, Unacked *nacked);
static void rlm_nack_cancel(ZNotice_t *notice, struct sockaddr_in *who);
static void rlm_rexmit(void *arg);
static Code_t realm_ulocate_dispatch(ZNotice_t *notice,int auth,struct sockaddr_in *who,Server *server,ZRealm *realm);
static Code_t realm_new_server(struct sockaddr_in *, ZNotice_t *, ZRealm *);
static Code_t realm_set_server(struct sockaddr_in *, ZRealm *);
#ifdef HAVE_KRB5
static Code_t ticket_retrieve(ZRealm *realm);
static int ticket_lookup(char *realm);
#endif

static int
is_usable(ZRealm_server *srvr)
{
    return !srvr->deleted && srvr->got_addr;
}

static int
is_sendable(ZRealm_server *srvr)
{
    return !srvr->deleted && srvr->got_addr && !srvr->dontsend;
}

static void
rlm_wakeup_cb(void *arg)
{
    ZRealm *realm = arg;
    ZNotice_t snotice;
    char *pack;
    char rlm_recipient[REALM_SZ + 1];
    int packlen, retval;

    memset (&snotice, 0, sizeof (snotice));

    snotice.z_opcode = REALM_BOOT;
    snotice.z_port = srv_addr.sin_port;
    snotice.z_class_inst = ZEPHYR_CTL_REALM;
    snotice.z_class = ZEPHYR_CTL_CLASS;
    snotice.z_recipient = "";
    snotice.z_kind = ACKED;
    snotice.z_num_other_fields = 0;
    snotice.z_default_format = "";
    snotice.z_sender = myname; /* my host name */
    sprintf(rlm_recipient, "@%s", realm->name);
    snotice.z_recipient = rlm_recipient;
    snotice.z_default_format = "";
    snotice.z_message = NULL;
    snotice.z_message_len = 0;

#ifdef HAVE_KRB5
    if (!ticket_lookup(realm->name))
	if ((retval = ticket_retrieve(realm)) != ZERR_NONE) {
	    syslog(LOG_WARNING, "rlm_wakeup failed: %s",
		   error_message(retval));
	    return;
	}
#endif

    if ((retval = ZFormatNotice(&snotice, &pack, &packlen, ZAUTH))
	!= ZERR_NONE)
    {
	syslog(LOG_WARNING, "rlm_wakeup format: %s",
	       error_message(retval));
	return;
    }
    if ((retval = ZParseNotice(pack, packlen, &snotice))
	!= ZERR_NONE) {
	syslog(LOG_WARNING, "rlm_wakeup parse: %s",
	       error_message(retval));
	free(pack);
	return;
    }

    realm_handoff(&snotice, 1, NULL, realm, 0);
    free(pack);
}

static void
rlm_set_server_address(ZRealm_server *srvr, struct hostent *hp)
{
    memmove(&srvr->addr.sin_addr, hp->h_addr, sizeof(struct in_addr));
    /* use the server port */
    srvr->addr.sin_port = srv_addr.sin_port;
    srvr->addr.sin_family = AF_INET;
    srvr->got_addr = 1;
    if (is_sendable(srvr) && srvr->realm->state == REALM_NEW) {
	srvr->realm->idx = realm_next_idx_by_idx(srvr->realm, srvr->realm->idx);
	srvr->realm->state = REALM_TARDY;
	/*
	 * Set a timer to send a wakeup to this realm.  We do this rather
	 * than just sending the notice now because, if we are not using
	 * C-ARES, then we might be called during server startup before
	 * the server is prepared to send notices.
	 */
	timer_set_rel(0, rlm_wakeup_cb, srvr->realm);
    }
}

#ifdef HAVE_ARES

static void rlm_server_address_timer_cb(void *srvr);
static void rlm_server_address_lookup_cb(void *, int, int, struct hostent *);

static void
rlm_lookup_server_address(ZRealm_server *srvr)
{
    /* Cancel any pending future lookup. */
    if (srvr->timer) {
	timer_reset(srvr->timer);
	srvr->timer = NULL;
    }
    ares_gethostbyname(achannel, srvr->name->string, AF_INET,
		       rlm_server_address_lookup_cb, srvr);
}

static void
rlm_server_address_timer_cb(void *arg)
{
    ZRealm_server *srvr = arg;

    srvr->timer = NULL;
    ares_gethostbyname(achannel, srvr->name->string, AF_INET,
		       rlm_server_address_lookup_cb, arg);
}

static void
rlm_server_address_lookup_cb(void *arg, int status, int timeouts,
			     struct hostent *hp)
{
    ZRealm_server *srvr = arg;
    int delay = 30;

    if (status == ARES_SUCCESS) {
	rlm_set_server_address(srvr, hp);
	delay = 24 * 3600; /* Check again once per day */
    } else {
	syslog(LOG_WARNING, "%s: hostname lookup failed: %s",
	       srvr->name->string, ares_strerror(status));
    }

    /*
     * Set a timer to trigger another lookup.
     * But, not if the server is deleted, which may have happened
     * while we were waiting for ARES to finish the last lookup.
     * Also, not if there is already a timer, which is possible if
     * there were two outstanding lookups and we are the second to
     * complete.  This can happen if we are asked to refresh the
     * server list while a previous lookup is still in progress,
     * since there is no convenient way to tell whether there is a
     * lookup in progress.
     */
    if (!srvr->timer && !srvr->deleted)
	srvr->timer = timer_set_rel(delay, rlm_server_address_timer_cb, arg);
}

#else

static void
rlm_lookup_server_address(ZRealm_server *srvr)
{
    struct hostent *hp;

    hp = gethostbyname(srvr->name->string);
    if (hp)
	rlm_set_server_address(srvr, hp);
    else
	syslog(LOG_WARNING, "hostname failed, %s", srvr->name->string);
}

#endif

static int
realm_get_idx_by_addr(ZRealm *realm,
		      struct sockaddr_in *who)
{
    ZRealm_server *srvr;
    int b;

    /* loop through the realms */
    for (b = 0; b < realm->count; b++) {
	srvr = realm->srvrs[b];
	if (!is_usable(srvr))
	    continue;
	if (srvr->addr.sin_addr.s_addr == who->sin_addr.s_addr)
	    return(b);
    }

    return 0;
}

static int
realm_next_idx_by_idx(ZRealm *realm, int idx)
{
    ZRealm_server *srvr;
    int b;

    /* loop through the servers */
    for (b = idx; b < realm->count; b++) {
	srvr = realm->srvrs[b];
	if (is_sendable(srvr))
	    return(b);
    }

    /* recycle */
    if (idx != 0)
	for (b = 0; b < idx; b++) {
	    srvr = realm->srvrs[b];
	    if (is_sendable(srvr))
		return(b);
	}

    return 0;
}

const char *
realm_expand_realm(char *realmname)
{
    int a;

    /* First, look for an exact match (case insensitive) */
#if defined(HAVE_KRB4) || defined(HAVE_KRB5)
    if (!strcasecmp(ZGetRealm(), realmname))
	return(ZGetRealm());
#endif

    for (a = 0; a < nrealms; a++)
      if (!strcasecmp(otherrealms[a]->name, realmname))
	return(otherrealms[a]->name);

    /* No exact match. See if there's a partial match */
#if defined(HAVE_KRB4) || defined(HAVE_KRB5)
    if (!strncasecmp(ZGetRealm(), realmname, strlen(realmname)))
	return(ZGetRealm());
#endif

    for (a = 0; a < nrealms; a++)
	if (!strncasecmp(otherrealms[a]->name, realmname, strlen(realmname)))
	    return(otherrealms[a]->name);
    return(realmname);
}

ZRealm *
realm_get_realm_by_pid(int pid)
{
    int a;

    for (a = 0; a < nrealms; a++)
	if (otherrealms[a]->child_pid == pid)
	    return(otherrealms[a]);

    return 0;
}

void
kill_realm_pids(void)
{
    int a;

    for (a = 0; a < nrealms; a++)
	if (otherrealms[a]->child_pid != 0)
	    kill(otherrealms[a]->child_pid, 9);

    return;
}

static ZRealmname *
get_realm_lists(char *file)
{
    ZRealmname *rlm_list, *rlm;
    int ii, nused, ntotal;
    FILE *fp;
    char buf[REALM_SZ + NS_MAXDNAME + 1]; /* one for newline */
    char realm[REALM_SZ], server[NS_MAXDNAME + 1];
    String *realm_name;

    nused = 0;
    if (!(fp = fopen(file, "r")))
	return((ZRealmname *)0);

    /* start with 16, realloc if necessary */
    ntotal = 16;
    rlm_list = (ZRealmname *)malloc(ntotal * sizeof(ZRealmname));
    if (!rlm_list) {
	syslog(LOG_CRIT, "get_realm_lists malloc");
	abort();
    }

    while (fgets(buf, sizeof(buf), fp)) {
	if (sscanf(buf, "%s %s", realm, server) != 2) {
	    syslog(LOG_CRIT, "bad format in %s", file);
	    abort();
	}
	realm_name = make_string(realm, 0);
	for (ii = 0; ii < nused; ii++) {
	    /* look for this realm */
	    if (rlm_list[ii].name == realm_name)
		break;
	}
	if (ii < nused) {
	    free_string(realm_name);
	    rlm = &rlm_list[ii];
	    if (rlm->nused +1 >= rlm->nservers) {
		/* make more space */
		rlm->servers = (struct _ZRealm_server *)
		    realloc((char *)rlm->servers,
			    (unsigned)rlm->nservers * 2 *
			    sizeof(struct _ZRealm_server));
		if (!rlm->servers) {
		    syslog(LOG_CRIT, "get_realm_lists realloc");
		    abort();
		}
		rlm->nservers *= 2;
	    }
	} else {
	    /* new realm */
	    if (nused + 1 >= ntotal) {
		/* make more space */
		rlm_list = (ZRealmname *)realloc((char *)rlm_list,
						(unsigned)ntotal * 2 *
						sizeof(ZRealmname));
		if (!rlm_list) {
		    syslog(LOG_CRIT, "get_realm_lists realloc");
		    abort();
		}
		ntotal *= 2;
	    }
	    rlm = &rlm_list[nused++];
	    rlm->name = realm_name;
	    rlm->nused = 0;
	    rlm->nservers = 16;
	    rlm->servers = (struct _ZRealm_server *)
		malloc(rlm->nservers * sizeof(struct _ZRealm_server));
	    if (!rlm->servers) {
		syslog(LOG_CRIT, "get_realm_lists malloc");
		abort();
	    }
	}
	memset(&rlm->servers[rlm->nused], 0, sizeof(struct _ZRealm_server));
	if (*server == '/') {
	    rlm->servers[rlm->nused].name = make_string(server + 1, 1);
	    rlm->servers[rlm->nused].dontsend = 1;
	} else {
	    rlm->servers[rlm->nused].name = make_string(server, 1);
	}
	rlm->nused++;
    }
    if (nused + 1 >= ntotal) {
	rlm_list = (ZRealmname *)realloc((char *)rlm_list,
					(unsigned)(ntotal + 1) *
					sizeof(ZRealmname));
	if (!rlm_list) {
	    syslog(LOG_CRIT, "get_realm_lists realloc");
	    abort();
	}
    }
    rlm_list[nused].name = 0;

    fclose(fp);
    return(rlm_list);
}

Code_t
realm_send_realms(void)
{
    int cnt, retval;
    for (cnt = 0; cnt < nrealms; cnt++) {
	retval = subscr_send_realm_subs(otherrealms[cnt]);
	if (retval != ZERR_NONE)
	    return(retval);
    }
    return ZERR_NONE;
}

int
realm_bound_for_realm(const char *realm, char *recip)
{
    char *rlm = NULL;
    int remote = strcmp(ZGetRealm(), realm);

    if (recip)
      rlm = strchr(recip, '@');

    if (!rlm && !remote)
	return 1;

    if (rlm && strcmp(realm_expand_realm(rlm + 1), realm) == 0)
	return 1;

    return 0;
}

int
realm_sender_in_realm(const char *realm, char *sender)
{
    char *rlm = NULL;
    int remote = strcmp(ZGetRealm(), realm);

    if (sender)
	rlm = strchr(sender, '@');

    if (!rlm && !remote)
	return 1;

    if (rlm && strcmp((rlm + 1), realm) == 0)
	return 1;

    return 0;
}

ZRealm *
realm_which_realm(struct sockaddr_in *who)
{
    ZRealm_server *srvr;
    int a, b;

    if (who->sin_port != srv_addr.sin_port)
	return 0;

    /* loop through the realms */
    for (a = 0; a < nrealms; a++)
	/* loop through the addresses for the realm */
	for (b = 0; b < otherrealms[a]->count; b++) {
	    srvr = otherrealms[a]->srvrs[b];
	    if (!is_usable(srvr))
		continue;
	    if (srvr->addr.sin_addr.s_addr == who->sin_addr.s_addr)
		return(otherrealms[a]);
	}

    return 0;
}

ZRealm *
realm_get_realm_by_name(char *name)
{
    int a;

    /* First, look for an exact match (case insensitive) */
    for (a = 0; a < nrealms; a++)
	if (!strcasecmp(otherrealms[a]->name, name))
	    return(otherrealms[a]);

    /* Failing that, look for an inexact match */
    for (a = 0; a < nrealms; a++)
	if (!strncasecmp(otherrealms[a]->name, name, strlen(name)))
	    return(otherrealms[a]);

    return 0;
}

ZRealm *
realm_get_realm_by_name_string(String *namestr)
{
    int a;

    for (a = 0; a < nrealms; a++)
	if (otherrealms[a]->namestr == namestr)
	    return otherrealms[a];

    return 0;
}

static void
rlm_nack_cancel(register ZNotice_t *notice,
		struct sockaddr_in *who)
{
    register ZRealm *which = realm_which_realm(who);
    register Unacked *nacked;

    zdbug((LOG_DEBUG, "rlm_nack_cancel: %s:%08X,%08X",
           inet_ntoa(notice->z_uid.zuid_addr),
           notice->z_uid.tv.tv_sec, notice->z_uid.tv.tv_usec));

    if (!which) {
	syslog(LOG_ERR, "non-realm ack?");
	return;
    }

    for (nacked = rlm_nacklist; nacked; nacked = nacked->next) {
	if (nacked->dest.rlm.realm == which) {
	    /* First, note the realm appears to be up */
	    which->state = REALM_UP;
	    if (ZCompareUID(&nacked->uid, &notice->z_uid)) {
		timer_reset(nacked->timer);

		if (nacked->ack_addr.sin_addr.s_addr)
		    rlm_ack(notice, nacked);

		/* free the data */
		free(nacked->packet);
		Unacked_delete(nacked);
		free(nacked);
		return;
	    }
	}
    }
    return;
}

static void
rlm_ack(ZNotice_t *notice,
	Unacked *nacked)
{
    ZNotice_t acknotice;
    ZPacket_t ackpack;
    int packlen;
    Code_t retval;

    /* tell the original sender the result */
    acknotice = *notice;
    acknotice.z_message_len = strlen(acknotice.z_message) + 1;

    packlen = sizeof(ackpack);

    if ((retval = ZFormatSmallRawNotice(&acknotice, ackpack, &packlen))
	!= ZERR_NONE) {
	syslog(LOG_ERR, "rlm_ack format: %s",
	       error_message(retval));
	return;
    }
    zdbug((LOG_DEBUG, "rlm_ack sending to %s/%d",
	   inet_ntoa(nacked->ack_addr.sin_addr),
	   ntohs(nacked->ack_addr.sin_port)));
    if ((retval = ZSetDestAddr(&nacked->ack_addr)) != ZERR_NONE) {
	syslog(LOG_WARNING, "rlm_ack set addr: %s",
	       error_message(retval));
	return;
    }
    if ((retval = ZSendPacket(ackpack, packlen, 0)) != ZERR_NONE) {
	syslog(LOG_WARNING, "rlm_ack xmit: %s", error_message(retval));
	return;
    }
}

Code_t
realm_dispatch(ZNotice_t *notice,
	       int auth,
	       struct sockaddr_in *who,
	       Server *server)
{
    ZRealm *realm;
    Code_t status = ZERR_NONE;
    char rlm_recipient[REALM_SZ + 1];
    int external = 0;
    String *notice_class;

    if (notice->z_kind == SERVACK || notice->z_kind == SERVNAK) {
	rlm_nack_cancel(notice, who);
	return(ZERR_NONE);
    }

    /* check if it's a control message */
    realm = realm_which_realm(who);

    notice_class = make_string(notice->z_class,1);

    if (class_is_admin(notice_class)) {
	syslog(LOG_WARNING, "%s sending admin opcode %s",
	       realm->name, notice->z_opcode);
    } else if (class_is_hm(notice_class)) {
	syslog(LOG_WARNING, "%s sending hm opcode %s",
	       realm->name, notice->z_opcode);
    } else if (class_is_control(notice_class)) {
	status = realm_control_dispatch(notice, auth, who,
					server, realm);
    } else if (class_is_ulogin(notice_class)) {
	/* don't need to forward this */
	if (server == me_server) {
            sprintf(rlm_recipient, "@%s", realm->name);
            notice->z_recipient = rlm_recipient;

            sendit(notice, 1, who, 0);
	}
    } else if (class_is_ulocate(notice_class)) {
	status = realm_ulocate_dispatch(notice, auth, who, server, realm);
    } else {
	/* redo the recipient */
	if (*notice->z_recipient == '\0') {
	    sprintf(rlm_recipient, "@%s", realm->name);
	    notice->z_recipient = rlm_recipient;
	    external = 0;
	} else if (realm_bound_for_realm(ZGetRealm(), notice->z_recipient)
		   && *notice->z_recipient == '@')
	{
	    /* we're responsible for getting this message out */
	    external = 1;
	    notice->z_recipient = "";
	}

	/* otherwise, send to local subscribers */
	sendit(notice, auth, who, external);
    }

    return(status);
}

void
realm_init(void)
{
    Client *client;
    ZRealmname *rlmnames;
    ZRealm *rlm;
    int ii, jj, kk, nrlmnames, nsendable;
    char realm_list_file[128];
    char rlmprinc[MAX_PRINCIPAL_SIZE];

    sprintf(realm_list_file, "%s/zephyr/%s", SYSCONFDIR, REALM_LIST_FILE);
    rlmnames = get_realm_lists(realm_list_file);
    if (!rlmnames) {
	zdbug((LOG_DEBUG, "No other realms"));
	/* should we nuke all existing server records? */
	return;
    }

    for (nrlmnames = 0; rlmnames[nrlmnames].name; nrlmnames++);

    /*
     * This happens only when we first start up.  Otherwise, otherrealms
     * is grown as needed.
     */
    if (!otherrealms) {
	otherrealms = (ZRealm **)malloc(nrlmnames * sizeof(ZRealm *));
	if (!otherrealms) {
	    syslog(LOG_CRIT, "malloc failed in realm_init");
	    abort();
	}
	memset(otherrealms, 0, (nrlmnames * sizeof(ZRealm *)));
	n_realm_slots = nrlmnames;
    }

    /* ii: entry in rlmnames */
    for (ii = 0; ii < nrlmnames; ii++) {
	nsendable = 0;
	rlm = realm_get_realm_by_name_string(rlmnames[ii].name);
	if (rlm) {
	    /* jj: server entry in otherrealms */
	    /* kk: server entry in rlmnames */
	    for (jj = 0; jj < rlm->count; jj++) {
		rlm->srvrs[jj]->deleted = 1;
		for (kk = 0; kk < rlmnames[ii].nused; kk++) {
		    if (rlmnames[ii].servers[kk].name != rlm->srvrs[jj]->name)
			continue;
		    /* update existing server */
		    rlm->srvrs[jj]->dontsend = rlmnames[ii].servers[kk].dontsend;
		    rlm->srvrs[jj]->deleted = 0;
		    rlm_lookup_server_address(rlm->srvrs[jj]);
		    if (is_sendable(rlm->srvrs[jj])) nsendable++;

		    /* mark realm.list server entry used */
		    rlmnames[ii].servers[kk].deleted = 1;
		    break;
		}
		if (rlm->srvrs[jj]->deleted && rlm->srvrs[jj]->timer) {
		    timer_reset(rlm->srvrs[jj]->timer);
		    rlm->srvrs[jj]->timer = NULL;
		}
	    }
	    for (jj = kk = 0; kk < rlmnames[ii].nused; kk++)
		if (!rlmnames[ii].servers[kk].deleted) jj++;

	    rlm->srvrs = realloc(rlm->srvrs,
				 (rlm->count + jj) * sizeof(ZRealm_server *));
	    if (!rlm->srvrs) {
		syslog(LOG_CRIT, "realloc failed in realm_init");
		abort();
	    }
	    for (kk = 0; kk < rlmnames[ii].nused; kk++) {
		if (rlmnames[ii].servers[kk].deleted) continue;
		rlm->srvrs[rlm->count] = malloc(sizeof(ZRealm_server));
		if (!rlm->srvrs[rlm->count]) {
		    syslog(LOG_CRIT, "realloc failed in realm_init");
		    abort();
		}
		*(rlm->srvrs[rlm->count]) = rlmnames[ii].servers[kk];
		rlm->srvrs[rlm->count]->realm = rlm;
		rlm_lookup_server_address(rlm->srvrs[rlm->count]);
		if (is_sendable(rlm->srvrs[rlm->count])) nsendable++;
		rlm->count++;
	    }
	    /* The current server might have been deleted or marked dontsend.
	       Advance to one we can use, if necessary. */
	    if (nsendable) {
		rlm->idx = realm_next_idx_by_idx(rlm, rlm->idx);
	    } else {
		rlm->idx = 0;
		rlm->state = REALM_NEW;
	    }
	    free(rlmnames[ii].servers);
	    continue;
	}

	if (nrealms >= n_realm_slots) {
	    otherrealms = realloc(otherrealms,
				  n_realm_slots * 2 * sizeof(ZRealm *));
	    if (!otherrealms) {
		syslog(LOG_CRIT, "realloc failed in realm_init");
		abort();
	    }
	    memset(otherrealms + n_realm_slots, 0,
		   n_realm_slots * sizeof(ZRealm *));
	    n_realm_slots *= 2;
	}

	rlm = (ZRealm *) malloc(sizeof(ZRealm));
	if (!rlm) {
	    syslog(LOG_CRIT, "malloc failed in realm_init");
	    abort();
	}
	memset(rlm, 0, sizeof(ZRealm));
	otherrealms[nrealms++] = rlm;

	rlm->namestr = rlmnames[ii].name;
	rlm->name = rlm->namestr->string;
	rlm->state = REALM_NEW;

	/* convert names to addresses */
	rlm->count = rlmnames[ii].nused;
	rlm->srvrs = malloc(rlm->count * sizeof(ZRealm_server *));
	if (!rlm->srvrs) {
	    syslog(LOG_CRIT, "malloc failed in realm_init");
	    abort();
	}
	for (jj = 0; jj < rlm->count; jj++) {
	    rlm->srvrs[jj] = &rlmnames[ii].servers[jj];
	    rlm->srvrs[jj]->realm = rlm;
	    rlm_lookup_server_address(rlm->srvrs[jj]);
	    if (is_sendable(rlm->srvrs[jj])) nsendable++;
	}

	client = (Client *) malloc(sizeof(Client));
	if (!client) {
	    syslog(LOG_CRIT, "malloc failed in realm_init");
	    abort();
	}
	memset(&client->addr, 0, sizeof(struct sockaddr_in));
#ifdef HAVE_KRB5
        client->session_keyblock = NULL;
#else
#ifdef HAVE_KRB4
	memset(&client->session_key, 0, sizeof(client->session_key));
#endif
#endif
	snprintf(rlmprinc, MAX_PRINCIPAL_SIZE, "%s.%s@%s", SERVER_SERVICE, SERVER_INSTANCE,
		rlm->name);
	client->principal = make_string(rlmprinc, 0);
	client->last_send = 0;
	client->last_ack = NOW;
	client->subs = NULL;
	client->realm = rlm;
	client->addr.sin_family = 0;
	client->addr.sin_port = 0;
	client->addr.sin_addr.s_addr = 0;

	rlm->client = client;
	rlm->idx = (nsendable) ?
	    realm_next_idx_by_idx(rlm, (random() % rlm->count)) : 0;
	rlm->subs = NULL;
	rlm->remsubs = NULL;
	rlm->child_pid = 0;
	rlm->have_tkt = 1;
    }
    free(rlmnames);
}

void
realm_deathgram(Server *server)
{
    ZRealm *realm;
    int jj = 0;

    /* Get it out once, and assume foreign servers will share */
    for (jj = 0; jj < nrealms; jj++) {
	ZNotice_t snotice;
	char *pack;
	char rlm_recipient[REALM_SZ + 1];
	int packlen, retval;

	realm = otherrealms[jj];
	memset (&snotice, 0, sizeof (snotice));

	snotice.z_kind = ACKED;
	snotice.z_port = srv_addr.sin_port;
	snotice.z_class = ZEPHYR_CTL_CLASS;
	snotice.z_class_inst = ZEPHYR_CTL_REALM;
	snotice.z_opcode = SERVER_SHUTDOWN;
	snotice.z_sender = myname; /* my host name */
	sprintf(rlm_recipient, "@%s", realm->name);
	snotice.z_recipient = rlm_recipient;
	snotice.z_default_format = "";
	snotice.z_num_other_fields = 0;
	snotice.z_default_format = "";
	snotice.z_message = (server) ? server->addr_str : NULL;
	snotice.z_message_len = (server) ? strlen(server->addr_str) + 1 : 0;

	zdbug((LOG_DEBUG, "rlm_deathgram: suggesting %s to %s",
	       (server) ? server->addr_str : "nothing", realm->name));

#ifdef HAVE_KRB5
	if (!ticket_lookup(realm->name))
	    if ((retval = ticket_retrieve(realm)) != ZERR_NONE) {
		syslog(LOG_WARNING, "rlm_deathgram failed: %s",
		       error_message(retval));
		return;
	    }
#endif

	if ((retval = ZFormatNotice(&snotice, &pack, &packlen, ZCAUTH))
	    != ZERR_NONE)
	{
	    syslog(LOG_WARNING, "rlm_deathgram format: %s",
		   error_message(retval));
	    return;
	}
	if ((retval = ZParseNotice(pack, packlen, &snotice)) != ZERR_NONE) {
	    syslog(LOG_WARNING, "rlm_deathgram parse: %s",
		   error_message(retval));
	    free(pack);
	    return;
	}

	realm_handoff(&snotice, 1, NULL, realm, 0);
	free(pack);
    }
}

static Code_t
realm_ulocate_dispatch(ZNotice_t *notice,
		       int auth,
		       struct sockaddr_in *who,
		       Server *server,
		       ZRealm *realm)
{
    register char *opcode = notice->z_opcode;

    if (!auth) {
	syslog(LOG_WARNING, "unauth locate msg from %s (%s/%s/%s)",
	       inet_ntoa(who->sin_addr),
	       notice->z_class, notice->z_class_inst,
	       notice->z_opcode); /* XXX */
	clt_ack(notice, who, AUTH_FAILED);
	return(ZERR_NONE);
    }

    if (!strcmp(opcode, REALM_REQ_LOCATE)) {
	ack(notice, who);
	ulogin_realm_locate(notice, who, realm);
    } else if (!strcmp(opcode, REALM_ANS_LOCATE)) {
	ack(notice, who);
	ulogin_relay_locate(notice, who);
    } else {
	syslog(LOG_WARNING, "%s unknown/illegal loc opcode %s",
	       realm->name, opcode);
	nack(notice, who);
    }

    return(ZERR_NONE);
}


Code_t
realm_control_dispatch(ZNotice_t *notice,
		       int auth,
		       struct sockaddr_in *who,
		       Server *server,
		       ZRealm *realm)
{
    register char *opcode = notice->z_opcode;
    Code_t status;

    if (!auth) {
	syslog(LOG_WARNING, "unauth ctl msg from %s (%s/%s/%s)",
	       inet_ntoa(who->sin_addr),
	       notice->z_class, notice->z_class_inst,
	       notice->z_opcode); /* XXX */
	if (server == me_server)
	    clt_ack(notice, who, AUTH_FAILED);
	return(ZERR_NONE);
    }

    if (strcmp(notice->z_class_inst, ZEPHYR_CTL_REALM)) {
	syslog(LOG_WARNING, "Invalid rlm_dispatch instance %s",
	       notice->z_class_inst);
	return(ZERR_NONE);
    }

    if (!strcmp(opcode, REALM_REQ_SUBSCRIBE) || !strcmp(opcode, REALM_ADD_SUBSCRIBE)) {
	/* try to add subscriptions */
	/* attempts to get defaults are ignored */
	if ((status = subscr_foreign_user(notice, who, server, realm)) != ZERR_NONE) {
	    clt_ack(notice, who, AUTH_FAILED);
	} else if (server == me_server) {
	    server_forward(notice, auth, who);
	    ack(notice, who);
	}
    } else if (!strcmp(opcode, REALM_UNSUBSCRIBE)) {
	/* try to remove subscriptions */
	if ((status = subscr_realm_cancel(who, notice, realm)) != ZERR_NONE) {
	    clt_ack(notice, who, NOT_FOUND);
	} else if (server == me_server) {
	    server_forward(notice, auth, who);
	    ack(notice, who);
	}
    } else if (!strcmp(opcode, REALM_BOOT)) {
	zdbug((LOG_DEBUG, "got a REALM_BOOT from %s",
               inet_ntoa(server->addr.sin_addr)));
	if (realm->state != REALM_UP) realm->state = REALM_STARTING;
	realm_set_server(who, realm);
#ifdef REALM_MGMT
	/* resend subscriptions but only if this was to us */
	if (server == me_server) {
	    if ((status = subscr_realm_subs(realm)) != ZERR_NONE) {
		clt_ack(notice, who, NOT_FOUND);
	    } else {
		/* do forward the hint in case it ever matters */
		server_forward(notice, auth, who);
		ack(notice, who);
	    }
	}
#endif
    } else if (!strcmp(opcode, SERVER_SHUTDOWN)) {
	/* try to remove subscriptions */
	if ((status = realm_new_server(who, notice, realm)) != ZERR_NONE) {
	    clt_ack(notice, who, NOT_FOUND);
	} else if (server == me_server) {
	    server_forward(notice, auth, who);
	    ack(notice, who);
	}
    } else {
	syslog(LOG_WARNING, "%s unknown/illegal ctl opcode %s",
	       realm->name, opcode);
	if (server == me_server)
	    nack(notice, who);
	return(ZERR_NONE);
    }
    return(ZERR_NONE);
}

static Code_t
realm_new_server(struct sockaddr_in *sin,
		 ZNotice_t *notice,
		 ZRealm *realm)
{
    unsigned long addr;
    ZRealm *rlm;
    struct sockaddr_in sinaddr;
    int srvidx;

    if (!realm)
	return ZSRV_NORLM;

    srvidx = realm_get_idx_by_addr(realm, sin);
    zdbug((LOG_DEBUG, "rlm_new_srv: message from %d in %s (%s)",
	   srvidx, realm->name, inet_ntoa(sin->sin_addr)));
    if (realm->idx == srvidx) {
	if (notice->z_message_len) {
	    addr = inet_addr(notice->z_message);
	    sinaddr.sin_addr.s_addr = addr;
	    rlm = realm_which_realm(&sinaddr);
	    /* Not exactly */
	    if (!rlm || (rlm != realm))
		return ZSRV_NORLM;
	    /* Validate the hint */
	    realm->idx =
		realm_next_idx_by_idx(realm, realm_get_idx_by_addr(realm,
								   &sinaddr));
	} else {
	    realm->idx = realm_next_idx_by_idx(realm, (realm->idx + 1) %
					       realm->count);
	}
	zdbug((LOG_DEBUG, "rlm_new_srv: switched servers (%s)", inet_ntoa((realm->srvrs[realm->idx]->addr).sin_addr)));
    } else {
      zdbug((LOG_DEBUG, "rlm_new_srv: not switching servers (%s)", inet_ntoa((realm->srvrs[realm->idx]->addr).sin_addr)));
    }
    return 0;
}

static Code_t
realm_set_server(struct sockaddr_in *sin,
		 ZRealm *realm)
{
    ZRealm *rlm;
    int idx;

    rlm = realm_which_realm(sin);
    /* Not exactly */
    if (!rlm || (rlm != realm))
	return ZSRV_NORLM;
    idx = realm_get_idx_by_addr(realm, sin);

    /* Not exactly */
    if (!is_sendable(realm->srvrs[idx]))
	return ZSRV_NORLM;

    realm->idx = idx;

    zdbug((LOG_DEBUG, "rlm_pick_srv: switched servers (%s)", inet_ntoa((realm->srvrs[realm->idx]->addr).sin_addr)));

    return 0;
}

void
realm_handoff(ZNotice_t *notice,
	      int auth,
	      struct sockaddr_in *who,
	      ZRealm *realm,
	      int ack_to_sender)
{
#ifdef HAVE_KRB5
    Code_t retval;

    if (!auth) {
	zdbug((LOG_DEBUG, "realm_sendit unauthentic to realm %s",
	       realm->name));
	realm_sendit(notice, who, auth, realm, ack_to_sender);
	return;
    }

    if (!ticket_lookup(realm->name))
	if ((retval = ticket_retrieve(realm)) != ZERR_NONE) {
	    syslog(LOG_WARNING, "rlm_handoff failed: %s",
		   error_message(retval));
	    realm_sendit(notice, who, auth, realm, ack_to_sender);
	    return;
	}

    zdbug((LOG_DEBUG, "realm_sendit to realm %s auth %d", realm->name, auth));
    /* valid ticket available now, send the message */
    retval = realm_sendit_auth(notice, who, auth, realm, ack_to_sender);
#else /* HAVE_KRB4 */
    realm_sendit(notice, who, auth, realm, ack_to_sender);
#endif /* HAVE_KRB4 */
}

static void
realm_sendit(ZNotice_t *notice,
	     struct sockaddr_in *who,
	     int auth,
	     ZRealm *realm,
	     int ack_to_sender)
{
    char *pack;
    int packlen;
    Code_t retval;
    Unacked *nacked;

    if (realm->count == 0 || realm->state == REALM_NEW) {
	/* XXX we should have a queue or something */
	syslog(LOG_WARNING, "rlm_sendit no servers for %s", realm->name);
	return;
    }

    notice->z_auth = auth;
    notice->z_authent_len = 0;
    notice->z_ascii_authent = "";
    notice->z_checksum = 0;

    /* format the notice */
    if ((retval = ZFormatRawNotice(notice, &pack, &packlen)) != ZERR_NONE) {
	syslog(LOG_WARNING, "rlm_sendit format: %s",
	       error_message(retval));
	return;
    }

    /* now send */
    if ((retval = ZSetDestAddr(&realm->srvrs[realm->idx]->addr)) != ZERR_NONE) {
	syslog(LOG_WARNING, "rlm_sendit set addr: %s",
	       error_message(retval));
	free(pack);
	return;
    }
    if ((retval = ZSendPacket(pack, packlen, 0)) != ZERR_NONE) {
	syslog(LOG_WARNING, "rlm_sendit xmit: %s", error_message(retval));
	free(pack);
	return;
    }

    /* now we've sent it, mark it as not ack'ed */

    if (!(nacked = (Unacked *)malloc(sizeof(Unacked)))) {
	/* no space: just punt */
	syslog(LOG_ERR, "rlm_sendit nack malloc");
	free(pack);
	return;
    }

    memset(nacked, 0, sizeof(Unacked));
    nacked->packet = pack;
    nacked->dest.rlm.realm = realm;
    nacked->dest.rlm.rlm_srv_idx = realm->idx;
    nacked->packsz = packlen;
    nacked->uid = notice->z_uid;
    if (ack_to_sender)
	nacked->ack_addr = *who;
    else
	nacked->ack_addr.sin_addr.s_addr = 0;

    /* set a timer to retransmit */
    nacked->timer = timer_set_rel(rexmit_times[0], rlm_rexmit, nacked);
    /* chain in */
    Unacked_insert(&rlm_nacklist, nacked);
    return;
}

static void
packet_ctl_nack(Unacked *nackpacket)
{
    ZNotice_t notice;

    /* extract the notice */
    ZParseNotice(nackpacket->packet, nackpacket->packsz, &notice);
    if (nackpacket->ack_addr.sin_addr.s_addr != 0)
	nack(&notice, &nackpacket->ack_addr);
    else
	syslog(LOG_WARNING, "would have acked nobody (%s/%s/%s)",
	       notice.z_class, notice.z_class_inst, notice.z_opcode); /* XXX */
}

static void
rlm_rexmit(void *arg)
{
    Unacked *nackpacket = (Unacked *) arg;
    Code_t retval;
    register ZRealm *realm;

    zdbug((LOG_DEBUG,"rlm_rexmit"));

    realm = nackpacket->dest.rlm.realm;

    zdbug((LOG_DEBUG, "rlm_rexmit: sending to %s:%d (%d)",
	   realm->name, realm->idx, nackpacket->rexmits));

    if (realm->count == 0 || realm->state == REALM_NEW)
	return;

    /* Check to see if we've retransmitted as many times as we can */
    if (nackpacket->rexmits >= (NUM_REXMIT_TIMES * realm->count)) {
	/* give a server ack that the packet is lost/realm dead */
	packet_ctl_nack(nackpacket);
	Unacked_delete(nackpacket);

	zdbug((LOG_DEBUG, "rlm_rexmit: %s appears dead", realm->name));
	realm->state = REALM_DEAD;

	free(nackpacket->packet);
	free(nackpacket);
	return;
    }

    /* if we've reached our limit, move on to the next server */
    if ((realm->state == REALM_TARDY) ||
	(nackpacket->rexmits &&
	 !((nackpacket->rexmits+1) % (NUM_REXMIT_TIMES/3))))
    {
	realm->idx = realm_next_idx_by_idx(realm, (realm->idx + 1) %
					   realm->count);
	zdbug((LOG_DEBUG, "rlm_rexmit: %s switching servers:%d (%s)",
	       realm->name, realm->idx,
	       inet_ntoa((realm->srvrs[realm->idx]->addr).sin_addr)));
    }

    /* throttle back if it looks like the realm is down */
    if ((realm->state != REALM_DEAD) ||
	((nackpacket->rexmits % (realm->count+1)) == 1)) {
	/* do the retransmit */
	retval = ZSetDestAddr(&realm->srvrs[realm->idx]->addr);
	if (retval != ZERR_NONE) {
	    syslog(LOG_WARNING, "rlm_rexmit set addr: %s",
		   error_message(retval));
	} else {
	    retval = ZSendPacket(nackpacket->packet, nackpacket->packsz, 0);
	    if (retval != ZERR_NONE)
		syslog(LOG_WARNING, "rlm_rexmit xmit: %s",
		       error_message(retval));
	}
	/* no per-server nack queues for foreign realms yet, doesn't matter */
	nackpacket->dest.rlm.rlm_srv_idx = realm->idx;
	zdbug((LOG_DEBUG, "rlm_rexmit(%s): send to %s", realm->name,
	       inet_ntoa((realm->srvrs[realm->idx]->addr).sin_addr)));
    } else {
	zdbug((LOG_DEBUG, "rlm_rexmit(%s): not sending to %s", realm->name,
	       inet_ntoa((realm->srvrs[realm->idx]->addr).sin_addr)));
    }

    /* reset the timer */
    nackpacket->rexmits++;
    nackpacket->timer =
	timer_set_rel(rexmit_times[nackpacket->rexmits%NUM_REXMIT_TIMES],
		      rlm_rexmit, nackpacket);
    if (rexmit_times[nackpacket->rexmits%NUM_REXMIT_TIMES] == -1) {
	zdbug((LOG_DEBUG, "rlm_rexmit(%s): would send at -1 to %s",
	       realm->name, inet_ntoa((realm->srvrs[realm->idx]->addr).sin_addr)));
    }

    return;
}

void
realm_dump_realms(FILE *fp)
{
    register int ii, jj;

    for (ii = 0; ii < nrealms; ii++) {
	(void) fprintf(fp, "%d:%s\n", ii, otherrealms[ii]->name);
	for (jj = 0; jj < otherrealms[ii]->count; jj++) {
	    (void) fprintf(fp, "\t%s%s%s%s\n",
			   inet_ntoa(otherrealms[ii]->srvrs[jj]->addr.sin_addr),
			   otherrealms[ii]->srvrs[jj]->dontsend ? " nosend" : "",
			   otherrealms[ii]->srvrs[jj]->got_addr ? " gotaddr" : "",
			   otherrealms[ii]->srvrs[jj]->deleted ? " deleted" : "");
	}
	/* dump the subs */
	subscr_dump_subs(fp, otherrealms[ii]->subs);
    }
}

#ifdef HAVE_KRB5

static Code_t
realm_auth_sendit_nacked(char *buffer, int packlen, ZRealm *realm,
			 ZUnique_Id_t uid, int ack_to_sender,
			 struct sockaddr_in *who)
{
    Unacked *nacked;

    nacked = (Unacked *) malloc(sizeof(Unacked));
    if (nacked == NULL)
	return ENOMEM;

    memset(nacked, 0, sizeof(Unacked));
    nacked->packet = buffer;
    nacked->dest.rlm.realm = realm;
    nacked->dest.rlm.rlm_srv_idx = realm->idx;
    nacked->packsz = packlen;
    nacked->uid = uid;

    /* Do the ack for the last frag, below */
    if (ack_to_sender)
	nacked->ack_addr = *who;
    else
	nacked->ack_addr.sin_addr.s_addr = 0;

    /* set a timer to retransmit */
    nacked->timer = timer_set_rel(rexmit_times[0], rlm_rexmit, nacked);

    /* chain in */
    Unacked_insert(&rlm_nacklist, nacked);

    return ZERR_NONE;
}

static Code_t
realm_sendit_auth(ZNotice_t *notice,
		  struct sockaddr_in *who,
		  int auth,
		  ZRealm *realm,
		  int ack_to_sender)
{
    char *buffer = NULL;
    int hdrlen, offset, fragsize, message_len;
    int origoffset, origlen;
    Code_t retval;
    char multi[64];
    ZNotice_t partnotice, newnotice;

    if (realm->count == 0 || realm->state == REALM_NEW) {
	/* XXX we should have a queue or something */
	syslog(LOG_WARNING, "rlm_sendit_auth no servers for %s", realm->name);
	return ZERR_INTERNAL;
    }

    offset = 0;

    buffer = (char *)malloc(sizeof(ZPacket_t));
    if (!buffer) {
	syslog(LOG_ERR, "realm_sendit_auth malloc");
	return ENOMEM; /* DON'T put on nack list */
    }

    newnotice = *notice;

    hdrlen = 0;
    retval = ZMakeZcodeRealmAuthentication(&newnotice, buffer, sizeof(ZPacket_t),
					   &hdrlen, realm->name);
    if (retval)
	syslog(LOG_WARNING,
	       "rlm_sendit_auth: ZMakeZcodeRealmAuthentication: %s",
	       error_message(retval));

    if (!retval) {
	retval = ZSetDestAddr(&realm->srvrs[realm->idx]->addr);
	if (retval)
	    syslog(LOG_WARNING, "rlm_sendit_auth: ZSetDestAddr: %s",
		   error_message(retval));
    }

    /* This is not terribly pretty, but it does do its job.
     * If a packet we get that needs to get sent off to another realm is
     * too big after we slap on our authent, we refragment it further,
     * a la Z_SendFragmentedNotice. This obviates the need for what
     * used to be done in ZFormatAuthenticRealmNotice, as we do it here.
     * At some point it should be pulled back out into its own function,
     * but only the server uses it.
     */

    if (!retval &&
	((notice->z_message_len+hdrlen > (int)sizeof(ZPacket_t)) ||
	 (notice->z_message_len+hdrlen > Z_MAXPKTLEN))) {

	/* Reallocate buffers inside the refragmenter */
	free(buffer);
	buffer = NULL;

	partnotice = *notice;

	origoffset = 0;
	origlen = notice->z_message_len;

	if (notice->z_multinotice && strcmp(notice->z_multinotice, "")) {
	    if (sscanf(notice->z_multinotice, "%d/%d", &origoffset,
		       &origlen) != 2) {
		syslog(LOG_WARNING,
		       "rlm_sendit_auth frag: multinotice parse failed");
		retval = ZERR_BADFIELD;
	    }
	}

	fragsize = Z_MAXPKTLEN - hdrlen - Z_FRAGFUDGE;

	if (fragsize < 0)
	    retval = ZERR_HEADERLEN;

	while (!retval &&
	       (offset < notice->z_message_len || !notice->z_message_len)) {
	    (void)sprintf(multi, "%d/%d", offset+origoffset, origlen);
	    partnotice.z_multinotice = multi;
	    if (offset > 0) {
		(void)Z_gettimeofday(&partnotice.z_uid.tv,
				      (struct timezone *)0);
		partnotice.z_uid.tv.tv_sec = htonl((u_long)
						   partnotice.z_uid.tv.tv_sec);
		partnotice.z_uid.tv.tv_usec =
		    htonl((u_long) partnotice.z_uid.tv.tv_usec);
		(void)memcpy((char *)&partnotice.z_uid.zuid_addr, &__My_addr,
			      sizeof(__My_addr));
		partnotice.z_sender_sockaddr.ip4.sin_family = AF_INET; /* XXX */
		(void)memcpy((char *)&partnotice.z_sender_sockaddr.ip4.sin_addr,
			      &__My_addr, sizeof(__My_addr));
	    }
	    message_len = min(notice->z_message_len-offset, fragsize);
	    partnotice.z_message = notice->z_message+offset;
	    partnotice.z_message_len = message_len;

	    buffer = (char *)malloc(sizeof(ZPacket_t));
	    if (!buffer) {
		syslog(LOG_ERR, "realm_sendit_auth malloc");
		retval = ENOMEM; /* DON'T put on nack list */
	    }

	    if (!retval) {
		retval = ZMakeZcodeRealmAuthentication(&partnotice, buffer,
						       sizeof(ZPacket_t),
						       &hdrlen,
						       realm->name);
		if (retval != ZERR_NONE)
		    syslog(LOG_WARNING, "rlm_sendit_auth set addr: %s",
			   error_message(retval));
	    }

	    if (!retval) {
		(void) memcpy(buffer + hdrlen, partnotice.z_message,
			      partnotice.z_message_len);

		retval = ZSendPacket(buffer,
				     hdrlen + partnotice.z_message_len, 0);
		if (retval)
		    syslog(LOG_WARNING, "rlm_sendit_auth xmit: %s",
			   error_message(retval));
	    }

	    if (!retval) {
		retval = realm_auth_sendit_nacked(buffer, hdrlen +
                                                partnotice.z_message_len,realm,
						  partnotice.z_uid,
						  ack_to_sender, who);
		if (retval) /* no space: just punt */
		    syslog(LOG_ERR,
			   "rlm_sendit_auth: realm_auth_sendit_nacked: %s",
			   error_message(retval));
	    }

	    if (!retval)
		offset += fragsize;

	    if (!notice->z_message_len)
		break;
	}
    } else if (!retval) {
	/* This is easy, no further fragmentation needed */
	(void)memcpy(buffer + hdrlen, newnotice.z_message,
		      newnotice.z_message_len);

	retval = ZSendPacket(buffer, hdrlen + newnotice.z_message_len, 0);
	if (retval)
	    syslog(LOG_WARNING, "rlm_sendit_auth xmit: %s",
		   error_message(retval));
	else {
	    retval = realm_auth_sendit_nacked(buffer,
                                              hdrlen + newnotice.z_message_len,
                                              realm, newnotice.z_uid,
					      ack_to_sender, who);
	    if (retval) /* no space: just punt */
		syslog(LOG_ERR, "rlm_sendit_auth: realm_auth_sendit_nacked: %s",
		       error_message(retval));
	}
    }

    if (retval && buffer != NULL)
	free(buffer);
    return retval;
}

static int
ticket_lookup(char *realm)
{
    krb5_error_code result;
    krb5_timestamp sec;
    krb5_ccache ccache;
    krb5_creds creds_in, creds;

    result = krb5_cc_default(Z_krb5_ctx, &ccache);
    if (result)
      return 0;

    memset(&creds_in, 0, sizeof(creds_in));
    memset(&creds, 0, sizeof(creds));

    result = krb5_cc_get_principal(Z_krb5_ctx, ccache, &creds_in.client);
    if (result) {
      krb5_cc_close(Z_krb5_ctx, ccache);
      return 0;
    }

    result = krb5_build_principal(Z_krb5_ctx, &creds_in.server,
                                  strlen(realm),
                                  realm,
                                  SERVER_KRB5_SERVICE, SERVER_INSTANCE,
				  NULL);
    if (result) {
      krb5_cc_close(Z_krb5_ctx, ccache);
      return 0;
    }

    result = krb5_cc_retrieve_cred(Z_krb5_ctx, ccache, 0, &creds_in, &creds);
    krb5_cc_close(Z_krb5_ctx, ccache);
    /* good ticket? */

    krb5_timeofday (Z_krb5_ctx, &sec);
    krb5_free_cred_contents(Z_krb5_ctx, &creds_in); /* hope this is OK */
    if ((result == 0) && (sec < creds.times.endtime)) {
      krb5_free_cred_contents(Z_krb5_ctx, &creds);
      return (1);
    }
    if (!result)
      krb5_free_cred_contents(Z_krb5_ctx, &creds);

    return (0);
}

static Code_t
ticket_retrieve(ZRealm *realm)
{
    int pid;
    krb5_ccache ccache;
    krb5_error_code result;
    krb5_creds creds_in, *creds;

    get_tgt();

    if (realm->child_pid)
	/* Right idea. Basically, we haven't gotten it yet */
	return KRB5KRB_AP_ERR_TKT_EXPIRED;

    if (realm->have_tkt) {
	/* Get a pointer to the default ccache. We don't need to free this. */
	result = krb5_cc_default(Z_krb5_ctx, &ccache);

	/* GRRR.  There's no allocator or constructor for krb5_creds */
	/* GRRR.  It would be nice if this API were documented at all */
	memset(&creds_in, 0, sizeof(creds_in));

	if (!result)
	    result = krb5_cc_get_principal(Z_krb5_ctx, ccache, &creds_in.client);
	/* construct the service principal */
	if (!result)
	    result = krb5_build_principal(Z_krb5_ctx, &creds_in.server,
					  strlen(realm->name), realm->name,
					  SERVER_KRB5_SERVICE, SERVER_INSTANCE,
					  NULL);

	/* HOLDING: creds_in.server */

	/* look up or get the credentials we need */
	if (!result)
	    result = krb5_get_credentials(Z_krb5_ctx, 0 /* flags */, ccache,
					  &creds_in, &creds);
	krb5_cc_close(Z_krb5_ctx, ccache);
	krb5_free_cred_contents(Z_krb5_ctx, &creds_in); /* hope this is OK */
	if (!result) {
	    krb5_free_creds(Z_krb5_ctx, creds);
	    return 0;
	}
    } else {
	syslog(LOG_ERR, "tkt_rtrv: don't have ticket, but have no child");
        result = KRB5KRB_AP_ERR_TKT_EXPIRED;
    }

    pid = fork();
    if (pid < 0) {
	syslog(LOG_ERR, "tkt_rtrv: can't fork");
	return errno;
    }
    else if (pid == 0) {
#ifdef _POSIX_VERSION
	struct sigaction action;

	action.sa_flags = 0;
	sigemptyset(&action.sa_mask);
	action.sa_handler = 0;
	sigaction(SIGCHLD, &action, NULL);
	sigaction(SIGINT, &action, NULL);
	sigaction(SIGTERM, &action, NULL);
	sigaction(SIGUSR1, &action, NULL);
	sigaction(SIGUSR2, &action, NULL);
	sigaction(SIGFPE, &action, NULL);
	sigaction(SIGHUP, &action, NULL);
#ifdef SIGEMT
	sigaction(SIGEMT, &action, NULL);
#endif
#else
	signal(SIGCHLD, SIG_DFL);
	signal(SIGINT, SIG_DFL);
	signal(SIGTERM, SIG_DFL);
	signal(SIGUSR1, SIG_DFL);
	signal(SIGUSR2, SIG_DFL);
	signal(SIGFPE, SIG_DFL);
	signal(SIGHUP, SIG_DFL);
#ifdef SIGEMT
	signal(SIGEMT, SIG_DFL);
#endif
#endif

	syslog(LOG_INFO, "tkt_rtrv running for %s", realm->name);
	while (1) {
	    /* Get a pointer to the default ccache.
	       We don't need to free this. */
	    result = krb5_cc_default(Z_krb5_ctx, &ccache);

	    /* GRRR.  There's no allocator or constructor for krb5_creds */
	    /* GRRR.  It would be nice if this API were documented at all */
	    memset(&creds_in, 0, sizeof(creds_in));

	    if (!result)
		result = krb5_cc_get_principal(Z_krb5_ctx, ccache,
					       &creds_in.client);
	    /* construct the service principal */
	    if (!result)
		result = krb5_build_principal(Z_krb5_ctx, &creds_in.server,
					      strlen(realm->name), realm->name,
					      SERVER_KRB5_SERVICE,
					      SERVER_INSTANCE,
					      NULL);

	    /* HOLDING: creds_in.server */

	    /* look up or get the credentials we need */
	    if (!result)
		result = krb5_get_credentials(Z_krb5_ctx, 0 /* flags */, ccache,
					      &creds_in, &creds);
	    krb5_cc_close(Z_krb5_ctx, ccache);
	    krb5_free_cred_contents(Z_krb5_ctx, &creds_in); /* hope this is OK */
	    if (!result) {
		krb5_free_creds(Z_krb5_ctx, creds);
		syslog(LOG_INFO, "tkt_rtrv succeeded for %s", realm->name);
		exit(0);
	    }

	    /* Sleep a little while before retrying */
	    sleep(30);
	}
    } else {
	realm->child_pid = pid;
	realm->have_tkt = 0;

	syslog(LOG_WARNING, "tkt_rtrv: %s: %d", realm->name,
	       result);
	return (result);
    }
}
#endif /* HAVE_KRB5 */