File: msktutil.interactive

package info (click to toggle)
msktutil 0.5.1-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 752 kB
  • ctags: 279
  • sloc: cpp: 2,607; perl: 254; xml: 231; ansic: 85; makefile: 49; sh: 44
file content (444 lines) | stat: -rw-r--r-- 10,414 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
#!/usr/bin/perl -w

# FIXME: this script hasn't been updated for msktutil version 0.4...
# and I (jknight) don't really see what the purpose is in any case.
# YMMV!

#
#   $Id: msktutil.interactive 42 2006-08-03 16:28:20Z finley $
#    vi:set filetype=perl:
#

################################################################################
#
#   Use stuff
#
use strict;
use File::Basename;
use vars qw($VERSION $quiet);

#
################################################################################


################################################################################
#
#   Variables
#
my $progname = basename($0);
my $progdir = dirname($0);
my $progpath;

our %o;
my $cache_file = "$ENV{HOME}/.$progname.cache";
my $config_file = undef;

# Linux or Unix?
if(`uname -s` =~ m/linux/i) {
    $o{os} = 'Linux';
} else {
    $o{os} = 'Unix';
}

#
################################################################################


################################################################################
#
#   Main
#

# XXX put getopts here

read_config_file($cache_file);
#
# Must read specified config file second, as it will override cache info. -BEF-
if( defined($config_file) ) {
    if(-r $config_file) {
        read_config_file($config_file);

    } else {
        print "ERROR:  The config file you specified doesn't exist,\n";
        print "        or I don't have permission to read it.\n";
        exit 1;
    }
}


my $satisfied = 'no';
until($satisfied =~ m/^(y|yes)$/i) {
    query_user();
    print "\nAre you satisfied? [$satisfied]: ";
    $satisfied = get_response($satisfied);
}


# if msktutil is in same dir use it 
if ( -r "${progdir}/msktutil" ) {
		$progpath = "${progdir}/";
} else {
		$progpath = "";
}

my $cmd = qq(${progpath}msktutil);
$cmd .= qq( $o{options}) if($o{options} ne '');
$cmd .= qq( -b "$o{base}");
$cmd .= qq( -k $o{keytab});
$cmd .= qq( -h $o{hostname});
$cmd .= qq( -s $o{service});
$cmd .= qq( --upn $o{upn}) if($o{upn} ne '');
$cmd .= qq( --computer-name $o{computername});
$cmd .= qq( $o{extop});

print "\n";
print "Here is the command I've put together based on your input:\n";
print "\n";
print "  $cmd\n";
print "\n";
print "Shall I run it for you? [no]: ";
my $run_it = get_response('no');
if($run_it =~ m/^(y|yes)$/i) {
    !system($cmd) or die("Couldn't run\n  $cmd");
}

write_cache_file($cache_file);

exit 0;

#
################################################################################


################################################################################
#
#   Subroutines
#

#
# Usage:
#   $value = get_thing('thing_type');
#
# Ie:
#   $division = get_thing('division', ['default_value'[,'case']]);
#
sub get_thing {

    my $thing = shift;
    my $default = shift;
    my $case = shift;

    if(! defined($default) )    { $default = 'x'; }
    if(! defined($case) )       { $case = 'ucfirst'; }

    my $i;
    if( defined($o{$thing}) ) {
       $i = $o{$thing};
    } else {
        $i = $default;
    }
    chomp $i;

    if($case eq 'upper') {
        $thing = uc($thing);
    } elsif($case eq 'lower') {
        $thing = lc($thing);
    } else {
        $thing = ucfirst($thing);
    }

    print "  $thing [$i]: ";
    return get_response($i);
}


sub get_keytab {

    my $i;
    if( defined($o{keytab}) ) {
        $i = $o{keytab};

    } else {
        my @possible_dirs = (
            '/etc/krb5',
            '/etc',
            '/krb5',
        );

        my @possible_keytabs = (
            'krb5.keytab',
            'v5srvtab',
        );

        #
        # And see if a keytab file exists there on the local machine.
        foreach my $dir (@possible_dirs) {
            foreach my $k (@possible_keytabs) {
                my $file = "$dir/$k";
                if(-f $file) { 
                    #
                    # found it!
                    $i = $file;
                    last;
                }
            }
        }

        #
        # If we couldn't find a keytab file, see if we can figure out where
        # it should go.
        my $dir;
        if(! defined($i) ) {
            #
            # Choose a resonable default as a suggestion
            foreach (@possible_dirs) {
                $dir = $_;
                my $file = "$dir/krb5.conf";
                last if(-f $file);
            }

            if($dir eq '/krb5') {
                $i = "$dir/v5srvtab";
            } else {
                $i = "$dir/krb5.keytab";
            }
        }

    }


    # Minor clean up if needed.
    chomp $i;
    $i =~ s/\s+//g;

    print "  Keytab [$i]: ";
    return get_response($i);
}


sub get_mac {

    my $i;
    if( defined($o{mac}) ) {
        $i = $o{mac};
    } else {
        if( $o{os} =~ m/Linux/ ) {
            $i = `ifconfig | grep HWaddr | head -1 | sed -e"s/^.*HWaddr //"`;

        } else {
            $i = `ifconfig -a | grep ether | head -1 | sed -e"s/^.*ether //"`;
        }
    }

    # Minor clean up if needed.
    chomp $i;
    $i =~ s/\s+//g;

    print "  MAC [$i]: ";
    return get_response($i);
}


