Difference between revisions of "Apache Backdoor Modul"

From C4 Wiki
Jump to: navigation, search
(Webserver aufsetzen)
(Webserver aufsetzen)
Line 20: Line 20:
 
  hund@surfer:~$ man apxs2  
 
  hund@surfer:~$ man apxs2  
  
 +
=== Code ausführen ===
 +
 +
Zunächst erstelle ich eine Datei <tt>mod_hello.c</tt> in meinem home-Directory. Der Quellcode ist im Tutorial zu finden. Leider ist dieser fehlerhaft, hier die korrigierte und leicht gekürzte Fassung:
 +
 +
<pre>
 +
#include "httpd.h"
 +
#include "http_config.h"
 +
#include "http_core.h"
 +
#include "http_log.h"
 +
#include "http_main.h"
 +
#include "http_protocol.h"
 +
#include "http_request.h"
 +
#include "util_script.h"
 +
#include "http_connection.h"
 +
 +
/* This example just takes a pointer to the request record as its only
 +
* argument */
 +
static int hello_handler(request_rec *r)
 +
{
 +
 +
        /* We decline to handle a request if hello-handler is not the value
 +
        * of r->handler */
 +
        if (strcmp(r->handler, "hello-handler")) {
 +
                return DECLINED;
 +
        }
 +
 +
        /* The following line just prints a message to the errorlog */
 +
        ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_NOTICE, 0, r->server,
 +
        "mod_hello: %s", "Before content is output");
 +
 +
        /* We set the content type before doing anything else */
 +
        ap_set_content_type(r, "text/html");
 +
 +
        /* If the request is for a header only, and not a request for
 +
        * the whole content, then return OK now. We don't have to do
 +
        * anything else. */
 +
        if (r->header_only) {
 +
                return OK;
 +
        }
 +
 +
        /* Now we just print the contents of the document using the
 +
        * ap_rputs and ap_rprintf functions. More information about
 +
        * the use of these can be found in http_protocol.h */
 +
        ap_rputs("<HTML>\n", r);
 +
ap_rputs("\t<HEAD>\n", r);
 +
ap_rputs("\t\t<TITLE>\n\t\t\tHello There\n\t\t</TITLE>\n", r);
 +
ap_rputs("\t</HEAD>\n\n", r);
 +
ap_rputs("<BODY BGCOLOR=\"#FFFFFF\>"\n" ,r);
 +
ap_rputs("<H1>Hello </H1>\n", r);
 +
ap_rputs("Hello world\n", r);
 +
ap_rprintf(r, "<br>A sample line generated by ap_rprintf<br>\n");
 +
ap_rputs("</BODY></HTML>\n" ,r);
 +
 +
        /* We can either return OK or DECLINED at this point. If we return
 +
        * OK, then no other modules will attempt to process this request */
 +
        return OK;
 +
}
 +
 +
 +
/* Each function our module provides to handle a particular hook is
 +
* specified here. See mod_example.c for more information about this
 +
* step. Suffice to say that we need to list all of our handlers in
 +
* here. */
 +
static void x_register_hooks(apr_pool_t *p)
 +
{
 +
        ap_hook_handler(hello_handler, NULL, NULL, APR_HOOK_MIDDLE);
 +
}
 +
 +
 +
/* Module definition for configuration. We list all callback routines
 +
* that we provide the static hooks into our module from the other parts
 +
* of the server. This list tells the server what function will handle a
 +
* particular type or stage of request. If we don't provide a function
 +
* to handle a particular stage / callback, use NULL as a placeholder as
 +
* illustrated below. */
 +
module AP_MODULE_DECLARE_DATA hello_module =
 +
{
 +
        STANDARD20_MODULE_STUFF,
 +
        NULL, /* per-directory config creator */
 +
        NULL, /* directory config merger */
 +
        NULL, /* server config creator */
 +
        NULL, /* server config merger */
 +
        NULL, /* command table */
 +
        x_register_hooks, /* other request processing hooks */
 +
};
 +
</pre>
 +
 +
 +
ToDo: compile and run
  
 
''lama''
 
''lama''

Revision as of 21:08, 5 October 2011

Hello, world

