1 |
# ident.tcl -- |
# ident.tcl -- |
2 |
# |
# |
3 |
# Ident client for Tcl. |
# Ident client for Tcl (See RFC 1413). |
4 |
# |
# |
5 |
# Copyright (C) 2003 by Philip Moore <FireEgl@Tcl.no> |
# Copyright (C) 2003 by Philip Moore <FireEgl@Tcl.no> |
6 |
# This code may be distributed under the same terms as Tcl. |
# This code may be distributed under the same terms as Tcl. |
7 |
# |
# |
8 |
# RCS: @(#) $Id$ |
# RCS: @(#) $Id$ |
9 |
|
# |
10 |
# Options: |
# This package provides an ident client (See RFC 1413), |
11 |
# -lhost Specifies the -myaddr to use with the socket command. (optional, defaults to the systems default) |
# used for querying a remote system to find the username |
12 |
# -lport Specifies the local port that's currently connected to the remote (required). |
# that's connected to a port on the local machine. |
13 |
# -rhost Specifies the remote host that we're currently connected to. (required) |
# |
14 |
# -rport Specifies the remote port that we're currently connected to. (required) |
# Usage: |
15 |
# -timeout Specifies the timeout (in milliseconds) to use for the ident request. (optional, defaults to 30000) |
# |
16 |
# -command Specifies the command used as a callback when the ident request is complete. (optional) |
# ::ident::ident -option value ?-option value? ... |
17 |
# -authport Specifies the identd (auth) port to use on the remote (normally port 113). (optional, defaults to 113) |
# |
18 |
# -sock Specifies the socket that's currently connected to the remote host. (optional) |
# The possible options are listed below.. |
19 |
# Note, If -sock is used it takes the place of the -lhost, -lport, -rhost, -rport options. |
# |
20 |
|
# -authport |
21 |
# There's also some alias and convenience options.. |
# Specifies the identd (auth) port to use on the remote (normally port 113). |
22 |
# -localhost is the same as -lhost |
# (optional, defaults to 113) |
23 |
# -localport is the same as -lport |
# |
24 |
# -remotehost is the same as -rhost |
# -sock |
25 |
# -remoteport is the same as -rport |
# Specifies the socket that's currently connected to the remote host. |
26 |
# -socket is the same as -sock |
# (optional, if it's not specified, you must use -lport, -rhost, and -rport) |
27 |
# -local can be used to specify the local host and port separated by a colon. |
# Note, if -sock is used it takes the place of the -lhost, -lport, -rhost, -rport options. |
28 |
# -remote can be used to specify the remote host and port separated by a colon. |
# |
29 |
# There's actually more aliases than this.. I got carried away..sorry. Remove them if you want. |
# -lhost |
30 |
|
# Specifies the local hostname (or ip) to use for creating the socket to the remote. |
31 |
# Example Usage: |
# (optional, defaults to the systems default) |
32 |
# 1. This one specifies a callback command that receives the results of the ident request when it's ready: |
# |
33 |
# ::ident::ident -sock sock7 -timeout 7000 -command [list yourcallback yourargs] |
# -lport |
34 |
# Returns the token to be used by your callback command. |
# Specifies the local port that's currently connected to the remote. |
35 |
# When the ident request completes your callback command will recieve |
# (required, unless you use the -sock option) |
36 |
# three arguments appended to yourargs.. <token> <status> <username/or the error message> |
# |
37 |
# The status will be either "ok" or "error". |
# -rhost |
38 |
# |
# Specifies the remote host that we're currently connected to. |
39 |
# 2. This one waits for the ident reqest to complete and returns the results.. |
# (required, unless you use the -sock option) |
40 |
# ::ident::ident -lport 1234 -rhost remotehost -rport remoteport |
# |
41 |
# Returns a list with two elements, the first element is the status (either "ok" or "error"). |
# -rport |
42 |
# The second element is the users ident if the status is "ok", |
# Specifies the remote port that we're currently connected to. |
43 |
# or the error message if the status is "error". |
# (required, unless you use the -sock option) |
44 |
|
# |
45 |
|
# -timeout |
46 |
|
# Specifies the timeout (in milliseconds) to use for the ident request. |
47 |
|
# (optional, defaults to 30000) |
48 |
|
# |
49 |
|
# -command |
50 |
|
# Specifies the command used as a callback when the ident request is complete. |
51 |
|
# (optional, without this, the script blocks until the ident request fails/succeeds) |
52 |
|
# ::ident::ident will immediately return a unique ID used to identify |
53 |
|
# this ident request, and will later be passed to your callback command |
54 |
|
# when the lookup fails/succeeds. |
55 |
|
# |
56 |
|
# When the ident request completes, your callback command will recieve |
57 |
|
# three arguments in addition to the arguments you specified for the command: |
58 |
|
# <id> <status> <username/or the error message> |
59 |
|
# The status will be either "ok" or "error". |
60 |
|
# |
61 |
|
# If -command isn't used, ::ident::ident blocks until the ident request fails or succeeds, |
62 |
|
# and then returns a list with two elements.. |
63 |
|
# The first element will be either "ok" or "error" to indicate whether the ident succeeded or failed. |
64 |
|
# If the first element is "ok", the second element will the username on the remote. |
65 |
|
# If the first element is "error", the second element will be the reason it failed. |
66 |
|
# |
67 |
|
# |
68 |
|
# There's also some alias and convenience options... |
69 |
|
# -localhost or -myaddr is the same as -lhost |
70 |
|
# -localport is the same as -lport |
71 |
|
# -remotehost is the same as -rhost |
72 |
|
# -remoteport is the same as -rport |
73 |
|
# -socket is the same as -sock |
74 |
|
# -local can be used to specify the local host and port separated by a colon. |
75 |
|
# -remote can be used to specify the remote host and port separated by a colon. |
76 |
# |
# |
77 |
|
|
78 |
package require Tcl 8.2 |
package require Tcl 8.2 |
100 |
set info(lport) [lindex $sockinfo 2] |
set info(lport) [lindex $sockinfo 2] |
101 |
} |
} |
102 |
} |
} |
103 |
{-lhost} - {-localhost} - {-chost} - {-clienthost} { set info(lhost) $d } |
{-lhost} - {-localhost} - {-chost} - {-clienthost} - {-myaddr} { set info(lhost) $d } |
104 |
{-lport} - {-localport} - {-cport} - {-clientport} { set info(lport) $d } |
{-lport} - {-localport} - {-cport} - {-clientport} { set info(lport) $d } |
105 |
{-rhost} - {-remotehost} - {-phost} - {-peerhost} - {-shost} - {-serverhost} { set info(rhost) $d } |
{-rhost} - {-remotehost} - {-phost} - {-peerhost} - {-shost} - {-serverhost} { set info(rhost) $d } |
106 |
{-rport} - {-remoteport} - {-pport} - {-peerport} - {-sport} - {-serverport} { set info(rport) $d } |
{-rport} - {-remoteport} - {-pport} - {-peerport} - {-sport} - {-serverport} { set info(rport) $d } |
131 |
} else { |
} else { |
132 |
# We wait here and return the results when it's ready. |
# We wait here and return the results when it's ready. |
133 |
vwait "::ident::$id\(status)" |
vwait "::ident::$id\(status)" |
134 |
return [list $info(status) $info(ident)] |
# Returns the status and ident/error: |
135 |
|
list $info(status) $info(ident) |
136 |
} |
} |
137 |
} |
} |
138 |
|
|
161 |
proc ::ident::Read {sock id} { |
proc ::ident::Read {sock id} { |
162 |
fileevent $sock readable {} |
fileevent $sock readable {} |
163 |
upvar 0 ::ident::$id info |
upvar 0 ::ident::$id info |
164 |
if {[set code [catch {gets $sock line} err]] || $err == -1} { |
if {[catch {gets $sock line} err] || $err == -1} { |
165 |
Finish $id {error} {SOCKET-ERROR} |
Finish $id {error} {SOCKET-ERROR} |
166 |
} else { |
} else { |
167 |
set line [split $line :] |
switch -- [string toupper [string trim [lindex [set line [split $line :]] 1]]] { |
168 |
set code [string toupper [string trim [lindex $line 1]]] |
{ERROR} { Finish $id {error} [string trim [lindex $line 2]] } |
169 |
switch -- $code { |
{USERID} { Finish $id {ok} [string trim [lindex $line 3]] } |
170 |
{ERROR} { |
{default} { Finish $id {error} {UNKNOWN-RESPONSE} } |
|
Finish $id {error} [string trim [lindex $line 2]] |
|
|
} |
|
|
{USERID} { |
|
|
Finish $id {ok} [string trim [lindex $line 3]] |
|
|
} |
|
|
{default} { |
|
|
Finish $id {error} {UNKNOWN-RESPONSE} |
|
|
} |
|
171 |
} |
} |
172 |
} |
} |
173 |
catch { close $sock } |
catch { close $sock } |