[gosa.git] / gosa-plugins / mit-krb5 / admin / systems / services / kerberos / class_goKrbServer.inc
1 <?php
2 /*
3 This code is part of GOsa (https://gosa.gonicus.de)
4 Copyright (C) 2008 Fabian Hickert
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
22 /*! brief The kerberos service class
23 */
24 class goKrbServer extends goService{
26 var $cli_summary = "This pluign is used within the ServerService Pluign \nand indicates that this server supports NTP service.";
27 var $cli_description = "Some longer text\nfor help";
28 var $cli_parameters = array("eins" => "Eins ist toll", "zwei" => "Zwei ist noch besser");
30 /* This plugin only writes its objectClass */
31 var $objectclasses = array("goKrbServer");
32 var $attributes = array("goKrbRealm");
33 var $StatusFlag = "goKrbServerStatus";
35 /* This class can't be assigned twice so it conflicts with itsself */
36 var $conflicts = array("goKrbServer");
38 var $DisplayName = "";
39 var $dn = NULL;
40 var $goKrbServerStatus= "";
41 var $cn = "";
42 var $goKrbRealm = "";
43 var $view_logged = FALSE;
45 var $policies = array();
46 var $id = -1;
47 var $macAddress = "" ;
50 /*! \brief Initializes the kerberos service
51 @param Object GOsa configuration object.
52 @param String The object dn we are currently editing.
53 @param Object The parent object.
54 */
55 public function goKrbServer(&$config,$dn,$parent)
56 {
57 goService::goService($config,$dn);
58 $this->DisplayName = _("Kerberos service");
59 $this->parent = $parent;
61 /* Detect macAddress of this device
62 */
63 $this->macAddress = "";
64 if(isset($this->parent->parent->netConfigDNS->macAddress)){
65 $this->macAddress = &$this->parent->parent->netConfigDNS->macAddress;
66 }
68 /* Get configured policies
69 */
70 if(!empty($this->macAddress) && $this->is_account){
71 $o = new gosaSupportDaemon();
72 $tmp = $o->krb5_list_policies($this->macAddress);
73 if($o->is_error()){
74 msg_dialog::display(_("Service infrastructure"),msgPool::siError($o->get_error()),ERROR_DIALOG);
75 }else{
76 $this->policies = array();
77 foreach($tmp as $policy){
78 $this->policies[] = array("NAME" => $policy,"STATUS" => "LOADED","DATA" => array());
79 }
80 }
81 }
82 }
85 /*! \brief Create HTML output
86 @return String HMTL output.
87 */
88 public function execute()
89 {
90 $smarty = get_smarty();
91 $smarty->assign("MIT_KRB", class_available("passwordMethodMIT"));
93 if(!$this->view_logged){
94 $this->view_logged = TRUE;
95 new log("view","server/".get_class($this),$this->dn);
96 }
98 /*******
99 Display sub dialogs
100 *******/
102 /* CANCEL Policy dialog
103 */
104 if(isset($_POST['cancel_policy'])){
105 $this->dialog = NULL;
106 }
108 /* SAVE Policy dialog
109 */
110 if($this->dialog instanceof krb5_policy && isset($_POST['save_policy'])){
111 $this->dialog->save_object();
112 $msgs = $this->dialog->check();
113 if(count($msgs)){
114 msg_dialog::displayChecks($msgs);
115 }else{
116 $this->AddPolicy($this->id,$this->dialog->save());
117 $this->dialog = NULL;
118 }
119 }
121 /* DISPLAY policy dialog
122 */
123 if($this->dialog instanceof krb5_policy){
124 $this->dialog->save_object();
125 return($this->dialog->execute());
126 }
128 /*******
129 Create HTML output for this plugin
130 *******/
132 $tmp = $this->plinfo();
133 foreach($tmp['plProvidedAcls'] as $name => $translation){
134 $smarty->assign($name."ACL",$this->getacl($name));
135 }
137 foreach($this->attributes as $attr){
138 $smarty->assign($attr,$this->$attr);
139 }
141 $divlist = new divSelectBox("Kerberos Policies");
142 $action = "<input type='image' src='images/lists/edit.png' class='center' name='policy_edit_%id'>";
143 $action .= "<input type='image' src='images/lists/trash.png' class='center' name='policy_del_%id'>";
144 foreach($this->policies as $key => $policy){
145 if($policy['STATUS'] == "REMOVED") continue;
147 $actions = preg_replace("/%id/",$key,$action);
148 $field1 = array("string" => $policy['NAME']);
149 $field3 = array("string" => $actions,
150 "attach"=>"style='width:40px; text-align:right; border:0px;'");
152 $divlist->AddEntry(array($field1,$field3));
153 }
155 $smarty->assign("divlist",$divlist->DrawList());
156 return($smarty->fetch(get_template_path("goKrbServer.tpl",TRUE,dirname(__FILE__))));
157 }
160 /* \brief Return serice informations, which will be shown in the service overview.
161 @return Array Some service information.
162 */
163 public function getListEntry()
164 {
165 $fields = goService::getListEntry();
166 $fields['Message'] = _("Kerberos service (kadmin access informations)");
167 #$fields['AllowEdit'] = true;
168 return($fields);
169 }
172 /*! \brief Checks if all given values are valid
173 @return Array An array containing all error messages.
174 */
175 public function check()
176 {
177 $message = plugin::check();
178 if (empty($this->goKrbRealm)){
179 $message[]= msgPool::required(_("Realm"));
180 }
181 if(count($this->policies) && (empty($this->macAddress) || !tests::is_mac($this->macAddress))){
182 $message[] = _("Cannot apply policy changes! The defined kerberos server has no mac address.");
183 }
184 return($message);
185 }
188 /*! \brief Adds or updated a given policy.
189 @param Integer ID The policy to update, or -1 if it is a new one.
190 @param Array The policy settings.
191 */
192 public function AddPolicy($id,$policy)
193 {
194 if($id != -1 && $this->policies[$id]['NAME'] == $policy['NAME']){
196 /* Policy was edited
197 */
198 if($this->policies[$id]['STATUS'] == "LOADED"){
199 $policy['STATUS'] = "EDITED";
200 }else{
201 $policy['STATUS'] = $this->policies[$id]['STATUS'];
202 }
203 $this->policies[$this->id] = $policy;
205 }elseif($id != -1 && $this->policies[$id]['NAME'] != $policy['NAME']){
207 /* Policy was renamed, remove old and add new policy
208 */
209 $this->RemovePolicy($id);
210 $this->AddPolicy(-1,$policy);
211 }else{
213 /* Policy was added
214 */
215 $name = $policy['NAME'];
217 /* Check if there is already a policy with this name
218 which was removed before
219 */
220 foreach($this->policies as $pid => $entry){
221 if($entry['NAME'] == $name && $entry['STATUS'] == "REMOVED"){
222 $id = $pid;
223 break;
224 }
225 }
227 /* Update existing policy or create new one
228 */
229 if(isset($this->policies[$id])){
230 $policy['STATUS'] = "EDITED";
231 $this->policies[$id] = $policy;
232 }else{
233 $policy['STATUS'] = "ADDED";
234 $this->policies[] = $policy;
235 }
236 }
237 }
240 /*! \brief Returns all used policy names.
241 @return Array A list of used policy names.
242 */
243 public function getPolicyNames()
244 {
245 $ret = array();
246 foreach($this->policies as $policy){
247 if($policy['STATUS'] == "REMOVED") continue;
248 $ret[] = $policy['NAME'];
249 }
250 return($ret);
251 }
254 /*! \brief Marks a policy as remvoed
255 @param Integer the Id of the policy to remove
256 */
257 public function RemovePolicy($id)
258 {
259 /* Load policy information, if not done before
260 */
261 if($this->policies[$id]['STATUS'] == "LOADED" && empty($this->policies[$id]['DATA'])){
262 $o = new gosaSupportDaemon();
263 $this->policies[$id]['DATA'] = $o->krb5_get_policy($this->macAddress,$this->policies[$id]['NAME']);
264 }
265 if($this->policies[$id]['DATA']['POLICY_REFCNT']){
266 msg_dialog::display(_("Remove"),msgPool::stillInUse(_("Policy")),ERROR_DIALOG) ;
267 }else{
268 if($this->policies[$id]['STATUS'] == "ADDED"){
269 unset($this->policies[$id]);
270 }else{
271 $this->policies[$id]['STATUS'] = "REMOVED";
272 }
273 }
274 }
277 /*! \brief Save POSTed html variables
278 */
279 public function save_object()
280 {
281 if(isset($_POST['goKrbServerPosted'])){
282 plugin::save_object();
284 /* Add new Policy requested
285 */
286 if(isset($_POST['policy_add']) && class_available("krb5_policy")){
287 $this->dialog = new krb5_policy($this->config,array(),$this);
288 $this->id = -1;
289 }
291 /* Walk through POSTs an check for image-button posts
292 */
293 foreach($_POST as $name => $value){
295 /* Remove policy requested
296 */
297 if(preg_match("/^policy_del/",$name)){
298 $id = preg_replace("/^policy_del_([0-9]*)_.*/i","\\1",$name);
299 if(isset($this->policies[$id])){
300 $this->RemovePolicy($id);
301 }
302 break;
303 }
305 /* Edit Policy requested
306 */
307 if(preg_match("/^policy_edit/",$name)){
308 $id = preg_replace("/^policy_edit_([0-9]*)_.*/i","\\1",$name);
309 if(isset($this->policies[$id])){
311 /* Load policy information, if not done before
312 */
313 if($this->policies[$id]['STATUS'] == "LOADED" && empty($this->policies[$id]['DATA'])){
314 $o = new gosaSupportDaemon();
315 $this->policies[$id]['DATA'] = $o->krb5_get_policy($this->macAddress,$this->policies[$id]['NAME']);
316 }
318 /* Open dialog */
319 $this->id = $id;
320 $this->dialog = new krb5_policy($this->config,$this->policies[$id], $this);
321 }
322 break;
323 }
324 }
325 }
326 }
329 /*! \brief Save changes to ldap
330 */
331 public function save()
332 {
333 goService::save();
335 /* Send policy changes back to the si daemon
336 */
337 $actions = array("del" => array(),"add_edit" => array());
338 foreach($this->policies as $policy){
340 /* Unset not used vars */
341 if(isset($policy['DATA']['MASK'])) unset($policy['DATA']['MASK']);
342 if(isset($policy['DATA']['POLICY_REFCNT'])) unset($policy['DATA']['POLICY_REFCNT']);
344 switch($policy['STATUS']){
345 case "REMOVED" : $actions['del'] [] = $policy;break;
346 case "ADDED" :
347 case "EDITED" : $actions['add_edit'] [] = $policy;break;
348 }
349 }
351 $o = new gosaSupportDaemon();
352 $policies = $o->krb5_list_policies($this->macAddress);
354 /* Send remove policy event
355 */
356 foreach($actions['del'] as $policy){
357 if(!in_array_strict($policy['NAME'],$policies)) continue;
358 if(!$o->krb5_del_policy($this->macAddress,$policy['NAME'])){
359 msg_dialog::display(_("Service infrastructure"),msgPool::siError($o->get_error()),ERROR_DIALOG);
360 }
361 }
363 /* Send add new/edit policy event
364 */
365 foreach($actions['add_edit'] as $policy){
366 if(in_array_strict($policy['NAME'],$policies)){
367 if(!$o->krb5_set_policy($this->macAddress,$policy['NAME'],$policy['DATA'])){
368 msg_dialog::display(_("Service infrastructure"),msgPool::siError($o->get_error()),ERROR_DIALOG);
369 }
370 }else{
371 if(!$o->krb5_add_policy($this->macAddress,$policy['NAME'],$policy['DATA'])){
372 msg_dialog::display(_("Service infrastructure"),msgPool::siError($o->get_error()),ERROR_DIALOG);
373 }
374 }
375 }
376 }
379 /*! \brief Return plugin informations for acl handling
380 @return Array ACL infos.
381 */
382 static function plInfo()
383 {
384 return (array(
385 "plShortName" => _("Kerberos"),
386 "plDescription" => _("Kerberos access information")." ("._("Services").")",
387 "plSelfModify" => FALSE,
388 "plDepends" => array(),
389 "plPriority" => 95,
390 "plSection" => array("administration"),
391 "plCategory" => array("server"),
393 "plProvidedAcls"=> array(
394 "goKrbPolicy" => _("Policies"),
395 "goKrbRealm" => _("Realm"))
396 ));
397 }
398 }
399 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
400 ?>