~ubuntu-branches/ubuntu/lucid/seamonkey/lucid-security

« back to all changes in this revision

Viewing changes to security/nss/cmd/tstclnt/tstclnt.c

  • Committer: Bazaar Package Importer
  • Author(s): Fabien Tassin
  • Date: 2008-07-29 21:29:02 UTC
  • mfrom: (1.1.4 upstream)
  • Revision ID: james.westby@ubuntu.com-20080729212902-spm9kpvchp9udwbw
Tags: 1.1.11+nobinonly-0ubuntu1
* New security upstream release: 1.1.11 (LP: #218534)
  Fixes USN-602-1, USN-619-1, USN-623-1 and USN-629-1
* Refresh diverged patch:
  - update debian/patches/80_security_build.patch
* Fix FTBFS with missing -lfontconfig
  - add debian/patches/11_fix_ftbfs_with_fontconfig.patch
  - update debian/patches/series
* Build with default gcc (hardy: 4.2, intrepid: 4.3)
  - update debian/rules
  - update debian/control

Show diffs side-by-side

added added

removed removed

Lines of Context:
65
65
#include "sslproto.h"
66
66
#include "pk11func.h"
67
67
#include "plgetopt.h"
 
68
#include "plstr.h"
68
69
 
69
70
#if defined(WIN32)
70
71
#include <fcntl.h>
210
211
    fprintf(stderr, "%-20s Disable SSL v2.\n", "-2");
211
212
    fprintf(stderr, "%-20s Disable SSL v3.\n", "-3");
212
213
    fprintf(stderr, "%-20s Disable TLS (SSL v3.1).\n", "-T");
 
214
    fprintf(stderr, "%-20s Prints only payload data. Skips HTTP header.\n", "-S");
213
215
    fprintf(stderr, "%-20s Client speaks first. \n", "-f");
214
216
    fprintf(stderr, "%-20s Override bad server cert. Make it OK.\n", "-o");
215
217
    fprintf(stderr, "%-20s Disable SSL socket locking.\n", "-s");
397
399
    }
398
400
}
399
401
 
 
402
/*
 
403
 *  Prints output according to skipProtoHeader flag. If skipProtoHeader
 
404
 *  is not set, prints without any changes, otherwise looking
 
405
 *  for \n\r\n(empty line sequence: HTTP header separator) and
 
406
 *  prints everything after it.
 
407
 */
 
408
static void
 
409
separateReqHeader(const PRFileDesc* outFd, const char* buf, const int nb,
 
410
                  PRBool *wrStarted, int *ptrnMatched) {
 
411
 
 
412
    /* it is sufficient to look for only "\n\r\n". Hopping that
 
413
     * HTTP response format satisfies the standard */
 
414
    char *ptrnStr = "\n\r\n";
 
415
    char *resPtr;
 
416
 
 
417
    if (nb == 0) {
 
418
        return;
 
419
    }
 
420
 
 
421
    if (*ptrnMatched > 0) {
 
422
        /* Get here only if previous separateReqHeader call found
 
423
         * only a fragment of "\n\r\n" in previous buffer. */
 
424
        PORT_Assert(*ptrnMatched < 3);
 
425
 
 
426
        /* the size of fragment of "\n\r\n" what we want to find in this
 
427
         * buffer is equal to *ptrnMatched */
 
428
        if (*ptrnMatched <= nb) {
 
429
            /* move the pointer to the beginning of the fragment */
 
430
            int strSize = *ptrnMatched;
 
431
            char *tmpPtrn = ptrnStr + (3 - strSize);
 
432
            if (PL_strncmp(buf, tmpPtrn, strSize) == 0) {
 
433
                /* print the rest of the buffer(without the fragment) */
 
434
                PR_Write((void*)outFd, buf + strSize, nb - strSize);
 
435
                *wrStarted = PR_TRUE;
 
436
                return;
 
437
            }
 
438
        } else {
 
439
            /* we are here only when nb == 1 && *ptrnMatched == 2 */
 
440
            if (*buf == '\r') {
 
441
                *ptrnMatched = 1;
 
442
            } else {
 
443
                *ptrnMatched = 0;
 
444
            }
 
445
            return;
 
446
        }
 
447
    }
 
448
    resPtr = PL_strnstr(buf, ptrnStr, nb);
 
449
    if (resPtr != NULL) {
 
450
        /* if "\n\r\n" was found in the buffer, calculate offset
 
451
         * and print the rest of the buffer */
 
452
        int newBn = nb - (resPtr - buf + 3); /* 3 is the length of "\n\r\n" */
 
453
 
 
454
        PR_Write((void*)outFd, resPtr + 3, newBn);
 
455
        *wrStarted = PR_TRUE;
 
456
        return;
 
457
    } else {
 
458
        /* try to find a fragment of "\n\r\n" at the end of the buffer.
 
459
         * if found, set *ptrnMatched to the number of chars left to find
 
460
         * in the next buffer.*/
 
461
        int i;
 
462
        for(i = 1 ;i < 3;i++) {
 
463
            char *bufPrt;
 
464
            int strSize = 3 - i;
 
465
            
 
466
            if (strSize > nb) {
 
467
                continue;
 
468
            }
 
469
            bufPrt = (char*)(buf + nb - strSize);
 
470
            
 
471
            if (PL_strncmp(bufPrt, ptrnStr, strSize) == 0) {
 
472
                *ptrnMatched = i;
 
473
                return;
 
474
            }
 
475
        }
 
476
    }
 
477
}
 
478
 
400
479
#define SSOCK_FD 0
401
480
#define STDIN_FD 1
402
481
 
