home *** CD-ROM | disk | FTP | other *** search
/ Revista do CD-ROM 71 / CDROM71.ISO / internet / navoff / data1.cab / Sources / src / htsbauth.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-04-01  |  11.8 KB  |  400 lines

  1. /* ------------------------------------------------------------ */
  2. /*
  3. HTTrack Website Copier, Offline Browser for Windows and Unix
  4. Copyright (C) Xavier Roche, Yann Philippot and other contributors
  5.  
  6. This program is free software; you can redistribute it and/or
  7. modify it under the terms of the GNU General Public License
  8. as published by the Free Software Foundation; either version 2
  9. of the License, or any later version.
  10.  
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with this program; if not, write to the Free Software
  18. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  19.  
  20.  
  21. Important notes:
  22.  
  23. - We hereby ask people using this source NOT to use it in purpose of grabbing
  24. emails addresses, or collecting any other private information on persons.
  25. This would disgrace our work, and spoil the many hours we spent on it.
  26.  
  27.  
  28. This project has been developed by Xavier Roche and Yann Philippot,
  29. from the company Serianet at Caen, France (http://www.serianet.com)
  30. and other contributors (see the greetings file)
  31.  
  32. Please visit our Website: http://www.httrack.com
  33. */
  34.  
  35.  
  36. /* ------------------------------------------------------------ */
  37. /* File: httrack.c subroutines:                                 */
  38. /*       basic authentication: password storage                 */
  39. /* Author: Xavier Roche                                         */
  40. /* ------------------------------------------------------------ */
  41.  
  42.  
  43. #include "htsbauth.h"
  44.  
  45. /* specific definitions */
  46. #include "htsglobal.h"
  47. #include "htslib.h"
  48. #include <stdio.h>
  49. #include <stdlib.h>
  50. #include <string.h>
  51. /* END specific definitions */
  52.  
  53. // gestion des cookie
  54. // ajoute, dans l'ordre
  55. // !=0 : erreur
  56. int cookie_add(t_cookie* cookie,char* cook_name,char* cook_value,char* domain,char* path) {
  57.   char* a=cookie->data;
  58.   char* insert;
  59.   char cook[16384];
  60.   // effacer Θventuel cookie en double
  61.   cookie_del(cookie,cook_name,domain,path);
  62.   if ((int)strlen(cook_value)>1024) return -1;                              // trop long
  63.   if ((int)strlen(cook_name)>256) return -1;                                // trop long
  64.   if ((int)strlen(domain)>256) return -1;                                   // trop long
  65.   if ((int)strlen(path)>256) return -1;                                     // trop long
  66.   if ((int)(
  67.     strlen(cookie->data)
  68.     +strlen(cook_value)
  69.     +strlen(cook_name)
  70.     +strlen(domain)
  71.     +strlen(path)
  72.     +256
  73.     ) > cookie->max_len) return -1;               // impossible d'ajouter
  74.  
  75.   insert=a;          // insΘrer ici
  76.   while (*a) {
  77.     if ( strlen(cookie_get(a,2)) <  strlen(path) )      // long. path (le + long est prioritaire)
  78.       a=cookie->data+strlen(cookie->data);    // fin
  79.     else {
  80.       a=strchr(a,'\n');     // prochain champ
  81.       if (a==NULL)
  82.         a=cookie->data+strlen(cookie->data);    // fin
  83.       else
  84.         a++;
  85.       while(*a=='\n') a++;
  86.       insert=a;          // insΘrer ici
  87.     }
  88.   }
  89.   // construction du cookie
  90.   strcpy(cook,domain);
  91.   strcat(cook,"\t");
  92.   strcat(cook,"TRUE");
  93.   strcat(cook,"\t");
  94.   strcat(cook,path);
  95.   strcat(cook,"\t");
  96.   strcat(cook,"FALSE");
  97.   strcat(cook,"\t");
  98.   strcat(cook,"1999999999");
  99.   strcat(cook,"\t");
  100.   strcat(cook,cook_name);
  101.   strcat(cook,"\t");
  102.   strcat(cook,cook_value);
  103.   strcat(cook,"\n");
  104.   if (!( ((int) strlen(cookie->data) + (int) strlen(cook)) < cookie->max_len)) return -1;      // impossible d'ajouter
  105.   cookie_insert(insert,cook);
  106. #if DEBUG_COOK
  107.   printf("add_new cookie: name=\"%s\" value=\"%s\" domain=\"%s\" path=\"%s\"\n",cook_name,cook_value,domain,path);
  108.   //printf(">>>cook: %s<<<\n",cookie->data);
  109. #endif
  110.   return 0;
  111. }
  112.  
  113. // effacer cookie si existe
  114. int cookie_del(t_cookie* cookie,char* cook_name,char* domain,char* path) {
  115.   char *a,*b;
  116.   b=cookie_find(cookie->data,cook_name,domain,path);
  117.   if (b) {
  118.     a=cookie_nextfield(b);
  119.     cookie_delete(b,(int) a - (int) b);
  120. #if DEBUG_COOK
  121.     printf("deleted old cookie: %s %s %s\n",cook_name,domain,path);
  122. #endif
  123.   }
  124.   return 0;
  125. }
  126.  
  127. // rechercher cookie α partir de la position s (par exemple s=cookie.data)
  128. // renvoie pointeur sur ligne, ou NULL si introuvable
  129. // path est alignΘ α droite et cook_name peut Ωtre vide (chercher alors tout cookie)
  130. // .doubleclick.net    TRUE    /    FALSE    1999999999    id    A
  131. char* cookie_find(char* s,char* cook_name,char* domain,char* path) {
  132.   char* a=s;
  133.   while (*a) {
  134.     int t;
  135.     if (strnotempty(cook_name)==0)
  136.       t=1;                      // accepter par dΘfaut
  137.     else
  138.       t=( strcmp(cookie_get(a,5),cook_name)==0 );     // tester si mΩme nom
  139.     if (t) {  // mΩme nom ou nom qualconque
  140.       //
  141.       char* chk_dom=cookie_get(a,0);       // domaine concernΘ par le cookie
  142.       if ((int) strlen(chk_dom) <= (int) strlen(domain)) {
  143.         if ( strcmp(chk_dom,domain+strlen(domain)-strlen(chk_dom))==0 ) {  // mΩme domaine
  144.           //
  145.           char* chk_path=cookie_get(a,2);       // chemin concernΘ par le cookie
  146.           if ((int) strlen(chk_path) <= (int) strlen(path)) {
  147.             if (strncmp(path,chk_path,strlen(chk_path))==0 ) { // mΩme chemin
  148.               return a;
  149.             }
  150.           }
  151.         }
  152.       }
  153.     }
  154.     a=cookie_nextfield(a);
  155.   }
  156.   return NULL;
  157. }
  158.  
  159. // renvoie prochain champ
  160. char* cookie_nextfield(char* a) {
  161.   char* b=a;
  162.   a=strchr(a,'\n');     // prochain champ
  163.   if (a==NULL)
  164.     a=b+strlen(b);    // fin
  165.   else
  166.     a++;
  167.   while(*a=='\n') a++;
  168.   return a;
  169. }
  170.  
  171. // lire cookies.txt
  172. // lire Θgalement (Windows seulement) les *@*.txt (cookies IE copiΘs)
  173. // !=0 : erreur
  174. int cookie_load(t_cookie* cookie,char* fpath,char* name) {
  175.   cookie->data[0]='\0';
  176.  
  177.   // Fusionner d'abord les Θventuels cookies IE
  178. #if HTS_WIN
  179.   {
  180.     WIN32_FIND_DATA find;
  181.     HANDLE h;
  182.     char  pth[MAX_PATH + 32];
  183.     strcpy(pth,fpath);
  184.     strcat(pth,"*@*.txt");
  185.     h = FindFirstFile(pth,&find);
  186.     if (h != INVALID_HANDLE_VALUE) {
  187.       do {
  188.         if (!(find.dwFileAttributes  & FILE_ATTRIBUTE_DIRECTORY ))
  189.           if (!(find.dwFileAttributes  & FILE_ATTRIBUTE_SYSTEM )) {
  190.             FILE* fp=fopen(fconcat(fpath,find.cFileName),"rb");
  191.             if (fp) {
  192.               char cook_name[256];
  193.               char cook_value[1000];
  194.               char domainpathpath[512];
  195.               //
  196.               char domain[256];           // domaine cookie (.netscape.com)
  197.               char path[256];             // chemin (/)
  198.               int cookie_merged=0;
  199.               linput(fp,cook_name,250);
  200.               if (!feof(fp)) {
  201.                 linput(fp,cook_value,250);
  202.                 if ( (!feof(fp)) && (strnotempty(cook_value)) )  {
  203.                   linput(fp,domainpathpath,500);
  204.                   if (strnotempty(domainpathpath)) {
  205.                     if (ident_url(domainpathpath,domain,path)>=0) {
  206.                       cookie_add(cookie,cook_name,cook_value,domain,path);
  207.                       cookie_merged=1;
  208.                     }
  209.                   }
  210.                 }
  211.               }
  212.               fclose(fp);
  213.               if (cookie_merged)
  214.                 remove(fconcat(fpath,find.cFileName));
  215.             }  // if fp
  216.           }
  217.       } while(FindNextFile(h,&find));
  218.       FindClose(h);
  219.     }
  220.   }
  221. #endif
  222.   
  223.   // Ensuite, cookies.txt
  224.   {
  225.     FILE* fp = fopen(fconcat(fpath,name),"rb");
  226.     if (fp) {
  227.       char line[8192];
  228.       while( (!feof(fp)) && (((int) strlen(cookie->data)) < cookie->max_len)) {
  229.         rawlinput(fp,line,8100);
  230.         if (strnotempty(line)) {
  231.           if (strlen(line)<8000) {
  232.             if (line[0]!='#') {
  233.               char domain[256];           // domaine cookie (.netscape.com)
  234.               char path[256];             // chemin (/)
  235.               char cook_name[256];        // nom cookie (MYCOOK)
  236.               char cook_value[8192];      // valeur (ID=toto,S=1234)
  237.               strcpy(domain,cookie_get(line,0));       // host
  238.               strcpy(path,cookie_get(line,2));         // path
  239.               strcpy(cook_name,cookie_get(line,5));    // name
  240.               strcpy(cook_value,cookie_get(line,6));   // value
  241. #if DEBUG_COOK
  242.               printf("%s\n",line);
  243. #endif
  244.               cookie_add(cookie,cook_name,cook_value,domain,path);
  245.             }
  246.           }
  247.         }
  248.       }
  249.       fclose(fp);
  250.       return 0;
  251.     }
  252.   }
  253.   return -1;
  254. }
  255.  
  256. // Θcrire cookies.txt
  257. // !=0 : erreur
  258. int cookie_save(t_cookie* cookie,char* name) {
  259.   if (strnotempty(cookie->data)) {
  260.     char line[8192];
  261.     FILE* fp = fopen(fconv(name),"wb");
  262.     if (fp) {
  263.       char* a=cookie->data;
  264.       fprintf(fp,"# HTTrack Website Copier Cookie File"LF"# This file format is compatible with Netscape cookies"LF);
  265.       do {
  266.         a+=binput(a,line,8000);
  267.         fprintf(fp,"%s"LF,line);
  268.       } while(strnotempty(line));
  269.       fclose(fp);
  270.       return 0;
  271.     }
  272.   } else
  273.     return 0;
  274.   return -1;
  275. }
  276.  
  277. // insertion chaine ins avant s
  278. void cookie_insert(char* s,char* ins) {
  279.   char* buff;
  280.   if (strnotempty(s)==0) {    // rien α faire, juste concat
  281.     strcat(s,ins);
  282.   } else {
  283.     buff=(char*) malloc(strlen(s)+2);
  284.     if (buff) {
  285.       strcpy(buff,s);     // copie temporaire
  286.       strcpy(s,ins);      // insΘrer
  287.       strcat(s,buff);     // copier
  288.       free(buff);
  289.     }
  290.   }
  291. }
  292. // destruction chaine dans s position pos
  293. void cookie_delete(char* s,int pos) {
  294.   char* buff;
  295.   if (strnotempty(s+pos)==0) {    // rien α faire, effacer
  296.     s[0]='\0';
  297.   } else {
  298.     buff=(char*) malloc(strlen(s+pos)+2);
  299.     if (buff) {
  300.       strcpy(buff,s+pos);     // copie temporaire
  301.       strcpy(s,buff);         // copier
  302.       free(buff);
  303.     }
  304.   }
  305. }
  306.  
  307. // renvoie champ param de la chaine cookie_base
  308. // ex: cookie_get("ceci est<tab>un<tab>exemple",1) renvoi "un"
  309. char* cookie_get(char* cookie_base,int param) {
  310.   static char buffer[8192];
  311.   //
  312.   char * limit;
  313.   while(*cookie_base=='\n') cookie_base++;
  314.   limit = strchr(cookie_base,'\n');
  315.   if (!limit) limit=cookie_base+strlen(cookie_base);
  316.   if (limit) {
  317.     if (param) {
  318.       int i;
  319.       for(i=0;i<param;i++) {
  320.         if (cookie_base) {
  321.           cookie_base=strchr(cookie_base,'\t');       // prochain tab
  322.           if (cookie_base) cookie_base++;
  323.         }
  324.       }
  325.     }
  326.     if (cookie_base) {
  327.       if ((int) cookie_base < (int) limit) {
  328.         char* a = cookie_base;
  329.         while( (*a) && (*a!='\t') && (*a!='\n')) a++;
  330.         buffer[0]='\0';
  331.         strncat(buffer,cookie_base,(int) a - (int) cookie_base);
  332.         return buffer;
  333.       } else
  334.         return "";
  335.     } else
  336.       return "";
  337.   } else
  338.     return "";
  339. }
  340. // fin cookies
  341.  
  342.  
  343.  
  344. // -- basic auth --
  345.  
  346. /* dΘclarer un rΘpertoire comme possΘdant une authentification propre */
  347. int bauth_add(t_cookie* cookie,char* adr,char* fil,char* auth) {
  348.   if (cookie) {
  349.     if (!bauth_check(cookie,adr,fil)) {       // n'existe pas dΘja
  350.       bauth_chain* chain=&cookie->auth;
  351.       char* prefix=bauth_prefix(adr,fil);
  352.       /* fin de la chaine */
  353.       while(chain->next)
  354.         chain=chain->next;
  355.       chain->next=(bauth_chain*) calloc(sizeof(bauth_chain),1);
  356.       if (chain->next) {
  357.         chain=chain->next;
  358.         chain->next=NULL;
  359.         strcpy(chain->auth,auth);
  360.         strcpy(chain->prefix,prefix);
  361.         return 1;
  362.       }
  363.     }
  364.   }
  365.   return 0;
  366. }
  367.  
  368. /* tester adr et fil, et retourner authentification si nΘcessaire */
  369. /* sinon, retourne NULL */
  370. char* bauth_check(t_cookie* cookie,char* adr,char* fil) {
  371.   if (cookie) {
  372.     bauth_chain* chain=&cookie->auth;
  373.     char* prefix=bauth_prefix(adr,fil);
  374.     while(chain) {
  375.       if (strnotempty(chain->prefix)) {
  376.         if (strncmp(prefix,chain->prefix,strlen(chain->prefix))==0) {
  377.           return chain->auth;
  378.         }
  379.       }
  380.       chain=chain->next;
  381.     }
  382.   }
  383.   return NULL;
  384. }
  385.  
  386. char* bauth_prefix(char* adr,char* fil) {
  387.   static char prefix[HTS_URLMAXSIZE*2];
  388.   char* a;
  389.   strcpy(prefix,jump_identification(adr));
  390.   strcat(prefix,fil);
  391.   a=strchr(prefix,'?');
  392.   if (a) *a='\0';
  393.   if (strchr(prefix,'/')) {
  394.     a=prefix+strlen(prefix)-1;
  395.     while(*a != '/') a--;
  396.     *(a+1)='\0';
  397.   }
  398.   return prefix;
  399. }
  400.