home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 35 Internet / 35-Internet.zip / srev13g.zip / USERCHK.RXX < prev    next >
Text File  |  1999-06-19  |  14KB  |  486 lines

  1. /* Check users/password module for SRE-http. Uses contents of USERfile */
  2. /* Initializae with userfile,usequeue,usesem
  3.    call (via queues and semaphore) --  return q, return sem ,USERNAME PASSWORD
  4.    returns:  no match :  0
  5.                match:  N  priv_list
  6.         where N is the "# in list of match", and priv_list is space delimited
  7.  
  8.   Note: no longer supports "USERFILES.". Instead, read files from
  9.         the CFGLIST.CFG file (in the CFGS\ subdirectory)
  10.  
  11. */
  12.  
  13.  
  14. /******************* END OF USER CONFIGURABLE PARAMETERS ******************/
  15.  
  16. parse upper arg userfile,  usequeue , USESEM , max_semwait,logon_limit,sport
  17.  
  18. mytid=dostid()
  19. foo=pmprintf_sref(' SRE-http Users: thread, file, queue='||mytid', 'userfile', 'usequeue,,sport)
  20. foo=pmprintf_sref(' SRE-http Users: logon limit='||logon_limit,,sport)
  21.  
  22. resetit:
  23. if  usequeue="" | USESEM="" then do
  24.    call pmprintf('SRE-http Users ERROR: initialization ERROR: '||usequeue)
  25.    exit
  26. end
  27.  
  28. crlf='0d0a'x
  29.  
  30. logfailb=1
  31. logfaile=0
  32.  
  33. cfgs_dir=value('SRE_CFGS_DIR',,'os2environment')
  34. cfglist_file=cfgs_dir||'\CFGLIST.CFG'
  35. defrealm=value('SREF_DEFREALM_FILE_TEMP',,'os2environment')
  36.  
  37. call set_users(userfile)  /* see if anything in CFGLIST.CFG  */
  38.  
  39. foo=pmprintf_sref(' SRE-http Users:  #users='||nusers||', #wildcards= '||nwilds,,sport)
  40.  
  41. /* Initialization now done == start waiting for requests for user info */
  42. signal on error name iserror
  43. signal on syntax name iserror
  44. bakme:
  45.  a=rxqueue('s',usequeue)
  46.  aq=queued()
  47.  if aq=0 then do
  48.     WOW=EVENTSEM_WAIT(USESEM,max_semwait)
  49.     aq=-1
  50.     if wow=640 then do
  51.         signal bakme
  52.     end
  53.     IF WOW<>0 THEN do         /* FATAL ERROR */
  54.         call pmprintf(' Fatal semaphore error in User thread ')
  55.         EXIT
  56.     end
  57.  end
  58.  wow=EVENTSEM_RESET(usesem)
  59.  if aq=-1 then
  60.    if queued()=0 then signal bakme
  61.  
  62.  pull isit0
  63. goobs:
  64.  isit0=translate(isit0,' ','000d0a09'x)
  65.  
  66.  if isit0=" " then signal bakme
  67.      parse var isit0  idnum ',' newq ',' newsem ',' ISIT
  68.      parse var idnum idnum host_nickname
  69.      if newq="" | newsem="" then do
  70.         call pmprintf(' SRE-http Users: missing queue or semaphore ')
  71.         signal bakme
  72.      end
  73.      newq=strip(newq); newsem=strip(newsem) ;ISIT=STRIP(ISIT)
  74.  
  75.      if abbrev(strip(translate(isit)),'*LIST*') then  do
  76.         dog1=''
  77.         do mm=1 to names.0
  78.            raa=hostids.mm' // 'names.mm ',' pwds.mm ',' privs.mm
  79.            dog1=dog1||raa||crlf
  80.         end
  81.         do mm=1 to wildpwds.0
  82.            raa=wildhosts.mm' // * ,' wildpwds.mm ',' wildprivs.mm
  83.            dog1=dog1||raa||crlf
  84.         end
  85.         a=rxqueue('s',newq)
  86.         dog1=idnum','dog1
  87.         push  dog1
  88.         wow=eventsem_post(newsem)
  89.         signal bakme
  90.     end
  91.  
  92.     if abbrev(strip(translate(isit)),'*DIE*') then 
  93.          exit
  94.  
  95.     if abbrev(strip(translate(isit)),'*RESET*') then do
  96.        parse var isit foo newfile .
  97.        if newfile<>' ' then userfile=newfile
  98.        call set_users(userfile)
  99.      foo=pmprintf_sref(' SRE-http Users:  reset:  #users='||nusers||', #wildcards= '||nwilds,,sport)
  100.    end
  101.    else do
  102.       dog1=fig_User(isit,host_nickname)
  103.       a=rxqueue('s',newq)
  104.       dog1=idnum','dog1
  105.       push  dog1
  106.       wow=eventsem_post(newsem)
  107.    end
  108.  
  109. signal bakme
  110.  
  111. iserror:
  112. signal off error ; signal off syntax
  113. call pmprintf('SRE-http: error in Users thread ')
  114. a=rxqueue('d',usequeue)
  115. a=rxqueue('c',usequeue)
  116. a=eventsem_close(usesem)
  117. a=eventsem_create(usesem)
  118. a=rxqueue('s',newq)
  119. push idnum  ', 0 '
  120. wow=eventsem_post(newsem)
  121. call pmprintf('SRE-http: done resetting Users thread ')
  122. signal on error name iserror
  123. signal on syntax name iserror
  124. signal bakme
  125.  
  126. exit
  127.  
  128.  
  129. /* ---------- */
  130. fig_User:procedure expose names. pwds. orig. privs. wildorig. wildpwds. wildprivs. ,
  131.                 hostids. wildhosts. logfailb logfaile logfail. logon_limit crlf
  132.   parse arg inline, host_nickname
  133.   host_nickname=upper(strip(host_nickname))
  134.   acum=0
  135.  
  136.  
  137.   inline=translate(inline,' ','000d0a09'x)
  138.   parse upper var inline atype username password who .
  139.  
  140.   who=strip(who)
  141.  
  142. /* got a request -- look for a match */
  143.  
  144.  issec=time('s')
  145.  
  146. /* see if logon_limit has been exceeded for this ip address */
  147. if logon_limit>0 & logfailb <= logfaile & who<>' ' then do
  148.   if issec<logfail.logfaile.istime then do  /* reset at midnite */
  149.        drop logfail.
  150.        logfailb=0; logfaile=1
  151.   end
  152.   newb=0
  153.   do ikk=logfailb to logfaile
  154.       if logfail.ikk.istime < (issec-60) then do
  155.            newb=ikk ; iterate
  156.      end
  157.       if logfail.ikk.isip=who then acum=acum+1
  158.       if acum>logon_limit then leave
  159.   end
  160.   if newb>0 then do
  161.         do oo=logfailb to newb
  162.              drop logfail.oo.
  163.         end
  164.         logfailb=newb+1
  165.   end
  166.   if acum>logon_Limit then do
  167.       return -(acum+1)
  168.    end
  169. end
  170.  
  171. if atype='DIGEST' & username=' ' then return -acum
  172. if atype='BASIC' & (username=' ' | password=' ') then return -acum
  173.  
  174. oo=arraysearch('names','reslines',username,'X')
  175.  
  176. /* probably,there will be only 1 match. But in case there isn't,
  177.    check out several possible passwords. OR, if no password,
  178.    return the first password (along with the privset) */
  179.  ok=0
  180.  thematch=0
  181.  do mm=1 to oo          /* check the username matches */
  182.      m1=reslines.mm
  183.      if hostids.m1<>host_nickname & hostids.m1<>"" then
  184.         iterate
  185.  
  186.      if m1=" " then iterate
  187.      if password=UPPER(pwds.m1) | atype='DIGEST' then do   /* name and password match ! */
  188.           ok=m1                                            /* or let DIGEST do it */
  189.           if hostids.m1<>"" then leave /* if not, allow host specific to overwrite */
  190.      end
  191.      if pwds.m1='*' & ok=0 then do  /*note if it's a wild card match (first one only */
  192.         ok=-m1
  193.      end
  194.  end
  195. /* if ok<>0, then got a match. So return results */
  196.   if ok <> 0 then do     /* ok<0 means "wildcarded password */
  197.      ok=abs(ok)
  198.      if atype='DIGEST' then
  199.          thematch=pwds.ok
  200.      else
  201.          thematch=orig.ok
  202.      theprivs=privs.ok
  203.    end
  204.  
  205. /* no exact match -- try wildcard list */
  206.   if ok=0 then do
  207.     do mm=1 to wildpwds.0
  208.  
  209.       if wildhosts.mm<>host_nickname & wildhosts.mm<>"" then
  210.          iterate
  211.  
  212.       if password=wildpwds.mm | wildpwds.mm='*' then do   /* name and password match ! */
  213.           theprivs=wildprivs.mm
  214.           thematch=wildorig.mm
  215.           if wildhosts.mm<>"" then leave /* if not, allow host specific to overwrite */
  216.       END
  217.      end
  218.   end
  219.  
  220.   if thematch=0 then do
  221.      amess=-(acum+1)
  222.      if logon_limit>0 & who<>' ' then do
  223.         logfaile=logfaile+1
  224.         logfail.logfaile.isip=who
  225.         logfail.logfaile.istime=issec
  226.      end
  227.   end
  228.   else do
  229.      amess=thematch||' '||theprivs
  230.   end
  231.  
  232.   amess=translate(amess,' ','000d0a09'x)
  233.   return amess
  234.  
  235. /* ---------------- */
  236. /* read and set up users/pwds -- from generic list, and from cfglist.cfg list */
  237. set_users:
  238. parse arg afile
  239. nwilds=0 ; nusers=0 ; got1=0
  240. names.0=0 ; wildpwds.0=0
  241.  
  242. a=sref_fileread(afile,'ulist',,'E')   /* read it to a stem variable */
  243.  
  244. if a=0 then do
  245.       call pmprintf(' SRE-http Users ERROR: ERROR reading user-file: '||afile)
  246.       return 0
  247. end
  248.  
  249. foo=do_extends(1)
  250.  
  251. if ulist.0>0 then do            /* convert tabs to spaces */
  252.   do ir=1 to ulist.0
  253.     ulist.ir=translate(ulist.ir,' ','0009'x)
  254.   end
  255. end
  256. foo=add_cfglist('USERS','USERS.IN')          /*augment the ulist stem variable, using CFGLIST.CFG files */
  257.  
  258. foo=add_defrealm('USER')
  259.  
  260.  
  261. MKK=0
  262. DO KK=1 TO ULIST.0
  263.    AAA=STRIP(ULIST.KK)
  264.    IF ABBREV(AAA,';')=1 | AAA='' THEN ITERATE
  265.    MKK=MKK+1
  266.    ULIST.MKK=AAA
  267. END
  268. ULIST.0=MKK
  269. ause2=arraysort('ulist',,,,,,'I')
  270.  
  271. do zmm=1 to ulist.0
  272.  
  273.      t1=strip(ulist.zmm)
  274.  
  275.      if t1=" " then iterate
  276.      if abbrev(t1,';')=1 then iterate
  277.      foo=upper(strip(word(t1,1)))
  278.      ahost=' '
  279.      if right(foo,2)='//' then do
  280.          ahost=upper(left(foo,length(foo)-2))
  281.          parse var t1 ffo aname  apwd aprivs
  282.          parse UPPER var t1 ffo aname  aFOO aprivs
  283.      end
  284.      else DO
  285.          parse var t1 aname  apwd aprivs
  286.          parse upper var t1 aname  aFOO aprivs
  287.      END
  288.  
  289.      got1=got1+1
  290.      if aname='*' then do
  291.            nwilds=nwilds+1
  292.            wildpwds.nwilds=strip(apwd) ; wildprivs.nwilds=strip(aprivs)
  293.            wildorig.nwilds=got1
  294.            wildhosts.nwilds=ahost
  295.      end
  296.      else do
  297.           nusers=nusers+1
  298.           names.nusers=strip(aname)
  299.           pwds.nusers=strip(apwd) ; privs.nusers=strip(aprivs)
  300.           orig.nusers=got1
  301.           hostids.nusers=ahost
  302.      end
  303.  end 
  304.  names.0=nusers 
  305.  wildpwds.0=nwilds
  306.  return 0
  307.  
  308. /************/
  309. /* Redo ulist, by treating lines starting with , as continuation lines */
  310. do_extends:procedure expose ulist. crlf
  311.  
  312. if ulist.0=0 then return
  313. isnew=1
  314. tmps.1=ulist.1
  315. do mm=2 to ulist.0
  316.    ali=strip(ulist.mm)
  317.    if abbrev(ali,',')=0 then do
  318.         isnew=isnew+1
  319.         tmps.isnew=ulist.mm
  320.     end
  321.     else do
  322.         tmps.isnew=tmps.isnew||substr(ali,2)
  323.     end
  324. end
  325. do mm=1 to isnew
  326.     ulist.mm=tmps.mm
  327. end
  328. ulist.0=isnew
  329. return 0
  330.  
  331.  
  332.  
  333. /**********************************/
  334. /* Add entries to ULIST. variable, from the  ATYPE files listed in  the CFGLIST.CFG file
  335.    (but only for port SPORT) 
  336. */
  337.  
  338. add_cfglist:procedure expose ulist. cfglist_file sport cfgs_dir crlf
  339. parse upper arg atype,defname
  340. atype=strip(atype)
  341.  
  342. if cfglist_file='' then return /* nothing to do */
  343.  
  344. /* look for files */
  345. foo=translate(stream(cfglist_file,'c','open read'))
  346. if abbrev(foo,'READY')<>1 then do
  347.   call pmprintf('SRE-http: ' atype ' Warning: Unable to open cfglist.cfg ')
  348.   return 0            /* unable to open */
  349. end
  350. inj=stream(cfglist_file,'c','query size')
  351.  
  352. if inj=0 | inj='' then return   0        /* empty file */
  353. astuff=charin(cfglist_file,1,inj)
  354. foo=stream(cfglist_file,'c','close')
  355.  
  356. astuff=astuff||crlf||' '        /* place an elephant in cairo */
  357.  
  358. /* determine which files apply to this atype and port, by readling CFGLIST.CFG */
  359. mm=0
  360.  
  361. /* note: file is organized in blocks */
  362. curport=80              /* defaults port and host */
  363. curhost=''
  364. curfile=''
  365.  
  366. do forever              /* for all blocks in file */
  367.   if astuff='' then leave       /* all done  (note we always put an elephant in cairo */
  368.   parse var astuff aline (crlf) astuff
  369.  
  370.   if abbrev(aline,';')=1 then iterate   /* ignore comments */
  371.   if aline='' then do           /* block end */
  372.      if curport<>sport | curfile='' then do     /* different port, or this atype file not specified */ 
  373.         nop 
  374.      end
  375.      else do        /* otherwise, add this entry to filelist */
  376.         mm=mm+1
  377.         ufiles.mm.!host=curhost
  378.         ufiles.mm.!file=curfile
  379.      end
  380.      curport=80 ; curhost='' ; curfile=''  /* clear block */
  381.      iterate
  382.   end
  383.  
  384. /* process an entry in this block */
  385.   parse upper var aline ltype ':' lstuff ;ltype=strip(ltype) ; lstuff=strip(lstuff)
  386.   select
  387.     when ltype='PORT' then curport=lstuff
  388.     when ltype='HOST' then curhost=lstuff
  389.     when abbrev(ltype,atype)=1  then do
  390.        curfile=strip(translate(lstuff,'\','/'),'l','\')
  391.        if pos(':',curfile)=0 then curfile=cfgs_dir||'\'||curfile
  392.     end
  393.     when ltype='*'  then do
  394.        curfile=strip(translate(lstuff,'\','/'),,'\')
  395.        if pos(':',curfile)=0 then curfile=cfgs_dir||'\'||curfile
  396.        curfile=stream(curfile'\'defname,'c','query exists')
  397.     end
  398.  
  399.     otherwise nop
  400.   end
  401.  
  402. end
  403.  
  404. /* done reading cfglist; add entries from appropriate files */
  405. if mm=0 then return 1     /* no auxillary files of this type */
  406. ufiles.0=mm
  407.  
  408. do mm=1 to ulist.0              /* retain old ulist */
  409.   ulisttmp.mm=ulist.mm
  410. end
  411. ulisttmp.0=ulist.0
  412.  
  413.  
  414. do mm=1 to ufiles.0
  415.   afile=ufiles.mm.!file
  416.   ahost=ufiles.mm.!host
  417.  
  418.   a=sref_fileread(afile,'ulist',,'E')   /* read it to a stem variable */
  419.  
  420.   if a=0 | ulist.0=0 then do
  421.       call pmprintf(' SRE-http 'atype': WARNING**: bad auxillary file: '||afile)
  422.       iterate
  423.   end
  424.    
  425.   foo=do_extends(1)             /* fixup ulist. */
  426.    call pmprintf('SRE-http 'atype': adding from 'AHOST ' specific file 'afile ',' ulist.0)
  427.  
  428.    do ii=1 to ulist.0
  429.         aline=strip(ulist.ii)
  430.         if ALINE=' ' then iterate
  431.         aline=translate(aline,' ','0009'x)
  432.         if  abbrev(strip(aline),';')=1 then iterate
  433.         parse var aline aw1 .
  434.         if pos('//',aw1)>0 then do
  435.             call pmprintf('SRE-http: 'atype 'warning: disallowed auxillary entry: 'aline)
  436.             iterate
  437.         end
  438.         if ahost<>'' then aline=ahost||'// '||aline
  439.         ii2=ulisttmp.0+1
  440.         ulisttmp.ii2=aline
  441.         ulisttmp.0=ii2
  442.    end
  443. end
  444.  
  445. do mm=1 to ulisttmp.0
  446.    ulist.mm=ulisttmp.mm
  447. end
  448. ulist.0=ulisttmp.0
  449. drop ulisttmp.
  450. return 1
  451.  
  452.  
  453. /**********************************/
  454. /* Add entries to ulist. variable, from the defrealm_temp file.
  455.   Note that this file only has the appropriate port entries
  456.   in it */
  457. add_defrealm:procedure expose defrealm ulist.  crlf
  458.  
  459. parse upper arg atype
  460. atype=strip(atype)
  461.  
  462. /*call pmprintf(' xxx 'atype' 'defrealm) */
  463. if defrealm='' then return 0
  464. aa=sref_open_read(defrealm,15,'READ')
  465. if aa<=0 then return 0
  466.  
  467. ii=stream(defrealm,'c','query size')
  468. if ii=0 | ii='' then return 0
  469.  
  470. stuff=charin(defrealm,1,ii)
  471. foo=stream(defrealm,'c','close')
  472.  
  473. do forever
  474.   if stuff="" then return 1
  475.   parse var stuff aline (crlf) stuff
  476.   if abbrev(aline,';')=1 then iterate
  477.   parse var aline btype ':' aline
  478.   if btype<>atype then iterate
  479.   ii=ulist.0+1
  480.   ulist.ii=aline
  481.   ulist.0=ii
  482. end
  483.  
  484.  
  485.   
  486.