Enlightenment CVS committal

Author  : xcomputerman
Project : e17
Module  : apps/entrance

Dir     : e17/apps/entrance/src/daemon


Modified Files:
        Makefile.am auth.c spawner.c 
Added Files:
        Entranced.h 
Removed Files:
        spawner.h 


Log Message:
More Xauth code

===================================================================
RCS file: /cvsroot/enlightenment/e17/apps/entrance/src/daemon/Makefile.am,v
retrieving revision 1.6
retrieving revision 1.7
diff -u -3 -r1.6 -r1.7
--- Makefile.am 1 Jan 2004 07:48:54 -0000       1.6
+++ Makefile.am 22 Mar 2004 06:51:39 -0000      1.7
@@ -1,12 +1,12 @@
 ## Process this file with automake to produce Makefile.in
 
-INCLUDES = @ecore_cflags@ @edb_cflags@
+INCLUDES = @x_cflags@ @ecore_cflags@ @edb_cflags@
 
 sbin_PROGRAMS = entranced
 bin_SCRIPTS = entrance_wrapper
 
 entranced_SOURCES = \
-       spawner.c spawner.h md5.c md5.h auth.c
+       Entranced.h spawner.c md5.c md5.h auth.c util.c
 
-entranced_LDADD = @ecore_libs@ @edb_libs@
+entranced_LDADD = @x_libs@ -lXau @ecore_libs@ @edb_libs@
 
===================================================================
RCS file: /cvsroot/enlightenment/e17/apps/entrance/src/daemon/auth.c,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -3 -r1.2 -r1.3
--- auth.c      16 Jan 2004 19:20:41 -0000      1.2
+++ auth.c      22 Mar 2004 06:51:39 -0000      1.3
@@ -7,16 +7,20 @@
 #include <X11/Xlib.h>
 #include <X11/Xauth.h>
 #include <X11/extensions/security.h>
+#include <X11/Xos.h>
 
 #include <Ecore_X.h>
 #include <Ecore_Ipc.h>
 
-#include "spawner.h"
+#include "Entranced.h"
+#include "util.h"
 #include "md5.h"
 
 #define BUFSIZE 512
 
-char * entranced_cookie_new (void)
+#define AUTH_DATA_LEN 16
+
+char * entranced_cookie_new (void) 
 {
    int fd;
    int r, i;
@@ -28,13 +32,13 @@
    entranced_md5_init(ctx);
 
    if ((fd = open("/dev/urandom", O_RDONLY|O_NONBLOCK)) < 0) {
-      _DEBUG("Cookie generation failed: could not open /dev/urandom\n");
-      exit(-1);
+      entranced_debug("Cookie generation failed: could not open /dev/urandom\n");
+      return NULL;
    }
 
    if((r = read(fd, buf, sizeof(buf))) <= 0) {
-      _DEBUG("Cookie generation failed: could not read /dev/urandom\n");
-      exit(-1);
+      entranced_debug("Cookie generation failed: could not read /dev/urandom\n");
+      return NULL;
    }
 
    entranced_md5_update(ctx, buf, r);
@@ -51,21 +55,322 @@
    return cookie;
 }
 
