~ubuntu-branches/ubuntu/breezy/pam/breezy

« back to all changes in this revision

Viewing changes to debian/local/pam_getenv

  • Committer: Bazaar Package Importer
  • Author(s): Sam Hartman
  • Date: 2004-06-28 14:28:08 UTC
  • mfrom: (2.1.1 warty)
  • Revision ID: james.westby@ubuntu.com-20040628142808-adikk7vtfg3pzcjw
Tags: 0.76-22
* Add uploaders
* Document location of repository
* Fix options containing arguments in pam_unix, Closes: #254904

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/perl -w
 
2
 
 
3
=head1 NAME
 
4
 
 
5
pam_getenv - get environment variables from /etc/environment
 
6
 
 
7
=head1 SYNOPSIS
 
8
 
 
9
pam_getenv B<[-l] [-s]> I<env_var>
 
10
 
 
11
=head1 DESCRIPTION
 
12
 
 
13
This tool  will print out the value of I<env_var> from F</etc/environment>.  It will attempt to expand environment variable references in the definition of I<env_var> but will fail if PAM items are expanded.
 
14
 
 
15
The B<-l> option indicates the script should return an environment variable related to default locale information.
 
16
 
 
17
The B<-s> option indicates that the script should return an
 
18
system default environment variable.
 
19
 
 
20
Currently neither the B<-l> or B<-s> options do anything.  They are
 
21
included because future versions of Debian may have a separate
 
22
repository for the initial environment used by init scripts and for
 
23
system locale information.  These options will allow this script to be
 
24
a stable interface even in that environment.
 
25
 
 
26
=cut
 
27
 
 
28
# Copyright 2004 by Sam Hartman
 
29
# This script may be copied under the terms of the GNU GPL
 
30
# version 2, or at your option any later version.
 
31
 
 
32
use strict;
 
33
use vars qw(*ENVFILE);
 
34
 
 
35
sub read_line() {
 
36
  my $line;
 
37
  local $_;
 
38
  line: while (<ENVFILE>) {
 
39
    chomp;
 
40
    s/^\s+//;
 
41
s/\#.*$//;
 
42
    next if $_ eq "";
 
43
    if (s/\\\s*$//) {
 
44
      $line .= $_;
 
45
      next line;
 
46
    }
 
47
 
 
48
    $line .= $_;
 
49
    last;
 
50
  }
 
51
  $line;
 
52
  
 
53
}
 
54
 
 
55
 
 
56
sub parse_line($) {
 
57
  my $var;
 
58
  my (%x, @x);
 
59
  local $_ = shift;
 
60
  return undef unless defined $_ and s/(\S+)\s//;
 
61
  $var->{Name} = $1;
 
62
  s/^\s*//;
 
63
  @x = split(/=([^"\s]\S*|"[^"]*")\s*/, $_);
 
64
  unless (scalar(@x)%2 == 0) {
 
65
    push @x, undef;
 
66
  }
 
67
  %x = @x;
 
68
  @{$var}{"Default", "Override"} =
 
69
    @x{"DEFAULT", "OVERRIDE"};
 
70
  $var;
 
71
}
 
72
 
 
73
sub expand_val($) {
 
74
  my ($val) = @_;
 
75
return undef unless $val;
 
76
        die "Cannot handle PAM items\n" if /(?<!\\)\@/;
 
77
  $val =~ s/(?<!\\)\${([^}]+)}/$ENV{$1}||""/eg;
 
78
  return $val;
 
79
}
 
80
 
 
81
  
 
82
 
 
83
open (ENVFILE, "/etc/environment")
 
84
  or die "Cannot open environment file: $!\n";
 
85
 
 
86
while ($_ = shift) {
 
87
  next if $_ eq "-s";
 
88
  next if $_ eq "-l";
 
89
my $var;
 
90
  variable: while ($var = parse_line(read_line())) {
 
91
    my $val;
 
92
    next variable unless $var->{Name}  eq $_;
 
93
unless  ($val = expand_val($var->{Override})) {
 
94
  $val = expand_val($var->{Default});
 
95
}
 
96
    print ($val, "\n");
 
97
    exit(0);
 
98
  }
 
99
}
 
100
 
 
101