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 }
120 }
122 /* Create me for new accounts */
123 if ($dn == "new"){
124 $this->is_account= TRUE;
125 }
127 /* Make hash default to md5 if not set in config */
128 if (!isset($this->config->current['HASH'])){
129 $hash= "md5";
130 } else {
131 $hash= $this->config->current['HASH'];
132 }
134 /* Load data from LDAP? */
135 if ($dn != NULL){
137 /* Do base conversation */
138 if ($this->dn == "new"){
139 $ui= get_userinfo();
140 $this->base= dn2base($ui->dn);
141 } else {
142 $this->base= dn2base($dn);
143 }
145 /* get password storage type */
146 if (isset ($this->attrs['userPassword'][0])){
147 if (preg_match ("/^{([^}]+)}(.+)/", $this->attrs['userPassword'][0], $matches)){
148 $this->pw_storage= strtolower($matches[1]);
149 } else {
150 if ($this->attrs['userPassword'][0] != ""){
151 $this->pw_storage= "clear";
152 } else {
153 $this->pw_storage= $hash;
154 }
155 }
156 } else {
157 /* Preset with vaule from configuration */
158 $this->pw_storage= $hash;
159 }
161 /* Load extra attributes: certificate and picture */
162 $this->load_picture();
163 $this->load_cert();
164 if ($this->userCertificate != ""){
165 $this->had_userCertificate= TRUE;
166 }
167 }
169 /* Reset password storage indicator, used by password_change_needed() */
170 if ($dn == "new"){
171 $this->last_pw_storage= "unset";
172 } else {
173 $this->last_pw_storage= $this->pw_storage;
174 }
176 /* Generate dob entry */
177 if (isset ($this->attrs['dateOfBirth'])){
178 /* This entry is ISO 8601 conform */
179 list($year, $month, $day)= split("-", $this->attrs['dateOfBirth'][0], 3);
180 $this->dob= mktime ( 0, 0, 0, $month, $day, $year);
181 $this->use_dob= "1";
182 } else {
183 $this->use_dob= "0";
184 }
186 /* Put gender attribute to upper case */
187 if (isset ($this->attrs['gender'])){
188 $this->gender= strtoupper($this->attrs['gender'][0]);
189 }
190 }
193 /* execute generates the html output for this node */
194 function execute()
195 {
196 $smarty= get_smarty();
198 /* Fill calendar */
199 if ($this->dob == "0"){
200 $date= getdate();
201 } else {
202 $date= getdate($this->dob);
203 }
205 $days= array();
206 for($d= 1; $d<32; $d++){
207 $days[$d]= $d;
208 }
209 $years= array();
210 for($y= $date['year']-100; $y<=$date['year']+100; $y++){
211 $years[]= $y;
212 }
213 $years['-']= "";
214 $months= array(_("January"), _("February"), _("March"), _("April"),
215 _("May"), _("June"), _("July"), _("August"), _("September"),
216 _("October"), _("November"), _("December"), '-' => '');
217 $smarty->assign("day", $date["mday"]);
218 $smarty->assign("days", $days);
219 $smarty->assign("months", $months);
220 $smarty->assign("month", $date["mon"]-1);
221 $smarty->assign("years", $years);
222 $smarty->assign("year", $date["year"]);
224 /* Assign sex */
225 $sex= array(0 => "", "F" => _("female"), "M" => _("male"));
226 $smarty->assign("gender_list", $sex);
228 /* Get random number for pictures */
229 srand((double)microtime()*1000000);
230 $smarty->assign("rand", rand(0, 10000));
232 /* Do we represent a valid gosaAccount? */
233 if (!$this->is_account){
234 echo "<img alt=\"\" src=\"images/stop.png\" align=\"middle\"> <b>".
235 _("This account has no valid GOsa extensions.")."</b>";
236 return;
237 }
239 /* Want picture edit dialog? */
240 if (isset($_POST['edit_picture'])){
241 /* Save values for later recovery, in case some presses
242 the cancel button. */
243 $this->old_jpegPhoto= $this->jpegPhoto;
244 $this->old_photoData= $this->photoData;
245 $this->picture_dialog= TRUE;
246 $this->dialog= TRUE;
247 }
249 /* Remove picture? */
250 if (isset($_POST['picture_remove'])){
251 $this->jpegPhoto= "*removed*";
252 $this->set_picture ("./images/default.jpg");
253 $this->is_modified= TRUE;
255 return($smarty->fetch (get_template_path('generic_picture.tpl', TRUE, dirname(__FILE__))));
256 }
258 /* Save picture */
259 if (isset($_POST['picture_edit_finish'])){
261 /* Check for clean upload */
262 if ($_FILES['picture_file']['name'] != ""){
263 if (!is_uploaded_file($_FILES['picture_file']['tmp_name'])) {
264 print_red(_("The specified file has not been uploaded via HTTP POST! Aborted."));
265 exit;
266 }
268 /* Activate new picture */
269 $this->set_picture($_FILES['picture_file']['tmp_name']);
270 }
271 $this->picture_dialog= FALSE;
272 $this->dialog= FALSE;
273 $this->is_modified= TRUE;
274 }
277 /* Cancel picture */
278 if (isset($_POST['picture_edit_cancel'])){
280 /* Restore values */
281 $this->jpegPhoto= $this->old_jpegPhoto;
282 $this->photoData= $this->old_photoData;
284 /* Update picture */
285 $_SESSION['picture']= $this->photoData;
286 $this->picture_dialog= FALSE;
287 $this->dialog= FALSE;
288 }
290 /* Toggle dob information */
291 if (isset($_POST['set_dob'])){
292 $this->use_dob= ($this->use_dob == "0")?"1":"0";
293 }
296 /* Want certificate= */
297 if (isset($_POST['edit_cert'])){
299 /* Save original values for later reconstruction */
300 foreach (array("certificateSerialNumber", "userCertificate",
301 "userSMIMECertificate", "userPKCS12") as $val){
303 $oval= "old_$val";
304 $this->$oval= $this->$val;
305 }
307 $this->cert_dialog= TRUE;
308 $this->dialog= TRUE;
309 }
312 /* Cancel certificate dialog */
313 if (isset($_POST['cert_edit_cancel'])){
315 /* Restore original values in case of 'cancel' */
316 foreach (array("certificateSerialNumber", "userCertificate",
317 "userSMIMECertificate", "userPKCS12") as $val){
319 $oval= "old_$val";
320 $this->$val= $this->$oval;
321 }
322 $this->cert_dialog= FALSE;
323 $this->dialog= FALSE;
324 }
327 /* Remove certificate? */
328 foreach (array ("userCertificate", "userSMIMECertificate", "userPKCS12") as $val){
329 if (isset($_POST["remove_$val"])){
331 /* Reset specified cert*/
332 $this->$val= "";
333 $this->is_modified= TRUE;
334 }
335 }
338 /* Upload new cert and close dialog? */
339 if (isset($_POST['cert_edit_finish'])){
341 /* for all certificates do */
342 foreach (array ("userCertificate", "userSMIMECertificate", "userPKCS12")
343 as $val){
345 /* Check for clean upload */
346 if (array_key_exists($val."_file", $_FILES) &&
347 array_key_exists('name', $_FILES[$val."_file"]) &&
348 $_FILES[$val."_file"]['name'] != "" &&
349 is_uploaded_file($_FILES[$val."_file"]['tmp_name'])) {
350 $this->set_cert("$val", $_FILES[$val."_file"]['tmp_name']);
351 }
352 }
354 /* Save serial number */
355 if (isset($_POST["certificateSerialNumber"]) &&
356 $_POST["certificateSerialNumber"] != ""){
358 if (!is_id($_POST["certificateSerialNumber"])){
359 print_red (_("Please enter a valid serial number"));
361 foreach(array("userCertificate", "userSMIMECertificate", "userPKCS12") as $cert){
362 if ($this->$cert != ""){
363 $smarty->assign("$cert"."_state", "true");
364 } else {
365 $smarty->assign("$cert"."_state", "");
366 }
367 }
368 return ($smarty->fetch (get_template_path('generic_certs.tpl', TRUE, dirname(__FILE__))));
369 }
371 $this->certificateSerialNumber= $_POST["certificateSerialNumber"];
372 $this->is_modified= TRUE;
373 }
375 $this->cert_dialog= FALSE;
376 $this->dialog= FALSE;
377 }
379 /* Display picture dialog */
380 if ($this->picture_dialog){
381 return($smarty->fetch (get_template_path('generic_picture.tpl', TRUE, dirname(__FILE__))));
382 }
384 /* Display cert dialog */
385 if ($this->cert_dialog){
386 foreach(array("userCertificate", "userSMIMECertificate", "userPKCS12") as $cert){
387 if ($this->$cert != ""){
388 /* import certificate */
389 $certificate = new certificate;
390 $certificate->import($this->$cert);
392 /* Read out data*/
393 $timeto = $certificate->getvalidto_date();
394 $timefrom = $certificate->getvalidfrom_date();
395 $str = "<table summary=\"\" border=0><tr><td style='vertical-align:top'>CN</td><td>".preg_replace("/ /", " ", $certificate->getname())."</td></tr></table><br>".
396 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>");
397 $smarty->assign($cert."info",$str);
398 $smarty->assign($cert."_state","true");
399 } else {
400 $smarty->assign($cert."info", "<i>"._("No certificate installed")."</i>");
401 $smarty->assign($cert."_state","");
402 }
403 }
404 $smarty->assign("governmentmode", "false");
405 return($smarty->fetch (get_template_path('generic_certs.tpl', TRUE, dirname(__FILE__))));
406 }
408 /* Show us the edit screen */
409 $smarty->assign("bases", $this->config->idepartments);
410 $smarty->assign("base_select", $this->base);
411 $smarty->assign("selectmode", chkacl($this->acl, "create"));
412 $smarty->assign("certificatesACL", chkacl($this->acl, "certificates"));
413 $smarty->assign("jpegPhotoACL", chkacl($this->acl, "jpegPhoto"));
415 /* Prepare password hashes */
416 if ($this->pw_storage == ""){
417 $this->pw_storage= $this->config->current['HASH'];
418 }
420 $temp = passwordMethod::get_available_methods();
421 $hashes = $temp['name'];
423 $smarty->assign("pwmode", $hashes);
424 $smarty->assign("pwmode_select", $this->pw_storage);
425 $smarty->assign("passwordStorageACL", chkacl($this->acl, "passwordStorage"));
427 /* Load attributes and acl's */
428 foreach($this->attributes as $val){
429 $smarty->assign("$val", $this->$val);
430 $smarty->assign("$val"."ACL", chkacl($this->acl,$val));
431 }
433 /* Save government mode attributes */
434 if (isset($this->config->current['GOVERNMENTMODE']) &&
435 preg_match('/true/i', $this->config->current['GOVERNMENTMODE'])){
436 $smarty->assign("governmentmode", "true");
437 $ivbbmodes= array("nein", "ivbv", "testa", "ivbv,testa", "internet",
438 "internet,ivbv", "internet,testa", "internet,ivbv,testa", "ja");
439 $smarty->assign("ivbbmodes", $ivbbmodes);
440 foreach ($this->govattrs as $val){
441 $smarty->assign("$val", $this->$val);
442 $smarty->assign("$val"."ACL", chkacl($this->acl,$val));
443 }
444 } else {
445 $smarty->assign("governmentmode", "false");
446 }
448 /* Special mode for uid */
449 $uidACL= "";
450 if (isset ($this->dn)){
451 if ($this->dn != "new"){
452 $uidACL="readonly";
453 }
454 } else {
455 $uidACL= "readonly";
456 }
457 $uidACL.= " ".chkacl($this->acl, "uid");
459 $smarty->assign("uidACL", $uidACL);
460 $smarty->assign("is_template", $this->is_template);
461 $smarty->assign("use_dob", $this->use_dob);
463 if (isset($this->parent)){
464 if (isset($this->parent->by_object['phoneAccount']) &&
465 $this->parent->by_object['phoneAccount']->is_account){
466 $smarty->assign("has_phoneaccount", "true");
467 } else {
468 $smarty->assign("has_phoneaccount", "false");
469 }
470 } else {
471 $smarty->assign("has_phoneaccount", "false");
472 }
474 return($smarty->fetch (get_template_path('generic.tpl', TRUE, dirname(__FILE__))));
475 }
478 /* remove object from parent */
479 function remove_from_parent()
480 {
481 $ldap= $this->config->get_ldap_link();
482 $ldap->rmdir ($this->dn);
484 /* Delete references to groups */
485 $ldap->cd ($this->config->current['BASE']);
486 $ldap->search ("(&(objectClass=posixGroup)(memberUid=".$this->uid."))", array("uid"));
487 while ($ldap->fetch()){
488 $g= new group($this->config, $ldap->getDN());
489 $g->removeUser($this->uid);
490 $g->save ();
491 }
493 /* Delete references to object groups */
494 $ldap->cd ($this->config->current['BASE']);
495 $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".$this->dn."))", array("cn"));
496 while ($ldap->fetch()){
497 $og= new ogroup($this->config, $ldap->getDN());
498 unset($og->member[$this->dn]);
499 $og->save ();
500 }
502 /* Optionally execute a command after we're done */
503 $this->handle_post_events("remove");
504 }
507 /* Save data to object */
508 function save_object()
509 {
510 if (isset($_POST['generic'])){
512 /* Parents save function */
513 plugin::save_object ();
515 /* Save government mode attributes */
516 if ($this->config->current['GOVERNMENTMODE']){
517 foreach ($this->govattrs as $val){
518 if (chkacl ($this->acl, "$val") == "" && isset ($_POST["$val"])){
519 $data= stripcslashes($_POST["$val"]);
520 if ($data != $this->$val){
521 $this->is_modified= TRUE;
522 }
523 $this->$val= $data;
524 }
525 }
526 }
528 /* In template mode, the uid is autogenerated... */
529 if ($this->is_template){
530 $this->uid= strtolower($this->sn);
531 $this->givenName= $this->sn;
532 }
534 /* Save base and pw_storage, since these are no LDAP attributes */
535 if (isset($_POST['base'])){
536 foreach(array("base", "pw_storage") as $val){
537 $data= validate($_POST[$val]);
538 if ($data != $this->$val){
539 $this->is_modified= TRUE;
540 }
541 $this->$val= $data;
542 }
543 }
544 }
545 }
547 function rebind($ldap, $referral)
548 {
549 $credentials= LDAP::get_credentials($referral, $this->config->current['REFERRAL']);
550 if (ldap_bind($ldap, $credentials['ADMIN'], $credentials['PASSWORD'])) {
551 $this->error = "Success";
552 $this->hascon=true;
553 $this->reconnect= true;
554 return (0);
555 } else {
556 $this->error = "Could not bind to " . $binddn;
557 return NULL;
558 }
559 }
561 /* Save data to LDAP, depending on is_account we save or delete */
562 function save()
563 {
564 /* First use parents methods to do some basic fillup in $this->attrs */
565 plugin::save ();
567 /* Remove additional objectClasses */
568 $tmp= array();
569 foreach ($this->attrs['objectClass'] as $key => $set){
570 $found= false;
571 foreach (array("ivbbEntry", "gosaUserTemplate") as $val){
572 if (preg_match ("/^$set$/i", $val)){
573 $found= true;
574 break;
575 }
576 }
577 if (!$found){
578 $tmp[]= $set;
579 }
580 }
582 /* Replace the objectClass array. This is done because of the
583 separation into government and normal mode. */
584 $this->attrs['objectClass']= $tmp;
586 /* Add objectClasss for template mode? */
587 if ($this->is_template){
588 $this->attrs['objectClass'][]= "gosaUserTemplate";
589 }
591 /* Hard coded government mode? */
592 if ($this->config->current['GOVERNMENTMODE'] != 'false'){
593 $this->attrs['objectClass'][]= "ivbbEntry";
595 /* Copy standard attributes */
596 foreach ($this->govattrs as $val){
597 if ($this->$val != ""){
598 $this->attrs["$val"]= $this->$val;
599 } elseif (!$this->new) {
600 $this->attrs["$val"]= array();
601 }
602 }
604 }
606 /* Special handling for attribute userCertificate needed */
607 if ($this->userCertificate != ""){
608 $this->attrs["userCertificate;binary"]= $this->userCertificate;
609 $remove_userCertificate= false;
610 } else {
611 $remove_userCertificate= true;
612 }
614 /* Special handling for dob value */
615 if ($this->use_dob == "1"){
616 $this->attrs["dob"]= date("Y-m-d", $this->dob);
617 } else {
618 if ($this->new) {
619 unset($this->attrs["dob"]);
620 } else {
621 $this->attrs["dob"]= array();
622 }
623 }
624 if ($this->gender == "0"){
625 if ($this->new) {
626 unset($this->attrs["gender"]);
627 } else {
628 $this->attrs["gender"]= array();
629 }
630 }
632 /* Special handling for attribute jpegPhote needed, scale image via
633 image magick to 147x200 pixels and inject resulting data. */
634 if ($this->jpegPhoto != "*removed*"){
636 /* Fallback if there's no image magick inside PHP */
637 if (!function_exists("imagick_blob2image")){
638 /* Get temporary file name for conversation */
639 $fname = tempnam ("/tmp", "GOsa");
641 /* Open file and write out photoData */
642 $fp = fopen ($fname, "w");
643 fwrite ($fp, $this->photoData);
644 fclose ($fp);
646 /* Build conversation query. Filename is generated automatically, so
647 we do not need any special security checks. Exec command and save
648 output. For PHP safe mode, you'll need a configuration which respects
649 image magick as executable... */
650 $query= "convert -size 147x200 $fname -resize 147x200 +profile \"*\" -";
651 @DEBUG (DEBUG_SHELL, __LINE__, __FUNCTION__, __FILE__,
652 $query, "Execute");
654 /* Read data written by convert */
655 $output= "";
656 $sh= popen($query, 'r');
657 while (!feof($sh)){
658 $output.= fread($sh, 4096);
659 }
660 pclose($sh);
662 unlink($fname);
664 /* Save attribute */
665 $this->attrs["jpegPhoto"] = $output;
667 } else {
669 /* Load the new uploaded Photo */
670 if(!$handle = imagick_blob2image($this->photoData)) {
671 gosa_log("Can't Load image");
672 }
674 /* Resizing image to 147x200 and blur */
675 if(!imagick_resize($handle,147,200,IMAGICK_FILTER_GAUSSIAN,0)){
676 gosa_log("imagick_resize failed");
677 }
679 /* Converting image to JPEG */
680 if(!imagick_convert($handle,"JPEG")) {
681 gosa_log("Can't Convert to JPEG");
682 }
684 /* Creating binary Code for the Image */
685 if(!$dump = imagick_image2blob($handle)){
686 gosa_log("Can't create blob for image");
687 }
689 /* Sending Image */
690 $output= $dump;
692 /* Save attribute */
693 $this->attrs["jpegPhoto"] = $output;
694 }
696 } elseif(!$this->new) {
697 $this->attrs["jpegPhoto"] = array();
698 }
700 /* Build new dn */
701 if (isset($this->config->current['DNMODE']) && $this->config->current['DNMODE'] == "uid"){
702 $new_dn= 'uid='.$this->uid.','.get_people_ou().$this->base;
703 } else {
704 $new_dn= 'cn='.$this->cn.','.get_people_ou().$this->base;
705 }
707 /* This only gets called when user is renaming himself */
708 $ldap= $this->config->get_ldap_link();
709 if ($this->dn != $new_dn){
711 /* Write entry on new 'dn' */
712 $this->move($this->dn, $new_dn);
714 /* Happen to use the new one */
715 change_ui_dn($this->dn, $new_dn);
716 $this->dn= $new_dn;
717 }
720 /* Save data. Using 'modify' implies that the entry is already present, use 'add' for
721 new entries. So do a check first... */
722 $ldap->cat ($this->dn);
723 if ($ldap->fetch()){
724 $mode= "modify";
725 } else {
726 $mode= "add";
727 $ldap->cd($this->config->current['BASE']);
728 $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
729 }
731 /* Set password to some junk stuff in case of templates */
732 if ($this->is_template){
733 $this->attrs['userPassword']= '{crypt}N0T$3T4N0W';
734 }
736 @DEBUG (DEBUG_LDAP, __LINE__, __FUNCTION__, __FILE__,
737 $this->attributes, "Save via $mode");
739 /* Finally write data with selected 'mode' */
740 $ldap->cd ($this->dn);
741 $ldap->$mode ($this->attrs);
742 if (show_ldap_error($ldap->get_error())){
743 return (1);
744 }
746 /* Remove cert?
747 For some reason, the 'ldap' class doesn't want to remove binary entries, so I need
748 to work around myself. */
749 if ($remove_userCertificate == true && !$this->new && $this->had_userCertificate){
751 /* Reset array, assemble new, this should be reworked */
752 $this->attrs= array();
753 $this->attrs['userCertificate;binary']= array();
755 /* Prepare connection */
756 if (!($ds = ldap_connect($this->config->current['SERVER']))) {
757 die ("Could not connect to LDAP server");
758 }
759 ldap_set_option($ds, LDAP_OPT_PROTOCOL_VERSION, 3);
760 if (function_exists("ldap_set_rebind_proc") && isset($this->config->current['RECURSIVE']) && $this->config->current['RECURSIVE'] == "true") {
761 ldap_set_option($this->cid, LDAP_OPT_REFERRALS, 1);
762 ldap_set_rebind_proc($ds, array(&$this, "rebind"));
763 }
764 if(isset($config->current['TLS']) && $config->current['TLS'] == "true"){
765 ldap_start_tls($ds);
766 }
767 if (!($res = @ldap_bind($ds, $this->config->current['ADMIN'],
768 $this->config->current['PASSWORD']))) {
769 die ("Could not bind to LDAP");
770 }
772 /* Modify using attrs */
773 ldap_mod_del($ds,$this->dn,$this->attrs);
774 ldap_close($ds);
775 }
777 /* Kerberos server defined? */
778 if (isset($this->config->data['SERVERS']['KERBEROS'])){
779 $cfg= $this->config->data['SERVERS']['KERBEROS'];
780 }
781 if (isset($cfg['SERVER']) && function_exists('kadm5_init_with_password')){
783 /* Connect to the admin interface */
784 $handle = kadm5_init_with_password($cfg['SERVER'], $cfg['REALM'],
785 $cfg['ADMIN'], $cfg['PASSWORD']);
787 /* Errors? */
788 if ($handle === FALSE){
789 print_red (_("Kerberos database communication failed"));
790 return (2);
791 }
793 /* Build user principal, get list of existsing principals */
794 $principal= $this->uid."@".$cfg['REALM'];
795 $principals = kadm5_get_principals($handle);
797 /* User exists in database? */
798 if (in_array($principal, $principals)){
800 /* Ok. User exists. Remove him/her when pw_storage has
801 changed to be NOT kerberos. */
802 if ($this->pw_storage != "kerberos"){
803 $ret= kadm5_delete_principal ( $handle, $principal);
805 if ($ret === FALSE){
806 print_red (_("Can't remove user from kerberos database."));
807 }
808 }
810 } else {
812 /* User doesn't exists, create it when pw_storage is kerberos. */
813 if ($this->pw_storage == "kerberos"){
814 $ret= kadm5_create_principal ( $handle, $principal);
816 if ($ret === FALSE){
817 print_red (_("Can't add user to kerberos database."));
818 }
819 }
821 }
823 /* Free kerberos admin handle */
824 kadm5_destroy($handle);
825 }
827 /* Optionally execute a command after we're done */
828 if ($mode == "add"){
829 $this->handle_post_events("add");
830 } elseif ($this->is_modified){
831 $this->handle_post_events("modify");
832 }
834 return (0);
835 }
838 /* Check formular input */
839 function check()
840 {
841 $message= array();
843 /* Assemble cn */
844 $this->cn= $this->givenName." ".$this->sn;
846 /* Permissions for that base? */
847 if (isset($this->config->current['DNMODE']) && $this->config->current['DNMODE'] == "uid"){
848 $new_dn= 'uid='.$this->uid.','.get_people_ou().$this->base;
849 } else {
850 $new_dn= 'cn='.$this->cn.','.get_people_ou().$this->base;
851 }
853 $ui= get_userinfo();
854 $acl= get_permissions ($new_dn, $ui->subtreeACL);
855 $acl= get_module_permission($acl, "user", $new_dn);
856 if ($this->dn == "new" && chkacl($acl, "create") != ""){
857 $message[]= _("You have no permissions to create a user on this 'Base'.");
858 } elseif ($this->dn != $new_dn && $this->dn != "new"){
859 $acl= get_permissions ($this->dn, $ui->subtreeACL);
860 $acl= get_module_permission($acl, "user", $this->dn);
861 if (chkacl($acl, "create") != ""){
862 $message[]= _("You have no permissions to move a user from the original 'Base'.");
863 }
864 }
866 /* must: sn, givenName, uid */
867 if ($this->sn == "" && chkacl ($this->acl, "sn") == ""){
868 $message[]= _("The required field 'Name' is not set.");
869 }
871 /* UID already used? */
872 $ldap= $this->config->get_ldap_link();
873 $ldap->cd($this->config->current['BASE']);
874 $ldap->search("(uid=$this->uid)", array("uid"));
875 $ldap->fetch();
876 if ($ldap->count() != 0 && $this->dn == 'new'){
877 $message[]= _("There's already a person with this 'Login' in the database.");
878 }
880 /* In template mode, the uid and givenName are autogenerated... */
881 if (!$this->is_template){
882 if ($this->givenName == "" && chkacl ($this->acl, "givenName") == ""){
883 $message[]= _("The required field 'Given name' is not set.");
884 }
885 if ($this->uid == "" && chkacl ($this->acl, "uid") == ""){
886 $message[]= _("The required field 'Login' is not set.");
887 }
888 if (!(isset($this->config->current['DNMODE']) && $this->config->current['DNMODE'] == "uid")){
889 $ldap->cd($this->config->current['BASE']);
890 $ldap->search("(cn=".$this->cn.")", array("uid"));
891 $ldap->fetch();
892 if ($ldap->count() != 0 && $this->dn != $new_dn && $this->dn == 'new'){
893 $message[]= _("There's already a person with this 'Name'/'Given name' combination in the database.");
894 }
895 }
896 }
898 /* Check for valid input */
899 if (!is_uid($this->uid)){
900 $message[]= _("The field 'Login' contains invalid characters. Lowercase, numbers and dashes are allowed.");
901 }
902 if (!is_url($this->labeledURI)){
903 $message[]= _("The field 'Homepage' contains an invalid URL definition.");
904 }
905 if (preg_match ("/[\\\\]/", $this->sn)){
906 $message[]= _("The field 'Name' contains invalid characters.");
907 }
908 if (preg_match ("/[\\\\]/", $this->givenName)){
909 $message[]= _("The field 'Given name' contains invalid characters.");
910 }
912 /* Check phone numbers */
913 if (!is_phone_nr($this->homePhone)){
914 $message[]= _("The field 'Phone' contains an invalid phone number.");
915 }
916 if (!is_phone_nr($this->telephoneNumber)){
917 $message[]= _("The field 'Phone' contains an invalid phone number.");
918 }
919 if (!is_phone_nr($this->facsimileTelephoneNumber)){
920 $message[]= _("The field 'Fax' contains an invalid phone number.");
921 }
922 if (!is_phone_nr($this->mobile)){
923 $message[]= _("The field 'Mobile' contains an invalid phone number.");
924 }
925 if (!is_phone_nr($this->pager)){
926 $message[]= _("The field 'Pager' contains an invalid phone number.");
927 }
929 /* Check for reserved characers */
930 if (preg_match ('/[,+"<>;]/', $this->givenName)){
931 $message[]= _("The field 'Given name' contains invalid characters.");
932 }
933 if (preg_match ('/[,+"<>;]/', $this->sn)){
934 $message[]= _("The field 'Name' contains invalid characters.");
935 }
937 return $message;
938 }
941 /* Indicate whether a password change is needed or not */
942 function password_change_needed()
943 {
944 return ($this->pw_storage != $this->last_pw_storage);
945 }
948 /* Load a jpegPhoto from LDAP, this is going to be simplified later on */
949 function load_picture()
950 {
951 /* make connection and read jpegPhoto */
952 $ds= ldap_connect($this->config->current['SERVER']);
953 ldap_set_option($ds, LDAP_OPT_PROTOCOL_VERSION, 3);
954 if (function_exists("ldap_set_rebind_proc") && isset($this->config->current['RECURSIVE']) && $this->config->current['RECURSIVE'] == "true") {
955 ldap_set_option($this->cid, LDAP_OPT_REFERRALS, 1);
956 ldap_set_rebind_proc($ds, array(&$this, "rebind"));
957 }
959 if(isset($this->config->current['TLS']) &&
960 $this->config->current['TLS'] == "true"){
962 ldap_start_tls($ds);
963 }
965 $r= ldap_bind($ds);
966 $sr= @ldap_read($ds, $this->dn, "jpegPhoto=*", array("jpegPhoto"));
968 /* in case we don't get an entry, load a default picture */
969 $this->set_picture ("./images/default.jpg");
970 $this->jpegPhoto= "*removed*";
972 /* fill data from LDAP */
973 if ($sr) {
974 $ei=ldap_first_entry($ds, $sr);
975 if ($ei) {
976 if ($info = ldap_get_values_len($ds, $ei, "jpegPhoto")){
977 $this->photoData= $info[0];
978 $_SESSION['picture']= $this->photoData;
979 $this->jpegPhoto= "";
980 }
981 }
982 }
984 /* close conncetion */
985 ldap_unbind($ds);
986 }
989 /* Load a certificate from LDAP, this is going to be simplified later on */
990 function load_cert()
991 {
992 $ds= ldap_connect($this->config->current['SERVER']);
993 ldap_set_option($ds, LDAP_OPT_PROTOCOL_VERSION, 3);
994 if (function_exists("ldap_set_rebind_proc") && isset($this->config->current['RECURSIVE']) && $this->config->current['RECURSIVE'] == "true") {
995 ldap_set_option($this->cid, LDAP_OPT_REFERRALS, 1);
996 ldap_set_rebind_proc($ds, array(&$this, "rebind"));
997 }
998 if(isset($this->config->current['TLS']) &&
999 $this->config->current['TLS'] == "true"){
1001 ldap_start_tls($ds);
1002 }
1004 $r= ldap_bind($ds);
1005 $sr= @ldap_read($ds, $this->dn, "userCertificate=*", array("userCertificate"));
1007 if ($sr) {
1008 $ei= @ldap_first_entry($ds, $sr);
1010 if ($ei) {
1011 if (!$info = @ldap_get_values_len($ds, $ei, "userCertificate;binary")){
1012 $this->userCertificate= "";
1013 } else {
1014 $this->userCertificate= $info[0];
1015 }
1016 }
1017 } else {
1018 $this->userCertificate= "";
1019 }
1021 ldap_unbind($ds);
1022 }
1025 /* Load picture from file to object */
1026 function set_picture($filename)
1027 {
1028 if (!is_file($filename)){
1029 $filename= "./images/default.jpg";
1030 $this->jpegPhoto= "*removed*";
1031 }
1033 $fd = fopen ($filename, "rb");
1034 $this->photoData= fread ($fd, filesize ($filename));
1035 $_SESSION['picture']= $this->photoData;
1036 $this->jpegPhoto= "";
1038 fclose ($fd);
1039 }
1042 /* Load certificate from file to object */
1043 function set_cert($cert, $filename)
1044 {
1045 $fd = fopen ($filename, "rb");
1046 if (filesize($filename)>0) {
1047 $this->$cert= fread ($fd, filesize ($filename));
1048 fclose ($fd);
1049 $this->is_modified= TRUE;
1050 } else {
1051 print_red(_("Could not open specified certificate!"));
1052 }
1053 }
1055 /* Adapt from given 'dn' */
1056 function adapt_from_template($dn)
1057 {
1058 plugin::adapt_from_template($dn);
1060 /* Get base */
1061 $this->base= preg_replace('/^[^,]+,'.get_people_ou().'/i', '', $dn);
1063 if ($this->config->current['GOVERNMENTMODE']){
1065 /* Walk through govattrs */
1066 foreach ($this->govattrs as $val){
1068 if (isset($this->attrs["$val"][0])){
1070 /* If attribute is set, replace dynamic parts:
1071 %sn, %givenName and %uid. Fill these in our local variables. */
1072 $value= $this->attrs["$val"][0];
1074 foreach (array("sn", "givenName", "uid") as $repl){
1075 if (preg_match("/%$repl/i", $value)){
1076 $value= preg_replace ("/%$repl/i",
1077 $this->parent->$repl, $value);
1078 }
1079 }
1080 $this->$val= $value;
1081 }
1082 }
1083 }
1085 /* Get back uid/sn/givenName */
1086 if ($this->parent != NULL){
1087 $this->uid= $this->parent->uid;
1088 $this->sn= $this->parent->sn;
1089 $this->givenName= $this->parent->givenName;
1090 }
1091 }
1093 }
1095 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
1096 ?>