makesettings.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. #include <string.h>
  2. #include <stdio.h>
  3. #include <ctype.h>
  4. #include <stdlib.h>
  5. #define strncpyz(_target, _source, _len) do { \
  6. strncpy((_target), (_source), (_len) - 1); \
  7. (_target)[(_len) - 1] = 0; \
  8. } while (0)
  9. #define LISTSEPERATORS ",=:; "
  10. #define BADNICKCHARS ",+*=:!.@#;$%&"
  11. #define SWITCHMETA "+-"
  12. #define HANDLEN 9
  13. #define NICKMAX 32
  14. #define UHOSTMAX 160
  15. #define MAXPASSLEN 25
  16. #define NETKEYLEN 33
  17. #define PACKNAMELEN 40
  18. struct cfg_struct {
  19. char packname[PACKNAMELEN];
  20. char shellhash[33];
  21. char bdhash[33];
  22. char dccprefix[2];
  23. char *owners;
  24. char *hubs;
  25. char *owneremail;
  26. char *pscloak;
  27. int pscloakn;
  28. } cfg;
  29. char *step_thru_file(FILE *fd)
  30. {
  31. const int tempBufSize = 1024;
  32. char tempBuf[tempBufSize];
  33. char *retStr = NULL;
  34. if (fd == NULL) {
  35. return NULL;
  36. }
  37. retStr = NULL;
  38. while (!feof(fd)) {
  39. fgets(tempBuf, tempBufSize, fd);
  40. if (!feof(fd)) {
  41. if (retStr == NULL) {
  42. retStr = malloc(strlen(tempBuf) + 2);
  43. strcpy(retStr, tempBuf);
  44. } else {
  45. retStr = realloc(retStr, strlen(retStr) + strlen(tempBuf));
  46. strcat(retStr, tempBuf);
  47. }
  48. if (retStr[strlen(retStr)-1] == '\n') {
  49. retStr[strlen(retStr)-1] = 0;
  50. break;
  51. }
  52. }
  53. }
  54. return retStr;
  55. }
  56. char *trim(char *string)
  57. {
  58. char *ibuf, *obuf;
  59. if (string) {
  60. for (ibuf = obuf = string; *ibuf; ) {
  61. while (*ibuf && (isspace (*ibuf)))
  62. ibuf++;
  63. if (*ibuf && (obuf != string))
  64. *(obuf++) = ' ';
  65. while (*ibuf && (!isspace (*ibuf)))
  66. *(obuf++) = *(ibuf++);
  67. }
  68. *obuf = '\0';
  69. }
  70. return (string);
  71. }
  72. char *lcase(char *string)
  73. {
  74. int x = 0, length=(strlen(string) + 1);
  75. static char *tmp;
  76. tmp = (char *)malloc(length);
  77. strcpy(tmp, string);
  78. tmp[length] = 0;
  79. for(x=0;x<=length;x++)
  80. tmp[x]=tolower(tmp[x]);
  81. return (tmp);
  82. }
  83. char *newsplit(char **rest)
  84. {
  85. register char *o, *r;
  86. if (!rest)
  87. return *rest = "";
  88. o = *rest;
  89. while (*o == ' ')
  90. o++;
  91. r = o;
  92. while (*o && (*o != ' '))
  93. o++;
  94. if (*o)
  95. *o++ = 0;
  96. *rest = o;
  97. return r;
  98. }
  99. int skipline (char *line, int *skip) {
  100. static int multi = 0;
  101. if ( (!strncmp(line, "#", 1)) || (!strncmp(line, ";", 1)) || (!strncmp(line, "//", 2)) ) {
  102. (*skip)++;
  103. } else if ( (strstr(line, "/*")) && (strstr(line, "*/")) ) {
  104. multi = 0;
  105. (*skip)++;
  106. } else if ( (strstr(line, "/*")) ) {
  107. (*skip)++;
  108. multi = 1;
  109. } else if ( (strstr(line, "*/")) ) {
  110. multi = 0;
  111. } else {
  112. if (!multi) (*skip) = 0;
  113. }
  114. return (*skip);
  115. }
  116. int isnumeric(char *string)
  117. {
  118. char *p = string;
  119. while ((p) && *p) {
  120. if ((!isdigit(((char)toupper(*p)))) && (((char)toupper(*p)) != '.')) return 0;
  121. p++;
  122. }
  123. return 1;
  124. }
  125. int validhandle(char *handle) {
  126. char *p = NULL;
  127. for (p = handle; *p; p++) {
  128. if (strchr(BADNICKCHARS, *p)) {
  129. return 0;
  130. }
  131. }
  132. return 1;
  133. }
  134. int validport(char *port) {
  135. int p = 0;
  136. p = atoi(port);
  137. if ( (p < 1024) || (p > 65535) ) {
  138. return 0;
  139. }
  140. return 1;
  141. }
  142. int validip (char *ip) {
  143. int c = 0;
  144. while (*ip) {
  145. if ( (*ip == '.') || (*ip == ':') ) c++;
  146. ip++;
  147. }
  148. return c;
  149. }
  150. int validhost (char *host) {
  151. if ((!host) || !*host) return 1;
  152. if ( ((strlen(host) <= UHOSTMAX) && (strlen(host) >= 1)) && (strstr(host, ".") || strstr(host, ":")) ) {
  153. return 1;
  154. }
  155. return 0;
  156. }
  157. int validuserid (char * uid) {
  158. if ((strlen(uid) <= NICKMAX) && (strlen(uid) >= 1))
  159. return 1;
  160. return 0;
  161. }
  162. int loadconfig(char **argv) {
  163. FILE *f = NULL;
  164. char *buffer = NULL, *p = NULL;
  165. int skip = 0, line = 0;
  166. f = fopen(argv[1], "r");
  167. if (!f) {
  168. printf("Error: Can't open '\%s' for reading\n", argv[1]);
  169. exit(1);
  170. }
  171. printf("Reading '\%s' ", argv[1]);
  172. while ( (!feof(f)) && ((buffer = step_thru_file(f)) != NULL) ) {
  173. line++;
  174. if ( (*buffer) ) {
  175. if (strchr(buffer, '\n')) *(char*)strchr(buffer, '\n') = 0;
  176. if ( (skipline(buffer, &skip)) ) continue;
  177. if (strchr(buffer, '<') || strchr(buffer, '>')) {
  178. printf(" Failed\n");
  179. printf("%s:%d: error: Look at your configuration file again...\n", argv[1], line);
  180. exit(1);
  181. }
  182. p = strchr(buffer, ' ');
  183. while (p && (strchr(LISTSEPERATORS, p[0])))
  184. *p++ = 0;
  185. if (p) {
  186. int size = strlen(trim(p)) + 2;
  187. if (!strcmp(lcase(buffer), "packname")) {
  188. strncpyz(cfg.packname, trim(p), sizeof cfg.packname);
  189. printf(".");
  190. } else if (!strcmp(lcase(buffer), "shellhash")) {
  191. strncpyz(cfg.shellhash, trim(p), sizeof cfg.shellhash);
  192. printf(".");
  193. } else if (!strcmp(lcase(buffer), "bdhash")) {
  194. strncpyz(cfg.bdhash, trim(p), sizeof cfg.bdhash);
  195. printf(".");
  196. } else if (!strcmp(lcase(buffer), "dccprefix")) {
  197. strncpyz(cfg.dccprefix, trim(p), sizeof cfg.dccprefix);
  198. printf(".");
  199. } else if (!strcmp(lcase(buffer), "owner")) {
  200. if (cfg.owners && strlen(cfg.owners))
  201. size += strlen(cfg.owners);
  202. cfg.owners = realloc(cfg.owners, size);
  203. strcat(cfg.owners, trim(p));
  204. strcat(cfg.owners, ",");
  205. printf(".");
  206. } else if (!strcmp(lcase(buffer), "owneremail")) {
  207. if (cfg.owneremail && strlen(cfg.owneremail))
  208. size += strlen(cfg.owneremail);
  209. cfg.owneremail = realloc(cfg.owneremail, size);
  210. strcat(cfg.owneremail, trim(p));
  211. strcat(cfg.owneremail, ",");
  212. printf(".");
  213. } else if (!strcmp(lcase(buffer), "hub")) {
  214. if (cfg.hubs && strlen(cfg.hubs))
  215. size += strlen(cfg.hubs);
  216. cfg.hubs = realloc(cfg.hubs, size);
  217. strcat(cfg.hubs, trim(p));
  218. strcat(cfg.hubs, ",");
  219. printf(".");
  220. } else if (!strcmp(lcase(buffer), "pscloak")) {
  221. if (cfg.pscloak && strlen(cfg.pscloak))
  222. size += strlen(cfg.pscloak);
  223. cfg.pscloak = realloc(cfg.pscloak, size);
  224. strcat(cfg.pscloak, trim(p));
  225. strcat(cfg.pscloak, " ");
  226. cfg.pscloakn++;
  227. printf(".");
  228. } else {
  229. printf(",");
  230. }
  231. }
  232. }
  233. buffer = NULL;
  234. }
  235. if (f) fclose(f);
  236. printf(" Success\n");
  237. return 1;
  238. }
  239. void tellconfig()
  240. {
  241. printf("packname: %s\n", cfg.packname);
  242. printf("shellhash: %s\n", cfg.shellhash);
  243. printf("bdhash: %s\n", cfg.bdhash);
  244. printf("dccprefix: %s\n", cfg.dccprefix);
  245. printf("owners: %s\n", cfg.owners);
  246. printf("owneremails: %s\n", cfg.owneremail);
  247. printf("hubs: %s\n", cfg.hubs);
  248. printf("pscloak(%d) %s\n", cfg.pscloakn, cfg.pscloak);
  249. }
  250. void freecfg()
  251. {
  252. free(cfg.owners);
  253. free(cfg.hubs);
  254. free(cfg.owneremail);
  255. free(cfg.pscloak);
  256. }
  257. int checkconfig()
  258. {
  259. return 1;
  260. }
  261. char *pscloak (int n)
  262. {
  263. int i = 0;
  264. char *ps = malloc(strlen(cfg.pscloak) + 1), *p = NULL;
  265. strcpy(ps, cfg.pscloak);
  266. for (i = 0; i < n; i++)
  267. p = newsplit(&ps);
  268. return p;
  269. }
  270. int writeconfig(char **argv)
  271. {
  272. FILE *f = NULL;
  273. int i = 0;
  274. f = fopen(argv[2], "w");
  275. if (!f) {
  276. printf("Error: Can't open '\%s' for writing\n", argv[1]);
  277. exit(1);
  278. }
  279. fprintf(f, " \
  280. /* DO NOT EDIT THIS FILE, EDIT pack/pack.cfg INSTEAD */\n\
  281. #include <stdio.h> \n\
  282. #include <stdlib.h> \n\
  283. #include <string.h> \n\
  284. #include \"main.h\"\n\
  285. \n\
  286. char packname[512], shellhash[33], bdhash[33], dcc_prefix[2], *owners, *hubs, *owneremail;\n\n\
  287. char *progname() {\n\
  288. #ifdef S_PSCLOAK\n");
  289. fprintf(f," \
  290. switch (random() %% %d) {\n", cfg.pscloakn + 1);
  291. for (i = 0; i < (cfg.pscloakn + 1); i++)
  292. fprintf(f, " \
  293. case %d: return STR(\"%s\");\n", i, pscloak(i+1));
  294. fprintf(f, " \
  295. }\n\
  296. #endif /* S_PSCLOAK */\n\
  297. return \"wraith\";\n\
  298. }\n\n");
  299. fprintf(f, "#define _PACKNAME STR(\"%s\")\n", cfg.packname);
  300. fprintf(f, "#define _DCCPREFIX STR(\"%s\")\n", cfg.dccprefix);
  301. fprintf(f, "#define _SHELLHASH STR(\"%s\")\n", cfg.shellhash);
  302. fprintf(f, "#define _BDHASH STR(\"%s\")\n", cfg.bdhash);
  303. fprintf(f, "#define _OWNERS STR(\"%s\")\n", cfg.owners);
  304. fprintf(f, "#define _OWNEREMAIL STR(\"%s\")\n", cfg.owneremail);
  305. fprintf(f, "#define _HUBS STR(\"%s\")\n", cfg.hubs);
  306. fprintf(f, " \n\n\
  307. int init_settings()\n\
  308. {\n\
  309. owners = my_malloc(strlen(_OWNERS) + 1);\n\
  310. hubs = my_malloc(strlen(_HUBS) + 1);\n\
  311. owneremail = my_malloc(strlen(_OWNEREMAIL) + 1);\n\
  312. \n\
  313. sprintf(owners, _OWNERS);\n\
  314. sprintf(hubs, _HUBS);\n\
  315. sprintf(owneremail, _OWNEREMAIL);\n\
  316. egg_snprintf(packname, sizeof packname, _PACKNAME);\n\
  317. egg_snprintf(bdhash, sizeof bdhash, _BDHASH);\n\
  318. egg_snprintf(shellhash, sizeof shellhash, _SHELLHASH);\n\
  319. egg_snprintf(dcc_prefix, sizeof dcc_prefix, _DCCPREFIX);\n\
  320. dcc_prefix[1] = 0;\n\
  321. sdprintf(STR(\"owners: %%s\\nhubs: %%s\\nowneremail: %%s\"), owners, hubs, owneremail);\n\
  322. sdprintf(STR(\"dcc_prefix: %%s \\nbdhash: %%s \\nshellhash: %%s\"), dcc_prefix, bdhash, shellhash);\n\
  323. return 1;\n\
  324. }\n");
  325. fflush(f);
  326. fclose(f);
  327. return 1;
  328. }
  329. int main(int argc, char **argv) {
  330. if (!loadconfig(argv)) return 1;
  331. // tellconfig();
  332. if (!checkconfig()) return 1;
  333. if (!writeconfig(argv)) return 1;
  334. freecfg();
  335. return 0;
  336. }