KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > james > smtpserver > SaslProfile


1 /***********************************************************************
2  * Copyright (c) 2000-2004 The Apache Software Foundation. *
3  * All rights reserved. *
4  * ------------------------------------------------------------------- *
5  * Licensed under the Apache License, Version 2.0 (the "License"); you *
6  * may not use this file except in compliance with the License. You *
7  * may obtain a copy of the License at: *
8  * *
9  * http://www.apache.org/licenses/LICENSE-2.0 *
10  * *
11  * Unless required by applicable law or agreed to in writing, software *
12  * distributed under the License is distributed on an "AS IS" BASIS, *
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or *
14  * implied. See the License for the specific language governing *
15  * permissions and limitations under the License. *
16  ***********************************************************************/

17
18 package org.apache.james.smtpserver;
19
20 import cryptix.jce.provider.CryptixCrypto;
21 import cryptix.sasl.Base64;
22 import java.io.DataInputStream JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.io.PrintWriter JavaDoc;
25 import javax.security.sasl.*;
26 import org.apache.avalon.framework.logger.AbstractLogEnabled;
27 import org.apache.log4j.BasicConfigurator;
28
29 class SaslProfile extends AbstractLogEnabled {
30
31     private SaslServer server = null;
32     private DataInputStream JavaDoc in = null;
33     private PrintWriter JavaDoc out = null;
34
35     static {
36         // Set up a simple configuration that logs on the console.
37
BasicConfigurator.configure();
38
39         // Make use of Cryptix JCE and SASL libraries
40
java.security.Security.addProvider(new CryptixCrypto());
41         Sasl.setSaslClientFactory(new cryptix.sasl.ClientFactory());
42     }
43
44     SaslProfile(SaslServer _server, DataInputStream JavaDoc _in, PrintWriter JavaDoc _out) {
45         this.server = _server;
46         this.in = _in;
47         this.out = _out;
48     }
49     
50     boolean doAUTH(String JavaDoc initialResponse) {
51
52     // It receives a request from the client requesting authentication for
53
// a particular SASL mechanism, accompanied by an optional initial
54
// response.
55

56         try
57         {
58         // It processes the initial response and generates a challenge
59
// specific for the SASL mechanism to be sent back to the client if
60
// the response is processed successfully. If the response is not
61
// processed successfully, it sends an error to the client and
62
// terminates the authentication session.
63

64             byte[] challenge = null;
65             byte[] response = null;
66
67             challenge =
68                 server.evaluateResponse(Base64.decode(initialResponse));
69             System.err.println("1");
70             if (challenge != null) {
71                 System.err.println("334 "+Base64.encode(challenge));
72                 out.println("334 "+Base64.encode(challenge));
73             }
74             else {
75                 if (server.isComplete()) {
76                     return true;
77                 } else {
78                     System.err.println("334 ");
79                     out.println("334 ");
80                 }
81             }
82
83         // Responses/challenges are exchanged with the client. If the
84
// server cannot successful process a response, the server sends an
85
// error to the client and terminates the authentication. If the
86
// server has completed the authentication and has no more
87
// challenges to send, it sends a success indication to the client.
88

89             System.err.println("2");
90       
91             do {
92                 try {
93                     System.err.println("3");
94                     String JavaDoc input = in.readLine().trim();
95                     System.err.println("input: '"+input+"'");
96         
97                     if (server.isComplete()) return true;
98
99                     challenge = server.evaluateResponse(Base64.decode(input));
100                     if (challenge != null) {
101                         System.err.println("334 "+Base64.encode(challenge));
102                         out.println("334 "+Base64.encode(challenge));
103                     }
104                     else {
105                        if (server.isComplete()) {
106                            return true;
107                        } else {
108                            System.err.println("334 ");
109                            out.println("334 ");
110                        }
111                     }
112                 }
113                 catch (IOException JavaDoc e) {
114                     System.err.println("IOException: "+e.toString());
115                     return false;
116                 }
117              } while (!server.isComplete());
118       
119             return true;
120
121         // If the authentication has completed successfully, the server
122
// extracts the authorization ID of the client from the SaslServer
123
// instance (if appropriate) to be used for subsequent access
124
// control checks.
125

126         // For the rest of the session, messages to and from the client are
127
// encoded and decoded using the input and output streams that
128
// encapsulate the negotiated security layer (if any).
129

130         }
131         catch (SaslException e) {
132             System.err.println("SaslException: "+e.toString());
133             return false;
134         }
135     }
136
137 }
138
139
Popular Tags