~ubuntu-branches/ubuntu/gutsy/ntp/gutsy

« back to all changes in this revision

Viewing changes to ntpd/ntp_proto.c

  • Committer: Bazaar Package Importer
  • Author(s): Steve Kowalik
  • Date: 2007-05-18 22:41:56 UTC
  • mfrom: (1.2.3 upstream)
  • Revision ID: james.westby@ubuntu.com-20070518224156-563ruqxsxvqvoy8h
Tags: 1:4.2.4p0+dfsg-1ubuntu1
* Merge from Debian unstable.
* Remaining Ubuntu changes:
  - Update version in conflicts/replaces to that which was shipped in edgy,
    which was later than that in Debian (due to the ubuntuX).
  - Change default server to ntp.ubuntu.com.
  - Remove stop links from rc0 and rc6
  - Call dh_installinit with --error-handler
  - Set Ubuntu maintainer address.

Show diffs side-by-side

added added

removed removed

Lines of Context:
259
259
                        }
260
260
                        hpoll = sys_poll;
261
261
                        if (peer->flags & FLAG_BURST &&
262
 
                            peer_unfit(peer))
 
262
                            !peer_unfit(peer))
263
263
                                peer->burst = NTP_BURST;
264
264
                }
265
265
 
349
349
        int     rval;                   /* cookie snatcher */
350
350
#endif /* OPENSSL */
351
351
        int retcode = AM_NOMATCH;
 
352
        int     at_listhead;
352
353
 
353
354
        /*
354
355
         * Monitor the packet and get restrictions. Note that the packet
369
370
                sys_badlength++;
370
371
                return;                         /* bogus port */
371
372
        }
372
 
        ntp_monitor(rbufp);
373
 
        restrict_mask = restrictions(&rbufp->recv_srcadr);
 
373
        at_listhead = ntp_monitor(rbufp);
 
374
        restrict_mask = restrictions(&rbufp->recv_srcadr, at_listhead);
374
375
#ifdef DEBUG
375
376
        if (debug > 1)
