#include "Watchaccess.h" /* Script shell care extrage informatii din netstat */ #define NETSTAT "netstat -t |\ awk \'(($1==\"tcp\")&&($6==\"ESTABLISHED\")&&\ ((index($4,\"localhost\")==0)||(index($5,\"localhost\")==0)))\\\n\ {if ($7 == \"\") print \"?\\t\" $4 \"\\t\" $5;\ else print $7 \"\\t\" $4 \"\\t\" $5;}\'>.n;\ wc -c <.n; cat .n;rm .n" /* Script shell care extrage informatii din who */ #define WHO "who |\ awk \'{ if ($6==\"\") print $1 \"\\t\" $4 \"\\t\" $5 \"\\t?\";\ else print $1 \"\\t\" $4 \"\\t\" $5 \"\\t\" $6;}\'>.w;\ wc -c <.w; cat .w; rm .w" /* Script shell care extrage informatii din ps */ #define PS "ps -aux |\ awk \'($1!=\"root\")&&($1!=\"nobody\")&&($1!=\"proxy\")&&\ ($1!=\"bin\")&&($1!=\"squid\")&&($1!=\"USER\")&&($1!=\"\")\ {if (index($9,\":\")==0)\ print $1 \"\\t\" $2 \"\\t24:00\\t\"\ $11 \" \" $12 \" \" $13 \" \" $14 \" \" $15;\ else print $1 \"\\t\" $2 \"\\t\" $9 \"\\t\"\ $11 \" \" $12 \" \" $13 \" \" $14 \" \" $15;}\'>.p;\ wc -c <.p; cat .p; rm .p" main (argc, argv) int argc; char *argv[]; { Mesaj *Cerere, *Raspuns, *Initializare; struct sockaddr_in Constatare, Supraveghere; char s[MAX], d[MAX], r[MAX], *p; short l; int cn, cw, cp, sd, fd, len_addr; FILE *fn, *fw, *fp; /* Initializari */ if (argc != 3) /* Verifica numarul de argumente */ ERR ("Constatare", 101); gethostname (s, MAX); /* Creaza mesajul de initializare */ Initializare = Alloc (4 * sizeof(short) + strlen(argv[2])+1); AtribuireI (Initializare, 1, 1); AtribuireI (Initializare, 2, 1); AtribuireI (Initializare, 3, atoi (argv[1])); AtribuireS (Initializare, 0, argv[2]); /* Creaza adrese socket */ memset ((char *) &Supraveghere, 0, sizeof (Supraveghere)); memset ((char *) &Constatare, 0, sizeof (Constatare)); Constatare.sin_family = AF_INET; Constatare.sin_addr.s_addr = htonl (INADDR_ANY); Constatare.sin_port = htons (PORT_CONSTATARE); /* Initiaza comunicarea prin socket */ if ((sd = socket (AF_INET, SOCK_STREAM, 0)) < 0) ERR ("Constatare", 102); if (bind (sd, (struct sockaddr *) &Constatare, sizeof (Constatare)) != 0) ERR ("Constatare", 103); if (listen (sd, 5) != 0) ERR ("Constatare", 104); signal(SIGCHLD, SIG_IGN); /* Pentru evitare zombie */ for (;;) { /* Bucla principala de asteptare */ if (fd = accept (sd, (struct sockaddr *) &Supraveghere, &len_addr), 0) ERR ("Constatare", 105); fn = popen("date", "r"); fgets(r, MAX, fn); fclose(fn); r[strlen(r)-1] = 0; /* Pregateste data si ora */ sprintf(d, "C: %s", r); /* Primeste mesajul de cerere */ if (Recv (fd, (char *) &l, sizeof (short)) < 0) { MES (d, 106); continue; } l = ntohs (l); Cerere = Alloc (l); if (Recv (fd, ((char *) Cerere) + sizeof (short), l - sizeof (short)) < 0) { MES (d, 107); continue; } /* Valideaza continutul cererii primite */ if (!Valid (Cerere) || (ValoareI (Cerere, 1) != 1) || (ValoareI (Cerere, 2) != 1) || (ValoareI (Cerere, 3) != ValoareI (Initializare, 3)) || (strstr (ValoareS (Initializare, 0), ValoareS (Cerere, 0)) == 0)) { MES (d, 108); continue; } /* Preia informatiile de conectare si procese useri */ fn = popen (NETSTAT, "r"); fw = popen (WHO, "r"); fp = popen (PS, "r"); fscanf (fn, "%d\n", &cn); fscanf (fw, "%d\n", &cw); fscanf (fp, "%d\n", &cp); /* Prepara mesajul de raspuns */ Raspuns = Alloc (4 * sizeof (short) + strlen (s) + cn + cw + cp + 4); AtribuireI (Raspuns, 1, 1); AtribuireI (Raspuns, 2, 4); AtribuireI (Raspuns, 3, getuid ()); AtribuireS (Raspuns, 0, s); p = (char *) Raspuns + 4 * sizeof (short) + strlen (s) + 1; for (l = 0; l < cn; l++, p++) (*p) = fgetc (fn); /* Copiaza sirul netstat */ (*p) = 0; p++; for (l = 0; l < cw; l++, p++) (*p) = fgetc (fw); /* Copiaza sirul who */ (*p) = 0; p++; for (l = 0; l < cp; l++, p++) (*p) = fgetc (fp); /* Copiaza sirul ps */ (*p) = 0; p++; fclose(fn); fclose(fw); fclose(fp); /* Trimite raspunsul */ if (Send (fd, (char *) Raspuns, ValoareI (Raspuns, 0)) < 0) ERR (d, 109); Delete (Raspuns); close (fd); } } /* Programul 3.19 Fisierul sursa Constatare.c */