/* GM_ReadConfFile */
/* start Conf File specific header */
version = 'ReadConfFile 2.35'
revdate = '(13/08/97)'
config_file = 'UULib:pop3.conf'
logfile = 'pop3.log'
/* end Conf File specific header */

/* start Conf File specific body */

conf_file=arg(1)
log_file=arg(2)

retval = GM_ReadConfFile(conf_file' 'log_file)
If retval~=0 Then
Do
  Call Writeln('ERR Unable to read config file, closing connection')
  Return 5
End 

/* end Conf File specific body */

Exit 0
/* start common conf file */

Log: Procedure
  line=arg(1)
  tcplog=arg(2)

  logfile=GetClip('GM_logfile')
  version=GetClip('GM_version')

  log_type = Upper(Word(line,1))
  log_msg = Right(line,Length(line)-Length(Word(line,1)))
  If tcplog ~= '' Then
  Do
    progname=word(version,1)
    /* should hopefully stop errors when log message includes a " */
    log_msg=Compress(log_msg,'"')
    Address Command tcplog log_type '"'||progname||': '||log_msg||'"'
  End

  If logfile = 'LOGFILE' then logfile = 'T:smtp-mail.log'
  If Exists(logfile) Then
    OType = 'A'
  Else
    OType = 'W'
  Do While ~Open(Lock,'T:GM_Lock','W')
    Call Delay(Random(90,110))
  End
  If Open(log,logfile,OType) Then Do
    Call Writeln(log,date() time()||'[ '||log_type||']: '||log_msg)
    Call Close(log)
  End
  Call Close(Lock)
  Call Delete('T:GM_Lock')
Return 0

ReadConfFile: Procedure Expose LT
  conf_file=arg(1)
  log_file=arg(2)

  /*
  Address Command 'GM_ReadConfFile 'conf_file' 'log_file
  retal = RC
  */
  retval = GM_ReadConfFile(conf_file' 'log_file)
  If retval~=0 Then
  Do
    Call Writech(stdout,'421 Daemon program error, closing connection'LT)
    Call Log('ERR Unable to read config file, closing connection','TCPLOG')
    Return 5
  End
Return 0

