repcheck.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /** BEGIN COPYRIGHT BLOCK
  2. * Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
  3. * Copyright (C) 2006 Red Hat, Inc.
  4. * All rights reserved.
  5. *
  6. * License: GPL (version 3 or any later version).
  7. * See LICENSE for details.
  8. * END COPYRIGHT BLOCK **/
  9. #ifdef HAVE_CONFIG_H
  10. # include <config.h>
  11. #endif
  12. #include <stdio.h>
  13. #include <time.h>
  14. #include <string.h>
  15. #include <signal.h>
  16. #include "remote.h"
  17. #include "lber.h"
  18. #include "ldap.h"
  19. enum {ADD,DELETE,MODRDN,MODIFY,RESULT};
  20. typedef struct {
  21. int conn,op,type;
  22. char *dn;
  23. } Optype;
  24. Optype *pendops;
  25. int npend,maxop,connected;
  26. char *ldap_ops[]={"ADD","DEL","MODRDN","MOD ","RESULT",NULL};
  27. int ldap_val[]={LDAP_REQ_ADD,LDAP_REQ_DELETE,LDAP_REQ_MODRDN,LDAP_REQ_MODIFY};
  28. get_op_par(char *s,Optype *op)
  29. {
  30. char *t;
  31. int i;
  32. t=strstr(s,"conn=");
  33. for(t+=5,op->conn=0;isdigit(*t);t++)
  34. op->conn=op->conn*10+*t-'0';
  35. t=strstr(s,"op=");
  36. for(t+=3,op->op=0;isdigit(*t);t++)
  37. op->op=op->op*10+*t-'0';
  38. if(t=strstr(s,"dn="))
  39. op->dn=strdup(t+3);
  40. }
  41. send_op(char* s,int sfd)
  42. {
  43. int sz,i;
  44. Optype tmp;
  45. repconfirm *result;
  46. char *t;
  47. get_op_par(s,&tmp);
  48. for(i=0;i<maxop;i++)
  49. if(pendops[i].op==tmp.op && pendops[i].conn==tmp.conn){
  50. t=strstr(s,"err=");
  51. sz=strlen(pendops[i].dn);
  52. result=(repconfirm*)malloc(sizeof(repconfirm)+sz);
  53. for(t+=4,result->res=0;isdigit(*t);t++)
  54. result->res=result->res*10+*t-'0';
  55. result->type=htonl(ldap_val[pendops[i].type]);
  56. strcpy(result->dn,pendops[i].dn);
  57. result->dnSize=htonl(sz);
  58. result->res=htonl(result->res);
  59. if(write(sfd,result,sizeof(repconfirm)+sz)<=0){
  60. close(sfd);
  61. memset(pendops,0,maxop*sizeof(Optype));
  62. maxop=npend=connected=0;
  63. return;
  64. }
  65. if(i!=maxop)
  66. pendops[i]=pendops[maxop];
  67. else memset(pendops+i,0,sizeof(Optype));
  68. return;
  69. }
  70. }
  71. main(int argc, char**argv)
  72. {
  73. int i,port=16000;
  74. int sockfd;
  75. static char logline[512];
  76. char **tmp;
  77. struct hostent *serveraddr;
  78. struct sockaddr_in srvsaddr;
  79. char *p;
  80. while((i=getopt(argc,argv,"p:"))!=EOF){
  81. switch(i){
  82. case 'p': port=atoi(optarg);
  83. break;
  84. }
  85. }
  86. serveraddr=gethostbyname(argv[optind]);
  87. srvsaddr.sin_addr.s_addr=htonl(*((u_long*)(serveraddr->h_addr_list[0])));
  88. srvsaddr.sin_family=AF_INET;
  89. srvsaddr.sin_port=htons(port);
  90. maxop=npend=0;
  91. pendops=(Optype*)malloc(sizeof(Optype)*20);
  92. sigset(SIGPIPE,SIG_IGN);
  93. while(fgets(logline, sizeof(logline), stdin)){
  94. if (p = strchr(logline, '\n')) {
  95. *p = 0;
  96. }
  97. if(!connected){
  98. if((sockfd=socket(AF_INET,SOCK_STREAM,0))==-1){
  99. perror(argv[0]);
  100. exit(1);
  101. }
  102. i=1;
  103. if(setsockopt(sockfd,IPPROTO_TCP, TCP_NODELAY,&i,sizeof(int))!=0)
  104. perror("Nagle");
  105. if(connect(sockfd,(struct sockaddr*)&srvsaddr,sizeof(struct sockaddr))!=-1)
  106. connected=1;
  107. else {
  108. close(sockfd);
  109. continue;
  110. }
  111. }
  112. for(tmp=ldap_ops,i=0;tmp[i];i++)
  113. if(strstr(logline,tmp[i]))
  114. break;
  115. if(i<RESULT){
  116. get_op_par(logline,&pendops[maxop]);
  117. pendops[maxop].type=i;
  118. if(++maxop>npend)
  119. npend=maxop;
  120. if(!(npend%20)){
  121. pendops=(Optype*)realloc(pendops,sizeof(Optype)*(npend+20));
  122. memset(pendops+npend,0,sizeof(Optype)*20);
  123. }
  124. }
  125. if(i==RESULT)
  126. send_op(logline,sockfd);
  127. }
  128. close(sockfd);
  129. }