view src/kryshen/catalina/userconfig/PasswdUserConfig.java @ 18:1915c9c69129

Rename package.
author Mikhail Kryshen <mikhail@kryshen.net>
date Tue, 03 Nov 2009 03:42:30 +0300
parents
children
line source
1 /*
2 * Copyright 2009 Mikhail Kryshen
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
17 package kryshen.catalina.userconfig;
19 import java.io.BufferedReader;
20 import java.io.File;
21 import java.io.FileReader;
22 import java.io.IOException;
23 import java.io.InputStreamReader;
24 import java.util.regex.Pattern;
25 import org.apache.juli.logging.LogFactory;
27 /**
28 * Event listener for Host that deploys and updates applications
29 * provided by users listed in passwd database.
30 *
31 * @author Mikhail Kryshen
32 */
33 public class PasswdUserConfig extends UserConfig {
35 private static final String PASSWD_DATABASE = "passwd";
37 /* Passwd format details. */
38 private static final Pattern PASSWD_SPLIT_PATTERN = Pattern.compile(":");
39 private static final int PASSWD_FIELD_USERNAME = 0;
40 private static final int PASSWD_FIELD_HOME = 5;
42 /**
43 * Command for retrieving passwd database.
44 */
45 private String getent = null;
47 /**
48 * Path to local passwd file.
49 */
50 private String passwd = "/etc/passwd";
53 public PasswdUserConfig() {
54 log = LogFactory.getLog(PasswdUserConfig.class);
55 }
57 /**
58 * Get command for retrieving passwd database.
59 */
60 public String getGetent() {
61 return getent;
62 }
64 /**
65 * Set command for retrieving passwd database.
66 */
67 public void setGetent(String getent) {
68 this.getent = getent;
69 }
71 /**
72 * Get path to local passwd file.
73 */
74 public String getPasswd() {
75 return passwd;
76 }
78 /**
79 * Set path to local passwd file.
80 */
81 public void setPasswd(String passwd) {
82 this.passwd = passwd;
83 }
85 @Override
86 protected void deployUserApps() {
87 BufferedReader in;
89 try {
90 if (getent == null) {
91 in = new BufferedReader(new FileReader(passwd));
92 } else {
93 Process process = Runtime.getRuntime().exec(
94 new String[] {getent, PASSWD_DATABASE});
96 in = new BufferedReader(
97 new InputStreamReader(process.getInputStream()));
98 }
100 String line;
102 try {
103 while ((line = in.readLine()) != null) {
104 String[] fields = PASSWD_SPLIT_PATTERN.split(line);
106 String name = fields[PASSWD_FIELD_USERNAME];
107 File home = new File(fields[PASSWD_FIELD_HOME]);
109 if (!home.isDirectory()) {
110 //log.warn("Invalid home directory for user "
111 // + name + ": " + home.getPath() + ".");
112 continue;
113 }
115 deployUserApps(name, home);
116 }
117 } finally {
118 in.close();
119 }
120 } catch (IOException e) {
121 log.error("Error reading passwd database.", e);
122 } catch (ArrayIndexOutOfBoundsException e) {
123 log.error("Invalid passwd format.", e);
124 }
125 }
126 }