376
377
                printf("receive: at %ld %s<-%s flags %x restrict %03x\n",
1134
1135
        /*
1135
1136
         * Capture the header values.
1136
1137
         */
1137
 
        record_raw_stats(&peer->srcadr, &peer->dstadr->sin, &p_org,
 
1138
        record_raw_stats(&peer->srcadr, peer->dstadr ? &peer->dstadr->sin : NULL, &p_org,
1138
1139
            &p_rec, &p_xmt, &peer->rec);
1139
1140
        peer->leap = pleap;
1140
1141
        peer->stratum = min(pstratum, STRATUM_UNSPEC);
1470
1471
#endif
1471
1472
}
1472
1473
 
1473
 
 
1474
1474
/*
1475
 
 * peer_clear - clear peer filter registers.  See Section 3.4.8 of the spec.
 
1475
 * peer_crypto_clear - discard crypto information
1476
1476
 */
1477
1477
void
1478
 
peer_clear(
1479
 
        struct peer *peer,              /* peer structure */
1480
 
        char    *ident                  /* tally lights */
1481
 
        )
 
1478
peer_crypto_clear(
 
1479
                  struct peer *peer
 
1480
                  )
1482
1481
{
1483
 
        int     i;
1484
 
 
1485
1482
        /*
1486
1483
         * If cryptographic credentials have been acquired, toss them to
1487
1484
         * Valhalla. Note that autokeys are ephemeral, in that they are
1491
1488
         * purged, too. This makes it much harder to sneak in some
1492
1489
         * unauthenticated data in the clock filter.
1493
1490
         */
 
1491
        DPRINTF(1, ("peer_crypto_clear: at %ld next %ld assoc ID %d\n",
 
1492
                    current_time, peer->nextdate, peer->associd));
 
1493
 
1494
1494
#ifdef OPENSSL
1495
 
        key_expire(peer);
 
1495
        peer->assoc = 0;
 
1496
        peer->crypto = 0;
 
1497
 
1496
1498
        if (peer->pkey != NULL)
1497
1499
                EVP_PKEY_free(peer->pkey);
1498
 
        if (peer->ident_pkey != NULL)
1499
 
                EVP_PKEY_free(peer->ident_pkey);
 
1500
        peer->pkey = NULL;
 
1501
 
 
1502
        peer->digest = NULL;    /* XXX MEMLEAK? check whether this needs to be freed in any way - never was freed */
 
1503
 
1500
1504
        if (peer->subject != NULL)
1501
1505
                free(peer->subject);
 
1506
        peer->subject = NULL;
 
1507
 
1502
1508
        if (peer->issuer != NULL)
1503
1509
                free(peer->issuer);
 
1510
        peer->issuer = NULL;
 
1511
 
 
1512
        peer->pkeyid = 0;
 
1513
 
 
1514
        peer->pcookie = 0;
 
1515
 
 
1516
        if (peer->ident_pkey != NULL)
 
1517
                EVP_PKEY_free(peer->ident_pkey);
 
1518
        peer->ident_pkey = NULL;
 
1519
        
 
1520
        memset(&peer->fstamp, 0, sizeof(peer->fstamp));
 
1521
 
1504
1522
        if (peer->iffval != NULL)
1505
1523
                BN_free(peer->iffval);
 
1524
        peer->iffval = NULL;
 
1525
 
1506
1526
        if (peer->grpkey != NULL)
1507
1527
                BN_free(peer->grpkey);
 
1528
        peer->grpkey = NULL;
 
1529
 
 
1530
        value_free(&peer->cookval);
 
1531
        value_free(&peer->recval);
 
1532
 
1508
1533
        if (peer->cmmd != NULL) {
1509
1534
                free(peer->cmmd);
1510
1535
                peer->cmmd = NULL;
1511
1536
        }
1512
 
        value_free(&peer->cookval);
1513
 
        value_free(&peer->recval);
 
1537
 
 
1538
        key_expire(peer);
 
1539
 
1514
1540
        value_free(&peer->encrypt);
1515
 
        value_free(&peer->sndval);
1516
1541
#endif /* OPENSSL */
 
1542
}
 
1543
 
 
1544
/*
 
1545
 * peer_clear - clear peer filter registers.  See Section 3.4.8 of the spec.
 
1546
 */
 
1547
void
 
1548
peer_clear(
 
1549
        struct peer *peer,              /* peer structure */
 
1550
        char    *ident                  /* tally lights */
 
1551
        )
 
1552
{
 
1553
        int     i;
 
1554
 
 
1555
        peer_crypto_clear(peer);
 
1556
        
1517
1557
        if (peer == sys_peer)
1518
1558
                sys_peer = NULL;
1519
1559
 
1556
1596
        else
1557
1597
                peer->nextdate += (ntp_random() & ((1 << NTP_MINDPOLL) -
1558
1598
                    1));
1559
 
#ifdef DEBUG
1560
 
        if (debug)
1561
 
                printf("peer_clear: at %ld next %ld assoc ID %d refid %s\n",
1562
 
                    current_time, peer->nextdate, peer->associd, ident);
1563
 
#endif
 
1599
 
 
1600
        DPRINTF(1, ("peer_clear: at %ld next %ld assoc ID %d refid %s\n",
 
1601
                    current_time, peer->nextdate, peer->associd, ident));
1564
1602
}
1565
1603
 
1566
1604
 
1624
1662
        }
1625
1663
 
1626
1664
        /*
1627
 
         * Sort the samples in both lists by distance. Note, we do not
1628
 
         * displace a higher distance sample by a lower distance one
1629
 
         * unless lower by at least the precision.  
 
1665
         * If the clock discipline has stabilized, sort the samples in
 
1666
         * both lists by distance. Note, we do not displace a higher
 
1667
         * distance sample by a lower distance one unless lower by at
 
1668
         * least the precision.  
1630
1669
         */
