summaryrefslogtreecommitdiffstats
path: root/pki/base/java-tools/src/com/netscape/cmstools/PasswordCache.java
blob: e4d8106f7381c96cd94dae4bdf706659bc6e6754 (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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
// --- 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.cmstools;

import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FilterOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.io.UnsupportedEncodingException;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.StringTokenizer;

import org.mozilla.jss.CryptoManager;
import org.mozilla.jss.SecretDecoderRing.Decryptor;
import org.mozilla.jss.SecretDecoderRing.Encryptor;
import org.mozilla.jss.SecretDecoderRing.KeyManager;
import org.mozilla.jss.crypto.CryptoToken;
import org.mozilla.jss.crypto.TokenException;
import org.mozilla.jss.util.Base64OutputStream;
import org.mozilla.jss.util.Password;

/**
 * Tool for interacting with the PWcache
 * 
 * @version $Revision$, $Date$
 */

public class PasswordCache {

    /* These are the tags that identify various passwords 
     * They should probably be converted instances of some 
     * class so that we can expose an API to add additional 
     * TAG's for use if I want to add a password for use
     * with my own authenticaion module 
     */
    public static final String PROP_PWC_NICKNAME = "sso_key";
    public static final String PW_TAG_INTERNAL_LDAP_DB = "Internal LDAP Database";
    private static final String WRONG_NUM_ARGS = "Error: wrong number of arguments";
    private static final String CERTDB = "cert8.db";
    private static final String KEYDB = "key3.db";

    private static void usage() {
        System.out.println(
                "This tool has to be run from the same directory where pwcache.db file resides, normally <cms instance>/config directory, unless the file's full path is specified in the -c option..\nUsage: PasswordCache <SSO_PASSWORD> <-d cert/key db directory> <-h tokenName> <-P cert/key db prefix> <-c pwcache.db_file_full_path> <-k file containing Base64EncodedKeyID> <COMMAND> ...");
        System.out.println("  commands:");
        System.out.println("     'add <password_name> <password>'");
        System.out.println("     'change <password_name> <password>'");
        System.out.println("     'delete <password_name>'");
        System.out.println("     'rekey'");
        System.out.println("     'list'");
        System.out.println(
                "\nExample:\n\tPasswordCache thePassword1 -d /usr/netscape/servers/cms/alias -P cert-instance1-machine1- -c pwcache.db -k keyidFile list");
        System.exit(1);
    }

    private static boolean debugMode = false;

    public PasswordCache() {
    }

    private static void debug(String s) {
        if (debugMode == true)
            System.out.println("PasswordCache debug: " + s);
    }

    /**
     * clean up an argv by removing the trailing, empty arguments
     * 
     * This is necessary to support the script wrapper which calls the
     * tool with arguments in quotes such as:
     * "$1" "$2"
     * if $2 is not specified, the empty arg "" gets passed, which causes
     * an error in the arg-count checking code.
     */
    private static String[] cleanArgs(String[] s) {
        int length;
        int i;

        length = s.length;
        debug("before cleanArgs argv length =" + length);

        for (i = length - 1; i >= 0; i--) {
            if (s[i].equals("")) {
                length--;
            } else {
                break;
            }
        }

        String[] new_av = new String[length];
        for (i = 0; i < length; i++) {
            new_av[i] = s[i];
            debug("arg " + i + " is " + new_av[i]);
        }
        debug("after cleanArgs argv length =" + length);

        return new_av;
    }

    public static byte[] base64Decode(String s) throws IOException {
        byte[] d = com.netscape.osutil.OSUtil.AtoB(s);
        return d;
    }

    public static String base64Encode(byte[] bytes) throws IOException {
        // All this streaming is lame, but Base64OutputStream needs a
        // PrintStream
        ByteArrayOutputStream output = new ByteArrayOutputStream();
        Base64OutputStream b64 = new Base64OutputStream(new
                PrintStream(new
                        FilterOutputStream(output)
                )
                );

        b64.write(bytes);
        b64.flush();

        // This is internationally safe because Base64 chars are
        // contained within 8859_1
        return output.toString("8859_1");
    }

    public static void main(String[] av) {
        // default path is "."
        String mPath = ".";
        String mTokenName = null;
        // default prefix is ""
        String mPrefix = "";
        String mKeyIdString = null;
        byte[] mKeyId = null;
        String mCertDB = CERTDB;
        String mKeyDB = KEYDB;
        String mCacheFile = "pwcache.db";

        String pwdPath = null;
        String instancePath = null;
        String instanceName = null;

        String[] argv = cleanArgs(av);

        if (argv.length < 2) {
            usage();
        }

        String pw = argv[0];

        char[] testpw = pw.toCharArray();
        Password pass = new Password(testpw);

        String command = "";
        String aTag = "";
        String aPasswd = "";

        int i = 0;
        for (i = 1; i < argv.length; ++i) {
            if (argv[i].equals("-d")) {
                if (++i >= argv.length)
                    usage();
                mPath = argv[i];
            } else if (argv[i].equals("-h")) {
                if (++i >= argv.length)
                    usage();
                mTokenName = argv[i];
            } else if (argv[i].equals("-P")) {
                if (++i >= argv.length)
                    usage();
                mPrefix = argv[i];
            } else if (argv[i].equals("-c")) {
                if (++i >= argv.length)
                    usage();
                mCacheFile = argv[i];
            } else if (argv[i].equals("-k")) {
                if (++i >= argv.length)
                    usage();
                String keyFile = argv[i];
                try {
                    BufferedReader r = new BufferedReader(new FileReader(keyFile));
                    String listLine;
                    mKeyIdString = r.readLine();
                } catch (Exception e) {
                    System.out.println("Error: " + e.toString());
                    System.exit(1);
                }

                if (mKeyIdString != null) {
                    try {
                        mKeyId = base64Decode(mKeyIdString);
                        debug("base64Decode of key id string successful");
                    } catch (IOException e) {
                        System.out.println("base64Decode of key id string failed");
                        System.exit(1);
                    }
                }
            } else {
                command = argv[i++];
                debug("command = " + command);

                if ((command.equals("add")) ||
                        (command.equals("change"))) {
                    aTag = argv[i++];
                    aPasswd = argv[i];
                    debug("command is " + command + " " + aTag + ":" + aPasswd);
                } else if (command.equals("delete")) {
                    aTag = argv[i];
                } else if (command.equals("list")) {
                } else if (command.equals("rekey")) {
                }
                break;
            }
        }

        try {
            // initialize CryptoManager
            System.out.println("cert/key prefix = " + mPrefix);
            System.out.println("cert/key db path = " + mPath);
            System.out.println("password cache file = " + mCacheFile);

            CryptoManager.InitializationValues vals =
                    new CryptoManager.InitializationValues(mPath, mPrefix,
                            mPrefix, "secmod.db");

            CryptoManager.initialize(vals);

            CryptoManager cm = CryptoManager.getInstance();
            CryptoToken token = null;
            if (mTokenName == null) {
                token = cm.getInternalKeyStorageToken();
                System.out.println("token name = internal");
            } else {
                token = cm.getTokenByName(mTokenName);
                System.out.println("token name = " + mTokenName);
            }

            token.login(pass);
        } catch (Exception e) {
            e.printStackTrace();
            System.exit(1);
        }

        // generating new key
        if (command.equals("rekey")) {
            System.out.println("generating new key...");
            PWsdrCache cache = null;
            try {
                // compose instance name
                File passwordCacheDB = new File(mCacheFile);
                pwdPath = passwordCacheDB.getAbsolutePath();
                int beginIndex = pwdPath.lastIndexOf("cert-");
                instancePath = pwdPath.substring(beginIndex);
                int endIndex = 0;
                endIndex = instancePath.lastIndexOf("config");
                instanceName = instancePath.substring(0, (endIndex - 1));

                cache = new PWsdrCache(mCacheFile, mTokenName, null, true);
                cache.deleteUniqueNamedKey(PROP_PWC_NICKNAME
                                          + " "
                                          + instanceName);
                byte[] newKeyId = cache.generateSDRKeyWithNickName(
                                                        PROP_PWC_NICKNAME
                                                                + " "
                                                                + instanceName);
                if (newKeyId != null) {
                    String newKeyIDString = base64Encode(newKeyId);
                    System.out.println("key generated successfully with key id = " +
                                       newKeyIDString);
                    System.out.println("Save the VALUE portion of this key id in a local file,");
                    System.out.println("and under variable \"pwcKeyid\" in CS.cfg !!");
                    System.out.println("If you have not already done so,");
                    System.out.println("remove the old pwcache.db and use this local file to add passwords.");
                    // job is done
                    System.exit(0);
                } else {
                    System.out.println("key expected to be generated but wasn't");
                    System.exit(1);
                }
            } catch (Exception e) {
                System.out.println(e.toString());
                System.exit(1);
            }
        }

        PWsdrCache cache = null;
        try {
            cache = new PWsdrCache(mCacheFile, mTokenName, mKeyId, true);
        } catch (Exception e) {
            System.out.println(e.toString());
            System.exit(1);
        }

        if ((command.equals("add")) || (command.equals("change"))) {
            // current key id must be specified
            if (mKeyId == null) {
                System.out.println("operation failed: no key id specified");
                System.exit(1);
            }

            try {
                System.out.println("adding " + aTag + ":" + aPasswd);
                cache.addEntry(aTag, aPasswd);
            } catch (Exception e) {
                System.out.println("--failed--" + e.toString());
            }
        } else if (command.equals("list")) {
            cache.pprint();
        } else if (command.equals("delete")) {
            // current key id must be specified
            if (mKeyId == null) {
                System.out.println("operation failed: no key id specified");
                System.exit(1);
            }

            try {
                cache.deleteEntry(aTag);
            } catch (Exception e) {
                System.out.println("User not found");
            }
        } else {
            System.out.println("Illegal command: " + command);
            System.exit(1);
        }
    }
}

/* 
 * A class for managing passwords in the SDR password cache
 *
 * @author Christina Fu
 * @version $Revision$, $Date$
 */
class PWsdrCache {

    public static final String PROP_PWC_NICKNAME = "sso_key";

    private String mPWcachedb = null;
    private byte[] mKeyID = null;
    private String mTokenName = null;
    private CryptoToken mToken = null;

    // mTool tells if this is called from the PasswordCache tool
    private boolean mIsTool = false;

    // for PasswordCache tool (isTool == true)
    public PWsdrCache(String pwCache, String pwcTokenname, byte[] keyId,
                      boolean isTool) throws Exception {
        mPWcachedb = pwCache;
        mIsTool = isTool;
        mTokenName = pwcTokenname;
        CryptoManager cm = null;

        if (keyId != null) {
            mKeyID = keyId;
        }

        cm = CryptoManager.getInstance();
        if (mTokenName != null) {
            mToken = cm.getTokenByName(mTokenName);
            debug("PWsdrCache: mToken = " + mTokenName);
        } else {
            mToken = cm.getInternalKeyStorageToken();
            debug("PWsdrCache: mToken = internal");
        }
    }

    public byte[] getKeyId() {
        return mKeyID;
    }

    public String getTokenName() {
        return mTokenName;
    }

    public void deleteUniqueNamedKey(String nickName)
            throws Exception {
        KeyManager km = new KeyManager(mToken);
        km.deleteUniqueNamedKey(nickName);
    }

    public byte[] generateSDRKey() throws Exception {
        return generateSDRKeyWithNickName(PROP_PWC_NICKNAME);
    }

    public byte[] generateSDRKeyWithNickName(String nickName)
            throws Exception {
        try {
            if (mIsTool == true) {
                // generate SDR key
                KeyManager km = new KeyManager(mToken);
                try {
                    // Bugscape Bug #54838:  Due to the CMS cloning feature,
                    //                       we must check for the presence of
                    //                       a uniquely named symmetric key
                    //                       prior to making an attempt to
                    //                       generate it!
                    //
                    if (!(km.uniqueNamedKeyExists(nickName))) {
                        mKeyID = km.generateUniqueNamedKey(nickName);
                        debug("PWsdrCache: SDR key generated");
                    }
                } catch (TokenException e) {
                    log(0, "generateSDRKey() failed on " + e.toString());
                    throw e;
                }
            }
        } catch (Exception e) {
            log(0, e.toString());
            throw e;
        }
        return mKeyID;
    }

    public void addEntry(String tag, String pwd) throws IOException {
        addEntry(tag, pwd, (Hashtable<String, String>) null);
    }

    /*
     * Store passwd in pwcache.
     */
    public void addEntry(Hashtable<String, String> ht) throws IOException {
        addEntry((String) null, (String) null, ht);
    }

    /*
     * add passwd in pwcache.
     */
    public void addEntry(String tag, String pwd, Hashtable<String, String> tagPwds) throws IOException {
        System.out.println("PWsdrCache: in addEntry");
        String stringToAdd = null;
        String bufs = null;

        if (tagPwds == null) {
            stringToAdd = tag + ":" + pwd + "\n";
        } else {
            Enumeration<String> enum1 = tagPwds.keys();

            while (enum1.hasMoreElements()) {
                tag = enum1.nextElement();
                pwd = tagPwds.get(tag);
                debug("password tag: " + tag + " stored in " + mPWcachedb);

                if (stringToAdd == null) {
                    stringToAdd = tag + ":" + pwd + "\n";
                } else {
                    stringToAdd += tag + ":" + pwd + "\n";
                }
            }
        }

        String dcrypts = readPWcache();
        System.out.println("PWsdrCache: after readPWcache()");
        if (dcrypts != null) {
            // converts to Hashtable, replace if tag exists, add
            //                if tag doesn't exist
            Hashtable<String, String> ht = string2Hashtable(dcrypts);

            if (ht.containsKey(tag) == false) {
                debug("adding new tag: " + tag);
                ht.put(tag, pwd);
            } else {
                debug("replacing tag: " + tag);
                ht.put(tag, pwd);
            }
            bufs = hashtable2String(ht);
        } else {
            debug("adding new tag: " + tag);
            bufs = stringToAdd;
        }

        // write update to cache
        writePWcache(bufs);
    }

    /*
     * delete passwd in pwcache.
     */
    public void deleteEntry(String tag) throws IOException {
        String bufs = null;

        String dcrypts = readPWcache();

        if (dcrypts != null) {
            // converts to Hashtable, replace if tag exists, add
            //                if tag doesn't exist
            Hashtable<String, String> ht = string2Hashtable(dcrypts);

            if (ht.containsKey(tag) == false) {
                debug("tag: " + tag + " does not exist");
                return;
            } else {
                debug("deleting tag: " + tag);
                ht.remove(tag);
            }
            bufs = hashtable2String(ht);
        } else {
            debug("password cache contains no tags");
            return;
        }

        // write update to cache
        writePWcache(bufs);
    }

    /*
     * reads and decrypts the pwcache.db content
     */
    public String readPWcache() throws IOException {
        debug("about to read password cache");
        String dcrypts = null;
        if (mToken == null) {
            debug("mToken is null");
            throw new IOException("token must be specified");
        }

        Decryptor sdr = new Decryptor(mToken);

        // not used, but could used for debugging
        int totalRead = 0;
        FileInputStream inputs = null;
        ByteArrayOutputStream bos = new ByteArrayOutputStream();

        try {
            // for SDR -> read, decrypt, append, and write
            inputs = new FileInputStream(mPWcachedb);
            byte[] readbuf = new byte[2048]; // for now
            int numRead = 0;

            while ((numRead = inputs.read(readbuf)) != -1) {
                bos.write(readbuf, 0, numRead);
                totalRead += numRead;
            }
            inputs.close();
        } catch (FileNotFoundException e) {
            System.out.println("Failed for file " + mPWcachedb + " " + e.toString());
            throw new IOException(e.toString() + ": " + mPWcachedb);
        } catch (IOException e) {
            System.out.println("Failed for file " + mPWcachedb + " " + e.toString());
            throw new IOException(e.toString() + ": " + mPWcachedb);
        }

        if (totalRead > 0) {
            try {
                // decrypt it first to append
                byte[] dcryptb = sdr.decrypt(bos.toByteArray());

                dcrypts = new String(dcryptb, "UTF-8");
            } catch (TokenException e) {
                System.out.println("password cache decrypto failed " + e.toString());
                e.printStackTrace();
                throw new IOException("password cache decrypt failed");
            } catch (UnsupportedEncodingException e) {
                System.out.println("password cache decrypto failed " + e.toString());
                e.printStackTrace();
                throw new IOException("password cache decrypt failed");
            } catch (Exception e) {
                System.out.println("password cache decrypto failed " + e.toString());
                e.printStackTrace();
                throw new IOException("password cache decrypt failed");
            }
        }

        return dcrypts;
    }

    /*
     * encrypts and writes the whole String buf into pwcache.db
     */
    public void writePWcache(String bufs) throws IOException {

        try {
            Encryptor sdr = new Encryptor(mToken, mKeyID,
                                Encryptor.DEFAULT_ENCRYPTION_ALG);

            byte[] writebuf = null;

            try {
                // now encrypt it again
                writebuf = sdr.encrypt(bufs.getBytes("UTF-8"));
            } catch (Exception e) {
                System.out.println("password cache encrypt failed " + e.toString());
                e.printStackTrace();
                throw new IOException("password cache encrypt failed");
            }

            File tmpPWcache = new File(mPWcachedb + ".tmp");

            if (tmpPWcache.exists()) {
                // it wasn't removed?
                tmpPWcache.delete();
            }
            FileOutputStream outstream = new FileOutputStream(mPWcachedb + ".tmp");

            outstream.write(writebuf);
            outstream.close();

            // Make certain that this temporary file has
            // the correct permissions.
            if (!isNT()) {
                exec("chmod 00660 " + tmpPWcache.getAbsolutePath());
            }

            File origFile = new File(mPWcachedb);

            try {
                // Always remove any pre-existing target file
                if (origFile.exists()) {
                    origFile.delete();
                }

                if (isNT()) {
                    // NT is very picky on the path
                    exec("copy " +
                            tmpPWcache.getAbsolutePath().replace('/', '\\') + " " +
                            origFile.getAbsolutePath().replace('/', '\\'));
                } else {
                    // Create a copy of the temporary file which
                    // preserves the temporary file's permissions.
                    exec("cp -p " + tmpPWcache.getAbsolutePath() + " " +
                            origFile.getAbsolutePath());
                }

                // Remove the temporary file if and only if
                // the "rename" was successful.
                if (origFile.exists()) {
                    tmpPWcache.delete();

                    // Make certain that the final file has
                    // the correct permissions.
                    if (!isNT()) {
                        exec("chmod 00660 " + origFile.getAbsolutePath());
                    }

                    // report success
                    debug("Renaming operation completed for " + mPWcachedb);
                } else {
                    // report failure and exit
                    debug("Renaming operation failed for " + mPWcachedb);
                    System.exit(1);
                }
            } catch (IOException exx) {
                System.out.println("sdrPWcache: Error " + exx.toString());
                throw new IOException(exx.toString() + ": " + mPWcachedb);
            }
        } catch (FileNotFoundException e) {
            System.out.println("sdrPWcache: Error " + e.toString());
            throw new IOException(e.toString() + ": " + mPWcachedb);
        } catch (IOException e) {
            System.out.println("Failed for file " + mPWcachedb + " " + e.toString());
            throw new IOException(e.toString() + ": " + mPWcachedb);
        } catch (Exception e) {
            System.out.println("sdrPWcache: Error " + e.toString());
            throw new IOException(e.toString());
        }
    }

    public String hashtable2String(Hashtable<String, String> ht) {
        Enumeration<String> enum1 = ht.keys();
        String returnString = null;

        while (enum1.hasMoreElements()) {
            String tag = enum1.nextElement();
            String pwd = ht.get(tag);

            if (returnString == null) {
                returnString = tag + ":" + pwd + "\n";
            } else {
                returnString += tag + ":" + pwd + "\n";
            }
        }
        return returnString;
    }

    public Hashtable<String, String> string2Hashtable(String cache) {
        Hashtable<String, String> ht = new Hashtable<String, String>();

        // first, break into lines
        StringTokenizer st = new StringTokenizer(cache, "\n");

        while (st.hasMoreTokens()) {
            String line = (String) st.nextToken();
            // break into tag:password format for each line
            int colonIdx = line.indexOf(":");

            if (colonIdx != -1) {
                String tag = line.substring(0, colonIdx);
                String passwd = line.substring(colonIdx + 1,
                        line.length());

                ht.put(tag.trim(), passwd.trim());
            } else {
                //invalid format...log or throw...later
            }
        }
        return ht;
    }

    /*
     * get password from cache.  This one supplies cache file name
     */
    public Password getEntry(String fileName, String tag) {
        mPWcachedb = fileName;
        return getEntry(tag);
    }

    /*
     * if tag found with pwd, return it
     * if tag not found, return null, which will cause it to give up
     */
    public Password getEntry(String tag) {
        Hashtable<String, String> pwTable = null;
        String pw = null;

        debug("in getEntry, tag=" + tag);

        if (mPWcachedb == null) {
            debug("mPWcachedb file path name is not initialized");
            return null;
        }

        String dcrypts = null;

        try {
            dcrypts = readPWcache();
        } catch (IOException e) {
            System.out.println("dfailed readPWcache() " + e.toString());
            return null;
        }

        if (dcrypts != null) {
            // parse the cache
            String cache = dcrypts;

            // this is created and destroyed at each use
            pwTable = string2Hashtable(cache);
            debug("in getEntry, pw cache parsed");
            pw = (String) pwTable.get(tag);
        }

        if (pw != null) {
            debug("getEntry gotten password for " + tag);
            return new Password(pw.toCharArray());
        } else {
            System.out.println("getEntry did not get password for tag " + tag);
            return null;
        }
    }

    //copied from IOUtil.java
    /**
     * Checks if this is NT.
     */
    public static boolean isNT() {
        return ((File.separator).equals("\\"));
    }

    public static boolean exec(String cmd) throws IOException {
        try {
            String cmds[] = null;

            if (isNT()) {
                // NT
                cmds = new String[3];
                cmds[0] = "cmd";
                cmds[1] = "/c";
                cmds[2] = cmd;
            } else {
                // UNIX
                cmds = new String[3];
                cmds[0] = "/bin/sh";
                cmds[1] = "-c";
                cmds[2] = cmd;
            }
            Process process = Runtime.getRuntime().exec(cmds);

            process.waitFor();
            BufferedReader pOut = null;
            String l = null;

            if (process.exitValue() == 0) {

                /**
                 * pOut = new BufferedReader(
                 * new InputStreamReader(process.getInputStream()));
                 * while ((l = pOut.readLine()) != null) {
                 * System.out.println(l);
                 * }
                 **/
                return true;
            } else {

                /**
                 * pOut = new BufferedReader(
                 * new InputStreamReader(process.getErrorStream()));
                 * l = null;
                 * while ((l = pOut.readLine()) != null) {
                 * System.out.println(l);
                 * }
                 **/
                return false;
            }
        } catch (Exception e) {
            return false;
        }
    }

    public void debug(String msg) {
        System.out.println(msg);
    }

    public void log(int level, String msg) {
        System.out.println(msg);
    }

    /*
     * list passwds in pwcache.
     */
    public boolean pprint() {
        String dcrypts = null;

        try {
            dcrypts = readPWcache();
        } catch (IOException e) {
            System.out.println("failed readPWcache() " + e.toString());
            return false;
        }

        debug("----- Password Cache Content -----");

        if (dcrypts != null) {
            // first, break into lines
            StringTokenizer st = new StringTokenizer(dcrypts, "\n");

            while (st.hasMoreTokens()) {
                String line = (String) st.nextToken();
                // break into tag:password format for each line
                int colonIdx = line.indexOf(":");

                if (colonIdx != -1) {
                    String tag = line.substring(0, colonIdx);
                    String passwd = line.substring(colonIdx + 1,
                            line.length());

                    debug(tag.trim() +
                            " : " + passwd.trim());
                } else {
                    //invalid format...log or throw...later
                    debug("invalid format");
                }
            }
        } // else print nothing
        return true;
    }
}