	SUBROUTINE PHONE_AUTHORIZE
C
C	This routine is used to determine if a user is authorized to use
C	the autodial modem for local and/or long distance calling.  The
C	logical name VAXNET$PHONE_AUTH is translated in the group & system
C	logical name tables to find the authorization file.  If the logical
C	is found, the user authorization is performed.
C
C	Inputs:
C		None.
C
C	Outputs:
C		Returns if user is authorized, otherwise we exit.
C
	INCLUDE 'COM.INC/NOLIST'
	INCLUDE 'TRNLNM.INC/NOLIST'

	CHARACTER*(*) LOG_NAME
	CHARACTER*128 FILE_NAME
	PARAMETER (LOG_NAME = 'VAXNET$PHONE_AUTH')
	LOGICAL TRNLNM, AUTHORIZE_USER
	LOGICAL DO_PROCESS, DO_GROUP, DO_SYSTEM
	INTEGER FILE_SIZE
	DATA DO_GROUP, DO_SYSTEM /.TRUE.,.TRUE./

	IF (DO_GROUP) THEN
	  IF (TRNLNM (LOG_NAME, FILE_NAME, FILE_SIZE, TRN$M_GROUP)) THEN
	    DO_GROUP = AUTHORIZE_USER (FILE_NAME(1:FILE_SIZE))
	  ENDIF
	ENDIF

	IF (DO_SYSTEM) THEN
	  IF (TRNLNM (LOG_NAME, FILE_NAME, FILE_SIZE, TRN$M_SYSTEM)) THEN
	    DO_SYSTEM = AUTHORIZE_USER (FILE_NAME(1:FILE_SIZE))
	  ENDIF
	ENDIF
	RETURN
	END

	LOGICAL FUNCTION AUTHORIZE_USER (FILE_NAME)