1631
 
        for (i = 1; i < NTP_SHIFT; i++) {
1632
 
                for (j = 0; j < i; j++) {
1633
 
                        if (dst[j] > dst[i] + LOGTOD(sys_precision)) {
1634
 
                                k = ord[j];
1635
 
                                ord[j] = ord[i];
1636
 
                                ord[i] = k;
1637
 
                                etemp = dst[j];
1638
 
                                dst[j] = dst[i];
1639
 
                                dst[i] = etemp;
 
1670
        if (state == 4) {
 
1671
                for (i = 1; i < NTP_SHIFT; i++) {
 
1672
                        for (j = 0; j < i; j++) {
 
1673
                                if (dst[j] > dst[i] +
 
1674
                                    LOGTOD(sys_precision)) {
 
1675
                                        k = ord[j];
 
1676
                                        ord[j] = ord[i];
 
1677
                                        ord[i] = k;
 
1678
                                        etemp = dst[j];
 
1679
                                        dst[j] = dst[i];
 
1680
                                        dst[i] = etemp;
 
1681
                                }
1640
1682
                        }
1641
1683
                }
1642
1684
        }
1693
1735
 
1694
1736
        /*
1695
1737
         * A new sample is useful only if it is younger than the last
1696
 
         * one used, but only if the sucker has been synchronized.
 
1738
         * one used. Note the order is FIFO if the clock discipline has
 
1739
         * not stabilized.
1697
1740
         */
1698
 
        if (peer->filter_epoch[k] <= peer->epoch && sys_leap !=
1699
 
            LEAP_NOTINSYNC) {
 
1741
        if (peer->filter_epoch[k] <= peer->epoch) {
1700
1742
#ifdef DEBUG
1701
1743
                if (debug)
1702
1744
                        printf("clock_filter: discard %lu\n",
2335
2377
        keyid_t xkeyid = 0;     /* transmit key ID */
2336
2378
        l_fp    xmt_tx;
2337
2379
 
 
2380
        if (!peer->dstadr)      /* don't bother with peers without interface */
 
2381
                return;
 
2382
 
2338
2383
        /*
2339
2384
         * This is deliciously complicated. There are three cases.
2340
2385
         *
2406
2451
                get_systime(&peer->xmt);
2407
2452
                HTONL_FP(&peer->xmt, &xpkt.xmt);
2408
2453
                sendpkt(&peer->srcadr, peer->dstadr, sys_ttl[peer->ttl],
2409
 
                    &xpkt, sendlen);
 
2454
                        &xpkt, sendlen);
2410
2455
                peer->sent++;
2411
2456
#ifdef DEBUG
2412
2457
                if (debug)
2413
2458
                        printf("transmit: at %ld %s->%s mode %d\n",
2414
 
                            current_time, stoa(&peer->dstadr->sin),
2415
 
                            stoa(&peer->srcadr), peer->hmode);
 
2459
                               current_time, peer->dstadr ? stoa(&peer->dstadr->sin) : "-",
 
2460
                               stoa(&peer->srcadr), peer->hmode);
2416
2461
#endif
2417
2462
                return;
2418
2463
        }
2484
2529
                exten = NULL;
2485
2530
                switch (peer->hmode) {
2486
2531
 
2487
 
                /*
2488
 
                 * In broadcast server mode the autokey values are
2489
 
                 * required by the broadcast clients. Push them when a
2490
 
                 * new keylist is generated; otherwise, push the
2491
 
                 * association message so the client can request them at
2492
 
                 * other times.
2493
 
                 */
 
2532
                        /*
 
2533
                         * In broadcast server mode the autokey values are
 
2534
                         * required by the broadcast clients. Push them when a
 
2535
                         * new keylist is generated; otherwise, push the
 
2536
                         * association message so the client can request them at
 
2537
                         * other times.
 
2538
                         */
2494
2539
                case MODE_BROADCAST:
2495
2540
                        if (peer->flags & FLAG_ASSOC)
2496
2541
                                exten = crypto_args(peer, CRYPTO_AUTO |
2497
 
                                    CRYPTO_RESP, NULL);
 
2542
                                                    CRYPTO_RESP, NULL);
2498
2543
                        else
2499
2544
                                exten = crypto_args(peer, CRYPTO_ASSOC |
2500
 
                                    CRYPTO_RESP, NULL);
 
2545
                                                    CRYPTO_RESP, NULL);
2501
2546
                        break;
2502
2547
 
2503
2548
                /*
2520
2565
                         */
2521
2566
                        if (!peer->crypto)
2522
2567
                                exten = crypto_args(peer, CRYPTO_ASSOC,
2523
 
                                    sys_hostname);
 
2568
                                                    sys_hostname);
2524
2569
                        else if (!(peer->crypto & CRYPTO_FLAG_VALID))
2525
2570
                                exten = crypto_args(peer, CRYPTO_CERT,
2526
 
                                    peer->issuer);
 
2571
                                                    peer->issuer);
2527
2572
 
2528
2573
                        /*
2529
2574
                         * Identity. Note we have to sign the
2533
2578
                         */
2534
2579
                        else if (!(peer->crypto & CRYPTO_FLAG_VRFY))
2535
2580
                                exten = crypto_args(peer,
2536
 
                                    crypto_ident(peer), NULL);
 
2581
                                                    crypto_ident(peer), NULL);
2537
2582
                        else if (sys_leap != LEAP_NOTINSYNC &&
2538
 
                           !(peer->crypto & CRYPTO_FLAG_SIGN))
 
2583
                                 !(peer->crypto & CRYPTO_FLAG_SIGN))
2539
2584
                                exten = crypto_args(peer, CRYPTO_SIGN,
2540
 
                                    sys_hostname);
 
