Secure PHP Building 50 Practical Applications Development phần 8 doc

92 165 0
Secure PHP Building 50 Practical Applications Development phần 8 doc

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

The makesite script uses the Console/Getopt.php class from PEAR;therefore, the makesite.conf script loads this script in the beginning of the configura- tion file. Make sure that the $PEAR_DIR variable in makesite.conf is set properly to point to your PEAR installation. Creating the virtual host configuration Listing 17-2 shows the standard account’s virtual host configuration, std_vhosts.conf. This is loaded by the makesite script and processed by calling the makeVirtualHost() function within the configuration file. Listing 17-2: vhosts/std_vhost.conf <?php function makeVirtualHost() { $www = $GLOBALS[SYSTEM_INFO][www_partition]; $ipAddr = $GLOBALS[SYSTEM_INFO][server_ip]; $server = $GLOBALS[SERVER_NAME]; $serverRoot = sprintf(“%s/%s”, $www, $server); $docRoot = sprintf(“%s/%s/htdocs”, $www, $server); $logDir = sprintf(“%s/%s/logs”, $www, $server); $vhostConfig = <<<STD_VHOST_CONF # # Automated virtual host configuration for $GLOBALS[SERVER_NAME] # # Account Type: standard # <VirtualHost $ipAddr> ServerName $server DocumentRoot “$docRoot” ErrorLog “$logDir/errors.log” CustomLog “$logDir/access.log” common Continued Chapter 17: Apache Virtual Host Maker 615 22 549669 ch17.qxd 4/4/03 9:27 AM Page 615 Listing 17-2 (Continued) <Directory /> <Files “*.conf”> deny from all </Files> </Directory> </VirtualHost> STD_VHOST_CONF; $output[‘config’] = $vhostConfig; $output[‘makedir’] = array( ‘SERVER_ROOT’ => $serverRoot, ‘DOCUMENT_ROOT’ => $docRoot, ‘LOG_DIR’ => $logDir); return $output; } ?> The makesite script loads this file if the account type is specified as standard. The standard template can be selected by either explicitly selecting type standard or not specifying any account type, as the standard type is set as the default in make- site.conf (DEFAULT_ACCOUNT_TYPE). Of course, you can specify any account type as the default. When the std_vhost.conf file is loaded, the makeVirtualHost() function is called from makesite, which must return an Apache virtual server con- figuration enclosed in a <VirtualHost > container. In this sample configuration, a virtual host configuration is returned. That defines the server name, using the ServerName directive; the Web document root, using the DocumentRoot directive; and the error and access logs, using the ErrorLog and CustomLog directives, respectively. It also specifies that any files with .conf extensions are not allowed for Web browsing. You can create highly cus- tomizable configurations using PHP in the makeVirtualHost() function. The sam- ple configuration is simply a basic example. To learn more about Apache 2 configurations, visit http://httpd. apache.org to review online documentation. 616 Part IV: Using PHP for Sysadmin Tasks 22 549669 ch17.qxd 4/4/03 9:27 AM Page 616 Creating the contents configuration file Listing 17-3 shows the contents configuration file, std_contents.conf, which is stored in the vhosts subdirectory pointed to by $TEMPLATE_DIR in makesite.conf. This configuration file is loaded once the new user account, the virtual host con- figuration, and the necessary directory structure have been created. The purpose of this configuration file is to enable you to install contents. Listing 17-3: vhosts/std_contents.conf <?php // Master contents for standard account function copyContentsToSite($site = null) { $MASTER_CONTENTS_DIR = “vhosts/standard/htdocs/*”; $CP_BIN = $GLOBALS[SYSTEM_INFO][cp_bin]; $CP_OPT = $GLOBALS[SYSTEM_INFO][cp_opt]; $CHOWN_BIN = $GLOBALS[SYSTEM_INFO][chown_bin]; $CHMOD_BIN = $GLOBALS[SYSTEM_INFO][chmod_bin]; $user = $site[user]; $group = $site[group]; $docRoot = $site[DOCUMENT_ROOT]; $cmd = “$CP_BIN $CP_OPT $MASTER_CONTENTS_DIR $docRoot”; echo “$cmd\n”; exec($cmd, $output, $status); $cmd = “$CHOWN_BIN -R $user:$group $docRoot”; exec($cmd, $output, $status); $cmd = “$CHMOD_BIN -R 755 $docRoot”; exec($cmd, $output, $status); return TRUE; } ?> Chapter 17: Apache Virtual Host Maker 617 22 549669 ch17.qxd 4/4/03 9:27 AM Page 617 In the sample version, once the configuration file is loaded, the copyContentsToSite() function is run by the makesite script. This function per- forms a copy operation that copies all files in vhosts/standard/htdocs/* (includ- ing subdirectories) to the newly created Web site’s document root directory. Then it sets the directory ownership and file permissions for the entire document root so that files can be both accessible by the owner of the account and read by the Apache server. Of course, you can do much more using this configuration file. For example, you can install any specific applications you want to offer users of this account type. Creating the e-mail template Listing 17-4 shows the e-mail template, which is also stored in the vhosts directory pointed to by the $TEMPLATE_DIR variable in makesite.conf. This is a simple text file that stores e-mail headers and a message body containing a set of custom tags. These tags are parsed and replaced before mail is sent out. The mail is sent to the email address specified by the notify_email=email_address command-line argument for makesite. Listing 17-4: vhosts/std_vhost.mail From: Your Friendly ISP <admin@examplep.net> Content-Type: text/html Subject: Your <%VHOST%> is now ready [Account Type: <%TYPE%>] Dear Customer, Your web site <%VHOST%> is now ready. You can access it via http://<%VHOST%> Your account information is as follows: Shell account: <%USER%> [GROUP: <%GROUP%>] Password: <%PASSWD%> Your Web site information is as follows: [ ] PHP [ ] CGI [ ] SSI Server Root: <%SERVER_ROOT%> Document Root: <%DOCUMENT_ROOT%> Log dir: <%LOG_DIR%> 618 Part IV: Using PHP for Sysadmin Tasks 22 549669 ch17.qxd 4/4/03 9:27 AM Page 618 Thanks. Account Team, Your ISP Ideally, this e-mail is sent with enough instructions for the new account owner to be able to start using the Web site account. Creating the makesite script Listing 17-5 shows the makesite script. Listing 17-5: makesite #!/usr/bin/php -q <?php require_once(‘makesite.conf’); $CMD_SHORT_OPTIONS = ‘hu:p:v:t:rtn:g:’; $CMD_LONG_OPTIONS = array(‘help’, ‘add’, ‘enable’, ‘disable’, ‘user=’, ‘group=’, ‘pass=’, ‘vhost=’, ‘type=’, ‘restart’, ‘test’, ‘notify_email=’ ); $cmd = getCommandLineOptions(Console_Getopt::getopt($GLOBALS[‘argv’], $CMD_SHORT_OPTIONS, $CMD_LONG_OPTIONS) ); $SITE_INFO = null; Continued Chapter 17: Apache Virtual Host Maker 619 22 549669 ch17.qxd 4/4/03 9:27 AM Page 619 Listing 17-5 (Continued) if (empty($cmd) || (getValue($cmd, ‘v’, ‘vhost’)) == null ) { syntax(); exit; } if (isset($cmd[‘add’])) { $request = makeAddRequest($cmd); if ($request != null) { $type = $request[type]; $account = $GLOBALS[ACCOUNT_TYPE][$type]; // See if user account already exists or not // if new, create if(! userExists($request[user]) && ! createUser($request[user], $request[passwd], $account[shell])) { echo “User $request[user] does not exist\n”; echo “User $request[user] could not be created.\n”; return FALSE; } // See if group already exists or not // If new, create if(! groupExists($request[group])) { echo “Group $request[group] does not exist\n”; return FALSE; } $addOK = addSite($request); // If site was added successfully see if we need to // restart or test if ($addOK && (isset($request[restart]) || isset($request[test]) ) ) { if (!restartApache()) { 620 Part IV: Using PHP for Sysadmin Tasks 22 549669 ch17.qxd 4/4/03 9:27 AM Page 620 echo “Error: Apache could not be restarted!\n”; return FALSE; } if (isset($request[test]) && !testNewSite($request[vhost])) { echo “Error: site test failed!\n”; return FALSE; } } // Now link the user account to the web site document root if ($addOK && DEFAULT_SYMLINK_USER_TO_WEBSITE && ! createSymLink($request[user], $request[vhost])) { echo “Error: could not create symbolic link to site in user account!\n”; return FALSE; } // Now process content configuration if ($addOK && ! addContents($SITE_INFO, $account[master_contents])) { echo “Error: could not add contents!\n”; return FALSE; } // Now process content configration if ($addOK && isset($request[notify_email]) && ! sendMail($SITE_INFO, $request, $account[mail_template])) { echo “Error: could not send mail!\n”; return FALSE; } } } if (isset($cmd[‘enable’])) { echo “Enable named site \n”; enableSite($siteName); } Continued Chapter 17: Apache Virtual Host Maker 621 22 549669 ch17.qxd 4/4/03 9:27 AM Page 621 Listing 17-5 (Continued) if (isset($cmd[‘disable’])) { echo “Disable named site \n”; } //print_r($cmd); exit; function createUser($user = null, $pass = null, $shell =null) { echo “Creating user account: $user with password $pass shell=$shell\n”; if (empty($pass) || strlen($pass) < $GLOBALS[SYSTEM_INFO][min_passwd_length]) { echo “Error: Password is missing or too short.\n”; return FALSE; } if ($shell != null) { $shell = “-s $shell”; } $cmd = $GLOBALS[SYSTEM_INFO][useradd_bin]; exec(“$cmd -p $pass $shell $user”); return TRUE; } function userExists($user = null) { $passwdFile = $GLOBALS[SYSTEM_INFO][passwd_file]; $lines = file($passwdFile); foreach($lines as $record) { $str = explode(‘:’, $record); if (!strcmp($str[0], $user)) return TRUE; } return FALSE; } 622 Part IV: Using PHP for Sysadmin Tasks 22 549669 ch17.qxd 4/4/03 9:27 AM Page 622 function groupExists($group = null) { $groupFile = $GLOBALS[SYSTEM_INFO][group_file]; $lines = file($groupFile); foreach($lines as $record) { $str = explode(‘:’, $record); if (!strcmp($str[0], $group)) return TRUE; } return FALSE; } function addSite($request = null) { $vhost = $request[vhost]; $type = $request[type]; $user = $request[user]; $group = $request[group]; echo “Creating $vhost configuration\n”; // config file $vhostConfigFile = sprintf(“%s/%s/%s”, $GLOBALS[‘APACHE_INFO’][‘path’], $GLOBALS[‘APACHE_INFO’][‘vhost_conf_dir’], $vhost); // See if this virtual host already exists or not if (file_exists($vhostConfigFile)) { echo “Error: $vhostConfigFile already exists. Cannot add site!\n”; return FALSE; } $account = $GLOBALS[‘ACCOUNT_TYPE’][$type]; if (!isset($account)) { echo “Error: given account type ($type) not defined in makesite.conf\n”; return FALSE; } Continued Chapter 17: Apache Virtual Host Maker 623 22 549669 ch17.qxd 4/4/03 9:27 AM Page 623 Listing 17-5 (Continued) // Configure Apache Virtual Host $GLOBALS[SERVER_NAME] = $vhost; $results = loadVhostTemplate($account[vhost_template]); if ($results == null) return FALSE; $success = writeVirtualConfigFile($results[config], $vhostConfigFile); if (! $success) return FALSE; // Create directories if (DEBUG) echo “Create directories\n”; foreach($results[makedir] as $dirName => $dirPath) { makeDirectory($GLOBALS[SYSTEM_INFO][permission],$dirPath); setOwnerAndGroup($user, $group, $dirPath); setPermissions($GLOBALS[SYSTEM_INFO][permission], $dirPath); $GLOBALS[SITE_INFO][$dirName] = $dirPath; } // Perform apache syntax check for vhost configuration $success = checkApacheSyntax($vhostConfigFile); if (! $success) return FALSE; $success = appendVhostConfigToApacheConfig($vhostConfigFile); if (! $success) return FALSE; return TRUE; } function checkApacheSyntax($file = null) { $serverBin = sprintf(“%s/%s/%s”, $GLOBALS[APACHE_INFO][path], $GLOBALS[APACHE_INFO][bin_dir], $GLOBALS[APACHE_INFO][server_bin] ); if (! file_exists($serverBin)) { echo “Error: could not find $serverBin\n”; return FALSE; } $cmd = “$serverBin “ . $GLOBALS[APACHE_INFO][config_chk_opt] . “ “ . $file ; 624 Part IV: Using PHP for Sysadmin Tasks 22 549669 ch17.qxd 4/4/03 9:27 AM Page 624 [...]... serial 3H ; refresh 15M ; retry 1W ; expiry 1D ) ; minimum 1D IN NS 192.1 68. 0.11 1D IN NS 192.1 68. 1.254 1D IN MX 5 192.1 68. 0.100 1D IN MX 10 192.1 68. 0.101 ns 1D IN A 192.1 68. 0.11 www 1D IN A 192.1 68. 0.12 www IN CNAME apache.example.com ftp 1D IN A 192.1 68. 0.12 645 23 549669 ch 18. qxd 646 4/4/03 9:27 AM Page 646 Part IV: Using PHP for Sysadmin Tasks All the IP addresses and host names are inserted using... $FTP_SERVER_IP_ADDR $WWW_SERVER_ALIAS = = = = = = = = ‘example.com’; ‘192.1 68. 0.11’; ‘192.1 68. 1.254’; ‘192.1 68. 0.100’; ‘192.1 68. 0.101’; ‘192.1 68. 0.12’; ‘192.1 68. 0.12’; ‘apache.example.com’; ?> As mentioned before, the makezone script uses two types of template, one of which is used to create the zone There can be many different zone templates Listing 18- 2 shows a zone template called standard.template This template... function is called to display syntax ◆ If the add option is specified, the addZone() function is called to create the new zone 647 23 549669 ch 18. qxd 6 48 4/4/03 9:27 AM Page 6 48 Part IV: Using PHP for Sysadmin Tasks Listing 18- 5: makezone #!/usr/bin /php -q < ?php require_once(‘makezone.conf’); $CMD_SHORT_OPTIONS = ‘h’; $CMD_LONG_OPTIONS = array(‘help’, ‘add=’, ‘name=’, ‘template=’, ‘enable’, ‘disable’,... template in detail 643 23 549669 ch 18. qxd 644 4/4/03 9:27 AM Page 644 Part IV: Using PHP for Sysadmin Tasks Listing 18- 2: standard.template < ?php function getZoneConfiguration() { $output = . information is as follows: [ ] PHP [ ] CGI [ ] SSI Server Root: <%SERVER_ROOT%> Document Root: <%DOCUMENT_ROOT%> Log dir: <%LOG_DIR%> 6 18 Part IV: Using PHP for Sysadmin Tasks 22. $site[group]; $docRoot = $site[DOCUMENT_ROOT]; $cmd = “$CP_BIN $CP_OPT $MASTER_CONTENTS_DIR $docRoot”; echo “$cmd ”; exec($cmd, $output, $status); $cmd = “$CHOWN_BIN -R $user:$group $docRoot”; exec($cmd,. makeDirectory($mode = ‘0 750 , $path = null) { $cmd = $GLOBALS[SYSTEM_INFO][mkdir_bin] . “ -m $mode -p $path”; 6 28 Part IV: Using PHP for Sysadmin Tasks 22 549669 ch17.qxd 4/4/03 9:27 AM Page 6 28 if (DEBUG)

Ngày đăng: 13/08/2014, 12:21

Tài liệu cùng người dùng

Tài liệu liên quan