File: setup.php

package info (click to toggle)
lwat 0.17-4
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 860 kB
  • ctags: 945
  • sloc: php: 3,151; sh: 212; makefile: 148
file content (368 lines) | stat: -rw-r--r-- 14,639 bytes parent folder | download | duplicates (2)
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
<?php
/*
    LWAT, LDAP Webbased Administration Tool
    Copyright (C) 2007 Finn-Arne Johansen <faj@bzz.no> BzzWare AS, Norway

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License along
    with this program; if not, write to the Free Software Foundation, Inc.,
    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/

/***************************************************************************
 * parse_samba_conf - parses the smb.conf on the machin lwat is running from
 * check_samba_conf - Check if samba is configured somewhat correctly
 * check_or_create - Check if some objects exists, or try to create them
 ***************************************************************************/

/***************************************************************************
 * parse_samba_conf - parses the smb.conf on the machin lwat is running from
 ***************************************************************************/
function parse_samba_conf () {
    $sambaconffile= @file ('/etc/samba/smb.conf') ;
    $section = "empty";
    $sambaconf = array () ; 
    if (empty ($sambaconffile)) return $sambaconf ; 
    foreach ($sambaconffile as $lines) {
        $newline = trim ($lines) ; 
        switch ($newline[0]) {
            case "#":
            case ";":
            case ""; 
                break ;
            case "["; 
                $section = strtolower(substr($newline, 1, strlen($newline) - 2)) ; 
                $sambaconf[$section] = array () ; 
                break ;
            default:
		list ($key, $value) = explode (" = ", $newline, 2); 
		$sambaconf[$section][strtolower(trim ($key))] = trim ($value) ; 
	        break ;
	}
    }
    return $sambaconf ; 
}

/***************************************************************************
 * check_samba_conf - Check if samba is configured somewhat correctly
 *****************
 * Arguments
 *      ldap    - ldap connection
 *      base    - search base for ldap queries
 *      authenticated - True if there is already an authenticated connection
 *      foundbase - If base is found in the ldap db
 *      smbAdmPW - Samba Adminstrator password 
 *****************
 * return imidiatly if this function has already run
 * Set variable to remember that function already has run
 * Parse local samba config (may it can help us ...) 
 * set up search for ldap information from ldapDB
 ***************************************************************************/