/* ReadConfFile.rexx */
GM_ReadConfFile:

  parse arg conf_file ' ' logfile

  If Upper(conf_file) = 'CLEANUP' Then
    Exit 5
  Else If Upper(conf_file) = 'CLEANUP' Then
  Do
    Call SetClip('GM_config')
    Call SetClip('GM_version')
    Call SetClip('GM_disable_req')
    Call SetClip('GM_provider')
    Call SetClip('GM_popserver')
    Call SetClip('GM_nodename')
    Call SetClip('GM_username')
    Call SetClip('GM_popuser')
    Call SetClip('GM_poppass')
    Call Setclip('GM_logfile')
    Call Setclip('GM_topsize')
    Call Setclip('GM_leavesize')
    Call Setclip('GM_delivermail')
    Call Setclip('GM_demonenv')
    Call Setclip('GM_noshriek')
    Call Setclip('GM_debug')
    Call Setclip('GM_allowscore')
    Call Setclip('GM_killscore')
    Call Setclip('GM_userscore')
    Call Setclip('GM_hostscore')
    s = Getclip('GM_s')
    If s ~= '' Then
    Do i = 1 To s
      Call Setclip('GM_score.'||i)
      Call Setclip('GM_field_name.'||i)
      Call Setclip('GM_field_body.'||i)
    End
    Call Setclip('GM_s')
    x = Getclip('GM_x')
    If x ~= '' Then
    Do i = 1 To x
      Call Setclip('GM_user.'||i)
    End
    Call Setclip('GM_x')
    Call SetClip('GM_killfile')
    y = Getclip('GM_y')
    If y ~= '' Then
    Do i = 1 To y
      Call Setclip('GM_killaddr.'||i)
    End
    Call SetClip('GM_allowfile')
    z = Getclip('GM_z')
    If z ~= '' Then
    Do i = 1 To z
      Call Setclip('GM_allowaddr.'||i)
    End
    Call Setclip('GM_allowany')
    Call SetClip('GM_anyuser')
    Call SetClip('GM_destination')
    Call SetClip('GM_timezone')
    Call SetClip('GM_request')
    Call SetClip('GM_reqvar')
    Exit 10
  End
  Else
    Call SetClip('GM_config','Reading')

  libs = Show('L')
  If Pos('rexxsupport.library',libs) = 0 Then
    Address Command RexxLib

  libs = Show('L')
  If Pos('rexxreqtools.library',libs) = 0 Then Do
    Call Log('ERR Unable to load rexxreqtools.library, requester disabled','TCPLOG')
    Call SetClip('GM_disable_req',1)
  End 
  Else
    Call SetClip('GM_disable_req',0)

  destination = GetVar('HOSTNAME')
  If destination = '' Then
  Do
    Call Writech(stdout,'421 Daemon program error, closing connection'LT)
    Call Log('ERR Env:Hostname not set, closing connection','TCPLOG')
    Call SetClip('GM_config',0)
    Exit 5
  End
  
  timezone = GetVar('TIMEZONE')
  If timezone = '' Then
  Do
    timezone = +0000
    Call Log('NOTICE Cannot find Env:Timezone','TCPLOG')
  End
  
  anyuser = GetVar('Env:GM_ANYUSER')
  If anyuser ~= '' Then
  Do
    If Upper(anyuser) = 'YES' Then
      any_user = 1
  End
  
  requester_on = 0
  reqvar_exists=0
  reqvar = GetVar('Env:GM_REQUEST')
  If reqvar ~= '' Then
  Do
    reqvar_exists=1
    If Upper(reqvar) = 'YES' Then 
      requester_on = 1
  End

  Call SetClip('GM_provider',GetVar('PROVIDER'))
  Call SetClip('GM_popserver',GetVar('POPSERVER'))
  Call SetClip('GM_nodename',GetVar('NODENAME'))
  Call SetClip('GM_username',GetVar('USERNAME'))
  Call SetClip('GM_popuser',GetVar('POPUSER'))
  Call SetClip('GM_poppass',GetVar('POPPASS'))

  allow_any = 0 
  var_type = ''
  s = 0
  x = 0
  y = 0
  z = 0
  If ~Open(conf,conf_file,'r') Then Do
    Call SetClip('GM_config',0)
    Exit 10
  End
  Else Do
    Do While ~EOF(conf)
      line = Readln(conf)
      line=Strip(line)
      If line ~= '' Then Do
        If Left(line,1) ~= '#' Then Do
          Select
            When var_type = misc Then Do
              Select
                When Upper(Word(line,1)) = 'LOGDIR' Then Do
                  Parse var line . '=' logdir
                  logdir = PathPart(Strip(logdir))
                  logfile = AddPart(logdir,logfile)
                  Call Setclip('GM_logfile',logfile)
                End
                When Upper(Word(line,1)) = 'TOPSIZE' Then Do
                  Parse var line . '=' topsize
                  Call Setclip('GM_topsize',Strip(topsize))
                End
                When Upper(Word(line,1)) = 'LEAVESIZE' Then Do
                  Parse var line . '=' leavesize
                  Call Setclip('GM_leavesize',Strip(leavesize))
                End
                When Upper(Word(line,1)) = 'DELIVERMAIL' Then Do
                  Parse var line . '=' delivermail
                  Call Setclip('GM_delivermail',Strip(delivermail))
                End
                When Upper(Word(line,1)) = 'REALDELETE' Then Do
                  Parse var line . '=' realdelete
                  Call Setclip('GM_realdelete',Upper(Strip(realdelete)))
                End
                When Upper(Word(line,1)) = 'DEMONENV' Then Do
                  Parse var line . '=' demonenv
                  Call Setclip('GM_demonenv',Upper(Strip(demonenv)))
                End
                When Upper(Word(line,1)) = 'DELIVERENV' Then Do
                  Parse var line . '=' deliverenv
                  Call Setclip('GM_deliverenv',Upper(Strip(deliverenv)))
                End
                When Upper(Word(line,1)) = 'NOSHRIEK' Then Do
                  Parse var line . '=' noshriek
                  Call Setclip('GM_noshriek',Upper(Strip(noshriek)))
                End
                When Upper(Word(line,1)) = 'DEBUG' Then Do
                  Parse var line . '=' debug
                  Call Setclip('GM_debug',Upper(Strip(debug)))
                End
                When Upper(Word(line,1)) = 'ALLOWSCORE' Then Do
                  Parse var line . '=' allowscore
                  Call Setclip('GM_allowscore',Strip(allowscore))
                End
                When Upper(Word(line,1)) = 'KILLSCORE' Then Do
                  Parse var line . '=' killscore
                  Call Setclip('GM_killscore',Strip(killscore))
                End
                When Upper(Word(line,1)) = 'USERSCORE' Then Do
                  Parse var line . '=' userscore
                  Call Setclip('GM_userscore',Strip(userscore))
                End
                When Upper(Word(line,1)) = 'HOSTSCORE' Then Do
                  Parse var line . '=' hostscore
                  Call Setclip('GM_hostscore',Strip(hostscore))
                End
                When Upper(Word(line,1)) = 'SCOREFILE' Then Do
                  Parse var line . '=' scorefile
                  scorefile = Strip(scorefile)
                  If Open(score_file,scorefile,'r') Then
                  Do
                    Do While ~EOF(score_file)
                      score_line = Readln(score_file)
                      If score_line ~= '' Then Do
                        s = s + 1
                        score=Word(score_line,1)
                        field_name=ParsePattern(Word(score_line,2),NOCASE)
                        field_body=ParsePattern(Strip(Substr(score_line,Length(score) + 1 + Length(field_name) + 1 + 1)),NOCASE)
                        Call Setclip('GM_score.'||s,score)
                        Call Setclip('GM_field_name.'||s,field_name)
                        Call Setclip('GM_field_body.'||s,field_body)
                      End
                    End
                    Call Close(score_file)
                  End
                End
                Otherwise Do
                 /* nowt */
                End
              End
            End
            When var_type = users Then Do
              Select
                When Upper(Word(line,1)) = 'USERFILE' Then Do
                  Parse var line . '=' userfile
                  userfile = Strip(userfile)
                  If Open(user_file,userfile,'r') Then
                  Do
                    Do While ~EOF(user_file)
                      user_line = Readln(user_file)
                      If user_line ~= '' Then Do
                        If passwdfile = 1 Then Do
                          user_end=Pos('|',user_line)
                          user_line = Left(user_line,user_end-1)
                        End
                        x = x + 1
                        user = user_line
                        Call Setclip('GM_user.'||x,user)
                        risk = Verify(user,'`|$*<>#?','M')
                        If risk >0 Then Do
                          badchar = Substr(user,risk,1)
                        End
                      End
                    End
                    Call Close(user_file)
                  End
                End
                When Upper(Word(line,1)) = 'PASSFILE' Then Do
                  Parse var line . '=' passfile
                  passfile = Strip(passfile)
                  If Open(pass_file,passfile,'r') Then
                  Do
                    Do While ~EOF(pass_file)
                      pass_line = Readln(pass_file)
                      If pass_line ~= '' Then Do
                        user_end=Pos('|',pass_line)
                        If user_end = 0 Then
                          user_end=Pos(',',pass_line)
                        If user_end = 0 Then
                          user_end = Length(line)+1
                        user = Left(pass_line,user_end-1)
                        x = x + 1
                        Call Setclip('GM_user.'||x,user)
                        risk = Verify(user,'`|$*<>#?','M')
                        If risk >0 Then Do
                          badchar = Substr(user,risk,1)
                        End
                      End
                    End
                    Call Close(pass_file)
                  End
                End
                When Upper(Word(line,1)) = 'ANY' Then
                  any_user = 1
                Otherwise Do
                  x = x + 1
                  user = line
                  Call Setclip('GM_user.'||x,user)
                  risk = Verify(user,'`|$*<>#?','M')
                  If risk >0 Then Do
                    badchar = Substr(user,risk,1)
                  End
                End
              End
            End
            When var_type = kill Then Do
              Select
                When Upper(Word(line,1)) = 'KILLFILE' Then Do
                  Parse var line . '=' killfile
                  killfile = Strip(killfile)
                  If Open(kill_file,killfile,'r') Then
                  Do
                    Call SetClip('GM_killfile',killfile)
                    Do While ~EOF(kill_file)
                      kill_line = Readln(kill_file)
                      kill_line=Strip(kill_line)
                      If kill_line ~= '' Then Do
                        If Left(kill_line,1) = '<' Then
                          kill_line = Right(kill_line,Length(kill_line)-1)
                        If Right(kill_line,1) = '>' Then
                          kill_line = Left(kill_line,Length(kill_line)-1)
                        y = y + 1
                        kill_addr = ParsePattern(kill_line,'NOCASE')   
                        Call Setclip('GM_killaddr.'||y,kill_addr)
                      End
                    End
                    Call Close(kill_file)
                  End
                End
                Otherwise Do
                  y = y + 1
                  kill_addr = ParsePattern(line,'NOCASE')   
                  Call Setclip('GM_killaddr.'||y,kill_addr)
                End
              End
            End
            When var_type = allow Then Do
              Select
                When Upper(Word(line,1)) = 'ALLOWFILE' Then Do
                  Parse var line . '=' allowfile
                  allowfile = Strip(allowfile)
                  If Open(allow_file,allowfile,'r') Then
                  Do
                    Call SetClip('GM_allowfile',allowfile)
                    Do While ~EOF(allow_file)
                      allow_line = Readln(allow_file)
                      allow_line = Strip(allow_line)
                      If allow_line ~= '' Then Do
                        If Left(allow_line,1) = '<' Then
                          allow_line = Right(allow_line,Length(allow_line)-1)
                        If Right(allow_line,1) = '>' Then
                          allow_line = Left(allow_line,Length(allow_line)-1)
                        z = z + 1
                        allow_addr = ParsePattern(Strip(allow_line),'NOCASE')
                        Call Setclip('GM_allowaddr.'||z,allow_addr)
                      End
                    End
                    Call Close(allow_file)
                  End
                End
                When Upper(Word(line,1)) = 'ANY' Then
                  allow_any = 1
                Otherwise Do
                  z = z + 1
                  allow_addr = ParsePattern(line,'NOCASE')
                  Call Setclip('GM_allowaddr.'||z,allow_addr)
                End
              End
            End
            Otherwise Do
              /* nowt */
            End
          End
        End
        Else Do
          If Words(line) >=2 Then Do
            word2=Word(line,2)
            Select
              When Upper(word2) = 'LOG' Then Do
                var_type = misc
              End
              When Upper(word2) = 'USERS' Then Do
                var_type = users
              End
              When Upper(word2) = 'KILL' Then Do
                var_type = kill
              End
              When Upper(word2) = 'ALLOWED' Then Do
                var_type = allow
              End
              Otherwise Do
                /* nowt */
              End
            End
          End
        End
      End
    End
    Call Close(conf)
    Call Setclip('GM_x',x)
    Call Setclip('GM_y',y)
    Call Setclip('GM_z',z)
    Call Setclip('GM_s',s)
    Call Setclip('GM_allowany',allow_any)
    Call SetClip('GM_anyuser',any_user)
    Call SetClip('GM_destination',destination)
    Call SetClip('GM_timezone',timezone)
    Call SetClip('GM_request',requester_on)
    Call SetClip('GM_reqvar',reqvar_exists)

    Call Setclip('GM_config',Statef(conf_file))
  End
Return 0
Exit 0

/* end common conf file */