-static int _entranced_auth_entry_add() {
-   /* Code */
-   return 0;
+static void
+_entranced_auth_purge(Entranced_Display *d, FILE *auth_file)
+{
+   Xauth             *auth;
+   Ecore_List_Node   *li;
+   Ecore_List        *auth_keep = NULL;
+   
+   if (!d || !auth_file)
+      return;
+
+   entranced_debug("entranced_auth_purge: %s", d->name);
+   fseek (auth_file, 0L, SEEK_SET);
+   
+   auth_keep = ecore_list_new();
+
+   /* Read each auth entry and check if it matches one for this
+    * display */
+   while ((auth = XauReadAuth(auth_file)))
+   {
+      int match;
+      match = FALSE;
+
+      for (li = d->auths->first; li; li = li->next)
+      {
+         Xauth *disp_auth = (Xauth *) li->data;
+         if (!memcmp(disp_auth->address, auth->address, auth->address_length) &&
+               !memcmp(disp_auth->number, auth->number, auth->number_length))
+            match = TRUE;
+      }
+
+      if (match)
+         XauDisposeAuth(auth);
+      else
+         ecore_list_append(auth_keep, auth);
+   }
+
+   /* Write remaining entries to auth file */
+   if (!(auth_file = freopen(d->user_authfile, "w", auth_file)))
+   {
+      entranced_debug("entranced_auth_purge: Write failed!\n");
+      return;
+   }
+   
+   while(auth_keep->nodes)
+   {
+      Xauth *xa;
+      xa = (Xauth *) ecore_list_remove(auth_keep);
+      XauWriteAuth(auth_file, (Xauth *) xa);
+      XauDisposeAuth((Xauth *) xa);
+   }
+
+   ecore_list_destroy(auth_keep);
+      
+}
+
+/**
+ * Generate a new MIT-MAGIC-COOKIE-1 Xauth struct
+ *
+ * @return  A pointer to a new Xauth struct with cookie data, or NULL if
+ *          generation failed.
+ */
+Xauth * 
+entranced_auth_mit_get(void)
+{
+   Xauth    *new;
+   char     *cookie;
+
+   new = (Xauth *) malloc(sizeof(Xauth));
+
+   if (!new) return NULL;
+
+   new->family = FamilyWild;
+   new->address_length = 0;
+   new->address = 0;
+   new->number_length = 0;
+   new->number = 0;
+
+   new->data = (char *) malloc(AUTH_DATA_LEN);
+   if(!new->data)
+   {
+      free(new);
+      return NULL;
+   }
+   
+   new->name = strdup("MIT-MAGIC-COOKIE-1");
+   new->name_length = 18;
+   cookie = entranced_cookie_new();
+   if (!cookie)
+   {
+      free(new->name);
+      free(new->data);
+      free(new);
+      return NULL;
+   }
+   
+   memcpy(new->data, cookie, AUTH_DATA_LEN);
+   new->data_length = AUTH_DATA_LEN;
+
+   free(cookie);
+   
+   return new;
+}
+
+static Xauth *
+_entranced_auth_generate(void)
+{
+   Xauth          *auth = NULL;
+   unsigned short i;
+
+   auth = entranced_auth_mit_get();
+   if (auth)
+   {
+      entranced_debug("Generated MIT-MAGIC-COOKIE-1 ");
+      for (i = 0; i < auth->data_length; ++i)
+         entranced_debug(" %02x", auth->data[i] & 0xff);
+      entranced_debug("\n");
+   }
+   else
+      entranced_debug("Auth generation failed.\n");
+
+   return auth;
 }
 
-void entranced_auth_display_generate() {
-   /* Code */
+static int 
+_entranced_auth_entry_add(Entranced_Display *d, FILE *auth_file, const char *addr, 
int addrlen) 
+{
+   Xauth       *auth = NULL;
+   char        dispnum[8];
+
+   if (!d)
+      return FALSE;
+
+   /* Generate a new Xauth and set the address */
+   if (!(auth = _entranced_auth_generate()))
+      return FALSE;
+   auth->address = calloc(1, addrlen);
+   if(!auth->address)
+   {
+      free(auth);
+      return FALSE;
+   }
+   memcpy(auth->address, addr, addrlen);
+   auth->address_length = addrlen;
+
+   /* Set the display number */
+   snprintf(dispnum, 8, "%d", d->dispnum);
+   auth->number = strdup(dispnum);
+   auth->number_length = strlen(dispnum);
+
+   if (!XauWriteAuth(auth_file, auth))
+   {
+      entranced_debug("_entrance_auth_entry_add: Auth write failed!\n");
+      return FALSE;
+   }
+
+   if (!d->auths)
+      d->auths = ecore_list_new();
+
+   if (!ecore_list_append(d->auths, auth))
+   {
+      entranced_debug("_entrance_auth_entry_add: Could not add auth entry to 
list!\n");
+      return FALSE;
+   }
+
+   return TRUE;
+
 }
 
-void entranced_auth_user_add() {
-   /* Code */
+int
+entranced_auth_display_secure (Entranced_Display *d)
+{
+   FILE              *auth_file;
+   char              buf[PATH_MAX];
+   char              hostname[1024];
+
+   if (!d)
+      return FALSE;
+
+   umask(022);
+
+   entranced_debug("entranced_auth_display_secure: Setting up access for display 
%s\n", d->name);
+
+   if (d->authfile)
+      free(d->authfile);
+   d->authfile = NULL;
+
+   /* FIXME: Config-ize */
+   snprintf(buf, PATH_MAX, "/var/lib/entrance/%s%s", d->name, ".Xauth");
+   unlink(d->authfile);
+
+   if(!(auth_file = fopen(d->authfile, "w")))
+   {
+      free(d->authfile);
+      d->authfile = NULL;
+      return FALSE;
+   }
+   
+   /* XXX: This code assumes X server is running on localhost and
+    *      may need to be modified for Xdmcp
+    */
+   ecore_list_goto_first(d->auths);
+   while(d->auths->nodes)
+   {
+      Xauth *tmp;
+      tmp = (Xauth *) ecore_list_remove(d->auths);
+      XauDisposeAuth(tmp);
+   }
+   
+   if (!gethostname(hostname, 1023))
+   {
+      hostname[1023] = '\0';
+      if (d->hostname)
+         free(d->hostname);
+      d->hostname = strdup(hostname);
+   }
+
+   /* Add auth entry for localhost */
+   if (!_entranced_auth_entry_add(d, auth_file, d->hostname, 
+                                 strlen(d->hostname)))
+      return FALSE;
+   
+   fclose(auth_file);
+   setenv("XAUTHORITY", d->authfile, TRUE);
+
+   entranced_debug("entranced_auth_display_secure: Successfully set up access for %s 
(localhost)\n", d->name);
+
+   return TRUE;
 }
 
-static int _entranced_auth_purge() {
-   /* Code */
-   return 0;
+int
+entranced_auth_user_add(Entranced_Display *d, uid_t user, const char *homedir)
+{
+   FILE              *auth_file;
+   int               ret = TRUE;
+   char              buf[PATH_MAX];
+   Ecore_List_Node   *li;
+
+   if (!d || !homedir)
+      return FALSE;
+
+   entranced_debug("entranced_auth_user_add: Adding auth cookie for UID %d\n", user);
+
+   while(1)
+   {
+      umask (077);
+
+      if (!homedir)
+         d->user_authfile = NULL;
+      else
+      {
+         snprintf(buf, PATH_MAX, "%s/.Xauthority", homedir);
+         d->user_authfile = strdup(buf);
+      }
+
+      /* Make sure the file can be written to */
+      if((auth_file = fopen(d->user_authfile, "a+")))
+         fclose(auth_file);
+      else
+      {
+         entranced_debug("entranced_auth_user_add: Unable to write auth file %s", 
d->user_authfile);
+         free(d->user_authfile);
+         d->user_authfile = NULL;
+         return FALSE;
+      }
+      /* TODO: May need a permissions/paranoia check */
+
+      /* Lock the authorization file */
+      /* FIXME: What if for some reason we never succeed in getting
+       *        a lock ?
+       */
+      if (XauLockAuth(d->user_authfile, 3, 3, 0) != LOCK_SUCCESS)
+      {
+         syslog(LOG_CRIT, "entranced_auth_user_add: Unable to lock auth file %s", 
d->user_authfile);
+         free(d->user_authfile);
+         d->user_authfile = NULL;
+
+         umask (022);
+      }
+      else
+         break;
+   }
+
+   /* Open file and write auth entries */
+   if(!(auth_file = fopen(d->user_authfile, "a+")))
+   {
+      syslog(LOG_CRIT, "entranced_auth_user_add: Open auth file %s failed after 
lock", d->user_authfile);
+      XauUnlockAuth (d->user_authfile);
+      free(d->user_authfile);
+      d->user_authfile = NULL;
+
+      umask (022);
+
+      return FALSE;
+   }
+
+   entranced_debug("entranced_auth_user_add: Opened %s for writing cookies\n", 
d->user_authfile);
+
+   /* Remove any existing old entries for this display */
+   _entranced_auth_purge(d, auth_file);
+   
+   for (li = d->auths->first; li; li = li->next)
+   {
+      if (!XauWriteAuth (auth_file, (Xauth *) li->data))
+      {
+         syslog(LOG_CRIT, "entranced_user_auth_add: Unable to write cookie");
+         ret = FALSE;
+         break;
+      }
+   }
+
+   fclose(auth_file);
+   XauUnlockAuth(d->user_authfile);
+   entranced_debug("entranced_auth_user_add: Finished writing auth entries for uid 
%s", user);
+
+   return ret;
+      
 }
 
+
===================================================================
RCS file: /cvsroot/enlightenment/e17/apps/entrance/src/daemon/spawner.c,v
retrieving revision 1.13
retrieving revision 1.14
diff -u -3 -r1.13 -r1.14
--- spawner.c   16 Feb 2004 01:04:06 -0000      1.13
+++ spawner.c   22 Mar 2004 06:51:39 -0000      1.14
@@ -1,10 +1,11 @@
 #include <Ecore.h>
 #include <Ecore_X.h>
 #include <Ecore_Ipc.h>
-#include "spawner.h"
+#include "Entranced.h"
+#include "util.h"
 
 /* Globals */
-/* Entranced_Spawner_Display *d; */
+/* Entranced_Display *d; */
 static Ecore_Event_Handler *_e_handler = NULL;
 static Ecore_Event_Handler *_d_handler = NULL;
 static Ecore_Event_Filter *_e_filter = NULL;
@@ -65,15 +66,15 @@
 
 /**
  * Create a new display context.
- * @return A pointer to an Entranced_Spawner_Display handle for the new context
+ * @return A pointer to an Entranced_Display handle for the new context
  */
-Entranced_Spawner_Display *
-Entranced_Spawner_Display_New(void)
+Entranced_Display *
+Entranced_Display_New(void)
 {
-   Entranced_Spawner_Display *d;
+   Entranced_Display *d;
 
-   d = malloc(sizeof(Entranced_Spawner_Display));
-   memset(d, 0, sizeof(Entranced_Spawner_Display));
+   d = malloc(sizeof(Entranced_Display));
+   memset(d, 0, sizeof(Entranced_Display));
    /* TODO: Config-ize these parameters */
    d->xprog = strdup(X_SERVER);
    d->attempts = 5;
@@ -90,7 +91,7 @@
  * @param d The spawner display context that will handle this server
  */
 void
-Entranced_Spawn_X(Entranced_Spawner_Display * d)
+Entranced_Spawn_X(Entranced_Display * d)
 {
    int i = 0;
 
@@ -112,7 +113,7 @@
  * @return The status of the display context after the launch attempt
  */
 pid_t
-Entranced_Start_Server_Once(Entranced_Spawner_Display * d)
+Entranced_Start_Server_Once(Entranced_Display * d)
 {
    double start_time;
    char x_cmd[PATH_MAX];
@@ -175,7 +176,7 @@
  * @param d The spawner display context that this session will use
  */
 void
-Entranced_Spawn_Entrance(Entranced_Spawner_Display * d)
+Entranced_Spawn_Entrance(Entranced_Display * d)
 {
    char entrance_cmd[PATH_MAX];
 
@@ -191,14 +192,14 @@
 int
 Entranced_Respawn_Reset(void *data)
 {
-   _DEBUG("Respawn timer reset.\n");
+   entranced_debug("Respawn timer reset.\n");
    is_respawning = 0;
    respawn_timer = NULL;
    return 0;
 }
 
 int
-Entranced_X_Restart(Entranced_Spawner_Display * d)
+Entranced_X_Restart(Entranced_Display * d)
 {
 
    /* Attempt to restart X server */
@@ -253,7 +254,7 @@
 {
 /*    Ecore_Event_Signal_User *e = (Ecore_Event_Signal_User *) event; */
 
-   _DEBUG("SIGUSR event triggered.\n");
+   entranced_debug("SIGUSR event triggered.\n");
 
    /* X sends SIGUSR1 to let us know it is ready */
 /*    if (e->number == 1)*/
@@ -265,18 +266,18 @@
 Entranced_Exe_Exited(void *data, int type, void *event)
 {
    Ecore_Event_Exe_Exit *e = (Ecore_Event_Exe_Exit *) event;
-   Entranced_Spawner_Display *d = (Entranced_Spawner_Display *) data;
+   Entranced_Display *d = (Entranced_Display *) data;
 
-   _DEBUG("Ecore_Event_Exe_Exit triggered.\n");
+   entranced_debug("Ecore_Event_Exe_Exit triggered.\n");
 
    if (is_respawning)
    {
-      _DEBUG("Event ignored.\n");
+      entranced_debug("Event ignored.\n");
       return 1;
    }
    else
    {
-      _DEBUG("Processing Event.\n");
+      entranced_debug("Processing Event.\n");
    }
 
    is_respawning = 1;
@@ -337,7 +338,7 @@
 int
 Entranced_Signal_Exit(void *data, int type, void *event)
 {
-   _DEBUG("Ecore_Signal_Exit_Triggered\n");
+   entranced_debug("Ecore_Signal_Exit_Triggered\n");
    syslog(LOG_INFO, "Caught exit signal.");
    syslog(LOG_INFO, "Display and display manager are shutting down.");
    ecore_main_loop_quit();
@@ -347,7 +348,7 @@
 void
 Entranced_AtExit(void)
 {
-   _DEBUG("Entranced exits.\n");
+   entranced_debug("Entranced exits.\n");
 }
 
 /*
@@ -358,12 +359,13 @@
 {
    int c;
    int nodaemon = 0;            /* TODO: Config-ize this variable */
-   Entranced_Spawner_Display *d;
+   Entranced_Display *d;
    struct option d_opt[] = {
-      {"config", 1, 0, 1},
-      {"display", 1, 0, 1},
-      {"nodaemon", 0, 0, 1},
-      {"help", 0, 0, 2},
+      {"config", 1, 0, 'c'},
+      {"display", 1, 0, 'd'},
+      {"nodaemon", 0, 0, 'n'},
+      {"help", 0, 0, 'h'},
+      {"verbose", 0, 0, 'v'},
       {0, 0, 0, 0}
    };
    pid_t entranced_pid = getpid();
@@ -376,12 +378,12 @@
    openlog("entranced", LOG_NOWAIT, LOG_DAEMON);
 
    /* Set up a spawner context */
-   d = Entranced_Spawner_Display_New();
+   d = Entranced_Display_New();
 
    /* Parse command-line options */
    while (1)
    {
-      c = getopt_long_only(argc, argv, "c:d:", d_opt, NULL);
+      c = getopt_long_only(argc, argv, "c:d:nhv", d_opt, NULL);
       if (c == -1)
          break;
       switch (c)
@@ -393,10 +395,10 @@
            d->name = strdup(optarg);
            setenv("DISPLAY", optarg, 1);
            break;
-        case 1:
+        case 'n':
            nodaemon = 1;
            break;
-        case 2:
+        case 'h':
            /* This should probably in a separate usage function, but bleh */
            printf("Entranced - Launcher for the Entrance Display Manager\n");
            printf("Usage: %s [OPTION] ...\n\n", argv[0]);
@@ -405,6 +407,7 @@
            printf("  -c CONFIG          Specify config file for greeter\n");
            printf("  -d DISPLAY         Connect to an existing X server\n");
            printf("  -help              Display this help message\n");
+           /*printf("  -verbose           Display extra debugging info\n");*/
            printf
               ("  -nodaemon          Don't fork to background (useful for init 
scripts)\n");
            printf
@@ -416,6 +419,9 @@
            printf("entrance directly by typing \"entrance\".\n\n");
            exit(0);
 
+        /*case 'v':
+           config.debuglevel = 1;*/
+
       }
    }
 
@@ -487,12 +493,12 @@
    Entranced_Spawn_Entrance(d);
 
    /* Main program loop */
-   _DEBUG("Entering main loop.\n");
+   entranced_debug("Entering main loop.\n");
    ecore_main_loop_begin();
    ecore_main_loop_quit();
 
    /* Shut down */
-   _DEBUG("Exited main loop! Shutting down...\n");
+   entranced_debug("Exited main loop! Shutting down...\n");
    ecore_exe_terminate(d->e_exe);
    kill(d->pid.x, SIGTERM);
    sleep(5);




-------------------------------------------------------
This SF.Net email is sponsored by: IBM Linux Tutorials
Free Linux tutorial presented by Daniel Robbins, President and CEO of
GenToo technologies. Learn everything from fundamentals to system
administration.http://ads.osdn.com/?ad_id=1470&alloc_id=3638&op=click
_______________________________________________
enlightenment-cvs mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/enlightenment-cvs

Reply via email to