function check_samba ($ldap, $base, $authenticated,
                          $foundbase = true, $smbAdmPW) {
    global $samba_checked, $ldaphost, $groupprefix, $hostprefix; 
    if ($samba_checked) return ; 
    $samba_checked = true ; 
    $sambaconf = parse_samba_conf () ; 
    $want = array ('sambaDomainName', 'sambaSID') ; 
    $filter = ('objectClass=sambaDomain') ; 
    $result = ldap_search ($ldap, $base,  $filter, $want) ; 
    $entries = ldap_get_entries ($ldap, $result) ; 
    $workgroup = $sambaconf["global"]["workgroup"] ; 
    switch ($entries[count]) {
        case 1:
	    $workgroup = $entries[0]['sambadomainname'][0] ; 
	    $sambaSID = $entries[0]['sambasid'][0] ; 
	    break ;
    }
    $ldapSambaAdmin = $sambaconf["global"]["ldap admin dn"] ; 
    $want = array ("cn", "description") ; 
    $filter = "(&(objectClass=simpleSecurityObject)(description=samba administrator))" ; 
    $result = ldap_search ($ldap, $base,  $filter, $want) ; 
    if ($result) {
	$entries = ldap_get_entries ($ldap, $result) ; 
	$ldapSambaAdmin = $entries[0]["dn"]; 
	$found = true ; 
    }
    if (!$ldapSambaAdmin) {
	$ldapSambaAdmin = "cn=smbadmin,$base" ; 
	$found=false ; 
    } 
    if ($found) {
        $want = array ("cn", "description") ; 
        list ( $filter, $rest) = explode (",", $ldapSambaAdmin, 2) ; 
	$result = ldap_search ($ldap, $rest, $filter, $want) ; 
	if ($result) {
	    $entries = ldap_get_entries ($ldap, $result) ; 
	    $ldapSambaAdmin = $entries[0]["dn"]; 
	} else {
	    unset ($entries) ; 
	}
	if (!$ldapSambaAdmin) {
	    $found=false ; 
	} else $found=true ; 
    }

    printf ("<TR>\n") ; 
    printf ("<FORM method=post>\n") ;
    printf ("<TD>Samba LDAP admin:</TD>\n"); 

    if ($found) {
        printf ("<TD>%s</TD>\n", $ldapSambaAdmin) ;
        printf ("<TD>OK</TD>\n") ; 
	printf ("<TD>\n"); 
        if (empty ($smbAdmPW)) {
	    printf ("<INPUT type=Hidden name=create value='smbAdmPW'>\n") ; 
	    printf ("<INPUT type=Hidden name=sambaAdmin value='%s'>\n", 
	             $ldapSambaAdmin) ;
	    printf ("<input type=submit value='%s'>", _("Generate new password")) ; 
	} else
	    printf ("Passwd: <INPUT type=text value='%s' readonly>\n", 
	            $smbAdmPW) ; 
	printf ("</TD>\n"); 
    } elseif ($authenticated) {
	printf ("<TD><FORM method=post>\n") ;
	printf ("<INPUT type=Hidden name=create value='ldapSambaAdmin'>\n") ; 
	printf ("<INPUT type=text name=sambaAdmin value='%s'>\n", $ldapSambaAdmin) ; 
        printf ("</TD>\n") ; 
        printf ("<TD>\n") ; 
        printf ("<input type=submit value='%s'>", _("Create")) ; 
        printf ("</TD>\n") ; 
    } else {
        printf ("<TD/><TD>%s</TD>\n", _("Not found, please log in")); 
        $foundbase = false ; 
    }
    printf ("</FORM>\n") ; 
    printf ("</TR>\n") ; 

    $want = array("sambaDomainName", "sambaSID") ; 
    $filter = "sambaDomainname=$workgroup" ; 
    $result = ldap_search ($ldap, $base,  $filter, $want) ; 
    if ($result) {
        $entries = ldap_get_entries ($ldap, $result) ; 
    } else {
        unset ($entries) ; 
    }
    printf ("<TR>\n") ; 
    printf ("<FORM method=post>\n") ;
    printf ("<TD>Samba Domain/SID:</TD>\n"); 

    if ($entries[count]) {
        printf ("<TD>%s/%s</TD>\n", $entries[0]["sambadomainname"][0], $entries[0]["sambasid"][0]) ;
        printf ("<TD>OK</TD>\n") ; 
    } elseif ($authenticated) {
	printf ("<TD><FORM method=post>\n") ;
	printf ("<INPUT type=Hidden name=create value='sambaDomainName'>\n") ; 
	printf ("<INPUT type=text name=domainName value='%s'>\n", $workgroup) ; 
	printf ("<INPUT type=text name=sid value=''>\n") ; 
        printf ("</TD>\n") ; 
        printf ("<TD>\n") ; 
        printf ("<input type=submit value='%s'>", _("Create")) ; 
        printf ("</TD>\n") ; 
    } else {
        printf ("<TD/><TD>%s</TD>\n", _("Not found, please log in")); 
        $foundbase = false ; 
    }
    printf ("</FORM>\n") ; 
    printf ("</TR>\n") ; 
    printf ("<TR>\n") ; 
    printf ("<TD colspan=2>ldap config for /etc/samba/smb.conf</TD>\n") ; 
    printf ("</TR>\n") ; 
    printf ("<TR>\n") ; 
    printf ("<TD/>\n") ; 
    printf ("<TD>\n") ; 
    printf ("passdb backend = ldapsam:ldap://%s<BR/>\n", $ldaphost) ; 
    printf ("ldap suffix = %s<BR/>\n", $base) ; 
    printf ("ldap group suffix = %s<BR/>\n", $groupprefix) ; 
    printf ("ldap machine suffix = %s<BR/>\n", $hostprefix) ; 
    printf ("ldap admin dn = %s<BR/>\n", $ldapSambaAdmin) ;
    printf ("ldap ssl = start_tls<BR/>\n") ; 
    printf ("</TD>\n") ; 
    printf ("</TR>\n") ; 
}

