~ubuntu-branches/ubuntu/trusty/zonecheck/trusty-proposed

« back to all changes in this revision

Viewing changes to zc/input/inetd.rb

  • Committer: Bazaar Package Importer
  • Author(s): Stephane Bortzmeyer
  • Date: 2004-03-10 14:08:05 UTC
  • Revision ID: james.westby@ubuntu.com-20040310140805-ij55fso1e23bk8ye
Tags: upstream-2.0.3
ImportĀ upstreamĀ versionĀ 2.0.3

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# $Id: inetd.rb,v 1.13 2003/12/16 16:40:39 sdalu Exp $
 
2
 
 
3
 
4
# CONTACT     : zonecheck@nic.fr
 
5
# AUTHOR      : Stephane D'Alu <sdalu@nic.fr>
 
6
#
 
7
# CREATED     : 2003/08/27 12:02:17
 
8
# REVISION    : $Revision: 1.13 $ 
 
9
# DATE        : $Date: 2003/12/16 16:40:39 $
 
10
#
 
11
# CONTRIBUTORS: (see also CREDITS file)
 
12
#
 
13
#
 
14
# LICENSE     : GPL v2 (or MIT/X11-like after agreement)
 
15
# COPYRIGHT   : AFNIC (c) 2003
 
16
#
 
17
# This file is part of ZoneCheck.
 
18
#
 
19
# ZoneCheck is free software; you can redistribute it and/or modify it
 
20
# under the terms of the GNU General Public License as published by
 
21
# the Free Software Foundation; either version 2 of the License, or
 
22
# (at your option) any later version.
 
23
 
24
# ZoneCheck is distributed in the hope that it will be useful, but
 
25
# WITHOUT ANY WARRANTY; without even the implied warranty of
 
26
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
27
# General Public License for more details.
 
28
#
 
29
# You should have received a copy of the GNU General Public License
 
30
# along with ZoneCheck; if not, write to the Free Software Foundation,
 
31
# Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 
32
#
 
33
 
 
34
require 'getoptlong'
 
35
require 'param'
 
36
 
 
37
##
 
38
## Processing parameters from INETD
 
39
##
 
40
## WARN: don't forget to update locale/inetd.*
 
41
##
 
42
## ----------------------------------------------------------------------
 
43
##
 
44
##
 
45
## usage: PROGNAME: [-hV] [-46] [-c conf]
 
46
##         --lang          Select another language (en, fr, ...)
 
47
##     -h, --help          Show this message
 
48
##     -V, --version       Display version and exit
 
49
##     -c, --config        Specify location of the configuration file
 
50
##         --testdir       Location of the directory holding tests
 
51
##     -r, --resolver      Resolver to use for guessing 'ns' information
 
52
##     -4, --ipv4          Only allow to check the zone with IPv4 connectivity
 
53
##     -6, --ipv6          Only allow to check the zone with IPv6 connectivity
 
54
##
 
55
##
 
56
## Command           Args
 
57
##   ?|help           0     This message
 
58
##   q|quit|exit      0     Leave ZoneCheck
 
59
##   check            0     Launch the zone checking process
 
60
##   zone|domain      1     Zone to test
 
61
##   nslist           0+    Set the list of the zone nameservers
 
62
##   set              1-2   Option to set (see options)
 
63
##   unset            1     Option to unset (see options)
 
64
##   preset           1     Preset configuration (supported: classic, fatal)
 
65
##   show             1     Show possibles values (supported: profiles)
 
66
## 
 
67
## Options           Args
 
68
##   lang             1     Select another language (en, fr, ...)
 
69
##   one              0     Only display the most relevant message
 
70
##   quiet            0     Don't display extra titles
 
71
##   tagonly          0     Display only tag (suitable for scripting)
 
72
##   category         1+    Only perform test for the specified category
 
73
##   profile          1     Force a profile to use
 
74
##   verbose          1+    Display extra information (see verbose)
 
75
##   output           1+    Output (see output)
 
76
##   error            1+    Behaviour in case of error (see error)
 
77
##   transp           1+    Transport/routing layer (see transp)
 
78
##   option           1+    Set extra options (-,-opt,opt,opt=foo)
 
79
## 
 
80
## Arguments for Options
 
81
##   verbose:             [intro/testname/explain/details]
 
82
##                        [reportok|fatalonly] [testdesc|counter]
 
