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