Explorar o código

* More work on storing conf settings in binary

svn: 1301
Bryan Drewery %!s(int64=22) %!d(string=hai) anos
pai
achega
e1e4cbe0d8
Modificáronse 6 ficheiros con 181 adicións e 26 borrados
  1. 47 6
      src/binary.c
  2. 4 0
      src/binary.h
  3. 79 4
      src/conf.c
  4. 4 2
      src/conf.h
  5. 45 14
      src/main.c
  6. 2 0
      src/settings.h

+ 47 - 6
src/binary.c

@@ -38,7 +38,7 @@ settings_t settings = {
 
 #define PACK_ENC 1
 #define PACK_DEC 2
-static void edpack(struct settings_struct *, const char *, int);
+static void edpack(settings_t *, const char *, int);
 
 int checked_bin_buf = 0;
 
@@ -119,8 +119,8 @@ bin_md5(const char *fname, int todo, MD5_CTX * ctx)
       if (!memcmp(buf, &settings.prefix, PREFIXLEN)) {
         strncpyz(settings.hash, hash, 65);
         edpack(&settings, hash, PACK_ENC);
-        fwrite(&settings.hash, sizeof(struct settings_struct) - PREFIXLEN, 1, fn);
-        i = sizeof(struct settings_struct) - PREFIXLEN;
+        fwrite(&settings.hash, sizeof(settings_t) - PREFIXLEN, 1, fn);
+        i = sizeof(settings_t) - PREFIXLEN;
       }
     }
 
@@ -230,7 +230,7 @@ readcfg(const char *cfgfile)
   return 1;
 }
 
-static void edpack(struct settings_struct *incfg, const char *hash, int what)
+static void edpack(settings_t *incfg, const char *hash, int what)
 {
   char *tmp = NULL;
   char *(*enc_dec_string)();
@@ -273,8 +273,8 @@ static void edpack(struct settings_struct *incfg, const char *hash, int what)
 }
 
 
-static void
-tellconfig(struct settings_struct *incfg)
+void
+tellconfig(settings_t *incfg)
 {
 #define dofield(_field)		printf("%s: %s\n", #_field, _field);
   /* -- STATIC -- */
@@ -336,3 +336,44 @@ tellconfig(&settings);
     }
   }
 }
+
+void write_settings(const char *fname)
+{
+  MD5_CTX ctx;
+
+  MD5_Init(&ctx);
+  if (bin_md5(fname, WRITE_MD5, &ctx))
+    printf("* Wrote settings to %s.\n", fname);
+  exit(0);
+}
+
+void conf_to_bin(conf_t *in)
+{
+  conf_bot *bot = NULL;
+  printf("converting conf to bin\n");
+  sprintf(settings.uid, "%d", in->uid);
+  sprintf(settings.watcher, "%d", in->watcher);
+  sprintf(settings.autocron, "%d", in->autocron);
+  sprintf(settings.autouname, "%d", in->autouname);
+  sprintf(settings.portmin, "%d", in->portmin);
+  sprintf(settings.portmax, "%d", in->portmax);
+  sprintf(settings.pscloak, "%d", in->pscloak);
+
+  strncpyz(settings.binname, in->binname, 16);
+  strncpyz(settings.username, in->username, 16);
+
+  strncpyz(settings.uname, in->uname, 350);
+  strncpyz(settings.homedir, in->homedir, 350);
+  strncpyz(settings.binpath, in->binpath, 350);
+  for (bot = in->bots; bot && bot->nick; bot = bot->next) {
+    sprintf(settings.bots, "%s%s %s %s%s %s,", settings.bots && settings.bots[0] ? settings.bots : "",
+                           bot->nick,
+                           bot->ip ? bot->ip : ".", 
+                           bot->host6 ? "+" : "", 
+                           bot->host ? bot->host : (bot->host6 ? bot->host6 : "."),
+                           bot->ip6 ? bot->ip6 : "");
+    }
+
+  tellconfig(&settings);
+  write_settings(binname);
+}

+ 4 - 0
src/binary.h

@@ -1,10 +1,14 @@
 #ifndef _BINARY_H
 #  define _BINARY_H
 
+#include "conf.h"
+
 extern int checked_bin_buf;
 
 #  define WRITE_MD5 	1
 #  define GET_MD5		2
 
 void check_sum(const char *, const char *);
+void write_settings(const char *);
+void conf_to_bin(conf_t *);
 #endif /* !_BINARY_H */

+ 79 - 4
src/conf.c

