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){
240 /* Do we need to flip is_account state? */
241 if(isset($_POST['modify_state'])){
242 if($this->is_account && $this->acl_is_removeable()){
243 $this->is_account= FALSE;
244 }elseif(!$this->is_account && $this->acl_is_createable()){
245 $this->is_account= TRUE;
246 }
247 }
249 /* Do we represent a valid posixAccount? */
250 if (!$this->is_account && $this->parent == NULL ){
251 $display= "<img alt=\"\" src=\"images/stop.png\" align=\"middle\"> <b>".
252 _("This account has no unix extensions.")."</b>";
253 $display.= back_to_main();
254 return ($display);
255 }
258 /* Show tab dialog headers */
259 if ($this->parent != NULL){
260 if ($this->is_account){
261 if (isset($this->parent->by_object['sambaAccount'])){
262 $obj= $this->parent->by_object['sambaAccount'];
263 }
264 if (isset($obj) && $obj->is_account == TRUE &&
265 ((isset($this->parent->by_object['sambaAccount']))&&($this->parent->by_object['sambaAccount']->is_account))
266 ||(isset($this->parent->by_object['environment'] ))&&($this->parent->by_object['environment'] ->is_account)){
268 /* Samba3 dependency on posix accounts are enabled
269 in the moment, because I need to rely on unique
270 uidNumbers. There'll be a better solution later
271 on. */
272 $display= $this->show_disable_header(_("Remove posix account"),
273 _("This account has unix features enabled. To disable them, you'll need to remove the samba / environment account first."), TRUE);
274 } else {
275 $display= $this->show_disable_header(_("Remove posix account"),
276 _("This account has posix features enabled. You can disable them by clicking below."));
277 }
278 } else {
279 $display= $this->show_enable_header(_("Create posix account"),
280 _("This account has posix features disabled. You can enable them by clicking below."));
281 return($display);
282 }
283 }
284 }
285 /* Trigger group edit? */
286 if (isset($_POST['edit_groupmembership'])){
287 $this->group_dialog= TRUE;
288 $this->dialog= TRUE;
289 }
291 /* Cancel group edit? */
292 if (isset($_POST['add_groups_cancel']) ||
293 isset($_POST['add_groups_finish'])){
294 $this->group_dialog= FALSE;
295 $this->dialog= FALSE;
296 }
298 /* Add selected groups */
299 if (isset($_POST['add_groups_finish']) && isset($_POST['groups']) &&
300 count($_POST['groups'])){
302 echo "FIXME, 302, put the acl check into addGroup function ";
303 #if (chk acl ($this->acl, "memberUid") == ""){
304 # $this->addGroup ($_POST['groups']);
305 # $this->is_modified= TRUE;
306 #}
307 }
309 /* Delete selected groups */
310 if (isset($_POST['delete_groupmembership']) &&
311 isset($_POST['group_list']) && count($_POST['group_list'])){
313 echo "FIXME, 302, put the acl check into addGroup function ";
314 #if (chk acl ($this->acl, "memberUid") == ""){
315 # $this->delGroup ($_POST['group_list']);
316 # $this->is_modified= TRUE;
317 #}
318 }
320 /* Add user workstation? */
321 if (isset($_POST["add_ws"])){
322 $this->show_ws_dialog= TRUE;
323 $this->dialog= TRUE;
324 }
326 /* Add user workstation? */
327 if (isset($_POST["add_ws_finish"]) && isset($_POST['wslist'])){
328 foreach($_POST['wslist'] as $ws){
329 $this->accessTo[$ws]= $ws;
330 }
331 ksort($this->accessTo);
332 $this->is_modified= TRUE;
333 }
335 /* Remove user workstations? */
336 if (isset($_POST["delete_ws"]) && isset($_POST['workstation_list'])){
337 foreach($_POST['workstation_list'] as $name){
338 unset ($this->accessTo[$name]);
339 }
340 $this->is_modified= TRUE;
341 }
343 /* Add user workstation finished? */
344 if (isset($_POST["add_ws_finish"]) || isset($_POST["add_ws_cancel"])){
345 $this->show_ws_dialog= FALSE;
346 $this->dialog= FALSE;
347 }
349 /* Templates now! */
350 $smarty= get_smarty();
352 /* Show ws dialog */
353 if ($this->show_ws_dialog){
354 /* Save data */
355 $sysfilter= get_global("sysfilter");
356 foreach( array("depselect", "regex") as $type){
357 if (isset($_POST[$type])){
358 $sysfilter[$type]= $_POST[$type];
359 }
360 }
361 if (isset($_GET['search'])){
362 $s= mb_substr($_GET['search'], 0, 1, "UTF8")."*";
363 if ($s == "**"){
364 $s= "*";
365 }
366 $sysfilter['regex']= $s;
367 }
368 register_global("sysfilter", $sysfilter);
370 /* Get workstation list */
371 $exclude= "";
372 foreach($this->accessTo as $ws){
373 $exclude.= "(cn=$ws)";
374 }
375 if ($exclude != ""){
376 $exclude= "(!(|$exclude))";
377 }
378 $regex= $sysfilter['regex'];
379 $filter= "(&(|(objectClass=goServer)(objectClass=gotoWorkstation)(objectClass=gotoTerminal))$exclude(cn=*)(cn=$regex))";
380 $res= get_list($filter, "groups", $sysfilter['depselect'], array("cn"), GL_SUBSEARCH | GL_SIZELIMIT);
381 $wslist= array();
382 foreach ($res as $attrs){
383 $wslist[]= preg_replace('/\$/', '', $attrs['cn'][0]);
384 }
385 asort($wslist);
386 $smarty->assign("search_image", get_template_path('images/search.png'));
387 $smarty->assign("launchimage", get_template_path('images/small_filter.png'));
388 $smarty->assign("tree_image", get_template_path('images/tree.png'));
389 $smarty->assign("deplist", $this->config->idepartments);
390 $smarty->assign("alphabet", generate_alphabet());
391 foreach( array("depselect", "regex") as $type){
392 $smarty->assign("$type", $sysfilter[$type]);
393 }
394 $smarty->assign("hint", print_sizelimit_warning());
395 $smarty->assign("wslist", $wslist);
396 $smarty->assign("apply", apply_filter());
397 $display= $smarty->fetch (get_template_path('trust_machines.tpl', TRUE, dirname(__FILE__)));
398 return ($display);
399 }
401 /* Manage group add dialog */
402 if ($this->group_dialog){
404 /* Get global filter config */
405 $this->reload();
407 /* remove already assigned groups */
408 $glist= array();
409 foreach ($this->grouplist as $key => $value){
410 if (!isset($this->groupMembership[$key]) && obj_is_writable($key,"group","memberUid",$SkipWrite)){
411 $glist[$key]= $value;
412 }
413 }
415 if($this->SubSearch){
416 $smarty->assign("SubSearchCHK"," checked ");
417 }else{
418 $smarty->assign("SubSearchCHK","");
419 }
421 $smarty->assign("regex",$this->GroupRegex);
422 $smarty->assign("guser",$this->GroupUserRegex);
423 $smarty->assign("groups", $glist);
424 $smarty->assign("search_image", get_template_path('images/search.png'));
425 $smarty->assign("launchimage", get_template_path('images/small_filter.png'));
426 $smarty->assign("tree_image", get_template_path('images/tree.png'));
427 $smarty->assign("deplist", $this->config->idepartments);
428 $smarty->assign("alphabet", generate_alphabet());
429 $smarty->assign("depselect",$_SESSION['CurrentMainBase']);
430 $smarty->assign("hint", print_sizelimit_warning());
432 $smarty->assign("apply", apply_filter());
433 $display.= $smarty->fetch (get_template_path('posix_groups.tpl', TRUE, dirname(__FILE__)));
434 return ($display);
435 }
437 /* Show main page */
438 $smarty= get_smarty();
440 /* In 'MyAccount' mode, we must remove write acls if we are not in editing mode. */
441 $SkipWrite = (!isset($this->parent) || !$this->parent) && !isset($_SESSION['edit']);
443 /* Depending on pwmode, currently hardcoded because there are no other methods */
444 if ( 1 == 1 ){
445 $smarty->assign("pwmode", dirname(__FILE__)."/posix_shadow");
447 $shadowMinACL = $this->getacl("shadowMin",$SkipWrite);
448 $smarty->assign("shadowmins", sprintf(_("Password can't be changed up to %s days after last change"),
449 "<input name=\"shadowMin\" size=3 maxlength=4 $shadowMinACL value=\"".$this->shadowMin."\">"));
451 $shadowMaxACL = $this->getacl("shadowMax",$SkipWrite);
452 $smarty->assign("shadowmaxs", sprintf(_("Password must be changed after %s days"),
453 "<input name=\"shadowMax\" size=3 maxlength=4 $shadowMaxACL value=\"".$this->shadowMax."\">"));
455 $shadowInactiveACL= $this->getacl("shadowInactive",$SkipWrite);
456 $smarty->assign("shadowinactives", sprintf(_("Disable account after %s days of inactivity after password expiery"),
457 "<input name=\"shadowInactive\" size=3 maxlength=4 $shadowInactiveACL value=\"".$this->shadowInactive."\">"));
459 $shadowWarningACL = $this->getacl("shadowWarning",$SkipWrite);
460 $smarty->assign("shadowwarnings", sprintf(_("Warn user %s days before password expiery"),
461 "<input name=\"shadowWarning\" size=3 maxlength=4 $shadowWarningACL value=\"".$this->shadowWarning."\">"));
463 foreach( array("use_shadowMin", "use_shadowMax",
464 "use_shadowExpire", "use_shadowInactive","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", $this->getacl($val,$SkipWrite));
471 }
473 if($this->mustchangepassword){
474 $smarty->assign("mustchangepassword", "checked");
475 } else {
476 $smarty->assign("mustchangepassword", "");
477 }
478 $smarty->assign("mustchangepasswordACL", $this->getacl("mustchangepassword",$SkipWrite));
479 }
481 /* Fill calendar */
482 $date= getdate($this->shadowExpire);
484 $days= array();
485 for($d= 1; $d<32; $d++){
486 $days[$d]= $d;
487 }
488 $years= array();
489 for($y= $date['year']-10; $y<$date['year']+10; $y++){
490 $years[]= $y;
491 }
492 $months= array(_("January"), _("February"), _("March"), _("April"),
493 _("May"), _("June"), _("July"), _("August"), _("September"),
494 _("October"), _("November"), _("December"));
495 $smarty->assign("day", $date["mday"]);
496 $smarty->assign("days", $days);
497 $smarty->assign("months", $months);
498 $smarty->assign("month", $date["mon"]-1);
499 $smarty->assign("years", $years);
500 $smarty->assign("year", $date["year"]);
502 /* Fill arrays */
503 $smarty->assign("shells", $this->loginShellList);
504 $smarty->assign("secondaryGroups", $this->secondaryGroups);
505 $smarty->assign("primaryGroup", $this->primaryGroup);
506 if (!count($this->groupMembership)){
507 $smarty->assign("groupMembership", array(" "));
508 } else {
509 $smarty->assign("groupMembership", $this->groupMembership);
510 }
511 if (count($this->groupMembership) > 16){
512 $smarty->assign("groups", "too_many_for_nfs");
513 } else {
514 $smarty->assign("groups", "");
515 }
516 $smarty->assign("printerList", $this->printerList);
517 $smarty->assign("languages", $this->config->data['MAIN']['LANGUAGES']);
519 /* Avoid "Undefined index: forceMode" */
520 $smarty->assign("forceMode", "");
522 /* Checkboxes */
523 if ($this->force_ids == 1){
524 $smarty->assign("force_ids", "checked");
525 if ($_SESSION['js']){
526 $smarty->assign("forceMode", "");
527 }
528 } else {
529 if ($_SESSION['js']){
530 #FIXME: whats up here. remove acl?
531 if($this->acl != "#none#")
532 $smarty->assign("forceMode", "disabled");
533 }
534 $smarty->assign("force_ids", "");
535 }
539 $smarty->assign("force_idsACL", $this->getacl("uidNumber",$SkipWrite).$this->getacl("gidNumber",$SkipWrite));
541 /* Load attributes and acl's */
542 foreach($this->attributes as $val){
543 if(($_SESSION["js"])&&(($val=="uidNumber")||($val=="gidNumber")))
544 {
545 $smarty->assign("$val"."ACL",$this->getacl($val,$SkipWrite));
546 $smarty->assign("$val", $this->$val);
547 continue;
548 }
549 $smarty->assign("$val", $this->$val);
550 $smarty->assign("$val"."ACL", $this->getacl($val,$SkipWrite));
551 }
552 if($SkipWrite){
553 $smarty->assign("groupMembershipACL","r");
554 }else{
555 $smarty->assign("groupMembershipACL","rw");
556 }
557 $smarty->assign("status", $this->status);
559 /* Work on trust modes */
560 $smarty->assign("trustmodeACL", $this->getacl("trustModel",$SkipWrite));
561 if ($this->trustModel == "fullaccess"){
562 $trustmode= 1;
563 // pervent double disable tag in html code, this will disturb our clean w3c html
564 $smarty->assign("trustmode", $this->getacl("trustModel",$SkipWrite));
566 } elseif ($this->trustModel == "byhost"){
567 $trustmode= 2;
568 $smarty->assign("trusthide", "");
569 } else {
570 // pervent double disable tag in html code, this will disturb our clean w3c html
571 $smarty->assign("trustmode", $this->getacl("trustModel",$SkipWrite));
572 $trustmode= 0;
573 }
574 $smarty->assign("trustmode", $trustmode);
575 $smarty->assign("trustmodes", array( 0 => _("disabled"), 1 => _("full access"),
576 2 => _("allow access to these hosts")));
580 if((count($this->accessTo))==0)
581 $smarty->assign("emptyArrAccess",true);
582 else
583 $smarty->assign("emptyArrAccess",false);
587 $smarty->assign("workstations", $this->accessTo);
589 $smarty->assign("apply", apply_filter());
590 $display.= $smarty->fetch (get_template_path('generic.tpl', TRUE, dirname(__FILE__)));
591 return($display);
592 }
595 /* remove object from parent */
596 function remove_from_parent()
597 {
598 /* Cancel if there's nothing to do here */
599 if ((!$this->initially_was_account) || (!$this->acl_is_removeable())){
600 return;
601 }
603 /* include global link_info */
604 $ldap= $this->config->get_ldap_link();
606 /* Remove and write to LDAP */
607 plugin::remove_from_parent();
609 /* Zero out array */
610 $this->attrs['gosaHostACL']= array();
612 /* Keep uid, because we need it for authentification! */
613 unset($this->attrs['uid']);
614 unset($this->attrs['trustModel']);
616 @DEBUG (DEBUG_LDAP, __LINE__, __FUNCTION__, __FILE__,
617 $this->attributes, "Save");
618 $ldap->cd($this->dn);
619 $this->cleanup();
620 $ldap->modify ($this->attrs);
622 show_ldap_error($ldap->get_error(), sprintf(_("Removing of user/posix account with dn '%s' failed."),$this->dn));
624 /* Delete group only if cn is uid and there are no other
625 members inside */
626 $ldap->cd ($this->config->current['BASE']);
627 $ldap->search ("(&(objectClass=posixGroup)(gidNumber=".$this->gidNumber."))", array("cn", "memberUid"));
628 if ($ldap->count() != 0){
629 $attrs= $ldap->fetch();
630 if ($attrs['cn'][0] == $this->uid &&
631 !isset($this->attrs['memberUid'])){
633 $ldap->rmDir($ldap->getDN());
634 }
635 }
637 /* Optionally execute a command after we're done */
638 $this->handle_post_events("remove");
639 }
642 function save_object()
643 {
644 if ((isset($_POST['posixTab'])) && (isset($_SESSION['edit']))){
645 /* Save values to object */
646 plugin::save_object();
649 /* Save force GID checkbox */
650 if($this->acl_is_writeable("gidNumber") || $this->acl_is_writeable("uidNumber")){
651 if (isset ($_POST['force_ids'])){
652 $data= 1;
653 } else {
654 $data= 0;
655 }
656 if ($this->force_ids != $data){
657 $this->is_modified= TRUE;
658 }
659 $this->force_ids= $data;
660 }
662 /*Save primary group settings */
663 if($this->acl_is_writeable("primaryGroup") && isset($_POST['primaryGroup'])){
664 $data= $_POST['primaryGroup'];
665 if ($this->primaryGroup != $data){
666 $this->is_modified= TRUE;
667 }
668 $this->primaryGroup= $_POST['primaryGroup'];
669 }
671 foreach(array("shadowMin","shadowMax","shadowExpire","shadowInactive","shadowWarning","mustchangepassword") as $var) {
672 if($this->acl_is_writeable($var)){
673 $use_var = "use_".$var;
674 if(isset($_POST['use_'.$var])){
675 $this->$use_var = true;
676 $this->$var = $_POST[$var];
677 }else{
678 $this->$use_var = false;
679 $this->$var = 0;
680 }
681 }
682 }
684 /* Trust mode - special handling */
685 if($this->acl_is_writeable("trustModel")){
686 if (isset($_POST['trustmode'])){
687 $saved= $this->trustModel;
688 if ($_POST['trustmode'] == "1"){
689 $this->trustModel= "fullaccess";
690 } elseif ($_POST['trustmode'] == "2"){
691 $this->trustModel= "byhost";
692 } else {
693 $this->trustModel= "";
694 }
695 if ($this->trustModel != $saved){
696 $this->is_modified= TRUE;
697 }
698 }
699 }
700 }
702 /* Get regex from alphabet */
703 if(isset($_GET['search'])){
704 $this->GroupRegex = $_GET['search']."*";
705 }
707 /* Check checkboxes and regexes */
708 if(isset($_POST["PosixGroupDialogPosted"])){
710 if(isset($_POST['SubSearch']) && ($_POST['SubSearch'])){
711 $this->SubSearch = true;
712 }else{
713 $this->SubSearch = false;
714 }
715 if(isset($_POST['guser'])){
716 $this->GroupUserRegex = $_POST['guser'];
717 }
718 if(isset($_POST['regex'])){
719 $this->GroupRegex = $_POST['regex'];
720 }
721 }
722 $this->GroupRegex = preg_replace("/\*\**/","*",$this->GroupRegex);
723 $this->GroupUserRegex = preg_replace("/\*\**/","*",$this->GroupUserRegex);
724 }
727 /* Save data to LDAP, depending on is_account we save or delete */
728 function save()
729 {
731 /* include global link_info */
732 $ldap= $this->config->get_ldap_link();
734 /* Adapt shadow values */
735 if (!$this->use_shadowExpire){
736 $this->shadowExpire= "0";
737 } else {
738 /* Transform seconds to days here */
739 $this->shadowExpire= (int)($this->shadowExpire / (60 * 60 * 24)) ;
740 }
741 if (!$this->use_shadowMax){
742 $this->shadowMax= "0";
743 }
744 if ($this->mustchangepassword){
745 $this->shadowLastChange= (int)(date("U") / 86400) - $this->shadowMax - 1;
746 } else {
747 $this->shadowLastChange= (int)(date("U") / 86400);
748 }
749 if (!$this->use_shadowWarning){
750 $this->shadowWarning= "0";
751 }
753 /* Check what to do with ID's */
754 if ($this->force_ids == 0){
756 /* Use id's that are already set */
757 if ($this->savedUidNumber != ""){
758 $this->uidNumber= $this->savedUidNumber;
759 $this->gidNumber= $this->savedGidNumber;
760 } else {
762 /* Calculate new id's. We need to place a lock before calling get_next_id
763 to get real unique values. */
764 $wait= 10;
765 while (get_lock("uidnumber") != ""){
766 sleep (1);
768 /* Oups - timed out */
769 if ($wait-- == 0){
770 print_red (_("Failed: overriding lock"));
771 break;
772 }
773 }
775 add_lock ("uidnumber", "gosa");
776 $this->uidNumber= $this->get_next_id("uidNumber");
777 if ($this->savedGidNumber != ""){
778 $this->gidNumber= $this->savedGidNumber;
779 } else {
780 $this->gidNumber= $this->get_next_id("gidNumber");
781 }
782 }
784 if ($this->primaryGroup != 0){
785 $this->gidNumber= $this->primaryGroup;
786 }
787 }
789 if ($this->use_shadowMin != "1" ) {
790 $this->shadowMin = "";
791 }
793 if (($this->use_shadowMax != "1") && ($this->mustchangepassword != "1")) {
794 $this->shadowMax = "";
795 }
797 if ($this->use_shadowWarning != "1" ) {
798 $this->shadowWarning = "";
799 }
801 if ($this->use_shadowInactive != "1" ) {
802 $this->shadowInactive = "";
803 }
805 if ($this->use_shadowExpire != "1" ) {
806 $this->shadowExpire = "";
807 }
809 /* Fill gecos */
810 if (isset($this->parent) && $this->parent != NULL){
811 $this->gecos= rewrite($this->parent->by_object['user']->cn);
812 if (!preg_match('/[a-z0-9 -]/i', $this->gecos)){
813 $this->gecos= "";
814 }
815 }
817 foreach(array("shadowMin","shadowMax","shadowWarning","shadowInactive","shadowExpire") as $attr){
818 $this->$attr = (int) $this->$attr;
819 }
820 /* Call parents save to prepare $this->attrs */
821 plugin::save();
823 /* Trust accounts */
824 $objectclasses= array();
825 foreach ($this->attrs['objectClass'] as $key => $class){
826 if (preg_match('/trustAccount/i', $class)){
827 continue;
828 }
829 $objectclasses[]= $this->attrs['objectClass'][$key];
830 }
831 $this->attrs['objectClass']= $objectclasses;
832 if ($this->trustModel != ""){
833 $this->attrs['objectClass'][]= "trustAccount";
834 $this->attrs['trustModel']= $this->trustModel;
835 $this->attrs['accessTo']= array();
836 if ($this->trustModel == "byhost"){
837 foreach ($this->accessTo as $host){
838 $this->attrs['accessTo'][]= $host;
839 }
840 }
841 } else {
842 if ($this->was_trust_account){
843 $this->attrs['accessTo']= array();
844 $this->attrs['trustModel']= array();
845 }
846 }
848 if(empty($this->attrs['gosaDefaultPrinter'])){
849 $thid->attrs['gosaDefaultPrinter']=array();
850 }
853 /* Save data to LDAP */
854 $ldap->cd($this->dn);
855 $this->cleanup();
856 unset($this->attrs['uid']);
857 $ldap->modify ($this->attrs);
859 show_ldap_error($ldap->get_error(), sprintf(_("Saving of user/posix account with dn '%s' failed."),$this->dn));
861 /* Remove lock needed for unique id generation */
862 del_lock ("uidnumber");
865 /* Posix accounts have group interrelationship, take care about these here. */
866 if ($this->force_ids == 0 && $this->primaryGroup == 0){
867 $ldap->cd($this->config->current['BASE']);
868 $ldap->search("(&(objectClass=posixGroup)(gidNumber=".$this->gidNumber."))", array("cn"));
870 /* Create group if it doesn't exist */
871 if ($ldap->count() == 0){
872 $groupdn= preg_replace ('/^'.$this->config->current['DNMODE'].'=[^,]+,'.get_people_ou().'/i', 'cn='.$this->uid.','.get_groups_ou(), $this->dn);
874 $g= new group($this->config, $groupdn);
875 $g->cn= $this->uid;
876 $g->force_gid= 1;
877 $g->gidNumber= $this->gidNumber;
878 $g->description= "Group of user ".$this->givenName." ".$this->sn;
879 $g->save ();
880 }
881 }
883 /* Take care about groupMembership values: add to groups */
884 foreach ($this->groupMembership as $key => $value){
885 $g= new grouptabs($this->config,$this->config->data['TABS']['GROUPTABS'], $key);
886 $g->by_object['group']->addUser($this->uid);
887 $g->save();
888 }
890 /* Remove from groups not listed in groupMembership */
891 foreach ($this->savedGroupMembership as $key => $value){
892 if (!isset($this->groupMembership[$key])){
893 $g= new grouptabs($this->config,$this->config->data['TABS']['GROUPTABS'], $key);
894 $g->by_object['group']->removeUser ($this->uid);
895 $g->save();
896 }
897 }
899 /* Optionally execute a command after we're done */
900 if ($this->initially_was_account == $this->is_account){
901 if ($this->is_modified){
902 $this->handle_post_events("mofify");
903 }
904 } else {
905 $this->handle_post_events("add");
906 }
907 }
909 /* Check formular input */
910 function check()
911 {
912 /* Include global link_info */
913 $ldap= $this->config->get_ldap_link();
915 /* Call common method to give check the hook */
916 $message= plugin::check();
918 /* must: homeDirectory */
919 if ($this->homeDirectory == ""){
920 $message[]= _("The required field 'Home directory' is not set.");
921 }
922 if (!is_path($this->homeDirectory)){
923 $message[]= _("Please enter a valid path in 'Home directory' field.");
924 }
926 /* Check ID's if they are forced by user */
927 if ($this->force_ids == "1"){
929 /* Valid uid/gid? */
930 if (!is_id($this->uidNumber)){
931 $message[]= _("Value specified as 'UID' is not valid.");
932 } else {
933 if ($this->uidNumber < $this->config->current['MINID']){
934 $message[]= _("Value specified as 'UID' is too small.");
935 }
936 }
937 if (!is_id($this->gidNumber)){
938 $message[]= _("Value specified as 'GID' is not valid.");
939 } else {
940 if ($this->gidNumber < $this->config->current['MINID']){
941 $message[]= _("Value specified as 'GID' is too small.");
942 }
943 }
944 }
946 /* Check shadow settings, well I like spaghetties... */
947 if ($this->use_shadowMin){
948 if (!is_id($this->shadowMin)){
949 $message[]= _("Value specified as 'shadowMin' is not valid.");
950 }
951 }
952 if ($this->use_shadowMax){
953 if (!is_id($this->shadowMax)){
954 $message[]= _("Value specified as 'shadowMax' is not valid.");
955 }
956 }
957 if ($this->use_shadowWarning){
958 if (!is_id($this->shadowWarning)){
959 $message[]= _("Value specified as 'shadowWarning' is not valid.");
960 }
961 if (!$this->use_shadowMax){
962 $message[]= _("'shadowWarning' without 'shadowMax' makes no sense.");
963 }
964 if ($this->shadowWarning > $this->shadowMax){
965 $message[]= _("Value specified as 'shadowWarning' should be smaller than 'shadowMax'.");
966 }
967 if ($this->use_shadowMin && $this->shadowWarning < $this->shadowMin){
968 $message[]= _("Value specified as 'shadowWarning' should be greater than 'shadowMin'.");
969 }
970 }
971 if ($this->use_shadowInactive){
972 if (!is_id($this->shadowInactive)){
973 $message[]= _("Value specified as 'shadowInactive' is not valid.");
974 }
975 if (!$this->use_shadowMax){
976 $message[]= _("'shadowInactive' without 'shadowMax' makes no sense.");
977 }
978 }
979 if ($this->use_shadowMin && $this->use_shadowMax){
980 if ($this->shadowMin > $this->shadowMax){
981 $message[]= _("Value specified as 'shadowMin' should be smaller than 'shadowMax'.");
982 }
983 }
985 // if(empty($this->gosaDefaultPrinter)){
986 // $message[]= _("You need to specify a valid default printer.");
987 // }
989 return ($message);
990 }
992 function addGroup ($groups)
993 {
994 /* include global link_info */
995 $ldap= $this->config->get_ldap_link();
997 /* Walk through groups and add the descriptive entry if not exists */
998 foreach ($groups as $value){
999 if (!array_key_exists($value, $this->groupMembership)){
1000 $ldap->cat($value, array('cn', 'description', 'dn'));
1001 $attrs= $ldap->fetch();
1002 error_reporting (0);
1003 if (!isset($attrs['description'][0])){
1004 $entry= $attrs["cn"][0];
1005 } else {
1006 $dsc= preg_replace ('/^Group of user/', _("Group of user"), $attrs["description"][0]);
1007 $entry= $attrs["cn"][0]." [$dsc]";
1008 }
1009 error_reporting (E_ALL);
1010 $this->groupMembership[$attrs['dn']]= $entry;
1011 }
1012 }
1014 /* Sort groups */
1015 asort ($this->groupMembership);
1016 reset ($this->groupMembership);
1017 }
1020 /* Del posix user from some groups */
1021 function delGroup ($groups)
1022 {
1023 $dest= array();
1025 foreach ($this->groupMembership as $key => $value){
1026 if (!in_array($key, $groups)){
1027 $dest[$key]= $value;
1028 }
1029 }
1030 $this->groupMembership= $dest;
1031 }
1033 /* Adapt from template, using 'dn' */
1034 function adapt_from_template($dn)
1035 {
1036 /* Include global link_info */
1037 $ldap= $this->config->get_ldap_link();
1039 plugin::adapt_from_template($dn);
1040 $template= $this->attrs['uid'][0];
1042 /* Adapt group membership */
1043 $ldap->cd($this->config->current['BASE']);
1044 $ldap->search("(&(objectClass=posixGroup)(memberUid=".$this->attrs["uid"][0]."))", array("description", "cn"));
1046 while ($this->attrs= $ldap->fetch()){
1047 if (!isset($this->attrs["description"][0])){
1048 $entry= $this->attrs["cn"][0];
1049 } else {
1050 $entry= $this->attrs["cn"][0]." [".$this->attrs["description"][0]."]";
1051 }
1052 $this->groupMembership[$ldap->getDN()]= $entry;
1053 }
1055 /* Fix primary group settings */
1056 $ldap->cd($this->config->current['BASE']);
1057 $ldap->search("(&(objectClass=posixGroup)(cn=$template)(gidNumber=".$this->gidNumber."))", array("cn"));
1058 if ($ldap->count() != 1){
1059 $this->primaryGroup= $this->gidNumber;
1060 }
1062 $ldap->cd($this->config->current['BASE']);
1063 $ldap->search("(&(objectClass=gosaUserTemplate)(uid=".$template."))", array("cn","accessTo"));
1064 while($attr = $ldap->fetch()){
1065 $tmp = $attr['accessTo'];
1066 unset ($tmp['count']);
1067 $this->accessTo = $tmp;
1068 }
1070 /* Adjust shadow checkboxes */
1071 foreach (array("shadowMin", "shadowMax", "shadowWarning", "shadowInactive",
1072 "shadowExpire") as $val){
1074 if ($this->$val != 0){
1075 $oval= "use_".$val;
1076 $this->$oval= "1";
1077 }
1078 }
1079 }
1081 function get_next_id($attrib)
1082 {
1083 $ids= array();
1084 $ldap= $this->config->get_ldap_link();
1086 $ldap->cd ($this->config->current['BASE']);
1087 if (preg_match('/gidNumber/i', $attrib)){
1088 $oc= "posixGroup";
1089 } else {
1090 $oc= "posixAccount";
1091 }
1092 $ldap->search ("(&(objectClass=$oc)($attrib=*))", array("$attrib"));
1094 /* Get list of ids */
1095 while ($attrs= $ldap->fetch()){
1096 $ids[]= (int)$attrs["$attrib"][0];
1097 }
1099 /* Find out next free id near to UID_BASE */
1100 for ($id= $this->config->current['UIDBASE']; $id++; $id<65000){
1101 if (!in_array($id, $ids)){
1102 return ($id);
1103 }
1104 }
1106 /* Should not happen */
1107 if ($id == 65000){
1108 print_red(_("Too many users, can't allocate a free ID!"));
1109 exit;
1110 }
1112 }
1114 function reload()
1115 {
1116 /* Set base for all searches */
1117 $base = $_SESSION['CurrentMainBase'];
1118 $base = $base;
1119 $ldap = $this->config->get_ldap_link();
1120 $attrs = array("cn", "description", "gidNumber");
1121 $Flags = GL_SIZELIMIT;
1123 /* Get groups */
1124 if ($this->GroupUserRegex == '*'){
1125 $filter = "(&(objectClass=posixGroup)(cn=".$this->GroupRegex."))";
1126 } else {
1127 $filter= "(&(objectClass=posixGroup)(cn=".$this->GroupRegex.")(memberUid=".$this->GroupUserRegex."))";
1128 }
1129 if($this->SubSearch){
1130 $Flags |= GL_SUBSEARCH;
1131 }else{
1132 $base = get_groups_ou().$base;
1133 }
1135 $res= get_list($filter, "groups", $base,$attrs, $Flags);
1137 /* check sizelimit */
1138 if (preg_match("/size limit/i", $ldap->error)){
1139 $_SESSION['limit_exceeded']= TRUE;
1140 }
1142 /* Create a list of users */
1143 $this->grouplist = array();
1144 foreach ($res as $value){
1145 $this->grouplist[$value['gidNumber'][0]]= $value;
1146 }
1148 $tmp=array();
1149 foreach($this->grouplist as $tkey => $val ){
1150 $tmp[strtolower($val['cn'][0]).$val['cn'][0]]=$val;
1151 }
1153 /* Sort index */
1154 ksort($tmp);
1156 /* Recreate index array[dn]=cn[description]*/
1157 $this->grouplist=array();
1158 foreach($tmp as $val){
1159 if(isset($val['description'])){
1160 $this->grouplist[$val['dn']]=$val['cn'][0]." [".$val['description'][0]."]";
1161 }else{
1162 $this->grouplist[$val['dn']]=$val['cn'][0];
1163 }
1164 }
1165 reset ($this->grouplist);
1166 }
1169 /* Create the posix dialog part for copy & paste */
1170 function getCopyDialog()
1171 {
1172 /* Skip dialog creation if this is not a valid account*/
1173 if(!$this->is_account) return("");
1174 if ($this->force_ids == 1){
1175 $force_ids = "checked";
1176 if ($_SESSION['js']){
1177 $forceMode = "";
1178 }
1179 } else {
1180 if ($_SESSION['js']){
1181 if($this->acl != "#none#")
1182 $forceMode ="disabled";
1183 }
1184 $force_ids = "";
1185 }
1187 $sta = "";
1189 /* Open group add dialog */
1190 if(isset($_POST['edit_groupmembership'])){
1191 $this->group_dialog = TRUE;
1192 $sta = "SubDialog";
1193 }
1195 /* If the group-add dialog is closed, call execute
1196 to ensure that the membership is updatd */
1197 if(isset($_POST['add_groups_finish']) || isset($_POST['add_groups_cancel'])){
1198 $this->execute();
1199 $this->group_dialog =FALSE;
1200 }
1202 if($this->group_dialog){
1203 $str = $this->execute(true);
1204 $ret = array();
1205 $ret['string'] = $str;
1206 $ret['status'] = $sta;
1207 return($ret);
1208 }
1210 /* If a group member should be deleted, simply call execute */
1211 if(isset($_POST['delete_groupmembership'])){
1212 $this->execute();
1213 }
1215 /* Assigned informations to smarty */
1216 $smarty = get_smarty();
1217 $smarty->assign("homeDirectory",$this->homeDirectory);
1218 $smarty->assign("uidNumber",$this->uidNumber);
1219 $smarty->assign("gidNumber",$this->gidNumber);
1220 $smarty->assign("forceMode",$forceMode);
1221 $smarty->assign("force_ids",$force_ids);
1222 if (!count($this->groupMembership)){
1223 $smarty->assign("groupMembership", array(" "));
1224 } else {
1225 $smarty->assign("groupMembership", $this->groupMembership);
1226 }
1228 /* Display wars message if there are more than 16 group members */
1229 if (count($this->groupMembership) > 16){
1230 $smarty->assign("groups", "too_many_for_nfs");
1231 } else {
1232 $smarty->assign("groups", "");
1233 }
1234 $str = $smarty->fetch(get_template_path("paste_generic.tpl",TRUE,dirname(__FILE__)));
1236 $ret = array();
1237 $ret['string'] = $str;
1238 $ret['status'] = $sta;
1239 return($ret);
1240 }
1243 function plInfo()
1244 {
1245 return (array(
1246 "plDescription" => _("POSIX account"),
1247 "plSelfModify" => TRUE,
1248 "plDepends" => array("user"),
1249 "plPriority" => 1,
1250 "plSection" => "personal",
1251 "plCategory" => array("users"),
1252 "plOptions" => array(),
1254 "plProvidedAcls" => array(
1256 "homeDirectory" => _("Home directory"),
1257 "loginShell" => _("Shell"),
1258 "uidNumber" => _("User ID"),
1259 "gidNumber" => _("Group ID"),
1261 "mustchangepassword"=> _("Force password change on login"),
1262 "shadowMin" => _("Shadow min"),
1263 "shadowMax" => _("Shadow max"),
1264 "shadowWarning" => _("Shadow warning"),
1265 "shadowInactive" => _("Shadow inactive"),
1266 "shadowExpire" => _("Shadow expire"),
1267 "trustModel" => _("System trust model")))
1268 );
1269 }
1270 }
1272 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
1273 ?>