83
##     intro          [i]  Print summary for domain and associated nameservers
 
84
##     testname       [n]  Print the test name
 
85
##     explain        [x]  Print an explanation for failed tests
 
86
##     details        [d]  Print a detailed description of the failure
 
87
##     reportok       [o]  Still report passed test
 
88
##     fatalonly      [f]  Print fatal errors only
 
89
##     testdesc       [t]  Print the test description before running it
 
90
##     counter        [c]  Print a test counter
 
91
## 
 
92
##   output:               [byseverity|byhost] [text|html]
 
93
##     byseverity    *[bs] Output is sorted/merged by severity
 
94
##     byhost         [bh] Output is sorted/merged by host
 
95
##     text          *[t]  Output plain text
 
96
##     html           [h]  Output HTML
 
97
## 
 
98
##   error:                [allfatal|allwarning|dfltseverity] [stop|nostop]
 
99
##     allfatal       [af] All error are considered fatal
 
100
##     allwarning     [aw] All error are considered warning
 
101
##     dfltseverity  *[ds] Use the severity associated with the test
 
102
##     stop          *[s]  Stop on the first fatal error
 
103
##     nostop         [ns] Never stop (even on fatal error)
 
104
## 
 
105
##   transp:               [ipv4/ipv6] [udp|tcp|std]
 
106
##     ipv4          *[4]  Use IPv4 routing protocol
 
107
##     ipv6          *[6]  Use IPv6 routing protocol
 
108
##     udp            [u]  Use UDP transport layer
 
109
##     tcp            [t]  Use TCP transport layer
 
110
##     std           *[s]  Use UDP with fallback to TCP for truncated messages
 
111
## 
 
112
## Example
 
113
##   preset classic
 
114
##   zone foorbar.com
 
115
##   check
 
116
## 
 
117
module Input
 
118
    class INETD
 
119
        with_msgcat "inetd.%s"
 
120
 
 
121
        def allow_preset ; false ; end
 
122
 
 
123
        def initialize
 
124
            @prompt = "zonecheck> "
 
125
        end
 
126
 
 
127
        def restart
 
128
        end
 
129
 
 
130
        def parse(p)
 
131
            begin
 
132
                ipv6, ipv4 = false, false
 
133
 
 
134
                opts = GetoptLong::new(
 
135
                [ '--help',     '-h',   GetoptLong::NO_ARGUMENT       ],
 
136
                [ '--version',  '-V',   GetoptLong::NO_ARGUMENT       ],
 
137
                [ '--lang',             GetoptLong::REQUIRED_ARGUMENT ],
 
138
                [ '--debug',    '-d',   GetoptLong::REQUIRED_ARGUMENT ],
 
139
                [ '--config',   '-c',   GetoptLong::REQUIRED_ARGUMENT ],
 
140
                [ '--testdir',          GetoptLong::REQUIRED_ARGUMENT ],
 
141
                [ '--resolver', '-r',   GetoptLong::REQUIRED_ARGUMENT ],
 
142
                [ '--ipv4',     '-4',   GetoptLong::NO_ARGUMENT       ],
 
143
                [ '--ipv6',     '-6',   GetoptLong::NO_ARGUMENT       ] )
 
144
 
 
145
                opts.each { |opt, arg|
 
146
                    case opt
 
147
                    when '--help'      then usage(EXIT_USAGE, $console.stdout)
 
148
                    when '--version'
 
149
                        l10n_version = $mc.get('input:version') % $zc_version
 
150
                        l10n_version.gsub!(/PROGNAME/, PROGNAME)
 
151
                        $console.stdout.puts l10n_version
 
152
                        exit EXIT_OK
 
153
                    when '--quiet'     then p.rflag.quiet       = true
 
154
                    when '--debug'     then $dbg.level          = arg
 
155
                    when '--lang'      then $locale.lang        = arg
 
156
                    when '--config'    then p.preconf.cfgfile   = arg.untaint
 
157
                    when '--testdir'   then p.preconf.testdir   = arg.untaint
 
158
                    when '--resolver'  then p.resolver.local    = arg
 
159
                    when '--ipv6'      then ipv6                = true
 
160
                    when '--ipv4'      then ipv4                = true
 
161
                    end
 
162
                }
 
163
            rescue GetoptLong::Error
 
164
                return false
 
165
            end
 
166
 
 
167
            ipv6 = ipv4 = true if !ipv6 && !ipv4
 
