b7cbba26206588aa5e8f17863455a6f5fa002811
1 <?php
2 /*!
3 \brief posixAccount plugin
4 \author Cajus Pollmeier <pollmeier@gonicus.de>
5 \version 2.00
6 \date 24.07.2003
8 This class provides the functionality to read and write all attributes
9 relevant for posixAccounts and shadowAccounts from/to the LDAP. It
10 does syntax checking and displays the formulars required.
11 */
13 class posixAccount extends plugin
14 {
15 /* Definitions */
16 var $plHeadline= "UNIX";
17 var $plDescription= "This does something";
19 /* CLI vars */
20 var $cli_summary= "Manage users posix account";
21 var $cli_description= "Some longer text\nfor help";
22 var $cli_parameters= array("eins" => "Eins ist toll", "zwei" => "Zwei ist noch besser");
24 /* Plugin specific values */
25 var $homeDirectory= "";
26 var $loginShell= "/bin/bash";
27 var $uidNumber= "";
28 var $gidNumber= "";
29 var $gecos= "";
30 var $shadowMin= "0";
31 var $shadowMax= "0";
32 var $shadowWarning= "0";
33 var $shadowLastChange= "0";
34 var $shadowInactive= "0";
35 var $shadowExpire= "0";
36 var $gosaDefaultPrinter= "";
37 var $gosaDefaultLanguage= "";
38 var $accessTo= array();
39 var $trustModel= "";
41 var $glist=array();
42 var $status= "";
43 var $loginShellList= array();
44 var $groupMembership= array();
45 var $savedGroupMembership= array();
46 var $savedUidNumber= "";
47 var $savedGidNumber= "";
48 var $use_shadowMin= "0";
49 var $use_shadowMax= "0";
50 var $use_shadowWarning= "0";
51 var $use_shadowInactive= "0";
52 var $use_shadowExpire= "0";
53 var $must_change_password= "0";
54 var $force_ids= 0;
55 var $printerList= array();
56 var $group_dialog= FALSE;
57 var $show_ws_dialog= FALSE;
58 var $secondaryGroups= array();
59 var $primaryGroup= 0;
60 var $was_trust_account= FALSE;
62 var $grouplist = array();
63 var $ui = array();
65 /* attribute list for save action */
66 var $CopyPasteVars = array("grouplist","groupMembership","use_shadowMin","use_shadowMax","use_shadowWarning","use_shadowInactive","use_shadowExpire","must_change_password","force_ids","printerList","grouplist","savedGidNumber","savedUidNumber","savedGroupMembership");
67 var $attributes = array("homeDirectory", "loginShell", "uidNumber", "gidNumber", "gecos",
68 "shadowMin", "shadowMax", "shadowWarning", "shadowInactive", "shadowLastChange",
69 "shadowExpire", "gosaDefaultPrinter", "gosaDefaultLanguage", "uid","accessTo","trustModel");
70 var $objectclasses= array("posixAccount", "shadowAccount");
73 /* constructor, if 'dn' is set, the node loads the given
74 'dn' from LDAP */
75 function posixAccount ($config, $dn= NULL)
76 {
77 /* Configuration is fine, allways */
78 $this->config= $config;
80 /* Load bases attributes */
81 plugin::plugin($config, $dn);
83 $ldap= $this->config->get_ldap_link();
85 if ($dn != NULL){
87 /* Correct is_account. shadowAccount is not required. */
88 if (isset($this->attrs['objectClass']) &&
89 in_array ('posixAccount', $this->attrs['objectClass'])){
91 $this->is_account= TRUE;
92 }
94 /* Is this account a trustAccount? */
95 if ($this->is_account && isset($this->attrs['trustModel'])){
96 $this->trustModel= $this->attrs['trustModel'][0];
97 $this->was_trust_account= TRUE;
98 } else {
99 $this->was_trust_account= FALSE;
100 $this->trustModel= "";
101 }
103 $this->accessTo = array();
104 if ($this->is_account && isset($this->attrs['accessTo'])){
105 for ($i= 0; $i<$this->attrs['accessTo']['count']; $i++){
106 $tmp= $this->attrs['accessTo'][$i];
107 $this->accessTo[$tmp]= $tmp;
108 }
109 }
110 $this->initially_was_account= $this->is_account;
112 /* Fill group */
113 $this->primaryGroup= $this->gidNumber;
115 /* Generate status text */
116 $current= date("U");
117 if (($current >= $this->shadowExpire) && $this->shadowExpire){
118 $this->status= _("expired");
119 if (($this->shadowExpire - $current) < $this->shadowInactive){
120 $this->status.= _(", grace time active");
121 }
122 } elseif (($this->shadowLastChange + $this->shadowMin) >= $current){
123 $this->status= _("active, password not changable");
124 } elseif (($this->shadowLastChange + $this->shadowMax) >= $current){
125 $this->status= _("active, password expired");
126 } else {
127 $this->status= _("active");
128 }
130 /* Get group membership */
131 $ldap->cd($this->config->current['BASE']);
132 $ldap->search("(&(objectClass=posixGroup)(memberUid=".$this->uid."))", array("cn", "description"));
134 while ($this->attrs= $ldap->fetch()){
135 if (!isset($this->attrs["description"][0])){
136 $entry= $this->attrs["cn"][0];
137 } else {
138 $dsc= preg_replace ('/^Group of user/', _("Group of user"), $this->attrs["description"][0]);
139 $entry= $this->attrs["cn"][0]." [$dsc]";
140 }
141 $this->groupMembership[$ldap->getDN()]= $entry;
142 }
143 asort($this->groupMembership);
144 reset($this->groupMembership);
145 $this->savedGroupMembership= $this->groupMembership;
146 $this->savedUidNumber= $this->uidNumber;
147 $this->savedGidNumber= $this->gidNumber;
148 }
150 /* Adjust shadow checkboxes */
151 foreach (array("shadowMin", "shadowMax", "shadowWarning", "shadowInactive",
152 "shadowExpire") as $val){
154 if ($this->$val != 0){
155 $oval= "use_".$val;
156 $this->$oval= "1";
157 }
158 }
160 /* Convert to seconds */
161 if ($this->shadowExpire != 0){
162 $this->shadowExpire*= 60 * 60 * 24;
163 } else {
164 $date= getdate();
165 $this->shadowExpire= floor($date[0] / (60*60*24)) * 60 * 60 * 24;
166 }
168 /* Generate shell list from /etc/gosa/shells */
169 if (file_exists('/etc/gosa/shells')){
170 $shells = file ('/etc/gosa/shells');
171 foreach ($shells as $line){
172 if (!preg_match ("/^#/", $line)){
173 $this->loginShellList[]= trim($line);
174 }
175 }
176 } else {
177 if ($this->loginShell == ""){
178 $this->loginShellList[]= _("unconfigured");
179 }
180 }
182 /* Insert possibly missing loginShell */
183 if ($this->loginShell != "" && !in_array($this->loginShell, $this->loginShellList)){
184 $this->loginShellList[]= $this->loginShell;
185 }
187 /* Generate printer list */
188 if (isset($this->config->data['SERVERS']['CUPS'])){
189 $this->printerList= get_printer_list ($this->config->data['SERVERS']['CUPS']);
190 asort($this->printerList);
191 }
193 /* Generate group list */
194 $ldap->cd($this->config->current['BASE']);
195 $ldap->search("(objectClass=posixGroup)", array("cn", "gidNumber"));
196 $this->secondaryGroups[]= "- "._("automatic")." -";
197 while ($attrs= $ldap->fetch()){
198 $this->secondaryGroups[$attrs['gidNumber'][0]]= $attrs['cn'][0];
199 }
200 asort ($this->secondaryGroups);
202 /* Get global filter config */
203 if (!is_global("sysfilter")){
204 $ui= get_userinfo();
205 $base= get_base_from_people($ui->dn);
206 $sysfilter= array( "depselect" => $base,
207 "regex" => "*");
208 register_global("sysfilter", $sysfilter);
209 }
210 $this->ui = get_userinfo();
211 }
214 /* execute generates the html output for this node */
215 function execute($isCopyPaste = false)
216 {
217 /* Call parent execute */
218 plugin::execute();
219 $display= "";
221 /* Department has changed? */
222 if(isset($_POST['depselect'])){
223 $_SESSION['CurrentMainBase']= validate($_POST['depselect']);
224 }
226 if(!$isCopyPaste){
227 /* Do we need to flip is_account state? */
228 if (isset($_POST['modify_state'])){
229 $this->is_account= !$this->is_account;
230 }
232 /* Do we represent a valid posixAccount? */
233 if (!$this->is_account && $this->parent == NULL ){
234 $display= "<img alt=\"\" src=\"images/stop.png\" align=\"middle\"> <b>".
235 _("This account has no unix extensions.")."</b>";
236 $display.= back_to_main();
237 return ($display);
238 }
241 /* Show tab dialog headers */
242 if ($this->parent != NULL){
243 if ($this->is_account){
244 if (isset($this->parent->by_object['sambaAccount'])){
245 $obj= $this->parent->by_object['sambaAccount'];
246 }
247 if (isset($obj) && $obj->is_account == TRUE &&
248 ((isset($this->parent->by_object['sambaAccount']))&&($this->parent->by_object['sambaAccount']->is_account))
249 ||(isset($this->parent->by_object['environment'] ))&&($this->parent->by_object['environment'] ->is_account)){
251 /* Samba3 dependency on posix accounts are enabled
252 in the moment, because I need to rely on unique
253 uidNumbers. There'll be a better solution later
254 on. */
255 $display= $this->show_header(_("Remove posix account"),
256 _("This account has unix features enabled. To disable them, you'll need to remove the samba / environment account first."), TRUE);
257 } else {
258 $display= $this->show_header(_("Remove posix account"),
259 _("This account has posix features enabled. You can disable them by clicking below."));
260 }
261 } else {
262 $display= $this->show_header(_("Create posix account"),
263 _("This account has posix features disabled. You can enable them by clicking below."));
264 return($display);
265 }
266 }
267 }
268 /* Trigger group edit? */
269 if (isset($_POST['edit_groupmembership'])){
270 $this->group_dialog= TRUE;
271 $this->dialog= TRUE;
272 }
274 /* Cancel group edit? */
275 if (isset($_POST['add_groups_cancel']) ||
276 isset($_POST['add_groups_finish'])){
277 $this->group_dialog= FALSE;
278 $this->dialog= FALSE;
279 }
281 /* Add selected groups */
282 if (isset($_POST['add_groups_finish']) && isset($_POST['groups']) &&
283 count($_POST['groups'])){
285 if (chkacl ($this->acl, "memberUid") == ""){
286 $this->addGroup ($_POST['groups']);
287 $this->is_modified= TRUE;
288 }
289 }
291 /* Delete selected groups */
292 if (isset($_POST['delete_groupmembership']) &&
293 isset($_POST['group_list']) && count($_POST['group_list'])){
295 if (chkacl ($this->acl, "memberUid") == ""){
296 $this->delGroup ($_POST['group_list']);
297 $this->is_modified= TRUE;
298 }
299 }
301 /* Add user workstation? */
302 if (isset($_POST["add_ws"])){
303 $this->show_ws_dialog= TRUE;
304 $this->dialog= TRUE;
305 }
307 /* Add user workstation? */
308 if (isset($_POST["add_ws_finish"]) && isset($_POST['wslist'])){
309 foreach($_POST['wslist'] as $ws){
310 $this->accessTo[$ws]= $ws;
311 }
312 ksort($this->accessTo);
313 $this->is_modified= TRUE;
314 }
316 /* Remove user workstations? */
317 if (isset($_POST["delete_ws"]) && isset($_POST['workstation_list'])){
318 foreach($_POST['workstation_list'] as $name){
319 unset ($this->accessTo[$name]);
320 }
321 $this->is_modified= TRUE;
322 }
324 /* Add user workstation finished? */
325 if (isset($_POST["add_ws_finish"]) || isset($_POST["add_ws_cancel"])){
326 $this->show_ws_dialog= FALSE;
327 $this->dialog= FALSE;
328 }
330 /* Templates now! */
331 $smarty= get_smarty();
333 /* Show ws dialog */
334 if ($this->show_ws_dialog){
335 /* Save data */
336 $sysfilter= get_global("sysfilter");
337 foreach( array("depselect", "regex") as $type){
338 if (isset($_POST[$type])){
339 $sysfilter[$type]= $_POST[$type];
340 }
341 }
342 if (isset($_GET['search'])){
343 $s= mb_substr($_GET['search'], 0, 1, "UTF8")."*";
344 if ($s == "**"){
345 $s= "*";
346 }
347 $sysfilter['regex']= $s;
348 }
349 register_global("sysfilter", $sysfilter);
351 /* Get workstation list */
352 $exclude= "";
353 foreach($this->accessTo as $ws){
354 $exclude.= "(cn=$ws)";
355 }
356 if ($exclude != ""){
357 $exclude= "(!(|$exclude))";
358 }
359 $acl= array($this->config->current['BASE'] => ":all");
360 $regex= $sysfilter['regex'];
361 $filter= "(&(|(objectClass=goServer)(objectClass=gotoWorkstation)(objectClass=gotoTerminal))$exclude(cn=*)(cn=$regex))";
362 $res= get_list($filter, $acl, $sysfilter['depselect'], array("cn"), GL_SUBSEARCH | GL_SIZELIMIT);
363 $wslist= array();
364 foreach ($res as $attrs){
365 $wslist[]= preg_replace('/\$/', '', $attrs['cn'][0]);
366 }
367 asort($wslist);
368 $smarty->assign("search_image", get_template_path('images/search.png'));
369 $smarty->assign("launchimage", get_template_path('images/small_filter.png'));
370 $smarty->assign("tree_image", get_template_path('images/tree.png'));
371 $smarty->assign("deplist", $this->config->idepartments);
372 $smarty->assign("alphabet", generate_alphabet());
373 foreach( array("depselect", "regex") as $type){
374 $smarty->assign("$type", $sysfilter[$type]);
375 }
376 $smarty->assign("hint", print_sizelimit_warning());
377 $smarty->assign("wslist", $wslist);
378 $smarty->assign("apply", apply_filter());
379 $display= $smarty->fetch (get_template_path('trust_machines.tpl', TRUE, dirname(__FILE__)));
380 return ($display);
381 }
383 /* Manage group add dialog */
384 if ($this->group_dialog){
386 /* Get global filter config */
387 if (!is_global("groupfilter")){
388 $ui= get_userinfo();
389 $base= get_base_from_people($ui->dn);
390 $groupfilter= array("primarygroups" => "checked",
391 "mailgroups" => "checked",
392 "sambagroups" => "checked",
393 "appgroups" => "checked",
394 "functionalgroups" => "checked",
395 "guser" => "*",
396 "subsearch" => "",
397 "depselect" => $_SESSION['CurrentMainBase'],
398 "regex" => "*");
399 register_global("groupfilter", $groupfilter);
400 }
402 /* Save data */
403 $groupfilter= get_global("groupfilter");
404 foreach( array("guser", "regex") as $type){
405 if (isset($_POST[$type])){
406 $groupfilter[$type]= $_POST[$type];
407 }
408 }
409 if (isset($_POST['depselect'])){
410 foreach( array("primarygroups", "sambagroups", "mailgroups", "appgroups",
411 "functionalgroups") as $type){
413 if (isset($_POST[$type])) {
414 $groupfilter[$type]= "checked";
415 } else {
416 $groupfilter[$type]= "";
417 }
418 }
419 }
420 if (isset($_GET['search'])){
421 $s= mb_substr($_GET['search'], 0, 1, "UTF8")."*";
422 if ($s == "**"){
423 $s= "*";
424 }
425 $groupfilter['regex']= $s;
426 }
427 register_global("groupfilter", $groupfilter);
429 /* Calculate actual groups */
431 $this->reload();
432 $glist= array();
433 foreach ($this->grouplist as $key => $value){
434 if (!isset($this->groupMembership[$key])){
435 $glist[$key]= $value;
436 }
437 }
439 $smarty->assign("groups", $glist);
440 $smarty->assign("search_image", get_template_path('images/search.png'));
441 $smarty->assign("launchimage", get_template_path('images/small_filter.png'));
442 $smarty->assign("tree_image", get_template_path('images/tree.png'));
443 $smarty->assign("deplist", $this->config->idepartments);
444 $smarty->assign("alphabet", generate_alphabet());
445 foreach( array("guser", "regex", "primarygroups", "mailgroups",
446 "appgroups", "sambagroups", "functionalgroups") as $type){
447 $smarty->assign("$type", $groupfilter[$type]);
448 }
449 $smarty->assign("depselect",$_SESSION['CurrentMainBase']);
450 $smarty->assign("hint", print_sizelimit_warning());
452 $smarty->assign("apply", apply_filter());
453 $display.= $smarty->fetch (get_template_path('posix_groups.tpl', TRUE, dirname(__FILE__)));
454 return ($display);
455 }
457 /* Show main page */
458 $smarty= get_smarty();
460 /* Depending on pwmode, currently hardcoded because there are no other methods */
461 if ( 1 == 1 ){
462 $smarty->assign("pwmode", dirname(__FILE__)."/posix_shadow");
463 $shadowMinACL= chkacl($this->acl, "shadowMin");
464 $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."\">"));
465 $shadowMaxACL= chkacl($this->acl, "shadowMax");
466 $smarty->assign("shadowmaxs", sprintf(_("Password must be changed after %s days"), "<input name=\"shadowMax\" size=3 maxlength=4 $shadowMaxACL value=\"".$this->shadowMax."\">"));
467 $shadowInactiveACL= chkacl($this->acl, "shadowInactive");
468 $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."\">"));
469 $shadowWarningACL= chkacl($this->acl, "shadowWarning");
470 $smarty->assign("shadowwarnings", sprintf(_("Warn user %s days before password expiery"), "<input name=\"shadowWarning\" size=3 maxlength=4 $shadowWarningACL value=\"".$this->shadowWarning."\">"));
471 foreach( array("must_change_password", "use_shadowMin", "use_shadowMax",
472 "use_shadowExpire", "use_shadowInactive",
473 "use_shadowWarning") as $val){
474 if ($this->$val == 1){
475 $smarty->assign("$val", "checked");
476 } else {
477 $smarty->assign("$val", "");
478 }
479 $smarty->assign("$val"."ACL", chkacl($this->acl, $val));
480 }
481 }
483 /* Fill calendar */
484 $date= getdate($this->shadowExpire);
486 $days= array();
487 for($d= 1; $d<32; $d++){
488 $days[$d]= $d;
489 }
490 $years= array();
491 for($y= $date['year']-10; $y<$date['year']+10; $y++){
492 $years[]= $y;
493 }
494 $months= array(_("January"), _("February"), _("March"), _("April"),
495 _("May"), _("June"), _("July"), _("August"), _("September"),
496 _("October"), _("November"), _("December"));
497 $smarty->assign("day", $date["mday"]);
498 $smarty->assign("days", $days);
499 $smarty->assign("months", $months);
500 $smarty->assign("month", $date["mon"]-1);
501 $smarty->assign("years", $years);
502 $smarty->assign("year", $date["year"]);
504 /* Fill arrays */
505 $smarty->assign("shells", $this->loginShellList);
506 $smarty->assign("secondaryGroups", $this->secondaryGroups);
507 $smarty->assign("primaryGroup", $this->primaryGroup);
508 if (!count($this->groupMembership)){
509 $smarty->assign("groupMembership", array(" "));
510 } else {
511 $smarty->assign("groupMembership", $this->groupMembership);
512 }
513 if (count($this->groupMembership) > 16){
514 $smarty->assign("groups", "too_many_for_nfs");
515 } else {
516 $smarty->assign("groups", "");
517 }
518 $smarty->assign("printerList", $this->printerList);
519 $smarty->assign("languages", $this->config->data['MAIN']['LANGUAGES']);
521 /* Avoid "Undefined index: forceMode" */
522 $smarty->assign("forceMode", "");
524 /* Checkboxes */
525 if ($this->force_ids == 1){
526 $smarty->assign("force_ids", "checked");
527 if ($_SESSION['js']){
528 $smarty->assign("forceMode", "");
529 }
530 } else {
531 if ($_SESSION['js']){
532 if($this->acl != "#none#")
533 $smarty->assign("forceMode", "disabled");
534 }
535 $smarty->assign("force_ids", "");
536 }
537 $smarty->assign("force_idsACL", chkacl($this->acl, "force_ids"));
539 /* Load attributes and acl's */
540 foreach($this->attributes as $val){
541 if((chkacl($this->acl,$val)=="")&&(($_SESSION["js"])&&(($val=="uidNumber")||($val=="gidNumber"))))
542 {
543 $smarty->assign("$val"."ACL","");
544 $smarty->assign("$val", $this->$val);
545 continue;
546 }
547 $smarty->assign("$val", $this->$val);
548 $smarty->assign("$val"."ACL", chkacl($this->acl,$val));
549 }
550 $smarty->assign("groupMembershipACL", chkacl($this->acl, "groupMembership"));
551 $smarty->assign("status", $this->status);
553 /* Work on trust modes */
554 $smarty->assign("trustmodeACL", chkacl($this->acl, "trustmode"));
555 if ($this->trustModel == "fullaccess"){
556 $trustmode= 1;
557 // pervent double disable tag in html code, this will disturb our clean w3c html
559 if(chkacl($this->acl, "trustmode")==""){
560 $smarty->assign("trusthide", "disabled");
561 }else{
562 $smarty->assign("trusthide", "");
563 }
565 } elseif ($this->trustModel == "byhost"){
566 $trustmode= 2;
567 $smarty->assign("trusthide", "");
568 } else {
569 // pervent double disable tag in html code, this will disturb our clean w3c html
570 if(chkacl($this->acl, "trustmode")==""){
571 $smarty->assign("trusthide", "disabled");
572 }else{
573 $smarty->assign("trusthide", "");
574 }
575 $trustmode= 0;
576 }
577 $smarty->assign("trustmode", $trustmode);
578 $smarty->assign("trustmodes", array( 0 => _("disabled"), 1 => _("full access"),
579 2 => _("allow access to these hosts")));
583 if((count($this->accessTo))==0)
584 $smarty->assign("emptyArrAccess",true);
585 else
586 $smarty->assign("emptyArrAccess",false);
590 $smarty->assign("workstations", $this->accessTo);
592 $smarty->assign("apply", apply_filter());
593 $display.= $smarty->fetch (get_template_path('generic.tpl', TRUE, dirname(__FILE__)));
594 return($display);
595 }
598 /* remove object from parent */
599 function remove_from_parent()
600 {
601 /* Cancel if there's nothing to do here */
602 if (!$this->initially_was_account){
603 return;
604 }
606 /* include global link_info */
607 $ldap= $this->config->get_ldap_link();
609 /* Remove and write to LDAP */
610 plugin::remove_from_parent();
612 /* Zero out array */
613 $this->attrs['gosaHostACL']= array();
615 /* Keep uid, because we need it for authentification! */
616 unset($this->attrs['uid']);
617 unset($this->attrs['trustModel']);
619 @DEBUG (DEBUG_LDAP, __LINE__, __FUNCTION__, __FILE__,
620 $this->attributes, "Save");
621 $ldap->cd($this->dn);
622 $this->cleanup();
623 $ldap->modify ($this->attrs);
625 show_ldap_error($ldap->get_error());
627 /* Delete group only if cn is uid and there are no other
628 members inside */
629 $ldap->cd ($this->config->current['BASE']);
630 $ldap->search ("(&(objectClass=posixGroup)(gidNumber=".$this->gidNumber."))", array("cn", "memberUid"));
631 if ($ldap->count() != 0){
632 $attrs= $ldap->fetch();
633 if ($attrs['cn'][0] == $this->uid &&
634 !isset($this->attrs['memberUid'])){
636 $ldap->rmDir($ldap->getDN());
637 }
638 }
640 /* Optionally execute a command after we're done */
641 $this->handle_post_events("remove");
642 }
645 function save_object()
646 {
647 if (isset($_POST['posixTab'])){
648 /* Save values to object */
649 plugin::save_object();
651 /* Save force GID attribute */
652 if (chkacl ($this->acl, "force_ids") == ""){
653 if (isset ($_POST['force_ids'])){
654 $data= 1;
655 } else {
656 $data= 0;
657 }
658 if ($this->force_ids != $data){
659 $this->is_modified= TRUE;
660 }
661 $this->force_ids= $data;
663 $data= $_POST['primaryGroup'];
664 if ($this->primaryGroup != $data){
665 $this->is_modified= TRUE;
666 }
667 $this->primaryGroup= $_POST['primaryGroup'];
668 }
670 /* Save pwmode dependent attributes, curently hardcoded because there're
671 no alternatives */
672 if (1 == 1){
673 foreach( array("must_change_password", "use_shadowMin", "use_shadowMax",
674 "use_shadowExpire", "use_shadowInactive",
675 "use_shadowWarning") as $val){
676 if (chkacl($this->acl, "$val") == ""){
677 if (isset ($_POST[$val])){
678 $data= 1;
679 } else {
680 $data= 0;
681 }
682 if ($data != $this->$val){
683 $this->is_modified= TRUE;
684 }
685 $this->$val= $data;
686 }
687 }
688 }
690 /* Trust mode - special handling */
691 if (isset($_POST['trustmode'])){
692 $saved= $this->trustModel;
693 if ($_POST['trustmode'] == "1"){
694 $this->trustModel= "fullaccess";
695 } elseif ($_POST['trustmode'] == "2"){
696 $this->trustModel= "byhost";
697 } else {
698 $this->trustModel= "";
699 }
700 if ($this->trustModel != $saved){
701 $this->is_modified= TRUE;
702 }
703 }
704 }
705 }
708 /* Save data to LDAP, depending on is_account we save or delete */
709 function save()
710 {
712 /* include global link_info */
713 $ldap= $this->config->get_ldap_link();
715 /* Adapt shadow values */
716 if (!$this->use_shadowExpire){
717 $this->shadowExpire= "0";
718 } else {
719 /* Transform seconds to days here */
720 $this->shadowExpire= (int)($this->shadowExpire / (60 * 60 * 24)) ;
721 }
722 if (!$this->use_shadowMax){
723 $this->shadowMax= "0";
724 }
725 if ($this->must_change_password){
726 $this->shadowLastChange= (int)(date("U") / 86400) - $this->shadowMax - 1;
727 } else {
728 $this->shadowLastChange= (int)(date("U") / 86400);
729 }
730 if (!$this->use_shadowWarning){
731 $this->shadowWarning= "0";
732 }
734 /* Check what to do with ID's */
735 if ($this->force_ids == 0){
737 /* Use id's that are already set */
738 if ($this->savedUidNumber != ""){
739 $this->uidNumber= $this->savedUidNumber;
740 $this->gidNumber= $this->savedGidNumber;
741 } else {
743 /* Calculate new id's. We need to place a lock before calling get_next_id
744 to get real unique values. */
745 $wait= 10;
746 while (get_lock("uidnumber") != ""){
747 sleep (1);
749 /* Oups - timed out */
750 if ($wait-- == 0){
751 print_red (_("Failed: overriding lock"));
752 break;
753 }
754 }
756 add_lock ("uidnumber", "gosa");
757 $this->uidNumber= $this->get_next_id("uidNumber");
758 if ($this->savedGidNumber != ""){
759 $this->gidNumber= $this->savedGidNumber;
760 } else {
761 $this->gidNumber= $this->get_next_id("gidNumber");
762 }
763 }
765 if ($this->primaryGroup != 0){
766 $this->gidNumber= $this->primaryGroup;
767 }
768 }
770 if ($this->use_shadowMin != "1" ) {
771 $this->shadowMin = "";
772 }
774 if (($this->use_shadowMax != "1") && ($this->must_change_password != "1")) {
775 $this->shadowMax = "";
776 }
778 if ($this->use_shadowWarning != "1" ) {
779 $this->shadowWarning = "";
780 }
782 if ($this->use_shadowInactive != "1" ) {
783 $this->shadowInactive = "";
784 }
786 if ($this->use_shadowExpire != "1" ) {
787 $this->shadowExpire = "";
788 }
790 /* Fill gecos */
791 if (isset($this->parent) && $this->parent != NULL){
792 $this->gecos= rewrite($this->parent->by_object['user']->cn);
793 if (!preg_match('/[a-z0-9 -]/i', $this->gecos)){
794 $this->gecos= "";
795 }
796 }
798 foreach(array("shadowMin","shadowMax","shadowWarning","shadowInactive","shadowExpire") as $attr){
799 $this->$attr = (int) $this->$attr;
800 }
801 /* Call parents save to prepare $this->attrs */
802 plugin::save();
804 /* Trust accounts */
805 $objectclasses= array();
806 foreach ($this->attrs['objectClass'] as $key => $class){
807 if (preg_match('/trustAccount/i', $class)){
808 continue;
809 }
810 $objectclasses[]= $this->attrs['objectClass'][$key];
811 }
812 $this->attrs['objectClass']= $objectclasses;
813 if ($this->trustModel != ""){
814 $this->attrs['objectClass'][]= "trustAccount";
815 $this->attrs['trustModel']= $this->trustModel;
816 $this->attrs['accessTo']= array();
817 if ($this->trustModel == "byhost"){
818 foreach ($this->accessTo as $host){
819 $this->attrs['accessTo'][]= $host;
820 }
821 }
822 } else {
823 if ($this->was_trust_account){
824 $this->attrs['accessTo']= array();
825 $this->attrs['trustModel']= array();
826 }
827 }
829 if(empty($this->attrs['gosaDefaultPrinter'])){
830 $thid->attrs['gosaDefaultPrinter']=array();
831 }
834 /* Save data to LDAP */
835 $ldap->cd($this->dn);
836 $this->cleanup();
837 unset($this->attrs['uid']);
838 $ldap->modify ($this->attrs);
840 show_ldap_error($ldap->get_error());
842 /* Remove lock needed for unique id generation */
843 del_lock ("uidnumber");
846 /* Posix accounts have group interrelationship, take care about these here. */
847 if ($this->force_ids == 0 && $this->primaryGroup == 0){
848 $ldap->cd($this->config->current['BASE']);
849 $ldap->search("(&(objectClass=posixGroup)(gidNumber=".$this->gidNumber."))", array("cn"));
851 /* Create group if it doesn't exist */
852 if ($ldap->count() == 0){
853 $groupdn= preg_replace ('/^'.$this->config->current['DNMODE'].'=[^,]+,'.get_people_ou().'/i', 'cn='.$this->uid.','.get_groups_ou(), $this->dn);
855 $g= new group($this->config, $groupdn);
856 $g->cn= $this->uid;
857 $g->force_gid= 1;
858 $g->gidNumber= $this->gidNumber;
859 $g->description= "Group of user ".$this->givenName." ".$this->sn;
860 $g->save ();
861 }
862 }
864 /* Take care about groupMembership values: add to groups */
865 foreach ($this->groupMembership as $key => $value){
866 $g= new grouptabs($this->config,$this->config->data['TABS']['GROUPTABS'], $key);
867 $g->by_object['group']->addUser($this->uid);
868 $g->save();
869 }
871 /* Remove from groups not listed in groupMembership */
872 foreach ($this->savedGroupMembership as $key => $value){
873 if (!isset($this->groupMembership[$key])){
874 $g= new grouptabs($this->config,$this->config->data['TABS']['GROUPTABS'], $key);
875 $g->by_object['group']->removeUser ($this->uid);
876 $g->save();
877 }
878 }
880 /* Optionally execute a command after we're done */
881 if ($this->initially_was_account == $this->is_account){
882 if ($this->is_modified){
883 $this->handle_post_events("mofify");
884 }
885 } else {
886 $this->handle_post_events("add");
887 }
888 }
890 /* Check formular input */
891 function check()
892 {
893 /* Include global link_info */
894 $ldap= $this->config->get_ldap_link();
896 /* Call common method to give check the hook */
897 $message= plugin::check();
899 /* must: homeDirectory */
900 if ($this->homeDirectory == ""){
901 $message[]= _("The required field 'Home directory' is not set.");
902 }
903 if (!is_path($this->homeDirectory)){
904 $message[]= _("Please enter a valid path in 'Home directory' field.");
905 }
907 /* Check ID's if they are forced by user */
908 if ($this->force_ids == "1"){
910 /* Valid uid/gid? */
911 if (!is_id($this->uidNumber)){
912 $message[]= _("Value specified as 'UID' is not valid.");
913 } else {
914 if ($this->uidNumber < $this->config->current['MINID']){
915 $message[]= _("Value specified as 'UID' is too small.");
916 }
917 }
918 if (!is_id($this->gidNumber)){
919 $message[]= _("Value specified as 'GID' is not valid.");
920 } else {
921 if ($this->gidNumber < $this->config->current['MINID']){
922 $message[]= _("Value specified as 'GID' is too small.");
923 }
924 }
925 }
927 /* Check shadow settings, well I like spaghetties... */
928 if ($this->use_shadowMin){
929 if (!is_id($this->shadowMin)){
930 $message[]= _("Value specified as 'shadowMin' is not valid.");
931 }
932 }
933 if ($this->use_shadowMax){
934 if (!is_id($this->shadowMax)){
935 $message[]= _("Value specified as 'shadowMax' is not valid.");
936 }
937 }
938 if ($this->use_shadowWarning){
939 if (!is_id($this->shadowWarning)){
940 $message[]= _("Value specified as 'shadowWarning' is not valid.");
941 }
942 if (!$this->use_shadowMax){
943 $message[]= _("'shadowWarning' without 'shadowMax' makes no sense.");
944 }
945 if ($this->shadowWarning > $this->shadowMax){
946 $message[]= _("Value specified as 'shadowWarning' should be smaller than 'shadowMax'.");
947 }
948 if ($this->use_shadowMin && $this->shadowWarning < $this->shadowMin){
949 $message[]= _("Value specified as 'shadowWarning' should be greater than 'shadowMin'.");
950 }
951 }
952 if ($this->use_shadowInactive){
953 if (!is_id($this->shadowInactive)){
954 $message[]= _("Value specified as 'shadowInactive' is not valid.");
955 }
956 if (!$this->use_shadowMax){
957 $message[]= _("'shadowInactive' without 'shadowMax' makes no sense.");
958 }
959 }
960 if ($this->use_shadowMin && $this->use_shadowMax){
961 if ($this->shadowMin > $this->shadowMax){
962 $message[]= _("Value specified as 'shadowMin' should be smaller than 'shadowMax'.");
963 }
964 }
966 // if(empty($this->gosaDefaultPrinter)){
967 // $message[]= _("You need to specify a valid default printer.");
968 // }
970 return ($message);
971 }
973 function addGroup ($groups)
974 {
975 /* include global link_info */
976 $ldap= $this->config->get_ldap_link();
978 /* Walk through groups and add the descriptive entry if not exists */
979 foreach ($groups as $value){
980 if (!array_key_exists($value, $this->groupMembership)){
981 $ldap->cat($value);
982 $attrs= $ldap->fetch();
983 error_reporting (0);
984 if (!isset($attrs['description'][0])){
985 $entry= $attrs["cn"][0];
986 } else {
987 $dsc= preg_replace ('/^Group of user/', _("Group of user"), $attrs["description"][0]);
988 $entry= $attrs["cn"][0]." [$dsc]";
989 }
990 error_reporting (E_ALL);
991 $this->groupMembership[$ldap->getDN()]= $entry;
992 }
993 }
995 /* Sort groups */
996 asort ($this->groupMembership);
997 reset ($this->groupMembership);
998 }
1001 /* Del posix user from some groups */
1002 function delGroup ($groups)
1003 {
1004 $dest= array();
1006 foreach ($this->groupMembership as $key => $value){
1007 if (!in_array($key, $groups)){
1008 $dest[$key]= $value;
1009 }
1010 }
1011 $this->groupMembership= $dest;
1012 }
1014 /* Adapt from template, using 'dn' */
1015 function adapt_from_template($dn)
1016 {
1017 /* Include global link_info */
1018 $ldap= $this->config->get_ldap_link();
1020 plugin::adapt_from_template($dn);
1021 $template= $this->attrs['uid'][0];
1023 /* Adapt group membership */
1024 $ldap->cd($this->config->current['BASE']);
1025 $ldap->search("(&(objectClass=posixGroup)(memberUid=".$this->attrs["uid"][0]."))", array("description", "cn"));
1027 while ($this->attrs= $ldap->fetch()){
1028 if (!isset($this->attrs["description"][0])){
1029 $entry= $this->attrs["cn"][0];
1030 } else {
1031 $entry= $this->attrs["cn"][0]." [".$this->attrs["description"][0]."]";
1032 }
1033 $this->groupMembership[$ldap->getDN()]= $entry;
1034 }
1036 /* Fix primary group settings */
1037 $ldap->cd($this->config->current['BASE']);
1038 $ldap->search("(&(objectClass=posixGroup)(cn=$template)(gidNumber=".$this->gidNumber."))", array("cn"));
1039 if ($ldap->count() != 1){
1040 $this->primaryGroup= $this->gidNumber;
1041 }
1043 $ldap->cd($this->config->current['BASE']);
1044 $ldap->search("(&(objectClass=gosaUserTemplate)(uid=".$template."))", array("cn","accessTo"));
1045 while($attr = $ldap->fetch()){
1046 $tmp = $attr['accessTo'];
1047 unset ($tmp['count']);
1048 $this->accessTo = $tmp;
1049 }
1051 /* Adjust shadow checkboxes */
1052 foreach (array("shadowMin", "shadowMax", "shadowWarning", "shadowInactive",
1053 "shadowExpire") as $val){
1055 if ($this->$val != 0){
1056 $oval= "use_".$val;
1057 $this->$oval= "1";
1058 }
1059 }
1060 }
1062 function get_next_id($attrib)
1063 {
1064 $ids= array();
1065 $ldap= $this->config->get_ldap_link();
1067 $ldap->cd ($this->config->current['BASE']);
1068 if (preg_match('/gidNumber/i', $attrib)){
1069 $oc= "posixGroup";
1070 } else {
1071 $oc= "posixAccount";
1072 }
1073 $ldap->search ("(&(objectClass=$oc)($attrib=*))", array("$attrib"));
1075 /* Get list of ids */
1076 while ($attrs= $ldap->fetch()){
1077 $ids[]= (int)$attrs["$attrib"][0];
1078 }
1080 /* Find out next free id near to UID_BASE */
1081 for ($id= $this->config->current['UIDBASE']; $id++; $id<65000){
1082 if (!in_array($id, $ids)){
1083 return ($id);
1084 }
1085 }
1087 /* Should not happen */
1088 if ($id == 65000){
1089 print_red(_("Too many users, can't allocate a free ID!"));
1090 exit;
1091 }
1093 }
1095 function reload()
1096 {
1098 /* Get config */
1099 $groupfilter= get_global('groupfilter');
1101 /* Set base for all searches */
1102 $base= $_SESSION['CurrentMainBase'];
1104 /* Regex filter? */
1105 if ($groupfilter['regex'] != ""){
1106 $regex= $groupfilter['regex'];
1107 } else {
1108 $regex= "*";
1109 }
1111 $error = "";
1112 $ldap = $this->config->get_ldap_link();
1114 $base= get_groups_ou().$base;
1115 $res= get_list("(objectClass=posixGroup)", $this->ui->subtreeACL, $base,
1116 array("cn", "description", "gidNumber"), GL_SIZELIMIT);
1117 if (preg_match("/size limit/i", $error)){
1118 $_SESSION['limit_exceeded']= TRUE;
1119 }
1121 $error = $ldap->error;
1122 $this->grouplist = array();
1123 foreach ($res as $value){
1124 $this->grouplist[$value['gidNumber'][0]]= $value;
1125 }
1127 $tmp=array();
1128 foreach($this->grouplist as $tkey => $val ){
1129 $tmp[strtolower($val['cn'][0]).$val['cn'][0]]=$val;
1130 }
1132 /* Sort index */
1133 ksort($tmp);
1135 /* Recreate index array[dn]=cn[description]*/
1136 $this->grouplist=array();
1137 foreach($tmp as $val){
1138 if(isset($val['description'])){
1139 $this->grouplist[$val['dn']]=$val['cn'][0]." [".$val['description'][0]."]";
1140 }else{
1141 $this->grouplist[$val['dn']]=$val['cn'][0];
1142 }
1143 }
1144 reset ($this->grouplist);
1145 }
1148 /* Create the posix dialog part for copy & paste */
1149 function getCopyDialog()
1150 {
1151 /* Skip dialog creation if this is not a valid account*/
1152 if(!$this->is_account) return("");
1153 if ($this->force_ids == 1){
1154 $force_ids = "checked";
1155 if ($_SESSION['js']){
1156 $forceMode = "";
1157 }
1158 } else {
1159 if ($_SESSION['js']){
1160 if($this->acl != "#none#")
1161 $forceMode ="disabled";
1162 }
1163 $force_ids = "";
1164 }
1166 $sta = "";
1168 /* Open group add dialog */
1169 if(isset($_POST['edit_groupmembership'])){
1170 $this->group_dialog = TRUE;
1171 $sta = "SubDialog";
1172 }
1174 /* If the group-add dialog is closed, call execute
1175 to ensure that the membership is updatd */
1176 if(isset($_POST['add_groups_finish']) || isset($_POST['add_groups_cancel'])){
1177 $this->execute();
1178 $this->group_dialog =FALSE;
1179 }
1181 if($this->group_dialog){
1182 $str = $this->execute(true);
1183 $ret = array();
1184 $ret['string'] = $str;
1185 $ret['status'] = $sta;
1186 return($ret);
1187 }
1189 /* If a group member should be deleted, simply call execute */
1190 if(isset($_POST['delete_groupmembership'])){
1191 $this->execute();
1192 }
1194 /* Assigned informations to smarty */
1195 $smarty = get_smarty();
1196 $smarty->assign("homeDirectory",$this->homeDirectory);
1197 $smarty->assign("uidNumber",$this->uidNumber);
1198 $smarty->assign("gidNumber",$this->gidNumber);
1199 $smarty->assign("forceMode",$forceMode);
1200 $smarty->assign("force_ids",$force_ids);
1201 if (!count($this->groupMembership)){
1202 $smarty->assign("groupMembership", array(" "));
1203 } else {
1204 $smarty->assign("groupMembership", $this->groupMembership);
1205 }
1207 /* Display wars message if there are more than 16 group members */
1208 if (count($this->groupMembership) > 16){
1209 $smarty->assign("groups", "too_many_for_nfs");
1210 } else {
1211 $smarty->assign("groups", "");
1212 }
1213 $str = $smarty->fetch(get_template_path("paste_generic.tpl",TRUE,dirname(__FILE__)));
1215 $ret = array();
1216 $ret['string'] = $str;
1217 $ret['status'] = $sta;
1218 return($ret);
1219 }
1221 }
1223 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
1224 ?>