7. YaPI::HTTPD

This package is the public Yast2 API to configure the apache2.

7.1. List of Global Functions

7.2. Functions

7.2.1. $hostList = GetHostsList();

This function returns a reference to a list of strings of all host ids. Even without any virtual host, there is always the "default" host id for the default host. On error, undef is returned and the Error() function can be used to get the error hash.

Example 105. 

 my $list = GetHostsList();
 if( not defined($list) ) {
     return Error();
 }
 foreach my $hostid ( @$list ) {
     print "ID: $hostid\n";
 }


Example 106. 

 my $modules = GetModuleList();
 if( $modules ) {
     foreach my $mod_name ( @$modules ) {
         print "active module: $mod_name\n";
     }
 }


Example 107. 

  WriteServerCert('*:443', $certData);
  WriteServerKey('*:443', $keyData);


Example 108. 

  WriteServerCA($hostID, $pemData);


Example 109. 

  $pemData = ReadServerCert($hostID);
  if( $pemData and open( CERT, "> /tmp/cert.pem" ) ) {
      print CERT $pemData;
      close(CERT);
      $text = `openssl x509 -in /tmp/cert.pem -text -noout`;
  }


7.2.2. $hostData = GetHost($hostid);

This function returns a reference to a host data list. The format of the Host data list is described above. In case of an error (for example, if there is no host with such an id) undef is returned.

Example 110. 

 # dumping all configured hosts
 my $hostList = GetHostsList();
 if( not defined $hostList ) {
     # error
 }
 foreach my $hostid ( @$hostList ) {
     my @host = GetHost( $hostid );
     print "# dumping $hostid\n";
     foreach my $directive ( @host ) {
         print $directive->{OVERHEAD}."\n";
         print $directive->{KEY}.' '.$directive->{VALUE}."\n";
     }
 }


Example 111. 

 my $modules = GetModuleList();
 if( $modules ) {
     foreach my $mod_name ( @$modules ) {
         print "active module: $mod_name\n";
     }
 }


Example 112. 

  WriteServerCert('*:443', $certData);
  WriteServerKey('*:443', $keyData);


Example 113. 

  WriteServerCA($hostID, $pemData);


Example 114. 

  $pemData = ReadServerCert($hostID);
  if( $pemData and open( CERT, "> /tmp/cert.pem" ) ) {
      print CERT $pemData;
      close(CERT);
      $text = `openssl x509 -in /tmp/cert.pem -text -noout`;
  }


7.2.3. ModifyHost($hostid,$hostdata)

This function modifies the host with $hostid. The complete host data will be replaced with $hostdata.

Example 115. 

 # turn off SSL and setting a comment in config file
 my @host = GetHost( $hostid );
 foreach my $directive ( @host ) {
     if( $directive->{KEY} eq 'SSL' ) {
         $directive->{VALUE} = 2;
         $directive->{OVERHEAD} = "# customer wants SSL to be required\n";
     }
 }
 ModifyHost( $hostid, \@host );


Example 116. 

 my @hostData = GetHost( $hostid );
 replaceKey( 'SSL', { KEY => 'SSL', VALUE => 1 }, \@hostData );
 replaceKey( 'ServerAdmin', { KEY => 'ServerAdmin', VALUE => 'my@my.dom' }, \@hostData );
 ModifyHost( $hostid, \@hostData );

 sub replaceKey {
     my $key      = shift;
     my $new      = shift;
     my $hostData = shift;
     my $found = 0;

     foreach( @$hostData ) {
         if( $_->{KEY} eq $new->{KEY} ) {
             $new->{OVERHEAD} = $_ ->{OVERHEAD} unless( exists($new->{OVERHEAD}) );
             $_ = $new;
             $found = 1;
             last;
         }
     }
     push( @$hostData, $new ) unless( $found );
     return 1;
 }


Example 117. 

 my $modules = GetModuleList();
 if( $modules ) {
     foreach my $mod_name ( @$modules ) {
         print "active module: $mod_name\n";
     }
 }


Example 118. 

  WriteServerCert('*:443', $certData);
  WriteServerKey('*:443', $keyData);


Example 119. 

  WriteServerCA($hostID, $pemData);


Example 120. 

  $pemData = ReadServerCert($hostID);
  if( $pemData and open( CERT, "> /tmp/cert.pem" ) ) {
      print CERT $pemData;
      close(CERT);
      $text = `openssl x509 -in /tmp/cert.pem -text -noout`;
  }


7.2.4. CreateHost($hostid,$hostdata)

This function creates a host with $hostid. $hostdata is the host data array.

