void message_handler(int id, char **msg, uint32_t count, Type type, short int *revents) { switch (type) { case TYPE__text: if (!clients[id].isLogin) { if (strlen(msg[0]) == 0) { kick_user(clients[id], recv_msg[LOGIN_INCORRECT]); *revents |= POLLHUP; } else if (!log_in(id, msg[0])) { kick_user(clients[id], recv_msg[LOGIN_EXIST]); *revents |= POLLHUP; } return; } break; case TYPE__list: send_client_list(id); return; case TYPE__dc: kick_user(clients[id], "* disconnect"); *revents |= POLLHUP; return; default: break; } for (uint32_t i = 0; i < count; i++) { printf("%s: %s\n", clients[id].nickName, msg[i]); broadcast(id, msg[i]); } }
void check_heartbeat_table(){ int i; for (i=0; i<MAX_USERS_NUMBER; ++i) { if ( (LOCAL_REPO[i].client_id != -1) && ((time(0) - LOCAL_REPO[i].hb_send_time) > 3) ) { kick_user(LOCAL_REPO[i].user_name, LOCAL_REPO[i].client_id); } } }
int main(int argc, char* argv[]) { ip_addr = "127.0.0.1"; port_num = 3490; pthread_mutex_init(&mutex, NULL); struct sockaddr_in sin = { 0 }; int socket_fd; inet_pton(AF_INET, ip_addr, &sin.sin_addr); sin.sin_family = AF_INET; sin.sin_port = htons(port_num); pthread_t serv_thread; if ((socket_fd = socket(AF_INET, SOCK_STREAM, 0)) == -1) { perror("Creating socket"); return 1; } int n = 1; setsockopt(socket_fd, SOL_SOCKET, SO_REUSEADDR, &n, sizeof(int)); if (bind(socket_fd, (struct sockaddr *) &sin, sizeof(sin)) == -1) { perror("Binding socket"); return 1; } if (listen(socket_fd, 2) == -1) { perror("Listening"); return 1; } printf("Listening for incoming connections...\n"); if (pthread_create(&serv_thread, NULL, server_session, &socket_fd) != 0) { perror("* Creating the listener thread"); return -1; } while (true) { char buff[MAX_MESSAGE_LENGTH]; memset(buff, 0, sizeof(buff)); fgets(buff, MAX_MESSAGE_LENGTH, stdin); if (strncmp(buff, "!kick", 5) == 0) { char login[MAX_MESSAGE_LENGTH]; strncpy(login, buff + 6, MAX_MESSAGE_LENGTH - 5); printf("%s\n", login); for (int i = 0; i < MAX_CLIENTS; i++) { if (clients[i].isAlive && (strncmp(login, clients[i].nickName, strnlen(clients[i].nickName, LOGIN_LENGTH)) == 0)) { kick_user(clients[i], "* Kicked by admin\n"); break; } } } else if (strncmp(buff, "!exit", 5)) { exit(0); } } }
void handle_request(void* received, int msg_type){ MSG_REQUEST temp = *(MSG_REQUEST*)(received); if (temp.request_type == PONG) { if ( time(0) - get_user_hbtime(temp.user_name) > 1){ kick_user(temp.user_name, get_user_id(temp.user_name)); } } else { MSG_USERS_LIST *list = get_list(temp.request_type); msgsnd(get_user_id(temp.user_name), list, _size(MSG_USERS_LIST), 0); free(list); } }
int main(int argc, const char **argv) { int ret = EXIT_SUCCESS; struct tools_ctx *tctx = NULL; const char *pc_username = NULL; int pc_debug = SSSDBG_DEFAULT; int pc_remove = 0; int pc_force = 0; int pc_kick = 0; poptContext pc = NULL; struct poptOption long_options[] = { POPT_AUTOHELP { "debug", '\0', POPT_ARG_INT | POPT_ARGFLAG_DOC_HIDDEN, &pc_debug, 0, _("The debug level to run with"), NULL }, { "remove", 'r', POPT_ARG_NONE, NULL, 'r', _("Remove home directory and mail spool"), NULL }, { "no-remove", 'R', POPT_ARG_NONE, NULL, 'R', _("Do not remove home directory and mail spool"), NULL }, { "force", 'f', POPT_ARG_NONE, NULL, 'f', _("Force removal of files not owned by the user"), NULL }, { "kick", 'k', POPT_ARG_NONE, NULL, 'k', _("Kill users' processes before removing him"), NULL }, POPT_TABLEEND }; debug_prg_name = argv[0]; ret = set_locale(); if (ret != EOK) { DEBUG(1, ("set_locale failed (%d): %s\n", ret, strerror(ret))); ERROR("Error setting the locale\n"); ret = EXIT_FAILURE; goto fini; } /* parse parameters */ pc = poptGetContext(NULL, argc, argv, long_options, 0); poptSetOtherOptionHelp(pc, "USERNAME"); while ((ret = poptGetNextOpt(pc)) > 0) { switch (ret) { case 'r': pc_remove = DO_REMOVE_HOME; break; case 'R': pc_remove = DO_NOT_REMOVE_HOME; break; case 'f': pc_force = DO_FORCE_REMOVAL; break; case 'k': pc_kick = 1; break; } } debug_level = debug_convert_old_level(pc_debug); if (ret != -1) { BAD_POPT_PARAMS(pc, poptStrerror(ret), ret, fini); } pc_username = poptGetArg(pc); if (pc_username == NULL) { BAD_POPT_PARAMS(pc, _("Specify user to delete\n"), ret, fini); } CHECK_ROOT(ret, debug_prg_name); ret = init_sss_tools(&tctx); if (ret != EOK) { DEBUG(1, ("init_sss_tools failed (%d): %s\n", ret, strerror(ret))); if (ret == ENOENT) { ERROR("Error initializing the tools - no local domain\n"); } else { ERROR("Error initializing the tools\n"); } ret = EXIT_FAILURE; goto fini; } /* if the domain was not given as part of FQDN, default to local domain */ ret = parse_name_domain(tctx, pc_username); if (ret != EOK) { ERROR("Invalid domain specified in FQDN\n"); ret = EXIT_FAILURE; goto fini; } /* * Fills in defaults for ops_ctx user did not specify. */ ret = userdel_defaults(tctx, tctx->confdb, tctx->octx, pc_remove); if (ret != EOK) { ERROR("Cannot set default values\n"); ret = EXIT_FAILURE; goto fini; } ret = sysdb_getpwnam_sync(tctx, tctx->sysdb, tctx->octx->name, tctx->octx); if (ret != EOK) { /* Error message will be printed in the switch */ goto done; } if ((tctx->octx->uid < tctx->local->id_min) || (tctx->local->id_max && tctx->octx->uid > tctx->local->id_max)) { ERROR("User %1$s is outside the defined ID range for domain\n", tctx->octx->name); ret = EXIT_FAILURE; goto fini; } if (pc_kick) { ret = kick_user(tctx); if (ret != EOK) { tctx->error = ret; goto done; } } /* userdel */ ret = userdel(tctx, tctx->sysdb, tctx->octx); if (ret != EOK) { goto done; } /* Set SELinux login context - must be done after transaction is done * b/c libselinux calls getpwnam */ ret = del_seuser(tctx->octx->name); if (ret != EOK) { ERROR("Cannot reset SELinux login context\n"); ret = EXIT_FAILURE; goto fini; } if (!pc_kick) { ret = is_logged_in(tctx, tctx->octx->uid); switch(ret) { case ENOENT: break; case EOK: ERROR("WARNING: The user (uid %1$lu) was still logged in when " "deleted.\n", (unsigned long) tctx->octx->uid); break; case ENOSYS: ERROR("Cannot determine if the user was logged in on this " "platform"); break; default: ERROR("Error while checking if the user was logged in\n"); break; } } ret = run_userdel_cmd(tctx); if (ret != EOK) { ERROR("The post-delete command failed: %1$s\n", strerror(ret)); goto fini; } if (tctx->octx->remove_homedir) { ret = remove_homedir(tctx, tctx->octx->home, tctx->octx->maildir, tctx->octx->name, tctx->octx->uid, pc_force); if (ret == EPERM) { ERROR("Not removing home dir - not owned by user\n"); } else if (ret != EOK) { ERROR("Cannot remove homedir: %1$s\n", strerror(ret)); ret = EXIT_FAILURE; goto fini; } } done: if (ret) { DEBUG(1, ("sysdb operation failed (%d)[%s]\n", ret, strerror(ret))); switch (ret) { case ENOENT: ERROR("No such user in local domain. " "Removing users only allowed in local domain.\n"); break; default: ERROR("Internal error. Could not remove user.\n"); break; } ret = EXIT_FAILURE; goto fini; } ret = EXIT_SUCCESS; fini: talloc_free(tctx); poptFreeContext(pc); exit(ret); }
void errproto_handler(int id, short int *revents) { kick_user(clients[id], recv_msg[ERR_PROTO]); *revents |= POLLERR; }