Code

Cleaned up code
[gosa.git] / include / heimdal / mkey.php
1 <?php
2 /*
3  * mkey.php,v 1.0 2006/08/25 21:00:00
4  *
5  * Copyright 2006 Alejandro Escanero Blanco <aescanero@chaosdimension.org>
6  *
7  * See the enclosed file COPYING for license information (GPL).  If you
8  * did not receive this file, see http://www.fsf.org/copyleft/gpl.html.
9  */
12 class entry{
13         var $timestamp;
14         var $vno;
15         var $keytype;
16         var $data;
17 };
19 class principal{
20         var $len;
21         var $realm;
22         var $name_type;
23         var $entry;
24         var $vno;
25 }
28 class mkey{
30         var $pvno; //version kerberos
31         var $tag;       //tag (id->version)
33         var $buf;               
34         var $mknvo;
35         var $realm;
36         var $ix_i;
37         var $ix;
38         var $fsz;
39         var $principals;
40         var $principals_l;
42         //function mkey(){};
44         function mkey($file){
45                 $this->ix=0;
46                 $this->ix_i=0;
47                 $this->buf="";
48                 $this->principals=array();
49                 $this->principals_l=0;
50                 $this->fsz=filesize($file);
51                 $mk=fopen($file,"r");
52                 if(!$mk){
53                         echo "no puedo abrir mkey";
54                         exit(1);
55                 }
56                 while(!feof($mk)){
57                         $this->buf.=fgets($mk,100);
58                 }
60                 /*print "DECODE:: \n";
61                 for ($i=0;$i<strlen($this->buf);$i++){ printf("%d %02x (%d) ",$i,ord(substr($this->buf,$i,1)),ord(substr($this->buf,$i,1)));echo substr($this->buf,$i,1)."\n";}*/
64                 //Begin procedure
66                 $this->pvno=$this->getdec(1);//00 version
67                 $this->tag=$this->getdec(1);//01 tag
68                 while($this->ix_i<$this->fsz){
69                         $this->mkeyPrincipal();
70                 }
71         }
73         function mkeyPrincipal(){
74                 $this->principals[]=new principal();
75                 $this->principals[$this->principals_l]->len=$this->getdec(4); // len
76                 $tmp=$this->getdec(2);// tmp
77                 $sz=$this->getdec(2);// size
78                 $this->principals[$this->principals_l]->realm=$this->getstr($sz);//realm
79                 $sz=$this->getdec(2);//size
80                 $data=$this->getstr($sz);               //Must be "K"
81                 $sz=$this->getdec(2);//size
82                 $data=$this->getstr($sz);               //Must be "M"
83                 $this->principals[$this->principals_l]->name_type=$this->getdec(4);//name type
84                 $this->principals[$this->principals_l]->entry=$this->mkeyEntry();//entry
85                 $this->principals[$this->principals_l]->vno=$this->getdec(4);//vno
86                 $this->principals_l++;
87         }
89         function mkeyEntry(){
90                 $local_entry=new entry();
91                 $local_entry->timestamp=$this->getdec(4);
92                 $local_entry->vno=$this->getdec(1);
93                 $local_entry->keytype=$this->getdec(2);
94                 $len=$this->getdec(2);
95                 $local_entry->data=$this->getstr($len);
96                 return($local_entry);
97         }
99         function getKeyType_Entry($pos=0){
100                 if($pos>($this->principals_l-1)){ echo "mkey error"; exit(-1); }
101                 return($this->principals[$pos]->entry->keytype);
102         }
104         function getKey_Entry($pos=0){
105                 if($pos>($this->principals_l-1)){ echo "mkey error"; exit(-1); }
106                 return($this->principals[$pos]->entry->data);
107         }
109         function getKey_Entry_A($pos=0){
110                 if($pos>($this->principals_l-1)){ echo "mkey error"; exit(-1); }
111                 $masterkey=array(strlen($mkey1));
112                 for ($i=0;$i<(strlen($mkey1));$i++){
113                         $masterkey[$i]=ord(substr($this->principals[$pos]->entry->data,$i,1));
114                 }
115                 return($masterkey);
116         }
118         function getNumber_Entry(){
119                 return($this->principals_l);
120         }
122         function getdec($len){
123                 $res=0;
124                 while ($len>0){
125                         $res*=0xff;
126                         $res+=ord(substr($this->buf,$this->ix_i,1));
127                         $this->ix_i++;
128                         $len--;
129                 }
130                 return($res);
131         }
133         function getstr($len){
134                 $res="";
135                 while ($len>0){
136                         $res.=substr($this->buf,$this->ix_i,1);
137                         $this->ix_i++;
138                         $len--;
139                 }
140                 return($res);
141         }
144 };
146 ?>