makesettings.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  1. #include <string.h>
  2. #include <stdio.h>
  3. #include <ctype.h>
  4. #include <stdlib.h>
  5. #include <time.h>
  6. #include <sys/types.h>
  7. #include <unistd.h>
  8. #define strncpyz(_target, _source, _len) do { \
  9. strncpy((_target), (_source), (_len) - 1); \
  10. (_target)[(_len) - 1] = 0; \
  11. } while (0)
  12. #define LISTSEPERATORS ",=:; "
  13. #define BADNICKCHARS ",+*=:!.@#;$%&"
  14. #define SWITCHMETA "+-"
  15. #define HANDLEN 9
  16. #define NICKMAX 32
  17. #define UHOSTMAX 160
  18. #define MAXPASSLEN 25
  19. #define NETKEYLEN 33
  20. #define PACKNAMELEN 40
  21. struct cfg_struct {
  22. char packname[PACKNAMELEN];
  23. char shellhash[33];
  24. char bdhash[33];
  25. char dccprefix[2];
  26. char *owners;
  27. char *hubs;
  28. char *owneremail;
  29. char *pscloak;
  30. int pscloakn;
  31. char *salt1;
  32. char *salt2;
  33. char *defines;
  34. int definesn;
  35. } cfg;
  36. void mallocstruct() {
  37. cfg.owners = malloc(1);
  38. cfg.hubs = malloc(1);
  39. cfg.owneremail = malloc(1);
  40. cfg.pscloak = malloc(1);
  41. cfg.salt1 = malloc(1);
  42. cfg.salt2 = malloc(1);
  43. cfg.defines = malloc(1);
  44. cfg.pscloakn = 0;
  45. cfg.definesn = 0;
  46. }
  47. char *step_thru_file(FILE *fd)
  48. {
  49. const int tempBufSize = 1024;
  50. char tempBuf[tempBufSize];
  51. char *retStr = NULL;
  52. if (fd == NULL) {
  53. return NULL;
  54. }
  55. retStr = NULL;
  56. while (!feof(fd)) {
  57. fgets(tempBuf, tempBufSize, fd);
  58. if (!feof(fd)) {
  59. if (retStr == NULL) {
  60. retStr = malloc(strlen(tempBuf) + 2);
  61. strcpy(retStr, tempBuf);
  62. } else {
  63. retStr = realloc(retStr, strlen(retStr) + strlen(tempBuf));
  64. strcat(retStr, tempBuf);
  65. }
  66. if (retStr[strlen(retStr)-1] == '\n') {
  67. retStr[strlen(retStr)-1] = 0;
  68. break;
  69. }
  70. }
  71. }
  72. return retStr;
  73. }
  74. char *trim(char *string)
  75. {
  76. char *ibuf, *obuf;
  77. if (string) {
  78. for (ibuf = obuf = string; *ibuf; ) {
  79. while (*ibuf && (isspace (*ibuf)))
  80. ibuf++;
  81. if (*ibuf && (obuf != string))
  82. *(obuf++) = ' ';
  83. while (*ibuf && (!isspace (*ibuf)))
  84. *(obuf++) = *(ibuf++);
  85. }
  86. *obuf = '\0';
  87. }
  88. return (string);
  89. }
  90. char *lcase(char *string)
  91. {
  92. int x = 0, length=(strlen(string) + 1);
  93. static char *tmp;
  94. tmp = (char *)malloc(length);
  95. strcpy(tmp, string);
  96. tmp[length] = 0;
  97. for(x=0;x<=length;x++)
  98. tmp[x]=tolower(tmp[x]);
  99. return (tmp);
  100. }
  101. char *newsplit(char **rest)
  102. {
  103. register char *o, *r;
  104. if (!rest)
  105. return *rest = "";
  106. o = *rest;
  107. while (*o == ' ')
  108. o++;
  109. r = o;
  110. while (*o && (*o != ' '))
  111. o++;
  112. if (*o)
  113. *o++ = 0;
  114. *rest = o;
  115. return r;
  116. }
  117. int skipline (char *line, int *skip) {
  118. static int multi = 0;
  119. if ( (!strncmp(line, "#", 1)) || (!strncmp(line, ";", 1)) || (!strncmp(line, "//", 2)) ) {
  120. (*skip)++;
  121. } else if ( (strstr(line, "/*")) && (strstr(line, "*/")) ) {
  122. multi = 0;
  123. (*skip)++;
  124. } else if ( (strstr(line, "/*")) ) {
  125. (*skip)++;
  126. multi = 1;
  127. } else if ( (strstr(line, "*/")) ) {
  128. multi = 0;
  129. } else {
  130. if (!multi) (*skip) = 0;
  131. }
  132. return (*skip);
  133. }
  134. int isnumeric(char *string)
  135. {
  136. char *p = string;
  137. while ((p) && *p) {
  138. if ((!isdigit(((char)toupper(*p)))) && (((char)toupper(*p)) != '.')) return 0;
  139. p++;
  140. }
  141. return 1;
  142. }
  143. int validhandle(char *handle) {
  144. char *p = NULL;
  145. for (p = handle; *p; p++) {
  146. if (strchr(BADNICKCHARS, *p)) {
  147. return 0;
  148. }
  149. }
  150. return 1;
  151. }
  152. int validport(char *port) {
  153. int p = 0;
  154. p = atoi(port);
  155. if ( (p < 1024) || (p > 65535) ) {
  156. return 0;
  157. }
  158. return 1;
  159. }
  160. int validip (char *ip) {
  161. int c = 0;
  162. while (*ip) {
  163. if ( (*ip == '.') || (*ip == ':') ) c++;
  164. ip++;
  165. }
  166. return c;
  167. }
  168. int validhost (char *host) {
  169. if ((!host) || !*host) return 1;
  170. if ( ((strlen(host) <= UHOSTMAX) && (strlen(host) >= 1)) && (strstr(host, ".") || strstr(host, ":")) ) {
  171. return 1;
  172. }
  173. return 0;
  174. }
  175. int validuserid (char * uid) {
  176. if ((strlen(uid) <= NICKMAX) && (strlen(uid) >= 1))
  177. return 1;
  178. return 0;
  179. }
  180. char *randstring(int len)
  181. {
  182. int j, r = 0;
  183. static char s[100];
  184. for (j = 0; j < len; j++) {
  185. r = random();
  186. if (r % 3 == 0)
  187. s[j] = '0' + (random() % 10);
  188. else if (r % 3 == 1)
  189. s[j] = 'a' + (random() % 26);
  190. else if (r % 3 == 2)
  191. s[j] = 'A' + (random() % 26);
  192. }
  193. s[len] = '\0';
  194. return s;
  195. }
  196. void dosalt(char *salt1, char *salt2) {
  197. FILE *f = NULL;
  198. f = fopen("src/salt.h.tmp", "w");
  199. fprintf(f, "#define STR(x) x\n");
  200. fprintf(f, "#define SALT1 STR(\"%s\")\n", salt1);
  201. fprintf(f, "#define SALT2 STR(\"%s\")\n", salt2);
  202. fflush(f);
  203. fclose(f);
  204. }
  205. int loadconfig(char **argv) {
  206. FILE *f = NULL;
  207. char *buffer = NULL, *p = NULL;
  208. int skip = 0, line = 0;
  209. f = fopen(argv[1], "r");
  210. if (!f) {
  211. printf("Error: Can't open '\%s' for reading\n", argv[1]);
  212. exit(1);
  213. }
  214. printf("Reading '\%s' ", argv[1]);
  215. while ( (!feof(f)) && ((buffer = step_thru_file(f)) != NULL) ) {
  216. line++;
  217. if ( (*buffer) ) {
  218. if (strchr(buffer, '\n')) *(char*)strchr(buffer, '\n') = 0;
  219. if ( (skipline(buffer, &skip)) ) continue;
  220. if (strchr(buffer, '<') || strchr(buffer, '>')) {
  221. printf(" Failed\n");
  222. printf("%s:%d: error: Look at your configuration file again...\n", argv[1], line);
  223. exit(1);
  224. }
  225. p = strchr(buffer, ' ');
  226. while (p && (strchr(LISTSEPERATORS, p[0])))
  227. *p++ = 0;
  228. if (p) {
  229. int size = strlen(trim(p)) + 2;
  230. if (!strcmp(lcase(buffer), "packname")) {
  231. strncpyz(cfg.packname, trim(p), sizeof cfg.packname);
  232. printf(".");
  233. } else if (!strcmp(lcase(buffer), "shellhash")) {
  234. strncpyz(cfg.shellhash, trim(p), sizeof cfg.shellhash);
  235. printf(".");
  236. } else if (!strcmp(lcase(buffer), "bdhash")) {
  237. strncpyz(cfg.bdhash, trim(p), sizeof cfg.bdhash);
  238. printf(".");
  239. } else if (!strcmp(lcase(buffer), "dccprefix")) {
  240. strncpyz(cfg.dccprefix, trim(p), sizeof cfg.dccprefix);
  241. printf(".");
  242. } else if (!strcmp(lcase(buffer), "owner")) {
  243. if (cfg.owners && strlen(cfg.owners))
  244. size += strlen(cfg.owners);
  245. size += strlen(trim(p)) + 1;
  246. cfg.owners = realloc(cfg.owners, size);
  247. strcat(cfg.owners, trim(p));
  248. strcat(cfg.owners, ",");
  249. printf(".");
  250. } else if (!strcmp(lcase(buffer), "owneremail")) {
  251. if (cfg.owneremail && strlen(cfg.owneremail))
  252. size += strlen(cfg.owneremail);
  253. size += strlen(trim(p)) + 1;
  254. cfg.owneremail = realloc(cfg.owneremail, size);
  255. strcat(cfg.owneremail, trim(p));
  256. strcat(cfg.owneremail, ",");
  257. printf(".");
  258. } else if (!strcmp(lcase(buffer), "hub")) {
  259. if (cfg.hubs && strlen(cfg.hubs))
  260. size += strlen(cfg.hubs);
  261. size += strlen(trim(p)) + 1;
  262. cfg.hubs = realloc(cfg.hubs, size);
  263. strcat(cfg.hubs, trim(p));
  264. strcat(cfg.hubs, ",");
  265. printf(".");
  266. } else if (!strcmp(lcase(buffer), "pscloak")) {
  267. if (cfg.pscloak && strlen(cfg.pscloak))
  268. size += strlen(cfg.pscloak);
  269. size += strlen(trim(p)) + 1;
  270. cfg.pscloak = realloc(cfg.pscloak, size);
  271. strcat(cfg.pscloak, trim(p));
  272. strcat(cfg.pscloak, " ");
  273. cfg.pscloakn++;
  274. printf(".");
  275. } else if (!strcmp(lcase(buffer), "-")) {
  276. } else if (!strcmp(lcase(buffer), "+")) {
  277. char *define;
  278. if (cfg.defines && strlen(cfg.defines))
  279. size += strlen(cfg.defines);
  280. trim(p);
  281. define = newsplit(&p);
  282. size += strlen(define) + 1;
  283. cfg.defines = realloc(cfg.defines, size);
  284. strcat(cfg.defines, define);
  285. strcat(cfg.defines, " ");
  286. cfg.definesn++;
  287. printf(".");
  288. } else if (!strcmp(lcase(buffer), "salt1")) {
  289. cfg.salt1 = malloc(strlen(trim(p)) + 1);
  290. strcat(cfg.salt1, trim(p));
  291. printf(".");
  292. } else if (!strcmp(lcase(buffer), "salt2")) {
  293. cfg.salt2 = malloc(strlen(trim(p)) + 1);
  294. strcat(cfg.salt2, trim(p));
  295. printf(".");
  296. } else {
  297. printf("%s %s\n", buffer, p);
  298. printf(",");
  299. }
  300. }
  301. }
  302. buffer = NULL;
  303. }
  304. if (f) fclose(f);
  305. if (cfg.salt1 && cfg.salt2 && cfg.salt1[2] && cfg.salt2[2]) {
  306. dosalt(cfg.salt1, cfg.salt2);
  307. } else { /* we need to generate the SALTS */
  308. char salt1[33], salt2[17];
  309. time_t now = time(NULL);
  310. salt1[0] = salt2[0] = 0;
  311. srandom(now % (getpid() + getppid()));
  312. printf("Creating Salts");
  313. if ((f = fopen("pack/pack.cfg", "a")) == NULL) {
  314. printf("Cannot created Salt-File.. aborting\n");
  315. exit(1);
  316. }
  317. strcat(salt1, randstring(sizeof salt1));
  318. strcat(salt2, randstring(sizeof salt2));
  319. salt1[sizeof salt1] = salt2[sizeof salt2] = 0;
  320. fprintf(f, "SALT1 %s\n", salt1);
  321. fprintf(f, "SALT2 %s\n", salt2);
  322. fflush(f);
  323. fclose(f);
  324. dosalt(salt1, salt2);
  325. }
  326. /* no longer needed, hooray configure
  327. if (cfg.definesn && cfg.defines) {
  328. char *def;
  329. int i = 0;
  330. f = fopen("src/conf.h~", "w");
  331. fprintf(f, "#ifndef _S_CONF_H\n#define _S_CONF_H\n\n");
  332. for (i = 0; i < cfg.definesn; i++) {
  333. def = newsplit(&cfg.defines);
  334. fprintf(f, "#define S_%s\n", def);
  335. }
  336. fprintf(f, "\n#endif\n");
  337. fflush(f);
  338. fclose(f);
  339. }
  340. */
  341. printf(" Success\n");
  342. return 1;
  343. }
  344. void tellconfig()
  345. {
  346. printf("packname: %s\n", cfg.packname);
  347. printf("shellhash: %s\n", cfg.shellhash);
  348. printf("bdhash: %s\n", cfg.bdhash);
  349. printf("dccprefix: %s\n", cfg.dccprefix);
  350. printf("owners: %s\n", cfg.owners);
  351. printf("owneremails: %s\n", cfg.owneremail);
  352. printf("hubs: %s\n", cfg.hubs);
  353. printf("pscloak(%d) %s\n", cfg.pscloakn, cfg.pscloak);
  354. }
  355. void freecfg()
  356. {
  357. free(cfg.owners);
  358. free(cfg.hubs);
  359. free(cfg.owneremail);
  360. free(cfg.pscloak);
  361. }
  362. int checkconfig()
  363. {
  364. return 1;
  365. }
  366. char *pscloak (int n)
  367. {
  368. int i = 0;
  369. char *ps = strdup(cfg.pscloak), *p = NULL;
  370. for (i = 0; i < n; i++)
  371. p = newsplit(&ps);
  372. return p;
  373. }
  374. int writeconfig(char **argv)
  375. {
  376. FILE *f = NULL;
  377. int i = 0;
  378. f = fopen(argv[2], "w");
  379. if (!f) {
  380. printf("Error: Can't open '\%s' for writing\n", argv[1]);
  381. exit(1);
  382. }
  383. fprintf(f, " \
  384. /* DO NOT EDIT THIS FILE, EDIT pack/pack.cfg INSTEAD */\n\
  385. #include <stdio.h> \n\
  386. #include <stdlib.h> \n\
  387. #include <string.h> \n\
  388. #include \"common.h\"\n\
  389. #include \"debug.h\"\n\
  390. \n\
  391. char packname[512], shellhash[33], bdhash[33], dcc_prefix[2], *owners, *hubs, *owneremail;\n\n\
  392. char *progname() {\n\
  393. #ifdef S_PSCLOAK\n");
  394. fprintf(f," \
  395. switch (random() %% %d) {\n", cfg.pscloakn + 1);
  396. for (i = 0; i < (cfg.pscloakn + 1); i++)
  397. fprintf(f, " \
  398. case %d: return STR(\"%s\");\n", i, pscloak(i+1));
  399. fprintf(f, " \
  400. }\n\
  401. #endif /* S_PSCLOAK */\n\
  402. return \"wraith\";\n\
  403. }\n\n");
  404. fprintf(f, "#define _PACKNAME STR(\"%s\")\n", cfg.packname);
  405. fprintf(f, "#define _DCCPREFIX STR(\"%s\")\n", cfg.dccprefix);
  406. fprintf(f, "#define _SHELLHASH STR(\"%s\")\n", cfg.shellhash);
  407. fprintf(f, "#define _BDHASH STR(\"%s\")\n", cfg.bdhash);
  408. fprintf(f, "#define _OWNERS STR(\"%s\")\n", cfg.owners);
  409. fprintf(f, "#define _OWNEREMAIL STR(\"%s\")\n", cfg.owneremail);
  410. fprintf(f, "#define _HUBS STR(\"%s\")\n", cfg.hubs);
  411. fprintf(f, " \n\n\
  412. int init_settings()\n\
  413. {\n\
  414. owners = malloc(strlen(_OWNERS) + 1);\n\
  415. hubs = malloc(strlen(_HUBS) + 1);\n\
  416. owneremail = malloc(strlen(_OWNEREMAIL) + 1);\n\
  417. \n\
  418. sprintf(owners, _OWNERS);\n\
  419. sprintf(hubs, _HUBS);\n\
  420. sprintf(owneremail, _OWNEREMAIL);\n\
  421. egg_snprintf(packname, sizeof packname, _PACKNAME);\n\
  422. egg_snprintf(bdhash, sizeof bdhash, _BDHASH);\n\
  423. egg_snprintf(shellhash, sizeof shellhash, _SHELLHASH);\n\
  424. egg_snprintf(dcc_prefix, sizeof dcc_prefix, _DCCPREFIX);\n\
  425. dcc_prefix[1] = 0;\n\
  426. sdprintf(STR(\"owners: %%s\\nhubs: %%s\\nowneremail: %%s\"), owners, hubs, owneremail);\n\
  427. sdprintf(STR(\"dcc_prefix: %%s \\nbdhash: %%s \\nshellhash: %%s\"), dcc_prefix, bdhash, shellhash);\n\
  428. return 1;\n\
  429. }\n");
  430. fflush(f);
  431. fclose(f);
  432. return 1;
  433. }
  434. int main(int argc, char **argv) {
  435. mallocstruct();
  436. if (!loadconfig(argv)) return 1;
  437. // tellconfig();
  438. if (!checkconfig()) return 1;
  439. if (!writeconfig(argv)) return 1;
  440. freecfg();
  441. return 0;
  442. }