Example 121. 

 my @newHost = (
                 { KEY => "ServerName",    VALUE => 'createTest2.suse.de' },
                 { KEY => "VirtualByName", VALUE => 1 },
                 { KEY => "ServerAdmin",   VALUE => 'no@one.de' }
               );
 CreateHost( '192.168.1.2/createTest2.suse.de', \@temp );


Example 122. 

 my $modules = GetModuleList();
 if( $modules ) {
     foreach my $mod_name ( @$modules ) {
         print "active module: $mod_name\n";
     }
 }


Example 123. 

  WriteServerCert('*:443', $certData);
  WriteServerKey('*:443', $keyData);


Example 124. 

  WriteServerCA($hostID, $pemData);


Example 125. 

  $pemData = ReadServerCert($hostID);
  if( $pemData and open( CERT, "> /tmp/cert.pem" ) ) {
      print CERT $pemData;
      close(CERT);
      $text = `openssl x509 -in /tmp/cert.pem -text -noout`;
  }


7.2.5. DeleteHost($hostid)

This function removes the host with $hostid. If the hostid is not found, undef is returned.

Example 126. 

 my $modules = GetModuleList();
 if( $modules ) {
     foreach my $mod_name ( @$modules ) {
         print "active module: $mod_name\n";
     }
 }


Example 127. 

  WriteServerCert('*:443', $certData);
  WriteServerKey('*:443', $keyData);


Example 128. 

  WriteServerCA($hostID, $pemData);


Example 129. 

  $pemData = ReadServerCert($hostID);
  if( $pemData and open( CERT, "> /tmp/cert.pem" ) ) {
      print CERT $pemData;
      close(CERT);
      $text = `openssl x509 -in /tmp/cert.pem -text -noout`;
  }


7.2.6. $moduleList = GetModuleList()

this function returns a reference to an array of strings. The list contains all active apache2 module names. This is more or less just the content of the sysconfig variable "APACHE_MODULES" from /etc/sysconfig/apache2.

Example 130. 

 my $modules = GetModuleList();
 if( $modules ) {
     foreach my $mod_name ( @$modules ) {
         print "active module: $mod_name\n";
     }
 }


7.2.7. $moduleList = GetKnownModules()

this function returns a reference to an array of hashes. Each has has the following keys:

Example 131. 

 # list all modules with enabled/disabled state
 my $knownMods  = GetKnownModules();
 my $activeMods = GetModuleList();
 my %activeMods = ();
 @activeMods{@$activeMods} = ();
 foreach my $km ( @$knownMods ) {
     my $state = (grep(/^$km$/, @$activeMods))?('on'):('off');
     delete($activeMods{$km});
     print "$km->{name} = $state\n";
 }

 # list active unknown mods now
 foreach my $m ( keys(%activeMods ) ) {
     print "$m = on\n";
 }


Example 132. 

  WriteServerCert('*:443', $certData);
  WriteServerKey('*:443', $keyData);


Example 133. 

  WriteServerCA($hostID, $pemData);


Example 134. 

  $pemData = ReadServerCert($hostID);
  if( $pemData and open( CERT, "> /tmp/cert.pem" ) ) {
      print CERT $pemData;
      close(CERT);
      $text = `openssl x509 -in /tmp/cert.pem -text -noout`;
  }


7.2.8. ModifyModuleList($moduleList, $state)

with this function you can turn on and off modules of the apache2 $modulelist is an array reference to a list of modulenames. This modifes more or less just the content of the sysconfig variable "APACHE_MODULES" from /etc/sysconfig/apache2. Unknown modules are allowed too but they will be appendet to the end of the list.

Example 135. 

 ModifyModuleList( [ 'perl' ], 1 );
 ModifyModuleList( [ 'php4' ], 0 );


Example 136. 

  WriteServerCert('*:443', $certData);
  WriteServerKey('*:443', $keyData);


Example 137. 

  WriteServerCA($hostID, $pemData);


Example 138. 

  $pemData = ReadServerCert($hostID);
  if( $pemData and open( CERT, "> /tmp/cert.pem" ) ) {
      print CERT $pemData;
      close(CERT);
      $text = `openssl x509 -in /tmp/cert.pem -text -noout`;
  }


7.2.9. $knownSelList = GetKnownModuleSelections()

this functions returns a reference to an array that contains hashes with information about all known module selections. One hash has the following keys:

Example 139. 

 my $knownSelList = GetKnownModuleSelections();
 foreach my $kms ( @$knownSelList ) {
     print "$kms->{id} = $kms->{summary}\n";
 }


Example 140. 

  WriteServerCert('*:443', $certData);
  WriteServerKey('*:443', $keyData);


Example 141. 

  WriteServerCA($hostID, $pemData);


