Przeglądaj źródła

* Cleanup use of bd::Stream for bd::String passing, and move all userfile writing to 1 function.

Bryan Drewery 17 lat temu
rodzic
commit
ab7322e41f
7 zmienionych plików z 47 dodań i 52 usunięć
  1. 1 1
      lib/bdlib
  2. 10 12
      src/EncryptedStream.c
  3. 2 1
      src/EncryptedStream.h
  4. 9 21
      src/mod/share.mod/share.c
  5. 18 10
      src/userrec.c
  6. 1 0
      src/userrec.h
  7. 6 7
      src/users.c

+ 1 - 1
lib/bdlib

@@ -1 +1 @@
-Subproject commit a47efebd0ef4656716172666877a9cfd61d45684
+Subproject commit b58d380d4da26014f0fea94c523c9667ccd95a26

+ 10 - 12
src/EncryptedStream.c

@@ -7,18 +7,16 @@
 #include <stdarg.h>
 #include "compat/compat.h"
 
-int EncryptedStream::gets (char *_data, size_t maxSize) {
-  size_t size = bd::Stream::gets(_data, maxSize);
-  if (key.length()) {
-    bd::String tmp(_data, size);
-    if (tmp[tmp.length() - 1] == '\n')
-      --tmp;
-    bd::String decrypted(decrypt_string(key, broken_base64Decode(tmp)));
-    decrypted += '\n';
-    strlcpy(_data, decrypted.c_str(), maxSize);
-    return decrypted.length();
-  }
-  return size;
+bd::String EncryptedStream::gets (size_t maxSize, char delim) {
+  if (!key.length()) return bd::Stream::gets(maxSize, delim);
+  bd::String tmp(bd::Stream::gets(maxSize, delim));
+  if (delim && tmp[tmp.length() - 1] == delim)
+    --tmp;
+  bd::String decrypted(decrypt_string(key, broken_base64Decode(tmp)));
+  /* The delimeter (\n) is not encrypted */
+  if (delim)
+    decrypted += delim;
+  return decrypted;
 }
 
 void EncryptedStream::puts (const bd::String& str_in)

+ 2 - 1
src/EncryptedStream.h

@@ -6,6 +6,7 @@ namespace bd {
 }
 #include <iostream>
 #include <bdlib/src/Stream.h>
+#include <bdlib/src/String.h>
 
 class EncryptedStream : public bd::Stream {
   private:
@@ -18,7 +19,7 @@ class EncryptedStream : public bd::Stream {
         EncryptedStream(bd::String& keyStr) : Stream(), key(keyStr) {};
         EncryptedStream(EncryptedStream& stream) : Stream(stream), key(stream.key) {};
 
-        virtual int gets(char *, size_t);
+        virtual bd::String gets(size_t, char delim = 0);
         virtual void puts (const bd::String& string);
 };
 #endif

+ 9 - 21
src/mod/share.mod/share.c

@@ -38,6 +38,7 @@
 #include "src/auth.h"
 #include "src/set.h"
 #include "src/EncryptedStream.h"
+#include <bdlib/src/String.h>
 
 #include <netinet/in.h>
 #include <arpa/inet.h>
@@ -1075,16 +1076,14 @@ share_end(int idx, char *par)
 
 static void share_userfile_line(int idx, char *par) {
   char *size = newsplit(&par);
-  size_t len = atoi(size);
 
-  stream_in.puts(par, len);
-  stream_in.puts('\n');
+  stream_in << bd::String(par, atoi(size));
+  stream_in << '\n';
 }
 
 static void share_userfile_start(int idx, char *par) {
   dcc[idx].status |= STAT_GETTING;
-  stream_in.seek(0, SEEK_SET);
-  stream_in.truncate();
+  stream_in.clear();
 }
 
 static void share_userfile_end(int idx, char *par) {
@@ -1252,17 +1251,7 @@ write_tmp_userfile(char *fn, const struct userrec *bu, int idx)
   int ok = 1;
 
   if ((f = fopen(fn, "wb"))) {
-/* FIXME: REMOVE AFTER 1.2.14 */
-    bool old = 0;
-
-    tand_t* bot = idx != -1 ? findbot(dcc[idx].nick) : NULL;
-    if (bot && bot->buildts < 1175102242) /* flood-* hacks */
-      old = 1;
-
-    const char salt1[] = SALT1;
-    EncryptedStream stream(salt1);
-    stream_writeuserfile(stream, bu, idx, old);
-    if ((fwrite(stream.data(), 1, stream.length(), f) != stream.length()) || (fflush(f)))
+    if (real_writeuserfile(idx, bu, f))
       ok = 0;
     fclose(f);
     fixmod(fn);
@@ -1469,7 +1458,7 @@ static void share_read_stream(int idx, bd::Stream& stream) {
 static void
 ulsend(int idx, const char* data, size_t datalen)
 {
-  char buf[1040];
+  char buf[1040] = "";
 
   size_t len = simple_snprintf(buf, sizeof(buf), "s l %d %s", datalen-1, data);/* -1 for newline */
   tputs(dcc[idx].sock, buf, len);
@@ -1482,11 +1471,10 @@ stream_send_users(int idx)
   stream_writeuserfile(stream, userlist, idx);
   stream.seek(0, SEEK_SET);
   dprintf(idx, "s ls\n");
-  char buf[1024] = "";
-  size_t len = 0;
+  bd::String buf;
   while (stream.tell() < stream.length()) {
-    len = stream.gets(buf, sizeof(buf));
-    ulsend(idx, buf, len);
+    buf = stream.getline(1024);
+    ulsend(idx, buf.c_str(), buf.length());
   }
   dprintf(idx, "s le\n");
 }

+ 18 - 10
src/userrec.c

@@ -506,18 +506,9 @@ int write_userfile(int idx)
     sort_userlist();
 
 
-/* FIXME: REMOVE AFTER 1.2.14 */
-  bool old = 0;
-
-  tand_t* bot = idx != -1 ? findbot(dcc[idx].nick) : NULL;
-  if (bot && bot->buildts < 1175102242) /* flood-* hacks */
-    old = 1;
   putlog(LOG_DEBUG, "@", "Writing user entries.");
 
-  const char salt1[] = SALT1;
-  EncryptedStream stream(salt1);
-  stream_writeuserfile(stream, userlist, idx, old);
-  if ((fwrite(stream.data(), 1, stream.length(), f) != stream.length()) || fflush(f)) {
+  if (real_writeuserfile(idx, userlist, f)) {
     putlog(LOG_MISC, "*", "ERROR writing user file. (%s)", strerror(ferror(f)));
     fclose(f);
     free(new_userfile);
@@ -532,6 +523,23 @@ int write_userfile(int idx)
   return 0;
 }
 
+/* Used by writeuserfile() and write_tmp_userfile() in share.c */
+int real_writeuserfile(int idx, const struct userrec *bu, FILE *f) {
+/* FIXME: REMOVE AFTER 1.2.14 */
+  bool old = 0;
+
+  tand_t* bot = idx != -1 ? findbot(dcc[idx].nick) : NULL;
+  if (bot && bot->buildts < 1175102242) /* flood-* hacks */
+    old = 1;
+
+  const char salt1[] = SALT1;
+  EncryptedStream stream(salt1);
+  stream_writeuserfile(stream, bu, idx, old);
+  if ((fwrite(bd::String(stream).data(), 1, stream.length(), f) != stream.length()) || (fflush(f)))
+    return 1;
+  return 0;
+}
+
 int change_handle(struct userrec *u, char *newh)
 {
   if (!u)

+ 1 - 0
src/userrec.h

@@ -17,6 +17,7 @@ int change_handle(struct userrec *, char *);
 void correct_handle(char *);
 void stream_writeuserfile(bd::Stream&, const struct userrec *, int, bool = 0);
 int write_userfile(int);
+int real_writeuserfile(int idx, const struct userrec *bu, FILE *f);
 void touch_laston(struct userrec *, char *, time_t);
 void user_del_chan(char *);
 struct userrec *host_conflicts(char *);

+ 6 - 7
src/users.c

@@ -590,8 +590,7 @@ int readuserfile(const char *file, struct userrec **ret)
   const char salt1[] = SALT1;
   EncryptedStream stream(salt1);
   stream.loadFile(file);
-  int res = stream_readuserfile(stream, ret);
-  return res;
+  return stream_readuserfile(stream, ret);
 }
 
 int stream_readuserfile(bd::Stream& stream, struct userrec **ret)
@@ -615,19 +614,19 @@ int stream_readuserfile(bd::Stream& stream, struct userrec **ret)
   }
   noshare = 1;
   /* read opening comment */
-  stream.gets(s, 180);
-  remove_crlf(s);
-  if (s[1] < '4') {
+  bd::String str(stream.getline(180));
+  if (str[1] < '4') {
     putlog(LOG_MISC, "*", "!*! Empty or malformed userfile.");
     return 0;
   }
-  if (s[1] > '4') {
+  if (str[1] > '4') {
     putlog(LOG_MISC, "*", "Invalid userfile format.");
     return 0;
   }
   while (stream.tell() < stream.length()) {
     s = buf;
-    stream.gets(s, sizeof(buf));
+    str = stream.getline(sizeof(buf));
+    strlcpy(s, str.c_str(), std::min(str.length(), sizeof(buf)));
     remove_crlf(s);
     if (1) {
       line++;