Main Page | Modules | Namespace List | Class Hierarchy | Class List | File List | Class Members | File Members | Related Pages

mass.c

Go to the documentation of this file.
00001  /*************************************************************************
00002  * Mass Advertising detection functions.
00003  *
00004  *  Copyright (c) 2000 James Hess
00005  *  All rights reserved.
00006  *
00007  * Redistribution and use in source and binary forms, with or without
00008  * modification, are permitted provided that the following conditions
00009  * are met:
00010  * 1. Redistributions of source code must retain the above copyright
00011  *    notice, this list of conditions and the following disclaimer.
00012  * 2. Redistributions in binary form must reproduce the above copyright
00013  *    notice, this list of conditions and the following disclaimer in the
00014  *    documentation and/or other materials provided with the distribution.
00015  * 3. Neither the name of the authors nor the names of its contributors
00016  *    may be used to endorse or promote products derived from this software
00017  *    without specific prior written permission.
00018  *
00019  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
00020  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00021  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00022  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
00023  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00024  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
00025  */
00038  /*  changes:
00039   *    [7/2/1998] Obsolete ->status stuff removed            
00040   *
00041   *
00042   */
00043 #include "services.h"
00044 #include "log.h"
00045 #include "nickserv.h"
00046 #include "mass.h"
00047 
00048 char *massAdClones[5] = {};
00049 void insert_ad(aMassAd * insert_this);
00050 const char s_MassBot[] = "MassServ";
00051 LIST_HEAD(, _mass_ad) masslist;
00052 
00059 void detect_mass(UserList *nl_from, char *buf, int which)
00060 {
00061     aMassAd *booya;
00062     aMassAd *f1, *f2, *f1o, *f2o;
00063 
00064     expire_ads();
00065 
00066     if (nl_from != 0) {
00067         f1o = f1 = find_ad(nl_from, 0);
00068         f2o = f2 = find_ad(nl_from, 1);
00069     } 
00070     else
00071         return;
00072 
00073     if (nl_from && ((!f1 && which == 0) || (!f2 && which == 1))) {
00074         booya = (aMassAd *) oalloc(sizeof(aMassAd));
00075         booya->sender = nl_from;
00076         booya->utc = time(NULL);
00077         booya->rcvd_by = which;
00078         insert_ad(booya);
00079         if (!booya->sender)
00080             logDump(corelog,
00081                     "[***] bug: booya->sender is NULL in detect_mass()");
00082     }
00083 
00084     if (f1 == NULL)
00085         f1 = find_ad(nl_from, 0);
00086     if (f2 == NULL)
00087         f2 = find_ad(nl_from, 1);
00088 
00089     if (f1 != NULL && f2 != NULL) {
00090         sSend(":%s GLOBOPS :\2Mass Ad:\2 (%s) %s",
00091               MassServ, nl_from->nick, buf);
00092         flush_ad(nl_from);
00093         // sSend(":%s SVSKILL %s :killed ([services] (Mass msg'ing))", source);
00094     } else if (!f1o && !f2o)
00095         sSend(":%s GLOBOPS :(Debug) \2Possible Mass Ad:\2 (%s) %s", MassServ,
00096               nl_from->nick, buf);
00097     return;
00098 }
00099 
00105 aMassAd *
00106 find_ad(UserList * u, int rcv)
00107 {
00108     aMassAd *search;
00109 
00110     if ((search = LIST_FIRST(&masslist)) == NULL)
00111         return NULL;
00112     while (search) {
00113         if (search->sender == u && (search->rcvd_by == rcv || rcv == -1))
00114             return search;
00115         search = LIST_NEXT(search, ma_lst);
00116     }
00117     return NULL;
00118 }
00119 
00123 void
00124 insert_ad(aMassAd * insert_this)
00125 {
00126     LIST_INSERT_HEAD(&masslist, insert_this, ma_lst);
00127 }
00128 
00133 void
00134 expire_ads(void)
00135 {
00136     aMassAd *iter, *iter_next;
00137     time_t present = time(NULL);
00138 
00139     iter = LIST_FIRST(&masslist);
00140     if (iter == NULL)
00141         return;
00142 
00143     while (iter) {
00144         iter_next = LIST_NEXT(iter, ma_lst);
00145 
00146         if ((present - iter->utc) > (15 * 60)) {
00147             //       wallops(NULL, "forgetting about ad from %s", iter->sender->nick);
00148             remove_ad(iter);
00149         }
00150         iter = iter_next;
00151     }
00152 }
00153 
00154 
00159 void
00160 flush_ad(UserList * foo)
00161 {
00162     aMassAd *iter, *iter_next;
00163     iter = NULL;
00164 
00165     iter = LIST_FIRST(&masslist);
00166 
00167     if (iter == NULL)
00168         return;
00169     while (iter) {
00170         iter_next = LIST_NEXT(iter, ma_lst);
00171 
00172         if (iter->sender == foo || !foo)
00173             remove_ad((iter));
00174         iter = iter_next;
00175     }
00176 }
00177 
00181 void
00182 remove_ad(aMassAd * zap)
00183 {
00184     if (zap == NULL)
00185         return;
00186 
00187     LIST_REMOVE(zap, ma_lst);
00188     FREE(zap);
00189 }
00190 
00191 
00196 void
00197 unlink_ad(aMassAd * zap)
00198 {
00199     LIST_REMOVE(zap, ma_lst);
00200 }
00201 
00202 
00203 
00207 char
00208 ranchar(int typ)
00209 {
00210     char aVal = 0;
00211     int rval = 0;
00212     /* ircnames */
00213     static char alphatab1[] =
00214         "ABCDEFGHIJKLMNOPQRSTUVWXYZabcefghijklmnopqrstuvwxyz";
00215     /* nicks */
00216     static char alphatab2[] =
00217         "ABCDEFGHIJKLMNOPQRSTUVWXYZabcefghijklmnopqrstuvwxyz_-[]\\^123456789";
00218     /* hostnames */
00219     static char alphatab3[] =
00220         "ABCDEFGHIJKLMNOPQRSTUVWXYZabcefghijklmnopqrstuvwxyz0123456789-";
00221 
00222     /* struct timeval tv_v;*/
00223     struct timezone tz_v;
00224 
00225     tz_v.tz_minuteswest = 0;
00226     tz_v.tz_dsttime = 0;
00227     rval = rand();
00228 
00229     switch (typ) {
00230     case 0:
00231         aVal = alphatab1[rval % (strlen(alphatab1) - 1)];
00232         break;
00233     case 1:
00234         aVal = alphatab2[rval % (strlen(alphatab2) - 1)];
00235         break;
00236     case 2:
00237         aVal = alphatab3[rval % (strlen(alphatab3) - 1)];
00238         break;
00239     default:
00240         aVal = alphatab1[rval % (strlen(alphatab1) - 1)];
00241         break;
00242     }
00243     return aVal;
00244 }
00245 
00254 char *
00255 ranstring(char *inbuf, int maxlen, int typ)
00256 {
00257     char *orig;
00258     int dolen = 0, minlen = 0, i = 0;
00259     /* struct timeval tv_v; */
00260 
00261     orig = inbuf;
00262     *inbuf = 0;
00263 
00264     switch (typ) {
00265     case 0:
00266         minlen = 4;
00267         break;
00268     case 1:
00269         minlen = 5;
00270         break;
00271     case 2:
00272         minlen = 3;
00273         break;
00274     case 3:
00275         minlen = 10;
00276         break;
00277     default:
00278         minlen = 1;
00279         break;
00280     }
00281 
00282     if (maxlen < 0 || minlen < 0 || minlen > maxlen) {
00283 //      wallops(NULL, "[***] ranstring() bug: maxlen=%d minlen=%d", maxlen,
00284 //              minlen);
00285         return NULL;
00286     }
00287 
00288     if (maxlen - minlen > 0)
00289         dolen = MIN((rand() % (maxlen - minlen)) + minlen, maxlen);
00290     else
00291         dolen = maxlen;
00292 
00293     switch (typ) {
00294     case 0:
00295     case 1:
00296     case 2:
00297     default:
00298         for (i = 0; i < dolen; i++) {
00299             if (inbuf < (orig + 1) || (typ < 1))
00300                 *inbuf = ranchar(0);
00301             else
00302                 *inbuf = ranchar(1);
00303             inbuf++;
00304         }
00305         *inbuf++ = 0;
00306         break;
00307     case 3:                 /* oh fun, a hostname :/ */
00308         if (dolen < 10)
00309             return NULL;
00310         for (i = 0; i < (dolen - 4); i++) {
00311             switch (i) {
00312             case 4:
00313                 *inbuf++ = '.';
00314                 break;
00315             case 7:
00316                 *inbuf++ = '.';
00317                 break;
00318             default:
00319 
00320                 *inbuf = ranchar(2);
00321                 inbuf++;
00322                 break;
00323             }
00324         }
00325         *inbuf++ = '.';
00326         *inbuf++ = 'c';
00327         *inbuf++ = 'o';
00328         *inbuf++ = 'm';
00329         *inbuf++ = 0;
00330         break;
00331     }
00332     return orig;
00333 
00334 }
00335 
00339 char *
00340 make_clone()
00341 {
00342     static char rand_nick[NICKLEN + 1];
00343     char rand_user[USERLEN + 1];
00344     char host[HOSTLEN + 1], blip1[6], blip2[5];
00345     char rand_ircname[50];
00346     int fails = 0;
00347 
00348     (void)ranstring(rand_nick, NICKLEN - 1, 0);
00349     (void)ranstring(rand_user, USERLEN - 1, 2);
00350     (void)ranstring(rand_ircname, sizeof(rand_ircname) - 1, 1);
00351     (void)ranstring(blip1, sizeof(blip1) - 1, 0);
00352     (void)ranstring(blip2, sizeof(blip2) - 1, 1);
00353 
00354     snprintf(host, sizeof(host), "services-%x.%s-sorcery-massdetect.net",
00355         (time(0)/30)%1000, blip1);
00356 
00357 
00358     if (getNickData(rand_nick) || getRegNickData(rand_nick)) {
00359         while ((getNickData(rand_nick) || getRegNickData(rand_nick)) && fails < 50) {
00360             (void)ranstring(rand_nick, NICKLEN - 1, 0);
00361             if (fails++ > 20)
00362                 return NULL;
00363             else if (fails > 1 && fails < 3)
00364                 sSend("GLOBOPS :[***] make_clone(MassServ): nick %s already exists",
00365                          rand_nick);
00366         }
00367     }
00368 
00369     addUser(rand_nick, rand_user, host, rand_ircname, "+m");
00370     return rand_nick;
00371 }
00372 
00377 void adCloneMaintenance()
00378 {
00379     char *y;
00380     int k;
00381 
00382     if (massAdClones[0] == 0 && massAdClones[1] == 0) {
00383         if ((y = make_clone())) {
00384             massAdClones[0] = strdup(y);
00385             if ((y = make_clone()))
00386                 massAdClones[1] = strdup(y);
00387         }
00388         massAdClones[2] = massAdClones[3] = massAdClones[4] = NULL;
00389     }
00390     else {
00391         if (massAdClones[0])
00392         {
00393             sSend(":%s QUIT :%s", massAdClones[0], massAdClones[0]);
00394             massAdClones[2] = massAdClones[0];
00395             if ((y = make_clone()))
00396                 massAdClones[0] = strdup(y);
00397         }
00398 
00399         if (massAdClones[1])
00400         {
00401             sSend(":%s QUIT :%s", massAdClones[1], massAdClones[1]);
00402             massAdClones[3] = massAdClones[1];
00403             if ((y = make_clone()))
00404                 massAdClones[1] = strdup(y);
00405         }
00406 
00407         for(k = 2; k < 4; k++)
00408         {
00409             if (massAdClones[k]) {
00410                 FREE(massAdClones[k]);
00411                 massAdClones[k] = 0;
00412             }
00413         }
00414     }
00415 }
00416 
00421 int adCheck(UserList *nl_from, char *nm_target, char *args[], int numargs)
00422 {
00423     int k = 0;
00424     char txt_message[IRCBUF];
00425 
00426     if (MassServ == NULL)
00427         return 0;
00428 
00429     if (nl_from == 0 || args == 0 || numargs < 1 || args[0]==0 || nm_target == 0)
00430         return 0;
00431     parse_str(args, numargs, 0, txt_message, IRCBUF);
00432 
00433     for(k = 0; k < 2; k++)
00434     {
00435         if (massAdClones[k] && strcasecmp(nm_target, massAdClones[k]) == 0)
00436         {
00437             detect_mass(nl_from, txt_message, k);
00438             return 1;
00439         }
00440     }
00441     return 0;
00442 }
00443 
00447 void timed_advert_maint(char *a)
00448 {
00449     if (MassServ != NULL) { /* Disable clones if MassServ off */
00450         timer(60*5, timed_advert_maint, NULL);
00451         expire_ads();
00452         adCloneMaintenance();
00453     }
00454 }
00455 
00456 /*************************************************************************/
00457 

Generated at Sat Oct 25 20:56:08 2003 for Services using Doxygen.
Services Copyr. 1996-2001 Chip Norkus, Max Byrd, Greg Poma, Michael Graff, James Hess, Dafydd James. All rights reserved See LICENSE for licensing information.