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");
118 $current= floor($current / 60 /60 / 24);
120 if (($current >= $this->shadowExpire) && $this->shadowExpire){
121 $this->status= _("expired");
122 if (($current - $this->shadowExpire) < $this->shadowInactive){
123 $this->status.= ", "._("grace time active");
124 }
125 } elseif (($this->shadowLastChange + $this->shadowMin) >= $current){
126 $this->status= _("active, password not changable");
127 } elseif (($this->shadowLastChange + $this->shadowMax) >= $current){
128 $this->status= _("active, password expired");
129 } else {
130 $this->status= _("active");
131 }
133 /* Get group membership */
134 $ldap->cd($this->config->current['BASE']);
135 $ldap->search("(&(objectClass=posixGroup)(memberUid=".$this->uid."))", array("cn", "description"));
137 while ($this->attrs= $ldap->fetch()){
138 if (!isset($this->attrs["description"][0])){
139 $entry= $this->attrs["cn"][0];
140 } else {
141 $dsc= preg_replace ('/^Group of user/', _("Group of user"), $this->attrs["description"][0]);
142 $entry= $this->attrs["cn"][0]." [$dsc]";
143 }
144 $this->groupMembership[$ldap->getDN()]= $entry;
145 }
146 asort($this->groupMembership);
147 reset($this->groupMembership);
148 $this->savedGroupMembership= $this->groupMembership;
149 $this->savedUidNumber= $this->uidNumber;
150 $this->savedGidNumber= $this->gidNumber;
151 }
153 /* Adjust shadow checkboxes */
154 foreach (array("shadowMin", "shadowMax", "shadowWarning", "shadowInactive",
155 "shadowExpire") as $val){
157 if ($this->$val != 0){
158 $oval= "use_".$val;
159 $this->$oval= "1";
160 }
161 }
163 /* Convert to seconds */
164 if ($this->shadowExpire != 0){
165 $this->shadowExpire*= 60 * 60 * 24;
166 } else {
167 $date= getdate();
168 $this->shadowExpire= floor($date[0] / (60*60*24)) * 60 * 60 * 24;
169 }
171 /* Generate shell list from /etc/gosa/shells */
172 if (file_exists('/etc/gosa/shells')){
173 $shells = file ('/etc/gosa/shells');
174 foreach ($shells as $line){
175 if (!preg_match ("/^#/", $line)){
176 $this->loginShellList[]= trim($line);
177 }
178 }
179 } else {
180 if ($this->loginShell == ""){
181 $this->loginShellList[]= _("unconfigured");
182 }
183 }
185 /* Insert possibly missing loginShell */
186 if ($this->loginShell != "" && !in_array($this->loginShell, $this->loginShellList)){
187 $this->loginShellList[]= $this->loginShell;
188 }
190 /* Generate printer list */
191 if (isset($this->config->data['SERVERS']['CUPS'])){
192 $this->printerList= get_printer_list ($this->config->data['SERVERS']['CUPS']);
193 asort($this->printerList);
194 }
196 /* Generate group list */
197 $ldap->cd($this->config->current['BASE']);
198 $ldap->search("(objectClass=posixGroup)", array("cn", "gidNumber"));
199 $this->secondaryGroups[]= "- "._("automatic")." -";
200 while ($attrs= $ldap->fetch()){
201 $this->secondaryGroups[$attrs['gidNumber'][0]]= $attrs['cn'][0];
202 }
203 asort ($this->secondaryGroups);
205 /* Get global filter config */
206 if (!is_global("sysfilter")){
207 $ui= get_userinfo();
208 $base= get_base_from_people($ui->dn);
209 $sysfilter= array( "depselect" => $base,
210 "regex" => "*");
211 register_global("sysfilter", $sysfilter);
212 }
213 $this->ui = get_userinfo();
214 }
217 /* execute generates the html output for this node */
218 function execute($isCopyPaste = false)
219 {
220 /* Call parent execute */
221 plugin::execute();
222 $display= "";
224 /* Department has changed? */
225 if(isset($_POST['depselect'])){
226 $_SESSION['CurrentMainBase']= validate($_POST['depselect']);
227 }
229 if(!$isCopyPaste){
230 /* Do we need to flip is_account state? */
231 if (isset($_POST['modify_state'])){
232 $this->is_account= !$this->is_account;
233 }
235 /* Do we represent a valid posixAccount? */
236 if (!$this->is_account && $this->parent == NULL ){
237 $display= "<img alt=\"\" src=\"images/stop.png\" align=\"middle\"> <b>".
238 _("This account has no unix extensions.")."</b>";
239 $display.= back_to_main();
240 return ($display);
241 }
244 /* Show tab dialog headers */
245 if ($this->parent != NULL){
246 if ($this->is_account){
247 if (isset($this->parent->by_object['sambaAccount'])){
248 $obj= $this->parent->by_object['sambaAccount'];
249 }
250 if (isset($obj) && $obj->is_account == TRUE &&
251 ((isset($this->parent->by_object['sambaAccount']))&&($this->parent->by_object['sambaAccount']->is_account))
252 ||(isset($this->parent->by_object['environment'] ))&&($this->parent->by_object['environment'] ->is_account)){
254 /* Samba3 dependency on posix accounts are enabled
255 in the moment, because I need to rely on unique
256 uidNumbers. There'll be a better solution later
257 on. */
258 $display= $this->show_header(_("Remove posix account"),
259 _("This account has unix features enabled. To disable them, you'll need to remove the samba / environment account first."), TRUE);
260 } else {
261 $display= $this->show_header(_("Remove posix account"),
262 _("This account has posix features enabled. You can disable them by clicking below."));
263 }
264 } else {
265 $display= $this->show_header(_("Create posix account"),
266 _("This account has posix features disabled. You can enable them by clicking below."));
267 return($display);
268 }
269 }
270 }
271 /* Trigger group edit? */
272 if (isset($_POST['edit_groupmembership'])){
273 $this->group_dialog= TRUE;
274 $this->dialog= TRUE;
275 }
277 /* Cancel group edit? */
278 if (isset($_POST['add_groups_cancel']) ||
279 isset($_POST['add_groups_finish'])){
280 $this->group_dialog= FALSE;
281 $this->dialog= FALSE;
282 }
284 /* Add selected groups */
285 if (isset($_POST['add_groups_finish']) && isset($_POST['groups']) &&
286 count($_POST['groups'])){
288 if (chkacl ($this->acl, "memberUid") == ""){
289 $this->addGroup ($_POST['groups']);
290 $this->is_modified= TRUE;
291 }
292 }
294 /* Delete selected groups */
295 if (isset($_POST['delete_groupmembership']) &&
296 isset($_POST['group_list']) && count($_POST['group_list'])){
298 if (chkacl ($this->acl, "memberUid") == ""){
299 $this->delGroup ($_POST['group_list']);
300 $this->is_modified= TRUE;
301 }
302 }
304 /* Add user workstation? */
305 if (isset($_POST["add_ws"])){
306 $this->show_ws_dialog= TRUE;
307 $this->dialog= TRUE;
308 }
310 /* Add user workstation? */
311 if (isset($_POST["add_ws_finish"]) && isset($_POST['wslist'])){
312 foreach($_POST['wslist'] as $ws){
313 $this->accessTo[$ws]= $ws;
314 }
315 ksort($this->accessTo);
316 $this->is_modified= TRUE;
317 }
319 /* Remove user workstations? */
320 if (isset($_POST["delete_ws"]) && isset($_POST['workstation_list'])){
321 foreach($_POST['workstation_list'] as $name){
322 unset ($this->accessTo[$name]);
323 }
324 $this->is_modified= TRUE;
325 }
327 /* Add user workstation finished? */
328 if (isset($_POST["add_ws_finish"]) || isset($_POST["add_ws_cancel"])){
329 $this->show_ws_dialog= FALSE;
330 $this->dialog= FALSE;
331 }
333 /* Templates now! */
334 $smarty= get_smarty();
336 /* Show ws dialog */
337 if ($this->show_ws_dialog){
338 /* Save data */
339 $sysfilter= get_global("sysfilter");
340 foreach( array("depselect", "regex") as $type){
341 if (isset($_POST[$type])){
342 $sysfilter[$type]= $_POST[$type];
343 }
344 }
345 if (isset($_GET['search'])){
346 $s= mb_substr($_GET['search'], 0, 1, "UTF8")."*";
347 if ($s == "**"){
348 $s= "*";
349 }
350 $sysfilter['regex']= $s;
351 }
352 register_global("sysfilter", $sysfilter);
354 /* Get workstation list */
355 $exclude= "";
356 foreach($this->accessTo as $ws){
357 $exclude.= "(cn=$ws)";
358 }
359 if ($exclude != ""){
360 $exclude= "(!(|$exclude))";
361 }
362 $acl= array($this->config->current['BASE'] => ":all");
363 $regex= $sysfilter['regex'];
364 $filter= "(&(|(objectClass=goServer)(objectClass=gotoWorkstation)(objectClass=gotoTerminal))$exclude(cn=*)(cn=$regex))";
365 $res= get_list($filter, $acl, $sysfilter['depselect'], array("cn"), GL_SUBSEARCH | GL_SIZELIMIT);
366 $wslist= array();
367 foreach ($res as $attrs){
368 $wslist[]= preg_replace('/\$/', '', $attrs['cn'][0]);
369 }
370 asort($wslist);
371 $smarty->assign("search_image", get_template_path('images/search.png'));
372 $smarty->assign("launchimage", get_template_path('images/small_filter.png'));
373 $smarty->assign("tree_image", get_template_path('images/tree.png'));
374 $smarty->assign("deplist", $this->config->idepartments);
375 $smarty->assign("alphabet", generate_alphabet());
376 foreach( array("depselect", "regex") as $type){
377 $smarty->assign("$type", $sysfilter[$type]);
378 }
379 $smarty->assign("hint", print_sizelimit_warning());
380 $smarty->assign("wslist", $wslist);
381 $smarty->assign("apply", apply_filter());
382 $display= $smarty->fetch (get_template_path('trust_machines.tpl', TRUE, dirname(__FILE__)));
383 return ($display);
384 }
386 /* Manage group add dialog */
387 if ($this->group_dialog){
389 /* Get global filter config */
390 if (!is_global("groupfilter")){
391 $ui= get_userinfo();
392 $base= get_base_from_people($ui->dn);
393 $groupfilter= array("primarygroups" => "checked",
394 "mailgroups" => "checked",
395 "sambagroups" => "checked",
396 "appgroups" => "checked",
397 "functionalgroups" => "checked",
398 "guser" => "*",
399 "subsearch" => "",
400 "depselect" => $_SESSION['CurrentMainBase'],
401 "regex" => "*");
402 register_global("groupfilter", $groupfilter);
403 }
405 /* Save data */
406 $groupfilter= get_global("groupfilter");
407 foreach( array("guser", "regex") as $type){
408 if (isset($_POST[$type])){
409 $groupfilter[$type]= $_POST[$type];
410 }
411 }
412 if (isset($_POST['depselect'])){
413 foreach( array("primarygroups", "sambagroups", "mailgroups", "appgroups",
414 "functionalgroups") as $type){
416 if (isset($_POST[$type])) {
417 $groupfilter[$type]= "checked";
418 } else {
419 $groupfilter[$type]= "";
420 }
421 }
422 }
423 if (isset($_GET['search'])){
424 $s= mb_substr($_GET['search'], 0, 1, "UTF8")."*";
425 if ($s == "**"){
426 $s= "*";
427 }
428 $groupfilter['regex']= $s;
429 }
430 register_global("groupfilter", $groupfilter);
432 /* Calculate actual groups */
434 $this->reload();
435 $glist= array();
436 foreach ($this->grouplist as $key => $value){
437 if (!isset($this->groupMembership[$key])){
438 $glist[$key]= $value;
439 }
440 }
442 $smarty->assign("groups", $glist);
443 $smarty->assign("search_image", get_template_path('images/search.png'));
444 $smarty->assign("launchimage", get_template_path('images/small_filter.png'));
445 $smarty->assign("tree_image", get_template_path('images/tree.png'));
446 $smarty->assign("deplist", $this->config->idepartments);
447 $smarty->assign("alphabet", generate_alphabet());
448 foreach( array("guser", "regex", "primarygroups", "mailgroups",
449 "appgroups", "sambagroups", "functionalgroups") as $type){
450 $smarty->assign("$type", $groupfilter[$type]);
451 }
452 $smarty->assign("depselect",$_SESSION['CurrentMainBase']);
453 $smarty->assign("hint", print_sizelimit_warning());
455 $smarty->assign("apply", apply_filter());
456 $display.= $smarty->fetch (get_template_path('posix_groups.tpl', TRUE, dirname(__FILE__)));
457 return ($display);
458 }
460 /* Show main page */
461 $smarty= get_smarty();
463 /* Depending on pwmode, currently hardcoded because there are no other methods */
464 if ( 1 == 1 ){
465 $smarty->assign("pwmode", dirname(__FILE__)."/posix_shadow");
466 $shadowMinACL= chkacl($this->acl, "shadowMin");
467 $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."\">"));
468 $shadowMaxACL= chkacl($this->acl, "shadowMax");
469 $smarty->assign("shadowmaxs", sprintf(_("Password must be changed after %s days"), "<input name=\"shadowMax\" size=3 maxlength=4 $shadowMaxACL value=\"".$this->shadowMax."\">"));
470 $shadowInactiveACL= chkacl($this->acl, "shadowInactive");
471 $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."\">"));
472 $shadowWarningACL= chkacl($this->acl, "shadowWarning");
473 $smarty->assign("shadowwarnings", sprintf(_("Warn user %s days before password expiery"), "<input name=\"shadowWarning\" size=3 maxlength=4 $shadowWarningACL value=\"".$this->shadowWarning."\">"));
474 foreach( array("must_change_password", "use_shadowMin", "use_shadowMax",
475 "use_shadowExpire", "use_shadowInactive",
476 "use_shadowWarning") as $val){
477 if ($this->$val == 1){
478 $smarty->assign("$val", "checked");
479 } else {
480 $smarty->assign("$val", "");
481 }
482 $smarty->assign("$val"."ACL", chkacl($this->acl, $val));
483 }
484 }
486 /* Fill calendar */
487 $date= getdate($this->shadowExpire);
489 $days= array();
490 for($d= 1; $d<32; $d++){
491 $days[$d]= $d;
492 }
493 $years= array();
494 for($y= $date['year']-10; $y<$date['year']+10; $y++){
495 $years[]= $y;
496 }
497 $months= array(_("January"), _("February"), _("March"), _("April"),
498 _("May"), _("June"), _("July"), _("August"), _("September"),
499 _("October"), _("November"), _("December"));
500 $smarty->assign("day", $date["mday"]);
501 $smarty->assign("days", $days);
502 $smarty->assign("months", $months);
503 $smarty->assign("month", $date["mon"]-1);
504 $smarty->assign("years", $years);
505 $smarty->assign("year", $date["year"]);
507 /* Fill arrays */
508 $smarty->assign("shells", $this->loginShellList);
509 $smarty->assign("secondaryGroups", $this->secondaryGroups);
510 $smarty->assign("primaryGroup", $this->primaryGroup);
511 if (!count($this->groupMembership)){
512 $smarty->assign("groupMembership", array(" "));
513 } else {
514 $smarty->assign("groupMembership", $this->groupMembership);
515 }
516 if (count($this->groupMembership) > 16){
517 $smarty->assign("groups", "too_many_for_nfs");
518 } else {
519 $smarty->assign("groups", "");
520 }
521 $smarty->assign("printerList", $this->printerList);
522 $smarty->assign("languages", $this->config->data['MAIN']['LANGUAGES']);
524 /* Avoid "Undefined index: forceMode" */
525 $smarty->assign("forceMode", "");
527 /* Checkboxes */
528 if ($this->force_ids == 1){
529 $smarty->assign("force_ids", "checked");
530 if ($_SESSION['js']){
531 $smarty->assign("forceMode", "");
532 }
533 } else {
534 if ($_SESSION['js']){
535 if($this->acl != "#none#")
536 $smarty->assign("forceMode", "disabled");
537 }
538 $smarty->assign("force_ids", "");
539 }
540 $smarty->assign("force_idsACL", chkacl($this->acl, "force_ids"));
542 /* Load attributes and acl's */
543 foreach($this->attributes as $val){
544 if((chkacl($this->acl,$val)=="")&&(($_SESSION["js"])&&(($val=="uidNumber")||($val=="gidNumber"))))
545 {
546 $smarty->assign("$val"."ACL","");
547 $smarty->assign("$val", $this->$val);
548 continue;
549 }
550 $smarty->assign("$val", $this->$val);
551 $smarty->assign("$val"."ACL", chkacl($this->acl,$val));
552 }
553 $smarty->assign("groupMembershipACL", chkacl($this->acl, "groupMembership"));
554 $smarty->assign("status", $this->status);
556 /* Work on trust modes */
557 $smarty->assign("trustmodeACL", chkacl($this->acl, "trustmode"));
558 if ($this->trustModel == "fullaccess"){
559 $trustmode= 1;
560 // pervent double disable tag in html code, this will disturb our clean w3c html
562 if(chkacl($this->acl, "trustmode")==""){
563 $smarty->assign("trusthide", "disabled");
564 }else{
565 $smarty->assign("trusthide", "");
566 }
568 } elseif ($this->trustModel == "byhost"){
569 $trustmode= 2;
570 $smarty->assign("trusthide", "");
571 } else {
572 // pervent double disable tag in html code, this will disturb our clean w3c html
573 if(chkacl($this->acl, "trustmode")==""){
574 $smarty->assign("trusthide", "disabled");
575 }else{
576 $smarty->assign("trusthide", "");
577 }
578 $trustmode= 0;
579 }
580 $smarty->assign("trustmode", $trustmode);
581 $smarty->assign("trustmodes", array( 0 => _("disabled"), 1 => _("full access"),
582 2 => _("allow access to these hosts")));
586 if((count($this->accessTo))==0)
587 $smarty->assign("emptyArrAccess",true);
588 else
589 $smarty->assign("emptyArrAccess",false);
593 $smarty->assign("workstations", $this->accessTo);
595 $smarty->assign("apply", apply_filter());
596 $display.= $smarty->fetch (get_template_path('generic.tpl', TRUE, dirname(__FILE__)));
597 return($display);
598 }
601 /* remove object from parent */
602 function remove_from_parent()
603 {
604 /* Cancel if there's nothing to do here */
605 if (!$this->initially_was_account){
606 return;
607 }
609 /* include global link_info */
610 $ldap= $this->config->get_ldap_link();
612 /* Remove and write to LDAP */
613 plugin::remove_from_parent();
615 /* Zero out array */
616 $this->attrs['gosaHostACL']= array();
618 /* Keep uid, because we need it for authentification! */
619 unset($this->attrs['uid']);
620 unset($this->attrs['trustModel']);
622 @DEBUG (DEBUG_LDAP, __LINE__, __FUNCTION__, __FILE__,
623 $this->attributes, "Save");
624 $ldap->cd($this->dn);
625 $this->cleanup();
626 $ldap->modify ($this->attrs);
628 show_ldap_error($ldap->get_error());
630 /* Delete group only if cn is uid and there are no other
631 members inside */
632 $ldap->cd ($this->config->current['BASE']);
633 $ldap->search ("(&(objectClass=posixGroup)(gidNumber=".$this->gidNumber."))", array("cn", "memberUid"));
634 if ($ldap->count() != 0){
635 $attrs= $ldap->fetch();
636 if ($attrs['cn'][0] == $this->uid &&
637 !isset($this->attrs['memberUid'])){
639 $ldap->rmDir($ldap->getDN());
640 }
641 }
643 /* Optionally execute a command after we're done */
644 $this->handle_post_events("remove");
645 }
648 function save_object()
649 {
650 if (isset($_POST['posixTab'])){
651 /* Save values to object */
652 plugin::save_object();
654 /* Save force GID attribute */
655 if (chkacl ($this->acl, "force_ids") == ""){
656 if (isset ($_POST['force_ids'])){
657 $data= 1;
658 } else {
659 $data= 0;
660 }
661 if ($this->force_ids != $data){
662 $this->is_modified= TRUE;
663 }
664 $this->force_ids= $data;
666 $data= $_POST['primaryGroup'];
667 if ($this->primaryGroup != $data){
668 $this->is_modified= TRUE;
669 }
670 $this->primaryGroup= $_POST['primaryGroup'];
671 }
673 /* Save pwmode dependent attributes, curently hardcoded because there're
674 no alternatives */
675 if (1 == 1){
676 foreach( array("must_change_password", "use_shadowMin", "use_shadowMax",
677 "use_shadowExpire", "use_shadowInactive",
678 "use_shadowWarning") as $val){
679 if (chkacl($this->acl, "$val") == ""){
680 if (isset ($_POST[$val])){
681 $data= 1;
682 } else {
683 $data= 0;
684 }
685 if ($data != $this->$val){
686 $this->is_modified= TRUE;
687 }
688 $this->$val= $data;
689 }
690 }
691 }
693 /* Trust mode - special handling */
694 if (isset($_POST['trustmode'])){
695 $saved= $this->trustModel;
696 if ($_POST['trustmode'] == "1"){
697 $this->trustModel= "fullaccess";
698 } elseif ($_POST['trustmode'] == "2"){
699 $this->trustModel= "byhost";
700 } else {
701 $this->trustModel= "";
702 }
703 if ($this->trustModel != $saved){
704 $this->is_modified= TRUE;
705 }
706 }
707 }
708 }
711 /* Save data to LDAP, depending on is_account we save or delete */
712 function save()
713 {
715 /* include global link_info */
716 $ldap= $this->config->get_ldap_link();
718 /* Adapt shadow values */
719 if (!$this->use_shadowExpire){
720 $this->shadowExpire= "0";
721 } else {
722 /* Transform seconds to days here */
723 $this->shadowExpire= (int)($this->shadowExpire / (60 * 60 * 24)) ;
724 }
725 if (!$this->use_shadowMax){
726 $this->shadowMax= "0";
727 }
728 if ($this->must_change_password){
729 $this->shadowLastChange= (int)(date("U") / 86400) - $this->shadowMax - 1;
730 } else {
731 $this->shadowLastChange= (int)(date("U") / 86400);
732 }
733 if (!$this->use_shadowWarning){
734 $this->shadowWarning= "0";
735 }
737 /* Check what to do with ID's */
738 if ($this->force_ids == 0){
740 /* Use id's that are already set */
741 if ($this->savedUidNumber != ""){
742 $this->uidNumber= $this->savedUidNumber;
743 $this->gidNumber= $this->savedGidNumber;
744 } else {
746 /* Calculate new id's. We need to place a lock before calling get_next_id
747 to get real unique values. */
748 $wait= 10;
749 while (get_lock("uidnumber") != ""){
750 sleep (1);
752 /* Oups - timed out */
753 if ($wait-- == 0){
754 print_red (_("Failed: overriding lock"));
755 break;
756 }
757 }
759 add_lock ("uidnumber", "gosa");
760 $this->uidNumber= $this->get_next_id("uidNumber");
761 if ($this->savedGidNumber != ""){
762 $this->gidNumber= $this->savedGidNumber;
763 } else {
764 $this->gidNumber= $this->get_next_id("gidNumber");
765 }
766 }
768 if ($this->primaryGroup != 0){
769 $this->gidNumber= $this->primaryGroup;
770 }
771 }
773 if ($this->use_shadowMin != "1" ) {
774 $this->shadowMin = "";
775 }
777 if (($this->use_shadowMax != "1") && ($this->must_change_password != "1")) {
778 $this->shadowMax = "";
779 }
781 if ($this->use_shadowWarning != "1" ) {
782 $this->shadowWarning = "";
783 }
785 if ($this->use_shadowInactive != "1" ) {
786 $this->shadowInactive = "";
787 }
789 if ($this->use_shadowExpire != "1" ) {
790 $this->shadowExpire = "";
791 }
793 /* Fill gecos */
794 if (isset($this->parent) && $this->parent != NULL){
795 $this->gecos= rewrite($this->parent->by_object['user']->cn);
796 if (!preg_match('/[a-z0-9 -]/i', $this->gecos)){
797 $this->gecos= "";
798 }
799 }
801 foreach(array("shadowMin","shadowMax","shadowWarning","shadowInactive","shadowExpire") as $attr){
802 $this->$attr = (int) $this->$attr;
803 }
804 /* Call parents save to prepare $this->attrs */
805 plugin::save();
807 /* Trust accounts */
808 $objectclasses= array();
809 foreach ($this->attrs['objectClass'] as $key => $class){
810 if (preg_match('/trustAccount/i', $class)){
811 continue;
812 }
813 $objectclasses[]= $this->attrs['objectClass'][$key];
814 }
815 $this->attrs['objectClass']= $objectclasses;
816 if ($this->trustModel != ""){
817 $this->attrs['objectClass'][]= "trustAccount";
818 $this->attrs['trustModel']= $this->trustModel;
819 $this->attrs['accessTo']= array();
820 if ($this->trustModel == "byhost"){
821 foreach ($this->accessTo as $host){
822 $this->attrs['accessTo'][]= $host;
823 }
824 }
825 } else {
826 if ($this->was_trust_account){
827 $this->attrs['accessTo']= array();
828 $this->attrs['trustModel']= array();
829 }
830 }
832 if(empty($this->attrs['gosaDefaultPrinter'])){
833 $thid->attrs['gosaDefaultPrinter']=array();
834 }
837 /* Save data to LDAP */
838 $ldap->cd($this->dn);
839 $this->cleanup();
840 unset($this->attrs['uid']);
841 $ldap->modify ($this->attrs);
843 show_ldap_error($ldap->get_error());
845 /* Remove lock needed for unique id generation */
846 del_lock ("uidnumber");
849 /* Posix accounts have group interrelationship, take care about these here. */
850 if ($this->force_ids == 0 && $this->primaryGroup == 0){
851 $ldap->cd($this->config->current['BASE']);
852 $ldap->search("(&(objectClass=posixGroup)(gidNumber=".$this->gidNumber."))", array("cn"));
854 /* Create group if it doesn't exist */
855 if ($ldap->count() == 0){
856 $groupdn= preg_replace ('/^'.$this->config->current['DNMODE'].'=[^,]+,'.get_people_ou().'/i', 'cn='.$this->uid.','.get_groups_ou(), $this->dn);
858 $g= new group($this->config, $groupdn);
859 $g->cn= $this->uid;
860 $g->force_gid= 1;
861 $g->gidNumber= $this->gidNumber;
862 $g->description= "Group of user ".$this->givenName." ".$this->sn;
863 $g->save ();
864 }
865 }
867 /* Take care about groupMembership values: add to groups */
868 foreach ($this->groupMembership as $key => $value){
869 $g= new grouptabs($this->config,$this->config->data['TABS']['GROUPTABS'], $key);
870 $g->by_object['group']->addUser($this->uid);
871 $g->save();
872 }
874 /* Remove from groups not listed in groupMembership */
875 foreach ($this->savedGroupMembership as $key => $value){
876 if (!isset($this->groupMembership[$key])){
877 $g= new grouptabs($this->config,$this->config->data['TABS']['GROUPTABS'], $key);
878 $g->by_object['group']->removeUser ($this->uid);
879 $g->save();
880 }
881 }
883 /* Optionally execute a command after we're done */
884 if ($this->initially_was_account == $this->is_account){
885 if ($this->is_modified){
886 $this->handle_post_events("mofify");
887 }
888 } else {
889 $this->handle_post_events("add");
890 }
891 }
893 /* Check formular input */
894 function check()
895 {
896 /* Include global link_info */
897 $ldap= $this->config->get_ldap_link();
899 /* Call common method to give check the hook */
900 $message= plugin::check();
902 /* must: homeDirectory */
903 if ($this->homeDirectory == ""){
904 $message[]= _("The required field 'Home directory' is not set.");
905 }
906 if (!is_path($this->homeDirectory)){
907 $message[]= _("Please enter a valid path in 'Home directory' field.");
908 }
910 /* Check ID's if they are forced by user */
911 if ($this->force_ids == "1"){
913 /* Valid uid/gid? */
914 if (!is_id($this->uidNumber)){
915 $message[]= _("Value specified as 'UID' is not valid.");
916 } else {
917 if ($this->uidNumber < $this->config->current['MINID']){
918 $message[]= _("Value specified as 'UID' is too small.");
919 }
920 }
921 if (!is_id($this->gidNumber)){
922 $message[]= _("Value specified as 'GID' is not valid.");
923 } else {
924 if ($this->gidNumber < $this->config->current['MINID']){
925 $message[]= _("Value specified as 'GID' is too small.");
926 }
927 }
928 }
930 /* Check shadow settings, well I like spaghetties... */
931 if ($this->use_shadowMin){
932 if (!is_id($this->shadowMin)){
933 $message[]= _("Value specified as 'shadowMin' is not valid.");
934 }
935 }
936 if ($this->use_shadowMax){
937 if (!is_id($this->shadowMax)){
938 $message[]= _("Value specified as 'shadowMax' is not valid.");
939 }
940 }
941 if ($this->use_shadowWarning){
942 if (!is_id($this->shadowWarning)){
943 $message[]= _("Value specified as 'shadowWarning' is not valid.");
944 }
945 if (!$this->use_shadowMax){
946 $message[]= _("'shadowWarning' without 'shadowMax' makes no sense.");
947 }
948 if ($this->shadowWarning > $this->shadowMax){
949 $message[]= _("Value specified as 'shadowWarning' should be smaller than 'shadowMax'.");
950 }
951 if ($this->use_shadowMin && $this->shadowWarning < $this->shadowMin){
952 $message[]= _("Value specified as 'shadowWarning' should be greater than 'shadowMin'.");
953 }
954 }
955 if ($this->use_shadowInactive){
956 if (!is_id($this->shadowInactive)){
957 $message[]= _("Value specified as 'shadowInactive' is not valid.");
958 }
959 if (!$this->use_shadowMax){
960 $message[]= _("'shadowInactive' without 'shadowMax' makes no sense.");
961 }
962 }
963 if ($this->use_shadowMin && $this->use_shadowMax){
964 if ($this->shadowMin > $this->shadowMax){
965 $message[]= _("Value specified as 'shadowMin' should be smaller than 'shadowMax'.");
966 }
967 }
969 // if(empty($this->gosaDefaultPrinter)){
970 // $message[]= _("You need to specify a valid default printer.");
971 // }
973 return ($message);
974 }
976 function addGroup ($groups)
977 {
978 /* include global link_info */
979 $ldap= $this->config->get_ldap_link();
981 /* Walk through groups and add the descriptive entry if not exists */
982 foreach ($groups as $value){
983 if (!array_key_exists($value, $this->groupMembership)){
984 $ldap->cat($value);
985 $attrs= $ldap->fetch();
986 error_reporting (0);
987 if (!isset($attrs['description'][0])){
988 $entry= $attrs["cn"][0];
989 } else {
990 $dsc= preg_replace ('/^Group of user/', _("Group of user"), $attrs["description"][0]);
991 $entry= $attrs["cn"][0]." [$dsc]";
992 }
993 error_reporting (E_ALL);
994 $this->groupMembership[$ldap->getDN()]= $entry;
995 }
996 }
998 /* Sort groups */
999 asort ($this->groupMembership);
1000 reset ($this->groupMembership);
1001 }
1004 /* Del posix user from some groups */
1005 function delGroup ($groups)
1006 {
1007 $dest= array();
1009 foreach ($this->groupMembership as $key => $value){
1010 if (!in_array($key, $groups)){
1011 $dest[$key]= $value;
1012 }
1013 }
1014 $this->groupMembership= $dest;
1015 }
1017 /* Adapt from template, using 'dn' */
1018 function adapt_from_template($dn)
1019 {
1020 /* Include global link_info */
1021 $ldap= $this->config->get_ldap_link();
1023 plugin::adapt_from_template($dn);
1024 $template= $this->attrs['uid'][0];
1026 /* Adapt group membership */
1027 $ldap->cd($this->config->current['BASE']);
1028 $ldap->search("(&(objectClass=posixGroup)(memberUid=".$this->attrs["uid"][0]."))", array("description", "cn"));
1030 while ($this->attrs= $ldap->fetch()){
1031 if (!isset($this->attrs["description"][0])){
1032 $entry= $this->attrs["cn"][0];
1033 } else {
1034 $entry= $this->attrs["cn"][0]." [".$this->attrs["description"][0]."]";
1035 }
1036 $this->groupMembership[$ldap->getDN()]= $entry;
1037 }
1039 /* Fix primary group settings */
1040 $ldap->cd($this->config->current['BASE']);
1041 $ldap->search("(&(objectClass=posixGroup)(cn=$template)(gidNumber=".$this->gidNumber."))", array("cn"));
1042 if ($ldap->count() != 1){
1043 $this->primaryGroup= $this->gidNumber;
1044 }
1046 $ldap->cd($this->config->current['BASE']);
1047 $ldap->search("(&(objectClass=gosaUserTemplate)(uid=".$template."))", array("cn","accessTo"));
1048 while($attr = $ldap->fetch()){
1049 $tmp = $attr['accessTo'];
1050 unset ($tmp['count']);
1051 $this->accessTo = $tmp;
1052 }
1054 /* Adjust shadow checkboxes */
1055 foreach (array("shadowMin", "shadowMax", "shadowWarning", "shadowInactive",
1056 "shadowExpire") as $val){
1058 if ($this->$val != 0){
1059 $oval= "use_".$val;
1060 $this->$oval= "1";
1061 }
1062 }
1063 }
1065 function get_next_id($attrib)
1066 {
1067 $ids= array();
1068 $ldap= $this->config->get_ldap_link();
1070 $ldap->cd ($this->config->current['BASE']);
1071 if (preg_match('/gidNumber/i', $attrib)){
1072 $oc= "posixGroup";
1073 } else {
1074 $oc= "posixAccount";
1075 }
1076 $ldap->search ("(&(objectClass=$oc)($attrib=*))", array("$attrib"));
1078 /* Get list of ids */
1079 while ($attrs= $ldap->fetch()){
1080 $ids[]= (int)$attrs["$attrib"][0];
1081 }
1083 /* Find out next free id near to UID_BASE */
1084 for ($id= $this->config->current['UIDBASE']; $id++; $id<65000){
1085 if (!in_array($id, $ids)){
1086 return ($id);
1087 }
1088 }
1090 /* Should not happen */
1091 if ($id == 65000){
1092 print_red(_("Too many users, can't allocate a free ID!"));
1093 exit;
1094 }
1096 }
1098 function reload()
1099 {
1101 /* Get config */
1102 $groupfilter= get_global('groupfilter');
1104 /* Set base for all searches */
1105 $base= $_SESSION['CurrentMainBase'];
1107 /* Regex filter? */
1108 if ($groupfilter['regex'] != ""){
1109 $regex= $groupfilter['regex'];
1110 } else {
1111 $regex= "*";
1112 }
1114 $error = "";
1115 $ldap = $this->config->get_ldap_link();
1117 $base= get_groups_ou().$base;
1118 $res= get_list("(objectClass=posixGroup)", $this->ui->subtreeACL, $base,
1119 array("cn", "description", "gidNumber"), GL_SIZELIMIT);
1120 if (preg_match("/size limit/i", $error)){
1121 $_SESSION['limit_exceeded']= TRUE;
1122 }
1124 $error = $ldap->error;
1125 $this->grouplist = array();
1126 foreach ($res as $value){
1127 $this->grouplist[$value['gidNumber'][0]]= $value;
1128 }
1130 $tmp=array();
1131 foreach($this->grouplist as $tkey => $val ){
1132 $tmp[strtolower($val['cn'][0]).$val['cn'][0]]=$val;
1133 }
1135 /* Sort index */
1136 ksort($tmp);
1138 /* Recreate index array[dn]=cn[description]*/
1139 $this->grouplist=array();
1140 foreach($tmp as $val){
1141 if(isset($val['description'])){
1142 $this->grouplist[$val['dn']]=$val['cn'][0]." [".$val['description'][0]."]";
1143 }else{
1144 $this->grouplist[$val['dn']]=$val['cn'][0];
1145 }
1146 }
1147 reset ($this->grouplist);
1148 }
1151 /* Create the posix dialog part for copy & paste */
1152 function getCopyDialog()
1153 {
1154 /* Skip dialog creation if this is not a valid account*/
1155 if(!$this->is_account) return("");
1156 if ($this->force_ids == 1){
1157 $force_ids = "checked";
1158 if ($_SESSION['js']){
1159 $forceMode = "";
1160 }
1161 } else {
1162 if ($_SESSION['js']){
1163 if($this->acl != "#none#")
1164 $forceMode ="disabled";
1165 }
1166 $force_ids = "";
1167 }
1169 $sta = "";
1171 /* Open group add dialog */
1172 if(isset($_POST['edit_groupmembership'])){
1173 $this->group_dialog = TRUE;
1174 $sta = "SubDialog";
1175 }
1177 /* If the group-add dialog is closed, call execute
1178 to ensure that the membership is updatd */
1179 if(isset($_POST['add_groups_finish']) || isset($_POST['add_groups_cancel'])){
1180 $this->execute();
1181 $this->group_dialog =FALSE;
1182 }
1184 if($this->group_dialog){
1185 $str = $this->execute(true);
1186 $ret = array();
1187 $ret['string'] = $str;
1188 $ret['status'] = $sta;
1189 return($ret);
1190 }
1192 /* If a group member should be deleted, simply call execute */
1193 if(isset($_POST['delete_groupmembership'])){
1194 $this->execute();
1195 }
1197 /* Assigned informations to smarty */
1198 $smarty = get_smarty();
1199 $smarty->assign("homeDirectory",$this->homeDirectory);
1200 $smarty->assign("uidNumber",$this->uidNumber);
1201 $smarty->assign("gidNumber",$this->gidNumber);
1202 $smarty->assign("forceMode",$forceMode);
1203 $smarty->assign("force_ids",$force_ids);
1204 if (!count($this->groupMembership)){
1205 $smarty->assign("groupMembership", array(" "));
1206 } else {
1207 $smarty->assign("groupMembership", $this->groupMembership);
1208 }
1210 /* Display wars message if there are more than 16 group members */
1211 if (count($this->groupMembership) > 16){
1212 $smarty->assign("groups", "too_many_for_nfs");
1213 } else {
1214 $smarty->assign("groups", "");
1215 }
1216 $str = $smarty->fetch(get_template_path("paste_generic.tpl",TRUE,dirname(__FILE__)));
1218 $ret = array();
1219 $ret['string'] = $str;
1220 $ret['status'] = $sta;
1221 return($ret);
1222 }
1224 }
1226 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
1227 ?>