1 16 17 package org.apache.ajp.tomcat4.config; 18 19 import java.io.File ; 20 import java.io.FileWriter ; 21 import java.io.IOException ; 22 import java.io.PrintWriter ; 23 import java.util.Date ; 24 import java.util.Hashtable ; 25 26 import org.apache.catalina.Context; 27 import org.apache.catalina.Host; 28 29 48 49 117 public class ApacheConfig extends BaseJkConfig { 118 119 120 public static final String MOD_JK_CONFIG = "conf/auto/mod_jk.conf"; 121 124 public static final String WORKERS_CONFIG = "conf/jk/workers.properties"; 125 126 public static final String JK_LOG_LOCATION = "logs/mod_jk.log"; 127 128 public static final String MOD_JK; 129 130 static{ 132 String os = System.getProperty("os.name").toLowerCase(); 133 if(os.indexOf("windows")>=0){ 134 MOD_JK = "modules/mod_jk.dll"; 135 }else if(os.indexOf("netware")>=0){ 136 MOD_JK = "modules/mod_jk.nlm"; 137 }else{ 138 MOD_JK = "libexec/mod_jk.so"; 139 } 140 } 141 142 private File jkConfig = null; 143 private File modJk = null; 144 145 private boolean sslExtract=true; 147 private String sslHttpsIndicator="HTTPS"; 148 private String sslSessionIndicator="SSL_SESSION_ID"; 149 private String sslCipherIndicator="SSL_CIPHER"; 150 private String sslCertsIndicator="SSL_CLIENT_CERT"; 151 152 Hashtable NamedVirtualHosts=null; 153 154 public ApacheConfig() { 155 } 156 157 159 167 public void setJkConfig(String path){ 168 jkConfig= (path==null)?null:new File (path); 169 } 170 171 175 public void setModJk(String path){ 176 modJk=( path==null?null:new File (path)); 177 } 178 179 189 public void setExtractSSL( boolean sslMode ) { 190 this.sslExtract=sslMode; 191 } 192 193 195 public void setHttpsIndicator( String s ) { 196 sslHttpsIndicator=s; 197 } 198 199 201 public void setSessionIndicator( String s ) { 202 sslSessionIndicator=s; 203 } 204 205 207 public void setCipherIndicator( String s ) { 208 sslCipherIndicator=s; 209 } 210 211 214 public void setCertsIndicator( String s ) { 215 sslCertsIndicator=s; 216 } 217 218 220 223 protected void initProperties() { 224 super.initProperties(); 225 226 jkConfig= getConfigFile( jkConfig, configHome, MOD_JK_CONFIG); 227 workersConfig=getConfigFile( workersConfig, configHome, 228 WORKERS_CONFIG); 229 if( modJk == null ) 230 modJk=new File (MOD_JK); 231 else 232 modJk=getConfigFile( modJk, configHome, MOD_JK ); 233 jkLog=getConfigFile( jkLog, configHome, JK_LOG_LOCATION); 234 } 235 237 protected PrintWriter getWriter() throws IOException { 238 String abJkConfig = jkConfig.getAbsolutePath(); 239 return new PrintWriter (new FileWriter (abJkConfig, append)); 240 } 241 242 243 245 247 protected boolean generateJkHead(PrintWriter mod_jk) 248 { 249 250 mod_jk.println("########## Auto generated on " + new Date () + 251 "##########" ); 252 mod_jk.println(); 253 254 if( ! modJk.exists() ) { 257 log( "mod_jk location: " + modJk ); 258 log( "Make sure it is installed corectly or " + 259 " set the config location" ); 260 log( "Using <Listener className=\""+getClass().getName()+"\" modJk=\"PATH_TO_MOD_JK.SO_OR_DLL\" />" ); 261 } 262 263 mod_jk.println("<IfModule !mod_jk.c>"); 265 mod_jk.println(" LoadModule jk_module \""+ 266 modJk.toString().replace('\\','/') + 267 "\""); 268 mod_jk.println("</IfModule>"); 269 mod_jk.println(); 270 271 272 if( ! workersConfig.exists() ) { 275 log( "Can't find workers.properties at " + workersConfig ); 276 log( "Please install it in the default location or " + 277 " set the config location" ); 278 log( "Using <Listener className=\"" + getClass().getName() + "\" workersConfig=\"FULL_PATH\" />" ); 279 return false; 280 } 281 282 mod_jk.println("JkWorkersFile \"" 283 + workersConfig.toString().replace('\\', '/') 284 + "\""); 285 286 mod_jk.println("JkLogFile \"" 287 + jkLog.toString().replace('\\', '/') 288 + "\""); 289 mod_jk.println(); 290 291 if( jkDebug != null ) { 292 mod_jk.println("JkLogLevel " + jkDebug); 293 mod_jk.println(); 294 } 295 return true; 296 } 297 298 protected void generateVhostHead(Host host, PrintWriter mod_jk) { 299 300 mod_jk.println(); 301 String vhostip = host.getName(); 302 String vhost = vhostip; 303 int ppos = vhost.indexOf(":"); 304 if(ppos >= 0) 305 vhost = vhost.substring(0,ppos); 306 307 mod_jk.println("<VirtualHost "+ vhostip + ">"); 308 mod_jk.println(" ServerName " + vhost ); 309 String [] aliases=host.findAliases(); 310 if( aliases.length > 0 ) { 311 mod_jk.print(" ServerAlias " ); 312 for( int ii=0; ii < aliases.length ; ii++) { 313 mod_jk.print( aliases[ii] + " " ); 314 } 315 mod_jk.println(); 316 } 317 indent=" "; 318 } 319 320 protected void generateVhostTail(Host host, PrintWriter mod_jk) { 321 mod_jk.println("</VirtualHost>"); 322 indent=""; 323 } 324 325 protected void generateSSLConfig(PrintWriter mod_jk) { 326 if( ! sslExtract ) { 327 mod_jk.println("JkExtractSSL Off"); 328 } 329 if( ! "HTTPS".equalsIgnoreCase( sslHttpsIndicator ) ) { 330 mod_jk.println("JkHTTPSIndicator " + sslHttpsIndicator); 331 } 332 if( ! "SSL_SESSION_ID".equalsIgnoreCase( sslSessionIndicator )) { 333 mod_jk.println("JkSESSIONIndicator " + sslSessionIndicator); 334 } 335 if( ! "SSL_CIPHER".equalsIgnoreCase( sslCipherIndicator )) { 336 mod_jk.println("JkCIPHERIndicator " + sslCipherIndicator); 337 } 338 if( ! "SSL_CLIENT_CERT".equalsIgnoreCase( sslCertsIndicator )) { 339 mod_jk.println("JkCERTSIndicator " + sslCertsIndicator); 340 } 341 342 mod_jk.println(); 343 } 344 345 String indent=""; 347 348 351 protected void generateStupidMappings(Context context, 352 PrintWriter mod_jk ) 353 { 354 String ctxPath = context.getPath(); 355 if(ctxPath == null) 356 return; 357 358 String nPath=("".equals(ctxPath)) ? "/" : ctxPath; 359 360 mod_jk.println(); 361 mod_jk.println(indent + "JkMount " + nPath + " " + jkWorker ); 362 if( "".equals(ctxPath) ) { 363 mod_jk.println(indent + "JkMount " + nPath + "* " + jkWorker ); 364 if ( context.getParent() instanceof Host ) { 365 mod_jk.println(indent + "DocumentRoot \"" + 366 getApacheDocBase(context) + "\""); 367 } else { 368 mod_jk.println(indent + 369 "# To avoid Apache serving root welcome files from htdocs, update DocumentRoot"); 370 mod_jk.println(indent + 371 "# to point to: \"" + getApacheDocBase(context) + "\""); 372 } 373 374 } else { 375 mod_jk.println(indent + "JkMount " + nPath + "/* " + jkWorker ); 376 } 377 } 378 379 380 private void generateNameVirtualHost( PrintWriter mod_jk, String ip ) { 381 if( !NamedVirtualHosts.containsKey(ip) ) { 382 mod_jk.println("NameVirtualHost " + ip + ""); 383 NamedVirtualHosts.put(ip,ip); 384 } 385 } 386 387 390 protected void generateContextMappings(Context context, PrintWriter mod_jk ) 391 { 392 String ctxPath = context.getPath(); 393 Host vhost = getHost(context); 394 395 if( noRoot && "".equals(ctxPath) ) { 396 log("Ignoring root context in non-forward-all mode "); 397 return; 398 } 399 400 mod_jk.println(); 401 mod_jk.println(indent + "#################### " + 402 ((vhost!=null ) ? vhost.getName() + ":" : "" ) + 403 (("".equals(ctxPath)) ? "/" : ctxPath ) + 404 " ####################" ); 405 mod_jk.println(); 406 408 generateStaticMappings( context, mod_jk ); 409 410 413 415 if(context.getLoginConfig() != null) { 416 String loginPage = context.getLoginConfig().getLoginPage(); 417 if(loginPage != null) { 418 int lpos = loginPage.lastIndexOf("/"); 419 String jscurl = loginPage.substring(0,lpos+1) + "j_security_check"; 420 addMapping( ctxPath, jscurl, mod_jk); 421 } 422 } 423 String [] servletMaps = context.findServletMappings(); 424 for(int ii=0; ii < servletMaps.length; ii++) { 425 addMapping( ctxPath, servletMaps[ii] , mod_jk ); 426 } 427 } 428 429 431 protected boolean addExtensionMapping( String ctxPath, String ext, 432 PrintWriter mod_jk ) 433 { 434 if( debug > 0 ) 435 log( "Adding extension map for " + ctxPath + "/*." + ext ); 436 mod_jk.println(indent + "JkMount " + ctxPath + "/*." + ext 437 + " " + jkWorker); 438 return true; 439 } 440 441 442 444 protected boolean addMapping( String fullPath, PrintWriter mod_jk ) { 445 if( debug > 0 ) 446 log( "Adding map for " + fullPath ); 447 mod_jk.println(indent + "JkMount " + fullPath + " " + jkWorker ); 448 return true; 449 } 450 452 protected boolean addMapping( String ctxP, String ext, PrintWriter mod_jk ) { 453 if( debug > 0 ) 454 log( "Adding map for " + ext ); 455 if(! ext.startsWith("/") ) 456 ext = "/" + ext; 457 if(ext.length() > 1) 458 mod_jk.println(indent + "JkMount " + ctxP + ext+ " " + jkWorker ); 459 return true; 460 } 461 462 private void generateWelcomeFiles(Context context, PrintWriter mod_jk ) { 463 String wf[]=context.findWelcomeFiles(); 464 if( wf==null || wf.length == 0 ) 465 return; 466 mod_jk.print(indent + " DirectoryIndex "); 467 for( int i=0; i<wf.length ; i++ ) { 468 mod_jk.print( wf[i] + " " ); 469 } 470 mod_jk.println(); 471 } 472 473 477 private void generateStaticMappings(Context context, PrintWriter mod_jk ) { 478 String ctxPath = context.getPath(); 479 480 String docBase = getApacheDocBase(context); 482 483 if( !"".equals(ctxPath) ) { 484 mod_jk.println(indent + "# Static files "); 486 mod_jk.println(indent + "Alias " + ctxPath + " \"" + docBase + "\""); 487 mod_jk.println(); 488 } else { 489 if ( getHost(context) != null ) { 490 mod_jk.println(indent + "DocumentRoot \"" + 491 getApacheDocBase(context) + "\""); 492 } else { 493 mod_jk.println(indent + 496 "# Be sure to update DocumentRoot"); 497 mod_jk.println(indent + 498 "# to point to: \"" + docBase + "\""); 499 } 500 } 501 mod_jk.println(indent + "<Directory \"" + docBase + "\">"); 502 mod_jk.println(indent + " Options Indexes FollowSymLinks"); 503 504 generateWelcomeFiles(context, mod_jk); 505 506 mod_jk.println(indent + "</Directory>"); 508 mod_jk.println(); 509 510 511 mod_jk.println(); 513 mod_jk.println(indent + 514 "# Deny direct access to WEB-INF and META-INF"); 515 mod_jk.println(indent + "#"); 516 mod_jk.println(indent + "<Location \"" + ctxPath + "/WEB-INF/*\">"); 517 mod_jk.println(indent + " AllowOverride None"); 518 mod_jk.println(indent + " deny from all"); 519 mod_jk.println(indent + "</Location>"); 520 mod_jk.println(); 522 mod_jk.println(indent + "<Location \"" + ctxPath + "/META-INF/*\">"); 523 mod_jk.println(indent + " AllowOverride None"); 524 mod_jk.println(indent + " deny from all"); 525 mod_jk.println(indent + "</Location>"); 526 if (File.separatorChar == '\\') { 527 mod_jk.println(indent + "#"); 528 mod_jk.println(indent + 529 "# Use Directory too. On Windows, Location doesn't" 530 + " work unless case matches"); 531 mod_jk.println(indent + "#"); 532 mod_jk.println(indent + 533 "<Directory \"" + docBase + "/WEB-INF/\">"); 534 mod_jk.println(indent + " AllowOverride None"); 535 mod_jk.println(indent + " deny from all"); 536 mod_jk.println(indent + "</Directory>"); 537 mod_jk.println(); 538 mod_jk.println(indent + 539 "<Directory \"" + docBase + "/META-INF/\">"); 540 mod_jk.println(indent + " AllowOverride None"); 541 mod_jk.println(indent + " deny from all"); 542 mod_jk.println(indent + "</Directory>"); 543 } 544 mod_jk.println(); 545 } 546 547 549 private String getApacheDocBase(Context context) 550 { 551 String docBase = getAbsoluteDocBase(context); 553 if (File.separatorChar == '\\') { 554 docBase = docBase.replace('\\','/'); 556 } 557 return docBase; 558 } 559 560 private String getVirtualHostAddress(String vhost, String vhostip) { 561 if( vhostip == null ) { 562 if ( vhost != null && vhost.length() > 0 && Character.isDigit(vhost.charAt(0)) ) 563 vhostip=vhost; 564 else 565 vhostip="*"; 566 } 567 return vhostip; 568 } 569 570 } 571 | Popular Tags |