Code afférent au projet Kouglof 2 de l'E2L
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 
 

1106 linhas
28 KiB

  1. /*******************************************************************
  2. Copyright (C) 2011-2024 Patrick H. E. Foubet - S.E.R.I.A.N.E.
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 3 of the License, or any
  6. later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. See the GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>
  13. *******************************************************************/
  14. /*
  15. ############################################################
  16. # Projet Kouglof 2 de l'Ecole du Logiciel Libre d'Ivry : #
  17. ############################################################
  18. octave.c : outil pour scanner l'interface reseau afin d'analyser les sites
  19. auxquels les applications veulent se connecter.
  20. A utiliser avec le fichier auth1.txt pour stopper les connexions non voulues
  21. Tous les details sur le site :
  22. https://e2li.org -> menu : Projet Prosecco.
  23. */
  24. #include <stdio.h>
  25. #include <stdlib.h>
  26. #include <sys/types.h>
  27. #include <unistd.h>
  28. #include <signal.h>
  29. #include <fcntl.h>
  30. #include <readline/readline.h>
  31. #include <readline/history.h>
  32. #include <sys/wait.h>
  33. #include <string.h>
  34. #include <syslog.h>
  35. #include <time.h>
  36. #include <arpa/inet.h>
  37. #include <pthread.h>
  38. #define Version "1.00"
  39. #define F_GETPIPE_SZ 1032
  40. #define F_SETPIPE_SZ 1031
  41. static int RUN=1, REQ=0, ENDT=0, REPR=0, p1[2],Trace=0;
  42. static pid_t pid;
  43. static char * NPROG, *IFACE=NULL;
  44. #define DELAYR 20 /* delai avant relance auto */
  45. void interup (int S)
  46. {
  47. if (S==SIGINT) {
  48. write(p1[1],"\n",1);
  49. REQ=1; return;
  50. }
  51. if (S==SIGCHLD) {
  52. if (waitpid(pid,NULL,WNOHANG) == pid) {
  53. ENDT=1;
  54. write(p1[1],"\n",1);
  55. }
  56. return;
  57. }
  58. fprintf(stderr,"Reçu signal %d !!??\n",S);
  59. }
  60. /* ### les niveaux de trace */
  61. #define TMIN 0
  62. #define TMAX 3
  63. #define T1 Trace > 0
  64. #define T2 Trace > 1
  65. #define T3 Trace > 2
  66. /* #### les fonctions adresses IPv4 */
  67. int isIPv4(char *a)
  68. {
  69. struct in_addr S;
  70. int r;
  71. if ((r = inet_pton(AF_INET,a, (void *)&S)) <= 0) return 0;
  72. return 1;
  73. }
  74. int isCidr(char*r)
  75. {
  76. char buf[20], *sn;
  77. int n;
  78. if (strlen(r)>18) return 0;
  79. strcpy(buf,r);
  80. if ((sn=strstr(buf,"/")) == NULL) return 0;
  81. *sn = '\0';
  82. if (!isIPv4(buf)) return 0;
  83. n = atoi(sn+1);
  84. if (n>32) return 0;
  85. return n;
  86. }
  87. int isSousRes(char*r, char *a)
  88. {
  89. struct in_addr Sa, Sr, Sm;
  90. int n, m1,m2,m3,m4, m;
  91. char buf[20],smasq[16],*sn;
  92. if (!isCidr(r)) return 0;
  93. if (!isIPv4(a)) return 0;
  94. strcpy(buf,r);
  95. if ((sn=strstr(buf,"/")) == NULL) return 0;
  96. *sn = '\0';
  97. n = atoi(sn+1);
  98. /* calcul du masq */
  99. m=n;
  100. if (m>7) { m1=255; m-=8;
  101. } else { m1=0;
  102. while (m>=0) m1 |= 0x80 >> --m;
  103. }
  104. if (m>7) { m2=255; m-=8;
  105. } else { m2=0;
  106. while (m>=0) m2 |= 0x80 >> --m;
  107. }
  108. if (m>7) { m3=255; m-=8;
  109. } else { m3=0;
  110. while (m>=0) m3 |= 0x80 >> --m;
  111. }
  112. if (m>7) { m4=255; m-=8;
  113. } else { m4=0;
  114. while (m>=0) m4 |= 0x80 >> --m;
  115. }
  116. sprintf(smasq,"%d.%d.%d.%d",m1,m2,m3,m4);
  117. inet_pton(AF_INET,smasq, (void *)&Sm);
  118. inet_pton(AF_INET,a, (void *)&Sa);
  119. inet_pton(AF_INET,buf, (void *)&Sr);
  120. if ((Sr.s_addr & Sm.s_addr) == (Sa.s_addr & Sm.s_addr)) return 1;
  121. return 0;
  122. }
  123. /* #### gestion dynamique des CIDR */
  124. #define NBC 100 /* nb de CIDR */
  125. char* Tcidr[NBC];
  126. int iC=0;
  127. int bloqueIP(char*);
  128. int addCidr(char * c)
  129. {
  130. int i=iC;
  131. if (i==NBC) return i;
  132. Tcidr[i] = (char*)malloc(strlen(c)+1);
  133. strcpy(Tcidr[i],c);
  134. iC++;
  135. bloqueIP(c);
  136. return i;
  137. }
  138. void delCidr(char * c)
  139. {
  140. int i;
  141. for (i=0; i<iC; i++)
  142. if (strcmp(c,Tcidr[i]) == 0) {
  143. iC--;
  144. if (iC == i) return;
  145. if (iC > 0) Tcidr[i] = Tcidr[iC];
  146. return;
  147. }
  148. }
  149. int isAddrInCidr(char * a, int M)
  150. {
  151. int i;
  152. for(i=0;i<iC;i++) {
  153. if (isSousRes(Tcidr[i],a)) {
  154. if (M) syslog(LOG_INFO,"CIDR %s contient %s !",Tcidr[i],a);
  155. return 1;
  156. }
  157. }
  158. return 0;
  159. }
  160. void validCidr(void)
  161. {
  162. int i,j;
  163. char a[20],*p, *g, *w;
  164. for(i=0;i<iC;i++)
  165. for (j=i+1;j<iC;j++) {
  166. if (isCidr(Tcidr[i]) < isCidr(Tcidr[j])) {
  167. p=Tcidr[i]; g=Tcidr[j];
  168. } else {
  169. p=Tcidr[j]; g=Tcidr[i];
  170. }
  171. strcpy(a,g);
  172. w = strstr(a,"/");
  173. *w = '\0';
  174. w++;
  175. if (isSousRes(p,a)) {
  176. if (T3) printf("T3: %s contient %s (%s)\n",p,a,w);
  177. delCidr(g);
  178. }
  179. }
  180. }
  181. void listCidr(void)
  182. {
  183. int i;
  184. printf("CIDR : %d\n",iC);
  185. for (i=0; i<iC; i++) printf("\t%s\n", Tcidr[i]);
  186. }
  187. /* ### gestion des listes */
  188. #define NBAll 500
  189. #define NBDen 300
  190. char * Allow[NBAll];
  191. char * Deny[NBDen];
  192. int iAll=0, iDen=0;
  193. int isDeny(char*u)
  194. {
  195. char *su;
  196. int i, tu, t;
  197. for (i=0;i<iDen;i++) {
  198. tu = strlen(u);
  199. t = strlen(Deny[i]);
  200. if (tu < t) continue;
  201. su = u + tu - t;;
  202. if (strcmp(su,Deny[i]) == 0) {
  203. if (su==u) return 1;
  204. if (*(su-1)=='.') return 1;
  205. }
  206. }
  207. for (i=0;i<iAll;i++) {
  208. tu = strlen(u);
  209. t = strlen(Allow[i]);
  210. if (tu < t) continue;
  211. su = u + tu - t;
  212. if (strcmp(su,Allow[i]) == 0) {
  213. if (*(Allow[i]) == '.') return 0;
  214. if (su==u) return 0;
  215. if (*(su-1)=='.') return 0;
  216. }
  217. }
  218. return 1; /* deny par defaut */
  219. }
  220. void listeAllow(void)
  221. {
  222. int i;
  223. printf("Allow : %d\n",iAll);
  224. for (i=0;i<iAll;i++) printf("\t%s\n",Allow[i]);
  225. }
  226. void listeDeny(void)
  227. {
  228. int i;
  229. printf("Deny : %d\n",iDen);
  230. for (i=0;i<iDen;i++) printf("\t%s\n",Deny[i]);
  231. }
  232. void dejaLa(char * e)
  233. {
  234. printf("%s est deja dans la liste !\n",e);
  235. }
  236. int dejaAllow(char *e)
  237. {
  238. int i;
  239. for (i=0;i<iAll;i++) {
  240. if (strlen(e) != strlen(Allow[i])) continue;
  241. if (strcmp(e,Allow[i])==0) {
  242. dejaLa(e); return 1;
  243. }
  244. }
  245. return 0;
  246. }
  247. int dejaDeny(char *e)
  248. {
  249. int i;
  250. for (i=0;i<iDen;i++) {
  251. if (strlen(e) != strlen(Deny[i])) continue;
  252. if (strcmp(e,Deny[i])==0) {
  253. dejaLa(e); return 1;
  254. }
  255. }
  256. return 0;
  257. }
  258. void recaplistes(void)
  259. {
  260. listeDeny();
  261. listeAllow();
  262. }
  263. int litligne(char * line)
  264. {
  265. char *w, **S;
  266. void * M;
  267. int t;
  268. if (*line == '#') return 1;
  269. if ((w=strstr(line, "\n")) != NULL) *w = '\0';
  270. w=line;
  271. if (*w == '-') w++;
  272. t=strlen(w);
  273. if (t==0) return 1;
  274. if (isCidr(w)) { /* test si CIDR */
  275. addCidr(w);
  276. return 1;
  277. }
  278. if (*line == '-') {
  279. if (iDen == NBDen) return 0;
  280. if (dejaDeny(w)) return 0;
  281. S = &Deny[iDen];
  282. iDen++;
  283. } else {
  284. if (iAll == NBAll) return 0;
  285. if (dejaAllow(w)) return 0;
  286. S = &Allow[iAll];
  287. iAll++;
  288. }
  289. if ((M = malloc(t+1)) == NULL) {
  290. perror("malloc"); return 0;
  291. }
  292. *S=(char*)M;
  293. strcpy(*S,w);
  294. return 1;
  295. }
  296. void lectliste(char *f)
  297. {
  298. FILE * fd;
  299. char *line = NULL;
  300. size_t ll = 0;
  301. int n;
  302. if ((fd = fopen(f,"r")) == NULL) {
  303. perror(f); return;
  304. }
  305. while ((n = getline(&line, &ll, fd)) > 0) {
  306. if (!litligne(line)) {
  307. if (T1) printf("T1: Erreur param. = %s\n",line);
  308. }
  309. }
  310. free(line);
  311. fclose(fd);
  312. validCidr();
  313. if (T1) listCidr();
  314. }
  315. /* ### gestion dynamique des elts */
  316. #define NBT 1000 /* nb d'elts */
  317. int Tno[NBT];
  318. int Trv[NBT];
  319. char* Turl[NBT];
  320. int iT=0, NbElt=0, MaxElt=0;
  321. int addElt(int n, char * u)
  322. {
  323. int i=iT;
  324. if (i == NBT) return i;
  325. Tno[i]=n;
  326. Trv[i]=0;
  327. Turl[i] = (char*)malloc(strlen(u)+1);
  328. strcpy(Turl[i],u);
  329. iT++;
  330. NbElt++;
  331. if (NbElt > MaxElt) MaxElt=NbElt;
  332. return i;
  333. }
  334. int isElt(int n)
  335. {
  336. int i;
  337. for (i=0; i<iT; i++) if (n==Tno[i]) return i;
  338. return -1;
  339. }
  340. void delIElt(int i)
  341. {
  342. if (i>=iT) return;
  343. if (T3) printf("T3: Del %d : %s \n",Tno[i],Turl[i]);
  344. iT--;
  345. if (iT == i) return;
  346. if (iT > 0) {
  347. Tno[i] = Tno[iT];
  348. Turl[i] = Turl[iT];
  349. Trv[i] = Trv[iT];
  350. }
  351. return;
  352. }
  353. void delElt(int n)
  354. {
  355. int i;
  356. for (i=0; i<iT; i++)
  357. if (n==Tno[i]) {
  358. delIElt(i);
  359. return;
  360. }
  361. }
  362. int markElt(int i, int v)
  363. {
  364. if (Trv[i] & v) return 0;
  365. Trv[i] |= v;
  366. return 1;
  367. }
  368. void listElt(char c)
  369. {
  370. int i,n=0;
  371. switch (c) {
  372. case '-':
  373. for (i=0; i<iT; i++)
  374. if (Tno[i]<0) { printf("%d : %s (%d)\n",Tno[i], Turl[i], Trv[i]);
  375. n++;
  376. }
  377. break;
  378. case '+':
  379. for (i=0; i<iT; i++)
  380. if (Tno[i]>0) { printf("%d : %s (%d)\n",Tno[i], Turl[i], Trv[i]);
  381. n++;
  382. }
  383. break;
  384. default:
  385. for (i=0; i<iT; i++) {
  386. printf("%d : %s (%d)\n",Tno[i], Turl[i], Trv[i]);
  387. n++;
  388. }
  389. break;
  390. }
  391. printf(" %d elements trouves.\n",n);
  392. }
  393. int comsh(char *com)
  394. {
  395. pid_t pid;
  396. int ret;
  397. if ((pid = fork()) < 0) {
  398. perror("fork2"); return 99;
  399. }
  400. if (pid == 0) {
  401. signal(SIGINT,SIG_IGN);
  402. execl("/bin/sh", "sh", "-c", com, (char *) 0);
  403. perror("execl2"); return 98;
  404. }
  405. waitpid(pid,&ret,0);
  406. return WEXITSTATUS(ret);
  407. }
  408. int exeCom(char * comm) /* on se reserve le droit de modifier */
  409. {
  410. char b[120];
  411. sprintf(b,"%s >/dev/null 2>&1",comm);
  412. return comsh(b);
  413. }
  414. /* ### fct de MAJ iptables */
  415. static char * IPT = "iptables";
  416. static char * IP6T = "ip6tables";
  417. static char * MYCH = "valide4";
  418. static char * OUTP = "OUTPUT";
  419. static char * MNO = "REJECT";
  420. static char * MOK = "ACCEPT";
  421. int initIPT(void)
  422. {
  423. int i=0;
  424. char b[90];
  425. if (REPR) return 0;
  426. sprintf(b,"%s -F",IPT);
  427. i += exeCom(b);
  428. sprintf(b,"%s -F",IP6T);
  429. i += exeCom(b);
  430. sprintf(b,"%s -L %s -n",IPT,MYCH);
  431. if (exeCom(b)) {
  432. sprintf(b,"%s -N %s",IPT,MYCH);
  433. i += exeCom(b);
  434. }
  435. sprintf(b,"%s -A %s -j %s",IPT,OUTP,MYCH);
  436. i += exeCom(b);
  437. return i;
  438. }
  439. int isPresentIP(char * comm, char * ip, char * chain)
  440. {
  441. char buf[100];
  442. sprintf(buf,"%s -L %s -n|grep %s",comm,chain,ip);
  443. if (exeCom(buf) == 0) return 1;
  444. return 0;
  445. }
  446. int retireChain(char * comm, char * ip, char * chain, char * jump)
  447. {
  448. char buf[100];
  449. sprintf(buf,"%s -D %s -d %s -j %s",comm,chain, ip, jump);
  450. return exeCom(buf);
  451. }
  452. int ajouteChain(char * comm, char * ip, char * chain, char * jump)
  453. {
  454. char buf[100];
  455. sprintf(buf,"%s -A %s -d %s -j %s",comm,chain, ip, jump);
  456. return exeCom(buf);
  457. }
  458. int bloqueIP(char* ip)
  459. {
  460. if (isAddrInCidr(ip,0)) return 0;
  461. if (isPresentIP(IPT,ip,OUTP)) return 0;
  462. return ajouteChain(IPT,ip,OUTP,MNO);
  463. }
  464. int debloqueIP(char* ip, char * url)
  465. {
  466. if (url != NULL) syslog(LOG_INFO,"%s=%s ACCEPT",url,ip);
  467. return ajouteChain(IPT,ip,MYCH,MOK);
  468. }
  469. int rebloqueIP(char* ip)
  470. {
  471. return retireChain(IPT,ip,MYCH,MOK);
  472. }
  473. void dropIP(char * l)
  474. {
  475. char *s,*d=l;
  476. while ((s=strstr(d, "A ")) != NULL) {
  477. s+=2;
  478. if ((d=strstr(s+2, ",")) == NULL) break;
  479. *d = '\0';
  480. d++;
  481. bloqueIP(s);
  482. }
  483. bloqueIP(s);
  484. }
  485. int verifIPOk(char * l, char * url)
  486. {
  487. char *s,*d=l;
  488. while ((s=strstr(d, "A ")) != NULL) {
  489. s+=2;
  490. if ((d=strstr(s+2, ",")) == NULL) break;
  491. *d = '\0';
  492. d++;
  493. if (isPresentIP(IPT,s,MYCH)) continue;
  494. if (isAddrInCidr(s,1)) debloqueIP(s,url);
  495. }
  496. if (isPresentIP(IPT,s,MYCH)) return 1;
  497. if (isAddrInCidr(s,1)) return(debloqueIP(s,url));
  498. return 1;
  499. }
  500. int dropIP6(char * l)
  501. {
  502. char *s,*d=l;
  503. while ((s=strstr(d, "A ")) != NULL) {
  504. s+=2;
  505. if ((d=strstr(s+2, ",")) == NULL) break;
  506. *d = '\0';
  507. d++;
  508. if (isPresentIP(IP6T,s,OUTP)) continue;
  509. ajouteChain(IP6T,s,OUTP,MNO);
  510. }
  511. if (isPresentIP(IP6T,s,OUTP)) return 1;
  512. ajouteChain(IP6T,s,OUTP,MNO);
  513. return 1;
  514. }
  515. /* tache de commande et periodiques */
  516. #define t0 (time_t)0
  517. time_t tim1=t0;
  518. void tachePer1(void) /* vide les elts toutes les 30 secondes */
  519. {
  520. static time_t tim0=t0, tw;
  521. int i, v;
  522. tw = time(NULL);
  523. if ((tw - tim0) < 30) {
  524. if (T3) printf ("T3: tache1 passe %s",ctime(&tw));
  525. return;
  526. }
  527. if (T3) printf ("T3: tache1 exec %s",ctime(&tw));
  528. tim1 = time(NULL);
  529. v = (tim1 - tim0) / 30;
  530. if (tim0 != t0) {
  531. for (i=iT-1; i>=0; i--) {
  532. if ((Trv[i]&0x6) == 6) delIElt(i); // IPv4 + IPV6
  533. else { Trv[i] += 8*v;
  534. if (Trv[i] > 80) delIElt(i); // On laisse 5 min.
  535. }
  536. }
  537. }
  538. tim0 = time(NULL);
  539. return;
  540. }
  541. void ajoutParam(char * ficp, char * param)
  542. {
  543. FILE * fw;
  544. fw = fopen(ficp,"a");
  545. fwrite(param,strlen(param),1,fw);
  546. fwrite("\n",1,1,fw);
  547. fclose(fw);
  548. }
  549. static int NBin=0, NBout=0;
  550. void prInOut(void)
  551. {
  552. printf(" %d messages DNS: %d requetes, %d reponses.\n",NBout+NBin,NBout,NBin);
  553. }
  554. int printQ(char * q)
  555. {
  556. char *rep=NULL;
  557. size_t lr = 0;
  558. int n;
  559. while (1) {
  560. printf("Voulez-vous %s ?\n Taper O (OUI) ou N (NON) :\n",q);
  561. if ((n = getline(&rep, &lr, stdin)) != 2) continue;
  562. if (*rep == 'O') return 1;
  563. if (*rep == 'N') return 0;
  564. }
  565. }
  566. void pr_encours(void)
  567. {
  568. printf(" ...\r"); fflush(stdout);
  569. }
  570. #define SUNIC "|sort|uniq"
  571. #define CHLOG "/var/log/user.log"
  572. #define CHLOGREP "/var/log/user.log|grep "
  573. #define CUT6 "|cut -d' ' -f6"
  574. #define CUT7S "|cut -d' ' -f7-"
  575. #define FHISTO ".octave_history"
  576. #define AWK5 "|awk '{ print $5}'"
  577. #define AWK4 "|awk '{ print $4}'"
  578. #define DREJ "^REJECT "
  579. void * fct_com(void * p)
  580. {
  581. int REQ=1;
  582. char *cmd = NULL, *fauth, com[100];
  583. int n2;
  584. pid_t pid;
  585. fauth = (char*)p;
  586. pid = getpid();
  587. read_history(FHISTO);
  588. if (REPR) comsh("reset");
  589. while (REQ) {
  590. if (kill(pid,SIGUSR1) < 0) { /* verif processus acquisition */
  591. ENDT=1;
  592. write(p1[1],"\n",1);
  593. }
  594. free(cmd);
  595. cmd = readline("O-> ");
  596. if ((n2 = strlen(cmd)) > 0) {
  597. write(p1[1],"\n",1);
  598. add_history(cmd);
  599. switch (*cmd) {
  600. case '+' :
  601. if (*(cmd+1) != '\0') {
  602. if (litligne(cmd+1)) { /* ajout au fichier fauth */
  603. if (debloqueIP(cmd+1,NULL)) printf("Element non valable !\n");
  604. else {
  605. if (printQ("ajouter au fichier parametres"))
  606. ajoutParam(fauth,cmd+1);
  607. listeAllow();
  608. }
  609. } else printf("Erreur ajout param. !\n");
  610. } else listeAllow();
  611. break;
  612. case '-' :
  613. if (*(cmd+1) != '\0') {
  614. if (litligne(cmd)) { /* ajout au fichier fauth */
  615. if (rebloqueIP(cmd+1)) printf("Element non valable !\n");
  616. else {
  617. if (printQ("ajouter au fichier parametres"))
  618. ajoutParam(fauth,cmd);
  619. listeDeny();
  620. }
  621. } else printf("Erreur ajout param. !\n");
  622. } else listeDeny();
  623. break;
  624. case 'l' :
  625. listElt(cmd[1]);
  626. printf(" %s Utilise %d elts/%d : %.2f%% (Max. %d)!\n",ctime(&tim1),iT,
  627. NBT, (float)(iT*100)/(float)NBT, MaxElt);
  628. prInOut();
  629. break;
  630. case 't' :
  631. if (*(cmd+1) != '\0') {
  632. if ((cmd[1] == '+') || (cmd[1] == '-')) {
  633. if ((cmd[1] == '+') && (Trace < TMAX)) Trace++;
  634. else {
  635. if ((cmd[1] == '-') && (Trace > TMIN)) Trace--;
  636. else printf("Erreur: niveau dans [%d, %d].\n",TMIN,TMAX);
  637. }
  638. } else printf("Erreur: Utiliser t+ ou t- !\n");
  639. }
  640. printf(" Trace niveau %d\n",Trace);
  641. break;
  642. case 'a' :
  643. if (*(cmd+1) != '\0') printf("ignore %s\n",cmd+1);
  644. sprintf(com,"grep '%s\\[%d\\]%s' %s%s%s",NPROG,pid,".* ok",
  645. CHLOG,CUT6,SUNIC);
  646. comsh(com);
  647. break;
  648. case 'i' :
  649. if (*(cmd+1) != '\0') printf("ignore %s\n",cmd+1);
  650. sprintf(com,"grep '%s\\[%d\\]%s' %s%s%s",NPROG,pid,".* DENY",
  651. CHLOG,CUT6,SUNIC);
  652. comsh(com);
  653. break;
  654. case 'e' :
  655. if (*(cmd+1) != '\0') printf("ignore %s\n",cmd+1);
  656. sprintf(com,"grep '%s\\[%d\\]%s' %s%s%s",NPROG,pid,".* ACCEPT",
  657. CHLOG,CUT6,SUNIC);
  658. comsh(com);
  659. break;
  660. case 'E' :
  661. if (*(cmd+1) != '\0') printf("ignore %s\n",cmd+1);
  662. sprintf(com,"grep '%s\\[%d\\]%s' %s",NPROG,pid,".*ERR: ",CHLOG);
  663. comsh(com);
  664. break;
  665. case 'L' :
  666. if (*(cmd+1) != '\0') printf("ignore %s\n",cmd+1);
  667. sprintf(com,"grep '%s\\[%d\\]' %s%s",NPROG,pid,
  668. CHLOGREP,"-v 'Re[pq]. '");
  669. comsh(com);
  670. break;
  671. case 'T' :
  672. if (*(cmd+1) != '\0')
  673. sprintf(com,"grep '%s\\[%d\\].*%s' %s%s",NPROG,pid,cmd+1,
  674. CHLOGREP," 'Re[pq]. '");
  675. else
  676. sprintf(com,"grep '%s\\[%d\\]' %s%s",NPROG,pid,
  677. CHLOGREP," 'Re[pq]. '");
  678. comsh(com);
  679. prInOut();
  680. break;
  681. case '>' :
  682. if (*(cmd+1) != '\0') printf("ignore %s\n",cmd+1);
  683. sprintf(com,"grep '%s\\[%d\\]' %s%s%s%s",NPROG,pid,
  684. CHLOGREP," 'Req. '",CUT7S,SUNIC);
  685. comsh(com);
  686. prInOut();
  687. break;
  688. case '<' :
  689. if (*(cmd+1) != '\0') printf("ignore %s\n",cmd+1);
  690. sprintf(com,"grep '%s\\[%d\\]' %s%s%s%s",NPROG,pid,
  691. CHLOGREP," 'Rep. '",CUT7S,SUNIC);
  692. comsh(com);
  693. prInOut();
  694. break;
  695. case 'r' :
  696. if (*(cmd+1) != '\0') printf("ignore %s\n",cmd+1);
  697. sprintf(com,"%s -L|grep %s%s%s",IPT,DREJ,AWK5,SUNIC);
  698. pr_encours();
  699. comsh(com);
  700. break;
  701. case 'R' :
  702. if (*(cmd+1) != '\0') printf("ignore %s\n",cmd+1);
  703. sprintf(com,"%s -L|grep %s%s%s",IP6T,DREJ,AWK4,SUNIC);
  704. pr_encours();
  705. comsh(com);
  706. break;
  707. case 'S' :
  708. if (*(cmd+1) != '\0') printf("ignore %s\n",cmd+1);
  709. RUN = 0;
  710. REQ = 0;
  711. write(p1[1],"\n",1);
  712. break;
  713. case 'V' :
  714. if (*(cmd+1) != '\0') printf("ignore %s\n",cmd+1);
  715. printf("Version %s\n",Version);
  716. prInOut();
  717. break;
  718. case ' ' :
  719. if (*(cmd+1) != '\0') comsh(cmd+1);
  720. break;
  721. default :
  722. printf("+url\t: Allow (autoriser une Url)\n");
  723. printf("-url\t: Deny (interdire une Url)\n");
  724. printf("a\t: Autorisations suivant analyse\n");
  725. printf("i\t: Interdictions suivant analyse\n");
  726. printf("e\t: Exceptions suivant analyse\n");
  727. printf("E\t: Liste des erreurs\n");
  728. printf("l[+|-]\t: Liste des elements dynamiques\n");
  729. printf("L\t: Logs du systeme\n");
  730. printf("r\t: Rejets actifs IPv4 (dure plusieurs sec.)\n");
  731. printf("R\t: Rejets actifs IPv6 (dure plusieurs sec.)\n");
  732. printf("S\t: Stopper\n");
  733. printf("t+|-\t: Niveau de trace : 0 (off) => 3\n");
  734. printf("T[mot]\t: Traces des demandes/reponses contenant mot\n");
  735. printf(">\t: Traces des demandes triees\n");
  736. printf("<\t: Traces des reponses triees\n");
  737. printf("V\t: Version\n");
  738. break;
  739. }
  740. }
  741. }
  742. write_history(FHISTO);
  743. free(cmd);
  744. /* fin dialogue */
  745. pthread_exit(NULL);
  746. }
  747. void getIface(void)
  748. {
  749. FILE * fd;
  750. char *line = NULL, *s, *w;
  751. size_t ll = 0;
  752. int n;
  753. if ((fd = fopen("/proc/net/route","r")) == NULL) {
  754. perror("route"); return;
  755. }
  756. while ((n = getline(&line, &ll, fd)) > 0) {
  757. if ((s=strstr(line,"00000000"))==NULL) continue;
  758. w=line;
  759. while ((*w != ' ') && (*w != '\t')) w++;
  760. *w = '\0';
  761. w++;
  762. while ((*w == ' ') || (*w == '\t')) w++;
  763. if (s==w) { /* ok */
  764. IFACE = (char*)malloc(strlen(line)+1);
  765. strcpy(IFACE,line);
  766. break;
  767. }
  768. }
  769. free(line);
  770. fclose(fd);
  771. }
  772. #define Vie (ie >= 0)
  773. int main(int N, char * P[])
  774. {
  775. pthread_t thid;
  776. FILE * fp;
  777. char *analyse="tcpdump", *line = NULL, *cmd = NULL, *s1, *s2, *refU;
  778. char *fauth = "auth1.txt", *strR = "-R", *Pars, strPID[8], **NP;
  779. size_t ll = 0, lc = 0;
  780. ssize_t n,n2;
  781. int Inter=0, LogC=0, i, ie, np=0, opt;
  782. if ((NPROG = strrchr(P[0],(int)'/')) == NULL) NPROG=P[0];
  783. else NPROG++;
  784. sprintf(strPID,"%d",getpid());
  785. /* verif. options */
  786. while ((opt = getopt(N, P, "ilp:R:t")) != -1) {
  787. switch (opt) {
  788. case 'i':
  789. Inter = 1;
  790. break;
  791. case 'l':
  792. LogC = 1;
  793. break;
  794. case 't':
  795. Trace = TMIN+1;
  796. break;
  797. case 'p':
  798. fauth = optarg;
  799. break;
  800. case 'R':
  801. REPR=1;
  802. np = atoi(optarg);
  803. break;
  804. default: /* '?' */
  805. fprintf(stderr, "Utilisation: %s [options]\nAvec les options :\n", NPROG);
  806. fprintf(stderr, "\t-i : mode interactif,\n");
  807. fprintf(stderr, "\t-l : log des requetes,\n");
  808. fprintf(stderr, "\t-p fichier : nom du fichier parametres (%s par defaut),\n",fauth);
  809. fprintf(stderr, "\t-t : avec trace.\n");
  810. return 1;
  811. }
  812. }
  813. if ((REPR) && (np != getpid())) {
  814. fprintf(stderr,"Erreur reprise %d\n", np);
  815. return 1;
  816. }
  817. if (optind < N) {
  818. fprintf(stderr,"Parametre inconnu : %s\n", P[optind]);
  819. return 1;
  820. }
  821. getIface();
  822. if (REPR) {
  823. while (IFACE==NULL) { sleep(1); getIface(); }
  824. } else {
  825. if (IFACE == NULL) {
  826. fprintf(stderr,"Interface reseau absente !\n");
  827. return 9;
  828. }
  829. }
  830. printf("%s %s sur %s\n", NPROG, Version, IFACE);
  831. /* verif privilege root */
  832. if ((getuid() > 0) && (geteuid() > 0)) {
  833. fprintf(stderr,"A executer sous root !\n");
  834. return 2;
  835. }
  836. if (T1) printf("T1: Fichier parametres = %s\n",fauth);
  837. signal(SIGUSR1,SIG_IGN);
  838. if (pipe(p1) < 0) {
  839. perror("pipe"); return 3;
  840. }
  841. openlog(NULL,LOG_PID,0);
  842. /* on lance le fils : */
  843. if ((pid = fork()) < 0) {
  844. perror("fork"); return 4;
  845. }
  846. if (pid == 0) {
  847. signal(SIGINT,SIG_IGN);
  848. close(0);
  849. close(p1[0]);
  850. dup2(p1[1],1); /* stdout dans p1 */
  851. dup2(p1[1],2); /* idem stderr */
  852. setsid();
  853. execlp(analyse,analyse,"-tn","-i",IFACE,"port","53",NULL);
  854. perror("execl");
  855. return 5;
  856. }
  857. if (Inter) signal(SIGINT,SIG_IGN);
  858. else signal(SIGINT,interup);
  859. if ((np=initIPT())!=0) {
  860. if (T1) printf("Erreur initIPT %d !!??\n",np);
  861. syslog(LOG_WARNING, "ERR: Erreur initIPT %d !!??\n",np);
  862. }
  863. /* lecture des listes */
  864. lectliste(fauth);
  865. if (T1) recaplistes();
  866. sleep(1); /* attend le fils en place */
  867. if (kill(pid,SIGUSR1) < 0) return 6;
  868. signal(SIGCHLD,interup);
  869. /*
  870. fcntl(p1[0], F_SETFL, O_NONBLOCK);
  871. flag0 = fcntl(0, F_GETFL, O_NONBLOCK);
  872. fcntl(0, F_SETFL, O_NONBLOCK);
  873. */
  874. /* on analyse la sortie de p1 */
  875. if ((fp = fdopen(p1[0],"r")) == NULL) {
  876. perror("fdopen"); return 7;
  877. }
  878. fcntl(p1[0], F_SETPIPE_SZ,1048576);
  879. if (T1) printf("Depart %s %s PIDF:%d !\n",NPROG, strPID,pid);
  880. if (T1) printf("Capacite pipe : %ld bytes\n", (long)fcntl(p1[0], F_GETPIPE_SZ));
  881. np=0;
  882. /* lancement du thread */
  883. if (Inter) {
  884. if (pthread_create(&thid,NULL,fct_com,(void*)fauth) != 0) {
  885. fprintf(stderr,"Erreur pthread_create !\n"); return 9;
  886. }
  887. }
  888. while (RUN) {
  889. tachePer1();
  890. if ((n = getline(&line, &ll, fp)) > 0) {
  891. if (ENDT) {
  892. printf("Erreur : plus de tache d'analyse !\n"); break;
  893. }
  894. if (RUN == 0) break;
  895. if ((n==1) && (*line=='\n')) continue;
  896. if (np==0) { np++;
  897. if (REPR) syslog(LOG_INFO,"Reprise de l'analyse !");
  898. else syslog(LOG_INFO,"Debut de l'analyse !");
  899. }
  900. /* analyse */
  901. if ((s1=strstr(line, " > ")) == NULL) continue;
  902. if (strncmp(s1-3,".53",3) == 0) { /* REPONSE */
  903. if ((s2=strstr(s1+3, ":")) == NULL) continue;
  904. NBin++;
  905. *s2 = '\0';
  906. s1 = s2 -1;
  907. while (*s1 != '.') s1--;
  908. np = atoi(s1+1);
  909. if ((ie = isElt(np)) == -1) { /* Elt OK ou ABSENT ! */
  910. ie = isElt(-np);
  911. s1 = s2+1;
  912. if ((s2=strstr(s1, " A ")) != NULL) { /* IPv4 */
  913. s2++;
  914. s1 = strrchr(s2,(int)' ');
  915. *s1 = '\0';
  916. if (LogC) syslog(LOG_INFO,"Rep. %d %s",np,s2);
  917. if Vie {
  918. markElt(ie,4); refU = Turl[ie];
  919. } else {
  920. if (T1) printf("Elt %d non trouve !!??\n",np);
  921. syslog(LOG_WARNING,"ERR: Elt %d non trouve !!??\n",np);
  922. refU = NULL;
  923. }
  924. if (!verifIPOk(s2, refU))
  925. if Vie syslog(LOG_INFO,"Deblocage IP4 %s",refU);
  926. } else {
  927. if ((s2=strstr(s1, " AAAA ")) != NULL) { /* IPv6 */
  928. s2++;
  929. s1 = strrchr(s2,(int)' ');
  930. *s1 = '\0';
  931. if (LogC) syslog(LOG_INFO,"Rep. %d %s",np,s2);
  932. if Vie markElt(ie,2);
  933. dropIP6(s2);
  934. } else {
  935. if Vie markElt(ie,1);
  936. }
  937. }
  938. continue;
  939. }
  940. s1 = s2+1;
  941. if ((s2=strstr(s1, " A ")) == NULL) {
  942. if ((s2=strstr(s1, " AAAA ")) == NULL) {
  943. markElt(ie,1);
  944. } else { /* traitement IPv6 */
  945. s2++;
  946. if (LogC) syslog(LOG_INFO,"Rep. %d %s",np,s2);
  947. s1 = strrchr(s2,(int)' ');
  948. *s1 = '\0';
  949. if (markElt(ie,2)) dropIP6(s2);
  950. }
  951. continue;
  952. }
  953. /* IPv4 REJECT */
  954. s2++;
  955. s1 = strrchr(s2,(int)' ');
  956. *s1 = '\0';
  957. if (LogC) syslog(LOG_INFO,"Rep. %d %s",np,s2);
  958. syslog(LOG_INFO,"%s DENY",Turl[ie]);
  959. if (markElt(ie,4)) dropIP(s2);
  960. } else { /* DEMANDE */
  961. NBout++;
  962. *s1 = '\0';
  963. s2 = s1 +1;
  964. while (*s1 != '.') s1--;
  965. np = atoi(s1+1);
  966. if ((s1=strstr(s2, " A? ")) == NULL) continue;
  967. s1 += 4;
  968. s2 = s1 +1;
  969. while (*s2 != ' ') s2++;
  970. *(s2-1) = '\0'; /* on supprime le '.' */
  971. if (LogC) syslog(LOG_INFO,"Req. %d %s",np,s1);
  972. if (strstr(s1, ".") == NULL) { /* il doit en rester 1 */
  973. if (T1) printf("Ignore : %d %s !\n",np,s1);
  974. syslog(LOG_WARNING,"ERR: Ignore %d %s !\n",np,s1);
  975. continue;
  976. }
  977. if (!isDeny(s1)) { // V2 ! On enregistre le OK en NEGATIF
  978. if (isElt(-np) < 0) {
  979. addElt(-np,s1);
  980. syslog(LOG_INFO,"%s ok",s1);
  981. }
  982. continue;
  983. }
  984. if (isElt(np) < 0) {
  985. i=addElt(np,s1);
  986. if (T3) printf("T3: addElt %d %d/%d\n",NbElt,i,NBT);
  987. }
  988. }
  989. }
  990. if (REQ) {
  991. printf("Taper votre commande : H pour help !\n");
  992. if ((n2 = getline(&cmd, &lc, stdin)) > 0) {
  993. switch (*cmd) {
  994. case 'C' :
  995. REQ = 0;
  996. break;
  997. case 'L' :
  998. listElt(cmd[1]);
  999. printf(" %s Utilise %d elts/%d : %.2f%% (Max. %d)!\n",
  1000. ctime(&tim1),iT,NBT,(float)(iT*100)/(float)NBT,MaxElt);
  1001. break;
  1002. case 'S' :
  1003. RUN = 0;
  1004. REQ = 0;
  1005. break;
  1006. default :
  1007. printf("C\t: continuer\n");
  1008. printf("L\t: liste des elts\n");
  1009. printf("S\t: stopper\n");
  1010. break;
  1011. }
  1012. }
  1013. }
  1014. }
  1015. syslog(LOG_INFO,"Fin de l'analyse !");
  1016. free(line);
  1017. free(cmd);
  1018. kill(pid,SIGTERM);
  1019. close(p1[0]);
  1020. close(p1[1]);
  1021. closelog();
  1022. if (ENDT) { /* relance auto */
  1023. comsh("reset");
  1024. if (T1) printf("Relance auto %s dans %d sec. ...\n",strPID, DELAYR);
  1025. sleep(DELAYR); /* attend N s */
  1026. NP = (char**)malloc((sizeof(Pars))*(N+3));
  1027. for (i=0;i<N;i++) NP[i] = P[i];
  1028. NP[i++]=strR;
  1029. NP[i++]=strPID;
  1030. NP[i]=NULL;
  1031. execv(P[0],NP);
  1032. perror("execv");
  1033. }
  1034. printf("Fin du programme!\n");
  1035. return 0;
  1036. }