summaryrefslogtreecommitdiffstats
path: root/pki/base/common/src/com/netscape/cmscore/security/PWCBsdr.java
blob: 299b9f6e636123702530da8a5ac18b13065be704 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
// --- BEGIN COPYRIGHT BLOCK ---
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; version 2 of the License.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License along
// with this program; if not, write to the Free Software Foundation, Inc.,
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
//
// (C) 2007 Red Hat, Inc.
// All rights reserved.
// --- END COPYRIGHT BLOCK ---
package com.netscape.cmscore.security;


import org.mozilla.jss.*;
import org.mozilla.jss.crypto.*;
import org.mozilla.jss.crypto.SecretDecoderRing;
import org.mozilla.jss.crypto.TokenException;
import java.io.*;
import java.lang.*;
import java.util.Hashtable;
import java.util.StringTokenizer;
import org.mozilla.jss.util.Password;
import org.mozilla.jss.util.PasswordCallback;
import org.mozilla.jss.util.PasswordCallbackInfo;
import com.netscape.cmscore.base.*;
import com.netscape.certsrv.apps.CMS;
import com.netscape.certsrv.logging.ILogger;


/* 
 * A class to retrieve passwords from the SDR password cache
 *
 * @author Christina Fu
 * @version $Revision$, $Date$
 */

public class PWCBsdr implements PasswordCallback {
    InputStream in = null;
    OutputStream out = null;
    String mprompt = "";  
    boolean firsttime = true;
    private PasswordCallback mCB = null;
    private String mPWcachedb = null;
    private ILogger mLogger = null;

    public PWCBsdr() {
        this(null);
    }
  
    public PWCBsdr(String prompt) {
        in = System.in;
        out = System.out;
        mprompt = prompt;

        /* to get the test program work
         System.out.println("before CMS.getLogger");
         try {
         */
        mLogger = CMS.getLogger();

        /*
         } catch (NullPointerException e) {
         System.out.println("after CMS.getLoggergot NullPointerException ... testing ok");
         }
         System.out.println("after CMS.getLogger");
         */
        // get path to password cache
        try {
            mPWcachedb = CMS.getConfigStore().getString("pwCache");
            CMS.debug("got pwCache from configstore: " +
                mPWcachedb);
        } catch (NullPointerException e) {
            System.out.println("after CMS.getConfigStore got NullPointerException ... testing ok");
        } catch (Exception e) {
            log(ILogger.LL_FAILURE, CMS.getLogMessage("CMSCORE_SECURITY_GET_CONFIG"));
            // let it fall through
        }

        //    System.out.println("after CMS.getConfigStore");
        if (File.separator.equals("/")) {  
            // Unix
            mCB = new PWsdrConsolePasswordCallback(prompt);
        } else {
            mCB = new PWsdrConsolePasswordCallback(prompt);
            // mCB = new PWsdrDialogPasswordCallback( prompt );
        }

        // System.out.println( "Created PWCBsdr with prompt of "
        //                   + mprompt );
    }

    /* We are now assuming that PasswordCallbackInfo.getname() returns 
     * the tag we are hoping to match in the cache.
     */

