1 <?php
2 /*!
3 \brief posixAccount plugin
4 \author Cajus Pollmeier <pollmeier@gonicus.de>
5 \version 2.00
6 \date 24.07.2003
8 This class provides the functionality to read and write all attributes
9 relevant for posixAccounts and shadowAccounts from/to the LDAP. It
10 does syntax checking and displays the formulars required.
11 */
13 class posixAccount extends plugin
14 {
15 /* Definitions */
16 var $plHeadline= "UNIX";
17 var $plDescription= "This does something";
19 /* CLI vars */
20 var $cli_summary= "Manage users posix account";
21 var $cli_description= "Some longer text\nfor help";
22 var $cli_parameters= array("eins" => "Eins ist toll", "zwei" => "Zwei ist noch besser");
24 /* Plugin specific values */
25 var $homeDirectory= "";
26 var $loginShell= "/bin/bash";
27 var $uidNumber= "";
28 var $gidNumber= "";
29 var $gecos= "";
30 var $shadowMin= "0";
31 var $shadowMax= "0";
32 var $shadowWarning= "0";
33 var $shadowLastChange= "0";
34 var $shadowInactive= "0";
35 var $shadowExpire= "0";
36 var $gosaDefaultPrinter= "";
37 var $gosaDefaultLanguage= "";
38 var $accessTo= array();
39 var $trustModel= "";
41 var $glist=array();
42 var $status= "";
43 var $loginShellList= array();
44 var $groupMembership= array();
45 var $savedGroupMembership= array();
46 var $savedUidNumber= "";
47 var $savedGidNumber= "";
48 var $use_shadowMin= "0";
49 var $use_shadowMax= "0";
50 var $use_shadowWarning= "0";
51 var $use_shadowInactive= "0";
52 var $use_shadowExpire= "0";
53 var $must_change_password= "0";
54 var $force_ids= 0;
55 var $printerList= array();
56 var $group_dialog= FALSE;
57 var $show_ws_dialog= FALSE;
58 var $secondaryGroups= array();
59 var $primaryGroup= 0;
60 var $was_trust_account= FALSE;
62 var $grouplist = array();
63 var $ui = array();
65 /* attribute list for save action */
66 var $attributes= array("homeDirectory", "loginShell", "uidNumber", "gidNumber", "gecos",
67 "shadowMin", "shadowMax", "shadowWarning", "shadowInactive", "shadowLastChange",
68 "shadowExpire", "gosaDefaultPrinter", "gosaDefaultLanguage", "uid","accessTo","trustModel");
69 var $objectclasses= array("posixAccount", "shadowAccount");
72 /* constructor, if 'dn' is set, the node loads the given
73 'dn' from LDAP */
74 function posixAccount ($config, $dn= NULL)
75 {
76 /* Configuration is fine, allways */
77 $this->config= $config;
79 /* Load bases attributes */
80 plugin::plugin($config, $dn);
82 $ldap= $this->config->get_ldap_link();
84 if ($dn != NULL){
86 /* Correct is_account. shadowAccount is not required. */
87 if (isset($this->attrs['objectClass']) &&
88 in_array ('posixAccount', $this->attrs['objectClass'])){
90 $this->is_account= TRUE;
91 }
93 /* Is this account a trustAccount? */
94 if ($this->is_account && isset($this->attrs['trustModel'])){
95 $this->trustModel= $this->attrs['trustModel'][0];
96 $this->was_trust_account= TRUE;
97 } else {
98 $this->was_trust_account= FALSE;
99 $this->trustModel= "";
100 }
102 $this->accessTo = array();
103 if ($this->is_account && isset($this->attrs['accessTo'])){
104 for ($i= 0; $i<$this->attrs['accessTo']['count']; $i++){
105 $tmp= $this->attrs['accessTo'][$i];
106 $this->accessTo[$tmp]= $tmp;
107 }
108 }
109 $this->initially_was_account= $this->is_account;
111 /* Fill group */
112 $this->primaryGroup= $this->gidNumber;
114 /* Generate status text */
115 $current= date("U");
116 if (($current >= $this->shadowExpire) && $this->shadowExpire){
117 $this->status= "expired";
118 if (($this->shadowExpire - $current) < $this->shadowInactive){
119 $this->status.= ", grace time active";
120 }
121 } elseif (($this->shadowLastChange + $this->shadowMin) >= $current){
122 $this->status= "active, password not changable";
123 } elseif (($this->shadowLastChange + $this->shadowMax) >= $current){
124 $this->status= "active, password expired";
125 } else {
126 $this->status= "active";
127 }
129 /* Get group membership */
130 $ldap->cd($this->config->current['BASE']);
131 $ldap->search("(&(objectClass=posixGroup)(memberUid=".$this->uid."))", array("cn", "description"));
133 while ($this->attrs= $ldap->fetch()){
134 if (!isset($this->attrs["description"][0])){
135 $entry= $this->attrs["cn"][0];
136 } else {
137 $dsc= preg_replace ('/^Group of user/', _("Group of user"), $this->attrs["description"][0]);
138 $entry= $this->attrs["cn"][0]." [$dsc]";
139 }
140 $this->groupMembership[$ldap->getDN()]= $entry;
141 }
142 asort($this->groupMembership);
143 reset($this->groupMembership);
144 $this->savedGroupMembership= $this->groupMembership;
145 $this->savedUidNumber= $this->uidNumber;
146 $this->savedGidNumber= $this->gidNumber;
147 }
149 /* Adjust shadow checkboxes */
150 foreach (array("shadowMin", "shadowMax", "shadowWarning", "shadowInactive",
151 "shadowExpire") as $val){
153 if ($this->$val != 0){
154 $oval= "use_".$val;
155 $this->$oval= "1";
156 }
157 }
159 /* Convert to seconds */
160 if ($this->shadowExpire != 0){
161 $this->shadowExpire*= 60 * 60 * 24;
162 } else {
163 $date= getdate();
164 $this->shadowExpire= floor($date[0] / (60*60*24)) * 60 * 60 * 24;
165 }
167 /* Generate shell list from /etc/gosa/shells */
168 if (file_exists('/etc/gosa/shells')){
169 $shells = file ('/etc/gosa/shells');
170 foreach ($shells as $line){
171 if (!preg_match ("/^#/", $line)){
172 $this->loginShellList[]= trim($line);
173 }
174 }
175 } else {
176 if ($this->loginShell == ""){
177 $this->loginShellList[]= _("unconfigured");
178 }
179 }
181 /* Insert possibly missing loginShell */
182 if ($this->loginShell != "" && !in_array($this->loginShell, $this->loginShellList)){
183 $this->loginShellList[]= $this->loginShell;
184 }
186 /* Generate printer list */
187 if (isset($this->config->data['SERVERS']['CUPS'])){
188 $this->printerList= get_printer_list ($this->config->data['SERVERS']['CUPS']);
189 asort($this->printerList);
190 }
192 /* Generate group list */
193 $ldap->cd($this->config->current['BASE']);
194 $ldap->search("(objectClass=posixGroup)", array("cn", "gidNumber"));
195 $this->secondaryGroups[]= "- "._("automatic")." -";
196 while ($attrs= $ldap->fetch()){
197 $this->secondaryGroups[$attrs['gidNumber'][0]]= $attrs['cn'][0];
198 }
199 asort ($this->secondaryGroups);
201 /* Get global filter config */
202 if (!is_global("sysfilter")){
203 $ui= get_userinfo();
204 $base= get_base_from_people($ui->dn);
205 $sysfilter= array( "depselect" => $base,
206 "regex" => "*");
207 register_global("sysfilter", $sysfilter);
208 }
209 $this->ui = get_userinfo();
210 }
213 /* execute generates the html output for this node */
214 function execute()
215 {
216 /* Call parent execute */
217 plugin::execute();
219 /* Do we need to flip is_account state? */
220 if (isset($_POST['modify_state'])){
221 $this->is_account= !$this->is_account;
222 }
224 /* Do we represent a valid posixAccount? */
225 if (!$this->is_account && $this->parent == NULL ){
226 $display= "<img alt=\"\" src=\"images/stop.png\" align=\"middle\"> <b>".
227 _("This account has no unix extensions.")."</b>";
228 $display.= back_to_main();
229 return ($display);
230 }
232 $display= "";
234 /* Show tab dialog headers */
235 if ($this->parent != NULL){
236 if ($this->is_account){
237 if (isset($this->parent->by_object['sambaAccount'])){
238 $obj= $this->parent->by_object['sambaAccount'];
239 }
240 if (isset($obj) && $obj->is_account == TRUE &&
241 ((isset($this->parent->by_object['sambaAccount']))&&($this->parent->by_object['sambaAccount']->is_account))
242 ||(isset($this->parent->by_object['environment'] ))&&($this->parent->by_object['environment'] ->is_account)){
244 /* Samba3 dependency on posix accounts are enabled
245 in the moment, because I need to rely on unique
246 uidNumbers. There'll be a better solution later
247 on. */
248 $display= $this->show_header(_("Remove posix account"),
249 _("This account has unix features enabled. To disable them, you'll need to remove the samba / environment account first."), TRUE);
250 } else {
251 $display= $this->show_header(_("Remove posix account"),
252 _("This account has posix features enabled. You can disable them by clicking below."));
253 }
254 } else {
255 $display= $this->show_header(_("Create posix account"),
256 _("This account has posix features disabled. You can enable them by clicking below."));
257 return($display);
258 }
259 }
261 /* Trigger group edit? */
262 if (isset($_POST['edit_groupmembership'])){
263 $this->group_dialog= TRUE;
264 $this->dialog= TRUE;
265 }
267 /* Cancel group edit? */
268 if (isset($_POST['add_groups_cancel']) ||
269 isset($_POST['add_groups_finish'])){
270 $this->group_dialog= FALSE;
271 $this->dialog= FALSE;
272 }
274 /* Add selected groups */
275 if (isset($_POST['add_groups_finish']) && isset($_POST['groups']) &&
276 count($_POST['groups'])){
278 if (chkacl ($this->acl, "memberUid") == ""){
279 $this->addGroup ($_POST['groups']);
280 $this->is_modified= TRUE;
281 }
282 }
284 /* Delete selected groups */
285 if (isset($_POST['delete_groupmembership']) &&
286 isset($_POST['group_list']) && count($_POST['group_list'])){
288 if (chkacl ($this->acl, "memberUid") == ""){
289 $this->delGroup ($_POST['group_list']);
290 $this->is_modified= TRUE;
291 }
292 }
294 /* Add user workstation? */
295 if (isset($_POST["add_ws"])){
296 $this->show_ws_dialog= TRUE;
297 $this->dialog= TRUE;
298 }
300 /* Add user workstation? */
301 if (isset($_POST["add_ws_finish"]) && isset($_POST['wslist'])){
302 foreach($_POST['wslist'] as $ws){
303 $this->accessTo[$ws]= $ws;
304 }
305 ksort($this->accessTo);
306 $this->is_modified= TRUE;
307 }
309 /* Remove user workstations? */
310 if (isset($_POST["delete_ws"]) && isset($_POST['workstation_list'])){
311 foreach($_POST['workstation_list'] as $name){
312 unset ($this->accessTo[$name]);
313 }
314 $this->is_modified= TRUE;
315 }
317 /* Add user workstation finished? */
318 if (isset($_POST["add_ws_finish"]) || isset($_POST["add_ws_cancel"])){
319 $this->show_ws_dialog= FALSE;
320 $this->dialog= FALSE;
321 }
323 /* Templates now! */
324 $smarty= get_smarty();
326 /* Show ws dialog */
327 if ($this->show_ws_dialog){
328 /* Save data */
329 $sysfilter= get_global("sysfilter");
330 foreach( array("depselect", "regex") as $type){
331 if (isset($_POST[$type])){
332 $sysfilter[$type]= $_POST[$type];
333 }
334 }
335 if (isset($_GET['search'])){
336 $s= mb_substr($_GET['search'], 0, 1, "UTF8")."*";
337 if ($s == "**"){
338 $s= "*";
339 }
340 $sysfilter['regex']= $s;
341 }
342 register_global("sysfilter", $sysfilter);
344 /* Get workstation list */
345 $exclude= "";
346 foreach($this->accessTo as $ws){
347 $exclude.= "(cn=$ws)";
348 }
349 if ($exclude != ""){
350 $exclude= "(!(|$exclude))";
351 }
352 $acl= array($this->config->current['BASE'] => ":all");
353 $regex= $sysfilter['regex'];
354 $filter= "(&(|(objectClass=goServer)(objectClass=gotoWorkstation)(objectClass=gotoTerminal))$exclude(cn=*)(cn=$regex))";
355 $res= get_list($acl, "$filter", TRUE, $sysfilter['depselect'], array("cn"), TRUE);
356 $wslist= array();
357 foreach ($res as $attrs){
358 $wslist[]= preg_replace('/\$/', '', $attrs['cn'][0]);
359 }
360 asort($wslist);
361 $smarty->assign("search_image", get_template_path('images/search.png'));
362 $smarty->assign("launchimage", get_template_path('images/small_filter.png'));
363 $smarty->assign("tree_image", get_template_path('images/tree.png'));
364 $smarty->assign("deplist", $this->config->idepartments);
365 $smarty->assign("alphabet", generate_alphabet());
366 foreach( array("depselect", "regex") as $type){
367 $smarty->assign("$type", $sysfilter[$type]);
368 }
369 $smarty->assign("hint", print_sizelimit_warning());
370 $smarty->assign("wslist", $wslist);
371 $smarty->assign("apply", apply_filter());
372 $display= $smarty->fetch (get_template_path('trust_machines.tpl', TRUE, dirname(__FILE__)));
373 return ($display);
374 }
376 /* Manage group add dialog */
377 if ($this->group_dialog){
379 /* Get global filter config */
380 if (!is_global("groupfilter")){
381 $base= get_base_from_people($ui->dn);
382 $groupfilter= array("primarygroups" => "checked",
383 "mailgroups" => "checked",
384 "sambagroups" => "checked",
385 "appgroups" => "checked",
386 "functionalgroups" => "checked",
387 "guser" => "*",
388 "subsearch" => "",
389 "depselect" => $base,
390 "regex" => "*");
391 register_global("groupfilter", $groupfilter);
392 }
394 /* Save data */
395 $groupfilter= get_global("groupfilter");
396 foreach( array("depselect", "guser", "regex") as $type){
397 if (isset($_POST[$type])){
398 $groupfilter[$type]= $_POST[$type];
399 }
400 }
401 if (isset($_POST['depselect'])){
402 foreach( array("primarygroups", "sambagroups", "mailgroups", "appgroups",
403 "functionalgroups") as $type){
405 if (isset($_POST[$type])) {
406 $groupfilter[$type]= "checked";
407 } else {
408 $groupfilter[$type]= "";
409 }
410 }
411 }
412 if (isset($_GET['search'])){
413 $s= mb_substr($_GET['search'], 0, 1, "UTF8")."*";
414 if ($s == "**"){
415 $s= "*";
416 }
417 $groupfilter['regex']= $s;
418 }
419 register_global("groupfilter", $groupfilter);
421 /* Calculate actual groups */
423 $this->reload();
424 $glist= array();
425 foreach ($this->grouplist as $key => $value){
426 if (!isset($this->groupMembership[$key])){
427 $glist[$key]= $value;
428 }
429 }
431 $smarty->assign("groups", $glist);
432 $smarty->assign("search_image", get_template_path('images/search.png'));
433 $smarty->assign("launchimage", get_template_path('images/small_filter.png'));
434 $smarty->assign("tree_image", get_template_path('images/tree.png'));
435 $smarty->assign("deplist", $this->config->idepartments);
436 $smarty->assign("alphabet", generate_alphabet());
437 foreach( array("depselect", "guser", "regex", "primarygroups", "mailgroups",
438 "appgroups", "sambagroups", "functionalgroups") as $type){
439 $smarty->assign("$type", $groupfilter[$type]);
440 }
441 $smarty->assign("hint", print_sizelimit_warning());
443 $smarty->assign("apply", apply_filter());
444 $display.= $smarty->fetch (get_template_path('posix_groups.tpl', TRUE, dirname(__FILE__)));
445 return ($display);
446 }
448 /* Show main page */
449 $smarty= get_smarty();
451 /* Depending on pwmode, currently hardcoded because there are no other methods */
452 if ( 1 == 1 ){
453 $smarty->assign("pwmode", dirname(__FILE__)."/posix_shadow");
454 $shadowMinACL= chkacl($this->acl, "shadowMin");
455 $smarty->assign("shadowmins", sprintf(_("Password can't be changed up to %s days after last change"), "<input name=\"shadowMin\" size=3 maxlength=4 $shadowMinACL value=\"".$this->shadowMin."\">"));
456 $shadowMaxACL= chkacl($this->acl, "shadowMax");
457 $smarty->assign("shadowmaxs", sprintf(_("Password must be changed after %s days"), "<input name=\"shadowMax\" size=3 maxlength=4 $shadowMaxACL value=\"".$this->shadowMax."\">"));
458 $shadowInactiveACL= chkacl($this->acl, "shadowInactive");
459 $smarty->assign("shadowinactives", sprintf(_("Disable account after %s days of inactivity after password expiery"), "<input name=\"shadowInactive\" size=3 maxlength=4 $shadowInactiveACL value=\"".$this->shadowInactive."\">"));
460 $shadowWarningACL= chkacl($this->acl, "shadowWarning");
461 $smarty->assign("shadowwarnings", sprintf(_("Warn user %s days before password expiery"), "<input name=\"shadowWarning\" size=3 maxlength=4 $shadowWarningACL value=\"".$this->shadowWarning."\">"));
462 foreach( array("must_change_password", "use_shadowMin", "use_shadowMax",
463 "use_shadowExpire", "use_shadowInactive",
464 "use_shadowWarning") as $val){
465 if ($this->$val == 1){
466 $smarty->assign("$val", "checked");
467 } else {
468 $smarty->assign("$val", "");
469 }
470 $smarty->assign("$val"."ACL", chkacl($this->acl, $val));
471 }
472 }
474 /* Fill calendar */
475 $date= getdate($this->shadowExpire);
477 $days= array();
478 for($d= 1; $d<32; $d++){
479 $days[$d]= $d;
480 }
481 $years= array();
482 for($y= $date['year']-10; $y<$date['year']+10; $y++){
483 $years[]= $y;
484 }
485 $months= array(_("January"), _("February"), _("March"), _("April"),
486 _("May"), _("June"), _("July"), _("August"), _("September"),
487 _("October"), _("November"), _("December"));
488 $smarty->assign("day", $date["mday"]);
489 $smarty->assign("days", $days);
490 $smarty->assign("months", $months);
491 $smarty->assign("month", $date["mon"]-1);
492 $smarty->assign("years", $years);
493 $smarty->assign("year", $date["year"]);
495 /* Fill arrays */
496 $smarty->assign("shells", $this->loginShellList);
497 $smarty->assign("secondaryGroups", $this->secondaryGroups);
498 $smarty->assign("primaryGroup", $this->primaryGroup);
499 if (!count($this->groupMembership)){
500 $smarty->assign("groupMembership", array(" "));
501 } else {
502 $smarty->assign("groupMembership", $this->groupMembership);
503 }
504 if (count($this->groupMembership) > 16){
505 $smarty->assign("groups", "too_many_for_nfs");
506 } else {
507 $smarty->assign("groups", "");
508 }
509 $smarty->assign("printerList", $this->printerList);
510 $smarty->assign("languages", $this->config->data['MAIN']['LANGUAGES']);
512 /* Avoid "Undefined index: forceMode" */
513 $smarty->assign("forceMode", "");
515 /* Checkboxes */
516 if ($this->force_ids == 1){
517 $smarty->assign("force_ids", "checked");
518 if ($_SESSION['js']){
519 $smarty->assign("forceMode", "");
520 }
521 } else {
522 if ($_SESSION['js']){
523 if($this->acl != "#none#")
524 $smarty->assign("forceMode", "disabled");
525 }
526 $smarty->assign("force_ids", "");
527 }
528 $smarty->assign("force_idsACL", chkacl($this->acl, "force_ids"));
530 /* Load attributes and acl's */
531 foreach($this->attributes as $val){
532 if((chkacl($this->acl,$val)=="")&&(($_SESSION["js"])&&(($val=="uidNumber")||($val=="gidNumber"))))
533 {
534 $smarty->assign("$val"."ACL","");
535 $smarty->assign("$val", $this->$val);
536 continue;
537 }
538 $smarty->assign("$val", $this->$val);
539 $smarty->assign("$val"."ACL", chkacl($this->acl,$val));
540 }
541 $smarty->assign("groupMembershipACL", chkacl($this->acl, "groupMembership"));
542 $smarty->assign("status", $this->status);
544 /* Work on trust modes */
545 $smarty->assign("trustmodeACL", chkacl($this->acl, "trustmode"));
546 if ($this->trustModel == "fullaccess"){
547 $trustmode= 1;
548 // pervent double disable tag in html code, this will disturb our clean w3c html
550 if(chkacl($this->acl, "trustmode")==""){
551 $smarty->assign("trusthide", "disabled");
552 }else{
553 $smarty->assign("trusthide", "");
554 }
556 } elseif ($this->trustModel == "byhost"){
557 $trustmode= 2;
558 $smarty->assign("trusthide", "");
559 } else {
560 // pervent double disable tag in html code, this will disturb our clean w3c html
561 if(chkacl($this->acl, "trustmode")==""){
562 $smarty->assign("trusthide", "disabled");
563 }else{
564 $smarty->assign("trusthide", "");
565 }
566 $trustmode= 0;
567 }
568 $smarty->assign("trustmode", $trustmode);
569 $smarty->assign("trustmodes", array( 0 => _("disabled"), 1 => _("full access"),
570 2 => _("allow access to these hosts")));
574 if((count($this->accessTo))==0)
575 $smarty->assign("emptyArrAccess",true);
576 else
577 $smarty->assign("emptyArrAccess",false);
581 $smarty->assign("workstations", $this->accessTo);
583 $smarty->assign("apply", apply_filter());
584 $display.= $smarty->fetch (get_template_path('generic.tpl', TRUE, dirname(__FILE__)));
585 return($display);
586 }
589 /* remove object from parent */
590 function remove_from_parent()
591 {
592 /* Cancel if there's nothing to do here */
593 if (!$this->initially_was_account){
594 return;
595 }
597 /* include global link_info */
598 $ldap= $this->config->get_ldap_link();
600 /* Remove and write to LDAP */
601 plugin::remove_from_parent();
603 /* Zero out array */
604 $this->attrs['gosaHostACL']= array();
606 /* Keep uid, because we need it for authentification! */
607 unset($this->attrs['uid']);
608 unset($this->attrs['trustModel']);
610 @DEBUG (DEBUG_LDAP, __LINE__, __FUNCTION__, __FILE__,
611 $this->attributes, "Save");
612 $ldap->cd($this->dn);
613 $this->cleanup();
614 $ldap->modify ($this->attrs);
616 show_ldap_error($ldap->get_error());
618 /* Delete group only if cn is uid and there are no other
619 members inside */
620 $ldap->cd ($this->config->current['BASE']);
621 $ldap->search ("(&(objectClass=posixGroup)(gidNumber=".$this->gidNumber."))", array("cn", "memberUid"));
622 if ($ldap->count() != 0){
623 $attrs= $ldap->fetch();
624 if ($attrs['cn'][0] == $this->uid &&
625 !isset($this->attrs['memberUid'])){
627 $ldap->rmDir($ldap->getDN());
628 }
629 }
631 /* Optionally execute a command after we're done */
632 $this->handle_post_events("remove");
633 }
636 function save_object()
637 {
638 if (isset($_POST['posixTab'])){
639 /* Save values to object */
640 plugin::save_object();
642 /* Save force GID attribute */
643 if (chkacl ($this->acl, "force_ids") == ""){
644 if (isset ($_POST['force_ids'])){
645 $data= 1;
646 } else {
647 $data= 0;
648 }
649 if ($this->force_ids != $data){
650 $this->is_modified= TRUE;
651 }
652 $this->force_ids= $data;
654 $data= $_POST['primaryGroup'];
655 if ($this->primaryGroup != $data){
656 $this->is_modified= TRUE;
657 }
658 $this->primaryGroup= $_POST['primaryGroup'];
659 }
661 /* Save pwmode dependent attributes, curently hardcoded because there're
662 no alternatives */
663 if (1 == 1){
664 foreach( array("must_change_password", "use_shadowMin", "use_shadowMax",
665 "use_shadowExpire", "use_shadowInactive",
666 "use_shadowWarning") as $val){
667 if (chkacl($this->acl, "$val") == ""){
668 if (isset ($_POST[$val])){
669 $data= 1;
670 } else {
671 $data= 0;
672 }
673 if ($data != $this->$val){
674 $this->is_modified= TRUE;
675 }
676 $this->$val= $data;
677 }
678 }
679 }
681 /* Trust mode - special handling */
682 if (isset($_POST['trustmode'])){
683 $saved= $this->trustModel;
684 if ($_POST['trustmode'] == "1"){
685 $this->trustModel= "fullaccess";
686 } elseif ($_POST['trustmode'] == "2"){
687 $this->trustModel= "byhost";
688 } else {
689 $this->trustModel= "";
690 }
691 if ($this->trustModel != $saved){
692 $this->is_modified= TRUE;
693 }
694 }
695 }
696 }
699 /* Save data to LDAP, depending on is_account we save or delete */
700 function save()
701 {
703 /* include global link_info */
704 $ldap= $this->config->get_ldap_link();
706 /* Adapt shadow values */
707 if (!$this->use_shadowExpire){
708 $this->shadowExpire= "0";
709 } else {
710 /* Transform seconds to days here */
711 $this->shadowExpire= (int)($this->shadowExpire / (60 * 60 * 24)) ;
712 }
713 if (!$this->use_shadowMax){
714 $this->shadowMax= "0";
715 }
716 if ($this->must_change_password){
717 $this->shadowLastChange= (int)(date("U") / 86400) - $this->shadowMax - 1;
718 } else {
719 $this->shadowLastChange= (int)(date("U") / 86400);
720 }
721 if (!$this->use_shadowWarning){
722 $this->shadowWarning= "0";
723 }
725 /* Check what to do with ID's */
726 if ($this->force_ids == 0){
728 /* Use id's that are already set */
729 if ($this->savedUidNumber != ""){
730 $this->uidNumber= $this->savedUidNumber;
731 $this->gidNumber= $this->savedGidNumber;
732 } else {
734 /* Calculate new id's. We need to place a lock before calling get_next_id
735 to get real unique values. */
736 $wait= 10;
737 while (get_lock("uidnumber") != ""){
738 sleep (1);
740 /* Oups - timed out */
741 if ($wait-- == 0){
742 print_red (_("Failed: overriding lock"));
743 break;
744 }
745 }
747 add_lock ("uidnumber", "gosa");
748 $this->uidNumber= $this->get_next_id("uidNumber");
749 if ($this->savedGidNumber != ""){
750 $this->gidNumber= $this->savedGidNumber;
751 } else {
752 $this->gidNumber= $this->get_next_id("gidNumber");
753 }
754 }
756 if ($this->primaryGroup != 0){
757 $this->gidNumber= $this->primaryGroup;
758 }
759 }
761 if ($this->use_shadowMin != "1" ) {
762 $this->shadowMin = "";
763 }
765 if (($this->use_shadowMax != "1") && ($this->must_change_password != "1")) {
766 $this->shadowMax = "";
767 }
769 if ($this->use_shadowWarning != "1" ) {
770 $this->shadowWarning = "";
771 }
773 if ($this->use_shadowInactive != "1" ) {
774 $this->shadowInactive = "";
775 }
777 if ($this->use_shadowExpire != "1" ) {
778 $this->shadowExpire = "";
779 }
781 /* Fill gecos */
782 if (isset($this->parent) && $this->parent != NULL){
783 $this->gecos= rewrite($this->parent->by_object['user']->cn);
784 if (!preg_match('/[a-z0-9 -]/i', $this->gecos)){
785 $this->gecos= "";
786 }
787 }
789 foreach(array("shadowMin","shadowMax","shadowWarning","shadowInactive","shadowExpire") as $attr){
790 $this->$attr = (int) $this->$attr;
791 }
792 /* Call parents save to prepare $this->attrs */
793 plugin::save();
795 /* Trust accounts */
796 $objectclasses= array();
797 foreach ($this->attrs['objectClass'] as $key => $class){
798 if (preg_match('/trustAccount/i', $class)){
799 continue;
800 }
801 $objectclasses[]= $this->attrs['objectClass'][$key];
802 }
803 $this->attrs['objectClass']= $objectclasses;
804 if ($this->trustModel != ""){
805 $this->attrs['objectClass'][]= "trustAccount";
806 $this->attrs['trustModel']= $this->trustModel;
807 $this->attrs['accessTo']= array();
808 if ($this->trustModel == "byhost"){
809 foreach ($this->accessTo as $host){
810 $this->attrs['accessTo'][]= $host;
811 }
812 }
813 } else {
814 if ($this->was_trust_account){
815 $this->attrs['accessTo']= array();
816 $this->attrs['trustModel']= array();
817 }
818 }
820 if(empty($this->attrs['gosaDefaultPrinter'])){
821 $thid->attrs['gosaDefaultPrinter']=array();
822 }
825 /* Save data to LDAP */
826 $ldap->cd($this->dn);
827 $this->cleanup();
828 $ldap->modify ($this->attrs);
830 show_ldap_error($ldap->get_error());
832 /* Remove lock needed for unique id generation */
833 del_lock ("uidnumber");
836 /* Posix accounts have group interrelationship, take care about these here. */
837 if ($this->force_ids == 0 && $this->primaryGroup == 0){
838 $ldap->cd($this->config->current['BASE']);
839 $ldap->search("(&(objectClass=posixGroup)(gidNumber=".$this->gidNumber."))", array("cn"));
841 /* Create group if it doesn't exist */
842 if ($ldap->count() == 0){
843 $groupdn= preg_replace ('/^'.$this->config->current['DNMODE'].'=[^,]+,'.get_people_ou().'/i', 'cn='.$this->uid.','.get_groups_ou(), $this->dn);
845 $g= new group($this->config, $groupdn);
846 $g->cn= $this->uid;
847 $g->force_gid= 1;
848 $g->gidNumber= $this->gidNumber;
849 $g->description= "Group of user ".$this->givenName." ".$this->sn;
850 $g->save ();
851 }
852 }
854 /* Take care about groupMembership values: add to groups */
855 foreach ($this->groupMembership as $key => $value){
856 $g= new group($this->config, $key);
857 $g->addUser ($this->uid);
858 $g->save();
860 /* May need to save the mail part, too */
861 if ($g->has_mailAccount){
862 $m= new mailgroup($this->config, $key);
863 $m->save();
864 }
865 }
867 /* Remove from groups not listed in groupMembership */
868 foreach ($this->savedGroupMembership as $key => $value){
869 if (!array_key_exists ($key, $this->groupMembership)){
870 $g= new group($this->config, $key);
871 $g->removeUser ($this->uid);
872 $g->save();
874 /* May need to save the mail part, too */
875 if ($g->has_mailAccount){
876 $m= new mailgroup($this->config, $key);
877 $m->save();
878 }
879 }
880 }
882 /* Optionally execute a command after we're done */
883 if ($this->initially_was_account == $this->is_account){
884 if ($this->is_modified){
885 $this->handle_post_events("mofify");
886 }
887 } else {
888 $this->handle_post_events("add");
889 }
890 }
892 /* Check formular input */
893 function check()
894 {
895 /* Include global link_info */
896 $ldap= $this->config->get_ldap_link();
898 /* Reset message array */
899 $message= array();
901 /* must: homeDirectory */
902 if ($this->homeDirectory == ""){
903 $message[]= _("The required field 'Home directory' is not set.");
904 }
905 if (!is_path($this->homeDirectory)){
906 $message[]= _("Please enter a valid path in 'Home directory' field.");
907 }
909 /* Check ID's if they are forced by user */
910 if ($this->force_ids == "1"){
912 /* Valid uid/gid? */
913 if (!is_id($this->uidNumber)){
914 $message[]= _("Value specified as 'UID' is not valid.");
915 } else {
916 if ($this->uidNumber < $this->config->current['MINID']){
917 $message[]= _("Value specified as 'UID' is too small.");
918 }
919 }
920 if (!is_id($this->gidNumber)){
921 $message[]= _("Value specified as 'GID' is not valid.");
922 } else {
923 if ($this->gidNumber < $this->config->current['MINID']){
924 $message[]= _("Value specified as 'GID' is too small.");
925 }
926 }
927 }
929 /* Check shadow settings, well I like spaghetties... */
930 if ($this->use_shadowMin){
931 if (!is_id($this->shadowMin)){
932 $message[]= _("Value specified as 'shadowMin' is not valid.");
933 }
934 }
935 if ($this->use_shadowMax){
936 if (!is_id($this->shadowMax)){
937 $message[]= _("Value specified as 'shadowMax' is not valid.");
938 }
939 }
940 if ($this->use_shadowWarning){
941 if (!is_id($this->shadowWarning)){
942 $message[]= _("Value specified as 'shadowWarning' is not valid.");
943 }
944 if (!$this->use_shadowMax){
945 $message[]= _("'shadowWarning' without 'shadowMax' makes no sense.");
946 }
947 if ($this->shadowWarning > $this->shadowMax){
948 $message[]= _("Value specified as 'shadowWarning' should be smaller than 'shadowMax'.");
949 }
950 if ($this->use_shadowMin && $this->shadowWarning < $this->shadowMin){
951 $message[]= _("Value specified as 'shadowWarning' should be greater than 'shadowMin'.");
952 }
953 }
954 if ($this->use_shadowInactive){
955 if (!is_id($this->shadowInactive)){
956 $message[]= _("Value specified as 'shadowInactive' is not valid.");
957 }
958 if (!$this->use_shadowMax){
959 $message[]= _("'shadowInactive' without 'shadowMax' makes no sense.");
960 }
961 }
962 if ($this->use_shadowMin && $this->use_shadowMax){
963 if ($this->shadowMin > $this->shadowMax){
964 $message[]= _("Value specified as 'shadowMin' should be smaller than 'shadowMax'.");
965 }
966 }
968 // if(empty($this->gosaDefaultPrinter)){
969 // $message[]= _("You need to specify a valid default printer.");
970 // }
972 return ($message);
973 }
975 function addGroup ($groups)
976 {
977 /* include global link_info */
978 $ldap= $this->config->get_ldap_link();
980 /* Walk through groups and add the descriptive entry if not exists */
981 foreach ($groups as $value){
982 if (!array_key_exists($value, $this->groupMembership)){
983 $ldap->cat($value);
984 $attrs= $ldap->fetch();
985 error_reporting (0);
986 if (!isset($attrs['description'][0])){
987 $entry= $attrs["cn"][0];
988 } else {
989 $dsc= preg_replace ('/^Group of user/', _("Group of user"), $attrs["description"][0]);
990 $entry= $attrs["cn"][0]." [$dsc]";
991 }
992 error_reporting (E_ALL);
993 $this->groupMembership[$ldap->getDN()]= $entry;
994 }
995 }
997 /* Sort groups */
998 asort ($this->groupMembership);
999 reset ($this->groupMembership);
1000 }
1003 /* Del posix user from some groups */
1004 function delGroup ($groups)
1005 {
1006 $dest= array();
1008 foreach ($this->groupMembership as $key => $value){
1009 if (!in_array($key, $groups)){
1010 $dest[$key]= $value;
1011 }
1012 }
1013 $this->groupMembership= $dest;
1014 }
1016 /* Adapt from template, using 'dn' */
1017 function adapt_from_template($dn)
1018 {
1019 /* Include global link_info */
1020 $ldap= $this->config->get_ldap_link();
1022 plugin::adapt_from_template($dn);
1023 $template= $this->attrs['uid'][0];
1025 /* Adapt group membership */
1026 $ldap->cd($this->config->current['BASE']);
1027 $ldap->search("(&(objectClass=posixGroup)(memberUid=".$this->attrs["uid"][0]."))", array("description", "cn"));
1029 while ($this->attrs= $ldap->fetch()){
1030 if (!isset($this->attrs["description"][0])){
1031 $entry= $this->attrs["cn"][0];
1032 } else {
1033 $entry= $this->attrs["cn"][0]." [".$this->attrs["description"][0]."]";
1034 }
1035 $this->groupMembership[$ldap->getDN()]= $entry;
1036 }
1038 /* Fix primary group settings */
1039 $ldap->cd($this->config->current['BASE']);
1040 $ldap->search("(&(objectClass=posixGroup)(cn=$template)(gidNumber=".$this->gidNumber."))", array("cn"));
1041 if ($ldap->count() != 1){
1042 $this->primaryGroup= $this->gidNumber;
1043 }
1045 $ldap->cd($this->config->current['BASE']);
1046 $ldap->search("(&(objectClass=gosaUserTemplate)(uid=".$template."))", array("cn","accessTo"));
1047 while($attr = $ldap->fetch()){
1048 $tmp = $attr['accessTo'];
1049 unset ($tmp['count']);
1050 $this->accessTo = $tmp;
1051 }
1053 /* Adjust shadow checkboxes */
1054 foreach (array("shadowMin", "shadowMax", "shadowWarning", "shadowInactive",
1055 "shadowExpire") as $val){
1057 if ($this->$val != 0){
1058 $oval= "use_".$val;
1059 $this->$oval= "1";
1060 }
1061 }
1062 }
1064 function get_next_id($attrib)
1065 {
1066 $ids= array();
1067 $ldap= $this->config->get_ldap_link();
1069 $ldap->cd ($this->config->current['BASE']);
1070 if (preg_match('/gidNumber/i', $attrib)){
1071 $oc= "posixGroup";
1072 } else {
1073 $oc= "posixAccount";
1074 }
1075 $ldap->search ("(&(objectClass=$oc)($attrib=*))", array("$attrib"));
1077 /* Get list of ids */
1078 while ($attrs= $ldap->fetch()){
1079 $ids[]= (int)$attrs["$attrib"][0];
1080 }
1082 /* Find out next free id near to UID_BASE */
1083 for ($id= $this->config->current['UIDBASE']; $id++; $id<65000){
1084 if (!in_array($id, $ids)){
1085 return ($id);
1086 }
1087 }
1089 /* Should not happen */
1090 if ($id == 65000){
1091 print_red(_("Too many users, can't allocate a free ID!"));
1092 exit;
1093 }
1095 }
1097 function reload()
1098 {
1100 /* Get config */
1101 $groupfilter= get_global('groupfilter');
1103 /* Set base for all searches */
1104 $base= $groupfilter['depselect'];
1106 /* Regex filter? */
1107 if ($groupfilter['regex'] != ""){
1108 $regex= $groupfilter['regex'];
1109 } else {
1110 $regex= "*";
1111 }
1113 $error = "";
1114 $ldap = $this->config->get_ldap_link();
1116 $base= get_groups_ou().$base;
1117 $res= get_list($this->ui->subtreeACL, "(objectClass=posixGroup)", FALSE, $base, array("cn", "description", "gidNumber"), TRUE);
1118 if (preg_match("/size limit/i", $error)){
1119 $_SESSION['limit_exceeded']= TRUE;
1120 }
1122 $error = $ldap->error;
1123 $this->grouplist = array();
1124 foreach ($res as $value){
1125 $this->grouplist[$value['gidNumber'][0]]= $value;
1126 }
1128 $tmp=array();
1129 foreach($this->grouplist as $tkey => $val ){
1130 $tmp[strtolower($val['cn'][0]).$val['cn'][0]]=$val;
1131 }
1133 /* Sort index */
1134 ksort($tmp);
1136 /* Recreate index array[dn]=cn[description]*/
1137 $this->grouplist=array();
1138 foreach($tmp as $val){
1139 if(isset($val['description'])){
1140 $this->grouplist[$val['dn']]=$val['cn'][0]." [".$val['description'][0]."]";
1141 }else{
1142 $this->grouplist[$val['dn']]=$val['cn'][0];
1143 }
1144 }
1145 reset ($this->grouplist);
1146 }
1147 }
1149 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
1150 ?>