@@ -7,6 +7,7 @@
 #include "common.h"
 #include "conf.h"
 #include "shell.h"
+#include "binary.h"
 #include "debug.h"
 #include "chanprog.h"
 #include "crypt.h"
@@ -97,7 +98,7 @@ swap_uids_back()
 }
 
 void
-confedit(char *fname)
+confedit()
 {
   FILE *f = NULL;
   char s[DIRMAX] = "", *editor = NULL;
@@ -201,10 +202,10 @@ confedit(char *fname)
   if (!can_stat(s))
     fatal("Error reading new config file", 0);
 
-  unlink(fname);
-  Encrypt_File(s, fname);
+  readconf(s, 0);	/* read cleartext conf tmp into &settings */
   unlink(s);
-  fatal("New config file saved, restart bot to use", 0);
+  conf_to_bin(&conffile);	/* will exit */
+  exit(0);	/* never reached */
 
 fatal:
   unlink(s);
@@ -768,3 +769,77 @@ fillconf(conf_t * inconf)
   inconf->pscloak = conffile.pscloak;
   inconf->uid = conffile.uid;
 }
+
+void tellconf(conf_t *inconf)
+{
+conf_bot *bot;
+int i = 0;
+
+printf("uid: %d\n", inconf->uid);
+printf("homedir: %s\n", inconf->homedir);
+printf("binpath: %s\n", inconf->binpath);
+printf("binname: %s\n", inconf->binname);
+printf("portmin: %d\n", inconf->portmin);
+printf("portmax: %d\n", inconf->portmax);
+printf("pscloak: %d\n", inconf->pscloak);
+printf("autocron: %d\n", inconf->autocron);
+printf("autouname: %d\n", inconf->autouname);
+printf("watcher: %d\n", inconf->watcher);
+    for (bot = inconf->bots; bot && bot->nick; bot = bot->next) {
+      i++;
+      printf("%d: %s IP: %s HOST: %s IP6: %s HOST6: %s PID: %d\n", i,
+                      bot->nick,
+                      bot->ip ? bot->ip : "",
+                      bot->host ? bot->host : "",
+                      bot->ip6 ? bot->ip6 : "",
+                      bot->host6 ? bot->host6 : "",
+                      bot->pid);
+    }
+printf("\n\n\n\n");
+}
+
+void bin_to_conf(settings_t *in)
+{
+printf("Converting binary data to conf struct\n");
+  conffile.uid = atol(settings.uid);
+  conffile.username = strdup(settings.username);
+  conffile.uname = strdup(settings.uname);
+  conffile.homedir = strdup(settings.homedir);
+  conffile.binpath = strdup(settings.binpath);
+  conffile.binname = strdup(settings.binname);
+  conffile.portmin = atol(settings.portmin);
+  conffile.portmax = atol(settings.portmax);
+  conffile.autouname = atoi(settings.autouname);
+  conffile.autocron = atoi(settings.autocron);
+  conffile.watcher = atoi(settings.watcher);
+  conffile.pscloak = atoi(settings.pscloak);
+
+  /* BOTS */
+  {
+    char *p = NULL, *tmp = NULL, *tmpp = NULL;
+    tmp = tmpp = strdup(settings.bots);
+    while ((p = strchr(tmp, ','))) {
+      char *nick = NULL, *host = NULL, *ip = NULL, *ipsix = NULL;
+
+      *p++ = 0;
+      if (!tmp[0])
+        break;
+      nick = newsplit(&tmp);
+      if (!nick || (nick && !nick[0]))
+        werr(ERR_BADCONF);
+
+
+      if (tmp[0])
+        ip = newsplit(&tmp);
+      if (tmp[0])
+        host = newsplit(&tmp);
+      if (tmp[0])
+        ipsix = newsplit(&tmp);
+
+      conf_addbot(nick, ip, host, ipsix);
+      tmp = p++;
+    }
+    free(tmpp);
+  }
+  tellconf(&conffile);
+}

+ 4 - 2
src/conf.h

@@ -5,7 +5,7 @@
 #include <stdio.h>
 #include "types.h"
 #include "eggdrop.h"
