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;
63 /* attribute list for save action */
64 var $attributes= array("homeDirectory", "loginShell", "uidNumber", "gidNumber", "gecos",
65 "shadowMin", "shadowMax", "shadowWarning", "shadowInactive", "shadowLastChange",
66 "shadowExpire", "gosaDefaultPrinter", "gosaDefaultLanguage", "uid");
67 var $objectclasses= array("posixAccount", "shadowAccount");
70 /* constructor, if 'dn' is set, the node loads the given
71 'dn' from LDAP */
72 function posixAccount ($config, $dn= NULL)
73 {
74 /* Configuration is fine, allways */
75 $this->config= $config;
77 /* Load bases attributes */
78 plugin::plugin($config, $dn);
80 $ldap= $this->config->get_ldap_link();
82 if ($dn != NULL){
84 /* Correct is_account. shadowAccount is not required. */
85 if (isset($this->attrs['objectClass']) &&
86 in_array ('posixAccount', $this->attrs['objectClass'])){
88 $this->is_account= TRUE;
89 }
91 /* Is this account a trustAccount? */
92 if ($this->is_account && isset($this->attrs['trustModel'])){
93 $this->trustModel= $this->attrs['trustModel'][0];
94 $this->was_trust_account= TRUE;
95 } else {
96 $this->was_trust_account= FALSE;
97 $this->trustModel= "";
98 }
99 if ($this->is_account && isset($this->attrs['accessTo'])){
100 for ($i= 0; $i<$this->attrs['accessTo']['count']; $i++){
101 $tmp= $this->attrs['accessTo'][$i];
102 $this->accessTo[$tmp]= $tmp;
103 }
104 }
105 $this->initially_was_account= $this->is_account;
107 /* Fill group */
108 $this->primaryGroup= $this->gidNumber;
110 /* Generate status text */
111 $current= date("U");
112 if (($current >= $this->shadowExpire) && $this->shadowExpire){
113 $this->status= "expired";
114 if (($this->shadowExpire - $current) < $this->shadowInactive){
115 $this->status.= ", grace time active";
116 }
117 } elseif (($this->shadowLastChange + $this->shadowMin) >= $current){
118 $this->status= "active, password not changable";
119 } elseif (($this->shadowLastChange + $this->shadowMax) >= $current){
120 $this->status= "active, password expired";
121 } else {
122 $this->status= "active";
123 }
125 /* Get group membership */
126 $ldap->cd($this->config->current['BASE']);
127 $ldap->search("(&(objectClass=posixGroup)(memberUid=".$this->uid."))", array("cn", "description"));
129 while ($this->attrs= $ldap->fetch()){
130 if (!isset($this->attrs["description"][0])){
131 $entry= $this->attrs["cn"][0];
132 } else {
133 $dsc= preg_replace ('/^Group of user/', _("Group of user"), $this->attrs["description"][0]);
134 $entry= $this->attrs["cn"][0]." [$dsc]";
135 }
136 $this->groupMembership[$ldap->getDN()]= $entry;
137 }
138 asort($this->groupMembership);
139 reset($this->groupMembership);
140 $this->savedGroupMembership= $this->groupMembership;
141 $this->savedUidNumber= $this->uidNumber;
142 $this->savedGidNumber= $this->gidNumber;
143 }
145 /* Adjust shadow checkboxes */
146 foreach (array("shadowMin", "shadowMax", "shadowWarning", "shadowInactive",
147 "shadowExpire") as $val){
149 if ($this->$val != 0){
150 $oval= "use_".$val;
151 $this->$oval= "1";
152 }
153 }
155 /* Convert to seconds */
156 if ($this->shadowExpire != 0){
157 $this->shadowExpire*= 60 * 60 * 24;
158 } else {
159 $date= getdate();
160 $this->shadowExpire= floor($date[0] / (60*60*24)) * 60 * 60 * 24;
161 }
163 /* Generate shell list from /etc/gosa/shells */
164 if (file_exists('/etc/gosa/shells')){
165 $shells = file ('/etc/gosa/shells');
166 foreach ($shells as $line){
167 if (!preg_match ("/^#/", $line)){
168 $this->loginShellList[]= trim($line);
169 }
170 }
171 } else {
172 if ($this->loginShell == ""){
173 $this->loginShellList[]= _("unconfigured");
174 }
175 }
177 /* Insert possibly missing loginShell */
178 if ($this->loginShell != "" && !in_array($this->loginShell, $this->loginShellList)){
179 $this->loginShellList[]= $this->loginShell;
180 }
182 /* Generate printer list */
183 if (isset($this->config->data['SERVERS']['CUPS'])){
184 $this->printerList= get_printer_list ($this->config->data['SERVERS']['CUPS']);
185 asort($this->printerList);
186 }
188 /* Generate group list */
189 $ldap->cd($this->config->current['BASE']);
190 $ldap->search("(objectClass=posixGroup)", array("cn", "gidNumber"));
191 $this->secondaryGroups[]= "- "._("automatic")." -";
192 while ($attrs= $ldap->fetch()){
193 $this->secondaryGroups[$attrs['gidNumber'][0]]= $attrs['cn'][0];
194 }
195 asort ($this->secondaryGroups);
197 /* Get global filter config */
198 if (!is_global("sysfilter")){
199 $ui= get_userinfo();
200 $base= get_base_from_people($ui->dn);
201 $sysfilter= array( "depselect" => $base,
202 "regex" => "*");
203 register_global("sysfilter", $sysfilter);
204 }
205 }
208 /* execute generates the html output for this node */
209 function execute()
210 {
211 /* Do we need to flip is_account state? */
212 if (isset($_POST['modify_state'])){
213 $this->is_account= !$this->is_account;
214 }
216 /* Do we represent a valid posixAccount? */
217 if (!$this->is_account && $this->parent == NULL ){
218 $display= "<img alt=\"\" src=\"images/stop.png\" align=\"middle\"> <b>".
219 _("This account has no unix extensions.")."</b>";
220 $display.= back_to_main();
221 return ($display);
222 }
224 $display= "";
226 /* Show tab dialog headers */
227 if ($this->parent != NULL){
228 if ($this->is_account){
229 if (isset($this->parent->by_object['sambaAccount'])){
230 $obj= $this->parent->by_object['sambaAccount'];
231 }
232 if (isset($obj) && $obj->is_account == TRUE &&
233 isset($this->parent->by_object['sambaAccount'])){
235 /* Samba3 dependency on posix accounts are enabled
236 in the moment, because I need to rely on unique
237 uidNumbers. There'll be a better solution later
238 on. */
239 $display= $this->show_header(_("Remove posix account"),
240 _("This account has unix features enabled. To disable them, you'll need to remove the samba account first."), TRUE);
241 } else {
242 $display= $this->show_header(_("Remove posix account"),
243 _("This account has posix features enabled. You can disable them by clicking below."));
244 }
245 } else {
246 $display= $this->show_header(_("Create posix account"),
247 _("This account has posix features disabled. You can enable them by clicking below."));
248 return($display);
249 }
250 }
252 /* Trigger group edit? */
253 if (isset($_POST['edit_groupmembership'])){
254 $this->group_dialog= TRUE;
255 $this->dialog= TRUE;
256 }
258 /* Cancel group edit? */
259 if (isset($_POST['add_groups_cancel']) ||
260 isset($_POST['add_groups_finish'])){
261 $this->group_dialog= FALSE;
262 $this->dialog= FALSE;
263 }
265 /* Add selected groups */
266 if (isset($_POST['add_groups_finish']) && isset($_POST['groups']) &&
267 count($_POST['groups'])){
269 if (chkacl ($this->acl, "memberUid") == ""){
270 $this->addGroup ($_POST['groups']);
271 $this->is_modified= TRUE;
272 }
273 }
275 /* Delete selected groups */
276 if (isset($_POST['delete_groupmembership']) &&
277 isset($_POST['group_list']) && count($_POST['group_list'])){
279 if (chkacl ($this->acl, "memberUid") == ""){
280 $this->delGroup ($_POST['group_list']);
281 $this->is_modified= TRUE;
282 }
283 }
285 /* Add user workstation? */
286 if (isset($_POST["add_ws"])){
287 $this->show_ws_dialog= TRUE;
288 $this->dialog= TRUE;
289 }
291 /* Add user workstation? */
292 if (isset($_POST["add_ws_finish"]) && isset($_POST['wslist'])){
293 foreach($_POST['wslist'] as $ws){
294 $this->accessTo[$ws]= $ws;
295 }
296 ksort($this->accessTo);
297 $this->is_modified= TRUE;
298 }
300 /* Remove user workstations? */
301 if (isset($_POST["delete_ws"]) && isset($_POST['workstation_list'])){
302 foreach($_POST['workstation_list'] as $name){
303 unset ($this->accessTo[$name]);
304 }
305 $this->is_modified= TRUE;
306 }
308 /* Add user workstation finished? */
309 if (isset($_POST["add_ws_finish"]) || isset($_POST["add_ws_cancel"])){
310 $this->show_ws_dialog= FALSE;
311 $this->dialog= FALSE;
312 }
314 /* Templates now! */
315 $smarty= get_smarty();
317 /* Show ws dialog */
318 if ($this->show_ws_dialog){
319 /* Save data */
320 $sysfilter= get_global("sysfilter");
321 foreach( array("depselect", "regex") as $type){
322 if (isset($_POST[$type])){
323 $sysfilter[$type]= $_POST[$type];
324 }
325 }
326 if (isset($_GET['search'])){
327 $s= mb_substr($_GET['search'], 0, 1, "UTF8")."*";
328 if ($s == "**"){
329 $s= "*";
330 }
331 $sysfilter['regex']= $s;
332 }
333 register_global("sysfilter", $sysfilter);
335 /* Get workstation list */
336 $exclude= "";
337 foreach($this->accessTo as $ws){
338 $exclude.= "(cn=$ws)";
339 }
340 if ($exclude != ""){
341 $exclude= "(!(|$exclude))";
342 }
343 $acl= array($this->config->current['BASE'] => ":all");
344 $regex= $sysfilter['regex'];
345 $filter= "(&(|(objectClass=goServer)(objectClass=gotoWorkstation)(objectClass=gotoTerminal))$exclude(cn=*)(cn=$regex))";
346 $res= get_list($acl, "$filter", TRUE, $sysfilter['depselect'], array("cn"), TRUE);
347 $wslist= array();
348 foreach ($res as $attrs){
349 $wslist[]= preg_replace('/\$/', '', $attrs['cn'][0]);
350 }
351 asort($wslist);
352 $smarty->assign("search_image", get_template_path('images/search.png'));
353 $smarty->assign("launchimage", get_template_path('images/small_filter.png'));
354 $smarty->assign("tree_image", get_template_path('images/tree.png'));
355 $smarty->assign("deplist", $this->config->idepartments);
356 $smarty->assign("alphabet", generate_alphabet());
357 foreach( array("depselect", "regex") as $type){
358 $smarty->assign("$type", $sysfilter[$type]);
359 }
360 $smarty->assign("hint", print_sizelimit_warning());
361 $smarty->assign("wslist", $wslist);
362 $smarty->assign("apply", apply_filter());
363 $display= $smarty->fetch (get_template_path('trust_machines.tpl', TRUE, dirname(__FILE__)));
364 return ($display);
365 }
367 /* Manage group add dialog */
368 if ($this->group_dialog){
369 $gd= new groupManagement($this->config, get_userinfo());
371 /* Save data */
372 $groupfilter= get_global("groupfilter");
373 foreach( array("depselect", "guser", "regex") as $type){
374 if (isset($_POST[$type])){
375 $groupfilter[$type]= $_POST[$type];
376 }
377 }
378 if (isset($_POST['depselect'])){
379 foreach( array("primarygroups", "sambagroups", "mailgroups", "appgroups",
380 "functionalgroups") as $type){
382 if (isset($_POST[$type])) {
383 $groupfilter[$type]= "checked";
384 } else {
385 $groupfilter[$type]= "";
386 }
387 }
388 }
389 if (isset($_GET['search'])){
390 $s= mb_substr($_GET['search'], 0, 1, "UTF8")."*";
391 if ($s == "**"){
392 $s= "*";
393 }
394 $groupfilter['regex']= $s;
395 }
396 register_global("groupfilter", $groupfilter);
398 /* Calculate actual groups */
399 $gd->reload2();
400 $glist= array();
401 foreach ($gd->grouplist as $key => $value){
402 if (!isset($this->groupMembership[$key])){
403 $glist[$key]= $value;
404 }
405 }
407 $smarty->assign("groups", $glist);
408 $smarty->assign("search_image", get_template_path('images/search.png'));
409 $smarty->assign("launchimage", get_template_path('images/small_filter.png'));
410 $smarty->assign("tree_image", get_template_path('images/tree.png'));
411 $smarty->assign("deplist", $this->config->idepartments);
412 $smarty->assign("alphabet", generate_alphabet());
413 foreach( array("depselect", "guser", "regex", "primarygroups", "mailgroups",
414 "appgroups", "sambagroups", "functionalgroups") as $type){
415 $smarty->assign("$type", $groupfilter[$type]);
416 }
417 $smarty->assign("hint", print_sizelimit_warning());
419 $smarty->assign("apply", apply_filter());
420 $display.= $smarty->fetch (get_template_path('posix_groups.tpl', TRUE, dirname(__FILE__)));
421 return ($display);
422 }
424 /* Show main page */
425 $smarty= get_smarty();
427 /* Depending on pwmode, currently hardcoded because there are no other methods */
428 if ( 1 == 1 ){
429 $smarty->assign("pwmode", dirname(__FILE__)."/posix_shadow");
430 $shadowMinACL= chkacl($this->acl, "shadowMin");
431 $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."\">"));
432 $shadowMaxACL= chkacl($this->acl, "shadowMax");
433 $smarty->assign("shadowmaxs", sprintf(_("Password must be changed after %s days"), "<input name=\"shadowMax\" size=3 maxlength=4 $shadowMaxACL value=\"".$this->shadowMax."\">"));
434 $shadowInactiveACL= chkacl($this->acl, "shadowInactive");
435 $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."\">"));
436 $shadowWarningACL= chkacl($this->acl, "shadowWarning");
437 $smarty->assign("shadowwarnings", sprintf(_("Warn user %s days before password expiery"), "<input name=\"shadowWarning\" size=3 maxlength=4 $shadowWarningACL value=\"".$this->shadowWarning."\">"));
438 foreach( array("must_change_password", "use_shadowMin", "use_shadowMax",
439 "use_shadowExpire", "use_shadowInactive",
440 "use_shadowWarning") as $val){
441 if ($this->$val == 1){
442 $smarty->assign("$val", "checked");
443 } else {
444 $smarty->assign("$val", "");
445 }
446 $smarty->assign("$val"."ACL", chkacl($this->acl, $val));
447 }
448 }
450 /* Fill calendar */
451 $date= getdate($this->shadowExpire);
453 $days= array();
454 for($d= 1; $d<32; $d++){
455 $days[$d]= $d;
456 }
457 $years= array();
458 for($y= $date['year']-10; $y<$date['year']+10; $y++){
459 $years[]= $y;
460 }
461 $months= array(_("January"), _("February"), _("March"), _("April"),
462 _("May"), _("June"), _("July"), _("August"), _("September"),
463 _("October"), _("November"), _("December"));
464 $smarty->assign("day", $date["mday"]);
465 $smarty->assign("days", $days);
466 $smarty->assign("months", $months);
467 $smarty->assign("month", $date["mon"]-1);
468 $smarty->assign("years", $years);
469 $smarty->assign("year", $date["year"]);
471 /* Fill arrays */
472 $smarty->assign("shells", $this->loginShellList);
473 $smarty->assign("secondaryGroups", $this->secondaryGroups);
474 $smarty->assign("primaryGroup", $this->primaryGroup);
475 if (!count($this->groupMembership)){
476 $smarty->assign("groupMembership", array(" "));
477 } else {
478 $smarty->assign("groupMembership", $this->groupMembership);
479 }
480 if (count($this->groupMembership) > 16){
481 $smarty->assign("groups", "too_many_for_nfs");
482 } else {
483 $smarty->assign("groups", "");
484 }
485 $smarty->assign("printerList", $this->printerList);
486 $smarty->assign("languages", $this->config->data['MAIN']['LANGUAGES']);
488 /* Checkboxes */
489 if ($this->force_ids == 1){
490 $smarty->assign("force_ids", "checked");
491 if ($_SESSION['js']){
492 $smarty->assign("forceMode", "");
493 }
494 } else {
495 if ($_SESSION['js']){
496 $smarty->assign("forceMode", "disabled");
497 }
498 $smarty->assign("force_ids", "");
499 }
500 $smarty->assign("force_idsACL", chkacl($this->acl, "force_ids"));
502 /* Load attributes and acl's */
503 foreach($this->attributes as $val){
504 if(($_SESSION["js"])&&(($val=="uidNumber")||($val=="gidNumber")))
505 {
506 $smarty->assign("$val"."ACL","");
507 $smarty->assign("$val", $this->$val);
508 continue;
509 }
510 $smarty->assign("$val", $this->$val);
511 $smarty->assign("$val"."ACL", chkacl($this->acl,$val));
512 }
513 $smarty->assign("groupMembershipACL", chkacl($this->acl, "groupMembership"));
514 $smarty->assign("status", $this->status);
516 /* Work on trust modes */
517 $smarty->assign("trustmodeACL", chkacl($this->acl, "trustmode"));
518 if ($this->trustModel == "fullaccess"){
519 $trustmode= 1;
520 // pervent double disable tag in html code, this will disturb our clean w3c html
522 if(chkacl($this->acl, "trustmode")==""){
523 $smarty->assign("trusthide", "disabled");
524 }else{
525 $smarty->assign("trusthide", "");
526 }
528 } elseif ($this->trustModel == "byhost"){
529 $trustmode= 2;
530 $smarty->assign("trusthide", "");
531 } else {
532 // pervent double disable tag in html code, this will disturb our clean w3c html
533 if(chkacl($this->acl, "trustmode")==""){
534 $smarty->assign("trusthide", "disabled");
535 }else{
536 $smarty->assign("trusthide", "");
537 }
538 $trustmode= 0;
539 }
540 $smarty->assign("trustmode", $trustmode);
541 $smarty->assign("trustmodes", array( 0 => _("disabled"), 1 => _("full access"),
542 2 => _("allow access to these hosts")));
546 if((count($this->accessTo))==0)
547 $smarty->assign("emptyArrAccess",true);
548 else
549 $smarty->assign("emptyArrAccess",false);
553 $smarty->assign("workstations", $this->accessTo);
555 $smarty->assign("apply", apply_filter());
556 $display.= $smarty->fetch (get_template_path('generic.tpl', TRUE, dirname(__FILE__)));
557 return($display);
558 }
561 /* remove object from parent */
562 function remove_from_parent()
563 {
564 /* Cancel if there's nothing to do here */
565 if (!$this->initially_was_account){
566 return;
567 }
569 /* include global link_info */
570 $ldap= $this->config->get_ldap_link();
572 /* Remove and write to LDAP */
573 plugin::remove_from_parent();
575 /* Zero out array */
576 $this->attrs['gosaHostACL']= array();
578 /* Keep uid, because we need it for authentification! */
579 unset($this->attrs['uid']);
581 @DEBUG (DEBUG_LDAP, __LINE__, __FUNCTION__, __FILE__,
582 $this->attributes, "Save");
583 $ldap->cd($this->dn);
584 $ldap->modify($this->attrs);
585 show_ldap_error($ldap->get_error());
587 /* Delete group only if cn is uid and there are no other
588 members inside */
589 $ldap->cd ($this->config->current['BASE']);
590 $ldap->search ("(&(objectClass=posixGroup)(gidNumber=".$this->gidNumber."))", array("cn", "memberUid"));
591 if ($ldap->count() != 0){
592 $attrs= $ldap->fetch();
593 if ($attrs['cn'][0] == $this->uid &&
594 !isset($this->attrs['memberUid'])){
596 $ldap->rmDir($ldap->getDN());
597 }
598 }
600 /* Optionally execute a command after we're done */
601 $this->handle_post_events("remove");
602 }
605 function save_object()
606 {
607 if (isset($_POST['posixTab'])){
608 /* Save values to object */
609 plugin::save_object();
611 /* Save force GID attribute */
612 if (chkacl ($this->acl, "force_ids") == ""){
613 if (isset ($_POST['force_ids'])){
614 $data= 1;
615 } else {
616 $data= 0;
617 }
618 if ($this->force_ids != $data){
619 $this->is_modified= TRUE;
620 }
621 $this->force_ids= $data;
623 $data= $_POST['primaryGroup'];
624 if ($this->primaryGroup != $data){
625 $this->is_modified= TRUE;
626 }
627 $this->primaryGroup= $_POST['primaryGroup'];
628 }
630 /* Save pwmode dependent attributes, curently hardcoded because there're
631 no alternatives */
632 if (1 == 1){
633 foreach( array("must_change_password", "use_shadowMin", "use_shadowMax",
634 "use_shadowExpire", "use_shadowInactive",
635 "use_shadowWarning") as $val){
636 if (chkacl($this->acl, "$val") == ""){
637 if (isset ($_POST[$val])){
638 $data= 1;
639 } else {
640 $data= 0;
641 }
642 if ($data != $this->$val){
643 $this->is_modified= TRUE;
644 }
645 $this->$val= $data;
646 }
647 }
648 }
650 /* Trust mode - special handling */
651 if (isset($_POST['trustmode'])){
652 $saved= $this->trustModel;
653 if ($_POST['trustmode'] == "1"){
654 $this->trustModel= "fullaccess";
655 } elseif ($_POST['trustmode'] == "2"){
656 $this->trustModel= "byhost";
657 } else {
658 $this->trustModel= "";
659 }
660 if ($this->trustModel != $saved){
661 $this->is_modified= TRUE;
662 }
663 }
664 }
665 }
668 /* Save data to LDAP, depending on is_account we save or delete */
669 function save()
670 {
671 /* include global link_info */
672 $ldap= $this->config->get_ldap_link();
674 /* Adapt shadow values */
675 if (!$this->use_shadowExpire){
676 $this->shadowExpire= "0";
677 } else {
678 /* Transform seconds to days here */
679 $this->shadowExpire= (int)($this->shadowExpire / (60 * 60 * 24)) + 1;
680 }
681 if (!$this->use_shadowMax){
682 $this->shadowMax= "0";
683 }
684 if ($this->must_change_password){
685 $this->shadowLastChange= (int)(date("U") / 86400) - $this->shadowMax - 1;
686 } else {
687 $this->shadowLastChange= (int)(date("U") / 86400);
688 }
689 if (!$this->use_shadowWarning){
690 $this->shadowWarning= "0";
691 }
693 /* Check what to do with ID's */
694 if ($this->force_ids == 0){
696 /* Use id's that are already set */
697 if ($this->savedUidNumber != ""){
698 $this->uidNumber= $this->savedUidNumber;
699 $this->gidNumber= $this->savedGidNumber;
700 } else {
702 /* Calculate new id's. We need to place a lock before calling get_next_id
703 to get real unique values. */
704 $wait= 10;
705 while (get_lock("uidnumber") != ""){
706 sleep (1);
708 /* Oups - timed out */
709 if ($wait-- == 0){
710 print_red (_("Failed: overriding lock"));
711 break;
712 }
713 }
715 add_lock ("uidnumber", "gosa");
716 $this->uidNumber= $this->get_next_id("uidNumber");
717 if ($this->savedGidNumber != ""){
718 $this->gidNumber= $this->savedGidNumber;
719 } else {
720 $this->gidNumber= $this->get_next_id("gidNumber");
721 }
722 }
724 if ($this->primaryGroup != 0){
725 $this->gidNumber= $this->primaryGroup;
726 }
727 }
729 if ($this->use_shadowMin != "1" ) {
730 $this->shadowMin = "";
731 }
733 if (($this->use_shadowMax != "1") && ($this->must_change_password != "1")) {
734 $this->shadowMax = "";
735 }
737 if ($this->use_shadowWarning != "1" ) {
738 $this->shadowWarning = "";
739 }
741 if ($this->use_shadowInactive != "1" ) {
742 $this->shadowInactive = "";
743 }
745 if ($this->use_shadowExpire != "1" ) {
746 $this->shadowExpire = "";
747 }
749 /* Fill gecos */
750 if (isset($this->parent) && $this->parent != NULL){
751 $this->gecos= rewrite($this->parent->by_object['user']->cn);
752 if (!preg_match('/[a-z0-9 -]/i', $this->gecos)){
753 $this->gecos= "";
754 }
755 }
757 /* Call parents save to prepare $this->attrs */
758 plugin::save();
760 /* Trust accounts */
761 $objectclasses= array();
762 foreach ($this->attrs['objectClass'] as $key => $class){
763 if (preg_match('/trustAccount/i', $class)){
764 continue;
765 }
766 $objectclasses[]= $this->attrs['objectClass'][$key];
767 }
768 $this->attrs['objectClass']= $objectclasses;
769 if ($this->trustModel != ""){
770 $this->attrs['objectClass'][]= "trustAccount";
771 $this->attrs['trustModel']= $this->trustModel;
772 $this->attrs['accessTo']= array();
773 if ($this->trustModel == "byhost"){
774 foreach ($this->accessTo as $host){
775 $this->attrs['accessTo'][]= $host;
776 }
777 }
778 } else {
779 if ($this->was_trust_account){
780 $this->attrs['accessTo']= array();
781 $this->attrs['trustModel']= array();
782 }
783 }
785 if(empty($this->attrs['gosaDefaultPrinter'])){
786 $thid->attrs['gosaDefaultPrinter']=array();
787 }
789 /* Save data to LDAP */
790 $ldap->cd($this->dn);
791 $ldap->modify($this->attrs);
792 show_ldap_error($ldap->get_error());
794 /* Remove lock needed for unique id generation */
795 del_lock ("uidnumber");
798 /* Posix accounts have group interrelationship, take care about these here. */
799 if ($this->force_ids == 0 && $this->primaryGroup == 0){
800 $ldap->cd($this->config->current['BASE']);
801 $ldap->search("(&(objectClass=posixGroup)(gidNumber=".$this->gidNumber."))", array("cn"));
803 /* Create group if it doesn't exist */
804 if ($ldap->count() == 0){
805 $groupdn= preg_replace ('/^'.$this->config->current['DNMODE'].'=[^,]+,'.get_people_ou().'/i', 'cn='.$this->uid.','.get_groups_ou(), $this->dn);
807 $g= new group($this->config, $groupdn);
808 $g->cn= $this->uid;
809 $g->force_gid= 1;
810 $g->gidNumber= $this->gidNumber;
811 $g->description= "Group of user ".$this->givenName." ".$this->sn;
812 $g->save ();
813 }
814 }
816 /* Take care about groupMembership values: add to groups */
817 foreach ($this->groupMembership as $key => $value){
818 $g= new group($this->config, $key);
819 $g->addUser ($this->uid);
820 $g->save();
822 /* May need to save the mail part, too */
823 if ($g->has_mailAccount){
824 $m= new mailgroup($this->config, $key);
825 $m->save();
826 }
827 }
829 /* Remove from groups not listed in groupMembership */
830 foreach ($this->savedGroupMembership as $key => $value){
831 if (!array_key_exists ($key, $this->groupMembership)){
832 $g= new group($this->config, $key);
833 $g->removeUser ($this->uid);
834 $g->save();
836 /* May need to save the mail part, too */
837 if ($g->has_mailAccount){
838 $m= new mailgroup($this->config, $key);
839 $m->save();
840 }
841 }
842 }
844 /* Optionally execute a command after we're done */
845 if ($this->initially_was_account == $this->is_account){
846 if ($this->is_modified){
847 $this->handle_post_events("mofify");
848 }
849 } else {
850 $this->handle_post_events("add");
851 }
852 }
854 /* Check formular input */
855 function check()
856 {
857 /* Include global link_info */
858 $ldap= $this->config->get_ldap_link();
860 /* Reset message array */
861 $message= array();
863 /* must: homeDirectory */
864 if ($this->homeDirectory == ""){
865 $message[]= _("The required field 'Home directory' is not set.");
866 }
867 if (!is_path($this->homeDirectory)){
868 $message[]= _("Please enter a valid path in 'Home directory' field.");
869 }
871 /* Check ID's if they are forced by user */
872 if ($this->force_ids == "1"){
874 /* Valid uid/gid? */
875 if (!is_id($this->uidNumber)){
876 $message[]= _("Value specified as 'UID' is not valid.");
877 } else {
878 if ($this->uidNumber < $this->config->current['MINID']){
879 $message[]= _("Value specified as 'UID' is too small.");
880 }
881 }
882 if (!is_id($this->gidNumber)){
883 $message[]= _("Value specified as 'GID' is not valid.");
884 } else {
885 if ($this->gidNumber < $this->config->current['MINID']){
886 $message[]= _("Value specified as 'GID' is too small.");
887 }
888 }
889 }
891 /* Check shadow settings, well I like spaghetties... */
892 if ($this->use_shadowMin){
893 if (!is_id($this->shadowMin)){
894 $message[]= _("Value specified as 'shadowMin' is not valid.");
895 }
896 }
897 if ($this->use_shadowMax){
898 if (!is_id($this->shadowMax)){
899 $message[]= _("Value specified as 'shadowMax' is not valid.");
900 }
901 }
902 if ($this->use_shadowWarning){
903 if (!is_id($this->shadowWarning)){
904 $message[]= _("Value specified as 'shadowWarning' is not valid.");
905 }
906 if (!$this->use_shadowMax){
907 $message[]= _("'shadowWarning' without 'shadowMax' makes no sense.");
908 }
909 if ($this->shadowWarning > $this->shadowMax){
910 $message[]= _("Value specified as 'shadowWarning' should be smaller than 'shadowMax'.");
911 }
912 if ($this->use_shadowMin && $this->shadowWarning < $this->shadowMin){
913 $message[]= _("Value specified as 'shadowWarning' should be greater than 'shadowMin'.");
914 }
915 }
916 if ($this->use_shadowInactive){
917 if (!is_id($this->shadowInactive)){
918 $message[]= _("Value specified as 'shadowInactive' is not valid.");
919 }
920 if (!$this->use_shadowMax){
921 $message[]= _("'shadowInactive' without 'shadowMax' makes no sense.");
922 }
923 }
924 if ($this->use_shadowMin && $this->use_shadowMax){
925 if ($this->shadowMin > $this->shadowMax){
926 $message[]= _("Value specified as 'shadowMin' should be smaller than 'shadowMax'.");
927 }
928 }
930 // if(empty($this->gosaDefaultPrinter)){
931 // $message[]= _("You need to specify a valid default printer.");
932 // }
934 return ($message);
935 }
937 function addGroup ($groups)
938 {
939 /* include global link_info */
940 $ldap= $this->config->get_ldap_link();
942 /* Walk through groups and add the descriptive entry if not exists */
943 foreach ($groups as $value){
944 if (!array_key_exists($value, $this->groupMembership)){
945 $ldap->cat($value);
946 $attrs= $ldap->fetch();
947 error_reporting (0);
948 if (!isset($attrs['description'][0])){
949 $entry= $attrs["cn"][0];
950 } else {
951 $dsc= preg_replace ('/^Group of user/', _("Group of user"), $attrs["description"][0]);
952 $entry= $attrs["cn"][0]." [$dsc]";
953 }
954 error_reporting (E_ALL);
955 $this->groupMembership[$ldap->getDN()]= $entry;
956 }
957 }
959 /* Sort groups */
960 asort ($this->groupMembership);
961 reset ($this->groupMembership);
962 }
965 /* Del posix user from some groups */
966 function delGroup ($groups)
967 {
968 $dest= array();
970 foreach ($this->groupMembership as $key => $value){
971 if (!in_array($key, $groups)){
972 $dest[$key]= $value;
973 }
974 }
975 $this->groupMembership= $dest;
976 }
978 /* Adapt from template, using 'dn' */
979 function adapt_from_template($dn)
980 {
981 /* Include global link_info */
982 $ldap= $this->config->get_ldap_link();
984 plugin::adapt_from_template($dn);
985 $template= $this->attrs['uid'][0];
987 /* Adapt group membership */
988 $ldap->cd($this->config->current['BASE']);
989 $ldap->search("(&(objectClass=posixGroup)(memberUid=".$this->attrs["uid"][0]."))", array("description", "cn"));
991 while ($this->attrs= $ldap->fetch()){
992 if (!isset($this->attrs["description"][0])){
993 $entry= $this->attrs["cn"][0];
994 } else {
995 $entry= $this->attrs["cn"][0]." [".$this->attrs["description"][0]."]";
996 }
997 $this->groupMembership[$ldap->getDN()]= $entry;
998 }
1000 /* Fix primary group settings */
1001 $ldap->cd($this->config->current['BASE']);
1002 $ldap->search("(&(objectClass=posixGroup)(cn=$template)(gidNumber=".$this->gidNumber."))", array("cn"));
1003 if ($ldap->count() != 1){
1004 $this->primaryGroup= $this->gidNumber;
1005 }
1006 }
1008 function get_next_id($attrib)
1009 {
1010 $ids= array();
1011 $ldap= $this->config->get_ldap_link();
1013 $ldap->cd ($this->config->current['BASE']);
1014 $ldap->search ("($attrib=*)", array("$attrib"));
1016 /* Get list of ids */
1017 while ($attrs= $ldap->fetch()){
1018 $ids[]= (int)$attrs["$attrib"][0];
1019 }
1021 /* Find out next free id near to UID_BASE */
1022 for ($id= $this->config->current['UIDBASE']; $id++; $id<65000){
1023 if (!in_array($id, $ids)){
1024 return ($id);
1025 }
1026 }
1028 /* Should not happen */
1029 if ($id == 65000){
1030 print_red(_("Too many users, can't allocate a free ID!"));
1031 exit;
1032 }
1034 }
1038 }
1040 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
1046 ?>