436
515
    int                useExportPolicy = 0;
437
516
    PRSocketOptionData opt;
438
517
    PRNetAddr          addr;
439
 
    PRHostEnt          hp;
440
518
    PRPollDesc         pollset[2];
441
519
    PRBool             useCommandLinePassword = PR_FALSE;
442
520
    PRBool             pingServerFirst = PR_FALSE;
443
521
    PRBool             clientSpeaksFirst = PR_FALSE;
 
522
    PRBool             wrStarted = PR_FALSE;
 
523
    PRBool             skipProtoHeader = PR_FALSE;
 
524
    int                headerSeparatorPtrnId = 0;
444
525
    int                error = 0;
 
526
    PRUint16           portno;
445
527
    PLOptState *optstate;
446
528
    PLOptStatus optstatus;
447
529
    PRStatus prStatus;
459
541
       }
460
542
    }
461
543
 
462
 
    optstate = PL_CreateOptState(argc, argv, "23BTfc:h:p:d:m:n:oqsvw:x");
 
544
    optstate = PL_CreateOptState(argc, argv, "23BTSfc:h:p:d:m:n:oqsvw:x");
463
545
    while ((optstatus = PL_GetNextOpt(optstate)) == PL_OPT_OK) {
464
546
        switch (optstate->option) {
465
547
          case '?':
473
555
 
474
556
          case 'T': disableTLS  = 1;                    break;
475
557
 
 
558
          case 'S': skipProtoHeader = PR_TRUE;          break;
 
559
 
476
560
          case 'c': cipherString = strdup(optstate->value); break;
477
561
 
478
562
          case 'h': host = strdup(optstate->value);     break;
514
598
        Usage(progName);
515
599
 
516
600
    if (!host || !port) Usage(progName);
 
601
    portno = (PRUint16)atoi(port);
517
602
 
518
603
    if (!certDir) {
519
604
        certDir = SECU_DefaultSSLDir(); /* Look in $SSL_DIR */
556
641
 
557
642
    status = PR_StringToNetAddr(host, &addr);
558
643
    if (status == PR_SUCCESS) {
559
 
        int portno = atoi(port);
560
 
        addr.inet.port = PR_htons((PRUint16)portno);
 
644
        addr.inet.port = PR_htons(portno);
561
645
    } else {
562
646
        /* Lookup host */
563
 
        char buf[PR_NETDB_BUF_SIZE];
564
 
        status = PR_GetIPNodeByName(host, PR_AF_INET6, PR_AI_DEFAULT, 
565
 
                                    buf, sizeof buf, &hp);
566
 
        if (status != PR_SUCCESS) {
 
647
        PRAddrInfo *addrInfo;
 
648
        void       *enumPtr   = NULL;
 
649
 
 
650
        addrInfo = PR_GetAddrInfoByName(host, PR_AF_UNSPEC, 
 
651
                                        PR_AI_ADDRCONFIG | PR_AI_NOCANONNAME);
 
652
        if (!addrInfo) {
567
653
            SECU_PrintError(progName, "error looking up host");
568
654
            return 1;
569
655
        }
570
 
        if (PR_EnumerateHostEnt(0, &hp, (PRUint16)atoi(port), &addr) == -1) {
 
656
        do {
 
657
            enumPtr = PR_EnumerateAddrInfo(enumPtr, addrInfo, portno, &addr);
 
658
        } while (enumPtr != NULL &&
 
659
                 addr.raw.family != PR_AF_INET &&
 
660
                 addr.raw.family != PR_AF_INET6);
 
661
        PR_FreeAddrInfo(addrInfo);
 
662
        if (enumPtr == NULL) {
571
663
            SECU_PrintError(progName, "error looking up host address");
572
664
            return 1;
573
665
        }
574
666
    }
575
667
 
576
 
    if (PR_IsNetAddrType(&addr, PR_IpAddrV4Mapped)) {
577
 
        /* convert to IPv4.  */
578
 
        addr.inet.family = PR_AF_INET;
579
 
        memcpy(&addr.inet.ip, &addr.ipv6.ip.pr_s6_addr[12], 4);
580
 
        memset(&addr.inet.pad[0], 0, sizeof addr.inet.pad);
581
 
    }
582
 
 
583
668
    printHostNameAndAddr(host, &addr);
584
669
 
585
670
    if (pingServerFirst) {
586
671
        int iter = 0;
587
672
        PRErrorCode err;
588
673
        do {
589
 
            s = PR_NewTCPSocket();
 
674
            s = PR_OpenTCPSocket(addr.raw.family);
590
675
            if (s == NULL) {
591
676
                SECU_PrintError(progName, "Failed to create a TCP socket");
592
677
            }
624
709
    }
625
710
 
626
711
    /* Create socket */
627
 
    s = PR_NewTCPSocket();
 
712
    s = PR_OpenTCPSocket(addr.raw.family);
628
713
    if (s == NULL) {
629
714
        SECU_PrintError(progName, "error creating socket");
630
715
        return 1;
940
1025
                /* EOF from socket... stop polling socket for read */
941
1026
                pollset[SSOCK_FD].in_flags = 0;
942
1027
            } else {
943
 
                PR_Write(std_out, buf, nb);
 
1028
                if (skipProtoHeader != PR_TRUE || wrStarted == PR_TRUE) {
 
1029
                    PR_Write(std_out, buf, nb);
 
1030
                } else {
 
1031
                    separateReqHeader(std_out, buf, nb, &wrStarted,
 
1032
                                      &headerSeparatorPtrnId);
 
1033
                }
944
1034
                if (verbose)
945
1035
                    fputs("\n\n", stderr);
946
1036
            }