-
+#include "settings.h"
 
 typedef struct conf_bot_b {
   struct conf_bot_b *next;
@@ -53,7 +53,7 @@ enum {
 void spawnbots();
 int killbot(char *);
 #endif /* LEAF */
-void confedit(char *) __attribute__((noreturn));
+void confedit() __attribute__((noreturn));
 #ifdef LEAF
 void conf_addbot(char *, char *, char *, char *);
 int conf_delbot(char *);
@@ -65,6 +65,8 @@ int readconf(char *, int);
 int parseconf();
 int writeconf(char *, FILE *, int);
 void fillconf(conf_t *);
+void bin_to_conf();
+void tellconf(conf_t *);
 
 extern char		cfile[DIRMAX];
 #endif /* !_CONF_H */

+ 45 - 14
src/main.c

@@ -502,8 +502,15 @@ static void core_halfhourly()
 #endif /* HUB */
 }
 
+/* FIXME: Remove after 1.2 */
 static void startup_checks() {
   int enc = CONF_ENC;
+  int old_hack = 0;
+
+  /* for compatability with old conf files 
+   * only check/use conf file if it exists and settings.uname is empty.
+   * if settings.uname is NOT empty, just erase the conf file if it exists
+   * otherwise, assume we're working only with the struct */
 
 #ifdef LEAF
   egg_snprintf(cfile, sizeof cfile, STR("%s/.known_hosts"), confdir());
@@ -518,6 +525,7 @@ static void startup_checks() {
 
   if (!can_stat(confdir())) {
 #ifdef LEAF
+/* FIXME: > 1.2 still making confdir() because tmp is inside */
     if (mkdir(confdir(),  S_IRUSR | S_IWUSR | S_IXUSR)) {
       unlink(confdir());
       if (!can_stat(confdir()))
@@ -528,13 +536,16 @@ static void startup_checks() {
     }
 #endif /* LEAF */
   }
+
   if (fixmod(confdir()))
     werr(ERR_CONFDIRMOD);
-  /*technically no longer needed? 
-   else if (!can_stat(cfile))
-     werr(ERR_NOCONF);
-  */
-  else if (can_stat(cfile) && fixmod(cfile))
+ 
+  if (can_stat(cfile) && !settings.uname[0])
+    old_hack = 1;
+  if (can_stat(cfile) && settings.uname[0])
+    unlink(cfile);		/* kill the old one! */
+
+  if (old_hack && can_stat(cfile) && fixmod(cfile))
     werr(ERR_CONFMOD);
 
   if (!can_stat(tempdir)) {
@@ -570,19 +581,36 @@ static void startup_checks() {
     fclose(f);
   }
 
-  if (can_stat(cfile))
-    readconf(cfile, enc);
-      
+  if (old_hack && can_stat(cfile))
+    readconf(cfile, enc);	/* will read into &conffile struct */
+  else if (settings.uname[0])
+    bin_to_conf();
+
+
 #ifndef CYGWIN_HACKS
   if (do_confedit)
-    confedit(cfile);		/* this will exit() */
+    confedit();		/* this will exit() */
 #endif /* !CYGWIN_HACKS */
   parseconf();
 
+  if (old_hack)
+    conf_to_bin(&conffile);
+/* -- */
+//exit(1);
+
+/* NO?
+  if (old_hack) {
 #ifdef LEAF
-  if (localhub)
-#endif /* LEAF */
-    writeconf(cfile, NULL, enc);
+    if (localhub)
+#endif
+      writeconf(cfile, NULL, enc);
+  } else if (!old_hack) {
+#ifdef LEAF
+    if (localhub)
+#endif
+    write_settings(binname);
+  }
+*/
 
   if (!can_stat(binname))
    werr(ERR_BINSTAT);
@@ -710,6 +738,7 @@ printf("out: %s\n", out);
   binname = getfullbinname(argv[0]);
 
   check_sum(binname, argc >= 3 && !strcmp(argv[1], "-p") ? argv[2] : NULL);
+  /* Now settings struct is filled */
 
   if (!checked_bin_buf)
     exit(1);
@@ -775,9 +804,11 @@ printf("out: %s\n", out);
     check_trace(1);
 #endif /* !CYGWIN_HACKS */
 
-  startup_checks();
+//  strcpy(settings.username, "bryan");
+//  write_settings(binname);
 
-  /* if we are here, then all the necesary files/dirs are accesable, lets load the config now. */
+  /* Check and load conf file */
+  startup_checks();
 
   if ((localhub && !updating) || !localhub) {
     if ((conf.bot->pid > 0) && conf.bot->pid_file) {

+ 2 - 0
src/settings.h

@@ -36,4 +36,6 @@ typedef struct settings_struct {
 
 extern settings_t       settings;
 
+void tellconfig(settings_t *);
+
 #endif /* !_SETTINGS_H */