1 <?php
2 /*!
3 \brief user 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 person, organizationalPerson, inetOrgPerson and gosaAccount
10 from/to the LDAP. It does syntax checking and displays the formulars required.
11 */
13 class user extends plugin
14 {
15 /* Definitions */
16 var $plHeadline= "Generic";
17 var $plDescription= "This does something";
19 /* CLI vars */
20 var $cli_summary= "Handling of GOsa's user base object";
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 $base= "";
26 var $cn= "";
27 var $personalTitle= "";
28 var $academicTitle= "";
29 var $homePostalAddress= "";
30 var $homePhone= "";
31 var $labeledURI= "";
32 var $o= "";
33 var $ou= "";
34 var $departmentNumber= "";
35 var $employeeNumber= "";
36 var $employeeType= "";
37 var $roomNumber= "";
38 var $telephoneNumber= "";
39 var $facsimileTelephoneNumber= "";
40 var $mobile= "";
41 var $pager= "";
42 var $l= "";
43 var $st= "";
44 var $postalAddress= "";
45 var $dob= "0";
46 var $use_dob= "0";
47 var $gender= "0";
49 var $jpegPhoto= "*removed*";
50 var $photoData= "";
51 var $old_jpegPhoto= "";
52 var $old_photoData= "";
53 var $cert_dialog= FALSE;
54 var $picture_dialog= FALSE;
56 var $userPKCS12= "";
57 var $userSMIMECertificate= "";
58 var $userCertificate= "";
59 var $certificateSerialNumber= "";
60 var $old_certificateSerialNumber= "";
61 var $old_userPKCS12= "";
62 var $old_userSMIMECertificate= "";
63 var $old_userCertificate= "";
65 var $gouvernmentOrganizationalUnit= "";
66 var $houseIdentifier= "";
67 var $street= "";
68 var $postalCode= "";
69 var $vocation= "";
70 var $ivbbLastDeliveryCollective= "";
71 var $gouvernmentOrganizationalPersonLocality= "";
72 var $gouvernmentOrganizationalUnitDescription= "";
73 var $gouvernmentOrganizationalUnitSubjectArea= "";
74 var $functionalTitle= "";
75 var $role= "";
76 var $publicVisible= "";
78 /* variables to trigger password changes */
79 var $pw_storage= "crypt";
80 var $last_pw_storage= "unset";
81 var $had_userCertificate= FALSE;
83 /* attribute list for save action */
84 var $attributes= array("sn", "givenName", "uid", "personalTitle", "academicTitle",
85 "homePostalAddress", "homePhone", "labeledURI", "o", "ou", "dob", "gender",
86 "departmentNumber", "employeeNumber", "employeeType", "l", "st",
87 "roomNumber", "telephoneNumber", "mobile", "pager", "cn", "userPKCS12",
88 "postalAddress", "facsimileTelephoneNumber", "userSMIMECertificate");
90 var $objectclasses= array("top", "person", "organizationalPerson", "inetOrgPerson",
91 "gosaAccount");
93 /* attributes that are part of the government mode */
94 var $govattrs= array("gouvernmentOrganizationalUnit", "houseIdentifier", "vocation",
95 "ivbbLastDeliveryCollective", "gouvernmentOrganizationalPersonLocality",
96 "gouvernmentOrganizationalUnitDescription","gouvernmentOrganizationalUnitSubjectArea",
97 "functionalTitle", "certificateSerialNumber", "publicVisible", "street", "role",
98 "postalCode");
101 /* constructor, if 'dn' is set, the node loads the given
102 'dn' from LDAP */
103 function user ($config, $dn= NULL)
104 {
105 /* Configuration is fine, allways */
106 $this->config= $config;
108 /* Load base attributes */
109 plugin::plugin ($config, $dn);
111 /* Load government mode attributes */
112 if ($this->config->current['GOVERNMENTMODE']){
114 /* Copy all attributs */
115 foreach ($this->govattrs as $val){
116 if (isset($this->attrs["$val"][0])){
117 $this->$val= $this->attrs["$val"][0];
118 }
119 }
121 /* Fix public visible attribute if unset */
122 if (!isset($this->attrs['publicVisible'])){
123 $this->publicVisible == "nein";
124 }
126 }
128 /* Create me for new accounts */
129 if ($dn == "new"){
130 $this->is_account= TRUE;
131 }
133 /* Make hash default to md5 if not set in config */
134 if (!isset($this->config->current['HASH'])){
135 $hash= "md5";
136 } else {
137 $hash= $this->config->current['HASH'];
138 }
140 /* Load data from LDAP? */
141 if ($dn != NULL){
143 /* Do base conversation */
144 if ($this->dn == "new"){
145 $ui= get_userinfo();
146 $this->base= dn2base($ui->dn);
147 } else {
148 $this->base= dn2base($dn);
149 }
151 /* get password storage type */
152 if (isset ($this->attrs['userPassword'][0])){
153 /* Initialize local array */
154 $matches= array();
155 if (preg_match ("/^{([^}]+)}(.+)/", $this->attrs['userPassword'][0], $matches)){
156 $this->pw_storage= strtolower($matches[1]);
157 } else {
158 if ($this->attrs['userPassword'][0] != ""){
159 $this->pw_storage= "clear";
160 } else {
161 $this->pw_storage= $hash;
162 }
163 }
164 } else {
165 /* Preset with vaule from configuration */
166 $this->pw_storage= $hash;
167 }
169 /* Load extra attributes: certificate and picture */
170 $this->load_picture();
171 $this->load_cert();
172 if ($this->userCertificate != ""){
173 $this->had_userCertificate= TRUE;
174 }
175 }
177 /* Reset password storage indicator, used by password_change_needed() */
178 if ($dn == "new"){
179 $this->last_pw_storage= "unset";
180 } else {
181 $this->last_pw_storage= $this->pw_storage;
182 }
184 /* Generate dob entry */
185 if (isset ($this->attrs['dateOfBirth'])){
186 /* This entry is ISO 8601 conform */
187 list($year, $month, $day)= split("-", $this->attrs['dateOfBirth'][0], 3);
188 $this->dob= mktime ( 0, 0, 0, $month, $day, $year);
189 $this->use_dob= "1";
190 } else {
191 $this->use_dob= "0";
192 }
194 /* Put gender attribute to upper case */
195 if (isset ($this->attrs['gender'])){
196 $this->gender= strtoupper($this->attrs['gender'][0]);
197 }
198 }
201 /* execute generates the html output for this node */
202 function execute()
203 {
204 $smarty= get_smarty();
206 /* Fill calendar */
207 if ($this->dob == "0"){
208 $date= getdate();
209 } else {
210 $date= getdate($this->dob);
211 }
213 $days= array();
214 for($d= 1; $d<32; $d++){
215 $days[$d]= $d;
216 }
217 $years= array();
218 for($y= $date['year']-100; $y<=$date['year']+100; $y++){
219 $years[]= $y;
220 }
221 $years['-']= "- ";
222 $months= array(_("January"), _("February"), _("March"), _("April"),
223 _("May"), _("June"), _("July"), _("August"), _("September"),
224 _("October"), _("November"), _("December"), '-' => '- ');
225 $smarty->assign("day", $date["mday"]);
226 $smarty->assign("days", $days);
227 $smarty->assign("months", $months);
228 $smarty->assign("month", $date["mon"]-1);
229 $smarty->assign("years", $years);
230 $smarty->assign("year", $date["year"]);
232 /* Assign sex */
233 $sex= array(0 => " ", "F" => _("female"), "M" => _("male"));
234 $smarty->assign("gender_list", $sex);
236 /* Get random number for pictures */
237 srand((double)microtime()*1000000);
238 $smarty->assign("rand", rand(0, 10000));
240 /* Do we represent a valid gosaAccount? */
241 if (!$this->is_account){
242 echo "<img alt=\"\" src=\"images/stop.png\" align=\"middle\"> <b>".
243 _("This account has no valid GOsa extensions.")."</b>";
244 return;
245 }
247 /* Want picture edit dialog? */
248 if (isset($_POST['edit_picture'])){
249 /* Save values for later recovery, in case some presses
250 the cancel button. */
251 $this->old_jpegPhoto= $this->jpegPhoto;
252 $this->old_photoData= $this->photoData;
253 $this->picture_dialog= TRUE;
254 $this->dialog= TRUE;
255 }
257 /* Remove picture? */
258 if (isset($_POST['picture_remove'])){
259 $this->jpegPhoto= "*removed*";
260 $this->set_picture ("./images/default.jpg");
261 $this->is_modified= TRUE;
263 return($smarty->fetch (get_template_path('generic_picture.tpl', TRUE, dirname(__FILE__))));
264 }
266 /* Save picture */
267 if (isset($_POST['picture_edit_finish'])){
269 /* Check for clean upload */
270 if ($_FILES['picture_file']['name'] != ""){
271 if (!is_uploaded_file($_FILES['picture_file']['tmp_name'])) {
272 print_red(_("The specified file has not been uploaded via HTTP POST! Aborted."));
273 exit;
274 }
276 /* Activate new picture */
277 $this->set_picture($_FILES['picture_file']['tmp_name']);
278 }
279 $this->picture_dialog= FALSE;
280 $this->dialog= FALSE;
281 $this->is_modified= TRUE;
282 }
285 /* Cancel picture */
286 if (isset($_POST['picture_edit_cancel'])){
288 /* Restore values */
289 $this->jpegPhoto= $this->old_jpegPhoto;
290 $this->photoData= $this->old_photoData;
292 /* Update picture */
293 $_SESSION['picture']= $this->photoData;
294 $this->picture_dialog= FALSE;
295 $this->dialog= FALSE;
296 }
298 /* Toggle dob information */
299 if (isset($_POST['set_dob'])){
300 $this->use_dob= ($this->use_dob == "0")?"1":"0";
301 }
304 /* Want certificate= */
305 if (isset($_POST['edit_cert'])){
307 /* Save original values for later reconstruction */
308 foreach (array("certificateSerialNumber", "userCertificate",
309 "userSMIMECertificate", "userPKCS12") as $val){
311 $oval= "old_$val";
312 $this->$oval= $this->$val;
313 }
315 $this->cert_dialog= TRUE;
316 $this->dialog= TRUE;
317 }
320 /* Cancel certificate dialog */
321 if (isset($_POST['cert_edit_cancel'])){
323 /* Restore original values in case of 'cancel' */
324 foreach (array("certificateSerialNumber", "userCertificate",
325 "userSMIMECertificate", "userPKCS12") as $val){
327 $oval= "old_$val";
328 $this->$val= $this->$oval;
329 }
330 $this->cert_dialog= FALSE;
331 $this->dialog= FALSE;
332 }
335 /* Remove certificate? */
336 foreach (array ("userCertificate", "userSMIMECertificate", "userPKCS12") as $val){
337 if (isset($_POST["remove_$val"])){
339 /* Reset specified cert*/
340 $this->$val= "";
341 $this->is_modified= TRUE;
342 }
343 }
346 /* Upload new cert and close dialog? */
347 if (isset($_POST['cert_edit_finish'])){
349 /* for all certificates do */
350 foreach (array ("userCertificate", "userSMIMECertificate", "userPKCS12")
351 as $val){
353 /* Check for clean upload */
354 if (array_key_exists($val."_file", $_FILES) &&
355 array_key_exists('name', $_FILES[$val."_file"]) &&
356 $_FILES[$val."_file"]['name'] != "" &&
357 is_uploaded_file($_FILES[$val."_file"]['tmp_name'])) {
358 $this->set_cert("$val", $_FILES[$val."_file"]['tmp_name']);
359 }
360 }
362 /* Save serial number */
363 if (isset($_POST["certificateSerialNumber"]) &&
364 $_POST["certificateSerialNumber"] != ""){
366 if (!is_id($_POST["certificateSerialNumber"])){
367 print_red (_("Please enter a valid serial number"));
369 foreach(array("userCertificate", "userSMIMECertificate", "userPKCS12") as $cert){
370 if ($this->$cert != ""){
371 $smarty->assign("$cert"."_state", "true");
372 } else {
373 $smarty->assign("$cert"."_state", "");
374 }
375 }
376 return ($smarty->fetch (get_template_path('generic_certs.tpl', TRUE, dirname(__FILE__))));
377 }
379 $this->certificateSerialNumber= $_POST["certificateSerialNumber"];
380 $this->is_modified= TRUE;
381 }
383 $this->cert_dialog= FALSE;
384 $this->dialog= FALSE;
385 }
387 /* Display picture dialog */
388 if ($this->picture_dialog){
389 return($smarty->fetch (get_template_path('generic_picture.tpl', TRUE, dirname(__FILE__))));
390 }
392 /* Display cert dialog */
393 if ($this->cert_dialog){
394 foreach(array("userCertificate", "userSMIMECertificate", "userPKCS12") as $cert){
395 if ($this->$cert != ""){
396 /* import certificate */
397 $certificate = new certificate;
398 $certificate->import($this->$cert);
400 /* Read out data*/
401 $timeto = $certificate->getvalidto_date();
402 $timefrom = $certificate->getvalidfrom_date();
403 $str = "<table summary=\"\" border=0><tr><td style='vertical-align:top'>CN</td><td>".preg_replace("/ /", " ", $certificate->getname())."</td></tr></table><br>".
404 sprintf(_("Certificate is valid from <b>%s</b> to <b>%s</b> and is currently <b>%s</b>."), date('d M Y',$timefrom),date('d M Y',$timeto), $certificate->isvalid()?"<font style='color:green'>"._("valid")."</font>":"<font style='color:red'>"._("invalid")."</font>");
405 $smarty->assign($cert."info",$str);
406 $smarty->assign($cert."_state","true");
407 } else {
408 $smarty->assign($cert."info", "<i>"._("No certificate installed")."</i>");
409 $smarty->assign($cert."_state","");
410 }
411 }
412 $smarty->assign("governmentmode", "false");
413 return($smarty->fetch (get_template_path('generic_certs.tpl', TRUE, dirname(__FILE__))));
414 }
416 /* Show us the edit screen */
417 $smarty->assign("bases", $this->config->idepartments);
418 $smarty->assign("base_select", $this->base);
419 $smarty->assign("selectmode", chkacl($this->acl, "create"));
420 $smarty->assign("certificatesACL", chkacl($this->acl, "certificates"));
421 $smarty->assign("jpegPhotoACL", chkacl($this->acl, "jpegPhoto"));
423 /* Prepare password hashes */
424 if ($this->pw_storage == ""){
425 $this->pw_storage= $this->config->current['HASH'];
426 }
428 $temp = passwordMethod::get_available_methods();
429 $hashes = $temp['name'];
431 $smarty->assign("pwmode", $hashes);
432 $smarty->assign("pwmode_select", $this->pw_storage);
433 $smarty->assign("passwordStorageACL", chkacl($this->acl, "passwordStorage"));
435 /* Load attributes and acl's */
436 foreach($this->attributes as $val){
437 $smarty->assign("$val", $this->$val);
438 $smarty->assign("$val"."ACL", chkacl($this->acl,$val));
439 }
441 /* Save government mode attributes */
442 if (isset($this->config->current['GOVERNMENTMODE']) &&
443 preg_match('/true/i', $this->config->current['GOVERNMENTMODE'])){
444 $smarty->assign("governmentmode", "true");
445 $ivbbmodes= array("nein", "ivbv", "testa", "ivbv,testa", "internet",
446 "internet,ivbv", "internet,testa", "internet,ivbv,testa");
447 $smarty->assign("ivbbmodes", $ivbbmodes);
448 foreach ($this->govattrs as $val){
449 $smarty->assign("$val", $this->$val);
450 $smarty->assign("$val"."ACL", chkacl($this->acl,$val));
451 }
452 } else {
453 $smarty->assign("governmentmode", "false");
454 }
456 /* Special mode for uid */
457 $uidACL= "";
458 if (isset ($this->dn)){
459 if ($this->dn != "new"){
460 $uidACL="readonly";
461 }
462 } else {
463 $uidACL= "readonly";
464 }
465 $uidACL.= " ".chkacl($this->acl, "uid");
467 $smarty->assign("uidACL", $uidACL);
468 $smarty->assign("is_template", $this->is_template);
469 $smarty->assign("use_dob", $this->use_dob);
471 if (isset($this->parent)){
472 if (isset($this->parent->by_object['phoneAccount']) &&
473 $this->parent->by_object['phoneAccount']->is_account){
474 $smarty->assign("has_phoneaccount", "true");
475 } else {
476 $smarty->assign("has_phoneaccount", "false");
477 }
478 } else {
479 $smarty->assign("has_phoneaccount", "false");
480 }
482 return($smarty->fetch (get_template_path('generic.tpl', TRUE, dirname(__FILE__))));
483 }
486 /* remove object from parent */
487 function remove_from_parent()
488 {
489 $ldap= $this->config->get_ldap_link();
490 $ldap->rmdir ($this->dn);
492 /* Delete references to groups */
493 $ldap->cd ($this->config->current['BASE']);
494 $ldap->search ("(&(objectClass=posixGroup)(memberUid=".$this->uid."))", array("uid"));
495 while ($ldap->fetch()){
496 $g= new group($this->config, $ldap->getDN());
497 $g->removeUser($this->uid);
498 $g->save ();
499 }
501 /* Delete references to object groups */
502 $ldap->cd ($this->config->current['BASE']);
503 $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".$this->dn."))", array("cn"));
504 while ($ldap->fetch()){
505 $og= new ogroup($this->config, $ldap->getDN());
506 unset($og->member[$this->dn]);
507 $og->save ();
508 }
510 /* Optionally execute a command after we're done */
511 $this->handle_post_events("remove");
512 }
515 /* Save data to object */
516 function save_object()
517 {
518 if (isset($_POST['generic'])){
520 /* Parents save function */
521 plugin::save_object ();
523 /* Save government mode attributes */
524 if ($this->config->current['GOVERNMENTMODE']){
525 foreach ($this->govattrs as $val){
526 if (chkacl ($this->acl, "$val") == "" && isset ($_POST["$val"])){
527 $data= stripcslashes($_POST["$val"]);
528 if ($data != $this->$val){
529 $this->is_modified= TRUE;
530 }
531 $this->$val= $data;
532 }
533 }
534 }
536 /* In template mode, the uid is autogenerated... */
537 if ($this->is_template){
538 $this->uid= strtolower($this->sn);
539 $this->givenName= $this->sn;
540 }
542 /* Save base and pw_storage, since these are no LDAP attributes */
543 if (isset($_POST['base'])){
544 foreach(array("base", "pw_storage") as $val){
545 $data= validate($_POST[$val]);
546 if ($data != $this->$val){
547 $this->is_modified= TRUE;
548 }
549 $this->$val= $data;
550 }
551 }
552 }
553 }
555 function rebind($ldap, $referral)
556 {
557 $credentials= LDAP::get_credentials($referral, $this->config->current['REFERRAL']);
558 if (ldap_bind($ldap, $credentials['ADMIN'], $credentials['PASSWORD'])) {
559 $this->error = "Success";
560 $this->hascon=true;
561 $this->reconnect= true;
562 return (0);
563 } else {
564 $this->error = "Could not bind to " . $credentials['ADMIN'];
565 return NULL;
566 }
567 }
569 /* Save data to LDAP, depending on is_account we save or delete */
570 function save()
571 {
572 /* First use parents methods to do some basic fillup in $this->attrs */
573 plugin::save ();
575 /* Remove additional objectClasses */
576 $tmp= array();
577 foreach ($this->attrs['objectClass'] as $key => $set){
578 $found= false;
579 foreach (array("ivbbEntry", "gosaUserTemplate") as $val){
580 if (preg_match ("/^$set$/i", $val)){
581 $found= true;
582 break;
583 }
584 }
585 if (!$found){
586 $tmp[]= $set;
587 }
588 }
590 /* Replace the objectClass array. This is done because of the
591 separation into government and normal mode. */
592 $this->attrs['objectClass']= $tmp;
594 /* Add objectClasss for template mode? */
595 if ($this->is_template){
596 $this->attrs['objectClass'][]= "gosaUserTemplate";
597 }
599 /* Hard coded government mode? */
600 if ($this->config->current['GOVERNMENTMODE'] != 'false'){
601 $this->attrs['objectClass'][]= "ivbbEntry";
603 /* Copy standard attributes */
604 foreach ($this->govattrs as $val){
605 if ($this->$val != ""){
606 $this->attrs["$val"]= $this->$val;
607 } elseif (!$this->new) {
608 $this->attrs["$val"]= array();
609 }
610 }
612 /* Remove attribute if set to "nein" */
613 if ($this->publicVisible == "nein"){
614 $this->attrs['publicVisible']= array();
615 }
617 }
619 /* Special handling for attribute userCertificate needed */
620 if ($this->userCertificate != ""){
621 $this->attrs["userCertificate;binary"]= $this->userCertificate;
622 $remove_userCertificate= false;
623 } else {
624 $remove_userCertificate= true;
625 }
627 /* Special handling for dob value */
628 if ($this->use_dob == "1"){
629 $this->attrs["dob"]= date("Y-m-d", $this->dob);
630 } else {
631 if ($this->new) {
632 unset($this->attrs["dob"]);
633 } else {
634 $this->attrs["dob"]= array();
635 }
636 }
637 if ($this->gender == "0"){
638 if ($this->new) {
639 unset($this->attrs["gender"]);
640 } else {
641 $this->attrs["gender"]= array();
642 }
643 }
645 /* Special handling for attribute jpegPhote needed, scale image via
646 image magick to 147x200 pixels and inject resulting data. */
647 if ($this->jpegPhoto != "*removed*"){
649 /* Fallback if there's no image magick inside PHP */
650 if (!function_exists("imagick_blob2image")){
651 /* Get temporary file name for conversation */
652 $fname = tempnam ("/tmp", "GOsa");
654 /* Open file and write out photoData */
655 $fp = fopen ($fname, "w");
656 fwrite ($fp, $this->photoData);
657 fclose ($fp);
659 /* Build conversation query. Filename is generated automatically, so
660 we do not need any special security checks. Exec command and save
661 output. For PHP safe mode, you'll need a configuration which respects
662 image magick as executable... */
663 $query= "convert -size 147x200 $fname -resize 147x200 +profile \"*\" -";
664 @DEBUG (DEBUG_SHELL, __LINE__, __FUNCTION__, __FILE__,
665 $query, "Execute");
667 /* Read data written by convert */
668 $output= "";
669 $sh= popen($query, 'r');
670 while (!feof($sh)){
671 $output.= fread($sh, 4096);
672 }
673 pclose($sh);
675 unlink($fname);
677 /* Save attribute */
678 $this->attrs["jpegPhoto"] = $output;
680 } else {
682 /* Load the new uploaded Photo */
683 if(!$handle = imagick_blob2image($this->photoData)) {
684 gosa_log("Can't Load image");
685 }
687 /* Resizing image to 147x200 and blur */
688 if(!imagick_resize($handle,147,200,IMAGICK_FILTER_GAUSSIAN,0)){
689 gosa_log("imagick_resize failed");
690 }
692 /* Converting image to JPEG */
693 if(!imagick_convert($handle,"JPEG")) {
694 gosa_log("Can't Convert to JPEG");
695 }
697 /* Creating binary Code for the Image */
698 if(!$dump = imagick_image2blob($handle)){
699 gosa_log("Can't create blob for image");
700 }
702 /* Sending Image */
703 $output= $dump;
705 /* Save attribute */
706 $this->attrs["jpegPhoto"] = $output;
707 }
709 } elseif(!$this->new) {
710 $this->attrs["jpegPhoto"] = array();
711 }
713 /* Build new dn */
714 if (isset($this->config->current['DNMODE']) && $this->config->current['DNMODE'] == "uid"){
715 $new_dn= 'uid='.$this->uid.','.get_people_ou().$this->base;
716 } else {
717 $new_dn= 'cn='.$this->cn.','.get_people_ou().$this->base;
718 }
720 /* This only gets called when user is renaming himself */
721 $ldap= $this->config->get_ldap_link();
722 if ($this->dn != $new_dn){
724 /* Write entry on new 'dn' */
725 $this->move($this->dn, $new_dn);
727 /* Happen to use the new one */
728 change_ui_dn($this->dn, $new_dn);
729 $this->dn= $new_dn;
730 }
733 /* Save data. Using 'modify' implies that the entry is already present, use 'add' for
734 new entries. So do a check first... */
735 $ldap->cat ($this->dn);
736 if ($ldap->fetch()){
737 $mode= "modify";
738 } else {
739 $mode= "add";
740 $ldap->cd($this->config->current['BASE']);
741 $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
742 }
744 /* Set password to some junk stuff in case of templates */
745 if ($this->is_template){
746 $this->attrs['userPassword']= '{crypt}N0T$3T4N0W';
747 }
749 @DEBUG (DEBUG_LDAP, __LINE__, __FUNCTION__, __FILE__,
750 $this->attributes, "Save via $mode");
752 /* Finally write data with selected 'mode' */
753 $ldap->cd ($this->dn);
754 $ldap->$mode ($this->attrs);
755 if (show_ldap_error($ldap->get_error())){
756 return (1);
757 }
759 /* Remove cert?
760 For some reason, the 'ldap' class doesn't want to remove binary entries, so I need
761 to work around myself. */
762 if ($remove_userCertificate == true && !$this->new && $this->had_userCertificate){
764 /* Reset array, assemble new, this should be reworked */
765 $this->attrs= array();
766 $this->attrs['userCertificate;binary']= array();
768 /* Prepare connection */
769 if (!($ds = ldap_connect($this->config->current['SERVER']))) {
770 die ("Could not connect to LDAP server");
771 }
772 ldap_set_option($ds, LDAP_OPT_PROTOCOL_VERSION, 3);
773 if (function_exists("ldap_set_rebind_proc") && isset($this->config->current['RECURSIVE']) && $this->config->current['RECURSIVE'] == "true") {
774 ldap_set_option($this->cid, LDAP_OPT_REFERRALS, 1);
775 ldap_set_rebind_proc($ds, array(&$this, "rebind"));
776 }
777 if(isset($config->current['TLS']) && $config->current['TLS'] == "true"){
778 ldap_start_tls($ds);
779 }
780 if (!($res = @ldap_bind($ds, $this->config->current['ADMIN'],
781 $this->config->current['PASSWORD']))) {
782 die ("Could not bind to LDAP");
783 }
785 /* Modify using attrs */
786 ldap_mod_del($ds,$this->dn,$this->attrs);
787 ldap_close($ds);
788 }
790 /* Kerberos server defined? */
791 if (isset($this->config->data['SERVERS']['KERBEROS'])){
792 $cfg= $this->config->data['SERVERS']['KERBEROS'];
793 }
794 if (isset($cfg['SERVER']) && function_exists('kadm5_init_with_password')){
796 /* Connect to the admin interface */
797 $handle = kadm5_init_with_password($cfg['SERVER'], $cfg['REALM'],
798 $cfg['ADMIN'], $cfg['PASSWORD']);
800 /* Errors? */
801 if ($handle === FALSE){
802 print_red (_("Kerberos database communication failed"));
803 return (2);
804 }
806 /* Build user principal, get list of existsing principals */
807 $principal= $this->uid."@".$cfg['REALM'];
808 $principals = kadm5_get_principals($handle);
810 /* User exists in database? */
811 if (in_array($principal, $principals)){
813 /* Ok. User exists. Remove him/her when pw_storage has
814 changed to be NOT kerberos. */
815 if ($this->pw_storage != "kerberos"){
816 $ret= kadm5_delete_principal ( $handle, $principal);
818 if ($ret === FALSE){
819 print_red (_("Can't remove user from kerberos database."));
820 }
821 }
823 } else {
825 /* User doesn't exists, create it when pw_storage is kerberos. */
826 if ($this->pw_storage == "kerberos"){
827 $ret= kadm5_create_principal ( $handle, $principal);
829 if ($ret === FALSE){
830 print_red (_("Can't add user to kerberos database."));
831 }
832 }
834 }
836 /* Free kerberos admin handle */
837 kadm5_destroy($handle);
838 }
840 /* Optionally execute a command after we're done */
841 if ($mode == "add"){
842 $this->handle_post_events("add");
843 } elseif ($this->is_modified){
844 $this->handle_post_events("modify");
845 }
847 return (0);
848 }
851 /* Check formular input */
852 function check()
853 {
854 $message= array();
856 /* Assemble cn */
857 $this->cn= $this->givenName." ".$this->sn;
859 /* Permissions for that base? */
860 if (isset($this->config->current['DNMODE']) && $this->config->current['DNMODE'] == "uid"){
861 $new_dn= 'uid='.$this->uid.','.get_people_ou().$this->base;
862 } else {
863 $new_dn= 'cn='.$this->cn.','.get_people_ou().$this->base;
864 }
866 $ui= get_userinfo();
867 $acl= get_permissions ($new_dn, $ui->subtreeACL);
868 $acl= get_module_permission($acl, "user", $new_dn);
869 if ($this->dn == "new" && chkacl($acl, "create") != ""){
870 $message[]= _("You have no permissions to create a user on this 'Base'.");
871 } elseif ($this->dn != $new_dn && $this->dn != "new"){
872 $acl= get_permissions ($this->dn, $ui->subtreeACL);
873 $acl= get_module_permission($acl, "user", $this->dn);
874 if (chkacl($acl, "create") != ""){
875 $message[]= _("You have no permissions to move a user from the original 'Base'.");
876 }
877 }
879 /* must: sn, givenName, uid */
880 if ($this->sn == "" && chkacl ($this->acl, "sn") == ""){
881 $message[]= _("The required field 'Name' is not set.");
882 }
884 /* UID already used? */
885 $ldap= $this->config->get_ldap_link();
886 $ldap->cd($this->config->current['BASE']);
887 $ldap->search("(uid=$this->uid)", array("uid"));
888 $ldap->fetch();
889 if ($ldap->count() != 0 && $this->dn == 'new'){
890 $message[]= _("There's already a person with this 'Login' in the database.");
891 }
893 /* In template mode, the uid and givenName are autogenerated... */
894 if (!$this->is_template){
895 if ($this->givenName == "" && chkacl ($this->acl, "givenName") == ""){
896 $message[]= _("The required field 'Given name' is not set.");
897 }
898 if ($this->uid == "" && chkacl ($this->acl, "uid") == ""){
899 $message[]= _("The required field 'Login' is not set.");
900 }
901 if (!(isset($this->config->current['DNMODE']) && $this->config->current['DNMODE'] == "uid")){
902 $ldap->cd($this->config->current['BASE']);
903 $ldap->search("(cn=".$this->cn.")", array("uid"));
904 $ldap->fetch();
905 if ($ldap->count() != 0 && $this->dn != $new_dn && $this->dn == 'new'){
906 $message[]= _("There's already a person with this 'Name'/'Given name' combination in the database.");
907 }
908 }
909 }
911 /* Check for valid input */
912 if (!is_uid($this->uid)){
913 $message[]= _("The field 'Login' contains invalid characters. Lowercase, numbers and dashes are allowed.");
914 }
915 if (!is_url($this->labeledURI)){
916 $message[]= _("The field 'Homepage' contains an invalid URL definition.");
917 }
918 if (preg_match ("/[\\\\]/", $this->sn)){
919 $message[]= _("The field 'Name' contains invalid characters.");
920 }
921 if (preg_match ("/[\\\\]/", $this->givenName)){
922 $message[]= _("The field 'Given name' contains invalid characters.");
923 }
925 /* Check phone numbers */
926 if (!is_phone_nr($this->homePhone)){
927 $message[]= _("The field 'Phone' contains an invalid phone number.");
928 }
929 if (!is_phone_nr($this->telephoneNumber)){
930 $message[]= _("The field 'Phone' contains an invalid phone number.");
931 }
932 if (!is_phone_nr($this->facsimileTelephoneNumber)){
933 $message[]= _("The field 'Fax' contains an invalid phone number.");
934 }
935 if (!is_phone_nr($this->mobile)){
936 $message[]= _("The field 'Mobile' contains an invalid phone number.");
937 }
938 if (!is_phone_nr($this->pager)){
939 $message[]= _("The field 'Pager' contains an invalid phone number.");
940 }
942 /* Check for reserved characers */
943 if (preg_match ('/[,+"<>;]/', $this->givenName)){
944 $message[]= _("The field 'Given name' contains invalid characters.");
945 }
946 if (preg_match ('/[,+"<>;]/', $this->sn)){
947 $message[]= _("The field 'Name' contains invalid characters.");
948 }
950 return $message;
951 }
954 /* Indicate whether a password change is needed or not */
955 function password_change_needed()
956 {
957 return ($this->pw_storage != $this->last_pw_storage);
958 }
961 /* Load a jpegPhoto from LDAP, this is going to be simplified later on */
962 function load_picture()
963 {
964 /* make connection and read jpegPhoto */
965 $ds= ldap_connect($this->config->current['SERVER']);
966 ldap_set_option($ds, LDAP_OPT_PROTOCOL_VERSION, 3);
967 if (function_exists("ldap_set_rebind_proc") && isset($this->config->current['RECURSIVE']) && $this->config->current['RECURSIVE'] == "true") {
968 ldap_set_option($this->cid, LDAP_OPT_REFERRALS, 1);
969 ldap_set_rebind_proc($ds, array(&$this, "rebind"));
970 }
972 if(isset($this->config->current['TLS']) &&
973 $this->config->current['TLS'] == "true"){
975 ldap_start_tls($ds);
976 }
978 $r= ldap_bind($ds);
979 $sr= @ldap_read($ds, $this->dn, "jpegPhoto=*", array("jpegPhoto"));
981 /* in case we don't get an entry, load a default picture */
982 $this->set_picture ("./images/default.jpg");
983 $this->jpegPhoto= "*removed*";
985 /* fill data from LDAP */
986 if ($sr) {
987 $ei=ldap_first_entry($ds, $sr);
988 if ($ei) {
989 if ($info = ldap_get_values_len($ds, $ei, "jpegPhoto")){
990 $this->photoData= $info[0];
991 $_SESSION['picture']= $this->photoData;
992 $this->jpegPhoto= "";
993 }
994 }
995 }
997 /* close conncetion */
998 ldap_unbind($ds);
999 }
1002 /* Load a certificate from LDAP, this is going to be simplified later on */
1003 function load_cert()
1004 {
1005 $ds= ldap_connect($this->config->current['SERVER']);
1006 ldap_set_option($ds, LDAP_OPT_PROTOCOL_VERSION, 3);
1007 if (function_exists("ldap_set_rebind_proc") && isset($this->config->current['RECURSIVE']) && $this->config->current['RECURSIVE'] == "true") {
1008 ldap_set_option($this->cid, LDAP_OPT_REFERRALS, 1);
1009 ldap_set_rebind_proc($ds, array(&$this, "rebind"));
1010 }
1011 if(isset($this->config->current['TLS']) &&
1012 $this->config->current['TLS'] == "true"){
1014 ldap_start_tls($ds);
1015 }
1017 $r= ldap_bind($ds);
1018 $sr= @ldap_read($ds, $this->dn, "userCertificate=*", array("userCertificate"));
1020 if ($sr) {
1021 $ei= @ldap_first_entry($ds, $sr);
1023 if ($ei) {
1024 if (!$info = @ldap_get_values_len($ds, $ei, "userCertificate;binary")){
1025 $this->userCertificate= "";
1026 } else {
1027 $this->userCertificate= $info[0];
1028 }
1029 }
1030 } else {
1031 $this->userCertificate= "";
1032 }
1034 ldap_unbind($ds);
1035 }
1038 /* Load picture from file to object */
1039 function set_picture($filename)
1040 {
1041 if (!is_file($filename)){
1042 $filename= "./images/default.jpg";
1043 $this->jpegPhoto= "*removed*";
1044 }
1046 $fd = fopen ($filename, "rb");
1047 $this->photoData= fread ($fd, filesize ($filename));
1048 $_SESSION['picture']= $this->photoData;
1049 $this->jpegPhoto= "";
1051 fclose ($fd);
1052 }
1055 /* Load certificate from file to object */
1056 function set_cert($cert, $filename)
1057 {
1058 $fd = fopen ($filename, "rb");
1059 if (filesize($filename)>0) {
1060 $this->$cert= fread ($fd, filesize ($filename));
1061 fclose ($fd);
1062 $this->is_modified= TRUE;
1063 } else {
1064 print_red(_("Could not open specified certificate!"));
1065 }
1066 }
1068 /* Adapt from given 'dn' */
1069 function adapt_from_template($dn)
1070 {
1071 plugin::adapt_from_template($dn);
1073 /* Get base */
1074 $this->base= preg_replace('/^[^,]+,'.get_people_ou().'/i', '', $dn);
1076 if ($this->config->current['GOVERNMENTMODE']){
1078 /* Walk through govattrs */
1079 foreach ($this->govattrs as $val){
1081 if (isset($this->attrs["$val"][0])){
1083 /* If attribute is set, replace dynamic parts:
1084 %sn, %givenName and %uid. Fill these in our local variables. */
1085 $value= $this->attrs["$val"][0];
1087 foreach (array("sn", "givenName", "uid") as $repl){
1088 if (preg_match("/%$repl/i", $value)){
1089 $value= preg_replace ("/%$repl/i",
1090 $this->parent->$repl, $value);
1091 }
1092 }
1093 $this->$val= $value;
1094 }
1095 }
1096 }
1098 /* Get back uid/sn/givenName */
1099 if ($this->parent != NULL){
1100 $this->uid= $this->parent->uid;
1101 $this->sn= $this->parent->sn;
1102 $this->givenName= $this->parent->givenName;
1103 }
1104 }
1106 }
1108 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
1109 ?>