    public Password getPasswordFirstAttempt(PasswordCallbackInfo info)
        throws PasswordCallback.GiveUpException {

        CMS.debug("in getPasswordFirstAttempt");

        /* debugging code to see if token is logged in
         try {
         CryptoManager cm = CryptoManager.getInstance();
         CryptoToken token =
         cm.getInternalKeyStorageToken();
         if (token.isLoggedIn() == false) {
         // missed it.
         CMS.debug("token not yet logged in!!");
         } else {
         CMS.debug("token logged in.");
         }
         } catch (Exception e) {
         CMS.debug("crypto manager error:"+e.toString());
         }
         CMS.debug("still in getPasswordFirstAttempt");
         */
        Password pw = null;
        String tmpPrompt = info.getName();

        String skip_token = System.getProperty("cms.skip_token");

        if (skip_token != null) {
            if (tmpPrompt.equals(skip_token)) {
                throw new PasswordCallback.GiveUpException();
            }
        }

        try {
            String defpw = System.getProperty("cms.defaultpassword");

            if (defpw != null) {
                return new Password(defpw.toCharArray());
            }

            /* mprompt has precedence over info.name */
            if (!(mprompt == null)) {
                tmpPrompt = mprompt;
            }

            if (tmpPrompt == null) { /* no name, fail */
                System.out.println("Shouldn't get here");
                throw new PasswordCallback.GiveUpException();
            } else {  /* get password from password cache */

                CMS.debug("getting tag = " + tmpPrompt);
                PWsdrCache pwc = new PWsdrCache(mPWcachedb, mLogger);

                pw = pwc.getEntry(tmpPrompt);

                if (pw != null) {
                    CMS.debug("non-null password returned in first attempt");
                    String tmp = new String(pw.getCharCopy());

                    return (pw);
                } else { /* password not found */
                    // we don't want caller to do getPasswordAgain,    for now
                    log(ILogger.LL_FAILURE, CMS.getLogMessage("CMSCORE_SECURITY_THROW_CALLBACK"));
                    throw new PasswordCallback.GiveUpException();
                }
            }
        } catch (Throwable e) {
            // System.out.println( "BUG HERE!!!!first!!!!!!!!!!!!!!!!!" );
            // e.printStackTrace();
            throw new PasswordCallback.GiveUpException();
        }
    }

    /* The password cache has failed to return a password (or a usable password.
     * Now we will try and get the password from the user and hopefully add
     * the password to the cache pw cache 
     */
    public Password getPasswordAgain(PasswordCallbackInfo info)
        throws PasswordCallback.GiveUpException {

        CMS.debug("in getPasswordAgain");
        try {
            Password pw = null;

            try {
                if (firsttime) {
                    try {
                        firsttime = false;

                        pw = mCB.getPasswordFirstAttempt(info);
                    } catch (PasswordCallback.GiveUpException e) {
                        throw new PasswordCallback.GiveUpException();
                    }
                } else {
                    pw = mCB.getPasswordAgain(info);
                }
                return (pw);
            } catch (PasswordCallback.GiveUpException e) {
                throw new PasswordCallback.GiveUpException();
            }
        } catch (Throwable e) {
            // System.out.println( "BUG HERE!! in the password again!!"
            //                   + "!!!!!!!!!!!" );
            // e.printStackTrace();
            throw new PasswordCallback.GiveUpException();
        }
    }

    public void log(int level, String msg) {
        if (mLogger == null) {
            System.out.println(msg);
        } else {
            mLogger.log(ILogger.EV_SYSTEM, ILogger.S_OTHER, level, "PWCBsdr " + msg); 
        }
    }
}


class PWsdrConsolePasswordCallback implements PasswordCallback {
    private String mPrompt = null;

    public PWsdrConsolePasswordCallback(String p) {
        mPrompt = p;
    }

    public String getPrompt() {
        return mPrompt;
    }

    public Password getPasswordFirstAttempt(PasswordCallbackInfo info)
        throws PasswordCallback.GiveUpException {
        if (mPrompt == null) {
            System.out.println("Get password " + info.getName());
        } else {
            System.out.println(getPrompt());
        }

        Password tmppw = PWUtil.readPasswordFromStream();

        return (tmppw);
    }

    public Password getPasswordAgain(PasswordCallbackInfo info)
        throws PasswordCallback.GiveUpException {
        System.out.println("Password Incorrect.");
        if (mPrompt == null) {
            System.out.println("Get password " + info.getName());
        } else {
            System.out.println(getPrompt());
        }

        Password tmppw = PWUtil.readPasswordFromStream();

        return (tmppw);
    }
}


class PWsdrDialogPasswordCallback extends JDialogPasswordCallback {
    private String mPrompt = null;

    public PWsdrDialogPasswordCallback(String p) {
        super();
        mPrompt = p;
    }

    public String getPrompt(PasswordCallbackInfo info) {
        if (mPrompt == null) {
            return super.getPrompt(info);
        } else {
            return mPrompt;
        }
    }
}