| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199 |
- /*
- * misc.c -- handles:
- * split() maskhost() dumplots() daysago() days() daysdur()
- * queueing output for the bot (msg and help)
- * resync buffers for sharebots
- * motd display and %var substitution
- *
- */
- #include "common.h"
- #include "misc.h"
- #include "rfc1459.h"
- #include "misc_file.h"
- #include "dcc.h"
- #include "users.h"
- #include "main.h"
- #include "debug.h"
- #include "dccutil.h"
- #include "chanprog.h"
- #include "color.h"
- #include "botmsg.h"
- #include "bg.h"
- #include <sys/stat.h>
- #include <sys/types.h>
- #include <sys/wait.h>
- #include <ctype.h>
- #include <unistd.h>
- #include <fcntl.h>
- #include "chan.h"
- #include "tandem.h"
- #include "modules.h"
- #include <pwd.h>
- #include <errno.h>
- #include <libgen.h>
- #ifdef HUB
- #include "userrec.h"
- #endif /* HUB */
- #ifdef S_ANTITRACE
- #include <sys/ptrace.h>
- #include <sys/wait.h>
- #endif /* S_ANTITRACE */
- #include <net/if.h>
- #include <sys/ioctl.h>
- #include <sys/socket.h>
- #include <signal.h>
- #include <sys/utsname.h>
- #include "stat.h"
- #include <stdarg.h>
- extern struct userrec *userlist;
- extern struct dcc_t *dcc;
- extern struct chanset_t *chanset;
- extern char version[], origbotname[], botname[],
- admin[], network[], motdfile[], ver[], botnetnick[],
- userfile[], dcc_prefix[],
- *binname, pid_file[], tempdir[], *owneremail;
- extern int backgrd, term_z, use_stderr, dcc_total, timesync,
- #ifdef HUB
- my_port,
- #endif
- strict_host, loading,
- localhub;
- extern time_t now;
- extern struct cfg_entry CFG_MOTD, CFG_LOGIN, CFG_BADPROCESS, CFG_PROCESSLIST, CFG_PROMISC,
- CFG_TRACE, CFG_HIJACK;
- void detected(int, char *);
- int server_lag = 0; /* GUESS! */
- /*
- * Misc functions
- */
- /* low-level stuff for other modules
- */
- /* This implementation wont overrun dst - 'max' is the max bytes that dst
- * can be, including the null terminator. So if 'dst' is a 128 byte buffer,
- * pass 128 as 'max'. The function will _always_ null-terminate 'dst'.
- *
- * Returns: The number of characters appended to 'dst'.
- *
- * Usage eg.
- *
- * char buf[128];
- * size_t bufsize = sizeof(buf);
- *
- * buf[0] = 0, bufsize--;
- *
- * while (blah && bufsize) {
- * bufsize -= egg_strcatn(buf, <some-long-string>, sizeof(buf));
- * }
- *
- * <Cybah>
- */
- int egg_strcatn(char *dst, const char *src, size_t max)
- {
- size_t tmpmax = 0;
- /* find end of 'dst' */
- while (*dst && max > 0) {
- dst++;
- max--;
- }
- /* Store 'max', so we can use it to workout how many characters were
- * written later on.
- */
- tmpmax = max;
- /* copy upto, but not including the null terminator */
- while (*src && max > 1) {
- *dst++ = *src++;
- max--;
- }
- /* null-terminate the buffer */
- *dst = 0;
- /* Don't include the terminating null in our count, as it will cumulate
- * in loops - causing a headache for the caller.
- */
- return tmpmax - max;
- }
- int my_strcpy(register char *a, register char *b)
- {
- register char *c = b;
- while (*b)
- *a++ = *b++;
- *a = *b;
- return b - c;
- }
- /* Split first word off of rest and put it in first
- */
- void splitc(char *first, char *rest, char divider)
- {
- char *p = strchr(rest, divider);
- if (p == NULL) {
- if (first != rest && first)
- first[0] = 0;
- return;
- }
- *p = 0;
- if (first != NULL)
- strcpy(first, rest);
- if (first != rest)
- /* In most circumstances, strcpy with src and dst being the same buffer
- * can produce undefined results. We're safe here, as the src is
- * guaranteed to be at least 2 bytes higher in memory than dest. <Cybah>
- */
- strcpy(rest, p + 1);
- }
- /* As above, but lets you specify the 'max' number of bytes (EXCLUDING the
- * terminating null).
- *
- * Example of use:
- *
- * char buf[HANDLEN + 1];
- *
- * splitcn(buf, input, "@", HANDLEN);
- *
- * <Cybah>
- */
- void splitcn(char *first, char *rest, char divider, size_t max)
- {
- char *p = strchr(rest, divider);
- if (p == NULL) {
- if (first != rest && first)
- first[0] = 0;
- return;
- }
- *p = 0;
- if (first != NULL)
- strncpyz(first, rest, max);
- if (first != rest)
- /* In most circumstances, strcpy with src and dst being the same buffer
- * can produce undefined results. We're safe here, as the src is
- * guaranteed to be at least 2 bytes higher in memory than dest. <Cybah>
- */
- strcpy(rest, p + 1);
- }
- char *splitnick(char **blah)
- {
- char *p = strchr(*blah, '!'), *q = *blah;
- if (p) {
- *p = 0;
- *blah = p + 1;
- return q;
- }
- return "";
- }
- void remove_crlf(char **line)
- {
- char *p;
- p = strchr(*line, '\n');
- if (p != NULL)
- *p = 0;
- p = strchr(*line, '\r');
- if (p != NULL)
- *p = 0;
- }
- char *newsplit(char **rest)
- {
- register char *o, *r;
- if (!rest)
- return *rest = "";
- o = *rest;
- while (*o == ' ')
- o++;
- r = o;
- while (*o && (*o != ' '))
- o++;
- if (*o)
- *o++ = 0;
- *rest = o;
- return r;
- }
- /* Convert "abc!user@a.b.host" into "*!user@*.b.host"
- * or "abc!user@1.2.3.4" into "*!user@1.2.3.*"
- * or "abc!user@0:0:0:0:0:ffff:1.2.3.4" into "*!user@0:0:0:0:0:ffff:1.2.3.*"
- * or "abc!user@3ffe:604:2:b02e:6174:7265:6964:6573" into
- * "*!user@3ffe:604:2:b02e:6174:7265:6964:*"
- */
- void maskhost(const char *s, char *nw)
- {
- register const char *p, *q, *e, *f;
- int i;
- *nw++ = '*';
- *nw++ = '!';
- p = (q = strchr(s, '!')) ? q + 1 : s;
- /* Strip of any nick, if a username is found, use last 8 chars */
- if ((q = strchr(p, '@'))) {
- int fl = 0;
- if ((q - p) > 9) {
- nw[0] = '*';
- p = q - 7;
- i = 1;
- } else
- i = 0;
- while (*p != '@') {
- if (!fl && strchr("~+-^=", *p)) {
- if (strict_host)
- nw[i] = '?';
- else
- i--;
- } else
- nw[i] = *p;
- fl++;
- p++;
- i++;
- }
- nw[i++] = '@';
- q++;
- } else {
- nw[0] = '*';
- nw[1] = '@';
- i = 2;
- q = s;
- }
- nw += i;
- e = NULL;
- /* Now q points to the hostname, i point to where to put the mask */
- if ((!(p = strchr(q, '.')) || !(e = strchr(p + 1, '.'))) && !strchr(q, ':'))
- /* TLD or 2 part host */
- strcpy(nw, q);
- else {
- if (e == NULL) { /* IPv6 address? */
- const char *mask_str;
- f = strrchr(q, ':');
- if (strchr(f, '.')) { /* IPv4 wrapped in an IPv6? */
- f = strrchr(f, '.');
- mask_str = ".*";
- } else /* ... no, true IPv6. */
- mask_str = ":*";
- strncpy(nw, q, f - q);
- /* No need to nw[f-q] = 0 here, as the strcpy below will
- * terminate the string for us.
- */
- nw += (f - q);
- strcpy(nw, mask_str);
- } else {
- for (f = e; *f; f++);
- f--;
- if (*f >= '0' && *f <= '9') { /* Numeric IP address */
- while (*f != '.')
- f--;
- strncpy(nw, q, f - q);
- /* No need to nw[f-q] = 0 here, as the strcpy below will
- * terminate the string for us.
- */
- nw += (f - q);
- strcpy(nw, ".*");
- } else { /* Normal host >= 3 parts */
- /* a.b.c -> *.b.c
- * a.b.c.d -> *.b.c.d if tld is a country (2 chars)
- * OR *.c.d if tld is com/edu/etc (3 chars)
- * a.b.c.d.e -> *.c.d.e etc
- */
- const char *x = strchr(e + 1, '.');
- if (!x)
- x = p;
- else if (strchr(x + 1, '.'))
- x = e;
- else if (strlen(x) == 3)
- x = p;
- else
- x = e;
- sprintf(nw, "*%s", x);
- }
- }
- }
- }
- /* Dump a potentially super-long string of text.
- */
- void dumplots(int idx, const char *prefix, char *data)
- {
- char *p = data, *q, *n, c;
- const int max_data_len = 500 - strlen(prefix);
- if (!*data) {
- dprintf(idx, "%s\n", prefix);
- return;
- }
- while (strlen(p) > max_data_len) {
- q = p + max_data_len;
- /* Search for embedded linefeed first */
- n = strchr(p, '\n');
- if (n && n < q) {
- /* Great! dump that first line then start over */
- *n = 0;
- dprintf(idx, "%s%s\n", prefix, p);
- *n = '\n';
- p = n + 1;
- } else {
- /* Search backwards for the last space */
- while (*q != ' ' && q != p)
- q--;
- if (q == p)
- q = p + max_data_len;
- c = *q;
- *q = 0;
- dprintf(idx, "%s%s\n", prefix, p);
- *q = c;
- p = q;
- if (c == ' ')
- p++;
- }
- }
- /* Last trailing bit: split by linefeeds if possible */
- n = strchr(p, '\n');
- while (n) {
- *n = 0;
- dprintf(idx, "%s%s\n", prefix, p);
- *n = '\n';
- p = n + 1;
- n = strchr(p, '\n');
- }
- if (*p)
- dprintf(idx, "%s%s\n", prefix, p); /* Last trailing bit */
- }
- /* Convert an interval (in seconds) to one of:
- * "19 days ago", "1 day ago", "18:12"
- */
- void daysago(time_t now, time_t then, char *out)
- {
- if (now - then > 86400) {
- int days = (now - then) / 86400;
- sprintf(out, "%d day%s ago", days, (days == 1) ? "" : "s");
- return;
- }
- #ifdef S_UTCTIME
- egg_strftime(out, 6, "%H:%M", gmtime(&then));
- #else /* !S_UTCTIME */
- egg_strftime(out, 6, "%H:%M", localtime(&then));
- #endif /* S_UTCTIME */
- }
- /* Convert an interval (in seconds) to one of:
- * "in 19 days", "in 1 day", "at 18:12"
- */
- void days(time_t now, time_t then, char *out)
- {
- if (now - then > 86400) {
- int days = (now - then) / 86400;
- sprintf(out, "in %d day%s", days, (days == 1) ? "" : "s");
- return;
- }
- #ifdef S_UTCTIME
- egg_strftime(out, 9, "at %H:%M", gmtime(&now));
- #else /* !S_UTCTIME */
- egg_strftime(out, 9, "at %H:%M", localtime(&now));
- #endif /* S_UTCTIME */
- }
- /* Convert an interval (in seconds) to one of:
- * "for 19 days", "for 1 day", "for 09:10"
- */
- void daysdur(time_t now, time_t then, char *out)
- {
- char s[81];
- int hrs, mins;
- if (now - then > 86400) {
- int days = (now - then) / 86400;
- sprintf(out, "for %d day%s", days, (days == 1) ? "" : "s");
- return;
- }
- strcpy(out, "for ");
- now -= then;
- hrs = (int) (now / 3600);
- mins = (int) ((now - (hrs * 3600)) / 60);
- sprintf(s, "%02d:%02d", hrs, mins);
- strcat(out, s);
- }
- /* show l33t banner */
- char *wbanner() {
- int r;
- r = random();
- switch (r % 9) {
- case 0: return STR(" .__ __ .__\n__ _ ______________ |__|/ |_| |__\n\\ \\/ \\/ /\\_ __ \\__ \\ | \\ __\\ | \\\n \\ / | | \\// __ \\| || | | Y \\\n \\/\\_/ |__| (____ /__||__| |___| /\n \\/ \\/\n");
- case 1: return STR(" _ _ _ \n__ ___ __ __ _(_) |_| |__ \n\\ \\ /\\ / / '__/ _` | | __| '_ \\ \n \\ V V /| | | (_| | | |_| | | |\n \\_/\\_/ |_| \\__,_|_|\\__|_| |_|\n");
- case 2: return STR("@@@ @@@ @@@ @@@@@@@ @@@@@@ @@@ @@@@@@@ @@@ @@@\n@@@ @@@ @@@ @@@@@@@@ @@@@@@@@ @@@ @@@@@@@ @@@ @@@\n@@! @@! @@! @@! @@@ @@! @@@ @@! @@! @@! @@@\n!@! !@! !@! !@! @!@ !@! @!@ !@! !@! !@! @!@\n@!! !!@ @!@ @!@!!@! @!@!@!@! !!@ @!! @!@!@!@!\n!@! !!! !@! !!@!@! !!!@!!!! !!! !!! !!!@!!!!\n!!: !!: !!: !!: :!! !!: !!! !!: !!: !!: !!!\n:!: :!: :!: :!: !:! :!: !:! :!: :!: :!: !:!\n :::: :: ::: :: ::: :: ::: :: :: :: :::\n :: : : : : : : : : : : : : : :\n");
- case 3: return STR(" o8o . oooo\n `'' .o8 `888\noooo oooo ooo oooo d8b .oooo. oooo .o888oo 888 .oo.\n `88. `88. .8' `888''8P `P )88b `888 888 888P'Y88b\n `88..]88..8' 888 .oP'888 888 888 888 888\n `888'`888' 888 d8( 888 888 888 . 888 888\n `8' `8' d888b `Y888''8o o888o '888' o888o o888o\n");
- case 4: return STR(" *\n * * **\n** *** ** **\n** * ** **\n ** *** **** *** **** ******** **\n ** *** *** * **** **** * **** *** ******** ** ***\n ** *** **** ** **** * *** * *** ** ** * ***\n ** ** ** ** * **** ** ** *** ***\n ** ** ** ** ** ** ** ** ** **\n ** ** ** ** ** ** ** ** ** **\n ** ** ** ** ** ** ** ** ** **\n ** ** * ** ** ** ** ** ** **\n ******* ******* *** ** ** ** ** ** **\n ***** ***** *** ***** ** *** * ** ** **\n *** ** *** ** **\n *\n *\n *\n *\n");
- case 5: return STR(" ::: === === :::==== :::==== ::: :::==== ::: ===\n ::: === === ::: === ::: === ::: :::==== ::: ===\n === === === ======= ======== === === ========\n =========== === === === === === === === ===\n ==== ==== === === === === === === === ===\n");
- case 6: return STR(" _ _ _ ______ _______ _____ _______ _ _\n | | | |_____/ |_____| | | |_____|\n |__|__| | \\_ | | __|__ | | |\n");
- case 7: return STR(" dBPdBPdBP dBBBBBb dBBBBBb dBP dBBBBBBP dBP dBP\n dBP BB\n dBPdBPdBP dBBBBK dBP BB dBP dBP dBBBBBP\n dBPdBPdBP dBP BB dBP BB dBP dBP dBP dBP\n dBBBBBBBP dBP dB' dBBBBBBB dBP dBP dBP dBP\n");
- case 8: return STR(" /\n # #/\n ### # ##\n## # ## ##\n## ## ##\n ## ### #### ### /### /### ### ########## /##\n ## ### ###/ ###/ #### / ###/ ########### ## / ###\n ## ### ### ## ###/ ### ## ## ##/ ###\n ## ## ## ## ## ## ## ## ## ##\n ## ## ## ## ## ## ## ## ## ##\n ## ## ## ## ## ## ## ## ## ##\n ## ## ## ## ## ## ## ## ## ##\n ## /# / ## ## /# ## ## ## ##\n ######/ ######/ ### ####/ ## ### /## ## ##\n ##### ##### ### ### ##/##/ ## ## ##\n /\n /\n /\n /");
- }
- return "";
- }
- void show_banner(int idx)
- {
- dumplots(-dcc[idx].sock, "", wbanner()); /* we use sock so that colors aren't applied to banner */
- dprintf(idx, "\n \n");
- dprintf(idx, STR("info, bugs, suggestions, comments:\n- http://wraith.shatow.net/ -\n \n"));
- }
- /* show motd to dcc chatter */
- void show_motd(int idx)
- {
-
- if (CFG_MOTD.gdata && *(char *) CFG_MOTD.gdata) {
- char *who, *buf, date[50];
- time_t time;
- void *buf_ptr;
- buf = buf_ptr = strdup((char *) CFG_MOTD.gdata);
- who = newsplit(&buf);
- time = atoi(newsplit(&buf));
- #ifdef S_UTCTIME
- egg_strftime(date, sizeof date, "%c %Z", gmtime(&time));
- #else /* !S_UTCTIME */
- egg_strftime(date, sizeof date, "%c %Z", localtime(&time));
- #endif /* S_UTCTIME */
- dprintf(idx, "Motd set by \002%s\002 (%s)\n", who, date);
- dumplots(idx, "* ", replace(buf, "\\n", "\n"));
- dprintf(idx, " \n");
- free(buf_ptr);
- } else
- dprintf(idx, STR("Motd: none\n"));
- }
- void show_channels(int idx, char *handle)
- {
- struct chanset_t *chan;
- struct flag_record fr = { FR_CHAN | FR_GLOBAL, 0, 0, 0, 0 };
- struct userrec *u;
- int first = 0, l = 0, total = 0;
- char format[120];
- #ifdef LEAF
- module_entry *me = module_find("irc", 0, 0);
- Function *func = me->funcs;
- #endif /* LEAF */
- if (handle)
- u = get_user_by_handle(userlist, handle);
- else
- u = dcc[idx].user;
- for (chan = chanset;chan;chan = chan->next) {
- get_user_flagrec(u, &fr, chan->dname);
- if (l < strlen(chan->dname)) {
- l = strlen(chan->dname);
- }
- if (chk_op(fr, chan))
- total++;
- }
- egg_snprintf(format, sizeof format, " %%c%%-%us %%-s%%-s%%-s%%-s%%-s\n", (l+2));
- for (chan = chanset;chan;chan = chan->next) {
- #ifdef LEAF
- int opped = (func[16] (chan));
- #else /* !LEAF */
- int opped = 0;
- #endif /* LEAF */
- get_user_flagrec(u, &fr, chan->dname);
- if (chk_op(fr, chan)) {
- if (!first) {
- dprintf(idx, STR("%s %s access to %d channel%s:\n"), handle ? u->handle : "You", handle ? "has" : "have", total, (total > 1) ? "s" : "");
-
- first = 1;
- }
- dprintf(idx, format, opped ? '@' : ' ', chan->dname, !shouldjoin(chan) ? "(inactive) " : "",
- channel_private(chan) ? "(private) " : "", !channel_manop(chan) ? "(no manop) " : "",
- channel_bitch(chan) ? "(bitch) " : "", channel_closed(chan) ? "(closed)" : "");
- }
- }
- if (!first)
- dprintf(idx, STR("%s %s not have access to any channels.\n"), handle ? u->handle : "You", handle ? "does" : "do");
- }
- int getting_users()
- {
- int i;
- for (i = 0; i < dcc_total; i++)
- if ((dcc[i].type == &DCC_BOT) && (dcc[i].status & STAT_GETTING))
- return 1;
- return 0;
- }
- char *extracthostname(char *hostmask)
- {
- char *p = strrchr(hostmask, '@');
- return p ? p + 1 : "";
- }
- /* Create a string with random letters and digits
- */
- void make_rand_str(char *s, int len)
- {
- int j, r = 0;
- for (j = 0; j < len; j++) {
- r = random();
- if (r % 4 == 0)
- s[j] = '0' + (random() % 10);
- else if (r % 4 == 1)
- s[j] = 'a' + (random() % 26);
- else if (r % 4 == 2)
- s[j] = 'A' + (random() % 26);
- else
- s[j] = '!' + (random() % 15);
- if (s[j] == 33 || s[j] == 37 || s[j] == 34 || s[j] == 40 || s[j] == 41 || s[j] == 38 || s[j] == 36) //no % ( ) &
- s[j] = 35;
- }
- s[len] = '\0';
- }
- /* Convert an octal string into a decimal integer value. If the string
- * is empty or contains non-octal characters, -1 is returned.
- */
- int oatoi(const char *octal)
- {
- register int i;
- if (!*octal)
- return -1;
- for (i = 0; ((*octal >= '0') && (*octal <= '7')); octal++)
- i = (i * 8) + (*octal - '0');
- if (*octal)
- return -1;
- return i;
- }
- /* Return an allocated buffer which contains a copy of the string
- * 'str', with all 'div' characters escaped by 'mask'. 'mask'
- * characters are escaped too.
- *
- * Remember to free the returned memory block.
- */
- char *str_escape(const char *str, const char div, const char mask)
- {
- const int len = strlen(str);
- int buflen = (2 * len), blen = 0;
- char *buf = malloc(buflen + 1), *b = buf;
- const char *s;
- if (!buf)
- return NULL;
- for (s = str; *s; s++) {
- /* Resize buffer. */
- if ((buflen - blen) <= 3) {
- buflen = (buflen * 2);
- buf = realloc(buf, buflen + 1);
- if (!buf)
- return NULL;
- b = buf + blen;
- }
- if (*s == div || *s == mask) {
- sprintf(b, "%c%02x", mask, *s);
- b += 3;
- blen += 3;
- } else {
- *(b++) = *s;
- blen++;
- }
- }
- *b = 0;
- return buf;
- }
- /* Is every character in a string a digit? */
- int str_isdigit(const char *str)
- {
- if (!*str)
- return 0;
- for(; *str; ++str) {
- if (!egg_isdigit(*str))
- return 0;
- }
- return 1;
- }
- /* Search for a certain character 'div' in the string 'str', while
- * ignoring escaped characters prefixed with 'mask'.
- *
- * The string
- *
- * "\\3a\\5c i am funny \\3a):further text\\5c):oink"
- *
- * as str, '\\' as mask and ':' as div would change the str buffer
- * to
- *
- * ":\\ i am funny :)"
- *
- * and return a pointer to "further text\\5c):oink".
- *
- * NOTE: If you look carefully, you'll notice that strchr_unescape()
- * behaves differently than strchr().
- */
- char *strchr_unescape(char *str, const char div, register const char esc_char)
- {
- char buf[3];
- register char *s, *p;
- buf[3] = 0;
- for (s = p = str; *s; s++, p++) {
- if (*s == esc_char) { /* Found escape character. */
- /* Convert code to character. */
- buf[0] = s[1], buf[1] = s[2];
- *p = (unsigned char) strtol(buf, NULL, 16);
- s += 2;
- } else if (*s == div) {
- *p = *s = 0;
- return (s + 1); /* Found searched for character. */
- } else
- *p = *s;
- }
- *p = 0;
- return NULL;
- }
- /* As strchr_unescape(), but converts the complete string, without
- * searching for a specific delimiter character.
- */
- void str_unescape(char *str, register const char esc_char)
- {
- (void) strchr_unescape(str, 0, esc_char);
- }
- /* Kills the bot. s1 is the reason shown to other bots,
- * s2 the reason shown on the partyline. (Sup 25Jul2001)
- */
- void kill_bot(char *s1, char *s2)
- {
- #ifdef HUB
- write_userfile(-1);
- #endif /* HUB */
- call_hook(HOOK_DIE);
- chatout("*** %s\n", s1);
- botnet_send_chat(-1, botnetnick, s1);
- botnet_send_bye();
- fatal(s2, 0);
- }
- int isupdatehub()
- {
- #ifdef HUB
- struct userrec *buser;
- buser = get_user_by_handle(userlist, botnetnick);
- if ((buser) && (buser->flags & USER_UPDATEHUB))
- return 1;
- else
- #endif /* HUB */
- return 0;
- }
- int ischanhub()
- {
- struct userrec *buser;
- buser = get_user_by_handle(userlist, botnetnick);
- if ((buser) && (buser->flags & USER_CHANHUB))
- return 1;
- else
- return 0;
- }
- int dovoice(struct chanset_t *chan)
- {
- struct userrec *user = NULL;
- struct flag_record fr = { FR_GLOBAL | FR_CHAN, 0, 0 };
- if (!chan) return 0;
- user = get_user_by_handle(userlist, botnetnick);
- get_user_flagrec(user, &fr, chan->dname);
- if (glob_dovoice(fr) || chan_dovoice(fr))
- return 1;
- return 0;
- }
- int dolimit(struct chanset_t *chan)
- {
- struct userrec *user = NULL;
- struct flag_record fr = { FR_GLOBAL | FR_CHAN, 0, 0 };
- if (!chan) return 0;
- user = get_user_by_handle(userlist, botnetnick);
- get_user_flagrec(user, &fr, chan->dname);
- if (glob_dolimit(fr) || chan_dolimit(fr))
- return 1;
- return 0;
- }
- #ifdef S_LASTCHECK
- char last_buf[128]="";
- #endif /* S_LASTCHECK */
- void check_last() {
- #ifdef S_LASTCHECK
- char user[20];
- struct passwd *pw;
- if (!strcmp((char *) CFG_LOGIN.ldata ? CFG_LOGIN.ldata : CFG_LOGIN.gdata ? CFG_LOGIN.gdata : "ignore", "ignore"))
- return;
- pw = getpwuid(geteuid());
- if (!pw) return;
- strncpyz(user, pw->pw_name ? pw->pw_name : "" , sizeof(user));
- if (user[0]) {
- char *out;
- char buf[50];
- sprintf(buf, STR("last %s"), user);
- if (shell_exec(buf, NULL, &out, NULL)) {
- if (out) {
- char *p;
- p = strchr(out, '\n');
- if (p)
- *p = 0;
- if (strlen(out) > 10) {
- if (last_buf[0]) {
- if (strncmp(last_buf, out, sizeof(last_buf))) {
- char wrk[16384];
- sprintf(wrk, STR("Login: %s"), out);
- detected(DETECT_LOGIN, wrk);
- }
- }
- strncpyz(last_buf, out, sizeof(last_buf));
- }
- free(out);
- }
- }
- }
- #endif /* S_LASTCHECK */
- }
- void check_processes()
- {
- #ifdef S_PROCESSCHECK
- char *proclist,
- *out,
- *p,
- *np,
- *curp,
- buf[1024],
- bin[128];
- if (!strcmp((char *) CFG_BADPROCESS.ldata ? CFG_BADPROCESS.ldata : CFG_BADPROCESS.gdata ? CFG_BADPROCESS.gdata : "ignore", "ignore"))
- return;
- proclist = (char *) (CFG_PROCESSLIST.ldata && ((char *) CFG_PROCESSLIST.ldata)[0] ?
- CFG_PROCESSLIST.ldata : CFG_PROCESSLIST.gdata && ((char *) CFG_PROCESSLIST.gdata)[0] ? CFG_PROCESSLIST.gdata : NULL);
- if (!proclist)
- return;
- if (!shell_exec(STR("ps x"), NULL, &out, NULL))
- return;
- /* Get this binary's filename */
- strncpyz(buf, binname, sizeof(buf));
- p = strrchr(buf, '/');
- if (p) {
- p++;
- strncpyz(bin, p, sizeof(bin));
- } else {
- bin[0] = 0;
- }
- /* Fix up the "permitted processes" list */
- p = malloc(strlen(proclist) + strlen(bin) + 6);
- strcpy(p, proclist);
- strcat(p, " ");
- strcat(p, bin);
- strcat(p, " ");
- proclist = p;
- curp = out;
- while (curp) {
- np = strchr(curp, '\n');
- if (np)
- *np++ = 0;
- if (atoi(curp) > 0) {
- char *pid,
- *tty,
- *stat,
- *time,
- cmd[512],
- line[2048];
- strncpyz(line, curp, sizeof(line));
- /* it's a process line */
- /* Assuming format: pid tty stat time cmd */
- pid = newsplit(&curp);
- tty = newsplit(&curp);
- stat = newsplit(&curp);
- time = newsplit(&curp);
- strncpyz(cmd, curp, sizeof(cmd));
- /* skip any <defunct> procs "/bin/sh -c" crontab stuff and binname crontab stuff */
- if (!strstr(cmd, STR("<defunct>")) && !strncmp(cmd, STR("/bin/sh -c"), 10)
- && !strncmp(cmd, binname, strlen(binname))) {
- /* get rid of any args */
- if ((p = strchr(cmd, ' ')))
- *p = 0;
- /* remove [] or () */
- if (strlen(cmd)) {
- p = cmd + strlen(cmd) - 1;
- if (((cmd[0] == '(') && (*p == ')')) || ((cmd[0] == '[') && (*p == ']'))) {
- *p = 0;
- strcpy(buf, cmd + 1);
- strcpy(cmd, buf);
- }
- }
- /* remove path */
- if ((p = strrchr(cmd, '/'))) {
- p++;
- strcpy(buf, p);
- strcpy(cmd, buf);
- }
- /* skip "ps" */
- if (strcmp(cmd, "ps")) {
- /* see if proc's in permitted list */
- strcat(cmd, " ");
- if ((p = strstr(proclist, cmd))) {
- /* Remove from permitted list */
- while (*p != ' ')
- *p++ = 1;
- } else {
- char wrk[16384];
- sprintf(wrk, STR("Unexpected process: %s"), line);
- detected(DETECT_PROCESS, wrk);
- }
- }
- }
- }
- curp = np;
- }
- free(proclist);
- if (out)
- free(out);
- #endif /* S_PROCESSCHECK */
- }
- void check_promisc()
- {
- #ifdef S_PROMISC
- #ifdef SIOCGIFCONF
- char buf[8192];
- struct ifreq ifreq, *ifr;
- struct ifconf ifcnf;
- char *cp, *cplim;
- int sock;
- if (!strcmp((char *) CFG_PROMISC.ldata ? CFG_PROMISC.ldata : CFG_PROMISC.gdata ? CFG_PROMISC.gdata : "ignore", "ignore"))
- return;
- sock = socket(AF_INET, SOCK_STREAM, 0);
- ifcnf.ifc_len = 8191;
- ifcnf.ifc_buf = buf;
- if (ioctl(sock, SIOCGIFCONF, (char *) &ifcnf) < 0) {
- close(sock);
- return;
- }
- ifr = ifcnf.ifc_req;
- cplim = buf + ifcnf.ifc_len;
- for (cp = buf; cp < cplim; cp += sizeof(ifr->ifr_name) + sizeof(ifr->ifr_addr)) {
- ifr = (struct ifreq *) cp;
- ifreq = *ifr;
- if (!ioctl(sock, SIOCGIFFLAGS, (char *) &ifreq)) {
- if (ifreq.ifr_flags & IFF_PROMISC) {
- close(sock);
- detected(DETECT_PROMISC, STR("Detected promiscuous mode"));
- return;
- }
- }
- }
- close(sock);
- #endif /* SIOCGIFCONF */
- #endif /* S_PROMISC */
- }
- #ifdef S_ANTITRACE
- int traced = 0;
- void got_trace(int z)
- {
- traced = 0;
- }
- #endif /* S_ANTITRACE */
- void check_trace()
- {
- #ifdef S_ANTITRACE
- int x, parent, i;
- struct sigaction sv, *oldsv = NULL;
- if (!strcmp((char *) CFG_TRACE.ldata ? CFG_TRACE.ldata : CFG_TRACE.gdata ? CFG_TRACE.gdata : "ignore", "ignore"))
- return;
- parent = getpid();
- #ifdef __linux__
- egg_bzero(&sv, sizeof(sv));
- sv.sa_handler = got_trace;
- sigemptyset(&sv.sa_mask);
- oldsv = NULL;
- sigaction(SIGTRAP, &sv, oldsv);
- traced = 1;
- asm("INT3");
- sigaction(SIGTRAP, oldsv, NULL);
- if (traced)
- detected(DETECT_TRACE, STR("I'm being traced!"));
- else {
- x = fork();
- if (x == -1)
- return;
- else if (x == 0) {
- i = ptrace(PTRACE_ATTACH, parent, 0, 0);
- if (i == (-1) && errno == EPERM)
- detected(DETECT_TRACE, STR("I'm being traced!"));
- else {
- waitpid(parent, &i, 0);
- kill(parent, SIGCHLD);
- ptrace(PTRACE_DETACH, parent, 0, 0);
- kill(parent, SIGCHLD);
- }
- exit(0);
- } else
- wait(&i);
- }
- #endif /* __linux__ */
- #ifdef __FreeBSD__
- x = fork();
- if (x == -1)
- return;
- else if (x == 0) {
- i = ptrace(PT_ATTACH, parent, 0, 0);
- if (i == (-1) && errno == EBUSY)
- detected(DETECT_TRACE, STR("I'm being traced"));
- else {
- wait(&i);
- i = ptrace(PT_CONTINUE, parent, (caddr_t) 1, 0);
- kill(parent, SIGCHLD);
- wait(&i);
- i = ptrace(PT_DETACH, parent, (caddr_t) 1, 0);
- wait(&i);
- }
- exit(0);
- } else
- waitpid(x, NULL, 0);
- #endif /* __FreeBSD__ */
- #ifdef __OpenBSD__
- x = fork();
- if (x == -1)
- return;
- else if (x == 0) {
- i = ptrace(PT_ATTACH, parent, 0, 0);
- if (i == (-1) && errno == EBUSY)
- detected(DETECT_TRACE, STR("I'm being traced"));
- else {
- wait(&i);
- i = ptrace(PT_CONTINUE, parent, (caddr_t) 1, 0);
- kill(parent, SIGCHLD);
- wait(&i);
- i = ptrace(PT_DETACH, parent, (caddr_t) 1, 0);
- wait(&i);
- }
- exit(0);
- } else
- waitpid(x, NULL, 0);
- #endif /* __OpenBSD__ */
- #endif /* S_ANTITRACE */
- }
- int shell_exec(char *cmdline, char *input, char **output, char **erroutput)
- {
- FILE *inpFile,
- *outFile,
- *errFile;
- char tmpfile[161];
- int x, fd;
- int parent = getpid();
- if (!cmdline)
- return 0;
- /* Set up temp files */
- /* always use mkstemp() when handling temp filess! -dizz */
- sprintf(tmpfile, STR("%s.in-XXXXXX"), tempdir);
- if ((fd = mkstemp(tmpfile)) == -1 || (inpFile = fdopen(fd, "w+")) == NULL) {
- if (fd != -1) {
- unlink(tmpfile);
- close(fd);
- }
- putlog(LOG_ERRORS, "*" , STR("exec: Couldn't open '%s': %s"), tmpfile, strerror(errno));
- return 0;
- }
- unlink(tmpfile);
- if (input) {
- if (fwrite(input, 1, strlen(input), inpFile) != strlen(input)) {
- fclose(inpFile);
- putlog(LOG_ERRORS, "*", STR("exec: Couldn't write to '%s': %s"), tmpfile, strerror(errno));
- return 0;
- }
- fseek(inpFile, 0, SEEK_SET);
- }
- unlink(tmpfile);
- sprintf(tmpfile, STR("%s.err-XXXXXX"), tempdir);
- if ((fd = mkstemp(tmpfile)) == -1 || (errFile = fdopen(fd, "w+")) == NULL) {
- if (fd != -1) {
- unlink(tmpfile);
- close(fd);
- }
- putlog(LOG_ERRORS, "*", STR("exec: Couldn't open '%s': %s"), tmpfile, strerror(errno));
- return 0;
- }
- unlink(tmpfile);
- sprintf(tmpfile, STR("%s.out-XXXXXX"), tempdir);
- if ((fd = mkstemp(tmpfile)) == -1 || (outFile = fdopen(fd, "w+")) == NULL) {
- if (fd != -1) {
- unlink(tmpfile);
- close(fd);
- }
- putlog(LOG_ERRORS, "*", STR("exec: Couldn't open '%s': %s"), tmpfile, strerror(errno));
- return 0;
- }
- unlink(tmpfile);
- x = fork();
- if (x == -1) {
- putlog(LOG_ERRORS, "*", STR("exec: fork() failed: %s"), strerror(errno));
- fclose(inpFile);
- fclose(errFile);
- fclose(outFile);
- return 0;
- }
- if (x) {
- /* Parent: wait for the child to complete */
- int st = 0;
- waitpid(x, &st, 0);
- /* Now read the files into the buffers */
- fclose(inpFile);
- fflush(outFile);
- fflush(errFile);
- if (erroutput) {
- char *buf;
- int fs;
- fseek(errFile, 0, SEEK_END);
- fs = ftell(errFile);
- if (fs == 0) {
- (*erroutput) = NULL;
- } else {
- buf = malloc(fs + 1);
- fseek(errFile, 0, SEEK_SET);
- fread(buf, 1, fs, errFile);
- buf[fs] = 0;
- (*erroutput) = buf;
- }
- }
- fclose(errFile);
- if (output) {
- char *buf;
- int fs;
- fseek(outFile, 0, SEEK_END);
- fs = ftell(outFile);
- if (fs == 0) {
- (*output) = NULL;
- } else {
- buf = malloc(fs + 1);
- fseek(outFile, 0, SEEK_SET);
- fread(buf, 1, fs, outFile);
- buf[fs] = 0;
- (*output) = buf;
- }
- }
- fclose(outFile);
- return 1;
- } else {
- /* Child: make fd's and set them up as std* */
- int ind,
- outd,
- errd;
- char *argv[4];
- ind = fileno(inpFile);
- outd = fileno(outFile);
- errd = fileno(errFile);
- if (dup2(ind, STDIN_FILENO) == (-1)) {
- kill(parent, SIGCHLD);
- exit(1);
- }
- if (dup2(outd, STDOUT_FILENO) == (-1)) {
- kill(parent, SIGCHLD);
- exit(1);
- }
- if (dup2(errd, STDERR_FILENO) == (-1)) {
- kill(parent, SIGCHLD);
- exit(1);
- }
- argv[0] = STR("sh");
- argv[1] = STR("-c");
- argv[2] = cmdline;
- argv[3] = NULL;
- execvp(argv[0], &argv[0]);
- kill(parent, SIGCHLD);
- exit(1);
- }
- }
- /* Update system code
- */
- int ucnt = 0;
- void updatelocal(void)
- {
- #ifdef LEAF
- module_entry *me;
- #endif /* LEAF */
- Context;
- if (ucnt < 300) {
- ucnt++;
- return;
- }
- del_hook(HOOK_SECONDLY, (Function) updatelocal);
- ucnt = 0;
- /* let's drop the server connection ASAP */
- #ifdef LEAF
- if ((me = module_find("server", 0, 0))) {
- Function *func = me->funcs;
- (func[SERVER_NUKESERVER]) ("Updating...");
- }
- #endif /* LEAF */
- botnet_send_chat(-1, botnetnick, "Updating...");
- botnet_send_bye();
- fatal("Updating...", 1);
- usleep(2000 * 500);
- unlink(pid_file); //if this fails it is ok, cron will restart the bot, *hopefully*
- system(binname); //start new bot.
- exit(0);
- }
- int updatebin(int idx, char *par, int autoi)
- {
- char *path = NULL,
- *newbin;
- char buf[DIRMAX], old[DIRMAX], testbuf[DIRMAX];
- struct stat sb;
- int i;
- #ifdef LEAF
- module_entry *me;
- #endif /* LEAF */
- buf[0] = testbuf[0] = old[0] = 0;
- path = newsplit(&par);
- par = path;
- if (!par[0]) {
- if (idx)
- dprintf(idx, STR("Not enough parameters.\n"));
- return 1;
- }
- path = malloc(strlen(binname) + strlen(par) + 2);
- strcpy(path, binname);
- newbin = strrchr(path, '/');
- if (!newbin) {
- free(path);
- if (idx)
- dprintf(idx, STR("Don't know current binary name\n"));
- return 1;
- }
- newbin++;
- if (strchr(par, '/')) {
- *newbin = 0;
- if (idx)
- dprintf(idx, STR("New binary must be in %s and name must be specified without path information\n"), path);
- free(path);
- return 1;
- }
- strcpy(newbin, par);
- if (!strcmp(path, binname)) {
- free(path);
- if (idx)
- dprintf(idx, STR("Can't update with the current binary\n"));
- return 1;
- }
- if (stat(path, &sb)) {
- if (idx)
- dprintf(idx, STR("%s can't be accessed\n"), path);
- free(path);
- return 1;
- }
- if (chmod(path, S_IRUSR | S_IWUSR | S_IXUSR)) {
- if (idx)
- dprintf(idx, STR("Can't set mode 0600 on %s\n"), path);
- free(path);
- return 1;
- }
- //make a backup just in case.
- egg_snprintf(old, sizeof old, "%s.bin.old", tempdir);
- copyfile(binname, old);
- /* The binary should return '2' when ran with -2, if not it's probably corrupt. */
- egg_snprintf(testbuf, sizeof testbuf, "%s -2", path);
- i = system(testbuf);
- if (i == -1 || WEXITSTATUS(i) != 2) {
- if (idx)
- dprintf(idx, STR("Couldn't restart new binary (error %d)\n"), i);
- putlog(LOG_MISC, "*", STR("Couldn't restart new binary (error %d)\n"), i);
- return i;
- }
- if (movefile(path, binname)) {
- if (idx)
- dprintf(idx, STR("Can't rename %s to %s\n"), path, binname);
- free(path);
- return 1;
- }
- sprintf(buf, "%s", binname);
- #ifdef LEAF
- if (localhub) {
- /* if localhub = 1, this is the spawn bot and controls
- * the spawning of new bots. */
- sprintf(buf, "%s -L %s -P %d", buf, botnetnick, getpid());
- }
- #endif /* LEAF */
- //safe to run new binary..
- #ifdef LEAF
- if (!autoi && !localhub) //dont delete pid for auto update!!!
- #endif /* LEAF */
- unlink(pid_file); //delete pid so new binary doesnt exit.
- #ifdef HUB
- listen_all(my_port, 1); //close the listening port...
- usleep(5000);
- #endif /* HUB */
- putlog(LOG_DEBUG, "*", "Running for update: %s", buf);
- #ifdef LEAF
- if (!autoi && localhub) {
- /* let's drop the server connection ASAP */
- if ((me = module_find("server", 0, 0))) {
- Function *func = me->funcs;
- (func[SERVER_NUKESERVER]) ("Updating...");
- }
- #endif /* LEAF */
- if (idx)
- dprintf(idx, STR("Updating...bye\n"));
- putlog(LOG_MISC, "*", STR("Updating...\n"));
- botnet_send_chat(-1, botnetnick, "Updating...");
- botnet_send_bye();
- fatal("Updating...", 1);
- usleep(2000 * 500);
- system(buf); /* run the binary, it SHOULD work from earlier tests.. */
- exit(0);
- #ifdef LEAF
- } else if (localhub && autoi) {
- system(buf);
- add_hook(HOOK_SECONDLY, (Function) updatelocal);
- return 0;
- }
- #endif /* LEAF */
- /* this should never be reached */
- return 2;
- }
- int bot_aggressive_to(struct userrec *u)
- {
- char mypval[20],
- botpval[20];
- link_pref_val(u, botpval);
- link_pref_val(get_user_by_handle(userlist, botnetnick), mypval);
- if (strcmp(mypval, botpval) < 0)
- return 1;
- else
- return 0;
- }
- void detected(int code, char *msg)
- {
- #ifdef LEAF
- module_entry *me;
- #endif /* LEAF */
- char *p = NULL;
- char tmp[512];
- struct userrec *u;
- struct flag_record fr = { FR_GLOBAL, 0, 0 };
- int act;
- u = get_user_by_handle(userlist, botnetnick);
- #ifdef S_LASTCHECK
- if (code == DETECT_LOGIN)
- p = (char *) (CFG_LOGIN.ldata ? CFG_LOGIN.ldata : (CFG_LOGIN.gdata ? CFG_LOGIN.gdata : NULL));
- #endif /* S_LASTCHECK */
- #ifdef S_ANTITRACE
- if (code == DETECT_TRACE)
- p = (char *) (CFG_TRACE.ldata ? CFG_TRACE.ldata : (CFG_TRACE.gdata ? CFG_TRACE.gdata : NULL));
- #endif /* S_ANTITRACE */
- #ifdef S_PROMISC
- if (code == DETECT_PROMISC)
- p = (char *) (CFG_PROMISC.ldata ? CFG_PROMISC.ldata : (CFG_PROMISC.gdata ? CFG_PROMISC.gdata : NULL));
- #endif /* S_PROMISC */
- #ifdef S_PROCESSCHECK
- if (code == DETECT_PROCESS)
- p = (char *) (CFG_BADPROCESS.ldata ? CFG_BADPROCESS.ldata : (CFG_BADPROCESS.gdata ? CFG_BADPROCESS.gdata : NULL));
- #endif /* S_PROMISC */
- #ifdef S_HIJACKCHECK
- if (code == DETECT_SIGCONT)
- p = (char *) (CFG_HIJACK.ldata ? CFG_HIJACK.ldata : (CFG_HIJACK.gdata ? CFG_HIJACK.gdata : NULL));
- #endif /* S_PROMISC */
- if (!p)
- act = DET_WARN;
- else if (!strcmp(p, STR("die")))
- act = DET_DIE;
- else if (!strcmp(p, STR("reject")))
- act = DET_REJECT;
- else if (!strcmp(p, STR("suicide")))
- act = DET_SUICIDE;
- else if (!strcmp(p, STR("ignore")))
- act = DET_IGNORE;
- else
- act = DET_WARN;
- switch (act) {
- case DET_IGNORE:
- break;
- case DET_WARN:
- putlog(LOG_WARN, "*", msg);
- break;
- case DET_REJECT:
- do_fork();
- putlog(LOG_WARN, "*", STR("Setting myself +d: %s"), msg);
- sprintf(tmp, STR("+d: %s"), msg);
- set_user(&USERENTRY_COMMENT, u, tmp);
- get_user_flagrec(u, &fr, 0);
- fr.global = USER_DEOP | USER_BOT;
- set_user_flagrec(u, &fr, 0);
- sleep(1);
- break;
- case DET_DIE:
- putlog(LOG_WARN, "*", STR("Dying: %s"), msg);
- sprintf(tmp, STR("Dying: %s"), msg);
- set_user(&USERENTRY_COMMENT, u, tmp);
- #ifdef LEAF
- if ((me = module_find("server", 0, 0))) {
- Function *func = me->funcs;
- (func[SERVER_NUKESERVER]) ("BBL");
- }
- #endif /* LEAF */
- sleep(1);
- fatal(msg, 0);
- break;
- case DET_SUICIDE:
- putlog(LOG_WARN, "*", STR("Comitting suicide: %s"), msg);
- sprintf(tmp, STR("Suicide: %s"), msg);
- set_user(&USERENTRY_COMMENT, u, tmp);
- #ifdef LEAF
- if ((me = module_find("server", 0, 0))) {
- Function *func = me->funcs;
- (func[SERVER_NUKESERVER]) ("HARAKIRI!!");
- }
- #endif /* LEAF */
- sleep(1);
- unlink(binname);
- #ifdef HUB
- unlink(userfile);
- sprintf(tmp, STR("%s~"), userfile);
- unlink(tmp);
- #endif /* HUB */
- fatal(msg, 0);
- break;
- }
- }
- char kickprefix[25] = "";
- char bankickprefix[25] = "";
- char * kickreason(int kind) {
- int r;
- r=random();
- switch (kind) {
- case KICK_BANNED:
- switch (r % 6) {
- case 0: return STR("bye");
- case 1: return STR("banned");
- case 2: return STR("see you in hell");
- case 3: return STR("go away");
- case 4: return STR("cya around lewser");
- case 5: return STR("unwanted!");
- }
- case KICK_KUSER:
- switch (r % 4) {
- case 0: return STR("not wanted");
- case 1: return STR("something tells me you're annoying");
- case 2: return STR("don't bug me lewser");
- case 3: return STR("creep");
- }
- case KICK_KICKBAN:
- switch (r % 4) {
- case 0: return STR("gone");
- case 1: return STR("stupid");
- case 2: return STR("lewser");
- case 3: return STR("...");
- }
- case KICK_MASSDEOP:
- switch (r % 8) {
- case 0: return STR("spammer!");
- case 1: return STR("easy on the modes now");
- case 2: return STR("mode this");
- case 3: return STR("nice try");
- case 4: return STR("really?");
- case 5: return STR("you lose");
- case 6: return STR("scary... really scary...");
- case 7: return STR("i win kthx");
- }
- case KICK_BADOP:
- switch (r % 5) {
- case 0: return STR("neat...");
- case 1: return STR("oh, no you don't. go away.");
- case 2: return STR("didn't you forget something now?");
- case 3: return STR("no");
- case 4: return STR("hijack this");
- }
- case KICK_BADOPPED:
- switch (r % 5) {
- case 0: return STR("fuck off kid");
- case 1: return STR("asl?");
- case 2: return STR("whoa... what a hacker... skills!");
- case 3: return STR("yes! yes! yes! hit me baby one more time!");
- case 4: return STR("with your skills, you're better off jacking off than hijacking");
- }
- case KICK_MANUALOP:
- switch (r % 6) {
- case 0: return STR("naughty kid");
- case 1: return STR("didn't someone tell you that is bad?");
- case 2: return STR("want perm?");
- case 3: return STR("see how much good that did you?");
- case 4: return STR("not a smart move...");
- case 5: return STR("jackass!");
- }
- case KICK_MANUALOPPED:
- switch (r % 8) {
- case 0: return STR("your pal got mean friends. like me.");
- case 1: return STR("uhh now.. don't wake me up...");
- case 2: return STR("hi hun. missed me?");
- case 3: return STR("spammer! die!");
- case 4: return STR("boo!");
- case 5: return STR("that @ was useful, don't ya think?");
- case 6: return STR("not in my book");
- case 7: return STR("lol, really?");
- }
- case KICK_CLOSED:
- switch (r % 17) {
- case 0: return STR("locked");
- case 1: return STR("later");
- case 2: return STR("closed for now");
- case 3: return STR("come back later");
- case 4: return STR("better safe than sorry");
- case 5: return STR("cleanup, come back later");
- case 6: return STR("this channel is closed");
- case 7: return STR("shutting down for now");
- case 8: return STR("lockdown");
- case 9: return STR("reopening later");
- case 10: return STR("not for the public atm");
- case 11: return STR("private channel for now");
- case 12: return STR("might reopen soon, might reopen later");
- case 13: return STR("you're not supposed to be here right now");
- case 14: return STR("sorry, closed");
- case 15: return STR("try us later, atm we're locked down");
- case 16: return STR("closed. try tomorrow");
- }
- case KICK_FLOOD:
- switch (r % 7) {
- case 0: return STR("so much bullshit in such a short time. amazing.");
- case 1: return STR("slow down. i'm trying to read here.");
- case 2: return STR("uhm... you actually think irc is for talking?");
- case 3: return STR("talk talk talk");
- case 4: return STR("blabbering are we?");
- case 5: return STR("... and i don't even like you!");
- case 6: return STR("and you're outa here...");
- }
- case KICK_NICKFLOOD:
- switch (r % 7) {
- case 0: return STR("make up your mind?");
- case 1: return STR("be schizofrenic elsewhere");
- case 2: return STR("I'm loosing track of you... not!");
- case 3: return STR("that is REALLY annoying");
- case 4: return STR("try this: /NICK n00b");
- case 5: return STR("playing hide 'n' seek?");
- case 6: return STR("gotcha!");
- }
- case KICK_KICKFLOOD:
- switch (r % 6) {
- case 0: return STR("easier to just leave if you wan't to be alone");
- case 1: return STR("cool down");
- case 2: return STR("don't be so damned aggressive. that's my job.");
- case 3: return STR("kicking's fun, isn't it?");
- case 4: return STR("what's the rush?");
- case 5: return STR("next time you do that, i'll kick you again");
- }
- case KICK_BOGUSUSERNAME:
- return STR("bogus username");
- case KICK_MEAN:
- switch (r % 11) {
- case 0: return STR("hey! that wasn't very nice!");
- case 1: return STR("don't fuck with my pals");
- case 2: return STR("meanie!");
- case 3: return STR("I can be a bitch too...");
- case 4: return STR("leave the bots alone, will ya?");
- case 5: return STR("not very clever");
- case 6: return STR("watch it");
- case 7: return STR("fuck off");
- case 8: return STR("easy now. that's a friend.");
- case 9: return STR("abuse of power. leave that to me, will ya?");
- case 10: return STR("there as some things you cannot do, and that was one of them...");
- }
- case KICK_BOGUSKEY:
- return STR("I have a really hard time reading that key");
- default:
- return "OMFG@YUO";
- }
- }
- void makeplaincookie(char *chname, char *nick, char *buf)
- {
- /*
- plain cookie:
- Last 6 digits of time
- Last 5 chars of nick
- Last 4 regular chars of chan
- */
- char work[256],
- work2[256];
- int i,
- n;
- sprintf(work, STR("%010li"), (now + timesync));
- strcpy(buf, (char *) &work[4]);
- work[0] = 0;
- if (strlen(nick) < 5)
- while (strlen(work) + strlen(nick) < 5)
- strcat(work, " ");
- else
- strcpy(work, (char *) &nick[strlen(nick) - 5]);
- strcat(buf, work);
- n = 3;
- for (i = strlen(chname) - 1; (i >= 0) && (n >= 0); i--)
- if (((unsigned char) chname[i] < 128) && ((unsigned char) chname[i] > 32)) {
- work2[n] = tolower(chname[i]);
- n--;
- }
- while (n >= 0)
- work2[n--] = ' ';
- work2[4] = 0;
- strcat(buf, work2);
- }
- int goodpass(char *pass, int idx, char *nick)
- {
- char *tell;
- #ifdef S_NAZIPASS
- int i, nalpha = 0, lcase = 0, ucase = 0, ocase = 0, tc;
- #endif /* S_NAZIPASS */
- if (!pass[0])
- return 0;
- tell = malloc(300);
- #ifdef S_NAZIPASS
- for (i = 0; i < strlen(pass); i++) {
- tc = (int) pass[i];
- if (tc < 58 && tc > 47)
- ocase++; //number
- else if (tc < 91 && tc > 64)
- ucase++; //upper case
- else if (tc < 123 && tc > 96)
- lcase++; //lower case
- else
- nalpha++; //non-alphabet/number
- }
- /* if (ocase < 1 || lcase < 2 || ucase < 2 || nalpha < 1 || strlen(pass) < 8) { */
- if (ocase < 1 || lcase < 2 || ucase < 2 || strlen(pass) < 8) {
- #else /* !S_NAZIPASS */
- if (strlen(pass) < 8) {
- #endif /* S_NAZIPASS */
- sprintf(tell, "Insecure pass, must be: ");
- #ifdef S_NAZIPASS
- if (ocase < 1)
- strcat(tell, "\002>= 1 number\002, ");
- else
- strcat(tell, ">= 1 number, ");
- if (lcase < 2)
- strcat(tell, "\002>= 2 lcase\002, ");
- else
- strcat(tell, ">= 2 lowercase, ");
- if (ucase < 2)
- strcat(tell, "\002>= 2 ucase\002, ");
- else
- strcat(tell, ">= 2 uppercase, ");
- /* This is annoying as hell
- if (nalpha < 1)
- strcat(tell, "\002>= 1 non-alpha/num\002, ");
- else
- strcat(tell, ">= 1 non-alpha/num, ");
- */
- #endif /* S_NAZIPASS */
- if (strlen(pass) < 8)
- strcat(tell, "\002>= 8 chars.\002");
- else
- strcat(tell, ">= 8 chars.");
- if (idx)
- dprintf(idx, "%s\n", tell);
- else if (nick[0])
- dprintf(DP_HELP, STR("NOTICE %s :%s\n"), nick, tell);
- free(tell);
- return 0;
- }
- free(tell);
- return 1;
- }
- char *replace (char *string, char *oldie, char *newbie)
- {
- static char newstring[1024] = "";
- int str_index, newstr_index, oldie_index, end, new_len, old_len, cpy_len;
- char *c;
- if (string == NULL) return "";
- if ((c = (char *) strstr(string, oldie)) == NULL) return string;
- new_len = strlen(newbie);
- old_len = strlen(oldie);
- end = strlen(string) - old_len;
- oldie_index = c - string;
- newstr_index = 0;
- str_index = 0;
- while(str_index <= end && c != NULL) {
- cpy_len = oldie_index-str_index;
- strncpy(newstring + newstr_index, string + str_index, cpy_len);
- newstr_index += cpy_len;
- str_index += cpy_len;
- strcpy(newstring + newstr_index, newbie);
- newstr_index += new_len;
- str_index += old_len;
- if((c = (char *) strstr(string + str_index, oldie)) != NULL)
- oldie_index = c - string;
- }
- strcpy(newstring + newstr_index, string + str_index);
- return (newstring);
- }
- char *getfullbinname(char *argv0)
- {
- char *cwd, *bin, *p, *p2;
- bin = strdup(argv0);
- if (bin[0] == '/') {
- return bin;
- }
- cwd = malloc(8192);
- getcwd(cwd, 8191);
- cwd[8191] = 0;
- if (cwd[strlen(cwd) - 1] == '/')
- cwd[strlen(cwd) - 1] = 0;
- p = bin;
- p2 = strchr(p, '/');
- while (p) {
- if (p2)
- *p2++ = 0;
- if (!strcmp(p, "..")) {
- p = strrchr(cwd, '/');
- if (p)
- *p = 0;
- } else if (strcmp(p, ".")) {
- strcat(cwd, "/");
- strcat(cwd, p);
- }
- p = p2;
- if (p)
- p2 = strchr(p, '/');
- }
- free(bin);
- bin = strdup(cwd);
- free(cwd);
- return bin;
- }
- char *werr_tostr(int errnum)
- {
- switch (errnum) {
- case ERR_BINSTAT:
- return STR("Cannot access binary");
- case ERR_BINMOD:
- return STR("Cannot chmod() binary");
- case ERR_PASSWD:
- return STR("Cannot access the global passwd file");
- case ERR_WRONGBINDIR:
- return STR("Wrong directory/binary name");
- case ERR_CONFSTAT:
- #ifdef LEAF
- return STR("Cannot access config directory (~/.ssh/)");
- #else
- return STR("Cannot access config directory (./)");
- #endif /* LEAF */
- case ERR_TMPSTAT:
- #ifdef LEAF
- return STR("Cannot access tmp directory (~/.ssh/.../)");
- #else
- return STR("Cannot access config directory (./tmp/)");
- #endif /* LEAF */
- case ERR_CONFDIRMOD:
- #ifdef LEAF
- return STR("Cannot chmod() config directory (~/.ssh/)");
- #else
- return STR("Cannot chmod() config directory (./)");
- #endif /* LEAF */
- case ERR_CONFMOD:
- #ifdef LEAF
- return STR("Cannot chmod() config (~/.ssh/.known_hosts/)");
- #else
- return STR("Cannot chmod() config (./conf)");
- #endif /* LEAF */
- case ERR_TMPMOD:
- #ifdef LEAF
- return STR("Cannot chmod() tmp directory (~/.ssh/.../)");
- #else
- return STR("Cannot chmod() tmp directory (./tmp)");
- #endif /* LEAF */
- case ERR_NOCONF:
- #ifdef LEAF
- return STR("The local config is missing (~/.ssh/.known_hosts)");
- #else
- return STR("The local config is missing (./conf)");
- #endif /* LEAF */
- case ERR_CONFBADENC:
- return STR("Encryption in config is wrong/corrupt");
- case ERR_WRONGUID:
- return STR("UID in conf does not match getuid()");
- case ERR_WRONGUNAME:
- return STR("Uname in conf does not match uname()");
- case ERR_BADCONF:
- return STR("Config file is incomplete");
- default:
- return STR("Unforseen error");
- }
- }
- void werr(int errnum)
- {
- putlog(LOG_MISC, "*", STR("error #%d"), errnum);
- sdprintf(STR("error translates to: %s"), werr_tostr(errnum));
- printf(STR("(segmentation fault)\n"));
- fatal("", 0);
- }
- void local_check_should_lock()
- {
- module_entry *me;
- if ((me = module_find("channels", 0, 0))) {
- Function *func = me->funcs;
- /* check_should_lock() */
- (func[51]) ();
- }
- }
- /* convert binary hashes to hex */
- char *btoh(const unsigned char *md, int len)
- {
- int i;
- char buf[100], *ret;
- for (i = 0; i < len; i++)
- sprintf(&(buf[i*2]), "%02x", md[i]);
- ret = buf;
- return ret;
- }
- #define HELP_BOLD 1
- #define HELP_REV 2
- #define HELP_UNDER 4
- #define HELP_FLASH 8
- /* so many string++ is making the problem */
- void showhelp (int idx, struct flag_record *flags, char *string)
- {
- static int help_flags;
- struct flag_record fr = {FR_GLOBAL | FR_CHAN, 0, 0, 0, 0, 0};
- char helpstr[12288], tmp[2] = "", flagstr[10] = "";
- int ok = 1;
- helpstr[0] = 0;
- while (string && string[0]) {
- if (*string == '%') {
- if (!strncmp(string + 1, "{+", 2)) {
- while (*string && *string != '+') {
- string++;
- }
- flagstr[0] = 0;
- while (*string && *string != '}') {
- sprintf(tmp, "%c", *string);
- strcat(flagstr, tmp);
- string++;
- }
- string++;
- break_down_flags(flagstr, &fr, NULL);
- if (flagrec_ok(&fr, flags)) {
- ok = 1;
- while (*string && *string != '%') {
- sprintf(tmp, "%c", *string);
- strcat(helpstr, tmp);
- string++;
- }
- if (!strncmp(string + 1, "{-", 2)) {
- ok = 1;
- while (*string && *string != '}') {
- string++;
- }
- string++;
- }
- } else {
- ok = 0;
- }
- } else if (!strncmp(string + 1, "{-", 2)) {
- ok = 1;
- while (*string && *string != '}') {
- string++;
- }
- string++;
- } else if (*string == '{') {
- while (*string && *string != '}') {
- string++;
- }
- } else if (*(string + 1) == 'b') {
- string += 2;
- if (help_flags & HELP_BOLD) {
- help_flags &= ~HELP_BOLD;
- strcat(helpstr, color(idx, BOLD_CLOSE, 0));
- } else {
- help_flags |= HELP_BOLD;
- strcat(helpstr, color(idx, BOLD_OPEN, 0));
- }
- } else if (*(string + 1) == 'f') {
- string += 2;
-
- if (help_flags & HELP_FLASH) {
- strcat(helpstr, color(idx, FLASH_CLOSE, 0));
- help_flags &= ~HELP_FLASH;
- } else {
- help_flags |= HELP_FLASH;
- strcat(helpstr, color(idx, FLASH_OPEN, 0));
- }
- } else if (*(string + 1) == 'd') {
- string += 2;
- strcat(helpstr, dcc_prefix);
- } else if (*(string + 1) == '%') {
- string += 2;
- strcat(helpstr, "%");
- } else {
- if (ok) {
- sprintf(tmp, "%c", *string);
- strcat(helpstr, tmp);
- }
- string++;
- }
- } else {
- if (ok) {
- sprintf(tmp, "%c", *string);
- strcat(helpstr, tmp);
- }
- string++;
- }
- }
- helpstr[strlen(helpstr)] = 0;
- if (helpstr[0]) dumplots(idx, "", helpstr);
- }
- /* Arrange the N elements of ARRAY in random order. */
- static void shuffleArray(char *array[], int n)
- {
- int i;
- for (i = 0; i < n; i++) {
- int j = i + random() / (RAND_MAX / (n - i) + 1);
- char *t = array[j];
- array[j] = array[i];
- array[i] = t;
- }
- }
- void shuffle(char *string, char *delim)
- {
- char *array[501], *str, *work;
- int len = 0, i = 0;
- work = strdup(string);
- str = strtok(work, delim);
- while(str && *str)
- {
- array[len] = str;
- len++;
- str = strtok((char*) NULL, delim);
- }
- shuffleArray(array, len);
- string[0] = 0;
- for (i = 0; i < len; i++) {
- strcat(string, array[i]);
- if (i != len - 1)
- strcat(string, delim);
- }
- free(work);
- string[strlen(string)] = 0;
- }
- char *color(int idx, int type, int color)
- {
- int ansi = 0;
-
- /* if user is connected over TELNET or !backgrd, show ANSI
- * if they are relaying, they are most likely on an IRC client and should have mIRC codes
- */
- if ((idx && ((dcc[idx].type != &DCC_RELAYING) && (dcc[idx].status & STAT_TELNET))) || !backgrd) ansi++;
- if (type == BOLD_OPEN) {
- return ansi ? "\033[1m" : "\002";
- } else if (type == BOLD_CLOSE) {
- return ansi ? "\033[22m" : "\002";
- } else if (type == UNDERLINE_OPEN) {
- return ansi ? "\033[4m" : "\037";
- } else if (type == UNDERLINE_CLOSE) {
- return ansi ? "\033[24m" : "\037";
- } else if (type == FLASH_OPEN) {
- return ansi ? "\033[5m" : "\002\037";
- } else if (type == FLASH_CLOSE) {
- return ansi ? "\033[0m" : "\037\002";
- } else if (type == COLOR_OPEN) {
- switch (color) {
- case C_BLACK: return ansi ? "\033[30m" : "\00301";
- case C_RED: return ansi ? "\033[31m" : "\00305";
- case C_GREEN: return ansi ? "\033[32m" : "\00303";
- case C_BROWN: return ansi ? "\033[33m" : "\00307";
- case C_BLUE: return ansi ? "\033[34m" : "\00302";
- case C_PURPLE: return ansi ? "\033[35m" : "\00306";
- case C_CYAN: return ansi ? "\033[36m" : "\00310";
- case C_WHITE: return ansi ? "\033[1;37m" : "\00300";
- case C_DARKGREY: return ansi ? "\033[1;30m" : "\00314";
- case C_LIGHTRED: return ansi ? "\033[1;31m" : "\00304";
- case C_LIGHTGREEN: return ansi ? "\033[1;32m" : "\00309";
- case C_LIGHTBLUE: return ansi ? "\033[1;34m" : "\00312";
- case C_LIGHTPURPLE: return ansi ? "\033[1;35m" : "\00313";
- case C_LIGHTCYAN: return ansi ? "\033[1;36m" : "\00311";
- case C_LIGHTGREY: return ansi ? "\033[37m" : "\00315";
- case C_YELLOW: return ansi ? "\033[1;33m" : "\00308";
- default: break;
- }
- } else if (type == COLOR_CLOSE) {
- return ansi ? "\033[0m" : "\00300";
- }
- /* This should never be reached.. */
- return "";
- }
- int email(char *subject, char *msg, int who)
- {
- struct utsname un;
- char open[2048], addrs[1024];
- int mail = 0, sendmail = 0;
- FILE *f;
-
- uname(&un);
- if (is_file("/usr/sbin/sendmail"))
- sendmail++;
- else if (is_file("/usr/bin/mail"))
- mail++;
- else {
- putlog(LOG_WARN, "*", "I Have no usable mail client.");
- return 1;
- }
- open[0] = addrs[0] = 0;
- if (who & EMAIL_OWNERS) {
- sprintf(addrs, "%s", replace(owneremail, ",", " "));
- }
- if (who & EMAIL_TEAM) {
- if (addrs[0])
- sprintf(addrs, "%s wraith@shatow.net", addrs);
- else
- sprintf(addrs, "wraith@shatow.net");
- }
- if (sendmail)
- sprintf(open, "/usr/sbin/sendmail -t");
- else if (mail)
- sprintf(open, "/usr/bin/mail %s -a \"From: %s@%s\" -s \"%s\" -a \"Content-Type: text/plain\"", addrs, (origbotname && origbotname[0]) ? origbotname : "none", un.nodename, subject);
- if ((f = popen(open, "w"))) {
- if (sendmail) {
- struct passwd *pw;
- pw = getpwuid(geteuid());
- fprintf(f, "To: %s\n", addrs);
- fprintf(f, "From: %s@%s\n", (origbotname && origbotname[0]) ? origbotname : pw->pw_name, un.nodename);
- fprintf(f, "Subject: %s\n", subject);
- fprintf(f, "Content-Type: text/plain\n");
- }
- fprintf(f, "%s\n", msg);
- if (fflush(f))
- return 1;
- if (pclose(f))
- return 1;
- } else
- return 1;
- return 0;
- }
- void baduname(char *conf, char *my_uname) {
- char *tmpfile = malloc(strlen(tempdir) + 3 + 1);
- int send = 0;
- tmpfile[0] = 0;
- sprintf(tmpfile, "%s.un", tempdir);
- sdprintf("CHECKING %s", tmpfile);
- if (is_file(tmpfile)) {
- struct stat ss;
- time_t diff;
- stat(tmpfile, &ss);
- diff = now - ss.st_mtime;
- if (diff >= 86400) send++; /* only send once a day */
- } else {
- FILE *fp;
- if ((fp = fopen(tmpfile, "w"))) {
- fprintf(fp, "\n");
- fflush(fp);
- fclose(fp);
- send++; /* only send if we could write the file. */
- }
- }
- if (send) {
- struct passwd *pw;
- struct utsname un;
- char msg[501], subject[31];
- pw = getpwuid(geteuid());
- if (!pw) return;
- uname(&un);
- egg_snprintf(subject, sizeof subject, "CONF/UNAME() mismatch notice");
- egg_snprintf(msg, sizeof msg, "This is an auto email from a wraith bot which has you in it's OWNER_EMAIL list..\n \nThe uname() output on this box has changed, probably due to a kernel upgrade...\nMy login is: %s\nConf : %s\nUname(): %s\n \nThis email will only be sent once a day while this error is present.\nYou need to login to my shell (%s) and fix my local config.\n", pw->pw_name, conf, my_uname, un.nodename);
- email(subject, msg, EMAIL_OWNERS);
- }
- free(tmpfile);
- }
- char *homedir()
- {
- static char homedir[DIRMAX] = "";
- if (!homedir || (homedir && !homedir[0])) {
- char tmp[DIRMAX];
- struct passwd *pw;
- sdprintf(STR("If the bot dies after this, try compiling on Debian."));
- Context;
- pw = getpwuid(geteuid());
- sdprintf(STR("End Debian suggestion."));
- if (!pw)
- werr(ERR_PASSWD);
- Context;
- egg_snprintf(tmp, sizeof tmp, "%s", pw->pw_dir);
- Context;
- realpath(tmp, homedir); /* this will convert lame home dirs of /home/blah->/usr/home/blah */
- }
- return homedir;
- }
- char *confdir()
- {
- static char confdir[DIRMAX] = "";
- if (!confdir || (confdir && !confdir[0])) {
- #ifdef LEAF
- {
- egg_snprintf(confdir, sizeof confdir, "%s/.ssh", homedir());
- }
- #endif /* LEAF */
- #ifdef HUB
- {
- char *buf = strdup(binname);
- egg_snprintf(confdir, sizeof confdir, "%s", dirname(buf));
- free(buf);
- }
- #endif /* HUB */
- }
- return confdir;
- }
- char *my_uname()
- {
- static char os_uname[250] = "";
- if (!os_uname || (os_uname && !os_uname[0])) {
- char *unix_n, *vers_n;
- struct utsname un;
- if (uname(&un) < 0) {
- unix_n = "*unkown*";
- vers_n = "";
- } else {
- unix_n = un.nodename;
- #ifdef __FreeBSD__
- vers_n = un.release;
- #else /* __linux__ */
- vers_n = un.version;
- #endif /* __FreeBSD__ */
- }
- egg_snprintf(os_uname, sizeof os_uname, "%s %s", unix_n, vers_n);
- }
- return os_uname;
- }
|