Example 142. 

  $pemData = ReadServerCert($hostID);
  if( $pemData and open( CERT, "> /tmp/cert.pem" ) ) {
      print CERT $pemData;
      close(CERT);
      $text = `openssl x509 -in /tmp/cert.pem -text -noout`;
  }


7.2.10. $selList = GetModuleSelectionsList()

this function returns a reference to an array that contains strings with the names of the active module selections.

Example 143. 

 my $selList = GetModuleSelectionsList();
 print "active selections: ".join(',', @$selList)."\n";


Example 144. 

  WriteServerCert('*:443', $certData);
  WriteServerKey('*:443', $keyData);


Example 145. 

  WriteServerCA($hostID, $pemData);


Example 146. 

  $pemData = ReadServerCert($hostID);
  if( $pemData and open( CERT, "> /tmp/cert.pem" ) ) {
      print CERT $pemData;
      close(CERT);
      $text = `openssl x509 -in /tmp/cert.pem -text -noout`;
  }


7.2.11. ModifyModuleSelectionList($selList, $status)

this function modifies the module selection list. You can turn on and off module selections with the boolean $status. Changing the selections will directly influence the module list.

Example 147. 

 ModifyModuleSelectionList( ['perl-scripting', 'debug'],1  );
 ModifyModuleSelectionList( ['php4-scripting'], 0 );


Example 148. 

  WriteServerCert('*:443', $certData);
  WriteServerKey('*:443', $keyData);


Example 149. 

  WriteServerCA($hostID, $pemData);


Example 150. 

  $pemData = ReadServerCert($hostID);
  if( $pemData and open( CERT, "> /tmp/cert.pem" ) ) {
      print CERT $pemData;
      close(CERT);
      $text = `openssl x509 -in /tmp/cert.pem -text -noout`;
  }


7.2.12. ModifyService($status)

with this function you can turn on and off the apache2 runlevel script. Turning off means, no apache2 start at boot time.

Example 151. 

 ModifyService(0); # turn apache2 off at boot time
 ModifyService(1); # turn apache2 on at boot time


Example 152. 

  WriteServerCert('*:443', $certData);
  WriteServerKey('*:443', $keyData);


Example 153. 

  WriteServerCA($hostID, $pemData);


Example 154. 

  $pemData = ReadServerCert($hostID);
  if( $pemData and open( CERT, "> /tmp/cert.pem" ) ) {
      print CERT $pemData;
      close(CERT);
      $text = `openssl x509 -in /tmp/cert.pem -text -noout`;
  }


7.2.13. SwitchService($status)

with this function you can start and stop the apache2 service.

Example 155. 

 SwitchService( 0 ); # turning off the apache2 service
 SwitchService( 1 ); # turning on the apache2 service


Example 156. 

  WriteServerCert('*:443', $certData);
  WriteServerKey('*:443', $keyData);


Example 157. 

  WriteServerCA($hostID, $pemData);


Example 158. 

  $pemData = ReadServerCert($hostID);
  if( $pemData and open( CERT, "> /tmp/cert.pem" ) ) {
      print CERT $pemData;
      close(CERT);
      $text = `openssl x509 -in /tmp/cert.pem -text -noout`;
  }


7.2.14. ReloadService($status)

with this function you can reload the apache2 service

Example 159. 

 ReloadService();


Example 160. 

  WriteServerCert('*:443', $certData);
  WriteServerKey('*:443', $keyData);


Example 161. 

  WriteServerCA($hostID, $pemData);


Example 162. 

  $pemData = ReadServerCert($hostID);
  if( $pemData and open( CERT, "> /tmp/cert.pem" ) ) {
      print CERT $pemData;
      close(CERT);
      $text = `openssl x509 -in /tmp/cert.pem -text -noout`;
  }


7.2.15. $status = ReadService()

with this function you can read out the state of the apache2 runlevel script (starting apache2 at boot time).

Example 163. 

 print "apache2 is ".( (ReadService())?('on'):('off') )."\n";


Example 164. 

  WriteServerCert('*:443', $certData);
  WriteServerKey('*:443', $keyData);


Example 165. 

  WriteServerCA($hostID, $pemData);


Example 166. 

  $pemData = ReadServerCert($hostID);
  if( $pemData and open( CERT, "> /tmp/cert.pem" ) ) {
      print CERT $pemData;
      close(CERT);
      $text = `openssl x509 -in /tmp/cert.pem -text -noout`;
  }


7.2.16. CreateListen( $fromPort, $toPort, $listen, $doFirewall )

