📄 radtest.texi
字号:
@item keepauth=1Do not alter request authenticator when resending the request.@end table@table @var@item port-typeSpecifies which port to use when sending the request. Use @samp{auth}to send the request to the authentication port(@pxref{client.conf,auth-port}), and @samp{acct} to send it to theaccounting port (@pxref{client.conf,acct-port}).@item code@RADIUS{} request code. Either numeric or symbolic (@pxref{NumericValues}).@item expr-or-pair-listSpecifies the @AVP{}s to include in the request. This argument iseither an expression evaluating to @code{avlist}, or an immediate@code{avlist} (@pxref{Avlists}). In the latter case, the parenthesesaround the list are optional.@end table@end deffn@deffn {Radtest statement} expect @var{code} [@var{expr-or-pair-list}]Test if @code{REPLY_CODE} matches @var{code} and, optionally, if@code{REPLY} matches @var{expr-or-pair-list}. If so, print thestring @samp{PASS}, otherwise print @samp{FAIL}.@xref{Interacting with Radius Servers}, for the detailed discussion ofthis statement.@end deffn@node Sample Radtest Program@subsection Sample Radtest ProgramAs an example, let's consider @command{radauth} program(@pxref{Radauth}). Its main purpose is to send authenticationrequest to the remote server, analyze its reply and if it ispositive, send an appropriate accounting record, thereby initiatinguser's session. Optionally, the script should also be able tosend a lone accounting record.In the discussion below, we will show and explain subsequentparts of the script text. For the ease of explanation, each lineof program text will be prepended by its ordinal line number.@subheading Parsing command line optionsThe script begins as follows:@smallexample@group 1 #! /usr/bin/radtest -f 2 3 while getopt "n:s:P:hv" 4 begin 5 case $OPTVAR in 6 "-n") NASIP = $OPTARG 7 "-s") SID = $OPTARG 8 "-P") PID = $OPTARG 9 "-v") set -v@end group@end smallexample@table @asis@item 1It is a @dfn{pragmatic comment} informing shell that itshould run @command{radtest} in order to interpret the program.@item 3This line starts option processing loop. @code{Getopt}(@pxref{Built-in Primitives,getopt}) in line 3 analyzes eachsubsequent command line argument and if it is an option checkswhether it matches one of the option letters defined in itsfirst argument. The option letter will be returned in @code{OPTVAR}variable, its argument (if any) -- in @code{OPTARG} variable.@item 4 -- 8@code{OPTARG} value is analyzed using @code{case} statement. Lines6 -- 8 preserve @code{OPTARG} values in appropriate variables forlater use. @code{NASIP} will be used as the value of@attr{NAS-IP-Address} attribute, @attr{SID} is the session id(@attr{Acct-Session-Id} attribute), and @attr{PID} is the portnumber (for @attr{NAS-Port-Id} attribute.@item 9This line sets @option{-v} option to the @command{radtest}interpreter (@pxref{Invoking radtest}). @end table@noindentThe next piece of code handles @option{-h} and erroneous options:@smallexample@group 10 "-h") begin 11 print <<-EOT 12 usage: radauth [OPTIONS] [COMMAND] login [password] 13 Options are: 14 -v Print verbose descriptions of what is being done 15 -n IP Set NAS IP address 16 -s SID Set session ID 17 -P PORT Set NAS port number 18 COMMAND is one of: 19 auth Send only Access-Request (default) 20 acct Send Access-Request. If successfull, send 21 accounting start request 22 start Send accounting start request 23 stop Send accounting stop request 24 EOT 25 exit 0 26 end 27 ".*") begin 28 print "Unknown option: " $OPTVAR "\n" 29 exit 1 30 end 31 end 32 end@end group@end smallexample@table @asis @item 10 -- 26Print short description and exit, if the program is given @option{-h}.Notice that @samp{here document} syntax is used to print the text(@xref{Strings}, for its description). The leading whitespace inlines 12 to 24 is composed of tabulation characters (ASCII 9), notusual space characters (ASCII 32), as required by @samp{<<-}construct.@item 27 -- 30These lines handle unrecognized options.@item 31Closes case statement started on line 5 @item 32Closes compound statement started on line 4@end table@subheading Checking Command Line Consistency@smallexample@group 33 34 shift $@{OPTIND@}-1 35 36 if $# > 3 37 begin 38 print "Wrong number of arguments." 39 print "Try radauth -h for more info" 40 exit 1 41 end@end group@end smallexample@table @asis@item 34@code{OPTIND} keeps the ordinal number of the first non-optionalargument. This line shifts off all the options processed by@code{getopt}, so that the first non-optional argument may beaddressed by @code{$1} notation. Notice use of curly braces tosolve @dfn{minus ambiguity} (@pxref{minus-ambiguity}).@item 36 -- 41At this point we may have at most three arguments:command, user name, and password. If there are more, displaythe diagnostic message and exit the program.@end table @noindentNext piece of code: @smallexample@group 42 43 case $1 in 44 "auth|acct|start|stop") begin 45 COMMAND=$1 46 shift 1 47 end 48 ".*") COMMAND="auth" 49 end 50 51 LOGIN=$@{1:?User name is not specified. Try radauth -h for more info.@} 52 53 if $@{NASIP:-@} = "" 54 NASIP=$SOURCEIP 55 56 LIST = ( User-Name = $LOGIN NAS-IP-Address = $NASIP )@end group@end smallexample@table @asis@item 43 -- 48Check if a command is given. If so, store command name in the variable@code{COMMAND} and shift arguments by one, so login becomes argument@code{$1}. Otherwise, assume @samp{auth} command.@item 51If the user login name is supplied, store it into @code{LOGIN}variable. Otherwise, print diagnostic message and exit.@item 53 -- 54Provide a default value for @code{NASIP} variable from the built-invariable @code{SOURCEIP} (@pxref{Built-in Variables})@item 56The variable @code{LIST} will hold the list of A/V pairs to be sentto the server. This line initializes it with a list of two @AVP{}s:@attr{User-Name} and @attr{NAS-IP-Address}.@end table@subheading Defining Accounting FunctionAccounting function will be used to send accounting requests tothe server. It is supposed to take a single argument: an @code{avlist}of @AVP{}s to be sent to the server.@smallexample@group 57 58 'acct' 59 begin 60 if $@{SID:-@} = "" 61 input "Enter session ID: " SID 62 if $@{PID:-@} = "" 63 input "Enter NAS port ID: " PID 64 send auth Accounting-Request $1 + \ (Acct-Session-Id = $SID NAS-Port-Id = $PID)@end group@end smallexample@table @asis@item 58 -- 59These lines start the function definition. Notice quoting of thefunction name (@samp{acct}): it is necessary because it coincideswith a reserved keyword (@pxref{Reserved Keywords}).@item 60 -- 61If the value of @code{SID} (session ID) is not supplied, prompt theuser to input it.@item 62 -- 63If the value of @code{PID} (port ID) is not supplied, prompt theuser to input it.@item 64Send accounting request. The list of @AVP{}s to send is formed byconcatenating @attr{Acct-Session-Id} and @attr{NAS-Port-Id} attributesto the function's first argument.@end table@noindentThe final part of @code{acct} function analyzes the reply from theserver:@smallexample@group 65 if $REPLY_CODE != Accounting-Response 66 begin 67 print "Accounting failed.\n" 68 exit 1 69 end 70 print "Accounting OK.\n" 71 exit 0 72 end 73@end group@end smallexample@noindentNotice, that @code{acct} never returns. Instead it exits with anerror code indicating success or failure.@subheading Defining Authentication FunctionThe purpose of the authentication function @code{auth} isto send an @code{Access-Request} to the server and performsome actions based on its reply. The function will take three arguments:@table @code@item $1The list of @AVP{}s to include in the request.@item $2User password.@item $3This argument indicates whether accounting request must be sentafter successful authentication. String @samp{yes} means to sendthe accounting request, @samp{no} means not to send it.@end tableThe function is not expected to return. Instead it should exitto the shell with an appropriate error code.@smallexample@group 74 'auth' 75 begin 76 send auth Access-Request $1 + (User-Password = $2)@end group@end smallexample@table @asis@item 74 -- 75Begin the function definition. Notice quoting of thefunction name (@samp{auth}): it is necessary because it coincideswith a reserved keyword (@pxref{Reserved Keywords}).@item 76Send the initial authentication request. The list of @AVP{}s isformed by appending @attr{User-Password} pair to the list givenby the first argument to the function.@end table@noindentThe rest of the function analyzes the reply from the server and takesappropriate actions. Notice that if the server replies with an@code{Access-Challenge} packet, we will have to send subsequentauthentication requests, so this piece of code is enclosed withina @code{while} loop.First, the function handles @code{Access-Accept} and@code{Access-Reject} replies:@smallexample@group 77 while 1 78 begin 79 if $REPLY_CODE = Access-Accept 80 begin 81 print "Authentication passed. " + $REPLY[Reply-Message*] + "\n" 82 if $@{3:-no@} = no 83 exit 0 84 'acct'($1 + ( Acct-Status-Type = Start )) 85 end else if $REPLY_CODE = Access-Reject 86 begin 87 print "Authentication failed. " + $REPLY[Reply-Message*] + "\n" 88 break@end group@end smallexample@table @asis@item 77Begin an ``endless'' @code{while} loop. It will eventually be exitedeither using @code{break}, or using @code{exit} (see below).@item 79 -- 84Hanlde @code{Access-Accept} replies:@item 81Print the reply message. Notice the use of @samp{*} to print allthe instances of @attr{Reply-Message} attribute from the replypacket (@pxref{Accessing Elements of A/V Pair Lists}).@item 82 -- 83If the third argument is missing or is a string @samp{no}, exitindicating success (@pxref{Dereferencing Variables}). @item 84Otherwise, call @code{acct} function to perform accounting. The@AVP{}s included in the accounting request are formed by adding@attr{Acct-Status-Type} attribute to the list given by the firstargument to the function.@item 85 -- 88Handle @code{Access-Reject} replies. Print the reply message andbreak from the loop.@end table@noindentNext piece of code deals with @code{Access-Challenge} replies. Forsimplicity we assume that such replies always carry user menus(@xref{menus directory}, for the description of these). So, uponreceiving an @code{Access-Challenge} we should print out the menu,read the users selection and send back an @code{Access-Request}to the server. This part is the only one that actually continuesthe loop at line 77. @smallexample@group 89 end else if $REPLY_CODE = Access-Challenge 90 begin 91 print $REPLY[Reply-Message*] 92 input 93 send auth Access-Request \ 94 (User-Name = $LOGIN User-Password = $INPUT \ State = $REPLY[State])@end group@end smallexample@table @asis@item 91Print the menu contents carrieb by @attr{Reply-Message}attributes. There may be several instances of the attribute, hence theuse of @samp{*} to concatenate their values together.@item 92Read the input from the user. The input will be stored in @code{INPUT}variable. @xref{Built-in Primitives}, for the description of@code{input} statement.@item 93 -- 94Send an @code{Access-Request} packet with three attributes.@attr{User-Password} contains the user reply, @attr{State} containsthe menu state from the server reply packet.@end table@noindentFinal part of the function: @smallexample@group 95 end else begin 96 print "Authentication failed. Reply code " + $REPLY_CODE + "\n" 97 break 98 end 99 end100 exit 1101 end102@end group@end smallexample@table @asis@item 95 -- 98Handle unknown reply codes.@item 99Closes the while loop started on line 77.@item 100Exit to the shell indicating failure. This statement will be reachedonly if a @code{break} is executed either on line 88 or on line 97.@item 101Closes function definition started on lines 74 -- 75 @end table@subheading Final Part of Radauth ProgramThe final part selects an action based on the user command andexecutes it. It is equivalent to the @code{main} function in a@code{C} program:@smallexample@group103 case $@{COMMAND@} in104 "auth") 'auth'($LIST, $@{2:&Password: @}, no)105 "acct") 'auth'($LIST, $@{2:&Password: @}, yes)106 "start") 'acct'($LIST+(Acct-Status-Type = Start))107 "stop") 'acct'($LIST+(Acct-Status-Type = Stop))108 ".*") begin109 print "Unknown command. Try radauth -h for more info"110 exit 1111 end112 end113 114 # End of radauth@end group@end smallexample@table @asis@item 103Select an action based on the value of @code{COMMAND} variable.@item 104 -- 105Call @code{auth} function. If the second argument is given in thecommand line, its value is taken as user's password. Otherwise, theuser is prompted for the password with the string @samp{Password: }.The input is read with echo turned off to prevent the password frombeing compromised (the @samp{:&} construct, @pxref{DereferencingVariables}).@item 106 -- 107Call @code{acct} function for @samp{start} and @code{stop} commands.@item 108 -- 111Handle an unknown command verb.@item 112Closes @code{case} statement from line 103.@end table@c End of radtest.texi
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -