* Firmed up the login/logout process
authorArt Cancro <ajc@citadel.org>
Sun, 2 Nov 2003 04:02:36 +0000 (04:02 +0000)
committerArt Cancro <ajc@citadel.org>
Sun, 2 Nov 2003 04:02:36 +0000 (04:02 +0000)
* Don't allow load of pages other than login.php logout.php do_login.php
  if the session is not logged in.
* Removed sessionproxy.c

ctdlphp/ChangeLog
ctdlphp/ctdlheader.php
ctdlphp/ctdlprotocol.php
ctdlphp/do_login.php
ctdlphp/login.php
ctdlphp/sessionproxy.c [deleted file]

index ab901a0922e59362115c123f42d03679c68b8739..8ca1c4e6c19081e21a8cd638aef97be9c0204c43 100644 (file)
@@ -1,4 +1,10 @@
  $Log$
+ Revision 1.7  2003/11/02 04:02:36  ajc
+ * Firmed up the login/logout process
+ * Don't allow load of pages other than login.php logout.php do_login.php
+   if the session is not logged in.
+ * Removed sessionproxy.c
+
  Revision 1.6  2003/11/02 02:39:30  ajc
  * Added a proper CVS ID and Copyright disclaimer to every file.  Other
    developers and webmasters working on the project, please add your own
@@ -25,3 +31,4 @@
 
  Revision 1.1  2003/10/31 03:47:13  ajc
  * Initial CVS import
+
index 2bf55be696a76b0ecc30a164290c40aa80a932ea..48ca29c559ffb13e92957e616ae364289b2d8a9b 100644 (file)
@@ -15,12 +15,27 @@ include "ctdlprotocol.php";
 function bbs_page_header() {
        global $session;
 
-       if(strcmp('4.3.0', phpversion()) > 0) {
+       if (strcmp('4.3.0', phpversion()) > 0) {
                die("This program requires PHP 4.3.0 or newer.");
        }
 
        establish_citadel_session();
 
+       // If the user is trying to call up any page other than
+       // login.php logout.php do_login.php,
+       // and the session is not logged in, redirect to login.php
+       //
+       if ($_SESSION["logged_in"] != 1) {
+               $filename = basename(getenv('SCRIPT_NAME'));
+               if (    (strcmp($filename, "login.php"))
+                  &&   (strcmp($filename, "logout.php"))
+                  &&   (strcmp($filename, "do_login.php"))
+               ) {
+                       header("Location: login.php");
+                       exit(0);
+               }
+       }
+
        echo <<<LITERAL
 
 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
index 228404872249d430c08e32e1a106b51ae328ab86..2fd38d4f3a16dae766565b65b1bfd3af10d1f0d8 100644 (file)
@@ -53,6 +53,7 @@ function login_existing_user($user, $pass) {
 
        $_SESSION["username"] = $user;
        $_SESSION["password"] = $pass;
+       become_logged_in(substr($resp, 4));
 
        return array(TRUE, "Login successful.  Have fun.");
 }
@@ -82,11 +83,20 @@ function create_new_user($user, $pass) {
 
        $_SESSION["username"] = $user;
        $_SESSION["password"] = $pass;
+       become_logged_in(substr($resp, 4));
 
        return array(TRUE, "Login successful.  Have fun.");
 }
 
 
+//
+// Code common to both existing-user and new-user logins
+//
+function become_logged_in($server_parms) {
+       $_SESSION["logged_in"] = 1;
+}
+
+
 
 //
 // Learn all sorts of interesting things about the Citadel server to
@@ -108,19 +118,25 @@ function ctdl_get_serv_info() {
 }
 
 
-
+//
+// Temporary function to verify communication with the Citadel server.
+//
 function test_for_echo() {
-
        global $clientsocket, $session;
 
        $command = "ECHO Video vertigo ... test for echo.";
-
        serv_puts($command);
        $response = serv_gets();
        echo $response, "<BR>";
        flush();
 }
 
+
+//
+// Display a system banner.
+// (This is probably temporary because it outputs more or less finalized
+// markup.  For now it's just usable.)
+//
 function ctdl_mesg($msgname) {
        global $clientsocket;
 
@@ -142,4 +158,5 @@ function ctdl_mesg($msgname) {
        }
 }
 
+
 ?>
index 319347de38597ed8fa50e87aad69e69239b82881..4d0751d000a6ff7a6926e436718eb2f3632d5ab5 100644 (file)
@@ -8,31 +8,34 @@
 // This program is released under the terms of the GNU General Public License.
 //
 
-       include "ctdlheader.php";
-       bbs_page_header();
-
-
-       if ($_POST["action"] == "Login") {
-               list($retval, $msg) =
-                       login_existing_user($_POST["user"], $_POST["pass"]);
-                       echo $msg, "<BR>\n";
-       }
-       if ($_POST["action"] == "New User") {
-               list($retval, $msg) =
-                       create_new_user($_POST["user"], $_POST["pass"]);
-                       echo $msg, "<BR>\n";
-       }
-       else {
-               echo "uuuuhhhhhhhh....<BR>\n";
-       }
+include "ctdlheader.php";
+
+bbs_page_header();
+
+if ($_POST["action"] == "Login") {
+       list($retval, $msg) =
+               login_existing_user($_POST["user"], $_POST["pass"]);
+}
+else if ($_POST["action"] == "New User") {
+       list($retval, $msg) =
+               create_new_user($_POST["user"], $_POST["pass"]);
+}
+else {
+       echo "uuuuhhhhhhhhh....<BR>\n" ;
+}
+
+
+if ($retval == FALSE) {
+       echo "<DIV ALIGN=CENTER>", $msg, "<BR><BR>\n" ;
+       echo "<a href=\"login.php\">Try again</A><BR>\n" ;
+       echo "<a href=\"logout.php\">Log out</A><BR>\n" ;
+}
+else {
+       echo "Hi there.  You're online.<BR>\n" ;
+       echo "<a href=\"page2.php\">Page Two</a><BR>\n" ;
+}
+
+bbs_page_footer();
 
 ?>
 
-
-<a href="page2.php">Page Two</a><BR>
-<a href="page3.php">Page Three</a><BR>
-
-<?PHP
-       bbs_page_footer();
-?>
-
index ebf514e63b36106585113b6f4505e5f5cfaed94b..00f25254e4cb6f3609cdc15da2dfbf747a82c4d6 100644 (file)
        <tr><td align=center COLSPAN=2>
        <input type="submit" name="action" value="Login">
        <input type="submit" name="action" value="New User">
-       <input type="submit" name="action" value="Exit">
+       <a href="logout.php">Exit</A>
        </td></tr>
 
        </table>
        </form>
        </div>
 
-<a href="page2.php">Page Two</a><BR>
-<a href="page3.php">Page Three</a><BR>
-
 <?PHP
        bbs_page_footer();
 ?>
diff --git a/ctdlphp/sessionproxy.c b/ctdlphp/sessionproxy.c
deleted file mode 100644 (file)
index 2dd4af9..0000000
+++ /dev/null
@@ -1,370 +0,0 @@
-/*
- * $Id$
- *
- * Session proxy for Citadel PHP bindings
- *
- * This is an unfinished session proxy ... it is a C version of the
- * session proxy implemented in sessionproxy.php ... that version is pure PHP
- * so we should probably stick with it as long as it works.
- *
- * Copyright (c) 2003 by Art Cancro <ajc@uncensored.citadel.org>
- * This program is released under the terms of the GNU General Public License.
- */
-
-#define SIZ 4096
-
-#include <ctype.h>
-#include <stdlib.h>
-#include <unistd.h>
-#include <stdio.h>
-#include <fcntl.h>
-#include <signal.h>
-#include <sys/types.h>
-#include <sys/wait.h>
-#include <sys/socket.h>
-#include <sys/time.h>
-#include <limits.h>
-#include <netinet/in.h>
-#include <arpa/inet.h>
-#include <sys/un.h>
-#include <netdb.h>
-#include <string.h>
-#include <pwd.h>
-#include <errno.h>
-#include <stdarg.h>
-#include <pthread.h>
-#include <signal.h>
-
-
-#ifndef INADDR_NONE
-#define INADDR_NONE 0xffffffff
-#endif
-
-
-void timeout(int signum)
-{
-       fprintf(stderr, "Connection timed out.\n");
-       exit(3);
-}
-
-
-/*
- * Connect a unix domain socket
- */
-int uds_connectsock(char *sockpath)
-{
-       struct sockaddr_un addr;
-       int s;
-
-       memset(&addr, 0, sizeof(addr));
-       addr.sun_family = AF_UNIX;
-       strncpy(addr.sun_path, sockpath, sizeof addr.sun_path);
-
-       s = socket(AF_UNIX, SOCK_STREAM, 0);
-       if (s < 0) {
-               fprintf(stderr, "Can't create socket: %s\n",
-                       strerror(errno));
-               return(-1);
-       }
-
-       if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
-               fprintf(stderr, "Can't connect: %s\n",
-                       strerror(errno));
-               close(s);
-               return(-1);
-       }
-
-       return s;
-}
-
-
-/*
- * Connect a TCP/IP socket
- */
-int tcp_connectsock(char *host, char *service)
-{
-       struct hostent *phe;
-       struct servent *pse;
-       struct protoent *ppe;
-       struct sockaddr_in sin;
-       int s;
-
-       memset(&sin, 0, sizeof(sin));
-       sin.sin_family = AF_INET;
-
-       pse = getservbyname(service, "tcp");
-       if (pse) {
-               sin.sin_port = pse->s_port;
-       } else if ((sin.sin_port = htons((u_short) atoi(service))) == 0) {
-               fprintf(stderr, "Can't get %s service entry\n", service);
-               return (-1);
-       }
-       phe = gethostbyname(host);
-       if (phe) {
-               memcpy(&sin.sin_addr, phe->h_addr, phe->h_length);
-       } else if ((sin.sin_addr.s_addr = inet_addr(host)) == INADDR_NONE) {
-               fprintf(stderr, "Can't get %s host entry: %s\n",
-                       host, strerror(errno));
-               return (-1);
-       }
-       if ((ppe = getprotobyname("tcp")) == 0) {
-               fprintf(stderr, "Can't get TCP protocol entry: %s\n",
-                       strerror(errno));
-               return (-1);
-       }
-
-       s = socket(PF_INET, SOCK_STREAM, ppe->p_proto);
-       if (s < 0) {
-               fprintf(stderr, "Can't create socket: %s\n", strerror(errno));
-               return (-1);
-       }
-       signal(SIGALRM, timeout);
-       alarm(30);
-
-       if (connect(s, (struct sockaddr *) &sin, sizeof(sin)) < 0) {
-               fprintf(stderr, "Can't connect to %s.%s: %s\n",
-                       host, service, strerror(errno));
-               close(s);
-               return (-1);
-       }
-       alarm(0);
-       signal(SIGALRM, SIG_IGN);
-
-       return (s);
-}
-
-
-
-
-/*
- * Input binary data from socket
- */
-int sock_read(int sock, char *buf, int bytes)
-{
-       int len, rlen;
-
-       len = 0;
-       while (len < bytes) {
-               rlen = read(sock, &buf[len], bytes - len);
-               if (rlen < 1) {
-                       fprintf(stderr, "Server connection broken: %s\n",
-                               strerror(errno));
-                       return(-1);
-               }
-               len = len + rlen;
-       }
-       return(len);
-}
-
-
-/*
- * input string from pipe
- */
-int sock_gets(int sock, char *strbuf)
-{
-       int ch, len;
-       char buf[2];
-
-       len = 0;
-       strcpy(strbuf, "");
-       do {
-               if (sock_read(sock, &buf[0], 1) < 0) return(-1);
-               ch = buf[0];
-               strbuf[len++] = ch;
-       } while ((ch != 10) && (ch != 0) && (len < (SIZ-1)));
-       if (strbuf[len-1] == 10) strbuf[--len] = 0;
-       if (strbuf[len-1] == 13) strbuf[--len] = 0;
-       return(len);
-}
-
-
-
-/*
- * send binary to server
- */
-int sock_write(int sock, char *buf, int nbytes)
-{
-       int bytes_written = 0;
-       int retval;
-       while (bytes_written < nbytes) {
-               retval = write(sock, &buf[bytes_written],
-                              nbytes - bytes_written);
-               if (retval < 1) {
-                       fprintf(stderr, "Server connection broken: %s\n",
-                               strerror(errno));
-                       return(-1);
-               }
-               bytes_written = bytes_written + retval;
-       }
-       return(bytes_written);
-}
-
-
-/*
- * send line to server
- */
-int sock_puts(int sock, char *string)
-{
-       char buf[SIZ];
-
-       sprintf(buf, "%s\n", string);
-       return sock_write(sock, buf, strlen(buf));
-}
-
-
-/*
- * Create a Unix domain socket and listen on it
- */
-int ig_uds_server(char *sockpath, int queue_len)
-{
-       struct sockaddr_un addr;
-       int s;
-       int i;
-       int actual_queue_len;
-
-       actual_queue_len = queue_len;
-       if (actual_queue_len < 5) actual_queue_len = 5;
-
-       i = unlink(sockpath);
-       if (i != 0) if (errno != ENOENT) {
-               fprintf(stderr, "can't unlink %s: %s\n",
-                       sockpath, strerror(errno));
-               return(-1);
-       }
-
-       memset(&addr, 0, sizeof(addr));
-       addr.sun_family = AF_UNIX;
-       strncpy(addr.sun_path, sockpath, sizeof addr.sun_path);
-
-       s = socket(AF_UNIX, SOCK_STREAM, 0);
-       if (s < 0) {
-               fprintf(stderr, "Can't create a socket: %s\n",
-                       strerror(errno));
-               return(-1);
-       }
-
-       if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
-               fprintf(stderr, "Can't bind: %s\n",
-                       strerror(errno));
-               return(-1);
-       }
-
-       if (listen(s, actual_queue_len) < 0) {
-               fprintf(stderr, "Can't listen: %s\n", strerror(errno));
-               return(-1);
-       }
-
-       chmod(sockpath, 0700);  /* only me me me can talk to this */
-       return(s);
-}
-
-
-
-/*
- * main loop
- */
-int main(int argc, char **argv) {
-
-       char buf[SIZ];
-       char dbuf[SIZ];
-       int i, f;
-       int ctdl_sock;
-       int listen_sock;
-       int cmd_sock;
-
-       /* Fail if we weren't supplied with the right number of arguments
-        */
-       if (argc != 2) {
-               exit(1);
-       }
-
-       /* Fail if we can't connect to Citadel
-        */
-       ctdl_sock = uds_connectsock("/appl/citadel/citadel.socket");
-       if (ctdl_sock < 0) {
-               exit(2);
-       }
-
-       /* Fail if we can't read the server greeting message
-        */
-       if (sock_gets(ctdl_sock, buf) < 0) {
-               exit(3);
-       }
-
-       /* Fail if the server isn't giving us an error-free startup
-        */
-       if (buf[0] != '2') {
-               exit(4);
-       }
-
-       /* Now we're solid with the Citadel server.  Nice.  It's time to
-        * open our proxy socket so PHP can talk to us.  Fail if we can't
-        * set this up.
-        */
-       listen_sock = ig_uds_server(argv[1], 5);
-       if (listen_sock < 0) {
-               close(ctdl_sock);
-               exit(5);
-       }
-
-       /* The socket is ready to listen for connections, so it's time for
-        * this program to go into the background.  Fork, then close all file
-        * descriptors so that the PHP script that called us can continue
-        * its processing.
-        */
-       f = fork();
-       if (f < 0) {
-               close(ctdl_sock);
-               close(listen_sock);
-               unlink(argv[1]);
-               exit(6);
-       }
-       if (f != 0) {
-               exit(0);
-       }
-       if (f == 0) {
-               setpgrp();
-               for (i=0; i<256; ++i) {
-                       /* Close fd's so PHP doesn't get all, like, whatever */
-                       if ( (i != ctdl_sock) && (i != listen_sock) ) {
-                               close(i);
-                       }
-               }
-       }
-
-       /* Listen for connections. */
-
-       signal(SIGPIPE, SIG_IGN);
-       while (cmd_sock = accept(listen_sock, NULL, 0), cmd_sock >= 0) {
-
-               while (sock_gets(cmd_sock, buf) >= 0) {
-                       if (sock_puts(ctdl_sock, buf) < 0) goto CTDL_BAIL;
-                       if (sock_gets(ctdl_sock, buf) < 0) goto CTDL_BAIL;
-                       sock_puts(cmd_sock, buf);
-
-                       if (buf[0] == '1') do {
-                               if (sock_gets(ctdl_sock, dbuf) < 0) {
-                                       goto CTDL_BAIL;
-                               }
-                               sock_puts(cmd_sock, dbuf);
-                       } while (strcmp(dbuf, "000"));
-
-                       else if (buf[0] == '4') do {
-                               sock_gets(cmd_sock, dbuf);
-                               if (sock_puts(ctdl_sock, dbuf) < 0) {
-                                       goto CTDL_BAIL;
-                               }
-                       } while (strcmp(dbuf, "000"));
-
-               }
-               close(cmd_sock);
-       }
-
-CTDL_BAIL:
-       /* Clean up and go away.
-        */
-       close(ctdl_sock);
-       close(listen_sock);
-       unlink(argv[1]);
-       exit(0);
-}