* BIG rewrite of header handling and slimming of context_loop and session_loop; shuff...
[citadel.git] / webcit / serv_func.c
index 7dbc16c940d10037122e841f8e2df9d115492a78..88289bde65adabc9c242d14b94dac00a303fef1a 100644 (file)
@@ -5,6 +5,8 @@
 #include "webcit.h"
 #include "webserver.h"
 
+int is_uds = 0;
+char serv_sock_name[PATH_MAX] = "";
 
 void DeleteServInfo(ServInfo **FreeMe)
 {
@@ -29,26 +31,26 @@ void DeleteServInfo(ServInfo **FreeMe)
  * browser_host                the citadell we want to connect to
  * user_agent          which browser uses our client?
  */
-ServInfo *get_serv_info(StrBuf *browser_host, char *user_agent)
+ServInfo *get_serv_info(StrBuf *browser_host, StrBuf *user_agent)
 {
        ServInfo *info;
        StrBuf *Buf;
-       char buf[SIZ];
        int a;
 
+       Buf = NewStrBuf();
        /** Tell the server what kind of client is connecting */
        serv_printf("IDEN %d|%d|%d|%s|%s",
                    DEVELOPER_ID,
                    CLIENT_ID,
                    CLIENT_VERSION,
-                   user_agent,
+                   ChrPtr(user_agent),
                    ChrPtr(browser_host)
        );
-       serv_getln(buf, sizeof buf);
+       StrBuf_ServGetln(Buf);
 
        /** Tell the server what kind of richtext we prefer */
-       serv_puts("MSGP text/calendar|text/html|text/plain");
-       serv_getln(buf, sizeof buf);
+       serv_puts("MSGP text/calendar|text/vnote|text/html|text/plain");
+       StrBuf_ServGetln(Buf);
 
        /*
         * Tell the server that when we save a calendar event, we
@@ -56,18 +58,19 @@ ServInfo *get_serv_info(StrBuf *browser_host, char *user_agent)
         * instead of by the client.
         */
        serv_puts("ICAL sgi|1");
-       serv_getln(buf, sizeof buf);
+       StrBuf_ServGetln(Buf);
 
        /** Now ask the server to tell us a little bit about itself... */
        serv_puts("INFO");
-       serv_getln(buf, sizeof buf);
-       if (buf[0] != '1')
+       StrBuf_ServGetln(Buf);
+       if (GetServerStatus(Buf, NULL) != 1) {
+               FreeStrBuf(&Buf);
                return NULL;
+       }
 
        info = (ServInfo*)malloc(sizeof(ServInfo));
        memset(info, 0, sizeof(ServInfo));
        a = 0;
-       Buf = NewStrBuf();
        while (StrBuf_ServGetln(Buf), (strcmp(ChrPtr(Buf), "000")!= 0)) {
 /*             lprintf (1, "a: %d [%s]", a, ChrPtr(Buf));*/
                switch (a) {
@@ -127,7 +130,160 @@ ServInfo *get_serv_info(StrBuf *browser_host, char *user_agent)
        return info;
 }
 
+int GetConnected (void)
+{
+       StrBuf *Buf;
+       wcsession *WCC = WC;
+
+       if (WCC->ReadBuf == NULL)
+               WCC->ReadBuf = NewStrBuf();
+       if (is_uds) /* unix domain socket */
+               WCC->serv_sock = uds_connectsock(serv_sock_name);
+       else        /* tcp socket */
+               WCC->serv_sock = tcp_connectsock(ctdlhost, ctdlport);
+       
+       if (WCC->serv_sock < 0) {
+               do_logout();
+               FreeStrBuf(&WCC->ReadBuf);
+               return 1;
+       }
+       else {
+               long Status;
+               Buf = NewStrBuf();
+               WCC->connected = 1;
+               StrBuf_ServGetln(Buf);  /* get the server greeting */
+               GetServerStatus(Buf, &Status);
+               FreeStrBuf(&Buf);
+               /* Are there too many users already logged in? */
+               if (Status == 571) {
+                       wprintf(_("This server is already serving its maximum number of users and cannot accept any additional logins at this time.  Please try again later or contact your system administrator."));
+                       end_burst();
+                       end_webcit_session();
+                       return 1;
+               }
+
+               /*
+                * From what host is our user connecting?  Go with
+                * the host at the other end of the HTTP socket,
+                * unless we are following X-Forwarded-For: headers
+                * and such a header has already turned up something.
+                */
+               if ( (!follow_xff) || (StrLength(WCC->Hdr->browser_host) == 0) ) {
+                       if (WCC->Hdr->browser_host == NULL) {
+                               WCC->Hdr->browser_host = NewStrBuf();
+                               Put(WCC->Hdr->HTTPHeaders, HKEY("FreeMeWithTheOtherHeaders"), 
+                                   WCC->Hdr->browser_host, HFreeStrBuf);
+                       }
+                       locate_host(WCC->Hdr->browser_host, WCC->Hdr->http_sock);
+               }
+               if (WCC->serv_info == NULL)
+                       WCC->serv_info = get_serv_info(WCC->Hdr->browser_host, WCC->Hdr->user_agent);
+               if (WCC->serv_info == NULL){
+                       begin_burst();
+                       wprintf(_("Received unexpected answer from Citadel "
+                                 "server; bailing out."));
+                       hprintf("HTTP/1.1 200 OK\r\n");
+                       hprintf("Content-type: text/plain; charset=utf-8\r\n");
+                       end_burst();
+                       end_webcit_session();
+                       return 1;
+               }
+               if (WCC->serv_info->serv_rev_level < MINIMUM_CIT_VERSION) {
+                       begin_burst();
+                       wprintf(_("You are connected to a Citadel "
+                                 "server running Citadel %d.%02d. \n"
+                                 "In order to run this version of WebCit "
+                                 "you must also have Citadel %d.%02d or"
+                                 " newer.\n\n\n"),
+                               WCC->serv_info->serv_rev_level / 100,
+                               WCC->serv_info->serv_rev_level % 100,
+                               MINIMUM_CIT_VERSION / 100,
+                               MINIMUM_CIT_VERSION % 100
+                               );
+                       hprintf("HTTP/1.1 200 OK\r\n");
+                       hprintf("Content-type: text/plain; charset=utf-8\r\n");
+                       end_burst();
+                       end_webcit_session();
+                       return 1;
+               }
+       }
+       if (WCC->ReadBuf == NULL)
+               WCC->ReadBuf = NewStrBuf();
+       if (is_uds)/* unix domain socket */
+               WCC->serv_sock = uds_connectsock(serv_sock_name);
+       else /* tcp socket */
+               WCC->serv_sock = tcp_connectsock(ctdlhost, ctdlport);
+       
+       if (WCC->serv_sock < 0) {
+               do_logout();
+               FreeStrBuf(&WCC->ReadBuf);
+               return 1;
+       }
+       else {
+               long Status;
+               StrBuf *Buf;
+
+               Buf = NewStrBuf();
+               WCC->connected = 1;
+               StrBuf_ServGetln(Buf);
+               GetServerStatus(Buf,&Status);
+               /* get the server greeting */
+               
+               /* Are there too many users already logged in? */
+               if (Status == 571) {
+                       wprintf(_("This server is already serving its maximum number of users and cannot accept any additional logins at this time.  Please try again later or contact your system administrator."));
+                       end_burst();
+                       end_webcit_session();
+                       return 1;
+               }
 
+               /*
+                * From what host is our user connecting?  Go with
+                * the host at the other end of the HTTP socket,
+                * unless we are following X-Forwarded-For: headers
+                * and such a header has already turned up something.
+                */
+               if ( (!follow_xff) || (StrLength(WCC->Hdr->browser_host) == 0) ) {
+                       if (WCC->Hdr->browser_host == NULL) {
+                               WCC->Hdr->browser_host = NewStrBuf();
+                               Put(WCC->Hdr->HTTPHeaders, HKEY("FreeMeWithTheOtherHeaders"), 
+                                   WCC->Hdr->browser_host, HFreeStrBuf);
+                       }
+                       locate_host(WCC->Hdr->browser_host, WCC->Hdr->http_sock);
+               }
+               if (WCC->serv_info == NULL)
+                       WCC->serv_info = get_serv_info(WCC->Hdr->browser_host, WCC->Hdr->user_agent);
+               if (WCC->serv_info == NULL){
+                       begin_burst();
+                       wprintf(_("Received unexpected answer from Citadel "
+                                 "server; bailing out."));
+                       hprintf("HTTP/1.1 200 OK\r\n");
+                       hprintf("Content-type: text/plain; charset=utf-8\r\n");
+                       end_burst();
+                       end_webcit_session();
+                       return 1;
+               }
+               if (WCC->serv_info->serv_rev_level < MINIMUM_CIT_VERSION) {
+                       begin_burst();
+                       wprintf(_("You are connected to a Citadel "
+                                 "server running Citadel %d.%02d. \n"
+                                 "In order to run this version of WebCit "
+                                 "you must also have Citadel %d.%02d or"
+                                 " newer.\n\n\n"),
+                               WCC->serv_info->serv_rev_level / 100,
+                               WCC->serv_info->serv_rev_level % 100,
+                               MINIMUM_CIT_VERSION / 100,
+                               MINIMUM_CIT_VERSION % 100
+                               );
+                       hprintf("HTTP/1.1 200 OK\r\n");
+                       hprintf("Content-type: text/plain; charset=utf-8\r\n");
+                       end_burst();
+                       end_webcit_session();
+                       return 1;
+               }
+       }
+       return 0;
+}
 
 /**
  *  Read Citadel variformat text and spit it out as HTML.
@@ -632,6 +788,9 @@ void
 InitModule_SERVFUNC
 (void)
 {
+       is_uds = strcasecmp(ctdlhost, "uds") == 0;
+       if (is_uds)
+               snprintf(serv_sock_name, PATH_MAX, "%s/citadel.socket", ctdlport);
 
        RegisterConditional(HKEY("COND:SERV:OPENID"), 2, conditional_serv_supports_openid, CTX_NONE);
        RegisterConditional(HKEY("COND:SERV:NEWU"), 2, conditional_serv_newuser_disabled, CTX_NONE);
@@ -646,4 +805,12 @@ InitModule_SERVFUNC
 /*TODO //      RegisterNamespace("SERV:LDAP_SUPP", 0, 0, tmplput_serv_ldap_enabled, 0); */
 }
 
+
+
+void 
+SessionDestroyModule_SERVFUNC
+(wcsession *sess)
+{
+       DeleteServInfo(&sess->serv_info);
+}
 /*@}*/