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 ) {
