/* * Copyright (c) 2001 Matthew Feldt. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided the copyright notice above is * retained. * * THIS SOFTWARE IS PROVIDED ''AS IS'' AND WITHOUT ANY EXPRESSED OR * IMPLIED WARRANTIES. */ /** * PasswordManagerService.java * * Java Examples In A Nutshell Copyright (c) 2000 David Flanagan * Exercise 6-2: * Write a network service and client that allows a user to change his current * pasword that is registered with your PasswordManager class. If you've read * Chapter 16, Remote Method Invocation, modify PasswordManager so that it * runs as a RMI remote object and write a client program that uses the remote * object to change a pasword. If you have not read that chapter yet, write the * password-changing service to run under the Server class developed in Chapter * 5 and use the GenericClient class from that same chapter to interact with * the service. In either case, create a security policy file that defines the set * of permissions required by your network service, and use this policy file to * enable your service to run with the -Djava.security.manager option to the * Java interpreter. * * @author Matthew Feldt * @version 1.0, 03/20/2001 18:09 */ package com.feldt.examples.security; import com.davidflanagan.examples.net.Server; import java.io.*; import java.util.*; import java.security.*; public class PasswordManagerService implements Server.Service { static final String OK = "+OK "; static final String ERR = "-ERR "; static final String COMMANDS = "Invalid command; valid commands: ADD, DEL, MDFY, QUIT"; public void serve(InputStream i, OutputStream o) throws IOException { BufferedReader in = new BufferedReader(new InputStreamReader(i)); PrintWriter out = new PrintWriter(o); PasswordManager pwm = new PasswordManager("password"); String line, username, password, newPassword; // print welcome banner out.print(OK + "Password Manager Service 1.0 server ready. \r\n"); out.flush(); while (true) { line = in.readLine(); StringTokenizer tokenizer = new StringTokenizer(line); if (! tokenizer.hasMoreTokens()) continue; // input was empty // Get first word of the input and convert to lower case String command = tokenizer.nextToken().toLowerCase(); if (command.equals("add")) { if (tokenizer.countTokens() != 2) { out.print(ERR + "ADD \r\n"); } else { username = tokenizer.nextToken(); password = tokenizer.nextToken(); try { if (pwm.create(username, password) == false) throw new Exception("create failed."); pwm.store(); out.print(OK + command + " " + username + "\r\n"); } catch (Exception e) { out.println(ERR + "PasswordManager: " + e.getMessage()); } } } else if (command.equals("del")) { if (tokenizer.countTokens() != 2) { out.print(ERR + "DEL \r\n"); } else { username = tokenizer.nextToken(); password = tokenizer.nextToken(); try { if (pwm.delete(username, password) == false) throw new Exception("DEL failed, " + "invalid password or no such user."); pwm.store(); out.print(OK + command + " " + username + "\r\n"); } catch (Exception e) { out.println(ERR + "PasswordManager: " + e.getMessage()); } } } else if (command.equals("mdfy")) { if (tokenizer.countTokens() != 3) { out.print(ERR + "MDFY \r\n"); } else { username = tokenizer.nextToken(); password = tokenizer.nextToken(); newPassword = tokenizer.nextToken(); try { if (pwm.modify(username, password, newPassword) == false) throw new Exception("MDFY failed, " + "invalid password or no such user."); pwm.store(); out.print(OK + command + " " + username + "\r\n"); } catch (Exception e) { out.println(ERR + "PasswordManager: " + e.getMessage()); } } } else if (command.equals("quit")) { break; } else { out.print(ERR + COMMANDS + "\r\n"); } out.flush(); } // print farewell banner and close connections out.print(OK + "Password Manager Service 1.0 server signing off. \r\n"); out.close(); in.close(); } }