2585
                                                    sys_hostname);
2541
2586
 
2542
2587
                        /*
2543
2588
                         * Autokey. We request the cookie only when the
2550
2595
                         * the autokey values without being asked.
2551
2596
                         */
2552
2597
                        else if (sys_leap != LEAP_NOTINSYNC &&
2553
 
                            peer->leap != LEAP_NOTINSYNC &&
2554
 
                            !(peer->crypto & CRYPTO_FLAG_AGREE))
 
2598
                                 peer->leap != LEAP_NOTINSYNC &&
 
2599
                                 !(peer->crypto & CRYPTO_FLAG_AGREE))
2555
2600
                                exten = crypto_args(peer, CRYPTO_COOK,
2556
 
                                    NULL);
 
2601
                                                    NULL);
2557
2602
                        else if (peer->flags & FLAG_ASSOC)
2558
2603
                                exten = crypto_args(peer, CRYPTO_AUTO |
2559
 
                                    CRYPTO_RESP, NULL);
 
2604
                                                    CRYPTO_RESP, NULL);
2560
2605
                        else if (!(peer->crypto & CRYPTO_FLAG_AUTO))
2561
2606
                                exten = crypto_args(peer, CRYPTO_AUTO,
2562
 
                                    NULL);
 
2607
                                                    NULL);
2563
2608
 
2564
2609
                        /*
2565
2610
                         * Postamble. We trade leapseconds only when the
2566
2611
                         * server and client are synchronized.
2567
2612
                         */
2568
2613
                        else if (sys_leap != LEAP_NOTINSYNC &&
2569
 
                            peer->leap != LEAP_NOTINSYNC &&
2570
 
                            peer->crypto & CRYPTO_FLAG_TAI &&
2571
 
                            !(peer->crypto & CRYPTO_FLAG_LEAP))
 
2614
                                 peer->leap != LEAP_NOTINSYNC &&
 
2615
                                 peer->crypto & CRYPTO_FLAG_TAI &&
 
2616
                                 !(peer->crypto & CRYPTO_FLAG_LEAP))
2572
2617
                                exten = crypto_args(peer, CRYPTO_TAI,
2573
 
                                    NULL);
 
2618
                                                    NULL);
2574
2619
                        break;
2575
2620
 
2576
2621
                /*
2601
2646
                         */
2602
2647
                        if (!peer->crypto)
2603
2648
                                exten = crypto_args(peer, CRYPTO_ASSOC,
2604
 
                                    sys_hostname);
 
2649
                                                    sys_hostname);
2605
2650
                        else if (!(peer->crypto & CRYPTO_FLAG_VALID))
2606
2651
                                exten = crypto_args(peer, CRYPTO_CERT,
2607
 
                                    peer->issuer);
 
2652
                                                    peer->issuer);
2608
2653
 
2609
2654
                        /*
2610
2655
                         * Identity
2611
2656
                         */
2612
2657
                        else if (!(peer->crypto & CRYPTO_FLAG_VRFY))
2613
2658
                                exten = crypto_args(peer,
2614
 
                                    crypto_ident(peer), NULL);
 
2659
                                                    crypto_ident(peer), NULL);
2615
2660
 
2616
2661
                        /*
2617
2662
                         * Autokey
2618
2663
                         */
2619
2664
                        else if (!(peer->crypto & CRYPTO_FLAG_AGREE))
2620
2665
                                exten = crypto_args(peer, CRYPTO_COOK,
2621
 
                                    NULL);
 
2666
                                                    NULL);
2622
2667
                        else if (!(peer->crypto & CRYPTO_FLAG_AUTO) &&
2623
 
                            (peer->cast_flags & MDF_BCLNT))
 
2668
                                 (peer->cast_flags & MDF_BCLNT))
2624
2669
                                exten = crypto_args(peer, CRYPTO_AUTO,
2625
 
                                    NULL);
 
2670
                                                    NULL);
2626
2671
 
2627
2672
                        /*
2628
2673
                         * Postamble. We can sign the certificate here,
2629
2674
                         * since there is no chance of deadlock.
2630
2675
                         */
2631
2676
                        else if (sys_leap != LEAP_NOTINSYNC &&
2632
 
                           !(peer->crypto & CRYPTO_FLAG_SIGN))
 
2677
                                 !(peer->crypto & CRYPTO_FLAG_SIGN))
2633
2678
                                exten = crypto_args(peer, CRYPTO_SIGN,
2634
 
                                    sys_hostname);
 
2679
                                                    sys_hostname);
