390a18f8cf742b6d89d8d18c30827a73666a06f2
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);
350 print_a($sysfilter);
352 /* Get workstation list */
353 $exclude= "";
354 foreach($this->accessTo as $ws){
355 $exclude.= "(cn=$ws)";
356 }
357 if ($exclude != ""){
358 $exclude= "(!(|$exclude))";
359 }
360 $acl= array($this->config->current['BASE'] => ":all");
361 $regex= $sysfilter['regex'];
362 $filter= "(&(|(objectClass=goServer)(objectClass=gotoWorkstation)(objectClass=gotoTerminal))$exclude(cn=*)(cn=$regex))";
363 $res= get_list($filter, $acl, $sysfilter['depselect'], array("cn"), GL_SUBSEARCH | GL_SIZELIMIT);
364 $wslist= array();
365 foreach ($res as $attrs){
366 $wslist[]= preg_replace('/\$/', '', $attrs['cn'][0]);
367 }
368 asort($wslist);
369 $smarty->assign("search_image", get_template_path('images/search.png'));
370 $smarty->assign("launchimage", get_template_path('images/small_filter.png'));
371 $smarty->assign("tree_image", get_template_path('images/tree.png'));
372 $smarty->assign("deplist", $this->config->idepartments);
373 $smarty->assign("alphabet", generate_alphabet());
374 foreach( array("depselect", "regex") as $type){
375 $smarty->assign("$type", $sysfilter[$type]);
376 }
377 $smarty->assign("hint", print_sizelimit_warning());
378 $smarty->assign("wslist", $wslist);
379 $smarty->assign("apply", apply_filter());
380 $display= $smarty->fetch (get_template_path('trust_machines.tpl', TRUE, dirname(__FILE__)));
381 return ($display);
382 }
384 /* Manage group add dialog */
385 if ($this->group_dialog){
387 /* Get global filter config */
388 if (!is_global("groupfilter")){
389 $ui= get_userinfo();
390 $base= get_base_from_people($ui->dn);
391 $groupfilter= array("primarygroups" => "checked",
392 "mailgroups" => "checked",
393 "sambagroups" => "checked",
394 "appgroups" => "checked",
395 "functionalgroups" => "checked",
396 "guser" => "*",
397 "subsearch" => "",
398 "depselect" => $_SESSION['CurrentMainBase'],
399 "regex" => "*");
400 register_global("groupfilter", $groupfilter);
401 }
403 /* Save data */
404 $groupfilter= get_global("groupfilter");
405 foreach( array("guser", "regex") as $type){
406 if (isset($_POST[$type])){
407 $groupfilter[$type]= $_POST[$type];
408 }
409 }
410 if (isset($_POST['depselect'])){
411 foreach( array("primarygroups", "sambagroups", "mailgroups", "appgroups",
412 "functionalgroups") as $type){
414 if (isset($_POST[$type])) {
415 $groupfilter[$type]= "checked";
416 } else {
417 $groupfilter[$type]= "";
418 }
419 }
420 }
421 if (isset($_GET['search'])){
422 $s= mb_substr($_GET['search'], 0, 1, "UTF8")."*";
423 if ($s == "**"){
424 $s= "*";
425 }
426 $groupfilter['regex']= $s;
427 }
428 register_global("groupfilter", $groupfilter);
430 /* Calculate actual groups */
432 $this->reload();
433 $glist= array();
434 foreach ($this->grouplist as $key => $value){
435 if (!isset($this->groupMembership[$key])){
436 $glist[$key]= $value;
437 }
438 }
440 $smarty->assign("groups", $glist);
441 $smarty->assign("search_image", get_template_path('images/search.png'));
442 $smarty->assign("launchimage", get_template_path('images/small_filter.png'));
443 $smarty->assign("tree_image", get_template_path('images/tree.png'));
444 $smarty->assign("deplist", $this->config->idepartments);
445 $smarty->assign("alphabet", generate_alphabet());
446 foreach( array("guser", "regex", "primarygroups", "mailgroups",
447 "appgroups", "sambagroups", "functionalgroups") as $type){
448 $smarty->assign("$type", $groupfilter[$type]);
449 }
450 $smarty->assign("depselect",$_SESSION['CurrentMainBase']);
451 $smarty->assign("hint", print_sizelimit_warning());
453 $smarty->assign("apply", apply_filter());
454 $display.= $smarty->fetch (get_template_path('posix_groups.tpl', TRUE, dirname(__FILE__)));
455 return ($display);
456 }
458 /* Show main page */
459 $smarty= get_smarty();
461 /* Depending on pwmode, currently hardcoded because there are no other methods */
462 if ( 1 == 1 ){
463 $smarty->assign("pwmode", dirname(__FILE__)."/posix_shadow");
464 $shadowMinACL= chkacl($this->acl, "shadowMin");
465 $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."\">"));
466 $shadowMaxACL= chkacl($this->acl, "shadowMax");
467 $smarty->assign("shadowmaxs", sprintf(_("Password must be changed after %s days"), "<input name=\"shadowMax\" size=3 maxlength=4 $shadowMaxACL value=\"".$this->shadowMax."\">"));
468 $shadowInactiveACL= chkacl($this->acl, "shadowInactive");
469 $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."\">"));
470 $shadowWarningACL= chkacl($this->acl, "shadowWarning");
471 $smarty->assign("shadowwarnings", sprintf(_("Warn user %s days before password expiery"), "<input name=\"shadowWarning\" size=3 maxlength=4 $shadowWarningACL value=\"".$this->shadowWarning."\">"));
472 foreach( array("must_change_password", "use_shadowMin", "use_shadowMax",
473 "use_shadowExpire", "use_shadowInactive",
474 "use_shadowWarning") as $val){
475 if ($this->$val == 1){
476 $smarty->assign("$val", "checked");
477 } else {
478 $smarty->assign("$val", "");
479 }
480 $smarty->assign("$val"."ACL", chkacl($this->acl, $val));
481 }
482 }
484 /* Fill calendar */
485 $date= getdate($this->shadowExpire);
487 $days= array();
488 for($d= 1; $d<32; $d++){
489 $days[$d]= $d;
490 }
491 $years= array();
492 for($y= $date['year']-10; $y<$date['year']+10; $y++){
493 $years[]= $y;
494 }
495 $months= array(_("January"), _("February"), _("March"), _("April"),
496 _("May"), _("June"), _("July"), _("August"), _("September"),
497 _("October"), _("November"), _("December"));
498 $smarty->assign("day", $date["mday"]);
499 $smarty->assign("days", $days);
500 $smarty->assign("months", $months);
501 $smarty->assign("month", $date["mon"]-1);
502 $smarty->assign("years", $years);
503 $smarty->assign("year", $date["year"]);
505 /* Fill arrays */
506 $smarty->assign("shells", $this->loginShellList);
507 $smarty->assign("secondaryGroups", $this->secondaryGroups);
508 $smarty->assign("primaryGroup", $this->primaryGroup);
509 if (!count($this->groupMembership)){
510 $smarty->assign("groupMembership", array(" "));
511 } else {
512 $smarty->assign("groupMembership", $this->groupMembership);
513 }
514 if (count($this->groupMembership) > 16){
515 $smarty->assign("groups", "too_many_for_nfs");
516 } else {
517 $smarty->assign("groups", "");
518 }
519 $smarty->assign("printerList", $this->printerList);
520 $smarty->assign("languages", $this->config->data['MAIN']['LANGUAGES']);
522 /* Avoid "Undefined index: forceMode" */
523 $smarty->assign("forceMode", "");
525 /* Checkboxes */
526 if ($this->force_ids == 1){
527 $smarty->assign("force_ids", "checked");
528 if ($_SESSION['js']){
529 $smarty->assign("forceMode", "");
530 }
531 } else {
532 if ($_SESSION['js']){
533 if($this->acl != "#none#")
534 $smarty->assign("forceMode", "disabled");
535 }
536 $smarty->assign("force_ids", "");
537 }
538 $smarty->assign("force_idsACL", chkacl($this->acl, "force_ids"));
540 /* Load attributes and acl's */
541 foreach($this->attributes as $val){
542 if((chkacl($this->acl,$val)=="")&&(($_SESSION["js"])&&(($val=="uidNumber")||($val=="gidNumber"))))
543 {
544 $smarty->assign("$val"."ACL","");
545 $smarty->assign("$val", $this->$val);
546 continue;
547 }
548 $smarty->assign("$val", $this->$val);
549 $smarty->assign("$val"."ACL", chkacl($this->acl,$val));
550 }
551 $smarty->assign("groupMembershipACL", chkacl($this->acl, "groupMembership"));
552 $smarty->assign("status", $this->status);
554 /* Work on trust modes */
555 $smarty->assign("trustmodeACL", chkacl($this->acl, "trustmode"));
556 if ($this->trustModel == "fullaccess"){
557 $trustmode= 1;
558 // pervent double disable tag in html code, this will disturb our clean w3c html
560 if(chkacl($this->acl, "trustmode")==""){
561 $smarty->assign("trusthide", "disabled");
562 }else{
563 $smarty->assign("trusthide", "");
564 }
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 if(chkacl($this->acl, "trustmode")==""){
572 $smarty->assign("trusthide", "disabled");
573 }else{
574 $smarty->assign("trusthide", "");
575 }
576 $trustmode= 0;
577 }
578 $smarty->assign("trustmode", $trustmode);
579 $smarty->assign("trustmodes", array( 0 => _("disabled"), 1 => _("full access"),
580 2 => _("allow access to these hosts")));
584 if((count($this->accessTo))==0)
585 $smarty->assign("emptyArrAccess",true);
586 else
587 $smarty->assign("emptyArrAccess",false);
591 $smarty->assign("workstations", $this->accessTo);
593 $smarty->assign("apply", apply_filter());
594 $display.= $smarty->fetch (get_template_path('generic.tpl', TRUE, dirname(__FILE__)));
595 return($display);
596 }
599 /* remove object from parent */
600 function remove_from_parent()
601 {
602 /* Cancel if there's nothing to do here */
603 if (!$this->initially_was_account){
604 return;
605 }
607 /* include global link_info */
608 $ldap= $this->config->get_ldap_link();
610 /* Remove and write to LDAP */
611 plugin::remove_from_parent();
613 /* Zero out array */
614 $this->attrs['gosaHostACL']= array();
616 /* Keep uid, because we need it for authentification! */
617 unset($this->attrs['uid']);
618 unset($this->attrs['trustModel']);
620 @DEBUG (DEBUG_LDAP, __LINE__, __FUNCTION__, __FILE__,
621 $this->attributes, "Save");
622 $ldap->cd($this->dn);
623 $this->cleanup();
624 $ldap->modify ($this->attrs);
626 show_ldap_error($ldap->get_error());
628 /* Delete group only if cn is uid and there are no other
629 members inside */
630 $ldap->cd ($this->config->current['BASE']);
631 $ldap->search ("(&(objectClass=posixGroup)(gidNumber=".$this->gidNumber."))", array("cn", "memberUid"));
632 if ($ldap->count() != 0){
633 $attrs= $ldap->fetch();
634 if ($attrs['cn'][0] == $this->uid &&
635 !isset($this->attrs['memberUid'])){
637 $ldap->rmDir($ldap->getDN());
638 }
639 }
641 /* Optionally execute a command after we're done */
642 $this->handle_post_events("remove");
643 }
646 function save_object()
647 {
648 if (isset($_POST['posixTab'])){
649 /* Save values to object */
650 plugin::save_object();
652 /* Save force GID attribute */
653 if (chkacl ($this->acl, "force_ids") == ""){
654 if (isset ($_POST['force_ids'])){
655 $data= 1;
656 } else {
657 $data= 0;
658 }
659 if ($this->force_ids != $data){
660 $this->is_modified= TRUE;
661 }
662 $this->force_ids= $data;
664 $data= $_POST['primaryGroup'];
665 if ($this->primaryGroup != $data){
666 $this->is_modified= TRUE;
667 }
668 $this->primaryGroup= $_POST['primaryGroup'];
669 }
671 /* Save pwmode dependent attributes, curently hardcoded because there're
672 no alternatives */
673 if (1 == 1){
674 foreach( array("must_change_password", "use_shadowMin", "use_shadowMax",
675 "use_shadowExpire", "use_shadowInactive",
676 "use_shadowWarning") as $val){
677 if (chkacl($this->acl, "$val") == ""){
678 if (isset ($_POST[$val])){
679 $data= 1;
680 } else {
681 $data= 0;
682 }
683 if ($data != $this->$val){
684 $this->is_modified= TRUE;
685 }
686 $this->$val= $data;
687 }
688 }
689 }
691 /* Trust mode - special handling */
692 if (isset($_POST['trustmode'])){
693 $saved= $this->trustModel;
694 if ($_POST['trustmode'] == "1"){
695 $this->trustModel= "fullaccess";
696 } elseif ($_POST['trustmode'] == "2"){
697 $this->trustModel= "byhost";
698 } else {
699 $this->trustModel= "";
700 }
701 if ($this->trustModel != $saved){
702 $this->is_modified= TRUE;
703 }
704 }
705 }
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->must_change_password){
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->must_change_password != "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());
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);
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[$ldap->getDN()]= $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 {
1099 /* Get config */
1100 $groupfilter= get_global('groupfilter');
1102 /* Set base for all searches */
1103 $base= $_SESSION['CurrentMainBase'];
1105 /* Regex filter? */
1106 if ($groupfilter['regex'] != ""){
1107 $regex= $groupfilter['regex'];
1108 } else {
1109 $regex= "*";
1110 }
1112 $error = "";
1113 $ldap = $this->config->get_ldap_link();
1115 $base= get_groups_ou().$base;
1116 $res= get_list("(objectClass=posixGroup)", $this->ui->subtreeACL, $base,
1117 array("cn", "description", "gidNumber"), GL_SIZELIMIT);
1118 if (preg_match("/size limit/i", $error)){
1119 $_SESSION['limit_exceeded']= TRUE;
1120 }
1122 $error = $ldap->error;
1123 $this->grouplist = array();
1124 foreach ($res as $value){
1125 $this->grouplist[$value['gidNumber'][0]]= $value;
1126 }
1128 $tmp=array();
1129 foreach($this->grouplist as $tkey => $val ){
1130 $tmp[strtolower($val['cn'][0]).$val['cn'][0]]=$val;
1131 }
1133 /* Sort index */
1134 ksort($tmp);
1136 /* Recreate index array[dn]=cn[description]*/
1137 $this->grouplist=array();
1138 foreach($tmp as $val){
1139 if(isset($val['description'])){
1140 $this->grouplist[$val['dn']]=$val['cn'][0]." [".$val['description'][0]."]";
1141 }else{
1142 $this->grouplist[$val['dn']]=$val['cn'][0];
1143 }
1144 }
1145 reset ($this->grouplist);
1146 }
1148 function getCopyDialog()
1149 {
1150 if(!$this->is_account) return("");
1151 if ($this->force_ids == 1){
1152 $force_ids = "checked";
1153 if ($_SESSION['js']){
1154 $forceMode = "";
1155 }
1156 } else {
1157 if ($_SESSION['js']){
1158 if($this->acl != "#none#")
1159 $forceMode ="disabled";
1160 }
1161 $force_ids = "";
1162 }
1164 $sta = "";
1166 if(isset($_POST['edit_groupmembership'])){
1167 $this->group_dialog = TRUE;
1168 $sta = "SubDialog";
1169 }
1171 if(isset($_POST['add_groups_finish']) || isset($_POST['add_groups_cancel'])){
1172 $this->execute();
1173 $this->group_dialog =FALSE;
1174 }
1176 if($this->group_dialog){
1177 $str = $this->execute(true);
1178 $ret = array();
1179 $ret['string'] = $str;
1180 $ret['status'] = $sta;
1181 return($ret);
1182 }
1184 if(isset($_POST['delete_groupmembership'])){
1185 $this->execute();
1186 }
1188 $smarty = get_smarty();
1189 $smarty->assign("homeDirectory",$this->homeDirectory);
1190 $smarty->assign("uidNumber",$this->uidNumber);
1191 $smarty->assign("gidNumber",$this->gidNumber);
1192 $smarty->assign("forceMode",$forceMode);
1193 $smarty->assign("force_ids",$force_ids);
1194 if (!count($this->groupMembership)){
1195 $smarty->assign("groupMembership", array(" "));
1196 } else {
1197 $smarty->assign("groupMembership", $this->groupMembership);
1198 }
1199 if (count($this->groupMembership) > 16){
1200 $smarty->assign("groups", "too_many_for_nfs");
1201 } else {
1202 $smarty->assign("groups", "");
1203 }
1204 $str = $smarty->fetch(get_template_path("paste_generic.tpl",TRUE,dirname(__FILE__)));
1206 $ret = array();
1207 $ret['string'] = $str;
1208 $ret['status'] = $sta;
1209 return($ret);
1210 }
1212 }
1214 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
1215 ?>