168
            $ipv4_stack &&= ipv4
 
169
            $ipv6_stack &&= ipv6
 
170
 
 
171
            true
 
172
        end
 
173
 
 
174
        def interact(p, c, tm, io=$console.stdout)
 
175
            io.puts $mc.get('input:inetd:welcome').gsub('VERSION', ZC_VERSION)
 
176
 
 
177
            io.print @prompt
 
178
            io.flush
 
179
            
 
180
            while true do
 
181
                # Check if ^D otherwise read a full line
 
182
                char = $stdin.getc
 
183
                break if char.nil? || char == 4
 
184
                $stdin.ungetc(char)
 
185
                line = $stdin.gets
 
186
                break if line.nil?
 
187
 
 
188
                line.strip!
 
189
                begin
 
190
                    case line
 
191
                    when ''
 
192
                    # Set
 
193
                    when /^preset\s+(\w+)$/
 
194
                        case $1
 
195
                        when 'classic'
 
196
                            p.verbose           = 'i,x,d,c'
 
197
                            io.puts '+ set verbose i,x,d,c'
 
198
                        when 'fatal'
 
199
                            p.verbose           = 'x,d,f'
 
200
                            p.rflag.quiet       = true
 
201
                            io.puts '+ set verbose x,d,f'
 
202
                            io.puts '+ set quiet'
 
203
                        else
 
204
                            error($mc.get('input:inetd:unknown_preset') % $1)
 
205
                        end
 
206
                    when /^set\s+(\w+)\s+(.*?)\s*$/
 
207
                        case $1
 
208
                        when 'verbose'  then p.verbose          = $2
 
209
                        when 'output'   then p.output           = $2
 
210
                        when 'error'    then p.error            = $2
 
211
                        when 'transp'   then p.transp           = $2
 
212
                        when 'option'   then p.option           = $2
 
213
                        when 'category' then p.category         = $2
 
214
                        when 'quiet'    then p.rflag.quiet      = true
 
215
                        when 'one'      then p.rflag.one        = true
 
216
                        when 'tagonly'  then p.rflag.tagonly    = true
 
217
                        when 'lang'     then $locale.lang       = $2
 
218
                        when 'profile'  then p.preconf.profile = c.profilename  = $2
 
219
                        end
 
220
                    when /^nslist\s+(.*)/          then p.domain.ns     = $1
 
221
                    when /^(?:zone|domain)\s+(.*)/ then p.domain.name   = $1
 
222
                    # Unset
 
223
                    when /^unset\s+(\w+)$/
 
224
                        case $1
 
225
                        when 'quiet'    then p.rflag.quiet      = false
 
226
                        when 'one'      then p.rflag.one        = false
 
227
                        when 'profile'  then p.preconf.profile = c.profilename  = nil
 
228
                        end
 
229
                    # Show
 
230
                    when /show\s+(\w+)$/
 
231
                        case $1
 
232
                        when 'profiles'
 
233
                            c.profiles.each { |profile| io.puts profile.name }
 
234
                        end
 
235
                    #
 
236
                    when '?', 'help'
 
237
                        io.puts $mc.get('input:inetd:help')
 
238
                    # Leave interaction loop
 
239
                    when 'check'                then return true
 
240
                    when 'quit', 'q', 'exit'    then return false
 
241
                        
 
242
                    # What did he said?!
 
243
                    else
 
244
                        error($mc.get('input:inetd:what'))
 
245
                    end
 
246
                rescue Param::ParamError, Config::ConfigError => e
 
247
                    error(e.to_s)
 
248
                end
 
249
 
 
250
                io.print @prompt
 
251
                io.flush
 
252
            end
 
253
 
 
254
            io.puts # Skip a line
 
255
            return false
 
256
        end
 
257
 
 
258
        def usage(errcode, io=$console.stdout)
 
259
            io.puts $mc.get('input:inetd:usage').gsub('PROGNAME', PROGNAME)
 
260
            io.flush
 
261
            exit errcode unless errcode.nil?
 
262
        end
 
263
 
 
264
        def error(str, errcode=nil, io=$console.stdout)
 
265
            l10n_error = $mc.get('word:error').upcase
 
266
            io.puts "#{l10n_error}: #{str}"
 
267
            io.flush
 
268
            exit errcode unless errcode.nil?
 
269
        end
 
270
    end
 
271
end