Code afférent au projet Kouglof 2 de l'E2L
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

3 недель назад
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  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. /* lich.c : listes chainees pour creer les elements (url, ip, ip6 ) */
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17. #include <string.h>
  18. #include <pthread.h>
  19. #include "lich.h"
  20. /* variables globales */
  21. static long LC_SIZE = (long)0;
  22. static long LC_SIZEL = (long)0;
  23. static struct elt * First=FIN; /* depart du chainage */
  24. static pthread_mutex_t mut_Elt = PTHREAD_MUTEX_INITIALIZER;
  25. long getLC_SIZE(void) { return LC_SIZE; }
  26. long getLC_SIZEL(void) { return LC_SIZEL; }
  27. struct elt ** getFirst(void) { return &First; }
  28. pthread_mutex_t * getMutElt(void) { return &mut_Elt; }
  29. struct elt * ajouteElt(char * n, int s)
  30. {
  31. void * M;
  32. struct elt *e, **D;
  33. long sizeMore;
  34. int i,c;
  35. /* creation de l'espace memoire de l'element */
  36. if ((M = malloc(sizeof(struct elt))) == NULL) {
  37. perror(Y(p00)); return FIN;
  38. }
  39. sizeMore = sizeof(struct elt);
  40. e = (struct elt *)M;
  41. /* creation de l'espace memoire de l'url */
  42. if ((M = malloc(strlen(n)+1)) == NULL) {
  43. perror(Y(p00)); return FIN;
  44. }
  45. e->url = (char *)M;
  46. sizeMore += strlen(n)+1;
  47. /* remplissage de l'element */
  48. strcpy(e->url,n);
  49. e->stat = s;
  50. e->ip = VIDE;
  51. e->ip6 = VIDE;
  52. e->next = FIN;
  53. for (i=0;i<ELT_NBN;i++) e->no[i] = 0;
  54. /* boucle sur l'adresse du suivant */
  55. D=&First;
  56. pthread_mutex_lock(&mut_Elt);
  57. while (*D != FIN) {
  58. if ((c=strcmp((*D)->url,n)) > 0) { /* insertion avant */
  59. e->next = *D;
  60. break;
  61. }
  62. if (c == 0) { /* deja present */
  63. pthread_mutex_unlock(&mut_Elt);
  64. free(M);
  65. free((void*)e);
  66. return *D;
  67. }
  68. D = &((*D)->next);
  69. }
  70. *D = e;
  71. LC_SIZE += sizeMore;
  72. pthread_mutex_unlock(&mut_Elt);
  73. return e;
  74. }
  75. static void ajouteLib(struct lib **Lib, char *l)
  76. {
  77. void * M;
  78. struct lib *e,**D;
  79. long sizeMore;
  80. int c;
  81. /* creation de l'espace memoire du libelle */
  82. if ((M = malloc(sizeof(struct lib))) == NULL) {
  83. perror(Y(p00)); return;
  84. }
  85. sizeMore = sizeof(struct lib);
  86. e = (struct lib *)M;
  87. /* creation de l'espace memoire du libelle */
  88. if ((M = malloc(strlen(l)+1)) == NULL) {
  89. perror(Y(p00)); return;
  90. }
  91. sizeMore += strlen(l)+1;
  92. e->lib = (char *)M;
  93. /* remplissage de l'element */
  94. strcpy(e->lib,l);
  95. e->next = VIDE;
  96. D=Lib;
  97. pthread_mutex_lock(&mut_Elt);
  98. while (*D != VIDE) {
  99. if ((c=strcmp((*D)->lib,l)) > 0) { /* insertion avant */
  100. e->next = *D;
  101. break;
  102. }
  103. if (c == 0) { /* deja present */
  104. pthread_mutex_unlock(&mut_Elt);
  105. free(M);
  106. free((void*)e);
  107. return;
  108. }
  109. D = &((*D)->next);
  110. }
  111. *D = e;
  112. LC_SIZE += sizeMore;
  113. pthread_mutex_unlock(&mut_Elt);
  114. }
  115. static void ajouteLibD(struct libd **Lib, char *l, char * l2) /*Libs:l2=NULL*/
  116. {
  117. void * M, *M2;
  118. struct libd *e,**D;
  119. long sizeMore;
  120. int c;
  121. /* creation de l'espace memoire du libelle */
  122. if ((M = malloc(sizeof(struct libd))) == NULL) {
  123. perror(Y(p00)); return;
  124. }
  125. sizeMore = sizeof(struct libd);
  126. e = (struct libd *)M;
  127. /* creation de l'espace memoire du libelle */
  128. if ((M = malloc(strlen(l)+1)) == NULL) {
  129. perror(Y(p00)); return;
  130. }
  131. sizeMore += strlen(l)+1;
  132. e->lib = (char *)M;
  133. if (l2 == NULL) e->lib2 = NULL;
  134. else {
  135. if ((M2 = malloc(strlen(l2)+1)) == NULL) {
  136. perror(Y(p00)); return;
  137. }
  138. sizeMore += strlen(l2)+1;
  139. e->lib2 = (char *)M2;
  140. strcpy(e->lib2,l2);
  141. }
  142. /* remplissage de l'element */
  143. strcpy(e->lib,l);
  144. e->next = VIDD;
  145. D=Lib;
  146. pthread_mutex_lock(&mut_Elt);
  147. while (*D != VIDD) {
  148. if ((c=strcmp((*D)->lib,l)) > 0) { /* insertion avant */
  149. e->next = *D;
  150. break;
  151. }
  152. if (c == 0) { /* deja present */
  153. pthread_mutex_unlock(&mut_Elt);
  154. free(M);
  155. if (l2 != NULL) free(M2);
  156. free((void*)e);
  157. return;
  158. }
  159. D = &((*D)->next);
  160. }
  161. *D = e;
  162. if (l2==NULL) LC_SIZE += sizeMore;
  163. else LC_SIZEL += sizeMore;
  164. pthread_mutex_unlock(&mut_Elt);
  165. }
  166. void ajouteIp(struct elt *E, char *ip, int v6)
  167. {
  168. if (v6) ajouteLib(&(E->ip6), ip);
  169. else ajouteLib(&(E->ip), ip);
  170. }
  171. struct elt * getNum(int n)
  172. {
  173. struct elt **D;
  174. int i;
  175. D=&First;
  176. while (*D != FIN) {
  177. for (i=0; i< ELT_NBN; i++) if ((*D)->no[i] == n) return *D;
  178. D = &((*D)->next);
  179. }
  180. return FIN; /* pas trouve ! */
  181. }
  182. /* Listes Cidr, Cidr6, Allow et Deny */
  183. static struct libd *Libs = VIDD;
  184. static struct libd *Cidr = VIDD;
  185. static struct libd *Cidr6 = VIDD;
  186. static struct lib *Allow = VIDE;
  187. static struct lib *Deny = VIDE;
  188. void ajouteLibs(char * e, char * l) { ajouteLibD(&Libs, e, l); }
  189. void ajouteCidr(char * u) { ajouteLibD(&Cidr,u,NULL); }
  190. void ajouteCidr6(char * u) { ajouteLibD(&Cidr6,u,NULL); }
  191. void ajouteAllow(char * u) { ajouteLib(&Allow,u); }
  192. void ajouteDeny(char * u) { ajouteLib(&Deny,u); }
  193. int estSousDomaine(char * u, char * d)
  194. {
  195. int t, tu;
  196. char *su;
  197. tu = strlen(u);
  198. t = strlen(d);
  199. if (tu < t) return 0;
  200. su = u + tu - t;
  201. if (strcmp(su,d) == 0) {
  202. if (*d == '.') return 1;
  203. if (su==u) return 1;
  204. if (*(su-1)=='.') return 1;
  205. }
  206. return 0;
  207. }
  208. static int addLib2(struct libd **L, char * u, char *l)
  209. {
  210. struct libd **D;
  211. void *M;
  212. int ret=0;
  213. D=L;
  214. pthread_mutex_lock(&mut_Elt);
  215. while (*D != VIDD) {
  216. if (strcmp(u,(*D)->lib)==0) { ret = 1; break; }
  217. D = &((*D)->next);
  218. }
  219. if (ret) {
  220. if ((*D)->lib2 != NULL) free((*D)->lib2);
  221. /* creation de l'espace memoire de lib2 */
  222. if ((M = malloc(strlen(l)+1)) == NULL) perror(Y(p00));
  223. else { /* remplissage de l'element */
  224. (*D)->lib2 = (char *)M;
  225. LC_SIZE += strlen(l)+1;
  226. strcpy((*D)->lib2,l);
  227. }
  228. }
  229. pthread_mutex_unlock(&mut_Elt);
  230. return ret;
  231. }
  232. int addNameCidr(char * u, char *l) { return addLib2(&Cidr,u,l); }
  233. int addNameCidr6(char * u, char *l) { return addLib2(&Cidr6,u,l); }
  234. static char * getEntErr(char c)
  235. {
  236. static char E[8];
  237. E[3]='s';
  238. E[1]=c;
  239. E[2]='%';
  240. *E='?';
  241. E[4]='\0';
  242. return E;
  243. }
  244. static char * getLib2(struct libd **L, char * u, int Lock)
  245. {
  246. struct libd **D;
  247. int ret=0;
  248. D=L;
  249. if (Lock) pthread_mutex_lock(&mut_Elt);
  250. while (*D != VIDD) {
  251. if (strcmp(u,(*D)->lib)==0) { ret = 1; break; }
  252. D = &((*D)->next);
  253. }
  254. if (Lock) pthread_mutex_unlock(&mut_Elt);
  255. if (ret) return (*D)->lib2;
  256. if (Lock == 0) { /* pour l'instant seult libs */
  257. fprintf(stderr,getEntErr('L'),u);
  258. exit(129);
  259. }
  260. return NULL;
  261. }
  262. char * getNameCidr(char * u) { return getLib2(&Cidr,u,1); }
  263. char * getNameCidr6(char * u) { return getLib2(&Cidr6,u,1); }
  264. char * getLibelle(char * e) { return getLib2(&Libs,e,0); }
  265. static int estDans(struct lib **L, char * u)
  266. {
  267. struct lib **D;
  268. int ret=0;
  269. D=L;
  270. pthread_mutex_lock(&mut_Elt);
  271. while (*D != VIDE) {
  272. if (estSousDomaine(u,(*D)->lib)) { ret = 1; break; }
  273. D = &((*D)->next);
  274. }
  275. pthread_mutex_unlock(&mut_Elt);
  276. return ret;
  277. }
  278. int inAllow(char * u) { return estDans(&Allow,u); }
  279. int inDeny(char * u) { return estDans(&Deny,u); }
  280. void listeLib(struct lib **L)
  281. {
  282. struct lib **D;
  283. D=L;
  284. while (*D != VIDE) {
  285. printf(Y(f49),(*D)->lib);
  286. D = &((*D)->next);
  287. }
  288. }
  289. void listeLibD(struct libd **L)
  290. {
  291. struct libd **D;
  292. int i=0;
  293. D=L;
  294. while (*D != VIDD) {
  295. printf(Y(f50),(*D)->lib,(*D)->lib2);
  296. i++;
  297. D = &((*D)->next);
  298. }
  299. printf(Y(f65),i);
  300. }
  301. static void listeLibD2(struct libd **L, char *s)
  302. {
  303. struct libd **D;
  304. int i=0;
  305. char *d;
  306. D=L;
  307. while (*D != VIDD) {
  308. if (*s != '\0') {
  309. if (strstr((*D)->lib,s) == NULL) {
  310. if (strstr((*D)->lib2,s) == NULL) {
  311. D = &((*D)->next); continue;
  312. }
  313. }
  314. }
  315. printf(Y(f66),(*D)->lib);
  316. d = (*D)->lib2;
  317. printf("\"");
  318. while (*d != '\0') {
  319. if (*d == '\n') printf("\\n");
  320. else {
  321. if (*d == '\r') printf("\\r");
  322. else {
  323. if (*d == '\t') printf("\\t");
  324. else {
  325. if (*d == '\e') printf("\\e");
  326. else {
  327. if (*d == '\\') printf("\\");
  328. printf("%c",*d);
  329. }
  330. }
  331. }
  332. }
  333. d++;
  334. }
  335. printf("\"\n");
  336. i++;
  337. D = &((*D)->next);
  338. }
  339. printf(Y(f65),i);
  340. }
  341. void listeAllow(void) { printf(Y(i13)); listeLib(&Allow); }
  342. void listeDeny(void) { printf(Y(i14)); listeLib(&Deny); }
  343. void listeLibs(char *s) { listeLibD2(&Libs,s); }