Back to index

webcit  8.12-dfsg
cookie_conversion.c
Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 1996-2012 by the citadel.org team
00003  *
00004  * This program is open source software.  You can redistribute it and/or
00005  * modify it under the terms of the GNU General Public License, version 3.
00006  *
00007  * This program is distributed in the hope that it will be useful,
00008  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00009  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00010  * GNU General Public License for more details.
00011  */
00012 
00013 #include "webcit.h"
00014 
00015 /*
00016  * String to unset the cookie.
00017  * Any date "in the past" will work, so I chose my birthday, right down to
00018  * the exact minute.  :)
00019  */
00020 static char *unset = "; expires=28-May-1971 18:10:00 GMT";
00021 typedef unsigned char byte;       /* Byte type used by cookie_to_stuff() */
00022 extern const char *get_selected_language(void);
00023 
00024 /*
00025  * Pack all session info into one easy-to-digest cookie. Healthy and delicious!
00026  */
00027 void stuff_to_cookie(int unset_cookies)
00028 {
00029        wcsession *WCC = WC;
00030        char buf[SIZ];
00031 
00032        if (unset_cookies) {
00033               hprintf("Set-cookie: webcit=%s; path=/\r\n", unset);
00034        }
00035        else
00036        {
00037               StrBufAppendPrintf(WCC->HBuf, "Set-cookie: webcit=");
00038               snprintf(buf, sizeof(buf), "%d", WCC->wc_session);
00039               StrBufHexescAppend(WCC->HBuf, NULL, buf);
00040               StrBufHexescAppend(WCC->HBuf, NULL, "|");
00041               StrBufHexescAppend(WCC->HBuf, WCC->wc_username, NULL);
00042               StrBufHexescAppend(WCC->HBuf, NULL, "|");
00043               StrBufHexescAppend(WCC->HBuf, WCC->wc_password, NULL);
00044               StrBufHexescAppend(WCC->HBuf, NULL, "|");
00045               StrBufHexescAppend(WCC->HBuf, WCC->CurRoom.name, NULL);
00046               StrBufHexescAppend(WCC->HBuf, NULL, "|");
00047               StrBufHexescAppend(WCC->HBuf, NULL, get_selected_language());
00048               StrBufHexescAppend(WCC->HBuf, NULL, "|");
00049 
00050               if (server_cookie != NULL) {
00051                      StrBufAppendPrintf(WCC->HBuf, 
00052                                       ";path=/ \r\n%s\r\n", 
00053                                       server_cookie);
00054               }
00055               else {
00056                      StrBufAppendBufPlain(WCC->HBuf,
00057                                         HKEY("; path=/\r\n"), 0);
00058               }
00059        }
00060 }
00061 
00062 /*
00063  * Extract all that fun stuff out of the cookie.
00064  */
00065 void cookie_to_stuff(StrBuf *cookie,
00066                      int *session,
00067                      StrBuf *user,
00068                      StrBuf *pass,
00069                      StrBuf *room,
00070                      StrBuf *language)
00071 {
00072        if (session != NULL) {
00073               *session = StrBufExtract_int(cookie, 0, '|');
00074        }
00075        if (user != NULL) {
00076               StrBufExtract_token(user, cookie, 1, '|');
00077        }
00078        if (pass != NULL) {
00079               StrBufExtract_token(pass, cookie, 2, '|');
00080        }
00081        if (room != NULL) {
00082               StrBufExtract_token(room, cookie, 3, '|');
00083        }
00084        if (language != NULL) {
00085               StrBufExtract_token(language, cookie, 4, '|');
00086        }
00087 }