Hier scheint ein brauchbares Tutorial für die erste Erfahrung zu liegen: http://www.penguinpowered.org/documentation/apache2_modules.html

Werde das jetzt mal durcharbeiten und meine Erfahrungen hier dokumentieren.

Webserver aufsetzen

Für dieses Beispiel habe ich einen Ubuntu Server frisch aufgesetzt. Bei abweichender Ausgangslage bitte entsprechend improvisieren.

Nach der Installation brauchen wir erstmal einen Apache2. Damit wir an das apxs-Kommando aus dem Tutorial kommen holen wir uns noch ein Zusatzpaket:

hund@surfer:~$ sudo apt-get install apache2 apache2-threaded-dev

Je nach verwendetem MPM scheint man statt apache2-threaded-dev auch apache2-prefork-dev verwenden zu können.

Die Standardkonfiguration vom Apachen sollte erstmal genügen --> Websites liegen in /var/www
Zuerst empfiehlt sich ein kurzer Blick in die Manpage.

hund@surfer:~$ man apxs2 

Code ausführen

Zunächst erstelle ich eine Datei mod_hello.c in meinem home-Directory. Der Quellcode ist im Tutorial zu finden. Leider ist dieser fehlerhaft, hier die korrigierte und leicht gekürzte Fassung:

#include "httpd.h" 
#include "http_config.h" 
#include "http_core.h" 
#include "http_log.h" 
#include "http_main.h" 
#include "http_protocol.h" 
#include "http_request.h" 
#include "util_script.h" 
#include "http_connection.h" 

/* This example just takes a pointer to the request record as its only 
* argument */ 
static int hello_handler(request_rec *r) 
{ 

        /* We decline to handle a request if hello-handler is not the value 
         * of r->handler */ 
        if (strcmp(r->handler, "hello-handler")) { 
                return DECLINED; 
        } 

        /* The following line just prints a message to the errorlog */ 
        ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_NOTICE, 0, r->server, 
        "mod_hello: %s", "Before content is output"); 

        /* We set the content type before doing anything else */ 
        ap_set_content_type(r, "text/html"); 

        /* If the request is for a header only, and not a request for 
         * the whole content, then return OK now. We don't have to do 
         * anything else. */ 
        if (r->header_only) { 
                return OK; 
        } 

        /* Now we just print the contents of the document using the 
         * ap_rputs and ap_rprintf functions. More information about 
         * the use of these can be found in http_protocol.h */ 
        ap_rputs("<HTML>\n", r);
	ap_rputs("\t<HEAD>\n", r);
	ap_rputs("\t\t<TITLE>\n\t\t\tHello There\n\t\t</TITLE>\n", r);
	ap_rputs("\t</HEAD>\n\n", r);
	ap_rputs("<BODY BGCOLOR=\"#FFFFFF\>"\n" ,r);
	ap_rputs("<H1>Hello </H1>\n", r);
	ap_rputs("Hello world\n", r);
	ap_rprintf(r, "<br>A sample line generated by ap_rprintf<br>\n");
	ap_rputs("</BODY></HTML>\n" ,r); 

        /* We can either return OK or DECLINED at this point. If we return 
        * OK, then no other modules will attempt to process this request */ 
        return OK; 
} 


/* Each function our module provides to handle a particular hook is 
* specified here. See mod_example.c for more information about this 
* step. Suffice to say that we need to list all of our handlers in 
* here. */ 
static void x_register_hooks(apr_pool_t *p) 
{ 
        ap_hook_handler(hello_handler, NULL, NULL, APR_HOOK_MIDDLE); 
} 


/* Module definition for configuration. We list all callback routines 
* that we provide the static hooks into our module from the other parts 
* of the server. This list tells the server what function will handle a 
* particular type or stage of request. If we don't provide a function 
* to handle a particular stage / callback, use NULL as a placeholder as 
* illustrated below. */ 
module AP_MODULE_DECLARE_DATA hello_module = 
{ 
        STANDARD20_MODULE_STUFF, 
        NULL, /* per-directory config creator */ 
        NULL, /* directory config merger */ 
        NULL, /* server config creator */ 
        NULL, /* server config merger */ 
        NULL, /* command table */ 
        x_register_hooks, /* other request processing hooks */ 
}; 


ToDo: compile and run

lama