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

  1. /* Check acesss privs module for SRE-http. Uses contents of accessfile 
  2. Standard arguments on initialization: access_file, queue, semaphore
  3. Caller places on queue: newq,newsem,AURL
  4. This put on newq: amatch,privs,options,realm
  5.   amatch= entry number of match (0 if NOt match)
  6.   privs = entry privileges
  7.   options = CACHE NOSSI NOSSP
  8.   realm = realm to use for "ask client for id stuff"
  9. Options and realm are often not available.
  10. */
  11.  
  12. parse upper arg accessfile,  usequeue , USESEM, max_semwait,sport
  13.  
  14. mytid=dostid()
  15. fo=pmprintf_sref(' SRE-http Access: thread, file, queue='||mytid', 'accessfile', 'usequeue,,sport)
  16.  
  17. cfgs_dir=value('SRE_CFGS_DIR',,'os2environment')
  18. cfglist_file=cfgs_dir||'\CFGLIST.CFG'
  19.  
  20. defrealm=value('SREF_DEFREALM_FILE_TEMP',,'os2environment')
  21.  
  22. resetit:
  23. if usequeue="" | USESEM="" then do
  24.    call pmprintf('SRE-http Access ERROR: initialization ERROR: '||usequeue)
  25.    exit
  26. end
  27.  
  28. call set_access(accessfile)
  29. foo=pmprintf_sref(' SRE-http Access:  #entries='||naccess,,sport)
  30.  
  31. /* Initialization now done == start waiting for requests for access info */
  32. signal on error name iserror
  33. signal on syntax name iserror
  34. bakme:
  35.  a=rxqueue('s',usequeue)
  36.  aq=queued()
  37.  if aq=0 then do
  38.     WOW=EVENTSEM_WAIT(USESEM,max_semwait)
  39.     aq=-1
  40.     if wow=640 then do
  41.      signal bakme
  42.     end
  43.     IF WOW<>0 THEN do         /* FATAL ERROR */
  44.         call pmprintf(' Fatal error in access thread 'wow)
  45.         EXIT
  46.     end
  47.  end
  48.  
  49.  wow=EVENTSEM_RESET(usesem)
  50.  if aq=-1 then
  51.      if queued()=0 then signal bakme
  52.  
  53.  pull isit0
  54.  
  55.  isit0=upper(translate(isit0,' ','000d0a09'x))
  56.  if isit0=" " then signal bakme
  57.  
  58.      parse var isit0  idnum ','  newq ',' newsem ',' ISIT
  59.      parse var idnum idnum host_nickname 
  60.  
  61.      if newq="" | newsem="" then do
  62.         call pmprintf(' SRE-http Access ERROR: missing queue or semaphore ')
  63.         signal bakme
  64.      end
  65.      newq=strip(newq); newsem=strip(newsem) ; ISIT=STRIP(ISIT)
  66.  
  67.     if abbrev(strip(translate(isit)),'*DIE*') then 
  68.          exit
  69.  
  70.  
  71.    if abbrev(isit,'*LIST*') then do
  72.         dog1=''
  73.         do mm=1 to urls.0
  74.             dog1=dog1||hosturls.mm '// ' urls.mm' , 'privs.mm' , 'realms.mm' ; 'opts.mm||'0d0a'x
  75.         end
  76.        a=rxqueue('s',newq)
  77.        push idnum ',' dog1
  78.        wow=eventsem_post(newsem)
  79.        signal bakme
  80.     end
  81.  
  82.     if abbrev(strip(translate(isit)),'*RESET*') then do
  83.        parse var isit foo newfile .
  84.        if newfile<>' ' then accessfile=newfile
  85.        call set_access(accessfile)
  86.        foo=pmprintf_sref(' SRE-http Access: thread reset,  #acesss entries='||naccess)
  87.    end
  88.    else do
  89.       if naccess=0 then do
  90.         dog1=' 0 '
  91.        end
  92.       else do
  93.         dog1=fig_access(isit,host_nickname)
  94.       end
  95.       a=rxqueue('s',newq)
  96.       push idnum ',' dog1
  97.       wow=eventsem_post(newsem)
  98.    end
  99.  
  100.  
  101. signal bakme
  102.  
  103. iserror:                /* jump here on an error */
  104.  
  105. signal off error ; signal off syntax
  106. call pmprintf_sref(' Error in daemon ('usequeue'), exiting: 'sigl','rc)
  107. a=rxqueue('d',usequeue)
  108. call pmprintf("   Status= "a " shutting down "usequeue)
  109. a=eventsem_close(usesem)
  110. call pmprintf("   Status= "a " shutting down "usesem)
  111. foo=value('SREF_REDO',1,'os2environment')
  112. badt=value('SREF_ACCESS_BAD',1,'os2environment')
  113.  
  114. exit
  115.  
  116.  
  117.  
  118. /* ---------- */
  119. fig_access:procedure expose urls. privs. opts. realms. nrealms realm_names. realm_privs. hosturls. usesem usequeue
  120.   parse arg inline,host_nickname
  121.  
  122.  
  123. /*call pmprintf(' aceess for 'inline)*/
  124.   isdebug=0
  125.  
  126.   if abbrev(inline,'++:') then do  /* special code used by checksel utility */
  127.       inline=substr(inline,4)
  128.       isdebug=1
  129.    end
  130.  
  131.   inline=strip(translate(inline,' ','000d0a09'x))
  132.   inline=translate(inline,'/','\')
  133.   inline=strip(inline,'l','/')
  134.   host_nickname=strip(upper(host_nickname))
  135.  
  136.  
  137. /* got a request -- look for a POSSIBLY multiple wild-card match */
  138.  gotit=0 ; RESU=' '; gotit2=''
  139.  
  140.  
  141. /* if superceding host, then first check host-specific entries */
  142. if abbrev(host_nickname,'_!')<>0 then do
  143.  do mm=1 to urls.0
  144.     if hosturls.mm<>host_nickname then iterate          
  145.     aresu=sref_wild_match(inline,urls.mm,resu)
  146.     if aresu=0 then iterate     /* no match */
  147.     if aresu=-1 then do  /* exact match */
  148.         gotit=mm
  149.         gotit2=urls.mm
  150.         leave
  151.     end
  152.     resu=aresu
  153.     GOTIT=MM
  154.     gotit2=urls.mm
  155.   end
  156. end
  157.  
  158.  
  159. /* no host specfic match, and not strict-superceding host? try defaults */
  160. if gotit=0 & abbrev(host_nickname,'_!!')=0 then do
  161.  do mm=1 to urls.0
  162.     if abbrev(host_nickname,'_!')=1   & hosturls.mm<>'' then iterate /* don't check superceding host entries */
  163.     if hosturls.mm<>host_nickname  & hosturls.mm<>'' then iterate          
  164.  
  165.     aresu=sref_wild_match(inline,urls.mm,resu)
  166. /*call pmprintf(inline' 'mm' chk 'urls.mm'='aresu)*/
  167.  
  168.     if aresu=0 then iterate     /* no match */
  169.     if aresu=-1 then do  /* exact match */
  170.         gotit=mm
  171.         gotit2=urls.mm
  172.         leave
  173.     end
  174.     resu=aresu
  175.     GOTIT=MM
  176.     gotit2=urls.mm
  177.  end
  178. end
  179. if gotit=0 then  return gotit
  180. pgot=privs.gotit
  181. /*call pmprintf(' gotit 'gotit', 'pgot)*/
  182.  
  183. if realms.gotit<>"" then do     /* find realm privs? */
  184.     do nn1=1 to nrealms
  185.         if realms.gotit=realm_names.nn1 then do
  186.                pgot=pgot||' '||realm_privs.nn1
  187.                leave
  188.         end
  189.     end
  190. end
  191.  
  192. gotitx=gotit
  193. if isdebug=1 then gotitx=gotitx' 'gotit2
  194. return gotitx ' , ' pgot ',' opts.gotit ', ' realms.gotit 
  195.  
  196.  
  197.  
  198. /* ---------------- */
  199. /* read and set up access privs */
  200. set_access:
  201. parse arg afile
  202.  
  203. naccess=0 
  204. nrealms=0
  205. urls.0=0
  206. a=sref_fileread(afile,'ULINES',,'E')   /* read it to a stem variable */
  207. foo=do_extends(0)
  208. if a=0 then do
  209.       foo=pmprintf_sref(' SRE-http Access ERROR: ERROR reading access-file: '||afile,,sport)
  210.       return 0
  211. end
  212.  
  213. foo=add_cfglist('ACCESS','ACCESS.IN')          /*augment the ULINES stem variable, using CFGLIST.CFG files */
  214.  
  215. foo=add_defrealm('ACCESS')
  216.  
  217. do mm=1 to ULINES.0
  218.  
  219.      t1=strip(translate(ULINES.mm,' ','0d0a09'x))
  220.  
  221.      if abbrev(t1,';')=1 | t1=' 'then
  222.            iterate
  223.  
  224.      ahost=' '
  225.      t1w=strip(upper(word(t1,1)))
  226.      if right(t1w,2)='//' then do
  227.          ahost=left(t1w,length(t1w)-2)
  228.          t1=strip(delword(t1,1,1))
  229.      end
  230.  
  231.      if upper(word(t1,1))='!REALM' then do
  232.          nrealms=nrealms+1
  233.          parse upper var t1 foo1 realm_names.nrealms  realm_privs.nrealms
  234.          iterate
  235.      end
  236.  
  237. /*     parse var t1 aurl  privs ',' options ',' realm */
  238.  
  239.      parse var t1 aurl  privs ',' options ',' realm
  240.      naccess=naccess+1
  241.      aurl=strip(aurl) 
  242.      aurl=translate(aurl,'/','\')
  243.      aurl=strip(aurl,'l','/')
  244.      urls.naccess=translate(aurl)
  245.      privs.naccess=translate(strip(privs))
  246.      opts.naccess=translate(strip(options))
  247.      realms.naccess=strip(realm)
  248.      hosturls.naccess=translate(strip(ahost))
  249. end
  250. urls.0=naccess
  251.  
  252.  
  253. return 0
  254.  
  255.  
  256.  
  257.  
  258. /************/
  259. /* Redo ULINES, by treating lines starting with , as continuation lines */
  260. do_extends:procedure expose ULINES.
  261. if ULINES.0=0 then return 0
  262. isnew=1
  263. tmps.1=ULINES.1
  264. do mm=2 to ULINES.0
  265.    ali=strip(ULINES.mm)
  266.    if abbrev(ali,',')=0 then do
  267.         isnew=isnew+1
  268.         tmps.isnew=ULINES.mm
  269.     end
  270.     else do
  271.         tmps.isnew=tmps.isnew||substr(ali,2)
  272.     end
  273. end
  274. do mm=1 to isnew
  275.     ULINES.mm=tmps.mm
  276. end
  277. ULINES.0=isnew
  278. return 0
  279.  
  280.  
  281.  
  282. /**********************************/
  283. /* Add entries to ulines. variable, from the defrealm_temp file.
  284.   Note that this file only has the appropriate port entries
  285.   in it */
  286. add_defrealm:procedure expose defrealm ulines. 
  287.  
  288. parse upper arg atype
  289. atype=strip(atype)
  290. crlf='0d0a'x
  291.  
  292. /*call pmprintf(' xxx 'atype' 'defrealm) */
  293. if defrealm='' then return 0
  294. aa=sref_open_read(defrealm,15,'READ')
  295. if aa<=0 then return 0
  296.  
  297. ii=stream(defrealm,'c','query size')
  298. if ii=0 | ii='' then return 0
  299.  
  300. stuff=charin(defrealm,1,ii)
  301. foo=stream(defrealm,'c','close')
  302.  
  303. do forever
  304.   if stuff="" then return 1
  305.   parse var stuff aline (crlf) stuff
  306.   if abbrev(aline,';')=1 then iterate
  307.   parse var aline btype ':' aline
  308.   if btype<>atype then iterate
  309.   ii=ulines.0+1
  310.   ulines.ii=aline
  311.   ulines.0=ii
  312. end
  313.  
  314.  
  315.  
  316. /**********************************/
  317. /* Add entries to ULINES. variable, from the  ATYPE files listed in  the CFGLIST.CFG file
  318.    (but only for port SPORT) 
  319. */
  320.  
  321. add_cfglist:procedure expose ULINES. cfglist_file sport cfgs_dir
  322. parse upper arg atype,defname
  323. atype=strip(atype)
  324.  
  325. if cfglist_file='' then return /* nothing to do */
  326.  
  327. /* look for files */
  328. foo=translate(stream(cfglist_file,'c','open read'))
  329. if abbrev(foo,'READY')<>1 then do
  330.   call pmprintf('SRE-http: ' atype ' Warning: Unable to open cfglist.cfg ')
  331.   return 0            /* unable to open */
  332. end
  333. inj=stream(cfglist_file,'c','query size')
  334.  
  335. if inj=0 | inj='' then return   0        /* empty file */
  336. astuff=charin(cfglist_file,1,inj)
  337. foo=stream(cfglist_file,'c','close')
  338.  
  339. crlf='0d0a'x
  340. astuff=astuff||crlf||' '        /* place an elephant in cairo */
  341.  
  342. /* determine which files apply to this atype and port, by readling CFGLIST.CFG */
  343. mm=0
  344.  
  345. /* note: file is organized in blocks */
  346. curport=80              /* defaults port and host */
  347. curhost=''
  348. curfile=''
  349.  
  350. do forever              /* for all blocks in file */
  351.   if astuff='' then leave       /* all done  (note we always put an elephant in cairo */
  352.   parse var astuff aline (crlf) astuff
  353.  
  354.   if abbrev(aline,';')=1 then iterate   /* ignore comments */
  355.   if aline='' then do           /* block end */
  356.      if curport<>sport | curfile='' then do     /* different port, or this atype file not specified */ 
  357.         nop 
  358.      end
  359.      else do        /* otherwise, add this entry to filelist */
  360.         mm=mm+1
  361.         ufiles.mm.!host=curhost
  362.         ufiles.mm.!file=curfile
  363.      end
  364.      curport=80 ; curhost='' ; curfile=''  /* clear block */
  365.      iterate
  366.   end
  367.  
  368. /* process an entry in this block */
  369.   parse upper var aline ltype ':' lstuff ;ltype=strip(ltype) ; lstuff=strip(lstuff)
  370.   select
  371.     when ltype='PORT' then curport=lstuff
  372.     when ltype='HOST' then curhost=lstuff
  373.     when abbrev(ltype,atype)=1  then do
  374.        curfile=strip(translate(lstuff,'\','/'),'l','\')
  375.        if pos(':',curfile)=0 then curfile=cfgs_dir||'\'||curfile
  376.     end
  377.     when ltype='*'  then do
  378.        curfile=strip(translate(lstuff,'\','/'),,'\')
  379.        if pos(':',curfile)=0 then curfile=cfgs_dir||'\'||curfile
  380.        curfile=stream(curfile'\'defname,'c','query exists')
  381.     end
  382.  
  383.     otherwise nop
  384.   end
  385.  
  386. end
  387.  
  388. /* done reading cfglist; add entries from appropriate files */
  389. if mm=0 then return 1     /* no auxillary files of this type */
  390. ufiles.0=mm
  391.  
  392. do mm=1 to ULINES.0              /* retain old ULINES */
  393.   ULINEStmp.mm=ULINES.mm
  394. end
  395. ULINEStmp.0=ULINES.0
  396.  
  397.  
  398. do mm=1 to ufiles.0
  399.   afile=ufiles.mm.!file
  400.   ahost=ufiles.mm.!host
  401.  
  402.   a=sref_fileread(afile,'ULINES',,'E')   /* read it to a stem variable */
  403.  
  404.   if a=0 | ULINES.0=0 then do
  405.       call pmprintf(' SRE-http 'atype': WARNING**: bad auxillary file: '||afile)
  406.       iterate
  407.   end
  408.    
  409.   foo=do_extends(1)             /* fixup ULINES. */
  410.    call pmprintf('  SRE-http '||lower(atype)||': adding from 'AHOST ' specific file 'afile ',' ULINES.0)
  411.  
  412.    do ii=1 to ULINES.0
  413.         aline=strip(ULINES.ii)
  414.         if ALINE=' ' then iterate
  415.         aline=translate(aline,' ','0009'x)
  416.         if  abbrev(strip(aline),';')=1 then iterate
  417.         parse var aline aw1 .
  418.         if pos('//',aw1)>0 then do
  419.             call pmprintf('SRE-http: 'atype 'warning: disallowed auxillary entry: 'aline)
  420.             iterate
  421.         end
  422.         if ahost<>'' then aline=ahost||'// '||aline
  423.         ii2=ULINEStmp.0+1
  424.         ULINEStmp.ii2=aline
  425.         ULINEStmp.0=ii2
  426.    end
  427. end
  428.  
  429. do mm=1 to ULINEStmp.0
  430.    ULINES.mm=ULINEStmp.mm
  431. end
  432. ULINES.0=ULINEStmp.0
  433. drop ULINEStmp.
  434. return 1
  435.  
  436.  
  437.   
  438.