244 lines
5.7 KiB
C
244 lines
5.7 KiB
C
|
|
|
|
#include "./service_file.h"
|
|
#include "aqhome-cgi/modules/mroot.h"
|
|
#include "aqhome-cgi/modules/common/madmin.h"
|
|
#include "aqhome-cgi/modules/common/mmodules.h"
|
|
#include "aqhome-cgi/modules/common/musers.h"
|
|
#include "aqhome-cgi/modules/mdevices.h"
|
|
#include "aqhome/aqhome.h"
|
|
|
|
#include <aqcgi/cgi.h>
|
|
#include <aqcgi/request.h>
|
|
|
|
#include <gwenhywfar/gwenhywfar.h>
|
|
#include <gwenhywfar/nogui.h>
|
|
#include <gwenhywfar/logger.h>
|
|
#include <gwenhywfar/text.h>
|
|
#include <gwenhywfar/debug.h>
|
|
|
|
#include <string.h>
|
|
#include <errno.h>
|
|
#include <stdio.h>
|
|
|
|
|
|
#define AQHOME_CGI_LOGFILE "/var/www/aqhome-cgi/log/aqhome-cgi.log"
|
|
|
|
#define AQHOME_CGI_DEFAULT_STATIC_FILES AQHOME_CGI_WWWDIR"/static"
|
|
#define AQHOME_CGI_DEFAULT_RUNTIME_FILES AQHOME_CGI_WWWDIR"/data"
|
|
|
|
|
|
|
|
static void _handleRequest(AQCGI_REQUEST *rq, const char *sPathStaticFiles, const char *sPathRuntimeFiles);
|
|
static int _handlePath(AQH_SERVICE *sv, AQCGI_REQUEST *rq, const char *sPathStaticFiles);
|
|
static void logStart(void);
|
|
static int _init(const char *sPathRuntimeFiles);
|
|
|
|
|
|
|
|
int main(int argc, char **argv)
|
|
{
|
|
GWEN_GUI *gui;
|
|
const char *sPathStaticFiles;
|
|
const char *sPathRuntimeFiles;
|
|
|
|
sPathStaticFiles=getenv("AQHOME_STATIC_FILES");
|
|
if (!(sPathStaticFiles && *sPathStaticFiles))
|
|
sPathStaticFiles=AQHOME_CGI_DEFAULT_STATIC_FILES;
|
|
|
|
sPathRuntimeFiles=getenv("AQHOME_RUNTIME_FILES");
|
|
if (!(sPathRuntimeFiles && *sPathRuntimeFiles))
|
|
sPathRuntimeFiles=AQHOME_CGI_DEFAULT_RUNTIME_FILES;
|
|
|
|
GWEN_Init();
|
|
gui=GWEN_NoGui_new();
|
|
GWEN_Gui_SetGui(gui);
|
|
|
|
logStart();
|
|
GWEN_Logger_Open(GWEN_LOGDOMAIN, "gwenhywfar", AQHOME_CGI_LOGFILE, GWEN_LoggerType_File, GWEN_LoggerFacility_Daemon);
|
|
GWEN_Logger_Open(AQH_LOGDOMAIN, "aqhome", AQHOME_CGI_LOGFILE, GWEN_LoggerType_File, GWEN_LoggerFacility_Daemon);
|
|
GWEN_Logger_Open(AQCGI_LOGDOMAIN, "aqcgi", AQHOME_CGI_LOGFILE, GWEN_LoggerType_File, GWEN_LoggerFacility_Daemon);
|
|
GWEN_Logger_Open(NULL, "aqhome-cgi", AQHOME_CGI_LOGFILE, GWEN_LoggerType_File, GWEN_LoggerFacility_Daemon);
|
|
|
|
GWEN_Logger_SetLevel(GWEN_LOGDOMAIN, GWEN_LoggerLevel_Debug);
|
|
GWEN_Logger_SetLevel(AQCGI_LOGDOMAIN, GWEN_LoggerLevel_Debug);
|
|
GWEN_Logger_SetLevel(NULL, GWEN_LoggerLevel_Debug);
|
|
|
|
GWEN_Logger_Close(GWEN_LOGDOMAIN);
|
|
GWEN_Logger_Open(GWEN_LOGDOMAIN, "gwenhywfar", AQHOME_CGI_LOGFILE, GWEN_LoggerType_File, GWEN_LoggerFacility_Daemon);
|
|
|
|
GWEN_Logger_SetLevel(GWEN_LOGDOMAIN, GWEN_LoggerLevel_Debug);
|
|
GWEN_Logger_SetLevel(AQCGI_LOGDOMAIN, GWEN_LoggerLevel_Debug);
|
|
GWEN_Logger_SetLevel(NULL, GWEN_LoggerLevel_Debug);
|
|
|
|
if (argc>1 && argv[1]) {
|
|
if (0==strcasecmp(argv[1], "init")) {
|
|
int rv;
|
|
|
|
rv=_init(sPathRuntimeFiles);
|
|
if (rv<0) {
|
|
fprintf(stderr, "Error on init (%d)\n", rv);
|
|
return 2;
|
|
}
|
|
|
|
}
|
|
}
|
|
else {
|
|
AQCGI_REQUEST *rq;
|
|
|
|
DBG_ERROR(NULL, "Init CGI");
|
|
AQCGI_Init();
|
|
rq=AQCGI_ReadRequest();
|
|
if (rq) {
|
|
_handleRequest(rq, sPathStaticFiles, sPathRuntimeFiles);
|
|
}
|
|
else {
|
|
fprintf(stdout, "Content-type: text/plain\n\n");
|
|
fprintf(stdout, "Error: No Request!\n");
|
|
}
|
|
AQCGI_Fini();
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
|
|
|
|
void _handleRequest(AQCGI_REQUEST *rq, const char *sPathStaticFiles, const char *sPathRuntimeFiles)
|
|
{
|
|
AQH_SERVICE *sv;
|
|
int rv;
|
|
|
|
sv=AQH_ServiceFiles_new(sPathRuntimeFiles);
|
|
|
|
rv=_handlePath(sv, rq, sPathStaticFiles);
|
|
if (rv<0) {
|
|
DBG_INFO(NULL, "here (%d)", rv);
|
|
}
|
|
AQH_Service_free(sv);
|
|
}
|
|
|
|
|
|
|
|
int _handlePath(AQH_SERVICE *sv, AQCGI_REQUEST *rq, const char *sPathStaticFiles)
|
|
{
|
|
AQH_MODULE *mRoot;
|
|
AQH_MODULE *mParent;
|
|
AQH_SESSION *session;
|
|
const GWEN_STRINGLIST *sl;
|
|
|
|
mRoot=AQH_ModRoot_new(sv, sPathStaticFiles);
|
|
mParent=mRoot;
|
|
session=AQH_ModService_ReadSession(mRoot, rq);
|
|
AQH_ModService_CalcSessionModPerms(mRoot, session);
|
|
|
|
sl=AQCGI_Request_GetStringlistPath(rq);
|
|
if (sl) {
|
|
GWEN_STRINGLISTENTRY *se;
|
|
|
|
se=GWEN_StringList_FirstEntry(sl);
|
|
while(se) {
|
|
GWEN_STRINGLISTENTRY *seNext;
|
|
const char *s;
|
|
|
|
seNext=GWEN_StringListEntry_Next(se);
|
|
s=GWEN_StringListEntry_Data(se);
|
|
if (s && *s) {
|
|
if (seNext) {
|
|
AQH_MODULE *m;
|
|
|
|
DBG_ERROR(NULL, "Entry: %s (%s)", s, seNext?"not last":"last");
|
|
m=AQH_ModService_LoadSubModule(mParent, rq, session, s);
|
|
if (m==NULL) {
|
|
AQH_Session_free(session);
|
|
AQH_Module_free(mRoot);
|
|
return GWEN_ERROR_GENERIC;
|
|
}
|
|
mParent=m;
|
|
}
|
|
else {
|
|
int rv;
|
|
|
|
/* last, let module handle remaining part */
|
|
rv=AQH_ModService_HandleRequest(mParent, rq, session, s);
|
|
if (rv<0) {
|
|
DBG_INFO(NULL, "here (%d)", rv);
|
|
AQH_Session_free(session);
|
|
AQH_Module_free(mRoot);
|
|
return rv;
|
|
}
|
|
break;
|
|
}
|
|
}
|
|
|
|
se=seNext;
|
|
}
|
|
AQH_Session_free(session);
|
|
AQH_Module_free(mRoot);
|
|
return 0;
|
|
}
|
|
else {
|
|
AQH_Session_free(session);
|
|
AQH_Module_free(mRoot);
|
|
return GWEN_ERROR_GENERIC;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
void logStart()
|
|
{
|
|
FILE *f;
|
|
|
|
f=fopen(AQHOME_CGI_LOGFILE, "a+");
|
|
if (f!=NULL) {
|
|
fprintf(f, "Started.\n");
|
|
fclose(f);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
int _init(const char *sPathRuntimeFiles)
|
|
{
|
|
AQH_SERVICE *sv;
|
|
int rv;
|
|
|
|
fprintf(stdout, "Creating aqhome-cgi environment in \"%s\"\n", sPathRuntimeFiles);
|
|
sv=AQH_ServiceFiles_new(sPathRuntimeFiles);
|
|
rv=AQH_ModAdmin_Create(sv);
|
|
if (rv<0) {
|
|
DBG_ERROR(NULL, "Error creating module \"admin\"");
|
|
AQH_Service_free(sv);
|
|
return rv;
|
|
}
|
|
|
|
rv=AQH_ModAdmModules_Create(sv);
|
|
if (rv<0) {
|
|
DBG_ERROR(NULL, "Error creating module \"modules\"");
|
|
AQH_Service_free(sv);
|
|
return rv;
|
|
}
|
|
|
|
rv=AQH_ModAdmUsers_Create(sv);
|
|
if (rv<0) {
|
|
DBG_ERROR(NULL, "Error creating module \"users\"");
|
|
AQH_Service_free(sv);
|
|
return rv;
|
|
}
|
|
|
|
rv=AQH_ModDevices_Create(sv);
|
|
if (rv<0) {
|
|
DBG_ERROR(NULL, "Error creating module \"devices\"");
|
|
AQH_Service_free(sv);
|
|
return rv;
|
|
}
|
|
|
|
AQH_Service_free(sv);
|
|
return 0;
|
|
}
|
|
|
|
|
|
|
|
|