2635
2680
                        else if (sys_leap != LEAP_NOTINSYNC &&
2636
 
                            peer->crypto & CRYPTO_FLAG_TAI &&
2637
 
                            !(peer->crypto & CRYPTO_FLAG_LEAP))
 
2681
                                 peer->crypto & CRYPTO_FLAG_TAI &&
 
2682
                                 !(peer->crypto & CRYPTO_FLAG_LEAP))
2638
2683
                                exten = crypto_args(peer, CRYPTO_TAI,
2639
 
                                    NULL);
 
2684
                                                    NULL);
2640
2685
                        break;
2641
2686
                }
2642
2687
 
2650
2695
                if (peer->cmmd != NULL) {
2651
2696
                        peer->cmmd->associd = htonl(peer->associd);
2652
2697
                        sendlen += crypto_xmit(&xpkt, &peer->srcadr,
2653
 
                            sendlen, peer->cmmd, 0);
 
2698
                                               sendlen, peer->cmmd, 0);
2654
2699
                        free(peer->cmmd);
2655
2700
                        peer->cmmd = NULL;
2656
2701
                }
2659
2704
 
2660
2705
                        if (exten->opcode != 0) {
2661
2706
                                ltemp = crypto_xmit(&xpkt,
2662
 
                                    &peer->srcadr, sendlen, exten, 0);
 
2707
                                                       &peer->srcadr, sendlen, exten, 0);
2663
2708
                                if (ltemp == 0) {
2664
2709
                                        peer->flash |= TEST9; /* crypto error */
2665
2710
                                        free(exten);
2714
2759
                exit (-1);
2715
2760
        }
2716
2761
        sendpkt(&peer->srcadr, peer->dstadr, sys_ttl[peer->ttl], &xpkt,
2717
 
            sendlen);
 
2762
                sendlen);
2718
2763
 
2719
2764
        /*
2720
2765
         * Calculate the encryption delay. Keep the minimum over
2733
2778
#ifdef DEBUG
2734
2779
        if (debug)
2735
2780
                printf(
2736
 
                    "transmit: at %ld %s->%s mode %d keyid %08x len %d mac %d index %d\n",
2737
 
                    current_time, ntoa(&peer->dstadr->sin),
2738
 
                    ntoa(&peer->srcadr), peer->hmode, xkeyid, sendlen -
2739
 
                    authlen, authlen, peer->keynumber);
 
2781
                        "transmit: at %ld %s->%s mode %d keyid %08x len %d mac %d index %d\n",
 
2782
                        current_time, peer->dstadr ? ntoa(&peer->dstadr->sin) : "-",
 
2783
                        ntoa(&peer->srcadr), peer->hmode, xkeyid, sendlen -
 
2784
                        authlen, authlen, peer->keynumber);
2740
2785
#endif
2741
2786
#else
2742
2787
#ifdef DEBUG
2743
2788
        if (debug)
2744
2789
                printf(
2745
 
                    "transmit: at %ld %s->%s mode %d keyid %08x len %d mac %d\n",
2746
 
                    current_time, ntoa(&peer->dstadr->sin),
2747
 
                    ntoa(&peer->srcadr), peer->hmode, xkeyid, sendlen -
2748
 
                    authlen, authlen);
 
2790
                        "transmit: at %ld %s->%s mode %d keyid %08x len %d mac %d\n",
 
2791
                        current_time, peer->dstadr ? ntoa(&peer->dstadr->sin) : "-",
 
2792
                        ntoa(&peer->srcadr), peer->hmode, xkeyid, sendlen -
 
2793
                        authlen, authlen);
2749
2794
#endif
2750
2795
#endif /* OPENSSL */
2751
2796
}
2806
2851
         * the counter is zero, drop the kiss.
2807
2852
         */
2808
2853
        if (mask & RES_LIMITED) {
 
2854
                sys_limitrejected++;
2809
2855
                if (sys_kod == 0 || !(mask & RES_DEMOBILIZE))
2810
2856
                        return;
2811
2857
 
3031
3077
         * the orphan parent.
3032
3078
         */
3033
3079
        if (peer->stratum > 1 && peer->refid != htonl(LOOPBACKADR) &&
3034
 
            (peer->refid == peer->dstadr->addr_refid || peer->refid ==
3035
 
            sys_refid))
 
3080
            ((!peer->dstadr || peer->refid == peer->dstadr->addr_refid) ||
 
3081
            peer->refid == sys_refid))
3036
3082
                rval |= TEST12;         /* synch loop */
3037
3083
 
3038
3084
        /*