int utmp_login(struct user_info *up) { struct user_info *uentp; int j; int h; j = binfo->utmpshm->guesthash_head[0] - 1; if (j < 0 || j >= MAXACTIVERUN) { return -1; } uentp = &(binfo->utmpshm->uinfo[j]); if (uentp->active && uentp->pid) { return -1; } binfo->utmpshm->guesthash_head[0] = binfo->utmpshm->guesthash_next[j + 1]; binfo->utmpshm->guesthash_next[j + 1] = 0; binfo->utmpshm->uinfo[j] = *up; binfo->utmpshm->activeuser++; update_max_online(); add_uindex(up->uid, j + 1); if (!strcasecmp(up->userid, "guest") && up->pid == 1) { h = utmp_iphash(up->from); binfo->utmpshm->guesthash_next[j + 1] = binfo->utmpshm->guesthash_head[h]; binfo->utmpshm->guesthash_head[h] = j + 1; binfo->utmpshm->wwwguestnum++; } utmp_kickidle(); return j + 1; }
static char * check_multi(char *id, int uid) { int i, uent; int h; if (uid <= 0 || uid > MAXUSERS) return NULL; if (strcasecmp(id, "guest")) { //这种算法, wwwlogin必须限制登录窗口数目, 否则 //上线名单会被轻易冲爆 for (i = 0; i < 6; i++) { uent = uindexshm->user[uid - 1][i] - 1; if (uent < 0) continue; if (do_check(uent, uid)) return makeurlbase(uent, uid); } return NULL; } else { h = utmp_iphash(realfromhost); uent = shm_utmp->guesthash_head[h]; while (uent != 0) { if (do_check(uent - 1, uid)) return makeurlbase(uent - 1, uid); uent = shm_utmp->guesthash_next[uent]; } } return NULL; }
static void utmp_kickidle() { int now; pid_t pid; int n, h, i; struct user_info *uentp, *u; now = time(NULL); if (now < binfo->utmpshm->uptime + 60) return; binfo->utmpshm->uptime = now; for (n = 0; n < MAXACTIVE; n++) { uentp = &(binfo->utmpshm->uinfo[n]); pid = uentp->pid; if (pid == 0) continue; if (uentp->active == 0) continue; if (pid == 1) //www //web session 长度,单位为秒 if ((now - uentp->lasttime < 60 * 60) && !uentp->wwwinfo.iskicked) continue; //telnet ssh 发呆长度,单位为秒 if (pid > 1 && now - uentp->lasttime < 3600) continue; if (pid > 1 && kill(pid, 0) == 0) { //如果进程存在 #ifdef DOTIMEOUT if (now - uentp->lasttime > ((uentp->ext_idle) ? IDLE_TIMEOUT * 3 : IDLE_TIMEOUT)) { kill(pid, SIGHUP); } continue; } #endif if (!strcasecmp(uentp->userid, "guest") && uentp->pid == 1) { u = &(binfo->utmpshm->uinfo[n]); h = utmp_iphash(u->from); i = binfo->utmpshm->guesthash_head[h]; if (i == n + 1) { binfo->utmpshm->guesthash_head[h] = binfo->utmpshm->guesthash_next[i]; } else { while (binfo->utmpshm->guesthash_next[i] != 0) { if (binfo->utmpshm->guesthash_next[i] == n + 1) { binfo->utmpshm-> guesthash_next[i] = binfo->utmpshm-> guesthash_next[n + 1]; break; } else { i = binfo->utmpshm-> guesthash_next[i]; } } } binfo->utmpshm->wwwguestnum--; } binfo->utmpshm->guesthash_next[n + 1] = binfo->utmpshm->guesthash_head[0]; binfo->utmpshm->guesthash_head[0] = n + 1; binfo->utmpshm->activeuser--; remove_uindex(uentp->uid, n + 1); uentp->active = 0; uentp->pid = 0; uentp->invisible = 1; uentp->sockactive = 0; uentp->sockaddr = 0; uentp->destuid = 0; uentp->lasttime = 0; } }