import cyrus-sasl-2.1.23
[cyrus-sasl.git] / java / CyrusSasl / GenericCommon.java
1 package CyrusSasl;
2
3 import java.util.Hashtable;
4 import java.net.*;
5
6 /**
7  * @version 1.0
8  * @author Tim Martin
9  */
10
11 public abstract class GenericCommon
12 {
13
14   /* These are the jni functions called by the routines in common
15    * see javasasl.c for their implementations
16    */
17
18     private native void jni_sasl_set_prop_string(int ptr, int propnum, String value);
19     private native void jni_sasl_set_prop_int(int ptr, int propnum, int value);
20     private native void jni_sasl_set_prop_bytes(int ptr, int propnum, byte[] value);
21     private native void jni_sasl_set_server(int ptr, byte []ipnum, int port);
22     private native void jni_sasl_set_client(int ptr, byte []ipnum, int port);
23     private native void jni_sasl_setSecurity(int ptr, int minssf, int maxssf);
24     private native int jni_sasl_getSecurity(int ptr);
25     private native byte[] jni_sasl_encode(int ptr, byte[] in,int len);
26     private native byte[] jni_sasl_decode(int ptr, byte[] in,int len);
27     private native void jni_sasl_dispose(int ptr);
28
29   /**
30    * security layer security strength factor
31    */
32   public static int SASL_SSF      =1;    
33
34   public static int SASL_MAXOUTBUF=2;     /* security layer max output buf unsigned */  
35   public static int SASL_REALM    =3;     /* server authentication realm used */
36   public static int SASL_GETOPTCTX=4;     /* context for getopt callback */
37
38
39     /**
40      * Local sockaddr_in (use setServer and setClient to set this)
41      */
42   public static int SASL_IP_LOCAL =5;   
43
44     /**
45      * Remote sockaddr_in (use setClient and setServer to set this)
46      */
47
48   public static int SASL_IP_REMOTE =6;  
49
50     /**
51      * External security factor (use setSecurity to set this)
52      */
53   public static int SASL_SSF_EXTERNAL=100;  
54   public static int SASL_SEC_PROPS   =101;  /* sasl_security_properties_t */
55
56
57     int ptr;                    // this is the actual pointer to sasl_conn_t
58     int ssfactive;              // active ssf on this connection
59
60   boolean finished;
61
62   public boolean done() { return finished; }
63
64   /**
65    * Set a SASL property that takes a string value
66    *
67    * @param PROPERTY one of the property constants
68    * @param value string value
69    */
70
71   public void setproperty(int PROPERTY, String value)
72   {
73     jni_sasl_set_prop_string(ptr,PROPERTY,value);
74   }
75
76   /**
77    * Set a SASL property that takes a integer value
78    *
79    * @param PROPERTY one of the property constants
80    * @param value integer value
81    */
82
83   public void setproperty(int PROPERTY, int value)
84   {
85     jni_sasl_set_prop_int(ptr,PROPERTY,value);
86   }
87
88   /**
89    * Set a SASL property that takes a byte[] value
90    *
91    * @param PROPERTY one of the property constants
92    * @param value byte[] value
93    */
94
95   public void setproperty(int PROPERTY, byte[] value)
96   {
97     jni_sasl_set_prop_bytes(ptr,PROPERTY,value);
98   }
99
100   /**
101    * Set the SASL properties for the server
102    * This sets the IP address and port
103    *
104    * @param name String of name of server (e.g. cyrus.andrew.cmu.edu)
105    * @param port port connected to on that server
106    */
107
108   private boolean setRemoteIP(String name,int port)
109   {
110     byte[]ip=null;
111     try {
112       InetAddress server=InetAddress.getByName(name);
113       ip=server.getAddress();    
114     } catch (UnknownHostException e) { 
115       return false;
116     }
117
118     jni_sasl_set_server(ptr, ip, port);
119     return true;
120   }
121
122   /**
123    * Set the SASL properties for the client
124    * This sets the IP address and port
125    *
126    * @param name String of local cannonical name (e.g. myhostname.andrew.cmu.edu)
127    * @param port port connecting
128    */
129
130   private boolean setLocalIP(String name, int port)
131   {
132     byte[]ip=null;
133     try {
134       InetAddress server=InetAddress.getByName(name);
135       ip=server.getAddress();    
136     } catch (UnknownHostException e) { 
137       return false;
138     }
139
140     jni_sasl_set_client(ptr, ip, port);
141     return true;
142   }
143
144   /**
145    * Set the SASL properties for the client
146    * This sets the IP address and port
147    *
148    * @param local local InetAdress
149    * @param port port connecting
150    */
151
152   public boolean setClient(InetAddress local,int port)
153   {
154     byte[]ip=local.getAddress();
155
156     jni_sasl_set_client(ptr, ip, port);
157
158     return true;
159   }
160
161   /**
162    * Set the SASL properties for the client
163    * This sets the IP address and port
164    * The local IP address is determined with InetAddress.getLocalHost()
165    *
166    * @param port port connecting
167    */
168
169   public boolean setClient(int port)
170   {
171     try {
172       return setClient(InetAddress.getLocalHost(),port);
173     } catch (UnknownHostException e) {
174       return false;
175     }
176   }
177
178   /**
179    * Sets the security properties for the session
180    *
181    * @param external external security strength
182    * @param minssf minimum security needed
183    * @param maxssf maximum security to negotiate
184    *
185    * @return if the propery was set sucessfully or not
186    */
187
188
189     public boolean setSecurity(int external, int minssf, int maxssf)
190     {
191         /* setproperty(SASL_SSF_EXTERNAL, external); */
192         
193         jni_sasl_setSecurity(ptr,minssf,maxssf);
194         
195         return true;
196     }
197
198     public int getSecurity() {
199         return jni_sasl_getSecurity(ptr);
200     }
201
202   /**
203    * Encode a String with the negotiated layer
204    *
205    * @param in String to be encoded
206    * @return the encoded string represented at a byte[] 
207    */
208   public byte[] encode(byte[] in)
209   {
210     
211     byte[] out=jni_sasl_encode(ptr,in,in.length);
212
213     return out;
214   }
215                                             
216   /**
217    * Decode a byte[] with the negotiated layer
218    *
219    * @param in byte[] to be decoded
220    * @param len number of bytes to be decoded
221    * @return the decoded string represented at a byte[]
222    */
223   public byte[] decode(byte[] in, int len)
224   {
225     
226     byte[] out=jni_sasl_decode(ptr,in,len);
227
228     return out;
229   }
230
231   /**
232    * Decode a String with the negotiated layer. NOTE: Be careful with
233    * this function. International or high ascii characters may do strange
234    * things. The byte[] method is preferred
235    *
236    * @param in String to be decoded
237    * @return the decoded string represented at a byte[]
238    */
239   public byte[] decode(String in)
240   {
241     return decode(in.getBytes(),in.length());
242   }
243
244   protected void setcommonproperties(Hashtable props)
245   {
246       int i_ssfmin = 0;
247       String s_ssfmin=(String) props.get("javax.security.sasl.encryption.minimum");
248       if (s_ssfmin!=null) i_ssfmin = Integer.parseInt(s_ssfmin);
249
250       int i_ssfmax = 256;
251       String s_ssfmax=(String) props.get("javax.security.sasl.encryption.maximum");
252       if (s_ssfmax!=null) i_ssfmax = Integer.parseInt(s_ssfmax);
253
254       
255       int i_external = 0;
256       /*      String external=(String) props.getProperty("security.policy.encryption.external",
257        */
258
259       setSecurity(i_external,
260                   i_ssfmin,
261                   i_ssfmax);
262       
263       String iplocal  = (String) props.get("javax.security.sasl.ip.local");     
264       if (iplocal!=null) setLocalIP(iplocal,0);
265
266       String ipremote = (String) props.get("javax.security.sasl.ip.remote");
267       if (ipremote!=null) setRemoteIP(ipremote,0);
268    
269       /*    String maxbuf=props.getProperty("security.maxbuf","65000"); */
270     /* xxx this raises an exception for some reason
271        setproperty(SASL_MAXOUTBUF,Integer.parseInt(maxbuf)); */
272   }
273
274
275     final protected void finalize () throws Throwable 
276     {
277         jni_sasl_dispose(ptr);
278     }
279
280     protected boolean complete = false;
281
282     public boolean isComplete()
283     {
284         return complete;
285     }
286
287     /* called by JNI layer */
288     public void setcomplete(int a)
289     {
290         complete = true;
291     }
292
293
294 }