Index: ChangeLog
===================================================================
--- ChangeLog	(revision 4779)
+++ ChangeLog	(revision 4788)
@@ -12,4 +12,5 @@
 	  (enumdev only at the moment) (pr1)
 	* Added support for a simple trap mechanism (pr1)
+	* Added simple authfile support to roard (pr1)
 
 v. 0.4beta3 - Wed Jan 26 2011 23:26 CET
Index: libroar/auth.c
===================================================================
--- libroar/auth.c	(revision 4745)
+++ libroar/auth.c	(revision 4788)
@@ -137,5 +137,8 @@
 
  mes.cmd     = ROAR_CMD_AUTH;
- mes.datalen = 4;
+ mes.datalen = 4 + authmes->len;
+
+ if ( mes.datalen > sizeof(mes.data) )
+  return -1;
 
  header[0] = authmes->type;
@@ -143,4 +146,8 @@
  header[2] = authmes->reserved.c[0];
  header[3] = authmes->reserved.c[1];
+
+ if ( authmes->len ) {
+  memcpy(mes.data + 4, authmes->data, authmes->len);
+ }
 
  if ( (ret = roar_req(con, &mes, NULL)) == -1 ) {
@@ -222,4 +229,35 @@
 
  return -1;
+}
+
+static int try_cookie (struct roar_connection * con, int * next) {
+ struct roar_libroar_config * config = roar_libroar_get_config();
+ struct roar_auth_message authmes;
+ struct roar_authfile * authfile;
+ struct roar_authfile_key * key;
+ int idx;
+ int done = 0;
+
+ roar_auth_mes_init(&authmes, ROAR_AUTH_T_COOKIE);
+
+ if ( (authfile = roar_authfile_open(ROAR_AUTHFILE_TYPE_AUTO, config->authfile, 0, ROAR_AUTHFILE_VERSION_AUTO)) == NULL )
+  return -1;
+
+ for (idx = 0; !done; idx++) {
+  if ( (key = roar_authfile_lookup_key(authfile, ROAR_AUTH_T_COOKIE, idx, NULL)) == NULL )
+   break;
+
+  authmes.data = key->data;
+  authmes.len  = key->len;
+
+  if ( roar_auth_ask_server(con, &authmes) != -1 )
+   done = 1;
+
+  roar_authfile_key_unref(key);
+ }
+
+ roar_authfile_close(authfile);
+
+ return done ? 0 : -1;
 }
 
@@ -236,4 +274,5 @@
               ROAR_AUTH_T_RHOST,
 //              ROAR_AUTH_T_PASSWORD,
+              ROAR_AUTH_T_COOKIE,
               ROAR_AUTH_T_NONE,
               _EOL
@@ -249,12 +288,12 @@
 
    switch (cur) {
-     case ROAR_AUTH_T_PASSWORD:
+    case ROAR_AUTH_T_PASSWORD:
       if ( (ret = try_password(con, &next)) == -1 )
        done = 0;
       break;
-     case ROAR_AUTH_T_TRUST:
-     case ROAR_AUTH_T_IDENT:
-     case ROAR_AUTH_T_RHOST:
-     case ROAR_AUTH_T_NONE:
+    case ROAR_AUTH_T_TRUST:
+    case ROAR_AUTH_T_IDENT:
+    case ROAR_AUTH_T_RHOST:
+    case ROAR_AUTH_T_NONE:
       roar_auth_mes_init(&authmes, ltt[i]);
       if ( (ret = roar_auth_ask_server(con, &authmes)) == -1 )
@@ -262,4 +301,9 @@
 
       next = authmes.type;
+     break;
+    case ROAR_AUTH_T_COOKIE:
+      if ( (ret = try_cookie(con, &next)) == -1 )
+       done = 0;
+      break;
      break;
     default: /* Bad error! */
Index: roard/roard.c
===================================================================
--- roard/roard.c	(revision 4761)
+++ roard/roard.c	(revision 4788)
@@ -32,4 +32,10 @@
  RESTART_RETRY,
  SHUTDOWN
+};
+
+enum af_mode {
+ AF_MODE_NONE,
+ AF_MODE_LOAD,
+ AF_MODE_GEN
 };
 
@@ -189,4 +195,9 @@
         " --trust-root          - Trust root user\n"
         " --no-trust-root       - Don't trust root user\n"
+        " --authfile-gen FILE   - Generate an new authfile\n"
+//        " --authfile-load FILE  - Load an authfile\n"
+        " --authfile-type TYPE  - Type of authfile\n"
+        " --authfile-acclev ACCLEV\n"
+        "                       - Sets the access level for authfile\n"
        );
 
@@ -1358,4 +1369,76 @@
 #endif
 
