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 $mustchangepassword= "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 var $GroupRegex = "*";
66 var $GroupUserRegex = "*";
67 var $SubSearch = false;
69 /* attribute list for save action */
70 var $CopyPasteVars = array("grouplist","groupMembership","use_shadowMin","use_shadowMax",
71 "use_shadowWarning","use_shadowInactive","use_shadowExpire","mustchangepassword",
72 "force_ids","printerList","grouplist","savedGidNumber","savedUidNumber","savedGroupMembership");
74 var $attributes = array("homeDirectory", "loginShell", "uidNumber", "gidNumber", "gecos",
75 "shadowMin", "shadowMax", "shadowWarning", "shadowInactive", "shadowLastChange",
76 "shadowExpire", "gosaDefaultPrinter", "gosaDefaultLanguage", "uid","accessTo","trustModel");
78 var $objectclasses= array("posixAccount", "shadowAccount");
81 /* constructor, if 'dn' is set, the node loads the given
82 'dn' from LDAP */
83 function posixAccount ($config, $dn= NULL)
84 {
85 /* Configuration is fine, allways */
86 $this->config= $config;
88 /* Load bases attributes */
89 plugin::plugin($config, $dn);
91 $ldap= $this->config->get_ldap_link();
93 if ($dn != NULL){
95 /* Correct is_account. shadowAccount is not required. */
96 if (isset($this->attrs['objectClass']) &&
97 in_array ('posixAccount', $this->attrs['objectClass'])){
99 $this->is_account= TRUE;
100 }
102 /* Is this account a trustAccount? */
103 if ($this->is_account && isset($this->attrs['trustModel'])){
104 $this->trustModel= $this->attrs['trustModel'][0];
105 $this->was_trust_account= TRUE;
106 } else {
107 $this->was_trust_account= FALSE;
108 $this->trustModel= "";
109 }
111 $this->accessTo = array();
112 if ($this->is_account && isset($this->attrs['accessTo'])){
113 for ($i= 0; $i<$this->attrs['accessTo']['count']; $i++){
114 $tmp= $this->attrs['accessTo'][$i];
115 $this->accessTo[$tmp]= $tmp;
116 }
117 }
118 $this->initially_was_account= $this->is_account;
120 /* Fill group */
121 $this->primaryGroup= $this->gidNumber;
123 /* Generate status text */
124 $current= date("U");
126 $current= floor($current / 60 /60 / 24);
128 if (($current >= $this->shadowExpire) && $this->shadowExpire){
129 $this->status= _("expired");
130 if (($current - $this->shadowExpire) < $this->shadowInactive){
131 $this->status.= ", "._("grace time active");
132 }
133 } elseif (($this->shadowLastChange + $this->shadowMin) >= $current){
134 $this->status= _("active, password not changable");
135 } elseif (($this->shadowLastChange + $this->shadowMax) >= $current){
136 $this->status= _("active, password expired");
137 } else {
138 $this->status= _("active");
139 }
141 /* Get group membership */
142 $ldap->cd($this->config->current['BASE']);
143 $ldap->search("(&(objectClass=posixGroup)(memberUid=".$this->uid."))", array("cn", "description"));
145 while ($this->attrs= $ldap->fetch()){
146 if (!isset($this->attrs["description"][0])){
147 $entry= $this->attrs["cn"][0];
148 } else {
149 $dsc= preg_replace ('/^Group of user/', _("Group of user"), $this->attrs["description"][0]);
150 $entry= $this->attrs["cn"][0]." [$dsc]";
151 }
152 $this->groupMembership[$ldap->getDN()]= $entry;
153 }
154 asort($this->groupMembership);
155 reset($this->groupMembership);
156 $this->savedGroupMembership= $this->groupMembership;
157 $this->savedUidNumber= $this->uidNumber;
158 $this->savedGidNumber= $this->gidNumber;
159 }
161 /* Adjust shadow checkboxes */
162 foreach (array("shadowMin", "shadowMax", "shadowWarning", "shadowInactive",
163 "shadowExpire") as $val){
165 if ($this->$val != 0){
166 $oval= "use_".$val;
167 $this->$oval= "1";
168 }
169 }
171 /* Convert to seconds */
172 if ($this->shadowExpire != 0){
173 $this->shadowExpire*= 60 * 60 * 24;
174 } else {
175 $date= getdate();
176 $this->shadowExpire= floor($date[0] / (60*60*24)) * 60 * 60 * 24;
177 }
179 /* Generate shell list from /etc/gosa/shells */
180 if (file_exists('/etc/gosa/shells')){
181 $shells = file ('/etc/gosa/shells');
182 foreach ($shells as $line){
183 if (!preg_match ("/^#/", $line)){
184 $this->loginShellList[]= trim($line);
185 }
186 }
187 } else {
188 if ($this->loginShell == ""){
189 $this->loginShellList[]= _("unconfigured");
190 }
191 }
193 /* Insert possibly missing loginShell */
194 if ($this->loginShell != "" && !in_array($this->loginShell, $this->loginShellList)){
195 $this->loginShellList[]= $this->loginShell;
196 }
198 /* Generate printer list */
199 if (isset($this->config->data['SERVERS']['CUPS'])){
200 $this->printerList= get_printer_list ($this->config->data['SERVERS']['CUPS']);
201 asort($this->printerList);
202 }
204 /* Generate group list */
205 $this->ui = get_userinfo();
206 $this->secondaryGroups[]= "- "._("automatic")." -";
207 $ldap->cd($this->config->current['BASE']);
208 $ldap->search("(objectClass=posixGroup)", array("cn", "gidNumber"));
209 while($attrs = $ldap->fetch()){
210 $this->secondaryGroups[$attrs['gidNumber'][0]]= $attrs['cn'][0];
211 }
212 asort ($this->secondaryGroups);
214 /* Get global filter config */
215 if (!is_global("sysfilter")){
216 $ui= get_userinfo();
217 $base= get_base_from_people($ui->dn);
218 $sysfilter= array( "depselect" => $base,
219 "regex" => "*");
220 register_global("sysfilter", $sysfilter);
221 }
222 $this->ui = get_userinfo();
223 }
226 /* execute generates the html output for this node */
227 function execute($isCopyPaste = false)
228 {
229 /* Call parent execute */
230 plugin::execute();
231 $display= "";
233 /* Department has changed? */
234 if(isset($_POST['depselect'])){
235 $_SESSION['CurrentMainBase']= validate($_POST['depselect']);
236 }
238 if(!$isCopyPaste){
239 /* Do we need to flip is_account state? */
240 if (isset($_POST['modify_state'])){
241 $this->is_account= !$this->is_account;
242 }
244 /* Do we represent a valid posixAccount? */
245 if (!$this->is_account && $this->parent == NULL ){
246 $display= "<img alt=\"\" src=\"images/stop.png\" align=\"middle\"> <b>".
247 _("This account has no unix extensions.")."</b>";
248 $display.= back_to_main();
249 return ($display);
250 }
253 /* Show tab dialog headers */
254 if ($this->parent != NULL){
255 if ($this->is_account){
256 if (isset($this->parent->by_object['sambaAccount'])){
257 $obj= $this->parent->by_object['sambaAccount'];
258 }
259 if (isset($obj) && $obj->is_account == TRUE &&
260 ((isset($this->parent->by_object['sambaAccount']))&&($this->parent->by_object['sambaAccount']->is_account))
261 ||(isset($this->parent->by_object['environment'] ))&&($this->parent->by_object['environment'] ->is_account)){
263 /* Samba3 dependency on posix accounts are enabled
264 in the moment, because I need to rely on unique
265 uidNumbers. There'll be a better solution later
266 on. */
267 $display= $this->show_disable_header(_("Remove posix account"),
268 _("This account has unix features enabled. To disable them, you'll need to remove the samba / environment account first."), TRUE);
269 } else {
270 $display= $this->show_disable_header(_("Remove posix account"),
271 _("This account has posix features enabled. You can disable them by clicking below."));
272 }
273 } else {
274 $display= $this->show_enable_header(_("Create posix account"),
275 _("This account has posix features disabled. You can enable them by clicking below."));
276 return($display);
277 }
278 }
279 }
280 /* Trigger group edit? */
281 if (isset($_POST['edit_groupmembership'])){
282 $this->group_dialog= TRUE;
283 $this->dialog= TRUE;
284 }
286 /* Cancel group edit? */
287 if (isset($_POST['add_groups_cancel']) ||
288 isset($_POST['add_groups_finish'])){
289 $this->group_dialog= FALSE;
290 $this->dialog= FALSE;
291 }
293 /* Add selected groups */
294 if (isset($_POST['add_groups_finish']) && isset($_POST['groups']) &&
295 count($_POST['groups'])){
297 /* FIX ME put the acl check into addGroup function*/
298 #if (chk acl ($this->acl, "memberUid") == ""){
299 # $this->addGroup ($_POST['groups']);
300 # $this->is_modified= TRUE;
301 #}
302 }
304 /* Delete selected groups */
305 if (isset($_POST['delete_groupmembership']) &&
306 isset($_POST['group_list']) && count($_POST['group_list'])){
308 /* FIX ME put the acl check into addGroup function*/
309 #if (chk acl ($this->acl, "memberUid") == ""){
310 # $this->delGroup ($_POST['group_list']);
311 # $this->is_modified= TRUE;
312 #}
313 }
315 /* Add user workstation? */
316 if (isset($_POST["add_ws"])){
317 $this->show_ws_dialog= TRUE;
318 $this->dialog= TRUE;
319 }
321 /* Add user workstation? */
322 if (isset($_POST["add_ws_finish"]) && isset($_POST['wslist'])){
323 foreach($_POST['wslist'] as $ws){
324 $this->accessTo[$ws]= $ws;
325 }
326 ksort($this->accessTo);
327 $this->is_modified= TRUE;
328 }
330 /* Remove user workstations? */
331 if (isset($_POST["delete_ws"]) && isset($_POST['workstation_list'])){
332 foreach($_POST['workstation_list'] as $name){
333 unset ($this->accessTo[$name]);
334 }
335 $this->is_modified= TRUE;
336 }
338 /* Add user workstation finished? */
339 if (isset($_POST["add_ws_finish"]) || isset($_POST["add_ws_cancel"])){
340 $this->show_ws_dialog= FALSE;
341 $this->dialog= FALSE;
342 }
344 /* Templates now! */
345 $smarty= get_smarty();
347 /* Show ws dialog */
348 if ($this->show_ws_dialog){
349 /* Save data */
350 $sysfilter= get_global("sysfilter");
351 foreach( array("depselect", "regex") as $type){
352 if (isset($_POST[$type])){
353 $sysfilter[$type]= $_POST[$type];
354 }
355 }
356 if (isset($_GET['search'])){
357 $s= mb_substr($_GET['search'], 0, 1, "UTF8")."*";
358 if ($s == "**"){
359 $s= "*";
360 }
361 $sysfilter['regex']= $s;
362 }
363 register_global("sysfilter", $sysfilter);
365 /* Get workstation list */
366 $exclude= "";
367 foreach($this->accessTo as $ws){
368 $exclude.= "(cn=$ws)";
369 }
370 if ($exclude != ""){
371 $exclude= "(!(|$exclude))";
372 }
373 $acl= array($this->config->current['BASE'] => ":all");
374 $regex= $sysfilter['regex'];
375 $filter= "(&(|(objectClass=goServer)(objectClass=gotoWorkstation)(objectClass=gotoTerminal))$exclude(cn=*)(cn=$regex))";
376 $res= get_list($filter, $acl, $sysfilter['depselect'], array("cn"), GL_SUBSEARCH | GL_SIZELIMIT);
377 $wslist= array();
378 foreach ($res as $attrs){
379 $wslist[]= preg_replace('/\$/', '', $attrs['cn'][0]);
380 }
381 asort($wslist);
382 $smarty->assign("search_image", get_template_path('images/search.png'));
383 $smarty->assign("launchimage", get_template_path('images/small_filter.png'));
384 $smarty->assign("tree_image", get_template_path('images/tree.png'));
385 $smarty->assign("deplist", $this->config->idepartments);
386 $smarty->assign("alphabet", generate_alphabet());
387 foreach( array("depselect", "regex") as $type){
388 $smarty->assign("$type", $sysfilter[$type]);
389 }
390 $smarty->assign("hint", print_sizelimit_warning());
391 $smarty->assign("wslist", $wslist);
392 $smarty->assign("apply", apply_filter());
393 $display= $smarty->fetch (get_template_path('trust_machines.tpl', TRUE, dirname(__FILE__)));
394 return ($display);
395 }
397 /* Manage group add dialog */
398 if ($this->group_dialog){
400 /* Get global filter config */
401 $this->reload();
403 /* remove already assigned groups */
404 $glist= array();
405 foreach ($this->grouplist as $key => $value){
406 if (!isset($this->groupMembership[$key]) && obj_is_writable($key,"group","memberUid")){
407 $glist[$key]= $value;
408 }
409 }
411 if($this->SubSearch){
412 $smarty->assign("SubSearchCHK"," checked ");
413 }else{
414 $smarty->assign("SubSearchCHK","");
415 }
417 $smarty->assign("regex",$this->GroupRegex);
418 $smarty->assign("guser",$this->GroupUserRegex);
419 $smarty->assign("groups", $glist);
420 $smarty->assign("search_image", get_template_path('images/search.png'));
421 $smarty->assign("launchimage", get_template_path('images/small_filter.png'));
422 $smarty->assign("tree_image", get_template_path('images/tree.png'));
423 $smarty->assign("deplist", $this->config->idepartments);
424 $smarty->assign("alphabet", generate_alphabet());
425 $smarty->assign("depselect",$_SESSION['CurrentMainBase']);
426 $smarty->assign("hint", print_sizelimit_warning());
428 $smarty->assign("apply", apply_filter());
429 $display.= $smarty->fetch (get_template_path('posix_groups.tpl', TRUE, dirname(__FILE__)));
430 return ($display);
431 }
433 /* Show main page */
434 $smarty= get_smarty();
436 /* Depending on pwmode, currently hardcoded because there are no other methods */
437 if ( 1 == 1 ){
438 $smarty->assign("pwmode", dirname(__FILE__)."/posix_shadow");
440 $shadowMinACL = $this->getacl("shadowMin");
441 $smarty->assign("shadowmins", sprintf(_("Password can't be changed up to %s days after last change"),
442 "<input name=\"shadowMin\" size=3 maxlength=4 $shadowMinACL value=\"".$this->shadowMin."\">"));
444 $shadowMaxACL = $this->getacl("shadowMax");
445 $smarty->assign("shadowmaxs", sprintf(_("Password must be changed after %s days"),
446 "<input name=\"shadowMax\" size=3 maxlength=4 $shadowMaxACL value=\"".$this->shadowMax."\">"));
448 $shadowInactiveACL= $this->getacl("shadowInactive");
449 $smarty->assign("shadowinactives", sprintf(_("Disable account after %s days of inactivity after password expiery"),
450 "<input name=\"shadowInactive\" size=3 maxlength=4 $shadowInactiveACL value=\"".$this->shadowInactive."\">"));
452 $shadowWarningACL = $this->getacl("shadowWarning");
453 $smarty->assign("shadowwarnings", sprintf(_("Warn user %s days before password expiery"),
454 "<input name=\"shadowWarning\" size=3 maxlength=4 $shadowWarningACL value=\"".$this->shadowWarning."\">"));
456 foreach( array("use_shadowMin", "use_shadowMax",
457 "use_shadowExpire", "use_shadowInactive","use_shadowWarning") as $val){
458 if ($this->$val == 1){
459 $smarty->assign("$val", "checked");
460 } else {
461 $smarty->assign("$val", "");
462 }
463 $smarty->assign("$val"."ACL", $this->getacl($val));
464 }
466 if($this->mustchangepassword){
467 $smarty->assign("mustchangepassword", "checked");
468 } else {
469 $smarty->assign("mustchangepassword", "");
470 }
471 $smarty->assign("mustchangepasswordACL", $this->getacl("mustchangepassword"));
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 }
531 $smarty->assign("force_idsACL", $this->getacl("uidNumber").$this->getacl("gidNumber"));
533 /* Load attributes and acl's */
534 foreach($this->attributes as $val){
535 if(($_SESSION["js"])&&(($val=="uidNumber")||($val=="gidNumber")))
536 {
537 $smarty->assign("$val"."ACL",$this->getacl($val));
538 $smarty->assign("$val", $this->$val);
539 continue;
540 }
541 $smarty->assign("$val", $this->$val);
542 $smarty->assign("$val"."ACL", $this->getacl($val));
543 }
544 $smarty->assign("groupMembershipACL","rw");// $this->getacl("groupMembership"));
545 $smarty->assign("status", $this->status);
547 /* Work on trust modes */
548 $smarty->assign("trustmodeACL", $this->getacl("trustModel"));
549 if ($this->trustModel == "fullaccess"){
550 $trustmode= 1;
551 // pervent double disable tag in html code, this will disturb our clean w3c html
552 $smarty->assign("trustmode", $this->getacl("trustModel"));
554 } elseif ($this->trustModel == "byhost"){
555 $trustmode= 2;
556 $smarty->assign("trusthide", "");
557 } else {
558 // pervent double disable tag in html code, this will disturb our clean w3c html
559 $smarty->assign("trustmode", $this->getacl("trustModel"));
560 $trustmode= 0;
561 }
562 $smarty->assign("trustmode", $trustmode);
563 $smarty->assign("trustmodes", array( 0 => _("disabled"), 1 => _("full access"),
564 2 => _("allow access to these hosts")));
568 if((count($this->accessTo))==0)
569 $smarty->assign("emptyArrAccess",true);
570 else
571 $smarty->assign("emptyArrAccess",false);
575 $smarty->assign("workstations", $this->accessTo);
577 $smarty->assign("apply", apply_filter());
578 $display.= $smarty->fetch (get_template_path('generic.tpl', TRUE, dirname(__FILE__)));
579 return($display);
580 }
583 /* remove object from parent */
584 function remove_from_parent()
585 {
586 /* Cancel if there's nothing to do here */
587 if (!$this->initially_was_account){
588 return;
589 }
591 /* include global link_info */
592 $ldap= $this->config->get_ldap_link();
594 /* Remove and write to LDAP */
595 plugin::remove_from_parent();
597 /* Zero out array */
598 $this->attrs['gosaHostACL']= array();
600 /* Keep uid, because we need it for authentification! */
601 unset($this->attrs['uid']);
602 unset($this->attrs['trustModel']);
604 @DEBUG (DEBUG_LDAP, __LINE__, __FUNCTION__, __FILE__,
605 $this->attributes, "Save");
606 $ldap->cd($this->dn);
607 $this->cleanup();
608 $ldap->modify ($this->attrs);
610 show_ldap_error($ldap->get_error(), sprintf(_("Removing of user/posix account with dn '%s' failed."),$this->dn));
612 /* Delete group only if cn is uid and there are no other
613 members inside */
614 $ldap->cd ($this->config->current['BASE']);
615 $ldap->search ("(&(objectClass=posixGroup)(gidNumber=".$this->gidNumber."))", array("cn", "memberUid"));
616 if ($ldap->count() != 0){
617 $attrs= $ldap->fetch();
618 if ($attrs['cn'][0] == $this->uid &&
619 !isset($this->attrs['memberUid'])){
621 $ldap->rmDir($ldap->getDN());
622 }
623 }
625 /* Optionally execute a command after we're done */
626 $this->handle_post_events("remove");
627 }
630 function save_object()
631 {
632 if (isset($_POST['posixTab'])){
633 /* Save values to object */
634 plugin::save_object();
636 /* Save force GID checkbox */
637 if (isset ($_POST['force_ids'])){
638 $data= 1;
639 } else {
640 $data= 0;
641 }
642 if ($this->force_ids != $data){
643 $this->is_modified= TRUE;
644 }
645 $this->force_ids= $data;
648 /*Save primary group settings */
649 $data= $_POST['primaryGroup'];
650 if ($this->primaryGroup != $data){
651 $this->is_modified= TRUE;
652 }
653 $this->primaryGroup= $_POST['primaryGroup'];
655 foreach(array("shadowMin","shadowMax","shadowExpire","shadowInactive","shadowWarning","mustchangepassword") as $var) {
656 if($this->acl_is_writeable($var)){
657 $use_var = "use_".$var;
658 if(isset($_POST['use_'.$var])){
659 $this->$use_var = true;
660 $this->$var = $_POST['shadowMin'];
661 }else{
662 $this->$use_var = false;
663 $this->$var = 0;
664 }
665 }
666 }
668 /* Trust mode - special handling */
669 if (isset($_POST['trustmode'])){
670 $saved= $this->trustModel;
671 if ($_POST['trustmode'] == "1"){
672 $this->trustModel= "fullaccess";
673 } elseif ($_POST['trustmode'] == "2"){
674 $this->trustModel= "byhost";
675 } else {
676 $this->trustModel= "";
677 }
678 if ($this->trustModel != $saved){
679 $this->is_modified= TRUE;
680 }
681 }
682 }
684 /* Get regex from alphabet */
685 if(isset($_GET['search'])){
686 $this->GroupRegex = $_GET['search']."*";
687 }
689 /* Check checkboxes and regexes */
690 if(isset($_POST["PosixGroupDialogPosted"])){
692 if(isset($_POST['SubSearch']) && ($_POST['SubSearch'])){
693 $this->SubSearch = true;
694 }else{
695 $this->SubSearch = false;
696 }
697 if(isset($_POST['guser'])){
698 $this->GroupUserRegex = $_POST['guser'];
699 }
700 if(isset($_POST['regex'])){
701 $this->GroupRegex = $_POST['regex'];
702 }
703 }
704 $this->GroupRegex = preg_replace("/\*\**/","*",$this->GroupRegex);
705 $this->GroupUserRegex = preg_replace("/\*\**/","*",$this->GroupUserRegex);
706 }
709 /* Save data to LDAP, depending on is_account we save or delete */
710 function save()
711 {
713 /* include global link_info */
714 $ldap= $this->config->get_ldap_link();
716 /* Adapt shadow values */
717 if (!$this->use_shadowExpire){
718 $this->shadowExpire= "0";
719 } else {
720 /* Transform seconds to days here */
721 $this->shadowExpire= (int)($this->shadowExpire / (60 * 60 * 24)) ;
722 }
723 if (!$this->use_shadowMax){
724 $this->shadowMax= "0";
725 }
726 if ($this->mustchangepassword){
727 $this->shadowLastChange= (int)(date("U") / 86400) - $this->shadowMax - 1;
728 } else {
729 $this->shadowLastChange= (int)(date("U") / 86400);
730 }
731 if (!$this->use_shadowWarning){
732 $this->shadowWarning= "0";
733 }
735 /* Check what to do with ID's */
736 if ($this->force_ids == 0){
738 /* Use id's that are already set */
739 if ($this->savedUidNumber != ""){
740 $this->uidNumber= $this->savedUidNumber;
741 $this->gidNumber= $this->savedGidNumber;
742 } else {
744 /* Calculate new id's. We need to place a lock before calling get_next_id
745 to get real unique values. */
746 $wait= 10;
747 while (get_lock("uidnumber") != ""){
748 sleep (1);
750 /* Oups - timed out */
751 if ($wait-- == 0){
752 print_red (_("Failed: overriding lock"));
753 break;
754 }
755 }
757 add_lock ("uidnumber", "gosa");
758 $this->uidNumber= $this->get_next_id("uidNumber");
759 if ($this->savedGidNumber != ""){
760 $this->gidNumber= $this->savedGidNumber;
761 } else {
762 $this->gidNumber= $this->get_next_id("gidNumber");
763 }
764 }
766 if ($this->primaryGroup != 0){
767 $this->gidNumber= $this->primaryGroup;
768 }
769 }
771 if ($this->use_shadowMin != "1" ) {
772 $this->shadowMin = "";
773 }
775 if (($this->use_shadowMax != "1") && ($this->mustchangepassword != "1")) {
776 $this->shadowMax = "";
777 }
779 if ($this->use_shadowWarning != "1" ) {
780 $this->shadowWarning = "";
781 }
783 if ($this->use_shadowInactive != "1" ) {
784 $this->shadowInactive = "";
785 }
787 if ($this->use_shadowExpire != "1" ) {
788 $this->shadowExpire = "";
789 }
791 /* Fill gecos */
792 if (isset($this->parent) && $this->parent != NULL){
793 $this->gecos= rewrite($this->parent->by_object['user']->cn);
794 if (!preg_match('/[a-z0-9 -]/i', $this->gecos)){
795 $this->gecos= "";
796 }
797 }
799 foreach(array("shadowMin","shadowMax","shadowWarning","shadowInactive","shadowExpire") as $attr){
800 $this->$attr = (int) $this->$attr;
801 }
802 /* Call parents save to prepare $this->attrs */
803 plugin::save();
805 /* Trust accounts */
806 $objectclasses= array();
807 foreach ($this->attrs['objectClass'] as $key => $class){
808 if (preg_match('/trustAccount/i', $class)){
809 continue;
810 }
811 $objectclasses[]= $this->attrs['objectClass'][$key];
812 }
813 $this->attrs['objectClass']= $objectclasses;
814 if ($this->trustModel != ""){
815 $this->attrs['objectClass'][]= "trustAccount";
816 $this->attrs['trustModel']= $this->trustModel;
817 $this->attrs['accessTo']= array();
818 if ($this->trustModel == "byhost"){
819 foreach ($this->accessTo as $host){
820 $this->attrs['accessTo'][]= $host;
821 }
822 }
823 } else {
824 if ($this->was_trust_account){
825 $this->attrs['accessTo']= array();
826 $this->attrs['trustModel']= array();
827 }
828 }
830 if(empty($this->attrs['gosaDefaultPrinter'])){
831 $thid->attrs['gosaDefaultPrinter']=array();
832 }
835 /* Save data to LDAP */
836 $ldap->cd($this->dn);
837 $this->cleanup();
838 unset($this->attrs['uid']);
839 $ldap->modify ($this->attrs);
841 show_ldap_error($ldap->get_error(), sprintf(_("Saving of user/posix account with dn '%s' failed."),$this->dn));
843 /* Remove lock needed for unique id generation */
844 del_lock ("uidnumber");
847 /* Posix accounts have group interrelationship, take care about these here. */
848 if ($this->force_ids == 0 && $this->primaryGroup == 0){
849 $ldap->cd($this->config->current['BASE']);
850 $ldap->search("(&(objectClass=posixGroup)(gidNumber=".$this->gidNumber."))", array("cn"));
852 /* Create group if it doesn't exist */
853 if ($ldap->count() == 0){
854 $groupdn= preg_replace ('/^'.$this->config->current['DNMODE'].'=[^,]+,'.get_people_ou().'/i', 'cn='.$this->uid.','.get_groups_ou(), $this->dn);
856 $g= new group($this->config, $groupdn);
857 $g->cn= $this->uid;
858 $g->force_gid= 1;
859 $g->gidNumber= $this->gidNumber;
860 $g->description= "Group of user ".$this->givenName." ".$this->sn;
861 $g->save ();
862 }
863 }
865 /* Take care about groupMembership values: add to groups */
866 foreach ($this->groupMembership as $key => $value){
867 $g= new grouptabs($this->config,$this->config->data['TABS']['GROUPTABS'], $key);
868 $g->by_object['group']->addUser($this->uid);
869 $g->save();
870 }
872 /* Remove from groups not listed in groupMembership */
873 foreach ($this->savedGroupMembership as $key => $value){
874 if (!isset($this->groupMembership[$key])){
875 $g= new grouptabs($this->config,$this->config->data['TABS']['GROUPTABS'], $key);
876 $g->by_object['group']->removeUser ($this->uid);
877 $g->save();
878 }
879 }
881 /* Optionally execute a command after we're done */
882 if ($this->initially_was_account == $this->is_account){
883 if ($this->is_modified){
884 $this->handle_post_events("mofify");
885 }
886 } else {
887 $this->handle_post_events("add");
888 }
889 }
891 /* Check formular input */
892 function check()
893 {
894 /* Include global link_info */
895 $ldap= $this->config->get_ldap_link();
897 /* Call common method to give check the hook */
898 $message= plugin::check();
900 /* must: homeDirectory */
901 if ($this->homeDirectory == ""){
902 $message[]= _("The required field 'Home directory' is not set.");
903 }
904 if (!is_path($this->homeDirectory)){
905 $message[]= _("Please enter a valid path in 'Home directory' field.");
906 }
908 /* Check ID's if they are forced by user */
909 if ($this->force_ids == "1"){
911 /* Valid uid/gid? */
912 if (!is_id($this->uidNumber)){
913 $message[]= _("Value specified as 'UID' is not valid.");
914 } else {
915 if ($this->uidNumber < $this->config->current['MINID']){
916 $message[]= _("Value specified as 'UID' is too small.");
917 }
918 }
919 if (!is_id($this->gidNumber)){
920 $message[]= _("Value specified as 'GID' is not valid.");
921 } else {
922 if ($this->gidNumber < $this->config->current['MINID']){
923 $message[]= _("Value specified as 'GID' is too small.");
924 }
925 }
926 }
928 /* Check shadow settings, well I like spaghetties... */
929 if ($this->use_shadowMin){
930 if (!is_id($this->shadowMin)){
931 $message[]= _("Value specified as 'shadowMin' is not valid.");
932 }
933 }
934 if ($this->use_shadowMax){
935 if (!is_id($this->shadowMax)){
936 $message[]= _("Value specified as 'shadowMax' is not valid.");
937 }
938 }
939 if ($this->use_shadowWarning){
940 if (!is_id($this->shadowWarning)){
941 $message[]= _("Value specified as 'shadowWarning' is not valid.");
942 }
943 if (!$this->use_shadowMax){
944 $message[]= _("'shadowWarning' without 'shadowMax' makes no sense.");
945 }
946 if ($this->shadowWarning > $this->shadowMax){
947 $message[]= _("Value specified as 'shadowWarning' should be smaller than 'shadowMax'.");
948 }
949 if ($this->use_shadowMin && $this->shadowWarning < $this->shadowMin){
950 $message[]= _("Value specified as 'shadowWarning' should be greater than 'shadowMin'.");
951 }
952 }
953 if ($this->use_shadowInactive){
954 if (!is_id($this->shadowInactive)){
955 $message[]= _("Value specified as 'shadowInactive' is not valid.");
956 }
957 if (!$this->use_shadowMax){
958 $message[]= _("'shadowInactive' without 'shadowMax' makes no sense.");
959 }
960 }
961 if ($this->use_shadowMin && $this->use_shadowMax){
962 if ($this->shadowMin > $this->shadowMax){
963 $message[]= _("Value specified as 'shadowMin' should be smaller than 'shadowMax'.");
964 }
965 }
967 // if(empty($this->gosaDefaultPrinter)){
968 // $message[]= _("You need to specify a valid default printer.");
969 // }
971 return ($message);
972 }
974 function addGroup ($groups)
975 {
976 /* include global link_info */
977 $ldap= $this->config->get_ldap_link();
979 /* Walk through groups and add the descriptive entry if not exists */
980 foreach ($groups as $value){
981 if (!array_key_exists($value, $this->groupMembership)){
982 $ldap->cat($value, array('cn', 'description', 'dn'));
983 $attrs= $ldap->fetch();
984 error_reporting (0);
985 if (!isset($attrs['description'][0])){
986 $entry= $attrs["cn"][0];
987 } else {
988 $dsc= preg_replace ('/^Group of user/', _("Group of user"), $attrs["description"][0]);
989 $entry= $attrs["cn"][0]." [$dsc]";
990 }
991 error_reporting (E_ALL);
992 $this->groupMembership[$attrs['dn']]= $entry;
993 }
994 }
996 /* Sort groups */
997 asort ($this->groupMembership);
998 reset ($this->groupMembership);
999 }
1002 /* Del posix user from some groups */
1003 function delGroup ($groups)
1004 {
1005 $dest= array();
1007 foreach ($this->groupMembership as $key => $value){
1008 if (!in_array($key, $groups)){
1009 $dest[$key]= $value;
1010 }
1011 }
1012 $this->groupMembership= $dest;
1013 }
1015 /* Adapt from template, using 'dn' */
1016 function adapt_from_template($dn)
1017 {
1018 /* Include global link_info */
1019 $ldap= $this->config->get_ldap_link();
1021 plugin::adapt_from_template($dn);
1022 $template= $this->attrs['uid'][0];
1024 /* Adapt group membership */
1025 $ldap->cd($this->config->current['BASE']);
1026 $ldap->search("(&(objectClass=posixGroup)(memberUid=".$this->attrs["uid"][0]."))", array("description", "cn"));
1028 while ($this->attrs= $ldap->fetch()){
1029 if (!isset($this->attrs["description"][0])){
1030 $entry= $this->attrs["cn"][0];
1031 } else {
1032 $entry= $this->attrs["cn"][0]." [".$this->attrs["description"][0]."]";
1033 }
1034 $this->groupMembership[$ldap->getDN()]= $entry;
1035 }
1037 /* Fix primary group settings */
1038 $ldap->cd($this->config->current['BASE']);
1039 $ldap->search("(&(objectClass=posixGroup)(cn=$template)(gidNumber=".$this->gidNumber."))", array("cn"));
1040 if ($ldap->count() != 1){
1041 $this->primaryGroup= $this->gidNumber;
1042 }
1044 $ldap->cd($this->config->current['BASE']);
1045 $ldap->search("(&(objectClass=gosaUserTemplate)(uid=".$template."))", array("cn","accessTo"));
1046 while($attr = $ldap->fetch()){
1047 $tmp = $attr['accessTo'];
1048 unset ($tmp['count']);
1049 $this->accessTo = $tmp;
1050 }
1052 /* Adjust shadow checkboxes */
1053 foreach (array("shadowMin", "shadowMax", "shadowWarning", "shadowInactive",
1054 "shadowExpire") as $val){
1056 if ($this->$val != 0){
1057 $oval= "use_".$val;
1058 $this->$oval= "1";
1059 }
1060 }
1061 }
1063 function get_next_id($attrib)
1064 {
1065 $ids= array();
1066 $ldap= $this->config->get_ldap_link();
1068 $ldap->cd ($this->config->current['BASE']);
1069 if (preg_match('/gidNumber/i', $attrib)){
1070 $oc= "posixGroup";
1071 } else {
1072 $oc= "posixAccount";
1073 }
1074 $ldap->search ("(&(objectClass=$oc)($attrib=*))", array("$attrib"));
1076 /* Get list of ids */
1077 while ($attrs= $ldap->fetch()){
1078 $ids[]= (int)$attrs["$attrib"][0];
1079 }
1081 /* Find out next free id near to UID_BASE */
1082 for ($id= $this->config->current['UIDBASE']; $id++; $id<65000){
1083 if (!in_array($id, $ids)){
1084 return ($id);
1085 }
1086 }
1088 /* Should not happen */
1089 if ($id == 65000){
1090 print_red(_("Too many users, can't allocate a free ID!"));
1091 exit;
1092 }
1094 }
1096 function reload()
1097 {
1098 /* Set base for all searches */
1099 $base = $_SESSION['CurrentMainBase'];
1100 $base = $base;
1101 $ldap = $this->config->get_ldap_link();
1102 $attrs = array("cn", "description", "gidNumber");
1103 $Flags = GL_SIZELIMIT;
1105 /* Get groups */
1106 if ($this->GroupUserRegex == '*'){
1107 $filter = "(&(objectClass=posixGroup)(cn=".$this->GroupRegex."))";
1108 } else {
1109 $filter= "(&(objectClass=posixGroup)(cn=".$this->GroupRegex.")(memberUid=".$this->GroupUserRegex."))";
1110 }
1111 if($this->SubSearch){
1112 $Flags |= GL_SUBSEARCH;
1113 }else{
1114 $base = get_groups_ou().$base;
1115 }
1118 $res= get_list($filter, $this->ui->subtreeACL, $base,$attrs, $Flags);
1120 /* check sizelimit */
1121 if (preg_match("/size limit/i", $ldap->error)){
1122 $_SESSION['limit_exceeded']= TRUE;
1123 }
1125 /* Create a list of users */
1126 $this->grouplist = array();
1127 foreach ($res as $value){
1128 $this->grouplist[$value['gidNumber'][0]]= $value;
1129 }
1131 $tmp=array();
1132 foreach($this->grouplist as $tkey => $val ){
1133 $tmp[strtolower($val['cn'][0]).$val['cn'][0]]=$val;
1134 }
1136 /* Sort index */
1137 ksort($tmp);
1139 /* Recreate index array[dn]=cn[description]*/
1140 $this->grouplist=array();
1141 foreach($tmp as $val){
1142 if(isset($val['description'])){
1143 $this->grouplist[$val['dn']]=$val['cn'][0]." [".$val['description'][0]."]";
1144 }else{
1145 $this->grouplist[$val['dn']]=$val['cn'][0];
1146 }
1147 }
1148 reset ($this->grouplist);
1149 }
1152 /* Create the posix dialog part for copy & paste */
1153 function getCopyDialog()
1154 {
1155 /* Skip dialog creation if this is not a valid account*/
1156 if(!$this->is_account) return("");
1157 if ($this->force_ids == 1){
1158 $force_ids = "checked";
1159 if ($_SESSION['js']){
1160 $forceMode = "";
1161 }
1162 } else {
1163 if ($_SESSION['js']){
1164 if($this->acl != "#none#")
1165 $forceMode ="disabled";
1166 }
1167 $force_ids = "";
1168 }
1170 $sta = "";
1172 /* Open group add dialog */
1173 if(isset($_POST['edit_groupmembership'])){
1174 $this->group_dialog = TRUE;
1175 $sta = "SubDialog";
1176 }
1178 /* If the group-add dialog is closed, call execute
1179 to ensure that the membership is updatd */
1180 if(isset($_POST['add_groups_finish']) || isset($_POST['add_groups_cancel'])){
1181 $this->execute();
1182 $this->group_dialog =FALSE;
1183 }
1185 if($this->group_dialog){
1186 $str = $this->execute(true);
1187 $ret = array();
1188 $ret['string'] = $str;
1189 $ret['status'] = $sta;
1190 return($ret);
1191 }
1193 /* If a group member should be deleted, simply call execute */
1194 if(isset($_POST['delete_groupmembership'])){
1195 $this->execute();
1196 }
1198 /* Assigned informations to smarty */
1199 $smarty = get_smarty();
1200 $smarty->assign("homeDirectory",$this->homeDirectory);
1201 $smarty->assign("uidNumber",$this->uidNumber);
1202 $smarty->assign("gidNumber",$this->gidNumber);
1203 $smarty->assign("forceMode",$forceMode);
1204 $smarty->assign("force_ids",$force_ids);
1205 if (!count($this->groupMembership)){
1206 $smarty->assign("groupMembership", array(" "));
1207 } else {
1208 $smarty->assign("groupMembership", $this->groupMembership);
1209 }
1211 /* Display wars message if there are more than 16 group members */
1212 if (count($this->groupMembership) > 16){
1213 $smarty->assign("groups", "too_many_for_nfs");
1214 } else {
1215 $smarty->assign("groups", "");
1216 }
1217 $str = $smarty->fetch(get_template_path("paste_generic.tpl",TRUE,dirname(__FILE__)));
1219 $ret = array();
1220 $ret['string'] = $str;
1221 $ret['status'] = $sta;
1222 return($ret);
1223 }
1226 function plInfo()
1227 {
1228 return (array(
1229 "plDescription" => _("POSIX account"),
1230 "plSelfModify" => TRUE,
1231 "plDepends" => array("user"),
1232 "plPriority" => 1,
1233 "plSection" => "personal",
1234 "plCategory" => array("users"),
1235 "plOptions" => array(),
1237 "plProvidedAcls" => array(
1239 "homeDirectory" => _("Home directory"),
1240 "loginShell" => _("Shell"),
1241 "uidNumber" => _("User ID"),
1242 "gidNumber" => _("Group ID"),
1244 "mustchangepassword"=> _("Force password change on login"),
1245 "shadowMin" => _("Shadow min"),
1246 "shadowMax" => _("Shadow max"),
1247 "shadowWarning" => _("Shadow warning"),
1248 "shadowInactive" => _("Shadow inactive"),
1249 "shadowExpire" => _("Shadow expire"),
1250 "trustModel" => _("System trust model")))
1251 );
1252 }
1253 }
1255 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
1256 ?>