21 #include <fcgi_stdio.h>
29 reject_user_agent (
const char *user_agent)
31 printf(
"Content-type: text/html\r\n"
34 "<head><title>ERROR</title></head>\n"
35 "<body bgcolor=#ffffff>\n"
36 "<h1>Error - Wrong Browser</h1>\n"
37 "Your browser was deteted to be %s<p>\n"
38 "This server returns finacial data (XML) that only\n"
39 "the GnuCash client understands. You must use GnuCash\n"
46 reject_method (
const char * method)
48 printf(
"Content-type: text/html\r\n"
51 "<head><title>ERROR</title></head>\n"
52 "<body bgcolor=#ffffff>\n"
53 "<h1>Error - Unsupported Method</h1>\n"
54 "Your browser sent METHOD=%s\n"
55 "<p>Only METHOD=POST is supported\n"
61 reject_session (
const char * session)
63 printf(
"Content-type: text/html\r\n"
66 "<head><title>ERROR</title></head>\n"
67 "<body bgcolor=#ffffff>\n"
68 "<h1>Error - Invalid Session ID</h1>\n"
69 "Your browser sent the session id %s\n"
70 "<p>This is not a valid session ID\n"
78 printf(
"Content-type: text/html\r\n"
81 "<head><title>ERROR</title></head>\n"
82 "<body bgcolor=#ffffff>\n"
83 "<h1>Error - Bad Login</h1>\n"
84 "Your supplied a bad username or password\n"
98 GList * logged_in_users = NULL;
108 auth_user (
const char * name,
const char *passwd,
char *buff)
111 const char *session_auth_string;
116 if (!name || !passwd)
return NULL;
119 logged_in_users = g_list_prepend (logged_in_users, guid);
121 guid_to_string_buffer (guid, buff);
132 have_session (
const char *session_auth_string)
135 GList *next = logged_in_users;
141 if (
guid_equal (&guid, next->data))
return TRUE;
153 find_cookie (
const char * cookie_name)
155 const char *cookie_string;
157 len = strlen (cookie_name);
159 cookie_string = getenv (
"HTTP_COOKIE");
160 if (!cookie_string)
return NULL;
162 while (cookie_string)
164 if (!strncmp (cookie_string, cookie_name, len) &&
165 (
'=' == cookie_string[len]))
167 return cookie_string + len + 1;
169 cookie_string = strchr (cookie_string,
';');
170 if (cookie_string) cookie_string ++;
184 parse_for_login (
char * bufp,
char **namep,
char **passwdp)
190 if (!strncmp (bufp,
"name=", 5))
194 else if (!strncmp (bufp,
"passwd=", 7))
199 bufp = strchr (bufp,
'&');
211 main (
int argc,
char *argv[])
213 int err, fake_argc = 1;
214 char * fake_argv[] = {
"hello", 0};
217 char *request_bufp, *reply_bufp;
227 rc = gnc_book_begin (book,
"file:/tmp/demo.gnucash", FALSE);
228 if (!rc)
goto bookerrexit;
230 rc = gnc_book_load (book);
231 if (!rc)
goto bookerrexit;
234 root = gnc_book_get_root_account (book);
239 while (FCGI_Accept() >= 0)
243 const char *request_method;
244 const char *user_agent;
245 const char *auth_string;
246 const char *content_length;
252 user_agent = getenv (
"HTTP_USER_AGENT");
253 if (strncmp (
"gnucash", user_agent, 7))
255 reject_user_agent (user_agent);
260 request_method = getenv (
"REQUEST_METHOD");
261 if (strcmp (
"POST", request_method))
264 reject_method(request_method);
270 auth_string = find_cookie (
"gnc-server");
275 gboolean valid_session;
276 valid_session = have_session (auth_string);
284 reject_session (auth_string);
291 content_length = getenv(
"CONTENT_LENGTH");
292 read_len = atoi (content_length);
295 request_bufp = (
char *) g_malloc (read_len);
296 fread (request_bufp, read_len, 1, stdin);
302 char *name = NULL, *passwd = NULL;
303 parse_for_login (request_bufp, &name, &passwd);
305 auth_string = auth_user (name, passwd, guidstr);
309 g_free (request_bufp);
320 printf(
"Content-type: text/gnc-xml\r\n"
322 "Content-Length: %d\r\n"
330 gncxml_write_account_tree_to_buf(root, &reply_bufp, &sz);
333 printf (
"%s", reply_bufp);
334 g_free (request_bufp);
348 q = gncxml_read_query (request_bufp, read_len);
349 xaccQuerySetGroup (q, root);
362 g_free (request_bufp);
368 err = gnc_book_get_error (book);
371 FCGI_SetExitStatus (500);
373 printf(
"Content-type: text/plain\r\n\r\n"
374 "error was %s\n", strerror (err));
void gnc_engine_shutdown(void)
gboolean string_to_guid(const gchar *string, GncGUID *guid)
QofBook * qof_book_new(void)
api for Version 1 XML-based file format
void qof_query_set_max_results(QofQuery *q, int n)
void qof_query_destroy(QofQuery *q)
gboolean guid_equal(const GncGUID *guid_1, const GncGUID *guid_2)
#define GUID_ENCODING_LENGTH
void gnc_engine_init(int argc, char **argv)
All type declarations for the whole Gnucash engine.
GList * qof_query_run(QofQuery *query)
void qof_book_destroy(QofBook *book)