1   package net.sourceforge.heracles.testUtilities;
2   
3   import java.util.ArrayList;
4   import java.util.Hashtable;
5   import java.util.Iterator;
6   import java.util.List;
7   
8   import javax.naming.Context;
9   import javax.naming.NameClassPair;
10  import javax.naming.NamingEnumeration;
11  import javax.naming.directory.Attribute;
12  import javax.naming.directory.Attributes;
13  import javax.naming.directory.BasicAttribute;
14  import javax.naming.directory.BasicAttributes;
15  import javax.naming.directory.DirContext;
16  import javax.naming.directory.InitialDirContext;
17  import javax.naming.directory.ModificationItem;
18  
19  import org.apache.log4j.Logger;
20  /**
21   * Title:            CreateTestData.java<br>
22   * Copyright:        HFT-SO<br>
23   * Java Version:     JDK 1.5<br>
24   *
25   * @author Philipp Gantert
26   * @version 1.0
27   */
28  public class CreateTestData {
29  
30  	private Logger logger = Logger.getLogger(getClass());
31  
32  	public static final String OUNAME = "HeraclesOU";
33  
34  	public static final String PASSWORD = "heracles";
35  
36  	public static final String DOMAIN = "HFT-SO.BBZ-SG.LAN";
37  	
38  	public static final String REALM= "@HFT-SO.BBZ-SG.LAN";
39  
40  	public static final String SURNAME = "heracles";
41  
42  	public static final String GIVENNAME = "user";
43  
44  	public static final String DESCRIPTION = "Test User of Heracles Project";
45  
46  	public static final String TELEPHONENUMBER = "+41 32 666 44 9";
47  
48  	public static final String COMPANY = "Company";
49  
50  	public static final String DEPARTMENT = "Development";
51  
52  	public static final String TITLE = "Master";
53  
54  	public static final String STREETADDRESS = "Sportstrasse 2";
55  
56  	public static final String POSTALCODE = "2540";
57  
58  	public static final String LOCATION = "Grenchen";
59  
60  	public static final String STATE = "Solothurn";
61  
62  	public static final String COUNTRY = "CH";
63  
64  	public static final String HOMEPAGE = "Homepage";
65  
66  	public static final String MAIL = "@heracles.sourceforge.net";
67  
68  	private Hashtable<String, String> envoierment = new Hashtable<String, String>(11);
69  
70  	private static DirContext ctx = null;
71  
72  	private List testData = new ArrayList();;
73  
74  	private int valueUsers;
75  
76  	private int valueHaveMail;
77  
78  	private int valueHavePhone;
79  
80  	private int valueOfDisabledAccounts;
81  
82  	/**
83  	 * @param valueUsers
84  	 * @param valueHaveMail
85  	 * @param valueHavePhone
86  	 * @param valueOfDisabledAccounts
87  	 */
88  	public CreateTestData(int valueUsers, int valueHaveMail, int valueHavePhone, int valueOfDisabledAccounts) throws Exception {
89  		this.valueUsers = valueUsers;
90  		this.valueHaveMail = valueHaveMail;
91  		this.valueHavePhone = valueHavePhone;
92  		this.valueOfDisabledAccounts = valueOfDisabledAccounts;
93  
94  		envoierment.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
95  		envoierment.put(Context.PROVIDER_URL, "ldaps://hftso1.hft-so.bbz-sg.lan:636/DC=HFT-SO,DC=BBZ-SG,DC=LAN");
96  		envoierment.put(Context.SECURITY_PRINCIPAL, "administrator@HFT-SO.BBZ-SG.LAN");
97  		envoierment.put(Context.SECURITY_CREDENTIALS, "!!!Sowieso!!!");
98  
99  		// System.setProperty("javax.net.debug", "all");
100 		envoierment.put(Context.SECURITY_AUTHENTICATION, "simple");
101 		envoierment.put(Context.SECURITY_PROTOCOL, "ssl");
102 
103 		// THE LOCATION OF THE CACERTS MUST BE SPECIFIED
104 		java.security.Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
105 		System.setProperty("javax.net.ssl.keyStore", this.getClass().getResource("/.keystore").toString().replace("file:/", ""));
106 		System.setProperty("javax.net.ssl.trustStore", this.getClass().getResource("/.keystore").toString().replace("file:/", ""));
107 
108 		ctx = new InitialDirContext(envoierment);
109 
110 		run();
111 
112 	}
113 
114 	private void run() throws Exception {
115 		createOU();
116 
117 		for (int i = 0; i < valueUsers; i++) {
118 			Attributes attributes = new BasicAttributes(true);
119 			String sn = SURNAME + i;
120 			String givenName = GIVENNAME + i;
121 
122 			/*
123 			 * Standard User Attributes
124 			 */
125 			Attribute attribute = new BasicAttribute("sn", sn);
126 			attributes.put(attribute);
127 			attribute = new BasicAttribute("givenName", givenName);
128 			attributes.put(attribute);
129 			attribute = new BasicAttribute("displayName", sn + " " + givenName);
130 			attributes.put(attribute);
131 			attribute = new BasicAttribute("description", DESCRIPTION);
132 			attributes.put(attribute);
133 			attribute = new BasicAttribute("sAMAccountName", givenName.toLowerCase() + "." + sn.toLowerCase());
134 			attributes.put(attribute);
135 			attribute = new BasicAttribute("userPrincipalName", givenName.toLowerCase() + "." + sn.toLowerCase() + REALM);
136 			attributes.put(attribute);
137 			attribute = new BasicAttribute("name", sn + " " + givenName);
138 			attributes.put(attribute);
139 			attribute = new BasicAttribute("title", TITLE);
140 			attributes.put(attribute);
141 			attribute = new BasicAttribute("wWWHomePage", HOMEPAGE);
142 			attributes.put(attribute);
143 			attribute = new BasicAttribute("c", COUNTRY);
144 			attributes.put(attribute);
145 			attribute = new BasicAttribute("l", LOCATION);
146 			attributes.put(attribute);
147 			attribute = new BasicAttribute("st", STATE);
148 			attributes.put(attribute);
149 			attribute = new BasicAttribute("company", COMPANY);
150 			attributes.put(attribute);
151 			attribute = new BasicAttribute("streetAddress", STREETADDRESS);
152 			attributes.put(attribute);
153 			attribute = new BasicAttribute("department", DEPARTMENT);
154 			attributes.put(attribute);
155 			attribute = new BasicAttribute("postalCode", POSTALCODE);
156 			attributes.put(attribute);
157 
158 			int UF_PASSWD_NOTREQD = 0x0020;
159 			int UF_NORMAL_ACCOUNT = 0x0200;
160 			int UF_PASSWORD_EXPIRED = 0x800000;
161 
162 			attributes.put("userAccountControl", Integer.toString(UF_NORMAL_ACCOUNT + UF_PASSWD_NOTREQD));
163 
164 			createUser(sn + " " + givenName, attributes);
165 			logger.debug(sn + " " + givenName + " --> erstellt");
166 			testData.add(sn + " " + givenName);
167 
168 			if (valueOfDisabledAccounts != 0) {
169 				int UF_ACCOUNTDISABLE = 0x0002;
170 				ModificationItem[] mods = new ModificationItem[1];
171 				mods[0] = new ModificationItem(DirContext.REPLACE_ATTRIBUTE, new BasicAttribute("userAccountControl", Integer.toString(UF_NORMAL_ACCOUNT + UF_ACCOUNTDISABLE)));
172 				ctx.modifyAttributes("CN=" + sn + " " + givenName + ",OU=" + OUNAME, mods);
173 				logger.debug("CN=" + sn + " " + givenName + " --> deaktiviert");
174 				valueOfDisabledAccounts--;
175 			}
176 
177 			if (valueHaveMail != 0) {
178 				ModificationItem[] mods = new ModificationItem[1];
179 				mods[0] = new ModificationItem(DirContext.REPLACE_ATTRIBUTE, new BasicAttribute("mail", givenName + "." + sn + MAIL));
180 				ctx.modifyAttributes("CN=" + sn + " " + givenName + ",OU=" + OUNAME, mods);
181 				logger.debug("CN=" + sn + " " + givenName + " --> Mail");
182 				valueHaveMail--;
183 			}
184 
185 			if (valueHavePhone != 0) {
186 				ModificationItem[] mods = new ModificationItem[1];
187 				mods[0] = new ModificationItem(DirContext.REPLACE_ATTRIBUTE, new BasicAttribute("telephoneNumber", TELEPHONENUMBER + i));
188 				ctx.modifyAttributes("CN=" + sn + " " + givenName + ",OU=" + OUNAME, mods);
189 				logger.debug("CN=" + sn + " " + givenName + " --> Telephone");
190 				valueHavePhone--;
191 			}
192 			ModificationItem[] mods = new ModificationItem[2];
193 			String newQuotedPassword = "\"" + this.PASSWORD + "\"";
194 			byte[] newUnicodePassword = newQuotedPassword.getBytes("UTF-16LE");
195 
196 			mods[0] = new ModificationItem(DirContext.REPLACE_ATTRIBUTE, new BasicAttribute("unicodePwd", newUnicodePassword));
197 			mods[1] = new ModificationItem(DirContext.REPLACE_ATTRIBUTE, new BasicAttribute("userAccountControl", Integer.toString(UF_NORMAL_ACCOUNT + UF_PASSWD_NOTREQD)));
198 			ctx.modifyAttributes("CN=" + sn + " " + givenName + ",OU=" + OUNAME, mods);
199 		}
200 
201 	}
202 
203 	/**
204 	 * Create OU in Ldap to insert the testdatas
205 	 */
206 	private void createOU() throws Exception {
207 		// LDAP Object-Classes
208 		Attributes attrs = new BasicAttributes(true);
209 		Attribute objFact = new BasicAttribute("objectclass");
210 		objFact.add("top");
211 		objFact.add("organizationalunit");
212 		attrs.put(objFact);
213 
214 		// OU Objekt receives a descriptoion
215 		Attribute attribute = new BasicAttribute("description", "TestDaten Heracles");
216 		attrs.put(attribute);
217 		ctx.createSubcontext("OU=" + OUNAME, attrs);
218 
219 	}
220 
221 	private void createGroup(String groupname) throws Exception {
222 		Attributes attrs = new BasicAttributes(true);
223 		// LDAP Object-Classes
224 		Attribute objFact = new BasicAttribute("objectclass");
225 		objFact.add("top");
226 		objFact.add("group");
227 		attrs.put(objFact);
228 
229 		// OU Objekt receives a descriptoion
230 		Attribute attribute6 = new BasicAttribute("sAMAccountName", groupname);
231 		Attribute attribute8 = new BasicAttribute("name", groupname);
232 		Attribute attribute = new BasicAttribute("description", "TestDaten Heracles");
233 		attrs.put(attribute);
234 		ctx.createSubcontext("CN=" + groupname + ",OU=" + OUNAME, attrs);
235 
236 	}
237 
238 	private void createUser(String commonname, Attributes attributes) throws Exception {
239 		Attribute userLdapObject = new BasicAttribute("objectclass");
240 		userLdapObject.add("top");
241 		userLdapObject.add("organizationalPerson");
242 		userLdapObject.add("person");
243 		userLdapObject.add("user");
244 		attributes.put(userLdapObject);
245 
246 		ctx.createSubcontext("CN=" + commonname + ",OU=" + OUNAME, attributes);
247 	}
248 
249 	public void createGroups(String name, int members) throws Exception {
250 		createGroup(name);
251 		testData.add(name);
252 		Iterator<String> iter = testData.iterator();
253 		while (iter.hasNext()) {
254 			String commonname = iter.next();
255 
256 			if (members != 0) {
257 				ModificationItem member[] = new ModificationItem[1];
258 				member[0] = new ModificationItem(DirContext.ADD_ATTRIBUTE, new BasicAttribute("member", "CN=" + commonname + ",OU=" + OUNAME + ",DC=HFT-SO,DC=BBZ-SG,DC=LAN"));
259 
260 				ctx.modifyAttributes("CN=" + name + ",OU=" + OUNAME, member);
261 				logger.debug("Added user to group: " + "CN=" + name + ",OU=" + OUNAME);
262 				members--;
263 			}
264 
265 		}
266 
267 	}
268 
269 	public void deleteTestData(List data) throws Exception {
270 		Iterator<String> iter = data.iterator();
271 		while (iter.hasNext()) {
272 			String commonname = (String) iter.next();
273 			ctx.destroySubcontext("CN=" + commonname + ",OU=" + OUNAME);
274 		}
275 		ctx.destroySubcontext("OU=" + OUNAME);
276 		logger.debug("Everthing deleted");
277 		NamingEnumeration enums = ctx.list("");
278 
279 		while (enums.hasMoreElements()) {
280 			NameClassPair name = (NameClassPair) enums.nextElement();
281 			logger.debug(name.getName());
282 			if (name.getName().equals("OU=" + OUNAME)) {
283 				throw new Exception("something was not deleted");
284 			}
285 
286 		}
287 
288 		ctx.close();
289 	}
290 
291 	/**
292 	 * @return the testData
293 	 */
294 	public List getTestData() {
295 		return testData;
296 	}
297 
298 }