Debian Codegraph @

templates/initsuff.php

raw
1 <?php
2 /*
3
4 This code is part of LDAP Account Manager (http://www.ldap-account-manager.org/)
5 Copyright (C) 2003 - 2026 Roland Gruber
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20
21 */
22
23 /**
24 * Creates main suffixes if they are missing.
25 *
26 * @author Roland Gruber
27 * @package main
28 */
29
30 /** security functions */
31 include_once __DIR__ . "/../lib/security.inc";
32 /** access to configuration settings */
33 include_once __DIR__ . "/../lib/config.inc";
34 /** LDAP access */
35 include_once __DIR__ . "/../lib/ldap.inc";
36 /** status messages */
37 include_once __DIR__ . "/../lib/status.inc";
38
39 // start session
40 startSecureSession();
41 enforceUserIsLoggedIn();
42
43 if (!checkIfWriteAccessIsAllowed()) {
44 die();
45 }
46
47 setlanguage();
48
49 if (!empty($_POST)) {
50 validateSecurityToken();
51 }
52 $newSuffixes = $_SESSION['missingSuffixes'] ?? [];
53
54 // check if user already pressed button
55 $failedDNs = [];
56 if (isset($_POST['add_suff']) || isset($_POST['cancel'])) {
57 if (isset($_POST['add_suff'])) {
58 // add entries
59 foreach ($newSuffixes as $newSuffix) {
60 // check if entry is already present
61 $info = @ldap_read($_SESSION['ldap']->server(), $newSuffix, "(objectclass=*)", ['dn'], 0, 0, 0, LDAP_DEREF_NEVER);
62 $res = false;
63 if ($info !== false) {
64 $res = ldap_get_entries($_SESSION['ldap']->server(), $info);
65 }
66 if ($res) {
67 continue;
68 }
69 $suff = $newSuffix;
70 // generate DN and attributes
71 $tmp = explode(",", $suff);
72 $name = explode("=", $tmp[0]);
73 array_shift($tmp);
74 $end = implode(",", $tmp);
75 if ($name[0] !== "ou") { // add root entry
76 $attr = [];
77 $attr[$name[0]] = $name[1];
78 $attr['objectClass'] = 'organization';
79 $dn = $suff;
80 if (!@ldap_add($_SESSION['ldap']->server(), $dn, $attr)) {
81 $failedDNs[$suff] = ldap_error($_SESSION['ldap']->server());
82 }
83 }
84 else { // add organizational unit
85 $name = $name[1];
86 $attr = [];
87 $attr['objectClass'] = "organizationalunit";
88 $attr['ou'] = $name;
89 $dn = $suff;
90 if (!@ldap_add($_SESSION['ldap']->server(), $dn, $attr)) {
91 // check if we have to add parent entries
92 if (ldap_errno($_SESSION['ldap']->server()) === 32) {
93 $dnParts = explode(",", $suff);
94 $subsuffs = [];
95 // make list of subsuffixes
96 $dnPartsCount = count($dnParts);
97 for ($k = 0; $k < $dnPartsCount; $k++) {
98 $part = explode("=", $dnParts[$k]);
99 $subsuffs[] = implode(",", array_slice($dnParts, $k));
100 if ($part[0] !== "ou") {
101 break;
102 }
103 }
104 // create missing entries
105 $subsuffCount = count($subsuffs);
106 for ($k = $subsuffCount - 1; $k >= 0; $k--) {
107 // check if subsuffix is present
108 $info = @ldap_read($_SESSION['ldap']->server(), $subsuffs[$k], "(objectclass=*)", ['dn'], 0, 0, 0, LDAP_DEREF_NEVER);
109 $res = false;
110 if ($info !== false) {
111 $res = ldap_get_entries($_SESSION['ldap']->server(), $info);
112 }
113 if (!$res) {
114 $suffarray = explode(",", $subsuffs[$k]);
115 $headarray = explode("=", $suffarray[0]);
116 $attr = [];
117 if ($headarray[0] === "ou") { // add ou entry
118 $attr['objectClass'] = 'organizationalunit';
119 $attr['ou'] = $headarray[1];
120 }
121 else { // add root entry
122 $attr['objectClass'][] = 'organization';
123 $attr[$headarray[0]] = $headarray[1];
124 if ($headarray[0] === "dc") {
125 $attr['o'] = $headarray[1];
126 $attr['objectClass'][] = 'dcObject';
127 }
128 }
129 $dn = $subsuffs[$k];
130 if (!@ldap_add($_SESSION['ldap']->server(), $dn, $attr)) {
131 $failedDNs[$suff] = ldap_error($_SESSION['ldap']->server());
132 break;
133 }
134 }
135 }
136 }
137 else {
138 $failedDNs[$suff] = ldap_error($_SESSION['ldap']->server());
139 }
140 }
141 }
142 }
143 }
144 include __DIR__ . '/../lib/adminHeader.inc';
145 // print error/success messages
146 if (isset($_POST['add_suff'])) {
147 if ($failedDNs !== []) {
148 // print error messages
149 foreach ($failedDNs as $suffix => $error) {
150 StatusMessage("ERROR", _("Failed to create entry!") . "<br>" . htmlspecialchars($error), htmlspecialchars($suffix));
151 }
152 }
153 else {
154 // print success message
155 StatusMessage("INFO", "", _("All changes were successful."));
156 unset($_SESSION['missingSuffixes']);
157 }
158 }
159 else {
160 // no suffixes were created
161 StatusMessage("INFO", "", _("No changes were made."));
162 }
163 include __DIR__ . '/../lib/adminFooter.inc';
164 exit;
165 }
166
167 // first show of page
168 include __DIR__ . '/../lib/adminHeader.inc';
169 echo '<div class="smallPaddingContent">';
170 echo "<form action=\"initsuff.php\" method=\"post\">\n";
171 $container = new htmlResponsiveRow();
172 $container->add(new htmlOutputText(_("The following suffixes are missing in LDAP. LAM can create them for you.")));
173 $container->add(new htmlOutputText(_("You can setup the LDAP suffixes for all account types in your LAM server profile on tab \"Account types\".")));
174 $container->addVerticalSpacer('1rem');
175 // print missing suffixes
176 foreach ($newSuffixes as $newSuffix) {
177 $container->add(new htmlOutputText($newSuffix));
178 }
179 $container->addVerticalSpacer('2rem');
180
181 $buttonContainer = new htmlGroup();
182 $buttonContainer->addElement(new htmlButton('add_suff', _("Create")));
183 $buttonContainer->addElement(new htmlButton('cancel', _("Cancel")));
184 $container->add($buttonContainer);
185 addSecurityTokenToMetaHTML($container);
186
187 parseHtml(null, $container, [], false, 'user');
188
189 echo "</form><br>\n";
190 echo "</div>\n";
191 include __DIR__ . '/../lib/adminFooter.inc';