Documentation Source Text

Check-in [7b4eed2b49]
Login

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Fixes to the --port option of althttpd.
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: 7b4eed2b49e0ebebfac52685ac83c4cdb4e8b07f
User & Date: drh 2012-06-09 02:24:40
Context
2012-06-10
22:19
Updates to documentation for the 3.7.13 release. check-in: 8ab5a61958 user: drh tags: trunk
2012-06-09
02:24
Fixes to the --port option of althttpd. check-in: 7b4eed2b49 user: drh tags: trunk
01:22
Enhance althttpd with new options --nojail, --port N, and --debug. If the --port N flag is used, then run as a server. --nojail avoids setting the chroot jail. --debug disables the timeout alarms. check-in: f23fa402b1 user: drh tags: trunk
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to misc/althttpd.c.

1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
....
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
        zLine[i] = '_';
      }else if( isupper(c) ){
        zLine[i] = tolower(c);
      }
    }
    strcpy(&zLine[i], ".website");
  }
  if( stat(zLine,&statbuf) && !S_ISDIR(statbuf.st_mode) ){
    sprintf(zLine, "%s/default.website", zRoot);
    if( stat(zLine,&statbuf) && !S_ISDIR(statbuf.st_mode) ){
      NotFound(__LINE__);
    }
  }
  
  zHome = StrDup(zLine);
................................................................................

  /* Record the time when processing begins.
  */
  time(&beginTime);

  /* Parse command-line arguments
  */
  while( argc>2 && argv[1][0]=='-' ){
    char *z = argv[1];

    if( z[0]=='-' && z[1]=='-' ) z++;
    if( strcmp(z,"-user")==0 ){
      zPermUser = argv[2];
      argv += 2;
      argc -= 2;
    }else if( strcmp(z,"-root")==0 ){
      zRoot = argv[2];
      argv += 2;
      argc -= 2;
    }else if( strcmp(z,"-logfile")==0 ){
      zLogFile = argv[2];
      argv += 2;
      argc -= 2;
    }else if( strcmp(z,"-https")==0 ){
      useHttps = atoi(argv[2]);
      zHttp = useHttps ? "http" : "https";
      argv += 2;
      argc -= 2;
    }else if( strcmp(z, "-port")==0 ){
      iPort = atoi(argv[2]);
      argv += 2;
      argc -= 2;
    }else if( strcmp(z, "-nojail")==0 ){

      useChrootJail = 0;
      argv++;
      argc--;

    }else if( strcmp(z, "-debug")==0 ){

      useTimeout = 0;
      argv++;
      argc--;

    }else{
      Malfunction(__LINE__, "unknown argument: [%s]", argv[1]);
    }


  }
  if( zRoot==0 ){
    Malfunction(__LINE__, "no --root specified");
  }
  
  /* Change directories to the root of the HTTP filesystem

  */
  if( chdir(zRoot)!=0 ){
    Malfunction(__LINE__, "cannot change to directory [%s]", zRoot);
  }

  /* Attempt to go into a chroot jail as user zPermUser













  */
  if( zPermUser ){
    struct passwd *pwd = getpwnam(zPermUser);
    if( pwd ){
      if( useChrootJail && chroot(".")<0 ){
        Malfunction(__LINE__, "unable to create chroot jail");
        zRoot = "";
      }
      setgid(pwd->pw_gid);
      setuid(pwd->pw_uid);
    }else{
      Malfunction(__LINE__, "no home directory for user [%s]", zPermUser);
    }
  }
  if( getuid()==0 ){
    Malfunction(__LINE__, "cannot run as root");
  }
  if( iPort && http_server(iPort, 0) ){
    Malfunction(__LINE__, "failed to start server");
  }

  /* Get the IP address from when the request originates
  */
  {
    struct sockaddr_in remoteName;
    unsigned int size = sizeof(struct sockaddr_in);
    if( getpeername(0, (struct sockaddr*)&remoteName, &size)>=0 ){







|







 







|

>


|
<
<

|
<
<

|
<
<

|

<
<

|
<
<
|
>
|
<
<
>

>
|
<
<
>

|

>
>





|
>




|
|
>
>
>
>
>
>
>
>
>
>
>
>
>




<
<
<
<



|





<
<
<







1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
....
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
        zLine[i] = '_';
      }else if( isupper(c) ){
        zLine[i] = tolower(c);
      }
    }
    strcpy(&zLine[i], ".website");
  }
  if( stat(zLine,&statbuf) || !S_ISDIR(statbuf.st_mode) ){
    sprintf(zLine, "%s/default.website", zRoot);
    if( stat(zLine,&statbuf) && !S_ISDIR(statbuf.st_mode) ){
      NotFound(__LINE__);
    }
  }
  
  zHome = StrDup(zLine);
................................................................................

  /* Record the time when processing begins.
  */
  time(&beginTime);

  /* Parse command-line arguments
  */
  while( argc>1 && argv[1][0]=='-' ){
    char *z = argv[1];
    char *zArg = argc>=3 ? argv[2] : "0";
    if( z[0]=='-' && z[1]=='-' ) z++;
    if( strcmp(z,"-user")==0 ){
      zPermUser = zArg;


    }else if( strcmp(z,"-root")==0 ){
      zRoot = zArg;


    }else if( strcmp(z,"-logfile")==0 ){
      zLogFile = zArg;


    }else if( strcmp(z,"-https")==0 ){
      useHttps = atoi(zArg);
      zHttp = useHttps ? "http" : "https";


    }else if( strcmp(z, "-port")==0 ){
      iPort = atoi(zArg);


    }else if( strcmp(z, "-jail")==0 ){
      if( atoi(zArg)==0 ){
        useChrootJail = 0;


      }
    }else if( strcmp(z, "-debug")==0 ){
      if( atoi(zArg) ){
        useTimeout = 0;


      }
    }else{
      Malfunction(__LINE__, "unknown argument: [%s]", z);
    }
    argv += 2;
    argc -= 2;
  }
  if( zRoot==0 ){
    Malfunction(__LINE__, "no --root specified");
  }
  
  /* Change directories to the root of the HTTP filesystem.  Then
  ** create a chroot jail there.
  */
  if( chdir(zRoot)!=0 ){
    Malfunction(__LINE__, "cannot change to directory [%s]", zRoot);
  }
  
  if( zPermUser && useChrootJail && getuid()==0 ){
    if( chroot(".")<0 ){
      Malfunction(__LINE__, "unable to create chroot jail");
    }else{
      zRoot = "";
    }
  }

  /* Activate the server, if requested */
  if( iPort && http_server(iPort, 0) ){
    Malfunction(__LINE__, "failed to start server");
  }

  /* Drop root privileges.
  */
  if( zPermUser ){
    struct passwd *pwd = getpwnam(zPermUser);
    if( pwd ){




      setgid(pwd->pw_gid);
      setuid(pwd->pw_uid);
    }else{
      Malfunction(__LINE__, "no such user [%s]", zPermUser);
    }
  }
  if( getuid()==0 ){
    Malfunction(__LINE__, "cannot run as root");
  }




  /* Get the IP address from when the request originates
  */
  {
    struct sockaddr_in remoteName;
    unsigned int size = sizeof(struct sockaddr_in);
    if( getpeername(0, (struct sockaddr*)&remoteName, &size)>=0 ){