sub get_computername {

    my $i;
    if( defined($o{computername}) ) {
        $i = $o{computername};
    } else {
        $_ = $o{hostname};
        my ($a, $b) = split(/\./);
        my $prefix;
        if($o{service} eq "host") {
           $prefix = "h"; 
        } else {
           $prefix = $o{service};
        }
        $i = $prefix . "-" . $a . "-" . $b;
    }
    my $max_length = 18;
    if( length($i) gt $max_length) {
        $i = substr($i, 0, $max_length);
    }
    chomp $i;
    $i =~ s/-+$//;

    print "  Computername [$i]: ";
    return get_response($i);
}


sub get_hostname {

    my $i;
    if( defined($o{hostname}) ) {
        $i = $o{hostname};
    } else {
        if( $o{os} =~ m/Linux/ ) {
            $i = `hostname -f`;
        } else {
            $i = `hostname`;
        }
    }
    chomp $i;

    print "  Hostname [$i]: ";
    return get_response($i);
}


#
# Usage:
#   $answer = get_response($answer);
#
sub get_response {

    my $garbage_out=$_[0];
    my $garbage_in=<STDIN>;

    chomp $garbage_out;
    chomp $garbage_in;

    unless($garbage_in eq "") { 
        $garbage_out = $garbage_in; 
    }

    return $garbage_out;
}


sub read_config_file {

    my $file = shift;

    if(! -e $file) {
        # Cache file doesn't exist yet.  That's OK.  I presume it's a cache 
        # file -- a config file should be tested for prior to invoking this 
        # function earlier. -BEF-
        return 1;
    }

    open(FILE,"<$file") or die("Couldn't open $file for reading.");
        while(<FILE>) {
            next if(m/^(\s+|#|$)/);
            chomp;
            my ($var, $val) = split(/=/, $_, 2);
            #
            # Ensure that the variable is lowercase.
            $var = lc($var);
            $o{$var} = $val;
        }
    close(FILE);

    return 1;
}


sub get_admin {

    my $thing = 'admin';
    my $default = shift;
    my $case = shift;

    if(! defined($default) )    { $default = 'x'; }
    if(! defined($case) )       { $case = 'ucfirst'; }

    my $i;
    if( defined($o{$thing}) ) {
       $i = $o{$thing};
    } else {
        $i = $default;
    }
    chomp $i;
    $i =~ s/\s+/_/g;

    if($case eq 'upper') {
        $thing = uc($thing);
    } elsif($case eq 'lower') {
        $thing = lc($thing);
    } else {
        $thing = ucfirst($thing);
    }

    print "  $thing [$i]: ";
    return get_response($i);
}


sub write_cache_file {

    my $file = shift;

    #
    # Don't cache certain bits.  Delete, rather than simply skipping,
    # so that we ensure they don't end up in the cache file. -BEF-
    delete $o{mac};
    delete $o{hostname};
    delete $o{computername};
    delete $o{csi};
    delete $o{os};

    open(FILE,">$file") or die("Couldn't open $file for writing.");
    foreach my $var (keys %o) {
        #
        # Write it out.
        print FILE $var . "=" . $o{$var} . "\n";
    }
    close(FILE);

    return 1;
}


sub query_user {
        print "\n";
        print "Please verify the following information:\n";
        print "--------------------------------------------------------------------------------\n";
        $o{hostname}    = get_hostname();

        #
        # Service name to be created or added to the keytab.  Examples include 
        # host, HTTP, afs, and cvs.  Typically a hosts keytab file only includes
        # the 'host' service. 
        #
        $o{service}     = get_thing('service','host');

        $o{computername} = get_computername();
#        $o{division}    = get_thing('division');
#        $o{building}    = get_thing('building');
#        $o{room}        = get_thing('room');
#        $o{csi}         = get_thing('csi','x','upper');
#        print qq(  >> Admin: use a group name if possible <<\n);
#        $o{admin}       = get_admin();
#        $o{switch}      = get_thing('switch');
#        $o{port}        = get_thing('port');
#        $o{mac}         = get_mac();
#        $o{description} = "$o{division} $o{building} $o{room} $o{csi} $o{admin} $o{switch} $o{port} $o{mac}";
        $o{base}        = get_thing('base',"OU=o$o{os},OU=ouCIS,OU=OPSDIVS,OU=EmployeeOrganizations");
        $o{keytab}      = get_keytab();
#        print qq(  >> UPN: typically auto-determined based on Hostname <<\n);
#        $o{upn}         = get_thing('upn','','upper');

        #
        # Other options/extopts that can be added:
        #	--des-only  --disable-des-only
        #	--no-pac    --disable-no-pac
        #	--delegation --disable-delegation
        #   --enctypes n   decimal number for msDS-supportedEncryptionTypes
        #                  3 - is DES only
        #                  7 - is default, DES+RC4
        #                 28 - AES256, AES128 and RC4 
        #   --server dc-hostname  Uses this server
        #   --verbose    to get more output, entry twice to get ldap debug
        #
        # Notes: delegation requires special privilages in AD. 
        #        -des-only and -enctypes are mutually exclusive
        #	
		print qq(  >> Some options: --delegation and --enctypes 28 are typical << \n);
        $o{options}     = get_thing('options','');
		print qq(  >>  --verbose --server AD_servername << \n);
        $o{extop}       = get_thing('extop','');
}


#
################################################################################