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 var $GroupRegex = "*";
66 var $GroupUserRegex = "*";
67 var $SubSearch = false;
69 /* attribute list for save action */
70 var $CopyPasteVars = array("grouplist","groupMembership","use_shadowMin","use_shadowMax","use_shadowWarning","use_shadowInactive","use_shadowExpire","must_change_password","force_ids","printerList","grouplist","savedGidNumber","savedUidNumber","savedGroupMembership");
71 var $attributes = array("homeDirectory", "loginShell", "uidNumber", "gidNumber", "gecos",
72 "shadowMin", "shadowMax", "shadowWarning", "shadowInactive", "shadowLastChange",
73 "shadowExpire", "gosaDefaultPrinter", "gosaDefaultLanguage", "uid","accessTo","trustModel");
74 var $objectclasses= array("posixAccount", "shadowAccount");
76 var $uid ="";
78 /* constructor, if 'dn' is set, the node loads the given
79 'dn' from LDAP */
80 function posixAccount ($config, $dn= NULL, $parent= NULL)
81 {
82 /* Configuration is fine, allways */
83 $this->config= $config;
85 /* Load bases attributes */
86 plugin::plugin($config, $dn, $parent);
88 /* set user id */
89 if(isset($this->attrs['uid'])){
90 $this->uid = $this->attrs['uid'][0];
91 }
93 $ldap= $this->config->get_ldap_link();
95 if ($dn != NULL){
97 /* Correct is_account. shadowAccount is not required. */
98 if (isset($this->attrs['objectClass']) &&
99 in_array ('posixAccount', $this->attrs['objectClass'])){
101 $this->is_account= TRUE;
102 }
104 /* Is this account a trustAccount? */
105 if ($this->is_account && isset($this->attrs['trustModel'])){
106 $this->trustModel= $this->attrs['trustModel'][0];
107 $this->was_trust_account= TRUE;
108 } else {
109 $this->was_trust_account= FALSE;
110 $this->trustModel= "";
111 }
113 $this->accessTo = array();
114 if ($this->is_account && isset($this->attrs['accessTo'])){
115 for ($i= 0; $i<$this->attrs['accessTo']['count']; $i++){
116 $tmp= $this->attrs['accessTo'][$i];
117 $this->accessTo[$tmp]= $tmp;
118 }
119 }
120 $this->initially_was_account= $this->is_account;
122 /* Fill group */
123 $this->primaryGroup= $this->gidNumber;
125 /* Generate status text */
126 $current= date("U");
128 $current= floor($current / 60 /60 / 24);
130 if (($current >= $this->shadowExpire) && $this->shadowExpire){
131 $this->status= _("expired");
132 if (($current - $this->shadowExpire) < $this->shadowInactive){
133 $this->status.= ", "._("grace time active");
134 }
135 } elseif (($this->shadowLastChange + $this->shadowMin) >= $current){
136 $this->status= _("active, password not changable");
137 } elseif (($this->shadowLastChange + $this->shadowMax) >= $current){
138 $this->status= _("active, password expired");
139 } else {
140 $this->status= _("active");
141 }
143 /* Get group membership */
144 $ldap->cd($this->config->current['BASE']);
145 $ldap->search("(&(objectClass=posixGroup)(memberUid=".$this->uid."))", array("cn", "description"));
147 while ($this->attrs= $ldap->fetch()){
148 if (!isset($this->attrs["description"][0])){
149 $entry= $this->attrs["cn"][0];
150 } else {
151 $dsc= preg_replace ('/^Group of user/', _("Group of user"), $this->attrs["description"][0]);
152 $entry= $this->attrs["cn"][0]." [$dsc]";
153 }
154 $this->groupMembership[$ldap->getDN()]= $entry;
155 }
156 asort($this->groupMembership);
157 reset($this->groupMembership);
158 $this->savedGroupMembership= $this->groupMembership;
159 $this->savedUidNumber= $this->uidNumber;
160 $this->savedGidNumber= $this->gidNumber;
161 }
163 /* Adjust shadow checkboxes */
164 foreach (array("shadowMin", "shadowMax", "shadowWarning", "shadowInactive",
165 "shadowExpire") as $val){
167 if ($this->$val != 0){
168 $oval= "use_".$val;
169 $this->$oval= "1";
170 }
171 }
173 /* Convert to seconds */
174 $this->shadowExpire= $this->convertToSeconds($this->shadowExpire);
176 /* Generate shell list from /etc/gosa/shells */
177 if (file_exists('/etc/gosa/shells')){
178 $shells = file ('/etc/gosa/shells');
179 foreach ($shells as $line){
180 if (!preg_match ("/^#/", $line)){
181 $this->loginShellList[]= trim($line);
182 }
183 }
184 } else {
185 if ($this->loginShell == ""){
186 $this->loginShellList[]= _("unconfigured");
187 }
188 }
190 /* Insert possibly missing loginShell */
191 if ($this->loginShell != "" && !in_array($this->loginShell, $this->loginShellList)){
192 $this->loginShellList[]= $this->loginShell;
193 }
195 /* Generate printer list */
196 if (isset($this->config->data['SERVERS']['CUPS'])){
197 $this->printerList= get_printer_list ($this->config->data['SERVERS']['CUPS']);
198 asort($this->printerList);
199 }
201 /* Set tag attribute if we've tagging activated */
202 $filter= "(objectClass=posixGroup)";
203 $ui= get_userinfo();
204 if ($ui->gosaUnitTag != "" && isset($config->current['STRICT_UNITS']) &&
205 preg_match('/TRUE/i', $config->current['STRICT_UNITS'])){
206 $filter= "(&(objectClass=posixGroup)(gosaUnitTag=".$ui->gosaUnitTag."))";
207 }
209 /* Generate group list */
210 $ldap->cd($this->config->current['BASE']);
211 $ldap->search("$filter", array("cn", "gidNumber"));
212 $this->secondaryGroups[]= "- "._("automatic")." -";
213 while ($attrs= $ldap->fetch()){
214 $this->secondaryGroups[$attrs['gidNumber'][0]]= $attrs['cn'][0];
215 }
216 asort ($this->secondaryGroups);
218 /* Get global filter config */
219 if (!is_global("sysfilter")){
220 $ui= get_userinfo();
221 $base= get_base_from_people($ui->dn);
222 $sysfilter= array( "depselect" => $base,
223 "regex" => "*");
224 register_global("sysfilter", $sysfilter);
225 }
226 $this->ui = get_userinfo();
227 }
230 /* execute generates the html output for this node */
231 function execute($isCopyPaste = false)
232 {
233 /* Call parent execute */
234 plugin::execute();
235 $display= "";
237 /* Department has changed? */
238 if(isset($_POST['depselect'])){
239 $_SESSION['CurrentMainBase']= validate($_POST['depselect']);
240 }
242 if(!$isCopyPaste){
243 /* Do we need to flip is_account state? */
244 if (isset($_POST['modify_state'])){
245 $this->is_account= !$this->is_account;
246 }
248 /* Do we represent a valid posixAccount? */
249 if (!$this->is_account && $this->parent == NULL ){
250 $display= "<img alt=\"\" src=\"images/stop.png\" align=\"middle\"> <b>".
251 _("This account has no unix extensions.")."</b>";
252 $display.= back_to_main();
253 return ($display);
254 }
257 /* Show tab dialog headers */
258 if ($this->parent != NULL){
259 if ($this->is_account){
260 if (isset($this->parent->by_object['sambaAccount'])){
261 $obj= $this->parent->by_object['sambaAccount'];
262 }
263 if (isset($obj) && $obj->is_account == TRUE &&
264 ((isset($this->parent->by_object['sambaAccount']))&&($this->parent->by_object['sambaAccount']->is_account))
265 ||(isset($this->parent->by_object['environment'] ))&&($this->parent->by_object['environment'] ->is_account)){
267 /* Samba3 dependency on posix accounts are enabled
268 in the moment, because I need to rely on unique
269 uidNumbers. There'll be a better solution later
270 on. */
271 $display= $this->show_header(_("Remove posix account"),
272 _("This account has unix features enabled. To disable them, you'll need to remove the samba / environment account first."), TRUE);
273 } else {
274 $display= $this->show_header(_("Remove posix account"),
275 _("This account has posix features enabled. You can disable them by clicking below."));
276 }
277 } else {
278 $display= $this->show_header(_("Create posix account"),
279 _("This account has posix features disabled. You can enable them by clicking below."));
280 return($display);
281 }
282 }
283 }
284 /* Trigger group edit? */
285 if (isset($_POST['edit_groupmembership'])){
286 $this->group_dialog= TRUE;
287 $this->dialog= TRUE;
288 }
290 /* Cancel group edit? */
291 if (isset($_POST['add_groups_cancel']) ||
292 isset($_POST['add_groups_finish'])){
293 $this->group_dialog= FALSE;
294 $this->dialog= FALSE;
295 }
297 /* Add selected groups */
298 if (isset($_POST['add_groups_finish']) && isset($_POST['groups']) &&
299 count($_POST['groups'])){
301 if (chkacl ($this->acl, "memberUid") == ""){
302 $this->addGroup ($_POST['groups']);
303 $this->is_modified= TRUE;
304 }
305 }
307 /* Delete selected groups */
308 if (isset($_POST['delete_groupmembership']) &&
309 isset($_POST['group_list']) && count($_POST['group_list'])){
311 if (chkacl ($this->acl, "memberUid") == ""){
312 $this->delGroup ($_POST['group_list']);
313 $this->is_modified= TRUE;
314 }
315 }
317 /* Add user workstation? */
318 if (isset($_POST["add_ws"])){
319 $this->show_ws_dialog= TRUE;
320 $this->dialog= TRUE;
321 }
323 /* Add user workstation? */
324 if (isset($_POST["add_ws_finish"]) && isset($_POST['wslist'])){
325 foreach($_POST['wslist'] as $ws){
326 $this->accessTo[$ws]= $ws;
327 }
328 ksort($this->accessTo);
329 $this->is_modified= TRUE;
330 }
332 /* Remove user workstations? */
333 if (isset($_POST["delete_ws"]) && isset($_POST['workstation_list'])){
334 foreach($_POST['workstation_list'] as $name){
335 unset ($this->accessTo[$name]);
336 }
337 $this->is_modified= TRUE;
338 }
340 /* Add user workstation finished? */
341 if (isset($_POST["add_ws_finish"]) || isset($_POST["add_ws_cancel"])){
342 $this->show_ws_dialog= FALSE;
343 $this->dialog= FALSE;
344 }
346 /* Templates now! */
347 $smarty= get_smarty();
349 /* Show ws dialog */
350 if ($this->show_ws_dialog){
351 /* Save data */
352 $sysfilter= get_global("sysfilter");
353 foreach( array("depselect", "regex") as $type){
354 if (isset($_POST[$type])){
355 $sysfilter[$type]= $_POST[$type];
356 }
357 }
358 if (isset($_GET['search'])){
359 $s= mb_substr($_GET['search'], 0, 1, "UTF8")."*";
360 if ($s == "**"){
361 $s= "*";
362 }
363 $sysfilter['regex']= $s;
364 }
365 register_global("sysfilter", $sysfilter);
367 /* Get workstation list */
368 $exclude= "";
369 foreach($this->accessTo as $ws){
370 $exclude.= "(cn=$ws)";
371 }
372 if ($exclude != ""){
373 $exclude= "(!(|$exclude))";
374 }
375 $acl= array($this->config->current['BASE'] => ":all");
376 $regex= $sysfilter['regex'];
377 $filter= "(&(|(objectClass=goServer)(objectClass=gotoWorkstation)(objectClass=gotoTerminal))$exclude(cn=*)(cn=$regex))";
378 $res= get_list($filter, $acl, $sysfilter['depselect'], array("cn"), GL_SUBSEARCH | GL_SIZELIMIT);
379 $wslist= array();
380 foreach ($res as $attrs){
381 $wslist[]= preg_replace('/\$/', '', $attrs['cn'][0]);
382 }
383 asort($wslist);
384 $smarty->assign("search_image", get_template_path('images/search.png'));
385 $smarty->assign("launchimage", get_template_path('images/small_filter.png'));
386 $smarty->assign("tree_image", get_template_path('images/tree.png'));
387 $smarty->assign("deplist", $this->config->idepartments);
388 $smarty->assign("alphabet", generate_alphabet());
389 foreach( array("depselect", "regex") as $type){
390 $smarty->assign("$type", $sysfilter[$type]);
391 }
392 $smarty->assign("hint", print_sizelimit_warning());
393 $smarty->assign("wslist", $wslist);
394 $smarty->assign("apply", apply_filter());
395 $display= $smarty->fetch (get_template_path('trust_machines.tpl', TRUE, dirname(__FILE__)));
396 return ($display);
397 }
399 /* Manage group add dialog */
400 if ($this->group_dialog){
402 /* Get global filter config */
403 $this->reload();
405 /* remove already assigned groups */
406 $glist= array();
407 foreach ($this->grouplist as $key => $value){
408 if (!isset($this->groupMembership[$key])){
409 $glist[$key]= $value;
410 }
411 }
413 if($this->SubSearch){
414 $smarty->assign("SubSearchCHK"," checked ");
415 }else{
416 $smarty->assign("SubSearchCHK","");
417 }
419 $smarty->assign("regex",$this->GroupRegex);
420 $smarty->assign("guser",$this->GroupUserRegex);
421 $smarty->assign("groups", $glist);
422 $smarty->assign("search_image", get_template_path('images/search.png'));
423 $smarty->assign("launchimage", get_template_path('images/small_filter.png'));
424 $smarty->assign("tree_image", get_template_path('images/tree.png'));
425 $smarty->assign("deplist", $this->config->idepartments);
426 $smarty->assign("alphabet", generate_alphabet());
427 $smarty->assign("depselect",$_SESSION['CurrentMainBase']);
428 $smarty->assign("hint", print_sizelimit_warning());
430 $smarty->assign("apply", apply_filter());
431 $display.= $smarty->fetch (get_template_path('posix_groups.tpl', TRUE, dirname(__FILE__)));
432 return ($display);
433 }
435 /* Show main page */
436 $smarty= get_smarty();
438 /* Depending on pwmode, currently hardcoded because there are no other methods */
439 if ( 1 == 1 ){
440 $smarty->assign("pwmode", dirname(__FILE__)."/posix_shadow");
441 $shadowMinACL= chkacl($this->acl, "shadowMin");
442 $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."\">"));
443 $shadowMaxACL= chkacl($this->acl, "shadowMax");
444 $smarty->assign("shadowmaxs", sprintf(_("Password must be changed after %s days"), "<input name=\"shadowMax\" size=3 maxlength=4 $shadowMaxACL value=\"".$this->shadowMax."\">"));
445 $shadowInactiveACL= chkacl($this->acl, "shadowInactive");
446 $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."\">"));
447 $shadowWarningACL= chkacl($this->acl, "shadowWarning");
448 $smarty->assign("shadowwarnings", sprintf(_("Warn user %s days before password expiery"), "<input name=\"shadowWarning\" size=3 maxlength=4 $shadowWarningACL value=\"".$this->shadowWarning."\">"));
449 foreach( array("must_change_password", "use_shadowMin", "use_shadowMax",
450 "use_shadowExpire", "use_shadowInactive",
451 "use_shadowWarning") as $val){
452 if ($this->$val == 1){
453 $smarty->assign("$val", "checked");
454 } else {
455 $smarty->assign("$val", "");
456 }
457 $smarty->assign("$val"."ACL", chkacl($this->acl, $val));
458 }
459 }
461 /* Fill calendar */
462 $date= getdate($this->shadowExpire);
464 $days= array();
465 for($d= 1; $d<32; $d++){
466 $days[$d]= $d;
467 }
468 $years= array();
469 for($y= $date['year']-10; $y<$date['year']+10; $y++){
470 $years[]= $y;
471 }
472 $months= array(_("January"), _("February"), _("March"), _("April"),
473 _("May"), _("June"), _("July"), _("August"), _("September"),
474 _("October"), _("November"), _("December"));
475 $smarty->assign("day", $date["mday"]);
476 $smarty->assign("days", $days);
477 $smarty->assign("months", $months);
478 $smarty->assign("month", $date["mon"]-1);
479 $smarty->assign("years", $years);
480 $smarty->assign("year", $date["year"]);
482 /* Fill arrays */
483 $smarty->assign("shells", $this->loginShellList);
484 $smarty->assign("secondaryGroups", $this->secondaryGroups);
485 $smarty->assign("primaryGroup", $this->primaryGroup);
486 if (!count($this->groupMembership)){
487 $smarty->assign("groupMembership", array(" "));
488 } else {
489 $smarty->assign("groupMembership", $this->groupMembership);
490 }
491 if (count($this->groupMembership) > 16){
492 $smarty->assign("groups", "too_many_for_nfs");
493 } else {
494 $smarty->assign("groups", "");
495 }
496 $smarty->assign("printerList", $this->printerList);
497 $smarty->assign("languages", $this->config->data['MAIN']['LANGUAGES']);
499 /* Avoid "Undefined index: forceMode" */
500 $smarty->assign("forceMode", "");
502 /* Checkboxes */
503 if ($this->force_ids == 1){
504 $smarty->assign("force_ids", "checked");
505 if ($_SESSION['js']){
506 $smarty->assign("forceMode", "");
507 }
508 } else {
509 if ($_SESSION['js']){
510 if($this->acl != "#none#")
511 $smarty->assign("forceMode", "disabled");
512 }
513 $smarty->assign("force_ids", "");
514 }
515 $smarty->assign("force_idsACL", chkacl($this->acl, "force_ids"));
517 /* Load attributes and acl's */
518 foreach($this->attributes as $val){
519 if((chkacl($this->acl,$val)=="")&&(($_SESSION["js"])&&(($val=="uidNumber")||($val=="gidNumber"))))
520 {
521 $smarty->assign("$val"."ACL","");
522 $smarty->assign("$val", $this->$val);
523 continue;
524 }
525 $smarty->assign("$val", $this->$val);
526 $smarty->assign("$val"."ACL", chkacl($this->acl,$val));
527 }
528 $smarty->assign("groupMembershipACL", chkacl($this->acl, "groupMembership"));
529 $smarty->assign("status", $this->status);
531 /* Work on trust modes */
532 $smarty->assign("trustmodeACL", chkacl($this->acl, "trustmode"));
533 if ($this->trustModel == "fullaccess"){
534 $trustmode= 1;
535 // pervent double disable tag in html code, this will disturb our clean w3c html
537 if(chkacl($this->acl, "trustmode")==""){
538 $smarty->assign("trusthide", "disabled");
539 }else{
540 $smarty->assign("trusthide", "");
541 }
543 } elseif ($this->trustModel == "byhost"){
544 $trustmode= 2;
545 $smarty->assign("trusthide", "");
546 } else {
547 // pervent double disable tag in html code, this will disturb our clean w3c html
548 if(chkacl($this->acl, "trustmode")==""){
549 $smarty->assign("trusthide", "disabled");
550 }else{
551 $smarty->assign("trusthide", "");
552 }
553 $trustmode= 0;
554 }
555 $smarty->assign("trustmode", $trustmode);
556 $smarty->assign("trustmodes", array( 0 => _("disabled"), 1 => _("full access"),
557 2 => _("allow access to these hosts")));
561 if((count($this->accessTo))==0)
562 $smarty->assign("emptyArrAccess",true);
563 else
564 $smarty->assign("emptyArrAccess",false);
568 $smarty->assign("workstations", $this->accessTo);
570 $smarty->assign("apply", apply_filter());
571 $display.= $smarty->fetch (get_template_path('generic.tpl', TRUE, dirname(__FILE__)));
572 return($display);
573 }
576 /* remove object from parent */
577 function remove_from_parent()
578 {
579 /* Cancel if there's nothing to do here */
580 if (!$this->initially_was_account){
581 return;
582 }
584 /* include global link_info */
585 $ldap= $this->config->get_ldap_link();
587 /* Remove and write to LDAP */
588 plugin::remove_from_parent();
590 /* Zero out array */
591 $this->attrs['gosaHostACL']= array();
593 /* Keep uid, because we need it for authentification! */
594 unset($this->attrs['uid']);
595 unset($this->attrs['trustModel']);
597 @DEBUG (DEBUG_LDAP, __LINE__, __FUNCTION__, __FILE__,
598 $this->attributes, "Save");
599 $ldap->cd($this->dn);
600 $this->cleanup();
601 $ldap->modify ($this->attrs);
603 show_ldap_error($ldap->get_error(), _("Removing UNIX account failed"));
605 /* Delete group only if cn is uid and there are no other
606 members inside */
607 $ldap->cd ($this->config->current['BASE']);
608 $ldap->search ("(&(objectClass=posixGroup)(gidNumber=".$this->gidNumber."))", array("cn", "memberUid"));
609 if ($ldap->count() != 0){
610 $attrs= $ldap->fetch();
611 if ($attrs['cn'][0] == $this->uid &&
612 !isset($this->attrs['memberUid'])){
614 $ldap->rmDir($ldap->getDN());
615 }
616 }
618 /* Optionally execute a command after we're done */
619 $this->handle_post_events("remove", array("uid" => $this->uid));
620 }
623 function save_object()
624 {
625 if (isset($_POST['posixTab'])){
626 /* Save values to object */
627 plugin::save_object();
629 /* Save force GID attribute */
630 if (chkacl ($this->acl, "force_ids") == ""){
631 if (isset ($_POST['force_ids'])){
632 $data= 1;
633 } else {
634 $data= 0;
635 }
636 if ($this->force_ids != $data){
637 $this->is_modified= TRUE;
638 }
639 $this->force_ids= $data;
641 $data= $_POST['primaryGroup'];
642 if ($this->primaryGroup != $data){
643 $this->is_modified= TRUE;
644 }
645 $this->primaryGroup= $_POST['primaryGroup'];
646 }
648 /* Save pwmode dependent attributes, curently hardcoded because there're
649 no alternatives */
650 if (1 == 1){
651 foreach( array("must_change_password", "use_shadowMin", "use_shadowMax",
652 "use_shadowExpire", "use_shadowInactive",
653 "use_shadowWarning") as $val){
654 if (chkacl($this->acl, "$val") == ""){
655 if (isset ($_POST[$val])){
656 $data= 1;
657 } else {
658 $data= 0;
659 }
660 if ($data != $this->$val){
661 $this->is_modified= TRUE;
662 }
663 $this->$val= $data;
664 }
665 }
666 }
668 /* Trust mode - special handling */
669 if (isset($_POST['trustmode'])){
670 $saved= $this->trustModel;
671 if ($_POST['trustmode'] == "1"){
672 $this->trustModel= "fullaccess";
673 } elseif ($_POST['trustmode'] == "2"){
674 $this->trustModel= "byhost";
675 } else {
676 $this->trustModel= "";
677 }
678 if ($this->trustModel != $saved){
679 $this->is_modified= TRUE;
680 }
681 }
682 }
684 /* Get regex from alphabet */
685 if(isset($_GET['search'])){
686 $this->GroupRegex = $_GET['search']."*";
687 }
689 /* Check checkboxes and regexes */
690 if(isset($_POST["PosixGroupDialogPosted"])){
691 if(isset($_POST['SubSearch']) && ($_POST['SubSearch'])){
692 $this->SubSearch = true;
693 }else{
694 $this->SubSearch = false;
695 }
697 if(isset($_POST['guser'])){
698 $this->GroupUserRegex = $_POST['guser'];
699 }
700 if(isset($_POST['regex'])){
701 $this->GroupRegex = $_POST['regex'];
702 }
703 }
704 $this->GroupRegex = preg_replace("/\*\**/","*",$this->GroupRegex);
705 $this->GroupUserRegex = preg_replace("/\*\**/","*",$this->GroupUserRegex);
706 }
709 /* Save data to LDAP, depending on is_account we save or delete */
710 function save()
711 {
713 /* include global link_info */
714 $ldap= $this->config->get_ldap_link();
716 /* Adapt shadow values */
717 if (!$this->use_shadowExpire){
718 $this->shadowExpire= "0";
719 } else {
720 /* Transform seconds to days here */
721 $this->shadowExpire= (int)($this->shadowExpire / (60 * 60 * 24)) ;
722 }
723 if (!$this->use_shadowMax){
724 $this->shadowMax= "0";
725 }
726 if ($this->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(), _("Saving UNIX account failed"));
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 if (!isset($this->savedGroupMembership[$key])){
868 $g= new grouptabs($this->config,$this->config->data['TABS']['GROUPTABS'], $key, false);
869 $g->by_object['group']->addUser($this->uid);
870 $g->save();
871 }
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("modify",array("uid" => $this->uid));
887 }
888 } else {
889 $this->handle_post_events("add",array("uid" , $this->uid));
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, array('cn', 'description', 'dn'));
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[$attrs['dn']]= $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.")(accessTo=*))", array("cn","accessTo"));
1049 while($attr = $ldap->fetch()){
1050 $tmp = $attr['accessTo'];
1051 unset ($tmp['count']);
1052 $this->accessTo = $tmp;
1053 }
1055 /* Adjust shadow checkboxes */
1056 foreach (array("shadowMin", "shadowMax", "shadowWarning", "shadowInactive",
1057 "shadowExpire") as $val){
1059 if ($this->$val != 0){
1060 $oval= "use_".$val;
1061 $this->$oval= "1";
1062 }
1063 }
1065 $this->shadowExpire= $this->convertToSeconds($this->shadowExpire);
1066 }
1068 function get_next_id($attrib)
1069 {
1070 $ids= array();
1071 $ldap= $this->config->get_ldap_link();
1073 $ldap->cd ($this->config->current['BASE']);
1074 if (preg_match('/gidNumber/i', $attrib)){
1075 $oc= "posixGroup";
1076 } else {
1077 $oc= "posixAccount";
1078 }
1079 $ldap->search ("(&(objectClass=$oc)($attrib=*))", array("$attrib"));
1081 /* Get list of ids */
1082 while ($attrs= $ldap->fetch()){
1083 $ids[]= (int)$attrs["$attrib"][0];
1084 }
1086 /* Find out next free id near to UID_BASE */
1087 for ($id= $this->config->current['UIDBASE']; $id++; $id<65000){
1088 if (!in_array($id, $ids)){
1089 return ($id);
1090 }
1091 }
1093 /* Should not happen */
1094 if ($id == 65000){
1095 print_red(_("Too many users, can't allocate a free ID!"));
1096 exit;
1097 }
1099 }
1101 function reload()
1102 {
1104 /* Set base for all searches */
1105 $base = $_SESSION['CurrentMainBase'];
1106 $base = $base;
1107 $ldap = $this->config->get_ldap_link();
1108 $attrs = array("cn", "description", "gidNumber");
1109 $Flags = GL_SIZELIMIT;
1111 /* Get groups */
1112 if ($this->GroupUserRegex == '*'){
1113 $filter = "(&(objectClass=posixGroup)(cn=".$this->GroupRegex."))";
1114 } else {
1115 $filter= "(&(objectClass=posixGroup)(cn=".$this->GroupRegex.")(memberUid=".$this->GroupUserRegex."))";
1116 }
1117 if($this->SubSearch){
1118 $Flags |= GL_SUBSEARCH;
1119 }else{
1120 $base = get_groups_ou().$base;
1121 }
1124 $res= get_list($filter, $this->ui->subtreeACL, $base,$attrs, $Flags);
1126 /* check sizelimit */
1127 if (preg_match("/size limit/i", $ldap->error)){
1128 $_SESSION['limit_exceeded']= TRUE;
1129 }
1131 /* Create a list of users */
1132 $this->grouplist = array();
1133 foreach ($res as $value){
1134 $this->grouplist[$value['gidNumber'][0]]= $value;
1135 }
1137 $tmp=array();
1138 foreach($this->grouplist as $tkey => $val ){
1139 $tmp[strtolower($val['cn'][0]).$val['cn'][0]]=$val;
1140 }
1142 /* Sort index */
1143 ksort($tmp);
1145 /* Recreate index array[dn]=cn[description]*/
1146 $this->grouplist=array();
1147 foreach($tmp as $val){
1148 if(isset($val['description'])){
1149 $this->grouplist[$val['dn']]=$val['cn'][0]." [".$val['description'][0]."]";
1150 }else{
1151 $this->grouplist[$val['dn']]=$val['cn'][0];
1152 }
1153 }
1154 reset ($this->grouplist);
1155 }
1158 /* Create the posix dialog part for copy & paste */
1159 function getCopyDialog()
1160 {
1161 /* Skip dialog creation if this is not a valid account*/
1162 if(!$this->is_account) return("");
1163 if ($this->force_ids == 1){
1164 $force_ids = "checked";
1165 if ($_SESSION['js']){
1166 $forceMode = "";
1167 }
1168 } else {
1169 if ($_SESSION['js']){
1170 if($this->acl != "#none#")
1171 $forceMode ="disabled";
1172 }
1173 $force_ids = "";
1174 }
1176 $sta = "";
1178 /* Open group add dialog */
1179 if(isset($_POST['edit_groupmembership'])){
1180 $this->group_dialog = TRUE;
1181 $sta = "SubDialog";
1182 }
1184 /* If the group-add dialog is closed, call execute
1185 to ensure that the membership is updatd */
1186 if(isset($_POST['add_groups_finish']) || isset($_POST['add_groups_cancel'])){
1187 $this->execute();
1188 $this->group_dialog =FALSE;
1189 }
1191 if($this->group_dialog){
1192 $str = $this->execute(true);
1193 $ret = array();
1194 $ret['string'] = $str;
1195 $ret['status'] = $sta;
1196 return($ret);
1197 }
1199 /* If a group member should be deleted, simply call execute */
1200 if(isset($_POST['delete_groupmembership'])){
1201 $this->execute();
1202 }
1204 /* Assigned informations to smarty */
1205 $smarty = get_smarty();
1206 $smarty->assign("homeDirectory",$this->homeDirectory);
1207 $smarty->assign("uidNumber",$this->uidNumber);
1208 $smarty->assign("gidNumber",$this->gidNumber);
1209 $smarty->assign("forceMode",$forceMode);
1210 $smarty->assign("force_ids",$force_ids);
1211 if (!count($this->groupMembership)){
1212 $smarty->assign("groupMembership", array(" "));
1213 } else {
1214 $smarty->assign("groupMembership", $this->groupMembership);
1215 }
1217 /* Display wars message if there are more than 16 group members */
1218 if (count($this->groupMembership) > 16){
1219 $smarty->assign("groups", "too_many_for_nfs");
1220 } else {
1221 $smarty->assign("groups", "");
1222 }
1223 $str = $smarty->fetch(get_template_path("paste_generic.tpl",TRUE,dirname(__FILE__)));
1225 $ret = array();
1226 $ret['string'] = $str;
1227 $ret['status'] = $sta;
1228 return($ret);
1229 }
1232 function convertToSeconds($val)
1233 {
1234 if ($val != 0){
1235 $val*= 60 * 60 * 24;
1236 } else {
1237 $date= getdate();
1238 $val= floor($date[0] / (60*60*24)) * 60 * 60 * 24;
1239 }
1240 }
1242 }
1244 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
1245 ?>