MFC numerous features from HEAD.
[dragonfly.git] / etc / pam.d / convert.sh
1 #! /bin/sh
2
3 # Copyright (c) 2005 The DragonFly Project.  All rights reserved.
4 #
5 # This code is derived from software contributed to The DragonFly Project
6 # by Joerg Sonnenberger <joerg@bec.de>
7 #
8 # Redistribution and use in source and binary forms, with or without
9 # modification, are permitted provided that the following conditions
10 # are met:
11 #
12 # 1. Redistributions of source code must retain the above copyright
13 #    notice, this list of conditions and the following disclaimer.
14 # 2. Redistributions in binary form must reproduce the above copyright
15 #    notice, this list of conditions and the following disclaimer in
16 #    the documentation and/or other materials provided with the
17 #    distribution.
18 # 3. Neither the name of The DragonFly Project nor the names of its
19 #    contributors may be used to endorse or promote products derived
20 #    from this software without specific, prior written permission.
21 #
22 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 # ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25 # FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
26 # COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27 # INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
28 # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
30 # AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
31 # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
32 # OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 # SUCH DAMAGE.
34 #
35 # $DragonFly: src/etc/pam.d/convert.sh,v 1.1 2005/07/22 18:20:43 joerg Exp $
36
37 if [ $# -ge 1 ]
38 then
39         dir="$1"
40 else
41         dir=/etc/pam.d
42 fi
43 if [ $# = 2 ]
44 then
45         file="$2"
46 else
47         file=/etc/pam.conf
48 fi
49 if [ $# -gt 2 ]
50 then
51         echo "Usage: $0 [ output directory ] [ input file ]"
52         echo "Default output is /etc/pam.d, default input is /etc/pam.conf"
53         exit 1
54 fi
55
56 awk '/^([#[:space:]]*)([[:alnum:]_]+)[[:space:]]+(auth|account|session|password)[[:space:]]+([^[:space:]].*)$/ {
57         match($0, /[#[:space:]]*/)
58         prefix = substr($0, 0, RLENGTH)
59         $0 = substr($0, RLENGTH + 1)
60         match($0, /[[:alnum:]_]+/)
61         name = substr($0, 0, RLENGTH)
62         $0 = substr($0, RLENGTH + 1)
63         match($0, /[[:space:]]+/)
64         $0 = substr($0, RLENGTH + 1)
65         match($0, /(auth|account|session|password)/)
66         type = substr($0, 0, RLENGTH)
67         $0 = substr($0, RLENGTH + 1)
68         match($0, /[[:space:]]+/)
69         arg = substr($0, RLENGTH + 1)
70
71         line = prefix type
72         tabs = ((16 - length(line)) / 8)
73         for (i = 0; i < tabs; i++)
74                 line = line "\t"
75         if ((name, type) in content)
76                 content[name, type] = content[name, type] "\n" line arg
77         else
78                 content[name, type] = line arg
79         services[name] = name
80 }
81
82 END {
83         'fdir=\"$dir\"'
84
85         split("auth account session password", types, " ")
86         for (service in services) {
87                 fname = fdir "/" service
88                 system("rm -f " fname)
89                 print "#\n# $DragonFly: src/etc/pam.d/convert.sh,v 1.1 2005/07/22 18:20:43 joerg Exp $\n#\n# PAM configuration for the \"" service "\" service\n#\n" >> fname
90                 for (type in types)
91                         if ((service, types[type]) in content)
92                                 print content[service, types[type]] >> fname
93                 close(fname)
94         }
95 }' < $file