/***************************************************************************
 * check_or_create - Check if some objects exists, or try to create them
 ***************************************************************************/
function check_or_create ($ldap, $base, $check, $prompt, $authenticated,
                          $foundbase = true) {
    printf ("<TR>\n") ; 
    if (empty($check)) {
	printf ("<TD>%s:</TD><TD>%s</TD>\n", $prompt, $base) ; 
	$filter = substr ($base, 0, strpos ($base, ",")) ; 
    } else {
	printf ("<TD>%s:</TD><TD>%s</TD>\n", $prompt, $check) ; 
	$filter = $check ; 
    }

    $want = array(substr ($filter, 0, strpos ($filter, "="))) ; 
    $result = ldap_search ($ldap, $base,  $filter, $want) ; 
    if ($result) {
        $entries = ldap_get_entries ($ldap, $result) ; 
    } else {
        unset ($entries) ; 
    }

    if (empty ($check)) 
        $check = $base ; 
    else
        $check = sprintf ("%s,%s", $check, $base) ; 
    if ($entries[0]["dn"] == $check) 
        printf ("<TD>OK</TD>\n") ; 
    elseif (!$foundbase) printf ("<TD>no base, unable to check</TD>") ; 
    elseif ($authenticated) {
	printf ("<TD><FORM method=post>\n") ;
	printf ("<INPUT type=Hidden name=create value='%s'>\n", $prompt) ; 
	printf ("<INPUT type=Hidden name=value value='%s'>\n", $filter) ; 
	printf ("<INPUT type=Hidden name=dn value='%s'>\n", $check) ; 
        printf ("<input type=submit value='%s'>", _("Create")) ; 
        printf ("</FORM></TD>\n") ; 
    } else {
        printf ("<TD>%s</TD>\n", _("Not found, please log in")); 
        $foundbase = false ; 
    }
    printf ("</TR>\n") ; 
    return $foundbase ; 
}

    require_once ('functions.php') ; 
    require('/usr/share/php/smarty/libs/Smarty.class.php') ; 

    ob_start () ; 
    loadConfig () ; 
    loadLocale () ; 

    session_start() ;
    $key = lwat_key () ; 
    $ldap = lwat_ldap (); 
    $authenticated = lwat_login ($ldap, readkey("admindn"), 
                                 readkey ("adminpw"), $key) ; 

    switch (readkey("create")) {
        case "base":
            $info["objectClass"] = array ("top", "dcObject", "organization") ; 
            $info["o"] = $base ; 
            $cn = array_slice (ldap_explode_dn($base,0), 1,1); 
            $cn = explode ("=", $cn[0]) ; 
            $info[$cn[0]] = $cn[1] ; 
            ldap_add ($ldap, $base, $info) ; 
            break ; 
        case "groupprefix":
        case "authprefix":
        case "hostprefix":
        case "netgroupprefix":
        case "automountprefix":
        case "variablesprefix":
        case "From template":
            $info["objectClass"] = array ("top", "organizationalUnit") ; 
            $value = explode ("=", readkey("value")) ; 
            $info[$value[0]] = $value[1] ; 
            if (!ldap_add ($ldap, readkey ("dn"), $info))
                debug (array ('dn' => readkey("dn"), 'info' => $info)) ; 
            break ; 
        case "smbAdmPW":
            $sambaAdmin = readkey ('sambaAdmin') ; 
            $smbAdmPW = pwgen () ; 
            $info["userPassword"] = cryptgen($smbAdmPW) ; 
            if (!ldap_modify ($ldap, "$sambaAdmin", $info))
                debug (array ('dn' => "cn=$sambaAdmin", 'info' => $info)) ; 
            break ; 
        case "ldapSambaAdmin":
            $smbAdmPW = pwgen () ; 
            $info["objectClass"] = array ("top", "organizationalRole", "simpleSecurityObject") ; 
            $sambaAdmin = readkey ("sambaAdmin") ; 
            $info["description"] = "Samba Administrator" ; 
            $info["userPassword"] = cryptgen($smbAdmPW) ; 
            $info["cn"] = substr($sambaAdmin, 3, strpos ($sambaAdmin, "," ) - 3) ; 
            if (!ldap_add ($ldap, "$sambaAdmin", $info))
                debug (array ('dn' => "cn=$sambaAdmin", 'info' => $info)) ; 
            break ; 
        case "sambaDomainName":
            $info["objectClass"] = array ("sambaDomain") ; 
            $info["sambaAlgorithmicRidbase"] = "1000" ; 
            $domainName = readkey ("domainName") ; 
            $info["sambaDomainName"] = $domainName ; 
            $info["sambaSID"] = readkey ("sid"); 
            if (!ldap_add ($ldap, "sambaDomainName=$domainName,$base", $info))
                debug (array ('dn' => "sambaDomainName=$domainName,$base", 'info' => $info)) ; 
            break ; 
        case "": 
            break ;
        default:
	    debug ($_POST) ; 
    }

    $want = array (dn) ; 
    $basedn = (ldap_explode_dn ($base, 0)) ; 

    printf ("<TABLE>\n") ; 
    $foundbase = check_or_create ($ldap, $base, "", "base", $authenticated) ; 
    if (isset ($groupprefix)) 
	check_or_create ($ldap, $base, $groupprefix, "groupprefix",
	                 $authenticated, $foundbase) ;
    if ($useLisGroup === false && isset ($authprefix))
	check_or_create ($ldap, $base, $authprefix, "authprefix",
			 $authenticated, $foundbase) ;
    if (isset ($hostprefix))
	check_or_create ($ldap, $base, $hostprefix, "hostprefix",
			 $authenticated, $foundbase) ;
    if (isset ($netgroupprefix)) 
	check_or_create ($ldap, $base, $netgroupprefix, "netgroupprefix",
			 $authenticated, $foundbase) ;
    printf ("<TR><TD>useLisGroup: </TD><TD>%s</TD></TR>\n", ($useLisGroup ? "true" : "false") ) ; 
    if (isset ($variablesprefix)) 
	check_or_create ($ldap, $base, $variablesprefix,
			 "variablesprefix", $authenticated, $foundbase) ;
    if (isset ($automountprefix))
	check_or_create ($ldap, $base, $automountprefix,
			 "automountprefix", $authenticated, $foundbase) ;
    $templates = parse_ini_file ('/etc/lwat/admin.ini', true) ; 
    foreach ($templates as $name => $template) {
	$found=strpos ($template["objectClass"], "sambaSamAccount") ; 
        if ($found !== false)
	    check_samba ($ldap, $base, $authenticated, $foundbase, $smbAdmPW) ;
    }
    foreach ($templates as $name => $template) {
        $found=strpos ($template["ou"], ",%base%") ; 
        if ($found !== false)
	    check_or_create ($ldap, $base, substr ($template["ou"], 0,$found), 
			 "From template", $authenticated, $foundbase) ;
    }
    printf ("</TABLE>\n") ; 

    printf ("<form name=login method=post>\n") ; 
    $foundadmin = $authenticated ; 
    while (!$foundadmin && !empty($base)) {
        $filter = "objectClass=simpleSecurityObject" ; 
        $want = array ("cn") ; 
        $result = @ldap_search ($ldap, $base, $filter, $want) ; 
        if ($result) 
            $foundadmin=true ; 
        else {
	    $base = ldap_explode_dn ($base,0) ;
	    array_splice ($base, 0,2) ; 
	    $base = implode (",", $base) ; 
	}
    }

    if (!$authenticated && $foundadmin) {
	printf ("<input type=hidden name=adminbase value=%s>\n", $base) ; 
	printf ("<table>\n") ; 
	printf ("<TR><TD>Login</TD><TD><input type=text name=admindn></TD></TR>\n"); 
	printf ("<TR><TD>Password</TD><TD><input type=password name=adminpw></TD></TR>\n"); 
	printf ("<TR><TD /><TD><INPUT type=submit name=submit value=%s><INPUT type=reset value=%s></TD></TR>\n", _("Login"), _("Reset")); 
	printf ("</TABLE>") ; 
	printf ("</form>\n") ; 
    }
    ldap_close ($ldap);

?>