Many hyperlinks are disabled.
Use anonymous login
to enable hyperlinks.
Overview
Comment: | Add basic authentication capabilities to althttpd.c |
---|---|
Downloads: | Tarball | ZIP archive |
Timelines: | family | ancestors | descendants | both | trunk |
Files: | files | file ages | folders |
SHA1: |
458e5b55bfbb953e26fc75140f5ce496 |
User & Date: | drh 2011-03-28 16:44:47.973 |
Context
2011-03-29
| ||
16:02 | Fix a typo in an error message for althttpd.c. (check-in: 67ea9276f4 user: drh tags: trunk) | |
2011-03-28
| ||
16:44 | Add basic authentication capabilities to althttpd.c (check-in: 458e5b55bf user: drh tags: trunk) | |
2011-03-23
| ||
18:24 | Add a line about the SQLITE_MAX_ATTACHED increase to the change log. (check-in: 15a9eb4640 user: drh tags: trunk) | |
Changes
Changes to misc/althttpd.c.
︙ | ︙ | |||
34 35 36 37 38 39 40 41 42 43 44 45 46 47 | ** other mischief. ** ** (5) Executable files are run as CGI. All other files are delivered ** as is. ** ** (6) For SSL support use stunnel and add the -https 1 option on the ** httpd command-line. */ #include <stdio.h> #include <ctype.h> #include <syslog.h> #include <stdlib.h> #include <sys/stat.h> #include <unistd.h> | > > > > > > > > > > > > > > > > > > > > > > | 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 | ** other mischief. ** ** (5) Executable files are run as CGI. All other files are delivered ** as is. ** ** (6) For SSL support use stunnel and add the -https 1 option on the ** httpd command-line. ** ** (7) If a file named "-auth" exists in the same directory as file to ** be run as CGI or to be delivered, then it contains information ** for HTTP Basic authorization. See file format details below. ** ** ** Basic Authorization: ** ** If the file "-auth" exists in the same directory as the content file ** (for both static content and CGI) then it contains the information used ** for basic authorization. The file format is as follows: ** ** * Blank lines and lines that begin with '#' are ignored ** * "http-redirect" forces a redirect to HTTPS if not there already ** * "https-only" disallows operation in HTTP ** * "user NAME LOGIN:PASSWORD" checks to see if LOGIN:PASSWORD ** authorization credentials are provided, and if so sets the ** REMOTE_USER to NAME. ** * "realm TEXT" sets the realm to TEXT. ** ** There can be multiple "user" lines. If no "user" line matches, the ** request fails with a 401 error. */ #include <stdio.h> #include <ctype.h> #include <syslog.h> #include <stdlib.h> #include <sys/stat.h> #include <unistd.h> |
︙ | ︙ | |||
104 105 106 107 108 109 110 111 112 113 114 115 116 117 | static char *zRealPort = 0; /* The real TCP port when running as daemon */ static char *zRemoteAddr = 0; /* IP address of the request */ static char *zReferer = 0; /* Name of the page that refered to us */ static char *zAccept = 0; /* What formats will be accepted */ static char *zContentLength = 0; /* Content length reported in the header */ static char *zContentType = 0; /* Content type reported in the header */ static char *zQuerySuffix = 0; /* The part of the URL after the first ? */ static int nIn = 0; /* Number of bytes of input */ static int nOut = 0; /* Number of bytes of output */ static char zReplyStatus[4]; /* Reply status code */ static int statusSent = 0; /* True after status line is sent */ static char *zLogFile = 0; /* Log to this file */ static int debugFlag = 0; /* True if being debugged */ static time_t beginTime; /* Time when this process starts */ | > > > | 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 | static char *zRealPort = 0; /* The real TCP port when running as daemon */ static char *zRemoteAddr = 0; /* IP address of the request */ static char *zReferer = 0; /* Name of the page that refered to us */ static char *zAccept = 0; /* What formats will be accepted */ static char *zContentLength = 0; /* Content length reported in the header */ static char *zContentType = 0; /* Content type reported in the header */ static char *zQuerySuffix = 0; /* The part of the URL after the first ? */ static char *zAuthType = 0; /* Authorization type (basic or digest) */ static char *zAuthArg = 0; /* Authorization values */ static char *zRemoteUser = 0; /* REMOTE_USER set by authorization module */ static int nIn = 0; /* Number of bytes of input */ static int nOut = 0; /* Number of bytes of output */ static char zReplyStatus[4]; /* Reply status code */ static int statusSent = 0; /* True after status line is sent */ static char *zLogFile = 0; /* Log to this file */ static int debugFlag = 0; /* True if being debugged */ static time_t beginTime; /* Time when this process starts */ |
︙ | ︙ | |||
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 | if( zLogFile && !omitLog ){ time_t now; struct tm *pTm; struct tms sTms; double rScale; int i; char zDate[200]; if( zScript==0 || zScript[0]==0 ) zScript = "*"; if( zRemoteAddr==0 || zRemoteAddr[0]==0 ) zRemoteAddr = "*"; if( zHttpHost==0 || zHttpHost[0]==0 ) zHttpHost = "*"; if( zReferer==0 || zReferer[0]==0 ) zReferer = "*"; for(i=0; zReferer[i]; i++){ if( isspace(zReferer[i]) ){ zReferer = "*"; break; } } if( zAgent==0 || zAgent[0]==0 ) zAgent = "*"; time(&now); pTm = localtime(&now); strftime(zDate, sizeof(zDate), "%Y-%m-%d %H:%M:%S", pTm); times(&sTms); rScale = 1.0/(double)sysconf(_SC_CLK_TCK); chdir(zRoot[0] ? zRoot : "/"); if( (log = fopen(zLogFile,"a"))!=0 ){ | > | | | 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 | if( zLogFile && !omitLog ){ time_t now; struct tm *pTm; struct tms sTms; double rScale; int i; char zDate[200]; char *zRM = zRemoteUser ? zRemoteUser : "*"; if( zScript==0 || zScript[0]==0 ) zScript = "*"; if( zRemoteAddr==0 || zRemoteAddr[0]==0 ) zRemoteAddr = "*"; if( zHttpHost==0 || zHttpHost[0]==0 ) zHttpHost = "*"; if( zReferer==0 || zReferer[0]==0 ) zReferer = "*"; for(i=0; zReferer[i]; i++){ if( isspace(zReferer[i]) ){ zReferer = "*"; break; } } if( zAgent==0 || zAgent[0]==0 ) zAgent = "*"; time(&now); pTm = localtime(&now); strftime(zDate, sizeof(zDate), "%Y-%m-%d %H:%M:%S", pTm); times(&sTms); rScale = 1.0/(double)sysconf(_SC_CLK_TCK); chdir(zRoot[0] ? zRoot : "/"); if( (log = fopen(zLogFile,"a"))!=0 ){ fprintf(log, "%s %s %s://%s%s %s %s %d %d %g %g %g %g %d %d %s %s\n", zDate, zRemoteAddr, zHttp, zHttpHost, zScript, zReferer, zReplyStatus, nIn, nOut, rScale*sTms.tms_utime, rScale*sTms.tms_stime, rScale*sTms.tms_cutime, rScale*sTms.tms_cstime, (int)(now - beginTime), nRequest, zAgent, zRM ); fclose(log); nIn = nOut = 0; } } if( closeConnection ){ exit(a); |
︙ | ︙ | |||
333 334 335 336 337 338 339 340 341 342 343 344 345 346 | "<head><title lineno=\"%d\">Not Found</title></head>\n" "<body><h1>Document Not Found</h1>\n" "The document %s is not avaivable on this server\n" "</body>\n", lineno, zScript); MakeLogEntry(0); exit(0); } /* ** Tell the client that there is an error in the script. */ static void CgiError(void){ StartResponse("500 Error"); nOut += printf( | > > > > > > > > > > > > > > > > > | 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 | "<head><title lineno=\"%d\">Not Found</title></head>\n" "<body><h1>Document Not Found</h1>\n" "The document %s is not avaivable on this server\n" "</body>\n", lineno, zScript); MakeLogEntry(0); exit(0); } /* ** Tell the client that authorization is required to access the ** document. */ static void NotAuthorized(const char *zRealm){ StartResponse("401 Authorization Required"); nOut += printf( "WWW-Authenticate: Basic realm=\"%s\"\r\n" "Content-type: text/html\r\n" "\r\n" "<head><title>Not Authorized</title></head>\n" "<body><h1>401 Not Authorized</h1>\n" "A login and password are required for this document\n" "</body>\n", zRealm); MakeLogEntry(0); } /* ** Tell the client that there is an error in the script. */ static void CgiError(void){ StartResponse("500 Error"); nOut += printf( |
︙ | ︙ | |||
420 421 422 423 424 425 426 427 428 429 430 431 432 433 | zHttp, zServerName, zServerPort, zPath, zQuerySuffix); } if( finish ){ nOut += printf("\r\n"); MakeLogEntry(0); } } /* ** Guess the mime-type of a document based on its name. */ const char *GetMimeType(const char *zName, int nName){ const char *z; int i; | > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > | 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 | zHttp, zServerName, zServerPort, zPath, zQuerySuffix); } if( finish ){ nOut += printf("\r\n"); MakeLogEntry(0); } } /* ** This function treats its input as a base-64 string and returns the ** decoded value of that string. Characters of input that are not ** valid base-64 characters (such as spaces and newlines) are ignored. */ void Decode64(char *z64){ char *zData; int n64; int i, j; int a, b, c, d; static int isInit = 0; static int trans[128]; static unsigned char zBase[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; if( !isInit ){ for(i=0; i<128; i++){ trans[i] = 0; } for(i=0; zBase[i]; i++){ trans[zBase[i] & 0x7f] = i; } isInit = 1; } n64 = strlen(z64); while( n64>0 && z64[n64-1]=='=' ) n64--; zData = z64; for(i=j=0; i+3<n64; i+=4){ a = trans[z64[i] & 0x7f]; b = trans[z64[i+1] & 0x7f]; c = trans[z64[i+2] & 0x7f]; d = trans[z64[i+3] & 0x7f]; zData[j++] = ((a<<2) & 0xfc) | ((b>>4) & 0x03); zData[j++] = ((b<<4) & 0xf0) | ((c>>2) & 0x0f); zData[j++] = ((c<<6) & 0xc0) | (d & 0x3f); } if( i+2<n64 ){ a = trans[z64[i] & 0x7f]; b = trans[z64[i+1] & 0x7f]; c = trans[z64[i+2] & 0x7f]; zData[j++] = ((a<<2) & 0xfc) | ((b>>4) & 0x03); zData[j++] = ((b<<4) & 0xf0) | ((c>>2) & 0x0f); }else if( i+1<n64 ){ a = trans[z64[i] & 0x7f]; b = trans[z64[i+1] & 0x7f]; zData[j++] = ((a<<2) & 0xfc) | ((b>>4) & 0x03); } zData[j] = 0; } /* ** Check to see if basic authorization credentials are provided for ** the user according to the information in zAuthFile. Return true ** if authorized. Return false if not authorized. ** ** File format: ** ** * Blank lines and lines that begin with '#' are ignored ** * "http-redirect" forces a redirect to HTTPS if not there already ** * "https-only" disallows operation in HTTP ** * "user NAME LOGIN:PASSWORD" checks to see if LOGIN:PASSWORD ** authorization credentials are provided, and if so sets the ** REMOTE_USER to NAME. ** * "realm TEXT" sets the realm to TEXT. */ static int CheckBasicAuthorization(const char *zAuthFile){ FILE *in; char *zRealm = "unknown realm"; char *zLoginPswd; char *zName; char zLine[2000]; in = fopen(zAuthFile, "r"); if( in==0 ){ NotFound(__LINE__); return 0; } if( zAuthArg ) Decode64(zAuthArg); while( fgets(zLine, sizeof(zLine), in) ){ char *zFieldName; char *zVal; zFieldName = GetFirstElement(zLine,&zVal); if( zFieldName==0 || *zFieldName==0 ) continue; if( zFieldName[0]=='#' ) continue; RemoveNewline(zVal); if( strcmp(zFieldName, "realm")==0 ){ zRealm = StrDup(zVal); }else if( strcmp(zFieldName,"user")==0 ){ if( zAuthArg==0 ) continue; zName = GetFirstElement(zVal, &zVal); zLoginPswd = GetFirstElement(zVal, &zVal); if( zLoginPswd==0 ) continue; if( zAuthArg && strcmp(zAuthArg,zLoginPswd)==0 ){ zRemoteUser = StrDup(zName); fclose(in); return 1; } }else if( strcmp(zFieldName,"https-only")==0 ){ if( !useHttps ){ NotFound(__LINE__); fclose(in); return 0; } }else if( strcmp(zFieldName,"http-redirect")==0 ){ if( !useHttps ){ zHttp = "https"; sprintf(zLine, "%s%s", zScript, zPathInfo); Redirect(zLine, 1); fclose(in); return 0; } }else{ NotFound(__LINE__); fclose(in); return 0; } } fclose(in); NotAuthorized(zRealm); return 0; } /* ** Guess the mime-type of a document based on its name. */ const char *GetMimeType(const char *zName, int nName){ const char *z; int i; |
︙ | ︙ | |||
657 658 659 660 661 662 663 664 665 666 667 668 669 670 | } return "application/octet-stream"; } /* ** The following table contains 1 for all characters that are permitted in ** the part of the URL before the query parameters and fragment. */ static const char allowedInName[] = { /* x0 x1 x2 x3 x4 x5 x6 x7 x8 x9 xa xb xc xd xe xf */ /* 0x */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 1x */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 2x */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, /* 3x */ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, | > > > > | 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 | } return "application/octet-stream"; } /* ** The following table contains 1 for all characters that are permitted in ** the part of the URL before the query parameters and fragment. ** ** Allowed characters: 0-9 a-z A-Z ,-./:_~ ** ** Disallowed characters include: !"#$%&'()*+;<=>?[\]^{|} */ static const char allowedInName[] = { /* x0 x1 x2 x3 x4 x5 x6 x7 x8 x9 xa xb xc xd xe xf */ /* 0x */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 1x */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 2x */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, /* 3x */ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, |
︙ | ︙ | |||
755 756 757 758 759 760 761 762 763 764 765 766 767 768 | MakeLogEntry(0); exit(0); } /* Get all the optional fields that follow the first line. */ zCookie = 0; while( fgets(zLine,sizeof(zLine),stdin) ){ char *zFieldName; char *zVal; nIn += strlen(zLine); zFieldName = GetFirstElement(zLine,&zVal); if( zFieldName==0 || *zFieldName==0 ) break; | > > | 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 | MakeLogEntry(0); exit(0); } /* Get all the optional fields that follow the first line. */ zCookie = 0; zAuthType = 0; zRemoteUser = 0; while( fgets(zLine,sizeof(zLine),stdin) ){ char *zFieldName; char *zVal; nIn += strlen(zLine); zFieldName = GetFirstElement(zLine,&zVal); if( zFieldName==0 || *zFieldName==0 ) break; |
︙ | ︙ | |||
794 795 796 797 798 799 800 801 802 803 804 805 806 807 | if( zServerPort && *zServerPort ){ *zServerPort = 0; zServerPort++; } if( zRealPort ){ zServerPort = StrDup(zRealPort); } } } /* Make an extra effort to get a valid server name and port number. ** Only Netscape provides this information. If the browser is ** Internet Explorer, then we have to find out the information for ** ourselves. | > > | 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 | if( zServerPort && *zServerPort ){ *zServerPort = 0; zServerPort++; } if( zRealPort ){ zServerPort = StrDup(zRealPort); } }else if( strcasecmp(zFieldName,"Authorization:")==0 ){ zAuthType = GetFirstElement(StrDup(zVal), &zAuthArg); } } /* Make an extra effort to get a valid server name and port number. ** Only Netscape provides this information. If the browser is ** Internet Explorer, then we have to find out the information for ** ourselves. |
︙ | ︙ | |||
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 | for(i=strlen(zDir)-1; i>0 && zDir[i]!='/'; i--){}; if( i==0 ){ strcpy(zDir,"/"); }else{ zDir[i] = 0; } /* Take appropriate action */ if( (statbuf.st_mode & 0100)==0100 && access(zFile,X_OK)==0 ){ /* ** The followings static variables are used to setup the environment ** for the CGI script */ static char *default_path = "/bin:/usr/bin"; static char *gateway_interface = "CGI/1.0"; static struct { char *zEnvName; char **pzEnvValue; } cgienv[] = { { "CONTENT_LENGTH", &zContentLength }, { "CONTENT_TYPE", &zContentType }, { "DOCUMENT_ROOT", &zHome }, { "GATEWAY_INTERFACE", &gateway_interface }, { "HTTP_ACCEPT", &zAccept }, { "HTTP_COOKIE", &zCookie }, { "HTTP_HOST", &zHttpHost }, { "HTTP_REFERER", &zReferer }, { "HTTP_USER_AGENT", &zAgent }, { "PATH", &default_path }, { "PATH_INFO", &zPathInfo }, { "QUERY_STRING", &zQueryString }, { "REMOTE_ADDR", &zRemoteAddr }, { "REQUEST_METHOD", &zMethod }, { "REQUEST_URI", &zScript }, { "SCRIPT_DIRECTORY", &zDir }, { "SCRIPT_FILENAME", &zFile }, { "SCRIPT_NAME", &zRealScript }, { "SERVER_NAME", &zServerName }, { "SERVER_PORT", &zServerPort }, { "SERVER_PROTOCOL", &zProtocol }, }; | > > > > > > > > > | 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 | for(i=strlen(zDir)-1; i>0 && zDir[i]!='/'; i--){}; if( i==0 ){ strcpy(zDir,"/"); }else{ zDir[i] = 0; } /* Check to see if there is an authorization file. If there is, ** process it. */ sprintf(zLine, "%s/-auth", zDir); if( access(zLine,R_OK)==0 && !CheckBasicAuthorization(zLine) ) return; /* Take appropriate action */ if( (statbuf.st_mode & 0100)==0100 && access(zFile,X_OK)==0 ){ /* ** The followings static variables are used to setup the environment ** for the CGI script */ static char *default_path = "/bin:/usr/bin"; static char *gateway_interface = "CGI/1.0"; static struct { char *zEnvName; char **pzEnvValue; } cgienv[] = { { "AUTH_TYPE", &zAuthType }, { "AUTH_CONTENT", &zAuthArg }, { "CONTENT_LENGTH", &zContentLength }, { "CONTENT_TYPE", &zContentType }, { "DOCUMENT_ROOT", &zHome }, { "GATEWAY_INTERFACE", &gateway_interface }, { "HTTP_ACCEPT", &zAccept }, { "HTTP_COOKIE", &zCookie }, { "HTTP_HOST", &zHttpHost }, { "HTTP_REFERER", &zReferer }, { "HTTP_USER_AGENT", &zAgent }, { "PATH", &default_path }, { "PATH_INFO", &zPathInfo }, { "QUERY_STRING", &zQueryString }, { "REMOTE_ADDR", &zRemoteAddr }, { "REQUEST_METHOD", &zMethod }, { "REQUEST_URI", &zScript }, { "REMOTE_USER", &zRemoteUser }, { "SCRIPT_DIRECTORY", &zDir }, { "SCRIPT_FILENAME", &zFile }, { "SCRIPT_NAME", &zRealScript }, { "SERVER_NAME", &zServerName }, { "SERVER_PORT", &zServerPort }, { "SERVER_PROTOCOL", &zProtocol }, }; |
︙ | ︙ |