+int add_authfile (const char * file, const char * type, enum af_mode mode, enum roard_client_acclev acclev) {
+ struct roar_authfile * authfile = NULL;
+ struct roar_authfile_key *  key = NULL;
+ int af_type = ROAR_AUTHFILE_TYPE_AUTO;
+
+ if ( type == NULL ) {
+  // noop.
+ } else if ( !strcasecmp(type, "roar") ) {
+  af_type = ROAR_AUTHFILE_TYPE_ROAR;
+ } else if ( !strcasecmp(type, "roar") ) {
+  af_type = ROAR_AUTHFILE_TYPE_ROAR;
+ } else if ( !strcasecmp(type, "esd") ) {
+  af_type = ROAR_AUTHFILE_TYPE_ESD;
+ } else if ( !strcasecmp(type, "pulse") ) {
+  af_type = ROAR_AUTHFILE_TYPE_PULSE;
+ } else if ( !strcasecmp(type, "htpasswd") ) {
+  af_type = ROAR_AUTHFILE_TYPE_HTPASSWD;
+ } else if ( !strcasecmp(type, "xauth") ) {
+  af_type = ROAR_AUTHFILE_TYPE_XAUTH;
+ } else {
+  ROAR_ERR("add_authfile(*): unknown authfile type '%s'.", type);
+  return -1;
+ }
+
+ if ( mode == AF_MODE_GEN && af_type == ROAR_AUTHFILE_TYPE_AUTO )
+  af_type = ROAR_AUTHFILE_TYPE_ESD;
+
+ switch (mode) {
+  case AF_MODE_NONE:
+    return 0;
+   break;
+  case AF_MODE_GEN:
+    switch (af_type) {
+     case ROAR_AUTHFILE_TYPE_ESD:
+       key = roar_authfile_key_new_random(ROAR_AUTH_T_COOKIE, 16, NULL);
+      break;
+     case ROAR_AUTHFILE_TYPE_PULSE:
+       key = roar_authfile_key_new_random(ROAR_AUTH_T_COOKIE, 256, NULL);
+      break;
+     default:
+       return -1;
+      break;
+    }
+
+    if ( key == NULL ) {
+     ROAR_ERR("add_authfile(*): Can not generate key.");
+     return -1;
+    }
+
+    if ( (authfile = roar_authfile_open(af_type, file, 1, ROAR_AUTHFILE_VERSION_AUTO)) == NULL ) {
+     roar_authfile_key_unref(key);
+     return -1;
+    }
+
+    if ( roar_authfile_add_key(authfile, key) == -1 ) {
+     ROAR_WARN("add_authfile(*): Can not add key to authfile.");
+    }
+
+    if ( auth_addkey_cookie(acclev, key->data, key->len) == -1 ) {
+     ROAR_WARN("add_authfile(*): Can not add key to internal key storage.");
+    }
+
+    roar_authfile_key_unref(key);
+    if ( roar_authfile_close(authfile) != 0 )
+     return -1;
+    return 0;
+   break;
+ }
+
+ return -1;
+}
+
 // X11:
 #ifdef ROAR_HAVE_LIBX11
@@ -1610,4 +1693,8 @@
  enum roard_client_acclev trust_acclev = ACCLEV_ALL;
  int                      trust_root   = 1;
+ char * af_file                        = NULL;
+ char * af_type                        = NULL;
+ enum af_mode af_mode                  = AF_MODE_NONE;
+ enum roard_client_acclev af_acclev    = ACCLEV_ALL;
 #ifndef ROAR_WITHOUT_DCOMP_LIGHT
  int    light_channels = LIGHT_CHANNELS_DEFAULT;
@@ -1890,4 +1977,19 @@
   } else if ( strcmp(k, "--no-trust-root") == 0 ) {
    trust_root = 0;
+
+  } else if ( strcmp(k, "--authfile-gen") == 0 ) {
+   _CKHAVEARGS(1);
+   af_file = argv[++i];
+   af_mode = AF_MODE_GEN;
+  } else if ( strcmp(k, "--authfile-load") == 0 ) {
+   _CKHAVEARGS(1);
+   af_file = argv[++i];
+   af_mode = AF_MODE_LOAD;
+  } else if ( strcmp(k, "--authfile-type") == 0 ) {
+   _CKHAVEARGS(1);
+   af_type = argv[++i];
+  } else if ( strcmp(k, "--authfile-acclev") == 0 ) {
+   _CKHAVEARGS(1);
+   af_acclev = clients_str2acclev(argv[++i]);
 
   } else if ( strcmp(k, "--list-cf") == 0 ) {
@@ -2448,4 +2550,10 @@
 #endif
 
+ if ( af_mode != AF_MODE_NONE ) {
+  if ( add_authfile(af_file, af_type, af_mode, af_acclev) == -1 ) {
+   ROAR_ERR("main(*): adding authfile '%s' failed!", af_file);
+  }
+ }
+
 #ifndef ROAR_WITHOUT_DCOMP_MIXER
  if ( m_drv != NULL ) {
