home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 35 Internet / 35-Internet.zip / srev13h.zip / checksel.cmd < prev    next >
OS/2 REXX Batch file  |  1999-03-21  |  17KB  |  538 lines

  1. /* check stuff for a candidate selector */
  2.  
  3. parse arg  ddir, tempfile, reqstrg,list,verb ,uri,user, ,
  4.           basedir ,workdir,privset,enmadd,transaction,verbose, ,
  5.          servername,host_nickname,homedir,aparam,semqueue,prog_file
  6.  
  7. if verb=" " then do
  8.    say "This SRE-http procedure is not meant to be run in standalone mode "
  9.    exit
  10. end  /* Do */
  11.  
  12. myaddr=extract('CLIENTADDR')
  13. /* if no username and password provided, get current ones */
  14. foo=sref_extract_upwd()
  15. parse var foo username ':' password
  16. anarg=a1' 'a2' 'anip
  17.  
  18. list=translate(packur(list),' ','+'||'0d0a09'x)
  19.  
  20. do until list=''
  21.    parse var list a1 '&' list
  22.    parse var a1 avar '=' aval;avar=upper(strip(avar))
  23.    select
  24.       when avar='TESTURL' then asel0=strip(aval)
  25.       when avar='TESTCLIENT'  then myaddr=strip(aval)
  26.       when avar='USERNAME' then username=strip(aval)
  27.       when avar='PASSWORD' then password=strip(aval)
  28.       otherwise nop
  29.    end
  30. end /* do */
  31.  
  32.  
  33. parse var semqueue mysem myqueue
  34. mysem=strip(mysem); myqueue=strip(myqueue)
  35. parse var asel0 asel '?' .
  36. asel=strip(asel)
  37.  
  38. stuff=get_host_info(protocol,asel)
  39. host_nickname=strip(host_nickname); ddir=strip(ddir)
  40. servername=strip(servername)
  41.  
  42.  
  43.  
  44. /* check for clientaddr argument. if none, use own */
  45. basesem='\SEM32\'||enmadd
  46. os2e='os2environment'
  47. mytid=dostid()
  48. no_virtual=0
  49. tempdata_dir=get_value('TEMPDATA_DIR',0)
  50. serverport=extract('serverport')
  51.  
  52. myname=sref_clientname(myaddr,mysem,myqueue,basesem,enmadd,transaction)
  53.  
  54. owners=get_value('OWNERS')
  55. badips=get_Value('UNALLOWEDIPS')
  56.  
  57.  
  58. protocol=extract('clientprotocol')
  59.  
  60. /* write some details */
  61.     
  62.   call lineout tempfile, '<!doctype html public "-//IETF//DTD HTML 2.0//EN">'
  63.   call lineout tempfile, "<html><head><title>Checking selector </title>"
  64.   call lineout tempfile, "</head><body>"
  65.   call lineout tempfile,' <h2>Checking selector:    'asel0' </h2><hr>'
  66.   call lineout tempfile,'<a href="'asel0'">'asel0'</a><p>'
  67.   call lineout tempfile,' <menu><li> <b>Servername: </b> <tt>'servername'</tt>'
  68.   call lineout tempfile,' <li> <b>w/host nickname: </b> <tt>'host_nickname'</tt>'
  69.   call lineout tempfile,' <li> <b>and data directory: </b> <tt>'ddir'</tt>'
  70.   call lineout tempfile,'</menu> '
  71.   call lineout tempfile,' <b>Your client address: </b><tt>' myaddr'</tt>'
  72.   call lineout tempfile,'<menu><li><b> Client name= </b><tt>' myname'</tt>' 
  73.   call lineout tempfile,' <li> Your username/password: <tt>'username ',' password '</tt>'
  74.  
  75.   if username<>'' & password<>'' then do
  76.       aline=wait_queue('USER','BASIC 'username' 'password)
  77.       parse var aline isgot privset1
  78.       call fix_privset1
  79.       if isgot>=1 then do
  80.          call lineout tempfile,'<br>   with client privileges = 'privset1
  81.          if privset1_secret<>'' then
  82.            call lineout tempfile,'(and '||words(privset1_secret)||' secret privileges)'
  83.       end
  84.       else do
  85.          call lineout tempfile,' <br> no match to this username/password '
  86.       end
  87.      call add_privs_2
  88.      call lineout tempfile,' <li><b>Other privileges:</b> <tt>'upper(privset) '</tt>'
  89.   end
  90.  
  91.  
  92.  
  93.   mdo=0
  94.   if badips=1 then do
  95.      mdo=sref_badips(myaddr,enmadd,host_nickname)
  96.      is_public=0
  97.   end
  98.   if mdo=1 then call lineout tempfile,' This is a UNALLOWEDIPS <p>'
  99.   if wordpos(who,owners)>0 then call lineout tempfile,' This is an OWNER '
  100.   call goodips myaddr
  101.   if result=1 then call lineout tempfile,' <li>This an INHOUSEIPS'
  102.   call lineout tempfile,'</menu>'        
  103.  
  104.  
  105.  if abbrev(asel,'!')=1 then do
  106.      call lineout tempfile,'<B> The selector is a special request '
  107.      call lineout tempfile,'</body><html>'
  108.      call lineout tempfile
  109.      call sref_gos('FILE type text/html name ' tempfile)
  110.      return ' '
  111.  end /* do */
  112.  
  113.  sreproxy=value('SREF_PROXY',,'os2environment')
  114.  if sreproxy=1 then do
  115.    dd=check_cache(host_nickname,asel)
  116.    if strip(translate(dd,' ','0d0a09'x))<>0 then do
  117.        parse var dd . '0d0a'x cfile .
  118.       call lineout tempfile,' <b>SRE-Proxy using = </b><tt>'cfile '</tt><p>'
  119.    end /* do */
  120.  end
  121.  
  122.  foo2=value(enmadd||'PUBURL_FILE')      /* any public urls to check? */
  123.  if foo2<>"" then do
  124.     aline=wait_queue('PUBURL',asel)
  125.     if aline<>0 then
  126.        call lineout tempfile,'<b> Public Url Match:</b><tt> 'aline '</tt><p>'
  127.   end
  128.  
  129.  
  130. /*  6) now check ACCESS file */
  131.  
  132.   aline=wait_queue('ACCESS','++:'||asel,'NO')
  133.   parse var aline istat ',' accprivs ',' accopts ',' urlrealm ',' afail_file ',' adv_opts
  134.   parse var istat istat istatname; istat=strip(istat)
  135.   accopts=space(strip(upper(translate(accopts,' ','09'x))),1)
  136.   accprivs=space(strip(upper(translate(accprivs,' ','09'x))),1)
  137.  
  138.   if istat=0 then
  139.     call lineout tempfile,' <u>Access status</u>=<tt> no match</tt>'
  140.   else
  141.     call lineout tempfile,' <u>Access status</u>: match entry 'istat '=<tt>' istatname'</tt>'
  142.  
  143.   if istat>0 then do
  144.    call lineout tempfile,'<ul><li><b>Resource privileges: </b><tt>'accprivs'</tt>'
  145.    call lineout tempfile,'   <li><b>Resource permissions: </b><tt>'accopts'</tt>'
  146.    call lineout tempfile,'   <li><b>Resource realm: </b><tt>'urlrealm'</tt>'
  147.    call lineout tempfile,'   <li><b>Access fail file: </b><tt>'afail_file'</tt>'
  148.    if adv_opts<>'' then do
  149.       call lineout tempfile,'   <li><b>Advanced options file: </b><tt>'adv_opts'</tt><pre>'
  150.       call get_adv_opts
  151.       call lineout tempfile,adv_opts'</pre>'
  152.    end
  153.    call lineout tempfile,'</ul>'
  154.   end
  155.  
  156. /*  8a) is it for the  DEFAULT "home page" 
  157.         (relative to datadir, No virtual directory  lookups! */
  158.  
  159.  if asel=' '  then do
  160.       sel=sref_get_default(default,ddir,verbose,myqueue) /* always relative to datadir */
  161.       call lineout tempfile,' Using Default (home page): <tt>'sel'</tt>'
  162.  end
  163.  
  164. /* Now see if the ACTION is really an alias for some other action */
  165.  
  166. aline=wait_queue('ALIAS','++:'||asel)
  167. parse var aline istat newsel
  168. parse var istat istat ':' istatname
  169.  
  170. if istat>0 then do
  171.   call lineout tempfile,'<b> Alias match to entry</b> 'istat'=<tt>' istatname '</tt>'
  172.   CALL LINEOUT TEMPFILE,'<MENU><LI> Match to: <tt>'||strip(word(newsel,1))||'</tt>'
  173.   CALL LINEOUT TEMPFILE,'<LI> which resolves to: <tt>'||strip(word(newsel,2))'</ul>'
  174. end
  175. else do
  176.   call lineout tempfile,'<u> <b>No</b> alias match </u>'
  177. end
  178.  
  179.  
  180. action=asel
  181. foo1=pos('~',action)
  182. if foo1>0 then do            /* do ~ for home_dir replacement, with $ option */
  183.   action=sref_home_dir(action,home_dir)
  184.   action=strip(action,'l','/')
  185.   call lineout tempfile,' <br>~ replacement yields: <tt>'action '</tt>'
  186. end
  187.  
  188.  
  189. icgi=pos('CGI-BIN/',upper(asel))
  190. if icgi>0 then do
  191.   cgi_bin_dir=STRIP(get_value('CGI_BIN_DIR',0))
  192.   aselc=substr(asel,icgi+8)
  193.   afilenam=sref_do_virtual(cgi_bin_dir,aselc,enmadd,0,transaction,HOMEDIR,HOST_NICKNAME)
  194.   call lineout tempfile,'<p> <u> Mapping to virtual directory</u> (cgi-bin_dir default):' afilenam
  195.   afilenam=sref_do_virtual(cgi_bin_dir,aselc,enmadd,1,transaction,HOMEDIR,HOST_NICKNAME)
  196.   if afilenam=0 then   /* try adding .cmd */
  197.       afilenam=sref_do_virtual(cgi_bin_dir,aselc'.cmd',enmadd,1,transaction,HOMEDIR,HOST_NICKNAME)
  198.   if afilenam=0 then   /* try adding .cmd */
  199.       afilenam=sref_do_virtual(cgi_bin_dir,aselc'.'serverport,enmadd,1,transaction,HOMEDIR,HOST_NICKNAME)
  200.   if afilenam=0 then call lineout tempfile,'<em>(was not found) </em>'
  201. end /* do */
  202. else do
  203.  
  204.  
  205.   qpos=pos('?',asel0)>0 ;afilenam=1
  206.   if qpos>0 then do
  207.     addon_dir=get_value('ADDON_DIR',0)
  208.     afilenam=sref_do_virtual(addon_dir,asel,enmadd,0,transaction,HOMEDIR,HOST_NICKNAME)
  209.     call lineout tempfile,'<p> <u> Mapping to virtual directory</u> (addon_dir default):' afilenam
  210.     afilenam=sref_do_virtual(addon_dir,aselc,enmadd,1,transaction,HOMEDIR,HOST_NICKNAME)
  211.     if afilenam=0 then   /* try adding .cmd */
  212.       afilenam=sref_do_virtual(addon_dir,asel'.cmd',enmadd,1,transaction,HOMEDIR,HOST_NICKNAME)
  213.     if afilenam=0 then   /* try adding .cmd */
  214.       afilenam=sref_do_virtual(addon_dir,asel'.'serverport,enmadd,1,transaction,HOMEDIR,HOST_NICKNAME)
  215.     if afilenam=0 then call lineout tempfile,'<em>(was not found) </em>'
  216.   end
  217.  
  218.   if  \(afilenam<>1 & pos('.',asel0)=0) then do
  219.     afilenam=sref_do_virtual(ddir,asel,enmadd,0,transaction,HOMEDIR,HOST_NICKNAME)
  220.     call lineout tempfile,'<p> <u> Mapping to virtual directory</u> (datadir default):' afilenam
  221.     afilenam=sref_do_virtual(ddir,asel,enmadd,1,transaction,HOMEDIR,HOST_NICKNAME)
  222.     if afilenam=0 then call lineout tempfile,'<em>(was not found) </em>'
  223.   end
  224.  
  225.  
  226. end
  227.  
  228.  call lineout tempfile,'</body><html>'
  229.  call lineout tempfile
  230.  call sref_gos('FILE ERASE type text/html name ' tempfile)
  231.  return ' '
  232.  
  233.  
  234. /******************* END OF MAIN  **************************/
  235.  
  236. /* ------------------------------------------------------ */
  237. /* query and wait on helper threads */
  238. /* ------------------------------------------------------*/
  239. wait_queue: procedure expose os2e mysem myqueue enmadd basesem  ,
  240.           mytid verbose transaction no_virtual servername host_nickname 
  241.  
  242. parse arg athread,args,extra1
  243. athread=strip(athread) ; args=strip(args)
  244. /* check to see if no need to call queue */
  245.  
  246. again1t:
  247. goober=enmadd||athread
  248.   a=rxqueue('s',goober)
  249.   queue  transaction ' ' host_nickname ',' myqueue ',' mysem ','  ARGS
  250.   a=eventsem_reset(mysem)
  251.   dothread=basesem||athread
  252.  
  253.   a=eventsem_post(dothread)
  254. again2t:
  255.   a=eventsem_wait(mysem,90000)  /* max wait of 90 seconds */
  256.  
  257.   if a<>0 then do
  258.         ss=sref_error(mytid','athread' A Fatal Semaphore failure in checksel: 'a,1)
  259.         String 'Web server is temporarily busy '
  260.         exit ' '
  261.   end
  262.   a=rxqueue('s',myqueue)
  263.   parse pull aline
  264.   PARSE VAR ALINE idnum ',' aline
  265.   idnum=strip(translate(idnum,' ','000d0a'x));TRANSACTION=STRIP(TRANSACTION)
  266.   if idnum<>transaction then  do /*wierd error: got someone else's message! */
  267.       say ' Read odd id from queue 'athread ':' transaction ',' idnum
  268.       if eventsem_query(mysem)=1 then aa=eventsem_reset(mysem)
  269.      signal again2t
  270.   end
  271.  
  272.   aline=strip(aline)
  273.   return aline
  274.  
  275.  
  276.  
  277.  
  278.  
  279.  
  280. /* -----------------------------------------------------------------------*/
  281. /* see if ip address matches one of a set of good ips (1 if yes)*/
  282. /* check for numeric or names ,and check for wildcards */
  283. /* if non-default host, ONLY get "Host specific" inhouseips */
  284. /* -----------------------------------------------------------------------*/
  285. goodips: procedure expose inhouseips. privset1 enmadd os2e verbose  transaction servername host_nickname ,
  286.                    clientname0 mysem myqueue basesem who
  287.  
  288.  
  289. parse arg anips
  290.  
  291. anips=strip(anips) ;hostn=strip(host_nickname)
  292.  
  293. if symbol(inhouseips.0)<>"VAR" then do  /* not been read yet */
  294.    aa='inhouseips.' ;addit=''
  295.    if hostN<>"" then addit='.'||hostn
  296.    nin=0
  297.    do mm=1 to 10000
  298.       useme=enmadd||aa||mm||addit ; useme=strip(useme)
  299.       tt=strip(value(useme,,os2e))
  300.       if tt="" | tt=0 then leave
  301.       nin=nin+1 ; inhouseips.nin=upper(tt)
  302.    end
  303.    inhouseips.0=nin
  304. end
  305.  
  306. /* check exact matches -- perhaps name matches */
  307. mdo=inhouseips.0
  308. do mm=1 to mdo
  309.   if inhouseips.mm=' '  then iterate
  310.   if verify(word(inhouseips.mm,1),'*1234567890.')=0 then do  /*a numeric ip */
  311.      if strip(word(inhouseips.mm,1))=anips then do        /*it's an exact match -- extract privset */
  312.         parse var inhouseips.mm ff  privset1
  313.         return 1
  314.     end
  315.   end
  316.   else do                      /* a name ip */
  317.       if clientname0=0 then 
  318.             clientname0=sref_clientname(who,mysem,myqueue,basesem,enmadd,transaction)
  319.       if strip(word(inhouseips.mm,1))=clientname0 then do        /*it's an exact match -- extract privset */
  320.          parse var inhouseips.mm ff  privset1
  321.          return 1
  322.       end
  323.   end           /* name or number inhouseip */
  324. end
  325.  
  326. /* if here, try wild card matches */
  327. do m2=1 to mdo           /* wild card matches */
  328.     imm=sref_wild_Match(anips,inhouseips.m2)
  329.     if imm=0 & clientname0<>0 then 
  330.         imm=sref_wild_match(clientname0,inhouseips.m2)
  331.     if imm=0 then iterate
  332.     parse var inhouseips.m2 . privset1
  333.     return 1
  334. end
  335. return 0                /* no match */
  336.  
  337.  
  338.  
  339.  
  340. /* ----------- */
  341. /* get environment value, possibly host specific
  342. hname=0 -- do not look under hostname
  343. hname=1 -- do not look under default
  344.  */
  345. /* ------------ */
  346. get_value: procedure expose enmadd host_nickname
  347. parse upper arg vname,hname0
  348. if hname0=0 then
  349.         hname=' '
  350. else
  351.     hname=strip(host_nickname)
  352.  
  353. vname=strip(vname) ;
  354. if hname<>' ' then do
  355.    booger=strip(enmadd||vname||'.'||hname)
  356.    aval=value(booger,,'os2environment')
  357.    if aval<>' ' | hname0=1 Then
  358.         return aval
  359. end
  360. aval=value(enmadd||vname,,'os2environment')
  361. return aval
  362.  
  363.  
  364.  
  365.  
  366. /******/
  367. /* syntax error */
  368. goterr:
  369. IF RC=-7 THEN EXIT ' '  /* if just  a closed connection ,then don't worry */
  370. LL=SIGL
  371. glue='SRE-http error at line 'll '(rc= 'rc
  372.  
  373. if rxfuncquery('PMPRINTF')=1 then do
  374.     say glue
  375. end
  376. else do
  377.   if macroquery('sref_error')=' ' then
  378.     say glue
  379.   else
  380.     ss=sref_error(glue,1)
  381. end
  382. IF COMPLETED()=0 THEN
  383.    'var type text/plain name glue'
  384.  
  385.  
  386. exit  ' '
  387.  
  388.  
  389. /********************************/
  390. /* check on advanced options */
  391. get_adv_opts:
  392. if adv_opts<>" " then do
  393.       wd=get_value(workdata_dir)
  394.       foo=strip(wd,'t','\')||'\'||strip(adv_opts)
  395.       adv_opts_file=stream(foo,'c','query exists')
  396.       if adv_opts_file="" then do
  397.          adss= sref_error("Warning: missing advanced options file: " foo " ("asel,1)
  398.       end
  399.       else do
  400.          adv_opts=charin(adv_opts_file,1,chars(adv_opts_file))
  401.       end
  402. end
  403. return 1
  404.  
  405.  
  406.  
  407.  
  408. /*********************/
  409. /* get host,datadir, & servername for this requesete */
  410. get_host_info:procedure expose host_nickname ddir servername  asel enmadd myaddr
  411. parse arg protocol,asel
  412.  
  413.  
  414. stuff=sref_host_info(myaddr,enmadd,asel,protocol)
  415. if strip(stuff)='BAD' then return -1           /* if http/1.1 client did not include at host: header */
  416.  
  417. if stuff<>0 then do
  418.       parse var stuff  servername ',' host_nickname  ',' ddir
  419.       host_nickname=strip(host_nickname); ddir=strip(ddir)
  420.       servername=strip(servername)
  421. end
  422. else do
  423.     ddir=datadir()
  424.     servername=servername()
  425.     host_nickname=' '
  426. end
  427. if abbrev(upper(asel),'HTTP://')>0 then do
  428.           parse var uri . '//' . '/' uri
  429.           if uri='' then uri='/'
  430.           parse var asel . '//'  . '/' asel
  431. end        
  432. return 1
  433.  
  434. /*****************************************/
  435. /* add privileges from various sources */
  436. add_privs_2:
  437. privset=''
  438. /* add public_privs to everyone */
  439.  public_privs=get_value('public_privs')
  440.  privset=privset||' '||public_privs
  441.  
  442. /* add "username " to privset (this step may be redundant, but... ? */
  443. addname=get_value('add_user_name')
  444. if addname=1 then do
  445.   if wordpos(upper(username),upper(privset))=0 then
  446.        privset=privset||' '||username
  447. end
  448.  
  449. /* check for dynamic privileges ? */
  450. check_add_privs=get_value('CHECK_ADD_PRIVS')
  451. if check_add_privs=1 then do
  452.        aprefix=strip(get_value('ADD_PRIVS_PREFIX'))
  453.        ap2=sref_get_add_privs(host_nickname,myaddr,serverport,enmadd,tempdata_dir,verbose)
  454.        do iw=1 to words(ap2)
  455.           privset=privset||' '||aprefix||strip(word(ap2,iw))
  456.        end
  457.        privset=translate(privset,' ','&')  /* correct entry errors */
  458. end
  459.  
  460.  
  461.  
  462. return privset
  463.  
  464. /********************************/
  465. /* check on advanced options */
  466. get_adv_opts:
  467.       wd=get_value(workdata_dir)
  468.       foo=strip(wd,'t','\')||'\'||strip(adv_opts)
  469.       adv_opts_file=stream(foo,'c','query exists')
  470.       if adv_opts_file="" then return " No such file: "adv_opts
  471.       return charin(adv_opts_file,1,chars(adv_opts_file))
  472.  
  473.  
  474. /********************************/
  475. /* checks sreproxy cache */
  476. check_cache:procedure expose enmadd mysem myqueue transaction 
  477. parse arg hname,sel
  478.  
  479. if left(sel,1)='!' then do
  480.   if abbrev(upper(sel),'!SEND')<>1 then return 0  /* do NOT cache special codes */
  481. end
  482.  
  483. if hname<>'' then
  484.   eek=hname'//'sel
  485. else
  486.   eek=sel
  487. eek=upper(eek)
  488. goober='SREF_SREPROXY'
  489. a=rxqueue('s',goober)
  490. queue  transaction','myqueue','mysem',L,'EEK
  491. a=eventsem_reset(mysem)
  492. dothread='\SEM32\SREF_SREPROXY'
  493. a=eventsem_post(dothread)
  494.  
  495. a=eventsem_wait(mysem)
  496. if a<>0 then do
  497.    sss=sref_error(' A Fatal Semaphore failure in checksel: 'a,1)
  498.    return -1
  499. end
  500. a=rxqueue('s',myqueue)
  501.  
  502. parse pull aline
  503. PARSE VAR ALINE idnum ',' aline
  504.  
  505. idnum=strip(translate(idnum,' ','000d0a'x))
  506. if idnum<>transaction then  do /*wierd error: got someone else's message, give up */
  507.       say ' Read odd id from checksel :' idnum0 ',' idnum
  508.       return -1
  509. end
  510.  
  511. aline=strip(aline)
  512. return aline
  513.  
  514. /****************************/
  515. /* remove repeats, extract secret privileges.
  516.   This will modify privest, and create privset1_secret and privset12 */
  517. fix_privset1:
  518. p1=''
  519. secp1=''
  520. privset1=translate(translate(privset1,' ',','||'0d0a0900'x))
  521. do mm=1 to words(privset1)
  522.    aw1=strip(word(privset1,mm))
  523.    if abbrev(aw1,'?')=1 then do
  524.      aw1=substr(aw1,2)
  525.      if wordpos(aw1,secp1)=0 then secp1=secp1||aw1||' '
  526.    end
  527.    else do
  528.       if wordpos(aw1,p1)=0 then p1=p1||aw1||' '
  529.    end
  530. end
  531. privset1=p1
  532. privset1_secret=secp1
  533. return 1
  534.  
  535.  
  536.  
  537.  
  538.