[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);
|
---|
[1256] | 207 | cs_strncpy(btxt, txt, sizeof(btxt));
|
---|
[8] | 208 | return;
|
---|
| 209 | }
|
---|
| 210 |
|
---|
| 211 | if (txt && btxt[0])
|
---|
| 212 | {
|
---|
| 213 | monitor_send(btxt);
|
---|
| 214 | txt[2]='E';
|
---|
[1256] | 215 | cs_strncpy(btxt, txt, sizeof(btxt));
|
---|
[8] | 216 | }
|
---|
| 217 | else
|
---|
| 218 | {
|
---|
| 219 | if (txt)
|
---|
[1256] | 220 | cs_strncpy(btxt, txt, sizeof(btxt));
|
---|
[8] | 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 |
|
---|
[1232] | 240 | char *monitor_get_srvname(int srvid, int caid){
|
---|
[1256] | 241 | int i;
|
---|
[1194] | 242 | struct s_srvid *this = cfg->srvid;
|
---|
[1256] | 243 | static char name[83];
|
---|
[1232] | 244 |
|
---|
[1194] | 245 | for (name[0] = 0; this && (!name[0]); this = this->next)
|
---|
[1256] | 246 | if (this->srvid == srvid)
|
---|
| 247 | for (i=0; i<this->ncaid; i++)
|
---|
[1232] | 248 | if (this->caid[i] == caid)
|
---|
[1256] | 249 | cs_strncpy(name, this->name, 32);
|
---|
[1232] | 250 |
|
---|
| 251 | if (!name[0]) sprintf(name, "[%04X:%04X]", caid, srvid);
|
---|
| 252 | if (!srvid) name[0] = '\0';
|
---|
[1194] | 253 | return(name);
|
---|
[8] | 254 | }
|
---|
| 255 |
|
---|
[930] | 256 | char *monitor_get_proto(int idx)
|
---|
[8] | 257 | {
|
---|
| 258 | int i;
|
---|
| 259 | char *ctyp;
|
---|
| 260 | switch(client[idx].typ)
|
---|
| 261 | {
|
---|
| 262 | case 's': ctyp="server" ; break;
|
---|
| 263 | case 'n': ctyp="resolver" ; break;
|
---|
| 264 | case 'l': ctyp="logger" ; break;
|
---|
| 265 | case 'p':
|
---|
| 266 | case 'r': if ((i=cs_idx2ridx(idx))<0) // should never happen
|
---|
| 267 | ctyp=(client[idx].typ=='p') ? "proxy" : "reader";
|
---|
| 268 | else
|
---|
| 269 | {
|
---|
| 270 | switch(reader[i].typ) // TODO like ph
|
---|
| 271 | {
|
---|
| 272 | case R_MOUSE : ctyp="mouse"; break;
|
---|
[802] | 273 | case R_INTERNAL: ctyp="intern"; break;
|
---|
[71] | 274 | case R_SMART : ctyp="smartreader"; break;
|
---|
[8] | 275 | case R_CAMD35 : ctyp="camd 3.5x";break;
|
---|
| 276 | case R_CAMD33 : ctyp="camd 3.3x";break;
|
---|
| 277 | case R_NEWCAMD : ctyp="newcamd"; break;
|
---|
| 278 | case R_RADEGAST: ctyp="radegast"; break;
|
---|
| 279 | case R_SERIAL : ctyp="serial"; break;
|
---|
[1256] | 280 | case R_GBOX : ctyp="gbox"; break;
|
---|
| 281 | #ifdef HAVE_PCSC
|
---|
| 282 | case R_PCSC : ctyp="pcsc"; break;
|
---|
| 283 | #endif
|
---|
| 284 | case R_CCCAM : ctyp="cccam"; break;
|
---|
| 285 | case R_CS378X : ctyp="cs378x"; break;
|
---|
[8] | 286 | default : ctyp="unknown"; break;
|
---|
| 287 | }
|
---|
| 288 | }
|
---|
| 289 | break;
|
---|
| 290 | default : ctyp=ph[client[idx].ctyp].desc;
|
---|
| 291 | }
|
---|
| 292 | return(ctyp);
|
---|
| 293 | }
|
---|
| 294 |
|
---|
[1194] | 295 | static char *monitor_client_info(char id, int i){
|
---|
| 296 | static char sbuf[256];
|
---|
[1256] | 297 | sbuf[0] = '\0';
|
---|
[8] | 298 |
|
---|
[1194] | 299 | if (client[i].pid){
|
---|
| 300 | char ldate[16], ltime[16], *usr;
|
---|
| 301 | int lsec, isec, cnr, con, cau;
|
---|
| 302 | time_t now;
|
---|
| 303 | struct tm *lt;
|
---|
| 304 | now=time((time_t)0);
|
---|
[803] | 305 |
|
---|
[1194] | 306 | if ((cfg->mon_hideclient_to <= 0) ||
|
---|
| 307 | (now-client[i].lastecm < cfg->mon_hideclient_to) ||
|
---|
| 308 | (now-client[i].lastemm < cfg->mon_hideclient_to) ||
|
---|
| 309 | (client[i].typ != 'c'))
|
---|
| 310 | {
|
---|
| 311 | lsec=now-client[i].login;
|
---|
| 312 | isec=now-client[i].last;
|
---|
| 313 | usr=client[i].usr;
|
---|
| 314 | if (((client[i].typ == 'r') || (client[i].typ == 'p')) && (con=cs_idx2ridx(i)) >= 0)
|
---|
| 315 | usr=reader[con].label;
|
---|
| 316 | if (client[i].dup)
|
---|
| 317 | con=2;
|
---|
| 318 | else
|
---|
| 319 | if ((client[i].tosleep) && (now-client[i].lastswitch>client[i].tosleep))
|
---|
| 320 | con = 1;
|
---|
| 321 | else
|
---|
| 322 | con = 0;
|
---|
| 323 | if (i - cdiff > 0)
|
---|
| 324 | cnr = i - cdiff;
|
---|
| 325 | else
|
---|
| 326 | cnr=(i > 1) ? i - 1 : 0;
|
---|
| 327 | if( (cau = client[i].au + 1) )
|
---|
| 328 | if ((now-client[i].lastemm) /60 > cfg->mon_aulow)
|
---|
| 329 | cau=-cau;
|
---|
| 330 | lt = localtime(&client[i].login);
|
---|
| 331 | sprintf(ldate, "%2d.%02d.%02d", lt->tm_mday, lt->tm_mon+1, lt->tm_year % 100);
|
---|
| 332 | sprintf(ltime, "%2d:%02d:%02d", lt->tm_hour, lt->tm_min, lt->tm_sec);
|
---|
| 333 | sprintf(sbuf, "[%c--CCC]%d|%c|%d|%s|%d|%d|%s|%d|%s|%s|%s|%d|%04X:%04X|%s|%d|%d\n",
|
---|
| 334 | id, client[i].pid, client[i].typ, cnr, usr, cau, client[i].crypted,
|
---|
| 335 | cs_inet_ntoa(client[i].ip), client[i].port, monitor_get_proto(i),
|
---|
| 336 | ldate, ltime, lsec, client[i].last_caid, client[i].last_srvid,
|
---|
[1232] | 337 | monitor_get_srvname(client[i].last_srvid, client[i].last_caid), isec, con);
|
---|
[1194] | 338 | }
|
---|
| 339 | }
|
---|
| 340 | return(sbuf);
|
---|
[8] | 341 | }
|
---|
| 342 |
|
---|
[1194] | 343 | static void monitor_process_info(){
|
---|
| 344 | int i;
|
---|
| 345 | time_t now = time((time_t)0);
|
---|
[803] | 346 |
|
---|
[1194] | 347 | for (i = 0; i < CS_MAXPID; i++){
|
---|
[1256] | 348 | if ((cfg->mon_hideclient_to <= 0) ||
|
---|
| 349 | ( now-client[i].lastecm < cfg->mon_hideclient_to) ||
|
---|
| 350 | ( now-client[i].lastemm < cfg->mon_hideclient_to) ||
|
---|
[1194] | 351 | ( client[i].typ != 'c')){
|
---|
| 352 | if (client[i].pid) {
|
---|
| 353 | if ((client[cs_idx].monlvl < 2) && (client[i].typ != 's')) {
|
---|
[1256] | 354 | if ((strcmp(client[cs_idx].usr, client[i].usr)) ||
|
---|
[1194] | 355 | ((client[i].typ != 'c') && (client[i].typ != 'm')))
|
---|
| 356 | continue;
|
---|
| 357 | }
|
---|
[1256] | 358 | monitor_send_info(monitor_client_info('I', i), 0);
|
---|
| 359 | }
|
---|
| 360 | }
|
---|
[1194] | 361 | }
|
---|
| 362 | monitor_send_info(NULL, 1);
|
---|
[1232] | 363 | }
|
---|
[8] | 364 |
|
---|
[1194] | 365 | static void monitor_send_details(char *txt, int pid){
|
---|
| 366 | char buf[256];
|
---|
| 367 | snprintf(buf, 255, "[D-----]%d|%s\n", pid, txt);
|
---|
| 368 | monitor_send_info(buf, 0);
|
---|
[8] | 369 | }
|
---|
| 370 |
|
---|
[1194] | 371 | static void monitor_send_details_version(){
|
---|
| 372 | char buf[256];
|
---|
| 373 | sprintf(buf, "[V-0000]version=%s, build=%s, system=%s%s\n", CS_VERSION_X, CS_SVN_VERSION, cs_platform(buf + 100), buf + 200);
|
---|
| 374 | monitor_send_info(buf, 1);
|
---|
[785] | 375 | }
|
---|
| 376 |
|
---|
[1194] | 377 | static void monitor_process_details_master(char *buf, int pid){
|
---|
| 378 | if (cfg->nice != 99)
|
---|
| 379 | sprintf(buf + 200, ", nice=%d", cfg->nice);
|
---|
| 380 | else
|
---|
| 381 | buf[200] = '\0';
|
---|
| 382 | sprintf(buf, "version=%s#%s, system=%s%s", CS_VERSION_X, CS_SVN_VERSION, cs_platform(buf + 100), buf + 200);
|
---|
| 383 | monitor_send_details(buf, pid);
|
---|
[8] | 384 |
|
---|
[1194] | 385 | sprintf(buf, "max. clients=%d, client max. idle=%ld sec", CS_MAXPID - 2, cfg->cmaxidle);
|
---|
| 386 | monitor_send_details(buf, pid);
|
---|
[8] | 387 |
|
---|
[1194] | 388 | if( cfg->max_log_size )
|
---|
| 389 | sprintf(buf + 200, "%d Kb", cfg->max_log_size);
|
---|
| 390 | else
|
---|
| 391 | strcpy(buf + 200, "unlimited");
|
---|
| 392 | sprintf(buf, "max. logsize=%s", buf + 200);
|
---|
| 393 | monitor_send_details(buf, pid);
|
---|
[8] | 394 |
|
---|
[1194] | 395 | sprintf(buf, "client timeout=%lu ms, cache delay=%ld ms", cfg->ctimeout, cfg->delay);
|
---|
| 396 | monitor_send_details(buf, pid);
|
---|
[8] | 397 |
|
---|
| 398 | //#ifdef CS_NOSHM
|
---|
| 399 | // sprintf(buf, "shared memory initialized (size=%d, fd=%d)", shmsize, shmid);
|
---|
| 400 | //#else
|
---|
| 401 | // sprintf(buf, "shared memory initialized (size=%d, id=%d)", shmsize, shmid);
|
---|
| 402 | //#endif
|
---|
| 403 | // monitor_send_details(buf, pid);
|
---|
| 404 | }
|
---|
| 405 |
|
---|
| 406 | #ifdef CS_RDR_INIT_HIST
|
---|
[1194] | 407 | static void monitor_process_details_reader(int pid, int idx){
|
---|
| 408 | int r_idx;
|
---|
| 409 | char *p;
|
---|
| 410 | if ((r_idx=cs_idx2ridx(idx))>=0)
|
---|
| 411 | for (p=(char *)reader[r_idx].init_history; *p; p+=strlen(p)+1)
|
---|
| 412 | monitor_send_details(p, pid);
|
---|
| 413 | else
|
---|
| 414 | monitor_send_details("Missing reader index !", pid);
|
---|
[8] | 415 | }
|
---|
| 416 | #endif
|
---|
| 417 |
|
---|
[1194] | 418 | static void monitor_process_details(char *arg){
|
---|
| 419 | int pid, idx;
|
---|
| 420 | char sbuf[256];
|
---|
| 421 | if (!arg) return;
|
---|
| 422 | if ((idx = idx_from_pid(pid = atoi(arg))) < 0)
|
---|
| 423 | monitor_send_details("Invalid PID", pid);
|
---|
| 424 | else
|
---|
| 425 | {
|
---|
| 426 | monitor_send_info(monitor_client_info('D', idx), 0);
|
---|
| 427 | switch(client[idx].typ)
|
---|
| 428 | {
|
---|
| 429 | case 's':
|
---|
| 430 | monitor_process_details_master(sbuf, pid);
|
---|
| 431 | break;
|
---|
| 432 | case 'c': case 'm':
|
---|
| 433 | break;
|
---|
| 434 | case 'r':
|
---|
[8] | 435 | #ifdef CS_RDR_INIT_HIST
|
---|
[1194] | 436 | monitor_process_details_reader(pid, idx);
|
---|
[8] | 437 | #endif
|
---|
[1194] | 438 | break;
|
---|
| 439 | case 'p':
|
---|
| 440 | break;
|
---|
| 441 | }
|
---|
| 442 | }
|
---|
| 443 | monitor_send_info(NULL, 1);
|
---|
[8] | 444 | }
|
---|
| 445 |
|
---|
[1194] | 446 | static void monitor_send_login(void){
|
---|
| 447 | char buf[64];
|
---|
| 448 | if (auth)
|
---|
| 449 | sprintf(buf, "[A-0000]1|%s logged in\n", client[cs_idx].usr);
|
---|
| 450 | else
|
---|
| 451 | strcpy(buf, "[A-0000]0|not logged in\n");
|
---|
| 452 | monitor_send_info(buf, 1);
|
---|
[8] | 453 | }
|
---|
| 454 |
|
---|
[1194] | 455 | static void monitor_login(char *usr){
|
---|
| 456 | char *pwd=NULL;
|
---|
| 457 | if ((usr) && (pwd=strchr(usr, ' ')))
|
---|
| 458 | *pwd++=0;
|
---|
| 459 | if (pwd)
|
---|
| 460 | monitor_auth_client(trim(usr), trim(pwd));
|
---|
| 461 | else
|
---|
| 462 | monitor_auth_client(NULL, NULL);
|
---|
| 463 | monitor_send_login();
|
---|
[8] | 464 | }
|
---|
| 465 |
|
---|
[1194] | 466 | static void monitor_logsend(char *flag){
|
---|
[8] | 467 | #ifdef CS_LOGHISTORY
|
---|
[1194] | 468 | int i;
|
---|
[8] | 469 | #endif
|
---|
[1194] | 470 | if (strcmp(flag, "on")) {
|
---|
| 471 | if (strcmp(flag, "onwohist")) {
|
---|
| 472 | client[cs_idx].log=0;
|
---|
| 473 | return;
|
---|
| 474 | }
|
---|
| 475 | }
|
---|
[822] | 476 |
|
---|
[1194] | 477 | if (client[cs_idx].log) // already on
|
---|
| 478 | return;
|
---|
[8] | 479 | #ifdef CS_LOGHISTORY
|
---|
[1194] | 480 | if (!strcmp(flag, "on")){
|
---|
| 481 | for (i = (*loghistidx + 3) % CS_MAXLOGHIST; i != *loghistidx; i = (i + 1) % CS_MAXLOGHIST){
|
---|
| 482 | char *p_usr, *p_txt;
|
---|
| 483 | p_usr=(char *)(loghist+(i*CS_LOGHISTSIZE));
|
---|
| 484 | p_txt = p_usr + 32;
|
---|
| 485 | if ((p_txt[0]) && ((client[cs_idx].monlvl > 1) || (!strcmp(p_usr, client[cs_idx].usr)))) {
|
---|
| 486 | char sbuf[8];
|
---|
| 487 | sprintf(sbuf, "%03d", client[cs_idx].logcounter);
|
---|
| 488 | client[cs_idx].logcounter=(client[cs_idx].logcounter + 1) % 1000;
|
---|
| 489 | memcpy(p_txt + 4, sbuf, 3);
|
---|
| 490 | monitor_send(p_txt);
|
---|
| 491 | }
|
---|
| 492 | }
|
---|
| 493 | }
|
---|
[8] | 494 | #endif
|
---|
[1194] | 495 | client[cs_idx].log=1;
|
---|
[1256] | 496 | }
|
---|
[1194] | 497 |
|
---|
| 498 | static void monitor_set_debuglevel(char *flag){
|
---|
| 499 | cs_dblevel^=atoi(flag);
|
---|
| 500 | kill(client[0].pid, SIGUSR1);
|
---|
[8] | 501 | }
|
---|
[803] | 502 |
|
---|
[1194] | 503 | static void monitor_set_account(char *args){
|
---|
[1190] | 504 | struct s_auth *account;
|
---|
| 505 | char delimiter[] = " =";
|
---|
| 506 | char *ptr;
|
---|
| 507 | int argidx, i, found;
|
---|
| 508 | char *argarray[3];
|
---|
| 509 | char *token[]={"au", "sleep", "uniq", "monlevel", "group", "services", "betatunnel", "ident", "caid", "chid", "class", "hostname"};
|
---|
| 510 | char buf[256];
|
---|
[827] | 511 |
|
---|
[1190] | 512 | argidx=0;
|
---|
| 513 | found=0;
|
---|
[827] | 514 |
|
---|
[1190] | 515 | ptr = strtok(args, delimiter);
|
---|
[827] | 516 |
|
---|
[1190] | 517 | // resolve arguments
|
---|
| 518 | while(ptr != NULL) {
|
---|
| 519 | argarray[argidx]=trim(ptr);
|
---|
| 520 | ptr = strtok(NULL, delimiter);
|
---|
| 521 | argidx++;
|
---|
| 522 | }
|
---|
[827] | 523 |
|
---|
[1190] | 524 | if(argidx != 3) {
|
---|
| 525 | sprintf(buf, "[S-0000]setuser failed - wrong number of parameters (%d)\n", argidx);
|
---|
| 526 | monitor_send_info(buf, 1);
|
---|
| 527 | return;
|
---|
| 528 | }
|
---|
[827] | 529 |
|
---|
[1190] | 530 | //search account
|
---|
| 531 | for (account=cfg->account; (account) ; account=account->next){
|
---|
| 532 | if (!strcmp(argarray[0], account->usr)){
|
---|
| 533 | found=1;
|
---|
| 534 | break;
|
---|
| 535 | }
|
---|
| 536 | }
|
---|
[827] | 537 |
|
---|
[1190] | 538 | if (found != 1){
|
---|
| 539 | sprintf(buf, "[S-0000]setuser failed - user %s not found\n", argarray[0]);
|
---|
| 540 | monitor_send_info(buf, 1);
|
---|
| 541 | return;
|
---|
| 542 | }
|
---|
[1256] | 543 |
|
---|
[1190] | 544 | found=0;
|
---|
| 545 | for (i=0; i<12; i++){
|
---|
[1256] | 546 | if (!strcmp(argarray[1], token[i])){
|
---|
| 547 | chk_account(token[i],argarray[2],account);
|
---|
[1190] | 548 | found = 1;
|
---|
[1256] | 549 | }
|
---|
[1190] | 550 | }
|
---|
[827] | 551 |
|
---|
[1256] | 552 | if (found != 1){
|
---|
| 553 | sprintf(buf, "[S-0000]setuser failed - parameter %s not exist", argarray[1]);
|
---|
| 554 | monitor_send_info(buf, 1);
|
---|
| 555 | return;
|
---|
| 556 | }
|
---|
| 557 |
|
---|
[1190] | 558 | cs_reinit_clients();
|
---|
[827] | 559 |
|
---|
[1190] | 560 | sprintf(buf, "[S-0000]setuser %s done - param %s set to %s\n", argarray[0], argarray[1], argarray[2]);
|
---|
| 561 | monitor_send_info(buf, 1);
|
---|
[827] | 562 | }
|
---|
| 563 |
|
---|
[1256] | 564 | static void monitor_set_server(char *args){
|
---|
| 565 | char delimiter[] = "=";
|
---|
| 566 | char *ptr;
|
---|
| 567 | int argidx, i, found;
|
---|
| 568 | char *argarray[3];
|
---|
| 569 | char *token[]={"clienttimeout", "fallbacktimeout", "clientmaxidle", "cachedelay", "bindwait", "netprio", "resolvedelay", "sleep", "unlockparental", "serialreadertimeout", "maxlogsize", "showecmdw", "waitforcards", "preferlocalcards"};
|
---|
| 570 | char buf[256];
|
---|
| 571 |
|
---|
| 572 | argidx=0; found=0;
|
---|
| 573 | ptr = strtok(args, delimiter);
|
---|
| 574 |
|
---|
| 575 | // resolve arguments
|
---|
| 576 | while(ptr != NULL) {
|
---|
| 577 | argarray[argidx]=trim(ptr);
|
---|
| 578 | ptr = strtok(NULL, delimiter);
|
---|
| 579 | argidx++;
|
---|
| 580 | }
|
---|
| 581 |
|
---|
| 582 | if(argidx != 2) {
|
---|
| 583 | sprintf(buf, "[S-0000]setserver failed - wrong number of parameters (%d)\n", argidx);
|
---|
| 584 | monitor_send_info(buf, 1);
|
---|
| 585 | return;
|
---|
| 586 | }
|
---|
| 587 |
|
---|
| 588 | trim(argarray[0]);
|
---|
| 589 | trim(argarray[1]);
|
---|
| 590 | strtolower(argarray[0]);
|
---|
| 591 |
|
---|
| 592 | for (i = 0; i < 14; i++)
|
---|
| 593 | if (!strcmp(argarray[0], token[i])) break;
|
---|
| 594 |
|
---|
| 595 | if (i < 14){
|
---|
| 596 | chk_t_global(token[i],argarray[1]);
|
---|
| 597 | sprintf(buf, "[S-0000]setserver done - param %s set to %s\n", argarray[0], argarray[1]);
|
---|
| 598 | monitor_send_info(buf, 1);
|
---|
| 599 | } else {
|
---|
| 600 | sprintf(buf, "[S-0000]setserver failed - parameter %s not exist", argarray[0]);
|
---|
| 601 | monitor_send_info(buf, 1);
|
---|
| 602 | return;
|
---|
| 603 | }
|
---|
| 604 |
|
---|
| 605 | if (cfg->ftimeout>=cfg->ctimeout) {
|
---|
| 606 | cfg->ftimeout = cfg->ctimeout - 100;
|
---|
| 607 | sprintf(buf, "[S-0000]setserver WARNING: fallbacktimeout adjusted to %lu ms", cfg->ftimeout);
|
---|
| 608 | monitor_send_info(buf, 1);
|
---|
| 609 | }
|
---|
| 610 | if(cfg->ftimeout < cfg->srtimeout) {
|
---|
| 611 | cfg->ftimeout = cfg->srtimeout + 100;
|
---|
| 612 | sprintf(buf, "[S-0000]setserver WARNING: fallbacktimeout adjusted to %lu ms", cfg->ftimeout);
|
---|
| 613 | monitor_send_info(buf, 1);
|
---|
| 614 | }
|
---|
| 615 | if(cfg->ctimeout < cfg->srtimeout) {
|
---|
| 616 | cfg->ctimeout = cfg->srtimeout + 100;
|
---|
| 617 | sprintf(buf, "[S-0000]setserver WARNING: clienttimeout adjusted to %lu ms", cfg->ctimeout);
|
---|
| 618 | monitor_send_info(buf, 1);
|
---|
| 619 | }
|
---|
| 620 | //kill(client[0].pid, SIGUSR1);
|
---|
| 621 | }
|
---|
| 622 |
|
---|
[8] | 623 | static int monitor_process_request(char *req)
|
---|
| 624 | {
|
---|
[1195] | 625 | int i, rc;
|
---|
| 626 | char *cmd[] = {"login", "exit", "log", "status", "shutdown", "reload", "details", "version", "debug", "setuser", "setserver"};
|
---|
[1256] | 627 | int cmdcnt = sizeof(cmd)/sizeof(char *); // Calculate the amount of items in array
|
---|
[1195] | 628 | char *arg;
|
---|
[1256] | 629 |
|
---|
[1195] | 630 | if( (arg = strchr(req, ' ')) ) { *arg++ = 0; trim(arg); }
|
---|
| 631 | //trim(req);
|
---|
| 632 | if ((!auth) && (strcmp(req, cmd[0]))) monitor_login(NULL);
|
---|
[1256] | 633 |
|
---|
[1195] | 634 | for (rc=1, i = 0; i < cmdcnt; i++)
|
---|
| 635 | if (!strcmp(req, cmd[i])) {
|
---|
| 636 | switch(i) {
|
---|
| 637 | case 0: monitor_login(arg); break; // login
|
---|
| 638 | case 1: rc=0; break; // exit
|
---|
| 639 | case 2: monitor_logsend(arg); break; // log
|
---|
| 640 | case 3: monitor_process_info(); break; // status
|
---|
| 641 | case 4: if (client[cs_idx].monlvl > 3) kill(client[0].pid, SIGQUIT); break; // shutdown
|
---|
| 642 | case 5: if (client[cs_idx].monlvl > 2) kill(client[0].pid, SIGHUP); break; // reload
|
---|
| 643 | case 6: monitor_process_details(arg); break; // details
|
---|
| 644 | case 7: monitor_send_details_version(); break; // version
|
---|
| 645 | case 8: if (client[cs_idx].monlvl > 3) monitor_set_debuglevel(arg); break; // debuglevel
|
---|
| 646 | case 9: if (client[cs_idx].monlvl > 3) monitor_set_account(arg); break; // setuser
|
---|
[1256] | 647 | case 10: if (client[cs_idx].monlvl > 3) monitor_set_server(arg); break; // setserver
|
---|
[1195] | 648 | default: continue;
|
---|
| 649 | }
|
---|
| 650 | break;
|
---|
| 651 | }
|
---|
| 652 | return(rc);
|
---|
[8] | 653 | }
|
---|
| 654 |
|
---|
[1194] | 655 | static void monitor_server(){
|
---|
| 656 | int n;
|
---|
| 657 | client[cs_idx].typ='m';
|
---|
| 658 | while (((n = process_input(mbuf, sizeof(mbuf), cfg->cmaxidle)) >= 0) && monitor_process_request((char *)mbuf));
|
---|
| 659 | cs_disconnect_client();
|
---|
[8] | 660 | }
|
---|
| 661 |
|
---|
[1194] | 662 | void module_monitor(struct s_module *ph){
|
---|
| 663 | static PTAB ptab;
|
---|
| 664 | ptab.ports[0].s_port = cfg->mon_port;
|
---|
| 665 | ph->ptab = &ptab;
|
---|
| 666 | ph->ptab->nports = 1;
|
---|
[8] | 667 |
|
---|
[1194] | 668 | if (cfg->mon_aulow < 1)
|
---|
| 669 | cfg->mon_aulow = 30;
|
---|
| 670 | strcpy(ph->desc, "monitor");
|
---|
| 671 | ph->type=MOD_CONN_UDP;
|
---|
| 672 | ph->multi = 0;
|
---|
| 673 | ph->watchdog = 1;
|
---|
| 674 | ph->s_ip = cfg->mon_srvip;
|
---|
| 675 | ph->s_handler = monitor_server;
|
---|
| 676 | ph->recv = monitor_recv;
|
---|
[8] | 677 | // ph->send_dcw=NULL;
|
---|
[1256] | 678 | }
|
---|
[928] | 679 |
|
---|
[1256] | 680 |
|
---|