with this function you can configure the addresses and ports the webserver is listening on. $fromPort and $toPort can have the same value. $listen must be a network interface of the host but can be an empty string for 'all' interfaces. The $doFirewall boolean indicates if the SuSEFirewall2 shall be configured for the settings.

Example 167. 

 CreateListen( 80, 80, '127.0.0.1', 0 );   # localhost without firewall setup
 CreateListen( 443, 443, '', 1 );          # HTTPS on all interfaces
 CreateListen( 80, 80, '192.168.0.1', 1 ); # internal+firewall setup


Example 168. 

  WriteServerCert('*:443', $certData);
  WriteServerKey('*:443', $keyData);


Example 169. 

  WriteServerCA($hostID, $pemData);


Example 170. 

  $pemData = ReadServerCert($hostID);
  if( $pemData and open( CERT, "> /tmp/cert.pem" ) ) {
      print CERT $pemData;
      close(CERT);
      $text = `openssl x509 -in /tmp/cert.pem -text -noout`;
  }


7.2.17. DeleteListen( $fromPort, $toPort, $listen, $doFirewall )

with this function you can delete an address and port the webserver is listening on. $fromPort and $toPort can have the same value. $listen must be a network interface of the host but can be an empty string for 'all' interfaces. If the listen parameter can't be found, undef is returned. The $doFirewall boolean indicates if the SuSEFirewall2 shall be configured for the settings.

Example 171. 

 DeleteListen( 80, 80, '127.0.0.1', 0 );   # localhost without firewall setup
 DeleteListen( 443, 443, '', 1 );          # HTTPS on all interfaces
 DeleteListen( 80, 80, '192.168.0.1', 1 ); # internal+firewall setup


Example 172. 

  WriteServerCert('*:443', $certData);
  WriteServerKey('*:443', $keyData);


Example 173. 

  WriteServerCA($hostID, $pemData);


Example 174. 

  $pemData = ReadServerCert($hostID);
  if( $pemData and open( CERT, "> /tmp/cert.pem" ) ) {
      print CERT $pemData;
      close(CERT);
      $text = `openssl x509 -in /tmp/cert.pem -text -noout`;
  }


7.2.18. $listenList = GetCurrentListen()

this function returns a list of hashes with the current listen data. Each hash has the following keys:

Example 175. 

 my $listenList = GetCurrentListen();
 foreach my $ld ( @$listenList ) {
     print "Listening on: ".$ld->{ADDRESS}."/".$ld->{PORT}."\n";
 }


Example 176. 

  WriteServerCert('*:443', $certData);
  WriteServerKey('*:443', $keyData);


Example 177. 

  WriteServerCA($hostID, $pemData);


Example 178. 

  $pemData = ReadServerCert($hostID);
  if( $pemData and open( CERT, "> /tmp/cert.pem" ) ) {
      print CERT $pemData;
      close(CERT);
      $text = `openssl x509 -in /tmp/cert.pem -text -noout`;
  }


7.2.19. $packList = GetServicePackages()

this function returns a list of strings with the needed RPM packages for this service.

Example 179. 

 my $packList = GetServicePackages();
 foreach my $pack ( @$packList ) {
     print "$pack needs to be installed to run this service\n";
 }


Example 180. 

  WriteServerCert('*:443', $certData);
  WriteServerKey('*:443', $keyData);


Example 181. 

  WriteServerCA($hostID, $pemData);


Example 182. 

  $pemData = ReadServerCert($hostID);
  if( $pemData and open( CERT, "> /tmp/cert.pem" ) ) {
      print CERT $pemData;
      close(CERT);
      $text = `openssl x509 -in /tmp/cert.pem -text -noout`;
  }


7.2.20. $packList = GetModulePackages()

this function returns a list of strings with the needed RPM pacakges for all activated apache2 modules.

Example 183. 

 my $packList = GetModulePackages();
 foreach my $pack ( @$packList ) {
     print "$pack needs to be installed to run the selected modules\n";
 }


Example 184. 

  WriteServerCert('*:443', $certData);
  WriteServerKey('*:443', $keyData);


Example 185. 

  WriteServerCA($hostID, $pemData);


Example 186. 

  $pemData = ReadServerCert($hostID);
  if( $pemData and open( CERT, "> /tmp/cert.pem" ) ) {
      print CERT $pemData;
      close(CERT);
      $text = `openssl x509 -in /tmp/cert.pem -text -noout`;
  }


7.2.21. $params = GetServerFlags()

returns a string with the apache2 server flags like "-DSSL"

Example 187. 

  print GetServerFlags();


Example 188. 

  WriteServerCert('*:443', $certData);
  WriteServerKey('*:443', $keyData);


Example 189. 

  WriteServerCA($hostID, $pemData);