C
C	This routine is used to search the phone authoriztion file to
C	determine if the current user is authorized to use the autodial
C	modem.  If the user is authorized then we return, otherwise we
C	let the user know they're not authorized and then exit.
C
C	Implicit Inputs:
C		FILE_NAME = The file name to open.
C
C	Explicit Inputs:
C		USER_NAME    = The users' login name.
C		PHONE_NUMBER = The phone number to dial.
C
C	Outputs:
C		Returns .TRUE./.FALSE. = File Success/Failure.
C
	INCLUDE 'COM.INC/NOLIST'
	INCLUDE 'TRNLNM.INC/NOLIST'

	CHARACTER*(*) FILE_NAME
	CHARACTER*(*) LOG_DIGITS, MODULE_NAME
	CHARACTER*(*) TAB_CH, SPACE_CH
	CHARACTER*(*) LONG_AUTH, NO_AUTH

	LOGICAL TRNLNM			! Translate a logical name.
	INTEGER CVT_DTB			! Convert decimal to binary.

	PARAMETER (TAB_CH = CHAR(9))		! Tab character.
	PARAMETER (SPACE_CH = CHAR(32))		! Space character.
	PARAMETER (MODULE_NAME = 'AUTHORIZE_USER')
	PARAMETER (LOG_DIGITS = 'VAXNET$LOCAL_DIGITS')
	PARAMETER (LONG_AUTH = SS//
	1 '*** Sorry, you are not authorized to make long distance calls.'
	2 //' ***'//BELL//DS)
	PARAMETER (NO_AUTH = SS//
	1 '*** Sorry, you are not authorized to use the autodial modems.'
	2 //' ***'//BELL//DS)

	INTEGER I			! Used for loops below.
	INTEGER EU			! End of the user name.
	INTEGER REU			! End of record useer name.
	INTEGER DIGITS			! Digits in the phone number.
	LOGICAL FILE_OPEN		! Authorization file is open.
	INTEGER LOCAL_DIGITS		! Local call number of digits.
	INTEGER SIZE			! The size of the record read.
	DATA LOCAL_DIGITS /8/		! Allow for "pbx-nnx-line".

	AUTHORIZE_USER = .TRUE.		! Presume file success.
C
C	The logical name VAXNET$LOCAL_DIGITS can be defined to specify the
C	number of digits to used for local phone calls.  This number is used
C	to determine when a long distance phone call is being attempted.
C
	IF (TRNLNM (LOG_DIGITS, SCRATCH, SIZE, TRN$M_SYSTEM)) THEN
	    IF (.NOT. CVT_DTB (SCRATCH(1:SIZE), LOCAL_DIGITS)) GOTO 9000
	ELSE
	    IF (TRNLNM (LOG_DIGITS, SCRATCH, SIZE, TRN$M_GROUP)) THEN
		IF (.NOT. CVT_DTB (SCRATCH(1:SIZE), LOCAL_DIGITS)) GOTO 9000
	    ENDIF
	ENDIF
C
C	Count the number of digits in the phone number.
C
	DIGITS = 0			! Initialize the digits count.
	DO 100 I = 1, PHONE_SIZE
	    IF ( ((PHONE_NUMBER(I:I) .GE. '0') .AND.
	1	  (PHONE_NUMBER(I:I) .LE. '9')) ) THEN
		  DIGITS = DIGITS + 1	! Adjust the digits count.
	    ENDIF
100	CONTINUE
C
C	Open the authorization file for input.
C
	OPEN (UNIT=PHONE_UNIT, TYPE='OLD', READONLY, SHARED,
	1				FILE=FILE_NAME, ERR=9900)
	FILE_OPEN = .TRUE.		! Show authorize file is open.
C
C	The user name returned by the GETJPI service is padded with spaces
C	out to 12 characters.  Therefore, we search for a space to find the
C	end of the user name for comparison below.
C
	EU = INDEX (USER_NAME, ' ') - 1	! Find end of the user name.
C
C	Read each record searching for the current user name.
C
C	Format:
C
C	username [ LOCAL ] [ ! Comments. ]
C
C	Parameters in brackets [...] are optional.  If the parameter
C	"LOCAL" isn't specified, the user is allowed both local and
C	long distance access.  Otherwise only local access is allowed.
C
	DO WHILE (.TRUE.)			! Loop until end of file.
	  READ (PHONE_UNIT, 110, END=500, ERR=9900) SIZE, SCRATCH
110	  FORMAT (Q, A)
	  I = 1					! Init starting position.
	  DO WHILE ( (I .LE. SIZE) .AND.
	1	     (SCRATCH(I:I) .NE. TAB_CH) .AND.
	2	     (SCRATCH(I:I) .NE. SPACE_CH) )
			I = I + 1		! Find end of username.
	  ENDDO
	  REU = I - 1				! Set end of user name.
	  IF (USER_NAME(1:EU) .EQ. SCRATCH(1:REU)) THEN
	    IF (REU .LT. SIZE) THEN		! More than user name.
	      I = REU + 1			! Copy end of user name.
	      DO WHILE ( (REU .LT. SIZE) .AND.
	1		 ((SCRATCH(I:I) .EQ. TAB_CH) .OR.
	2		  (SCRATCH(I:I) .EQ. SPACE_CH)) )
			 I = I + 1		! Skip past tab & spaces.
	      ENDDO
	    ENDIF
	    CLOSE (UNIT=PHONE_UNIT)		! Close authorization file.
	    IF (SCRATCH(I:I+4) .EQ. 'LOCAL') THEN ! Local access only ?
	      IF (DIGITS .GT. LOCAL_DIGITS) THEN  ! Local phone number ?
		CALL WRITE_USER (LONG_AUTH)	! No long distance access.
		CALL PHONE_LOG ('NOLONG')	! Log long distance attempt.
		CALL FINISH()			! And exit the program ...
	      ENDIF
	    ENDIF
	  RETURN			! The user is authorized.
	  ENDIF
	ENDDO
500	CLOSE (UNIT=PHONE_UNIT)		! Close authorization file.
	CALL WRITE_USER (NO_AUTH)	! Tell user not authorized.
	CALL PHONE_LOG ('NOAUTH')	! Log "No Authorize" entry.
	CALL FINISH()			! And exit the program ...
C
C	We come here when unable to convert the local digits value.
C
9000	CALL WRITE_USER (SS//
	1	'*** Error converting the local digits value --> "'//
	2	SCRATCH(1:SIZE)//'". ***'//BELL//DS)
	CALL FINISH()			! Exit on conversion errors.
C
C	We come here to report an error when we can't open or read the
C	phone authorization file.
C
9900	CALL RMS_ERROR (MODULE_NAME)	! Report the RMS error.
	CALL WRITE_USER (
	1 '*** The authorization file name is "'//FILE_NAME//'" ***'
	2 //DS)
	AUTHORIZE_USER = .FALSE.	! Return failure status.
	IF (FILE_OPEN) THEN
	    CLOSE (UNIT=PHONE_UNIT)	! Close authorization file.
	ENDIF
	CALL FINISH()			! Exit on authorize file errors.
	END
