home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 35 Internet / 35-Internet.zip / srev13h.zip / ALIASCHK.RXX < prev    next >
Text File  |  1999-11-11  |  13KB  |  502 lines

  1. /* Check aliases module for SRE-http. Uses contents of aliasfile
  2. To call: set queue/sempahore with:
  3.  newqueue,newsem,OLDURL
  4. Returns
  5.  match , newurl
  6. If match=0. then no match (and newurl=oldurl)
  7. */
  8.  
  9. parse upper arg aliasfile,  usequeue , USESEM, max_semwait,sport
  10.  
  11. mytid=dostid()
  12.  
  13. foo=pmprintf_sref(' SRE-http Alias:  thread, file and queue: 'mytid ', 'aliasfile', 'usequeue,,sport)
  14.  
  15. cfgs_dir=value('SRE_CFGS_DIR',,'os2environment')
  16. cfglist_file=cfgs_dir||'\CFGLIST.CFG'
  17.  
  18. defrealm=value('SREF_DEFREALM_FILE_TEMP',,'os2environment')
  19.  
  20.  
  21. resetit:
  22. if usequeue="" | USESEM="" then do
  23.    call pmprintf('SRE-http Alias ERROR: initialization ERROR: '||usequeue)
  24.    exit
  25. end
  26.  
  27. call set_alias(aliasfile)
  28.  
  29. foo=pmprintf_sref(' SRE-http Alias:  #aliases= '||naliases,,sport)
  30.  
  31.  
  32. /* Initialization now done == start waiting for requests for alias info */
  33. signal on error name iserror
  34. signal on syntax name iserror
  35. bakme:
  36.  a=rxqueue('s',usequeue)
  37.  aq=queued()
  38.  if aq=0 then do
  39.     WOW=EVENTSEM_WAIT(USESEM,max_semwait)
  40.     aq=-1
  41.     if wow=640 then do
  42.          signal bakme
  43.     end
  44.  
  45.     IF WOW<>0 THEN do         /* FATAL ERROR */
  46.        call pmprintf(' SRE-http Alias ERROR:  fatal semaphore error ')
  47.        exit
  48.     end
  49.  end
  50.  wow=EVENTSEM_RESET(usesem)
  51.  if aq=-1 then
  52.     if queued()=0 then    signal bakme
  53.  
  54.   PARSE pull isit0
  55.  
  56.   isit0=translate(isit0,' ','000d0a09'x)
  57.   if isit0="" then signal bakme
  58.  
  59. /* die command? */
  60.      if abbrev(isit0,'*DIE*')=1 then
  61.           exit
  62.  
  63.      parse var isit0  idnum ',' newq ',' newsem ',' ISIT
  64.      parse var idnum idnum host_nickname
  65.      isitorig=isit
  66.      if newq="" | newsem=""  then do
  67.         foo=pmprintf_sref(' SRE-http Alias ERROR: missing queue or semaphore ',,sport)
  68.         signal bakme
  69.      end
  70.      newq=upper(strip(newq)); newsem=upper(strip(newsem))
  71.      ISIT=STRIP(ISIT)
  72.  
  73.  
  74.    if abbrev(isit,'*LIST*') then do
  75.         dog1=''
  76.         do mm=1 to aliases.0
  77.             dog1=dog1||aliashosts.mm '// 'aliases.mm||'0d0a'x
  78.         end
  79.       a=rxqueue('s',newq)
  80.       push idnum ',' dog1
  81.       wow=eventsem_post(newsem)
  82.       signal bakme
  83.    end
  84.  
  85.   if abbrev(isit,'*RESET*') then do
  86.        parse var isit foo newfile .
  87.           if newfile<>' ' then aliasfile=newfile
  88.           call set_alias(aliasfile)
  89.           foo=pmprintf_sref(' SRE-http Alias reset:  #aliases= '||naliases,,sport)
  90.    end
  91.    else do
  92.        if  naliases=0 then do
  93.            dog1=' 0 '||isitorig
  94.        end
  95.        else do
  96.           if pos('?',isit)>0 then do
  97.              parse var isit a1 '?' a2
  98.              isit=translate(a1,'/','\')
  99.              isit=strip(isit,'l','/')||'?'||a2
  100.           end
  101.           dog1=fig_alias(isit,host_nickname)
  102.        end
  103.       a=rxqueue('s',newq)
  104.       push idnum ',' dog1
  105.       wow=eventsem_post(newsem)
  106.    end
  107.  
  108. signal bakme
  109.  
  110.  
  111. iserror:                /* jump here on an error */
  112.  
  113. signal off error ; signal off syntax
  114. call pmprintf_sref(' Error in daemon ('usequeue'), exiting: 'sigl','rc)
  115. a=rxqueue('d',usequeue)
  116. call pmprintf("   Status= "a " shutting down "usequeue)
  117. a=eventsem_close(usesem)
  118. call pmprintf("   Status= "a " shutting down "usesem)
  119. foo=value('SREF_REDO',1,'os2environment')
  120. badt=value('SREF_ALIAS_BAD',1,'os2environment')
  121.  
  122. exit
  123.  
  124.  
  125.  
  126. /* ---------- */
  127. fig_alias:procedure expose aliases. aliashosts. usequeue usesem
  128.  
  129.  
  130.   parse arg inline , host_nickname,isit0
  131.   isdebug=0
  132.   host_nickname=strip(upper(host_nickname))
  133.   inline=strip(inline)
  134.   if abbrev(inline,'++:')=1 then do
  135.       inline=substr(inline,4)
  136.       isdebug=1
  137.   end
  138.   inline=strip(strip(inline),'l','/')
  139.   sel=translate(inline,' ','000d0a09'x)
  140.   doexact=0
  141.   gotit=0 ; resu=' ';tsel=upper(sel); gotit2=''
  142.  
  143.  
  144. /* superceding host specific matches first? */
  145. if abbrev(host_nickname,'_!')=1 then do   /* check superceding-host specific matches first */
  146.   do m=1 to aliases.0
  147.       if host_nickname<>aliashosts.m then iterate  /* not for this host */
  148.       jlias=aliases.m
  149.       parse var jlias jlias1 .
  150.       aresu=sref_wild_match(tsel,jlias1,resu)
  151.  
  152.       if aresu=0 then iterate     /* no match */
  153.       if aresu=-1 then do  /* exact match */
  154.           resu=-1
  155.           gotit=m
  156.           gotit2=jlias
  157.           leave
  158.       end
  159.       resu=aresu
  160.       GOTIT=M
  161.       gotit2=jlias
  162.       iterate
  163.   end
  164. end
  165.  
  166. if tsel='' then tsel='/'
  167. /* no host specfic and not "strict-superceding host", try a generic match? */
  168. if gotit=0 & abbrev(host_nickname,'_!!')=0 then do
  169.   do m=1 to aliases.0               
  170.       if abbrev(host_nickname,'_!')=1 & aliashosts.m<>'' then iterate  /* do not check superceding hosts */
  171.       if (aliashosts.m<>'' & aliashosts.m<>host_nickname) then iterate
  172.       jlias=aliases.m
  173.       parse var jlias jlias1 .
  174.       aresu=sref_wild_match(tsel,jlias1,resu)
  175.  
  176.       if aresu=0 then iterate     /* no match */
  177.       if aresu=-1 then do  /* exact match */
  178.           resu=-1
  179.           gotit=m
  180.           gotit2=jlias
  181.           leave
  182.       end
  183.       resu=aresu
  184.       GOTIT=M
  185.       gotit2=jlias
  186.      iterate 
  187.   end
  188. end
  189.  
  190. select
  191.   when gotit=0 then usesel=inline  /* no match, use original */
  192.   when resu=-1 then do            /* exact match, use candidate as is */
  193.      parse var aliases.gotit . usesel 
  194.   end
  195.   otherwise do                  /* wildcard match */
  196.     parse var aliases.gotit . jlias1 ; jlias1=strip(jlias1)
  197.     if pos('*',jlias1)=0 then do    /* no wildcards in candidate */
  198.         usesel=jlias1
  199.     end
  200.     else do               /* fill in * appropriately */
  201.       psel=sel
  202.       do il=1 to words(resu)
  203.         psel=overlay(' ',psel,strip(word(resu,il)))
  204.       end
  205.       arf='';idog=0 ; jlias2=jlias1
  206.       do until jlias2=""
  207.         parse var jlias2 a1 '*' jlias2
  208.         arf=arf||a1
  209.         if jlias2<>' ' then do
  210.            idog=idog+1
  211.            if idog<=words(psel) then arf=arf||strip(word(psel,idog))
  212.  
  213.         end
  214.         else do
  215.            if right(jlias1,1)='*' then do
  216.                idog=idog+1
  217.                if idog<=words(psel) then arf=arf||strip(word(psel,idog))
  218.             end
  219.             leave
  220.         end     /* parse on * */
  221.       end         /* scanning jlias2 */
  222.       usesel=space(arf,0)
  223.     end           /* fill in wildcards */
  224.   end           /* otherwise */
  225. end   /*select */
  226.  
  227. /* fix up / */
  228.  parse var usesel k1 '?' k2
  229.  k1=translate(k1,'/','\')
  230.  if pos('?',usesel)>0 then
  231.         usesel=k1||'?'||k2
  232.    else
  233.        usesel=k1
  234.  
  235. if isdebug=1 then gotit=STRIP(gotit)':'gotit2
  236.  
  237. return gotit ' ' usesel
  238.  
  239.  
  240.  
  241. /* ---------------- */
  242. /* read and set up aliases */
  243. set_alias: 
  244. parse arg afile
  245.  
  246. naliases=0
  247. aliases.0=0
  248.  
  249. a=sref_fileread(afile,'ulines',,'E')   /* read it to a stem variable */
  250. foo=do_extends(1)
  251.  
  252.  
  253. oo=stream(afile,'c','close')
  254. if a=0 then do
  255.       foo=pmprintf_sref(' SRE-http Alias ERROR: ERROR reading user-file: '||afile,,sport)
  256.       return
  257. end
  258.  
  259. foo=add_cfglist('ALIAS','ALIASES.IN')          /*augment the ulines stem variable, using CFGLIST.CFG files */
  260.  
  261.  
  262. foo=add_defrealm('ALIAS')               /* augment from DEFREALM_TEMP_FILE */
  263.  
  264. exton=0
  265. do mm=1 to ulines.0
  266.   t1=translate(ulines.mm,' ','0d0a09'x)
  267.   t1=strip(t1)
  268.  
  269.   if exton=1 then do
  270.        t1=t1storage||t1
  271.   end
  272.   exton=0 
  273.  
  274.   if t1="" then iterate
  275.   if abbrev(t1,';')=1 then iterate
  276.  
  277.   if right(t1,2)=' ,' & mm < ulines.0 then do
  278.         exton=1
  279.         t1=left(t1,length(t1)-2)
  280.         t1storage=strip(t1)
  281.         iterate
  282.   end
  283.  
  284.  
  285.   tm1=strip(word(t1,1)) ; ahost=' '
  286.   if right(tm1,2)='//' then do
  287.         ahost=upper(left(tm1,length(tm1)-2))
  288.         parse var t1 foo t1a t1b
  289.   end
  290.   else do
  291.       parse var t1 t1a t1b
  292.   end
  293.  
  294.  
  295.   if pos('?',t1a)>0 then do
  296.      parse var t1a q1 '?' q2
  297.      t1a=strip(translate(q1,'/','\'),'l','/')||'?'||q2
  298.    end
  299.    else do
  300.       t1a=strip(translate(t1a,'/','\'),'l','/')
  301.    end
  302.  
  303.   if pos('?',t1b)>0 then do
  304.      parse var t1b q1 '?' q2
  305.      t1b=strip(translate(q1,'/','\'),'l','/')||'?'||q2
  306.    end
  307.    else do
  308.       t1b=strip(translate(t1b,'/','\'),'l','/')
  309.    end
  310.  
  311.    if t1a='' then t1a='/'
  312.    naliases=naliases+1
  313.    aliases.naliases=upper(t1a)||'  '||t1b
  314.    aliashosts.naliases=ahost
  315. end
  316. aliases.0=naliases
  317.  
  318. return 0
  319.  
  320.  
  321.  
  322. /************/
  323. /* Redo ulines, by treating lines starting with , as continuation lines */
  324. do_extends:procedure expose ulines.
  325.  
  326. if ulines.0=0 then return
  327. isnew=1
  328. tmps.1=ulines.1
  329. do mm=2 to ulines.0
  330.    ali=strip(ulines.mm)
  331.    if abbrev(ali,',')=0 then do
  332.         isnew=isnew+1
  333.         tmps.isnew=ulines.mm
  334.     end
  335.     else do
  336.         tmps.isnew=tmps.isnew||substr(ali,2)
  337.     end
  338. end
  339. do mm=1 to isnew
  340.     ulines.mm=tmps.mm
  341. end
  342. ulines.0=isnew
  343. return 0
  344.  
  345.  
  346.  
  347. /**********************************/
  348. /* Add entries to ulines. variable, from the defrealm_temp file.
  349.   Note that this file only has the appropriate port entries
  350.   in it */
  351. add_defrealm:procedure expose defrealm ulines. 
  352.  
  353. parse upper arg atype
  354. atype=strip(atype)
  355. crlf='0d0a'x
  356.  
  357. /* call pmprintf(' xxx 'atype' 'defrealm) */
  358. if defrealm='' then return 0
  359. aa=sref_open_read(defrealm,15,'READ')
  360. if aa<=0 then return 0
  361.  
  362. ii=stream(defrealm,'c','query size')
  363. if ii=0 | ii='' then return 0
  364.  
  365. stuff=charin(defrealm,1,ii)
  366. foo=stream(defrealm,'c','close')
  367.  
  368. do forever
  369.   if stuff="" then return 1
  370.   parse var stuff aline (crlf) stuff
  371.   if abbrev(aline,';')=1 then iterate
  372.   parse var aline btype ':' aline
  373.   if btype<>atype then iterate
  374.   ii=ulines.0+1
  375.   ulines.ii=aline
  376.   ulines.0=ii
  377. end
  378.  
  379.  
  380. /**********************************/
  381. /* Add entries to ulines. variable, from the  ATYPE files listed in  the CFGLIST.CFG file
  382.    (but only for port SPORT) 
  383. */
  384.  
  385. add_cfglist:procedure expose ulines. cfglist_file sport cfgs_dir
  386. parse upper arg atype,defname
  387. atype=strip(atype)
  388.  
  389. if cfglist_file='' then return /* nothing to do */
  390.  
  391. /* look for files */
  392. foo=translate(stream(cfglist_file,'c','open read'))
  393. if abbrev(foo,'READY')<>1 then do
  394.   call pmprintf('SRE-http: ' atype ' Warning: Unable to open cfglist.cfg ')
  395.   return 0            /* unable to open */
  396. end
  397. inj=stream(cfglist_file,'c','query size')
  398.  
  399. if inj=0 | inj='' then return   0        /* empty file */
  400. astuff=charin(cfglist_file,1,inj)
  401. foo=stream(cfglist_file,'c','close')
  402.  
  403. crlf='0d0a'x
  404. astuff=astuff||crlf||' '        /* place an elephant in cairo */
  405.  
  406. /* determine which files apply to this atype and port, by readling CFGLIST.CFG */
  407. mm=0
  408.  
  409. /* note: file is organized in blocks */
  410. curport=80              /* defaults port and host */
  411. curhost=''
  412. curfile=''
  413.  
  414. do forever              /* for all blocks in file */
  415.   if astuff='' then leave       /* all done  (note we always put an elephant in cairo */
  416.   parse var astuff aline (crlf) astuff
  417.  
  418.   if abbrev(aline,';')=1 then iterate   /* ignore comments */
  419.   if aline='' then do           /* block end */
  420.      if curport<>sport | curfile='' then do     /* different port, or this atype file not specified */ 
  421.         nop 
  422.      end
  423.      else do        /* otherwise, add this entry to filelist */
  424.         mm=mm+1
  425.         ufiles.mm.!host=curhost
  426.         ufiles.mm.!file=curfile
  427.      end
  428.      curport=80 ; curhost='' ; curfile=''  /* clear block */
  429.      iterate
  430.   end
  431.  
  432. /* process an entry in this block */
  433.   parse upper var aline ltype ':' lstuff ;ltype=strip(ltype) ; lstuff=strip(lstuff)
  434.   select
  435.     when ltype='PORT' then curport=lstuff
  436.     when ltype='HOST' then curhost=lstuff
  437.     when abbrev(ltype,atype)=1  then do
  438.        curfile=strip(translate(lstuff,'\','/'),'l','\')
  439.        if pos(':',curfile)=0 then curfile=cfgs_dir||'\'||curfile
  440.     end
  441.     when ltype='*'  then do
  442.        curfile=strip(translate(lstuff,'\','/'),,'\')
  443.        if pos(':',curfile)=0 then curfile=cfgs_dir||'\'||curfile
  444.        curfile=stream(curfile'\'defname,'c','query exists')
  445.     end
  446.  
  447.     otherwise nop
  448.   end
  449.  
  450. end
  451.  
  452. /* done reading cfglist; add entries from appropriate files */
  453. if mm=0 then return 1     /* no auxillary files of this type */
  454. ufiles.0=mm
  455.  
  456. do mm=1 to ulines.0              /* retain old uline */
  457.   ulisttmp.mm=ulines.mm
  458. end
  459. ulisttmp.0=ulines.0
  460.  
  461.  
  462. do mm=1 to ufiles.0
  463.   afile=ufiles.mm.!file
  464.   ahost=ufiles.mm.!host
  465.  
  466.   a=sref_fileread(afile,'ulines',,'E')   /* read it to a stem variable */
  467.  
  468.   if a=0 | ulines.0=0 then do
  469.       call pmprintf(' SRE-http 'atype': WARNING**: bad auxillary file: '||afile)
  470.       iterate
  471.   end
  472.    
  473.   foo=do_extends(1)             /* fixup ulines. */
  474.    call pmprintf('  SRE-http '||lower(atype)||': adding from 'AHOST ' specific file 'afile ',' ulines.0)
  475.  
  476.    do ii=1 to ulines.0
  477.         aline=strip(ulines.ii)
  478.         if ALINE=' ' then iterate
  479.         aline=translate(aline,' ','0009'x)
  480.         if  abbrev(strip(aline),';')=1 then iterate
  481.         parse var aline aw1 .
  482.         if pos('//',aw1)>0 then do
  483.             call pmprintf('SRE-http: 'atype 'warning: disallowed auxillary entry: 'aline)
  484.             iterate
  485.         end
  486.         if ahost<>'' then aline=ahost||'// '||aline
  487.         ii2=ulisttmp.0+1
  488.         ulisttmp.ii2=aline
  489.         ulisttmp.0=ii2
  490.    end
  491. end
  492.  
  493. do mm=1 to ulisttmp.0
  494.    ulines.mm=ulisttmp.mm
  495. end
  496. ulines.0=ulisttmp.0
  497. drop ulisttmp.
  498. return 1
  499.  
  500.  
  501.   
  502.