Example 190. 

  $pemData = ReadServerCert($hostID);
  if( $pemData and open( CERT, "> /tmp/cert.pem" ) ) {
      print CERT $pemData;
      close(CERT);
      $text = `openssl x509 -in /tmp/cert.pem -text -noout`;
  }


7.2.22. SetServerFlags($params)

Put into $params any server flags ("Defines") that you want to hand over to httpd at start time, or other command line flags. This could be -D SSL, for example. Or -DSTATUS.

Example 191. 

  SetServerFlags("-DReverseProxy");


Example 192. 

  WriteServerCert('*:443', $certData);
  WriteServerKey('*:443', $keyData);


Example 193. 

  WriteServerCA($hostID, $pemData);


Example 194. 

  $pemData = ReadServerCert($hostID);
  if( $pemData and open( CERT, "> /tmp/cert.pem" ) ) {
      print CERT $pemData;
      close(CERT);
      $text = `openssl x509 -in /tmp/cert.pem -text -noout`;
  }


7.2.23. WriteServerCert($hostId,$pemData)

this function writes the server certificate for the host with $hostID to the right place and sets the SSLCertificateFile directive to the right path. The certificate must be in PEM format and it can contain the private key too. If there is a private key in the PEM data, the SSLCertificateKeyFile directive is set too. The key can also be set via WriteServerKey. If the $pemData is undefined, an old certificate gets deleted and SSLCertificateFile directive gets dropped. Writing the server certificate does not turn on SSL automatically. On failure, undef is returned. The path for writing the certificate is /etc/apache2/ssl.crt the filename is $hostname-cert.pem

Example 195. 

  WriteServerCert('*:443', $pemData);
  $host = GetHost('*:443');
  replaceKey( 'SSL', { KEY => 'SSL', VALUE => 1 }, $host );
  ModifyHost('*:443', $host);


Example 196. 

  WriteServerCert('*:443', $certData);
  WriteServerKey('*:443', $keyData);


Example 197. 

  WriteServerCA($hostID, $pemData);


Example 198. 

  $pemData = ReadServerCert($hostID);
  if( $pemData and open( CERT, "> /tmp/cert.pem" ) ) {
      print CERT $pemData;
      close(CERT);
      $text = `openssl x509 -in /tmp/cert.pem -text -noout`;
  }


7.2.24. WriteServerKey($hostID, $pemData)

this function writes the server key for the host with $hostID to the right place and sets the SSLCertificateKeyFile directive to the right path. The key must be in PEM format and it can contain the certificate too. If there is a certificate in the PEM data, the SSLCertificateFile directive is set too. The certificate can also be set via WriteServerCert. If the $pemData is undefined, an old key gets deleted and SSLCertificateKeyFile directive gets dropped. Writing the server key does not turn on SSL automatically. On failure, undef is returned. The path for writing the keyfile is /etc/apache2/ssl.key the filename is $hostname-key.pem

Example 199. 

  WriteServerCert('*:443', $certData);
  WriteServerKey('*:443', $keyData);


7.2.25. WriteServerCA($hostID, $pemData)

this function writes the server CA for the host with $hostID to the right place and sets the SSLCACertificateFile directive to the right path. The CA must be in PEM format. If the $pemData is undefined, an old CA file gets deleted and SSLCACertificateFile directive gets dropped. Writing the server CA does not turn on SSL automatically. On failure, undef is returned. The path for writing the ca certificate file is /etc/apache2/ssl.crt the filename is $hostname-cacert.pem

Example 200. 

  WriteServerCA($hostID, $pemData);


7.2.26. $pemData = ReadServerCert($hostID)

this function returns the server certificate PEM data. Even if the key is stored in the same file, just the certificate part is returned. On failure, undef is returned.

Example 201. 

  $pemData = ReadServerCert($hostID);
  if( $pemData and open( CERT, "> /tmp/cert.pem" ) ) {
      print CERT $pemData;
      close(CERT);
      $text = `openssl x509 -in /tmp/cert.pem -text -noout`;
  }


7.2.27. $pemData = ReadServerKey($hostID)

this function returns the server key in PEM format. Even if the certificate is stored in the same file, just the private key part is returned. On failure, undef is returned.

Example 202. 

  $cert = ReadServerCert($hostID);
  $key  = ReadServerKey($hostID);


7.2.28. $pemData = ReadServerCA($hostID)

this function returns the server CA in PEM format. On failure, undef is returned.

Example 203. 

  $CA =  ReadServerCA($hostID);
  if( $CA ) {
      $fingerprint = `echo "$CA"|openssl x509 -fingerprint -noout`;
  }