pcrypt.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /*
  2. * pcrypt.c -- handles:
  3. * psybnc crypt and lfprintf functions.
  4. *
  5. */
  6. #include "main.h"
  7. #include "salt.h"
  8. unsigned char *hashdot(unsigned int r);
  9. unsigned int unhashdot(unsigned char *hash);
  10. char crybu[2000];
  11. char *psycrypt(char *st)
  12. {
  13. char *pte;
  14. char *ptt;
  15. char *pts1,*pts2;
  16. char *pt;
  17. char *hpt;
  18. char hbuf[3];
  19. int res;
  20. int slen=0;
  21. unsigned int tslt1 = CODE1;
  22. unsigned int tslt2 = CODE2;
  23. int p1,p2,p3,p4,p5;
  24. int erg;
  25. int de=0;
  26. memset(crybu,0x0,sizeof(crybu));
  27. pt = crybu;
  28. pte = pt;
  29. ptt = st;
  30. if (*ptt=='+') {
  31. ptt++;
  32. de=1;
  33. } else {
  34. *pte++='+';
  35. }
  36. pts1 = slt1 +SA1;
  37. pts2 = slt2 +SA2;
  38. while(*ptt!=0)
  39. {
  40. if (slen>1990) break;
  41. if (tslt1>255 || tslt1 <0) tslt1=CODE1;
  42. if (tslt2>255 || tslt2 <0) tslt2=CODE2;
  43. if (*pts1==0) pts1=slt1;
  44. if (*pts2==0) pts2=slt2;
  45. res=0;
  46. if (de) {
  47. hbuf[0]=*ptt++;
  48. hbuf[1]=*ptt;
  49. hbuf[2]=0;
  50. p1=unhashdot(hbuf);
  51. p2=*pts1;p3=tslt1;p4=*pts2;p5=tslt2;
  52. erg=p1-p2-p3+p4-p5;
  53. *pte=erg;
  54. res=erg;
  55. } else {
  56. p1=*ptt;p2=*pts1;p3=tslt1;p4=*pts2;p5=tslt2;
  57. res=p1;
  58. erg=p1+p2+p3-p4+p5;
  59. hpt=hashdot(erg);
  60. *pte++=hpt[0];slen++;
  61. *pte=hpt[1];
  62. }
  63. tslt1--;
  64. res=res/10;
  65. tslt2=tslt2+res;
  66. pte++;ptt++;pts1++;pts2++;slen=slen+1;
  67. }
  68. *pte=0;
  69. return pt;
  70. }
  71. char *cryptit(char *tocipher)
  72. {
  73. if (*tocipher=='+')
  74. return tocipher;
  75. else
  76. return psycrypt(tocipher);
  77. }
  78. char *decryptit(char *todecipher)
  79. {
  80. if (todecipher[0]=='+')
  81. return psycrypt(todecipher);
  82. else
  83. return todecipher;
  84. }
  85. /* hashing routines for string driven systems */
  86. unsigned char base[]="'`0123456789abcdefghijklmnopqrstuvxyzABCDEFGHIJKLMNOPQRSTUVWXYZ@$=&*-#";
  87. int baselen=67;
  88. unsigned char xres[3];
  89. unsigned char *hashdot(unsigned int r)
  90. {
  91. unsigned int cnt;
  92. unsigned int hh=0;
  93. unsigned int hl=0;
  94. cnt=r;
  95. for(;cnt>0;cnt--)
  96. {
  97. hl++;
  98. if (hl==baselen) {hl=0;hh++;}
  99. }
  100. xres[0]=base[hh];
  101. xres[1]=base[hl];
  102. xres[2]=0;
  103. return xres;
  104. }
  105. int wrong=0;
  106. unsigned int unhashdot(unsigned char *hash)
  107. {
  108. unsigned int lf=baselen;
  109. /* unsigned char *pt; */
  110. unsigned int erg=0;
  111. unsigned long ln=0;
  112. wrong=0;
  113. while (ln<baselen && base[ln] != hash[0]) {
  114. ln++;
  115. }
  116. if (ln!=baselen) {
  117. erg=ln * lf;
  118. } else {
  119. wrong=1;
  120. }
  121. ln=0;
  122. while (ln<baselen && base[ln] != hash[1]) {
  123. ln++;
  124. }
  125. if (ln!=baselen) {
  126. erg=erg+ln;
  127. } else {
  128. wrong=1;
  129. }
  130. return erg;
  131. }
  132. /* end pcrypt */
  133. extern char netpass[];
  134. int lfprintf(FILE *f, char *fmt, ...) {
  135. va_list va;
  136. char outbuf[8192];
  137. char *tptr, *tptr2, *temps1, *temps2;
  138. va_start(va, fmt);
  139. vsnprintf(outbuf, sizeof(outbuf), fmt, va);
  140. tptr2 = outbuf;
  141. if(strchr(outbuf, '\n')) {
  142. while( (tptr = strchr(outbuf, '\n')) ) {
  143. *tptr = 0;
  144. Context;
  145. temps1 = (char *) encrypt_string(netpass, tptr2);
  146. Context;
  147. if (fprintf(f, "%s\n", cryptit(temps1)) == EOF) {
  148. nfree(temps1);
  149. return -1;
  150. }
  151. nfree(temps1);
  152. tptr++;
  153. tptr2 = tptr;
  154. }
  155. } else {
  156. temps2 = (char *) encrypt_string(netpass, outbuf);
  157. fprintf(f, "%s", cryptit(temps2));
  158. nfree(temps2);
  159. return -1;
  160. }
  161. va_end(va);
  162. return 0;
  163. }