home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 35 Internet / 35-Internet.zip / srev13g.zip / ACCESCHK.RXX < prev    next >
Text File  |  1999-06-27  |  12KB  |  442 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:
  104. signal off error ; signal off syntax
  105. call pmprintf_sref(' error in access : resetting 'sigl)
  106. a=rxqueue('d',usequeue)
  107. a=rxqueue('c',usequeue)
  108. a=eventsem_close(usesem)
  109. a=eventsem_create(usesem)
  110. a=rxqueue('s',newq)
  111. push idnum  ', 0 '
  112. wow=eventsem_post(newsem)
  113. call pmprintf_sref(' done resetting access ')
  114. signal on error name iserror
  115. signal on syntax name iserror
  116. signal bakme
  117.  
  118.  
  119. exit
  120.  
  121.  
  122. /* ---------- */
  123. fig_access:procedure expose urls. privs. opts. realms. nrealms realm_names. realm_privs. hosturls.
  124.   parse arg inline,host_nickname
  125.  
  126.  
  127. /*call pmprintf(' aceess for 'inline)*/
  128.   isdebug=0
  129.  
  130.   if abbrev(inline,'++:') then do  /* special code used by checksel utility */
  131.       inline=substr(inline,4)
  132.       isdebug=1
  133.    end
  134.  
  135.   inline=strip(translate(inline,' ','000d0a09'x))
  136.   inline=translate(inline,'/','\')
  137.   inline=strip(inline,'l','/')
  138.   host_nickname=strip(upper(host_nickname))
  139.  
  140.  
  141. /* got a request -- look for a POSSIBLY multiple wild-card match */
  142.  gotit=0 ; RESU=' '; gotit2=''
  143.  
  144.  
  145. /* if superceding host, then first check host-specific entries */
  146. if abbrev(host_nickname,'_!')<>0 then do
  147.  do mm=1 to urls.0
  148.     if hosturls.mm<>host_nickname then iterate          
  149.     aresu=sref_wild_match(inline,urls.mm,resu)
  150.     if aresu=0 then iterate     /* no match */
  151.     if aresu=-1 then do  /* exact match */
  152.         gotit=mm
  153.         gotit2=urls.mm
  154.         leave
  155.     end
  156.     resu=aresu
  157.     GOTIT=MM
  158.     gotit2=urls.mm
  159.   end
  160. end
  161.  
  162.  
  163. /* no host specfic match, and not strict-superceding host? try defaults */
  164. if gotit=0 & abbrev(host_nickname,'_!!')=0 then do
  165.  do mm=1 to urls.0
  166.     if abbrev(host_nickname,'_1')=1   & hosturls.mm<>'' then iterate /* don't check superceding host entries */
  167.     if hosturls.mm<>host_nickname  & hosturls.mm<>'' then iterate          
  168.  
  169.     aresu=sref_wild_match(inline,urls.mm,resu)
  170. /*call pmprintf(inline' 'mm' chk 'urls.mm'='aresu)*/
  171.  
  172.     if aresu=0 then iterate     /* no match */
  173.     if aresu=-1 then do  /* exact match */
  174.         gotit=mm
  175.         gotit2=urls.mm
  176.         leave
  177.     end
  178.     resu=aresu
  179.     GOTIT=MM
  180.     gotit2=urls.mm
  181.  end
  182. end
  183. if gotit=0 then  return gotit
  184. pgot=privs.gotit
  185. /*call pmprintf(' gotit 'gotit', 'pgot)*/
  186.  
  187. if realms.gotit<>"" then do     /* find realm privs? */
  188.     do nn1=1 to nrealms
  189.         if realms.gotit=realm_names.nn1 then do
  190.                pgot=pgot||' '||realm_privs.nn1
  191.                leave
  192.         end
  193.     end
  194. end
  195.  
  196. gotitx=gotit
  197. if isdebug=1 then gotitx=gotitx' 'gotit2
  198. return gotitx ' , ' pgot ',' opts.gotit ', ' realms.gotit 
  199.  
  200.  
  201.  
  202. /* ---------------- */
  203. /* read and set up access privs */
  204. set_access:
  205. parse arg afile
  206.  
  207. naccess=0 
  208. nrealms=0
  209. urls.0=0
  210. a=sref_fileread(afile,'ULINES',,'E')   /* read it to a stem variable */
  211. foo=do_extends(0)
  212. if a=0 then do
  213.       foo=pmprintf_sref(' SRE-http Access ERROR: ERROR reading access-file: '||afile,,sport)
  214.       return 0
  215. end
  216.  
  217. foo=add_cfglist('ACCESS','ACCESS.IN')          /*augment the ULINES stem variable, using CFGLIST.CFG files */
  218.  
  219. foo=add_defrealm('ACCESS')
  220.  
  221. do mm=1 to ULINES.0
  222.  
  223.      t1=strip(translate(ULINES.mm,' ','0d0a09'x))
  224.  
  225.      if abbrev(t1,';')=1 | t1=' 'then
  226.            iterate
  227.  
  228.      ahost=' '
  229.      t1w=strip(upper(word(t1,1)))
  230.      if right(t1w,2)='//' then do
  231.          ahost=left(t1w,length(t1w)-2)
  232.          t1=strip(delword(t1,1,1))
  233.      end
  234.  
  235.      if upper(word(t1,1))='!REALM' then do
  236.          nrealms=nrealms+1
  237.          parse upper var t1 foo1 realm_names.nrealms  realm_privs.nrealms
  238.          iterate
  239.      end
  240.  
  241. /*     parse var t1 aurl  privs ',' options ',' realm */
  242.  
  243.      parse var t1 aurl  privs ',' options ',' realm
  244.      naccess=naccess+1
  245.      aurl=strip(aurl) 
  246.      aurl=translate(aurl,'/','\')
  247.      aurl=strip(aurl,'l','/')
  248.      urls.naccess=translate(aurl)
  249.      privs.naccess=translate(strip(privs))
  250.      opts.naccess=translate(strip(options))
  251.      realms.naccess=strip(realm)
  252.      hosturls.naccess=translate(strip(ahost))
  253. end
  254. urls.0=naccess
  255.  
  256.  
  257. return 0
  258.  
  259.  
  260.  
  261.  
  262. /************/
  263. /* Redo ULINES, by treating lines starting with , as continuation lines */
  264. do_extends:procedure expose ULINES.
  265. if ULINES.0=0 then return 0
  266. isnew=1
  267. tmps.1=ULINES.1
  268. do mm=2 to ULINES.0
  269.    ali=strip(ULINES.mm)
  270.    if abbrev(ali,',')=0 then do
  271.         isnew=isnew+1
  272.         tmps.isnew=ULINES.mm
  273.     end
  274.     else do
  275.         tmps.isnew=tmps.isnew||substr(ali,2)
  276.     end
  277. end
  278. do mm=1 to isnew
  279.     ULINES.mm=tmps.mm
  280. end
  281. ULINES.0=isnew
  282. return 0
  283.  
  284.  
  285.  
  286. /**********************************/
  287. /* Add entries to ulines. variable, from the defrealm_temp file.
  288.   Note that this file only has the appropriate port entries
  289.   in it */
  290. add_defrealm:procedure expose defrealm ulines. 
  291.  
  292. parse upper arg atype
  293. atype=strip(atype)
  294. crlf='0d0a'x
  295.  
  296. /*call pmprintf(' xxx 'atype' 'defrealm) */
  297. if defrealm='' then return 0
  298. aa=sref_open_read(defrealm,15,'READ')
  299. if aa<=0 then return 0
  300.  
  301. ii=stream(defrealm,'c','query size')
  302. if ii=0 | ii='' then return 0
  303.  
  304. stuff=charin(defrealm,1,ii)
  305. foo=stream(defrealm,'c','close')
  306.  
  307. do forever
  308.   if stuff="" then return 1
  309.   parse var stuff aline (crlf) stuff
  310.   if abbrev(aline,';')=1 then iterate
  311.   parse var aline btype ':' aline
  312.   if btype<>atype then iterate
  313.   ii=ulines.0+1
  314.   ulines.ii=aline
  315.   ulines.0=ii
  316. end
  317.  
  318.  
  319.  
  320. /**********************************/
  321. /* Add entries to ULINES. variable, from the  ATYPE files listed in  the CFGLIST.CFG file
  322.    (but only for port SPORT) 
  323. */
  324.  
  325. add_cfglist:procedure expose ULINES. cfglist_file sport cfgs_dir
  326. parse upper arg atype,defname
  327. atype=strip(atype)
  328.  
  329. if cfglist_file='' then return /* nothing to do */
  330.  
  331. /* look for files */
  332. foo=translate(stream(cfglist_file,'c','open read'))
  333. if abbrev(foo,'READY')<>1 then do
  334.   call pmprintf('SRE-http: ' atype ' Warning: Unable to open cfglist.cfg ')
  335.   return 0            /* unable to open */
  336. end
  337. inj=stream(cfglist_file,'c','query size')
  338.  
  339. if inj=0 | inj='' then return   0        /* empty file */
  340. astuff=charin(cfglist_file,1,inj)
  341. foo=stream(cfglist_file,'c','close')
  342.  
  343. crlf='0d0a'x
  344. astuff=astuff||crlf||' '        /* place an elephant in cairo */
  345.  
  346. /* determine which files apply to this atype and port, by readling CFGLIST.CFG */
  347. mm=0
  348.  
  349. /* note: file is organized in blocks */
  350. curport=80              /* defaults port and host */
  351. curhost=''
  352. curfile=''
  353.  
  354. do forever              /* for all blocks in file */
  355.   if astuff='' then leave       /* all done  (note we always put an elephant in cairo */
  356.   parse var astuff aline (crlf) astuff
  357.  
  358.   if abbrev(aline,';')=1 then iterate   /* ignore comments */
  359.   if aline='' then do           /* block end */
  360.      if curport<>sport | curfile='' then do     /* different port, or this atype file not specified */ 
  361.         nop 
  362.      end
  363.      else do        /* otherwise, add this entry to filelist */
  364.         mm=mm+1
  365.         ufiles.mm.!host=curhost
  366.         ufiles.mm.!file=curfile
  367.      end
  368.      curport=80 ; curhost='' ; curfile=''  /* clear block */
  369.      iterate
  370.   end
  371.  
  372. /* process an entry in this block */
  373.   parse upper var aline ltype ':' lstuff ;ltype=strip(ltype) ; lstuff=strip(lstuff)
  374.   select
  375.     when ltype='PORT' then curport=lstuff
  376.     when ltype='HOST' then curhost=lstuff
  377.     when abbrev(ltype,atype)=1  then do
  378.        curfile=strip(translate(lstuff,'\','/'),'l','\')
  379.        if pos(':',curfile)=0 then curfile=cfgs_dir||'\'||curfile
  380.     end
  381.     when ltype='*'  then do
  382.        curfile=strip(translate(lstuff,'\','/'),,'\')
  383.        if pos(':',curfile)=0 then curfile=cfgs_dir||'\'||curfile
  384.        curfile=stream(curfile'\'defname,'c','query exists')
  385.     end
  386.  
  387.     otherwise nop
  388.   end
  389.  
  390. end
  391.  
  392. /* done reading cfglist; add entries from appropriate files */
  393. if mm=0 then return 1     /* no auxillary files of this type */
  394. ufiles.0=mm
  395.  
  396. do mm=1 to ULINES.0              /* retain old ULINES */
  397.   ULINEStmp.mm=ULINES.mm
  398. end
  399. ULINEStmp.0=ULINES.0
  400.  
  401.  
  402. do mm=1 to ufiles.0
  403.   afile=ufiles.mm.!file
  404.   ahost=ufiles.mm.!host
  405.  
  406.   a=sref_fileread(afile,'ULINES',,'E')   /* read it to a stem variable */
  407.  
  408.   if a=0 | ULINES.0=0 then do
  409.       call pmprintf(' SRE-http 'atype': WARNING**: bad auxillary file: '||afile)
  410.       iterate
  411.   end
  412.    
  413.   foo=do_extends(1)             /* fixup ULINES. */
  414.    call pmprintf('SRE-http 'atype': adding from 'AHOST ' specific file 'afile ',' ULINES.0)
  415.  
  416.    do ii=1 to ULINES.0
  417.         aline=strip(ULINES.ii)
  418.         if ALINE=' ' then iterate
  419.         aline=translate(aline,' ','0009'x)
  420.         if  abbrev(strip(aline),';')=1 then iterate
  421.         parse var aline aw1 .
  422.         if pos('//',aw1)>0 then do
  423.             call pmprintf('SRE-http: 'atype 'warning: disallowed auxillary entry: 'aline)
  424.             iterate
  425.         end
  426.         if ahost<>'' then aline=ahost||'// '||aline
  427.         ii2=ULINEStmp.0+1
  428.         ULINEStmp.ii2=aline
  429.         ULINEStmp.0=ii2
  430.    end
  431. end
  432.  
  433. do mm=1 to ULINEStmp.0
  434.    ULINES.mm=ULINEStmp.mm
  435. end
  436. ULINES.0=ULINEStmp.0
  437. drop ULINEStmp.
  438. return 1
  439.  
  440.  
  441.   
  442.