[8] | 1 | #include "globals.h"
|
---|
| 2 | #ifdef CS_WITH_GBOX
|
---|
| 3 | # include "csgbox/gbox.h"
|
---|
| 4 | # define CS_VERSION_X CS_VERSION "-gbx-" GBXVERSION
|
---|
| 5 | #else
|
---|
| 6 | # define CS_VERSION_X CS_VERSION
|
---|
| 7 | #endif
|
---|
| 8 |
|
---|
[1194] | 9 | static int auth = 0;
|
---|
[8] | 10 |
|
---|
| 11 | static void monitor_check_ip()
|
---|
| 12 | {
|
---|
| 13 | int ok=0;
|
---|
| 14 | struct s_ip *p_ip;
|
---|
| 15 |
|
---|
| 16 | if (auth) return;
|
---|
| 17 | for (p_ip=cfg->mon_allowed; (p_ip) && (!ok); p_ip=p_ip->next)
|
---|
| 18 | ok=((client[cs_idx].ip>=p_ip->ip[0]) && (client[cs_idx].ip<=p_ip->ip[1]));
|
---|
| 19 | if (!ok)
|
---|
| 20 | {
|
---|
| 21 | cs_auth_client((struct s_auth *)0, "invalid ip");
|
---|
| 22 | cs_exit(0);
|
---|
| 23 | }
|
---|
| 24 | }
|
---|
| 25 |
|
---|
| 26 | static void monitor_auth_client(char *usr, char *pwd)
|
---|
| 27 | {
|
---|
| 28 | struct s_auth *account;
|
---|
| 29 |
|
---|
| 30 | if (auth) return;
|
---|
| 31 | if ((!usr) || (!pwd))
|
---|
| 32 | {
|
---|
| 33 | cs_auth_client((struct s_auth *)0, NULL);
|
---|
| 34 | cs_exit(0);
|
---|
| 35 | }
|
---|
| 36 | for (account=cfg->account, auth=0; (account) && (!auth);)
|
---|
| 37 | {
|
---|
| 38 | if (account->monlvl)
|
---|
| 39 | auth=!(strcmp(usr, account->usr) | strcmp(pwd, account->pwd));
|
---|
| 40 | if (!auth)
|
---|
| 41 | account=account->next;
|
---|
| 42 | }
|
---|
| 43 | if (!auth)
|
---|
| 44 | {
|
---|
| 45 | cs_auth_client((struct s_auth *)0, "invalid account");
|
---|
| 46 | cs_exit(0);
|
---|
| 47 | }
|
---|
| 48 | if (cs_auth_client(account, NULL))
|
---|
| 49 | cs_exit(0);
|
---|
| 50 | }
|
---|
| 51 |
|
---|
| 52 | static int secmon_auth_client(uchar *ucrc)
|
---|
| 53 | {
|
---|
| 54 | ulong crc;
|
---|
| 55 | struct s_auth *account;
|
---|
| 56 |
|
---|
| 57 | if (auth)
|
---|
| 58 | {
|
---|
| 59 | int s=memcmp(client[cs_idx].ucrc, ucrc, 4);
|
---|
| 60 | if (s)
|
---|
| 61 | cs_log("wrong user-crc or garbage !?");
|
---|
| 62 | return(!s);
|
---|
| 63 | }
|
---|
| 64 | client[cs_idx].crypted=1;
|
---|
| 65 | crc=(ucrc[0]<<24) | (ucrc[1]<<16) | (ucrc[2]<<8) | ucrc[3];
|
---|
| 66 | for (account=cfg->account; (account) && (!auth); account=account->next)
|
---|
| 67 | if ((account->monlvl) &&
|
---|
[70] | 68 | (crc==crc32(0L, MD5((unsigned char *)account->usr, strlen(account->usr), NULL), 16)))
|
---|
[8] | 69 | {
|
---|
| 70 | memcpy(client[cs_idx].ucrc, ucrc, 4);
|
---|
[70] | 71 | aes_set_key((char *)MD5((unsigned char *)account->pwd, strlen(account->pwd), NULL));
|
---|
[8] | 72 | if (cs_auth_client(account, NULL))
|
---|
| 73 | cs_exit(0);
|
---|
| 74 | auth=1;
|
---|
| 75 | }
|
---|
| 76 | if (!auth)
|
---|
| 77 | {
|
---|
| 78 | cs_auth_client((struct s_auth *)0, "invalid user");
|
---|
| 79 | cs_exit(0);
|
---|
| 80 | }
|
---|
| 81 | return(auth);
|
---|
| 82 | }
|
---|
| 83 |
|
---|
| 84 | int monitor_send_idx(int idx, char *txt)
|
---|
| 85 | {
|
---|
| 86 | int l;
|
---|
| 87 | unsigned char buf[256+32];
|
---|
| 88 | if (!client[idx].udp_fd)
|
---|
| 89 | return(-1);
|
---|
| 90 | usleep(500L); // avoid lost udp-pakets ..
|
---|
| 91 | if (!client[idx].crypted)
|
---|
| 92 | return(sendto(client[idx].udp_fd, txt, strlen(txt), 0,
|
---|
| 93 | (struct sockaddr *)&client[idx].udp_sa,
|
---|
| 94 | sizeof(client[idx].udp_sa)));
|
---|
| 95 | buf[0]='&';
|
---|
| 96 | buf[9]=l=strlen(txt);
|
---|
| 97 | l=boundary(4, l+5)+5;
|
---|
| 98 | memcpy(buf+1, client[idx].ucrc, 4);
|
---|
[70] | 99 | strcpy((char *)buf+10, txt);
|
---|
[8] | 100 | memcpy(buf+5, i2b(4, crc32(0L, buf+10, l-10)), 4);
|
---|
| 101 | aes_encrypt_idx(idx, buf+5, l-5);
|
---|
| 102 | return(sendto(client[idx].udp_fd, buf, l, 0,
|
---|
| 103 | (struct sockaddr *)&client[idx].udp_sa,
|
---|
| 104 | sizeof(client[idx].udp_sa)));
|
---|
| 105 | }
|
---|
| 106 |
|
---|
| 107 | #define monitor_send(t) monitor_send_idx(cs_idx, t)
|
---|
| 108 |
|
---|
| 109 | static int monitor_recv(uchar *buf, int l)
|
---|
| 110 | {
|
---|
| 111 | int n;
|
---|
| 112 | uchar nbuf[3] = { 'U', 0, 0 };
|
---|
| 113 | static int bpos=0;
|
---|
| 114 | static uchar *bbuf=NULL;
|
---|
| 115 | if (!bbuf)
|
---|
| 116 | {
|
---|
| 117 | bbuf=(uchar *)malloc(l);
|
---|
| 118 | if (!bbuf)
|
---|
| 119 | {
|
---|
| 120 | cs_log("Cannot allocate memory (errno=%d)", errno);
|
---|
| 121 | cs_exit(1);
|
---|
| 122 | }
|
---|
| 123 | }
|
---|
| 124 | if (bpos)
|
---|
| 125 | memcpy(buf, bbuf, n=bpos);
|
---|
| 126 | else
|
---|
[1126] | 127 | n=recv_from_udpipe(buf);
|
---|
[8] | 128 | bpos=0;
|
---|
| 129 | if (!n) return(buf[0]=0);
|
---|
| 130 | if (buf[0]=='&')
|
---|
| 131 | {
|
---|
| 132 | int bsize;
|
---|
| 133 | if (n<21) // 5+16 is minimum
|
---|
| 134 | {
|
---|
| 135 | cs_log("packet to short !");
|
---|
| 136 | return(buf[0]=0);
|
---|
| 137 | }
|
---|
| 138 | if (!secmon_auth_client(buf+1))
|
---|
| 139 | return(buf[0]=0);
|
---|
| 140 | aes_decrypt(buf+5, 16);
|
---|
| 141 | bsize=boundary(4, buf[9]+5)+5;
|
---|
| 142 | // cs_log("n=%d bsize=%d", n, bsize);
|
---|
| 143 | if (n>bsize)
|
---|
| 144 | {
|
---|
| 145 | // cs_log("DO >>>> copy-back");
|
---|
| 146 | memcpy(bbuf, buf+bsize, bpos=n-bsize);
|
---|
| 147 | n=bsize;
|
---|
[56] | 148 | if (!write(client[cs_idx].ufd, nbuf, sizeof(nbuf))) cs_exit(1); // trigger new event
|
---|
[8] | 149 | }
|
---|
| 150 | else if (n<bsize)
|
---|
| 151 | {
|
---|
| 152 | cs_log("packet-size mismatch !");
|
---|
| 153 | return(buf[0]=0);
|
---|
| 154 | }
|
---|
| 155 | aes_decrypt(buf+21, n-21);
|
---|
| 156 | if (memcmp(buf+5, i2b(4, crc32(0L, buf+10, n-10)), 4))
|
---|
| 157 | {
|
---|
| 158 | cs_log("CRC error ! wrong password ?");
|
---|
| 159 | return(buf[0]=0);
|
---|
| 160 | }
|
---|
| 161 | n=buf[9];
|
---|
| 162 | memmove(buf, buf+10, n);
|
---|
| 163 | }
|
---|
| 164 | else
|
---|
| 165 | {
|
---|
| 166 | uchar *p;
|
---|
| 167 | monitor_check_ip();
|
---|
| 168 | buf[n]='\0';
|
---|
[70] | 169 | if ((p=(uchar *)strchr((char *)buf, 10)) && (bpos=n-(p-buf)-1))
|
---|
[8] | 170 | {
|
---|
| 171 | memcpy(bbuf, p+1, bpos);
|
---|
| 172 | n=p-buf;
|
---|
[56] | 173 | if (!write(client[cs_idx].ufd, nbuf, sizeof(nbuf))) cs_exit(1); // trigger new event
|
---|
[8] | 174 | }
|
---|
| 175 | }
|
---|
| 176 | buf[n]='\0';
|
---|
[1117] | 177 | n=strlen(trim((char *)buf));
|
---|
| 178 | if (n) client[cs_idx].last=time((time_t *) 0);
|
---|
[8] | 179 | return(n);
|
---|
| 180 | }
|
---|
| 181 |
|
---|
| 182 | static void monitor_send_info(char *txt, int last)
|
---|
| 183 | {
|
---|
| 184 | static int seq=0, counter=0;
|
---|
| 185 | static char btxt[256] = {0};
|
---|
| 186 | char buf[8];
|
---|
| 187 | if (txt)
|
---|
| 188 | {
|
---|
| 189 | if (!btxt[0])
|
---|
| 190 | {
|
---|
| 191 | counter=0;
|
---|
| 192 | txt[2]='B';
|
---|
| 193 | }
|
---|
| 194 | else
|
---|
| 195 | counter++;
|
---|
[56] | 196 | sprintf(buf, "%03d", counter);
|
---|
[8] | 197 | memcpy(txt+4, buf, 3);
|
---|
| 198 | txt[3]='0'+seq;
|
---|
| 199 | }
|
---|
| 200 | else
|
---|
| 201 | if (!last)
|
---|
| 202 | return;
|
---|
| 203 |
|
---|
| 204 | if (!last)
|
---|
| 205 | {
|
---|
| 206 | if (btxt[0]) monitor_send(btxt);
|
---|
| 207 | strncpy(btxt, txt, sizeof(btxt));
|
---|
| 208 | return;
|
---|
| 209 | }
|
---|
| 210 |
|
---|
| 211 | if (txt && btxt[0])
|
---|
| 212 | {
|
---|
| 213 | monitor_send(btxt);
|
---|
| 214 | txt[2]='E';
|
---|
| 215 | strncpy(btxt, txt, sizeof(btxt));
|
---|
| 216 | }
|
---|
| 217 | else
|
---|
| 218 | {
|
---|
| 219 | if (txt)
|
---|
| 220 | strncpy(btxt, txt, sizeof(btxt));
|
---|
| 221 | btxt[2]=(btxt[2]=='B') ? 'S' : 'E';
|
---|
| 222 | }
|
---|
| 223 |
|
---|
| 224 | if (btxt[0])
|
---|
| 225 | {
|
---|
| 226 | monitor_send(btxt);
|
---|
| 227 | seq=(seq+1)%10;
|
---|
| 228 | }
|
---|
| 229 | btxt[0]=0;
|
---|
| 230 | }
|
---|
| 231 |
|
---|
[1194] | 232 | int cs_idx2ridx(int idx){
|
---|
| 233 | int i;
|
---|
| 234 | for (i = 0; i < CS_MAXREADER; i++)
|
---|
| 235 | if (reader[i].cs_idx==idx)
|
---|
| 236 | return(i);
|
---|
| 237 | return(-1);
|
---|
[8] | 238 | }
|
---|
| 239 |
|
---|
[1194] | 240 | char *monitor_get_srvname(int id){
|
---|
| 241 | struct s_srvid *this = cfg->srvid;
|
---|
| 242 | static char name[83];
|
---|
| 243 | for (name[0] = 0; this && (!name[0]); this = this->next)
|
---|
| 244 | if (this->srvid == id)
|
---|
| 245 | strncpy(name, this->name, 32);
|
---|
| 246 | if (!name[0]) sprintf(name, "[%04X]", id);
|
---|
| 247 | if (!id) name[0] = '\0';
|
---|
| 248 | return(name);
|
---|
[8] | 249 | }
|
---|
| 250 |
|
---|
[930] | 251 | char *monitor_get_proto(int idx)
|
---|
[8] | 252 | {
|
---|
| 253 | int i;
|
---|
| 254 | char *ctyp;
|
---|
| 255 | switch(client[idx].typ)
|
---|
| 256 | {
|
---|
| 257 | case 's': ctyp="server" ; break;
|
---|
| 258 | case 'n': ctyp="resolver" ; break;
|
---|
| 259 | case 'l': ctyp="logger" ; break;
|
---|
| 260 | case 'p':
|
---|
| 261 | case 'r': if ((i=cs_idx2ridx(idx))<0) // should never happen
|
---|
| 262 | ctyp=(client[idx].typ=='p') ? "proxy" : "reader";
|
---|
| 263 | else
|
---|
| 264 | {
|
---|
| 265 | switch(reader[i].typ) // TODO like ph
|
---|
| 266 | {
|
---|
| 267 | case R_MOUSE : ctyp="mouse"; break;
|
---|
[802] | 268 | case R_INTERNAL: ctyp="intern"; break;
|
---|
[71] | 269 | case R_SMART : ctyp="smartreader"; break;
|
---|
[8] | 270 | case R_CAMD35 : ctyp="camd 3.5x";break;
|
---|
| 271 | case R_CAMD33 : ctyp="camd 3.3x";break;
|
---|
| 272 | case R_NEWCAMD : ctyp="newcamd"; break;
|
---|
| 273 | case R_RADEGAST: ctyp="radegast"; break;
|
---|
| 274 | case R_SERIAL : ctyp="serial"; break;
|
---|
[829] | 275 | case R_GBOX : ctyp="gbox"; break;
|
---|
[832] | 276 | #ifdef HAVE_PCSC
|
---|
| 277 | case R_PCSC : ctyp="pcsc"; break;
|
---|
| 278 | #endif
|
---|
[829] | 279 | case R_CCCAM : ctyp="cccam"; break;
|
---|
| 280 | case R_CS378X : ctyp="cs378x"; break;
|
---|
[8] | 281 | default : ctyp="unknown"; break;
|
---|
| 282 | }
|
---|
| 283 | }
|
---|
| 284 | break;
|
---|
| 285 | default : ctyp=ph[client[idx].ctyp].desc;
|
---|
| 286 | }
|
---|
| 287 | return(ctyp);
|
---|
| 288 | }
|
---|
| 289 |
|
---|
[1194] | 290 | static char *monitor_client_info(char id, int i){
|
---|
| 291 | static char sbuf[256];
|
---|
| 292 | sbuf[0] = '\0';
|
---|
[8] | 293 |
|
---|
[1194] | 294 | if (client[i].pid){
|
---|
| 295 | char ldate[16], ltime[16], *usr;
|
---|
| 296 | int lsec, isec, cnr, con, cau;
|
---|
| 297 | time_t now;
|
---|
| 298 | struct tm *lt;
|
---|
| 299 | now=time((time_t)0);
|
---|
[803] | 300 |
|
---|
[1194] | 301 | if ((cfg->mon_hideclient_to <= 0) ||
|
---|
| 302 | (now-client[i].lastecm < cfg->mon_hideclient_to) ||
|
---|
| 303 | (now-client[i].lastemm < cfg->mon_hideclient_to) ||
|
---|
| 304 | (client[i].typ != 'c'))
|
---|
| 305 | {
|
---|
| 306 | lsec=now-client[i].login;
|
---|
| 307 | isec=now-client[i].last;
|
---|
| 308 | usr=client[i].usr;
|
---|
| 309 | if (((client[i].typ == 'r') || (client[i].typ == 'p')) && (con=cs_idx2ridx(i)) >= 0)
|
---|
| 310 | usr=reader[con].label;
|
---|
| 311 | if (client[i].dup)
|
---|
| 312 | con=2;
|
---|
| 313 | else
|
---|
| 314 | if ((client[i].tosleep) && (now-client[i].lastswitch>client[i].tosleep))
|
---|
| 315 | con = 1;
|
---|
| 316 | else
|
---|
| 317 | con = 0;
|
---|
| 318 | if (i - cdiff > 0)
|
---|
| 319 | cnr = i - cdiff;
|
---|
| 320 | else
|
---|
| 321 | cnr=(i > 1) ? i - 1 : 0;
|
---|
| 322 | if( (cau = client[i].au + 1) )
|
---|
| 323 | if ((now-client[i].lastemm) /60 > cfg->mon_aulow)
|
---|
| 324 | cau=-cau;
|
---|
| 325 | lt = localtime(&client[i].login);
|
---|
| 326 | sprintf(ldate, "%2d.%02d.%02d", lt->tm_mday, lt->tm_mon+1, lt->tm_year % 100);
|
---|
| 327 | sprintf(ltime, "%2d:%02d:%02d", lt->tm_hour, lt->tm_min, lt->tm_sec);
|
---|
| 328 | sprintf(sbuf, "[%c--CCC]%d|%c|%d|%s|%d|%d|%s|%d|%s|%s|%s|%d|%04X:%04X|%s|%d|%d\n",
|
---|
| 329 | id, client[i].pid, client[i].typ, cnr, usr, cau, client[i].crypted,
|
---|
| 330 | cs_inet_ntoa(client[i].ip), client[i].port, monitor_get_proto(i),
|
---|
| 331 | ldate, ltime, lsec, client[i].last_caid, client[i].last_srvid,
|
---|
| 332 | monitor_get_srvname(client[i].last_srvid), isec, con);
|
---|
| 333 | }
|
---|
| 334 | }
|
---|
| 335 | return(sbuf);
|
---|
[8] | 336 | }
|
---|
| 337 |
|
---|
[1194] | 338 | static void monitor_process_info(){
|
---|
| 339 | int i;
|
---|
| 340 | time_t now = time((time_t)0);
|
---|
[803] | 341 |
|
---|
[1194] | 342 | for (i = 0; i < CS_MAXPID; i++){
|
---|
| 343 | if ((cfg->mon_hideclient_to <= 0) ||
|
---|
| 344 | ( now-client[i].lastecm < cfg->mon_hideclient_to) ||
|
---|
| 345 | ( now-client[i].lastemm < cfg->mon_hideclient_to) ||
|
---|
| 346 | ( client[i].typ != 'c')){
|
---|
| 347 | if (client[i].pid) {
|
---|
| 348 | if ((client[cs_idx].monlvl < 2) && (client[i].typ != 's')) {
|
---|
| 349 | if ((strcmp(client[cs_idx].usr, client[i].usr)) ||
|
---|
| 350 | ((client[i].typ != 'c') && (client[i].typ != 'm')))
|
---|
| 351 | continue;
|
---|
| 352 | }
|
---|
| 353 | monitor_send_info(monitor_client_info('I', i), 0);
|
---|
[1191] | 354 | }
|
---|
[1194] | 355 | }
|
---|
| 356 | }
|
---|
| 357 | monitor_send_info(NULL, 1);
|
---|
[8] | 358 | }
|
---|
| 359 |
|
---|
[1194] | 360 | static void monitor_send_details(char *txt, int pid){
|
---|
| 361 | char buf[256];
|
---|
| 362 | snprintf(buf, 255, "[D-----]%d|%s\n", pid, txt);
|
---|
| 363 | monitor_send_info(buf, 0);
|
---|
[8] | 364 | }
|
---|
| 365 |
|
---|
[1194] | 366 | static void monitor_send_details_version(){
|
---|
| 367 | char buf[256];
|
---|
| 368 | sprintf(buf, "[V-0000]version=%s, build=%s, system=%s%s\n", CS_VERSION_X, CS_SVN_VERSION, cs_platform(buf + 100), buf + 200);
|
---|
| 369 | monitor_send_info(buf, 1);
|
---|
[785] | 370 | }
|
---|
| 371 |
|
---|
[1194] | 372 | static void monitor_process_details_master(char *buf, int pid){
|
---|
| 373 | if (cfg->nice != 99)
|
---|
| 374 | sprintf(buf + 200, ", nice=%d", cfg->nice);
|
---|
| 375 | else
|
---|
| 376 | buf[200] = '\0';
|
---|
| 377 | sprintf(buf, "version=%s#%s, system=%s%s", CS_VERSION_X, CS_SVN_VERSION, cs_platform(buf + 100), buf + 200);
|
---|
| 378 | monitor_send_details(buf, pid);
|
---|
[8] | 379 |
|
---|
[1194] | 380 | sprintf(buf, "max. clients=%d, client max. idle=%ld sec", CS_MAXPID - 2, cfg->cmaxidle);
|
---|
| 381 | monitor_send_details(buf, pid);
|
---|
[8] | 382 |
|
---|
[1194] | 383 | if( cfg->max_log_size )
|
---|
| 384 | sprintf(buf + 200, "%d Kb", cfg->max_log_size);
|
---|
| 385 | else
|
---|
| 386 | strcpy(buf + 200, "unlimited");
|
---|
| 387 | sprintf(buf, "max. logsize=%s", buf + 200);
|
---|
| 388 | monitor_send_details(buf, pid);
|
---|
[8] | 389 |
|
---|
[1194] | 390 | sprintf(buf, "client timeout=%lu ms, cache delay=%ld ms", cfg->ctimeout, cfg->delay);
|
---|
| 391 | monitor_send_details(buf, pid);
|
---|
[8] | 392 |
|
---|
| 393 | //#ifdef CS_NOSHM
|
---|
| 394 | // sprintf(buf, "shared memory initialized (size=%d, fd=%d)", shmsize, shmid);
|
---|
| 395 | //#else
|
---|
| 396 | // sprintf(buf, "shared memory initialized (size=%d, id=%d)", shmsize, shmid);
|
---|
| 397 | //#endif
|
---|
| 398 | // monitor_send_details(buf, pid);
|
---|
| 399 | }
|
---|
| 400 |
|
---|
| 401 | #ifdef CS_RDR_INIT_HIST
|
---|
[1194] | 402 | static void monitor_process_details_reader(int pid, int idx){
|
---|
| 403 | int r_idx;
|
---|
| 404 | char *p;
|
---|
| 405 | if ((r_idx=cs_idx2ridx(idx))>=0)
|
---|
| 406 | for (p=(char *)reader[r_idx].init_history; *p; p+=strlen(p)+1)
|
---|
| 407 | monitor_send_details(p, pid);
|
---|
| 408 | else
|
---|
| 409 | monitor_send_details("Missing reader index !", pid);
|
---|
[8] | 410 | }
|
---|
| 411 | #endif
|
---|
| 412 |
|
---|
[1194] | 413 | static void monitor_process_details(char *arg){
|
---|
| 414 | int pid, idx;
|
---|
| 415 | char sbuf[256];
|
---|
| 416 | if (!arg) return;
|
---|
| 417 | if ((idx = idx_from_pid(pid = atoi(arg))) < 0)
|
---|
| 418 | monitor_send_details("Invalid PID", pid);
|
---|
| 419 | else
|
---|
| 420 | {
|
---|
| 421 | monitor_send_info(monitor_client_info('D', idx), 0);
|
---|
| 422 | switch(client[idx].typ)
|
---|
| 423 | {
|
---|
| 424 | case 's':
|
---|
| 425 | monitor_process_details_master(sbuf, pid);
|
---|
| 426 | break;
|
---|
| 427 | case 'c': case 'm':
|
---|
| 428 | break;
|
---|
| 429 | case 'r':
|
---|
[8] | 430 | #ifdef CS_RDR_INIT_HIST
|
---|
[1194] | 431 | monitor_process_details_reader(pid, idx);
|
---|
[8] | 432 | #endif
|
---|
[1194] | 433 | break;
|
---|
| 434 | case 'p':
|
---|
| 435 | break;
|
---|
| 436 | }
|
---|
| 437 | }
|
---|
| 438 | monitor_send_info(NULL, 1);
|
---|
[8] | 439 | }
|
---|
| 440 |
|
---|
[1194] | 441 | static void monitor_send_login(void){
|
---|
| 442 | char buf[64];
|
---|
| 443 | if (auth)
|
---|
| 444 | sprintf(buf, "[A-0000]1|%s logged in\n", client[cs_idx].usr);
|
---|
| 445 | else
|
---|
| 446 | strcpy(buf, "[A-0000]0|not logged in\n");
|
---|
| 447 | monitor_send_info(buf, 1);
|
---|
[8] | 448 | }
|
---|
| 449 |
|
---|
[1194] | 450 | static void monitor_login(char *usr){
|
---|
| 451 | char *pwd=NULL;
|
---|
| 452 | if ((usr) && (pwd=strchr(usr, ' ')))
|
---|
| 453 | *pwd++=0;
|
---|
| 454 | if (pwd)
|
---|
| 455 | monitor_auth_client(trim(usr), trim(pwd));
|
---|
| 456 | else
|
---|
| 457 | monitor_auth_client(NULL, NULL);
|
---|
| 458 | monitor_send_login();
|
---|
[8] | 459 | }
|
---|
| 460 |
|
---|
[1194] | 461 | static void monitor_logsend(char *flag){
|
---|
[8] | 462 | #ifdef CS_LOGHISTORY
|
---|
[1194] | 463 | int i;
|
---|
[8] | 464 | #endif
|
---|
[1194] | 465 | if (strcmp(flag, "on")) {
|
---|
| 466 | if (strcmp(flag, "onwohist")) {
|
---|
| 467 | client[cs_idx].log=0;
|
---|
| 468 | return;
|
---|
| 469 | }
|
---|
| 470 | }
|
---|
[822] | 471 |
|
---|
[1194] | 472 | if (client[cs_idx].log) // already on
|
---|
| 473 | return;
|
---|
[8] | 474 | #ifdef CS_LOGHISTORY
|
---|
[1194] | 475 | if (!strcmp(flag, "on")){
|
---|
| 476 | for (i = (*loghistidx + 3) % CS_MAXLOGHIST; i != *loghistidx; i = (i + 1) % CS_MAXLOGHIST){
|
---|
| 477 | char *p_usr, *p_txt;
|
---|
| 478 | p_usr=(char *)(loghist+(i*CS_LOGHISTSIZE));
|
---|
| 479 | p_txt = p_usr + 32;
|
---|
| 480 | if ((p_txt[0]) && ((client[cs_idx].monlvl > 1) || (!strcmp(p_usr, client[cs_idx].usr)))) {
|
---|
| 481 | char sbuf[8];
|
---|
| 482 | sprintf(sbuf, "%03d", client[cs_idx].logcounter);
|
---|
| 483 | client[cs_idx].logcounter=(client[cs_idx].logcounter + 1) % 1000;
|
---|
| 484 | memcpy(p_txt + 4, sbuf, 3);
|
---|
| 485 | monitor_send(p_txt);
|
---|
| 486 | }
|
---|
| 487 | }
|
---|
| 488 | }
|
---|
[8] | 489 | #endif
|
---|
[1194] | 490 | client[cs_idx].log=1;
|
---|
| 491 | }
|
---|
| 492 |
|
---|
| 493 | static void monitor_set_debuglevel(char *flag){
|
---|
| 494 | cs_dblevel^=atoi(flag);
|
---|
| 495 | kill(client[0].pid, SIGUSR1);
|
---|
[8] | 496 | }
|
---|
[803] | 497 |
|
---|
[1194] | 498 | static void monitor_set_account(char *args){
|
---|
[1190] | 499 | struct s_auth *account;
|
---|
| 500 | char delimiter[] = " =";
|
---|
| 501 | char *ptr;
|
---|
| 502 | int argidx, i, found;
|
---|
| 503 | char *argarray[3];
|
---|
| 504 | char *token[]={"au", "sleep", "uniq", "monlevel", "group", "services", "betatunnel", "ident", "caid", "chid", "class", "hostname"};
|
---|
| 505 | char buf[256];
|
---|
[827] | 506 |
|
---|
[1190] | 507 | argidx=0;
|
---|
| 508 | found=0;
|
---|
[827] | 509 |
|
---|
[1190] | 510 | ptr = strtok(args, delimiter);
|
---|
[827] | 511 |
|
---|
[1190] | 512 | // resolve arguments
|
---|
| 513 | while(ptr != NULL) {
|
---|
| 514 | argarray[argidx]=trim(ptr);
|
---|
| 515 | ptr = strtok(NULL, delimiter);
|
---|
| 516 | argidx++;
|
---|
| 517 | }
|
---|
[827] | 518 |
|
---|
[1190] | 519 | if(argidx != 3) {
|
---|
| 520 | sprintf(buf, "[S-0000]setuser failed - wrong number of parameters (%d)\n", argidx);
|
---|
| 521 | monitor_send_info(buf, 1);
|
---|
| 522 | return;
|
---|
| 523 | }
|
---|
[827] | 524 |
|
---|
[1190] | 525 | //search account
|
---|
| 526 | for (account=cfg->account; (account) ; account=account->next){
|
---|
| 527 | if (!strcmp(argarray[0], account->usr)){
|
---|
| 528 | found=1;
|
---|
| 529 | break;
|
---|
| 530 | }
|
---|
| 531 | }
|
---|
[827] | 532 |
|
---|
[1190] | 533 | if (found != 1){
|
---|
| 534 | sprintf(buf, "[S-0000]setuser failed - user %s not found\n", argarray[0]);
|
---|
| 535 | monitor_send_info(buf, 1);
|
---|
| 536 | return;
|
---|
| 537 | }
|
---|
| 538 |
|
---|
| 539 | found=0;
|
---|
| 540 | for (i=0; i<12; i++){
|
---|
| 541 | if (!strcmp(argarray[1], token[i])){
|
---|
| 542 | chk_account(token[i],argarray[2],account);
|
---|
| 543 | found = 1;
|
---|
| 544 | }
|
---|
| 545 | }
|
---|
| 546 |
|
---|
| 547 | if (found != 1){
|
---|
| 548 | sprintf(buf, "[S-0000]setuser failed - parameter %s not exist", argarray[1]);
|
---|
| 549 | monitor_send_info(buf, 1);
|
---|
| 550 | return;
|
---|
| 551 | }
|
---|
[827] | 552 |
|
---|
[1190] | 553 | cs_reinit_clients();
|
---|
[827] | 554 |
|
---|
[1190] | 555 | sprintf(buf, "[S-0000]setuser %s done - param %s set to %s\n", argarray[0], argarray[1], argarray[2]);
|
---|
| 556 | monitor_send_info(buf, 1);
|
---|
[827] | 557 | }
|
---|
[830] | 558 |
|
---|
[1194] | 559 | static void monitor_set_server(char *args){
|
---|
[1190] | 560 | char delimiter[] = "=";
|
---|
| 561 | char *ptr;
|
---|
| 562 | int argidx, i, found;
|
---|
| 563 | char *argarray[3];
|
---|
| 564 | char *token[]={"clienttimeout", "fallbacktimeout", "clientmaxidle", "cachedelay", "bindwait", "netprio", "resolvedelay", "sleep", "unlockparental", "serialreadertimeout", "maxlogsize", "showecmdw", "waitforcards", "preferlocalcards"};
|
---|
| 565 | char buf[256];
|
---|
[830] | 566 |
|
---|
[1190] | 567 | argidx=0; found=0;
|
---|
| 568 | ptr = strtok(args, delimiter);
|
---|
[830] | 569 |
|
---|
[1190] | 570 | // resolve arguments
|
---|
| 571 | while(ptr != NULL) {
|
---|
| 572 | argarray[argidx]=trim(ptr);
|
---|
| 573 | ptr = strtok(NULL, delimiter);
|
---|
| 574 | argidx++;
|
---|
| 575 | }
|
---|
[830] | 576 |
|
---|
[1190] | 577 | if(argidx != 2) {
|
---|
| 578 | sprintf(buf, "[S-0000]setserver failed - wrong number of parameters (%d)\n", argidx);
|
---|
| 579 | monitor_send_info(buf, 1);
|
---|
| 580 | return;
|
---|
| 581 | }
|
---|
[830] | 582 |
|
---|
[1190] | 583 | trim(argarray[0]);
|
---|
| 584 | trim(argarray[1]);
|
---|
| 585 | strtolower(argarray[0]);
|
---|
[830] | 586 |
|
---|
[1190] | 587 | found = 0;
|
---|
| 588 | for (i = 0; i < 14; i++)
|
---|
| 589 | if (!strcmp(argarray[0], token[i])) found = 1;
|
---|
[830] | 590 |
|
---|
[1190] | 591 | if (found != 1){
|
---|
| 592 | chk_t_global(token[i],argarray[1]);
|
---|
| 593 | sprintf(buf, "[S-0000]setserver done - param %s set to %s\n", argarray[0], argarray[1]);
|
---|
| 594 | monitor_send_info(buf, 1);
|
---|
| 595 | } else {
|
---|
| 596 | sprintf(buf, "[S-0000]setserver failed - parameter %s not exist", argarray[0]);
|
---|
| 597 | monitor_send_info(buf, 1);
|
---|
| 598 | return;
|
---|
| 599 | }
|
---|
[830] | 600 |
|
---|
[1190] | 601 | if (cfg->ftimeout>=cfg->ctimeout) {
|
---|
| 602 | cfg->ftimeout = cfg->ctimeout - 100;
|
---|
| 603 | sprintf(buf, "[S-0000]setserver WARNING: fallbacktimeout adjusted to %lu ms", cfg->ftimeout);
|
---|
| 604 | monitor_send_info(buf, 1);
|
---|
| 605 | }
|
---|
| 606 | if(cfg->ftimeout < cfg->srtimeout) {
|
---|
| 607 | cfg->ftimeout = cfg->srtimeout + 100;
|
---|
| 608 | sprintf(buf, "[S-0000]setserver WARNING: fallbacktimeout adjusted to %lu ms", cfg->ftimeout);
|
---|
| 609 | monitor_send_info(buf, 1);
|
---|
| 610 | }
|
---|
| 611 | if(cfg->ctimeout < cfg->srtimeout) {
|
---|
| 612 | cfg->ctimeout = cfg->srtimeout + 100;
|
---|
| 613 | sprintf(buf, "[S-0000]setserver WARNING: clienttimeout adjusted to %lu ms", cfg->ctimeout);
|
---|
| 614 | monitor_send_info(buf, 1);
|
---|
| 615 | }
|
---|
| 616 | //kill(client[0].pid, SIGUSR1);
|
---|
[830] | 617 | }
|
---|
[827] | 618 |
|
---|
[8] | 619 | static int monitor_process_request(char *req)
|
---|
| 620 | {
|
---|
[1195] | 621 | int i, rc;
|
---|
| 622 | char *cmd[] = {"login", "exit", "log", "status", "shutdown", "reload", "details", "version", "debug", "setuser", "setserver"};
|
---|
| 623 | int cmdcnt = sizeof(cmd)/sizeof(char *); // Calculate the amount of items in array
|
---|
| 624 | char *arg;
|
---|
| 625 |
|
---|
| 626 | if( (arg = strchr(req, ' ')) ) { *arg++ = 0; trim(arg); }
|
---|
| 627 | //trim(req);
|
---|
| 628 | if ((!auth) && (strcmp(req, cmd[0]))) monitor_login(NULL);
|
---|
| 629 |
|
---|
| 630 | for (rc=1, i = 0; i < cmdcnt; i++)
|
---|
| 631 | if (!strcmp(req, cmd[i])) {
|
---|
| 632 | switch(i) {
|
---|
| 633 | case 0: monitor_login(arg); break; // login
|
---|
| 634 | case 1: rc=0; break; // exit
|
---|
| 635 | case 2: monitor_logsend(arg); break; // log
|
---|
| 636 | case 3: monitor_process_info(); break; // status
|
---|
| 637 | case 4: if (client[cs_idx].monlvl > 3) kill(client[0].pid, SIGQUIT); break; // shutdown
|
---|
| 638 | case 5: if (client[cs_idx].monlvl > 2) kill(client[0].pid, SIGHUP); break; // reload
|
---|
| 639 | case 6: monitor_process_details(arg); break; // details
|
---|
| 640 | case 7: monitor_send_details_version(); break; // version
|
---|
| 641 | case 8: if (client[cs_idx].monlvl > 3) monitor_set_debuglevel(arg); break; // debuglevel
|
---|
| 642 | case 9: if (client[cs_idx].monlvl > 3) monitor_set_account(arg); break; // setuser
|
---|
| 643 | case 10: if (client[cs_idx].monlvl > 3) monitor_set_server(arg); break; // setserver
|
---|
| 644 | default: continue;
|
---|
| 645 | }
|
---|
| 646 | break;
|
---|
| 647 | }
|
---|
| 648 | return(rc);
|
---|
[8] | 649 | }
|
---|
| 650 |
|
---|
[1194] | 651 | static void monitor_server(){
|
---|
| 652 | int n;
|
---|
| 653 | client[cs_idx].typ='m';
|
---|
| 654 | while (((n = process_input(mbuf, sizeof(mbuf), cfg->cmaxidle)) >= 0) && monitor_process_request((char *)mbuf));
|
---|
| 655 | cs_disconnect_client();
|
---|
[8] | 656 | }
|
---|
| 657 |
|
---|
[1194] | 658 | void module_monitor(struct s_module *ph){
|
---|
| 659 | static PTAB ptab;
|
---|
| 660 | ptab.ports[0].s_port = cfg->mon_port;
|
---|
| 661 | ph->ptab = &ptab;
|
---|
| 662 | ph->ptab->nports = 1;
|
---|
[8] | 663 |
|
---|
[1194] | 664 | if (cfg->mon_aulow < 1)
|
---|
| 665 | cfg->mon_aulow = 30;
|
---|
| 666 | strcpy(ph->desc, "monitor");
|
---|
| 667 | ph->type=MOD_CONN_UDP;
|
---|
| 668 | ph->multi = 0;
|
---|
| 669 | ph->watchdog = 1;
|
---|
| 670 | ph->s_ip = cfg->mon_srvip;
|
---|
| 671 | ph->s_handler = monitor_server;
|
---|
| 672 | ph->recv = monitor_recv;
|
---|
[8] | 673 | // ph->send_dcw=NULL;
|
---|
[928] | 674 | }
|
---|
| 675 |
|
---|
| 676 |
|
---|