- if (do_lookup) {
- CtdlLogPrintf(CTDL_DEBUG, "Looking up hostname '%s'\n", from_host);
- if ((addr.s_addr = inet_addr(from_host)) != -1) {
- locate_host(CC->cs_host, sizeof CC->cs_host,
- CC->cs_addr, sizeof CC->cs_addr,
- &addr);
- }
- else {
- safestrncpy(CC->cs_host, from_host, sizeof CC->cs_host);
- CC->cs_host[sizeof CC->cs_host - 1] = 0;
- }
- }
-
- CtdlLogPrintf(CTDL_NOTICE, "Client %d/%d/%01d.%02d (%s) from %s\n",
- dev_code,
- cli_code,
- (rev_level / 100),
- (rev_level % 100),
- desc,
- CC->cs_host);
- cprintf("%d Ok\n",CIT_OK);
-}
-
-
-/*
- * display system messages or help
- */
-void cmd_mesg(char *mname)
-{
- FILE *mfp;
- char targ[256];
- char buf[256];
- char buf2[256];
- char *dirs[2];
- DIR *dp;
- struct dirent *d;
-
- extract_token(buf, mname, 0, '|', sizeof buf);
-
- dirs[0] = strdup(ctdl_message_dir);
- dirs[1] = strdup(ctdl_hlp_dir);
-
- snprintf(buf2, sizeof buf2, "%s.%d.%d",
- buf, CC->cs_clientdev, CC->cs_clienttyp);
-
- /* If the client requested "?" then produce a listing */
- if (!strcmp(buf, "?")) {
- cprintf("%d %s\n", LISTING_FOLLOWS, buf);
- dp = opendir(dirs[1]);
- if (dp != NULL) {
- while (d = readdir(dp), d != NULL) {
- if (d->d_name[0] != '.') {
- cprintf(" %s\n", d->d_name);
- }
- }
- closedir(dp);
- }
- cprintf("000\n");
- free(dirs[0]);
- free(dirs[1]);
- return;
- }
-
- /* Otherwise, look for the requested file by name. */
- else {
- mesg_locate(targ, sizeof targ, buf2, 2, (const char **)dirs);
- if (IsEmptyStr(targ)) {
- snprintf(buf2, sizeof buf2, "%s.%d",
- buf, CC->cs_clientdev);
- mesg_locate(targ, sizeof targ, buf2, 2,
- (const char **)dirs);
- if (IsEmptyStr(targ)) {
- mesg_locate(targ, sizeof targ, buf, 2,
- (const char **)dirs);
- }
- }
- }
-
- free(dirs[0]);
- free(dirs[1]);
-
- if (IsEmptyStr(targ)) {
- cprintf("%d '%s' not found. (Searching in %s and %s)\n",
- ERROR + FILE_NOT_FOUND,
- mname,
- ctdl_message_dir,
- ctdl_hlp_dir
- );
- return;
- }
-
- mfp = fopen(targ, "r");
- if (mfp==NULL) {
- cprintf("%d Cannot open '%s': %s\n",
- ERROR + INTERNAL_ERROR, targ, strerror(errno));
- return;
- }
- cprintf("%d %s\n", LISTING_FOLLOWS,buf);
-
- while (fgets(buf, (sizeof buf - 1), mfp) != NULL) {
- buf[strlen(buf)-1] = 0;
- do_help_subst(buf);
- cprintf("%s\n",buf);
- }
-
- fclose(mfp);
- cprintf("000\n");
-}
-
-
-/*
- * enter system messages or help
- */
-void cmd_emsg(char *mname)
-{
- FILE *mfp;
- char targ[256];
- char buf[256];
- char *dirs[2];
- int a;
-
- unbuffer_output();
-
- if (CtdlAccessCheck(ac_aide)) return;
-
- extract_token(buf, mname, 0, '|', sizeof buf);
- for (a=0; !IsEmptyStr(&buf[a]); ++a) { /* security measure */
- if (buf[a] == '/') buf[a] = '.';
- }
-
- dirs[0] = strdup(ctdl_message_dir);
- dirs[1] = strdup(ctdl_hlp_dir);
-
- mesg_locate(targ, sizeof targ, buf, 2, (const char**)dirs);
- free(dirs[0]);
- free(dirs[1]);
-
- if (IsEmptyStr(targ)) {
- snprintf(targ, sizeof targ,
- "%s/%s",
- ctdl_hlp_dir, buf);
- }
-
- mfp = fopen(targ,"w");
- if (mfp==NULL) {
- cprintf("%d Cannot open '%s': %s\n",
- ERROR + INTERNAL_ERROR, targ, strerror(errno));
- return;
- }
- cprintf("%d %s\n", SEND_LISTING, targ);
-
- while (client_getln(buf, sizeof buf) >=0 && strcmp(buf, "000")) {
- fprintf(mfp, "%s\n", buf);
- }
-
- fclose(mfp);
-}
-
-
-/* Don't show the names of private rooms unless the viewing
- * user also knows the rooms.
- */
-void GenerateRoomDisplay(char *real_room,
- struct CitContext *viewed,
- struct CitContext *viewer) {
-
- int ra;
-
- strcpy(real_room, viewed->room.QRname);
- if (viewed->room.QRflags & QR_MAILBOX) {
- strcpy(real_room, &real_room[11]);
- }
- if (viewed->room.QRflags & QR_PRIVATE) {
- CtdlRoomAccess(&viewed->room, &viewer->user, &ra, NULL);
- if ( (ra & UA_KNOWN) == 0) {
- strcpy(real_room, "<private room>");
- }
- }
-
- if (viewed->cs_flags & CS_CHAT) {
- while (strlen(real_room) < 14) {
- strcat(real_room, " ");
- }
- strcpy(&real_room[14], "<chat>");
- }
-
-}
-
-/*
- * Convenience function.
- */
-int CtdlAccessCheck(int required_level) {
-
- if (CC->internal_pgm) return(0);
- if (required_level >= ac_internal) {
- cprintf("%d This is not a user-level command.\n",
- ERROR + HIGHER_ACCESS_REQUIRED);
- return(-1);
- }
-
- if ((required_level >= ac_logged_in) && (CC->logged_in == 0)) {
- cprintf("%d Not logged in.\n", ERROR + NOT_LOGGED_IN);
- return(-1);
- }
-
- if (CC->user.axlevel >= 6) return(0);
- if (required_level >= ac_aide) {
- cprintf("%d This command requires Aide access.\n",
- ERROR + HIGHER_ACCESS_REQUIRED);
- return(-1);
- }
-
- if (is_room_aide()) return(0);
- if (required_level >= ac_room_aide) {
- cprintf("%d This command requires Aide or Room Aide access.\n",
- ERROR + HIGHER_ACCESS_REQUIRED);
- return(-1);
- }
-
- /* shhh ... succeed quietly */
- return(0);
-}
-
-
-
-/*
- * Terminate another running session
- */
-void cmd_term(char *cmdbuf)
-{
- int session_num;
- struct CitContext *ccptr;
- int found_it = 0;
- int allowed = 0;
-
- session_num = extract_int(cmdbuf, 0);
- if (session_num == CC->cs_pid) {
- cprintf("%d You can't kill your own session.\n", ERROR + ILLEGAL_VALUE);
- return;
- }
-
- CtdlLogPrintf(CTDL_DEBUG, "Locating session to kill\n");
- begin_critical_section(S_SESSION_TABLE);
- for (ccptr = ContextList; ccptr != NULL; ccptr = ccptr->next) {
- if (session_num == ccptr->cs_pid) {
- found_it = 1;
- if ((ccptr->user.usernum == CC->user.usernum)
- || (CC->user.axlevel >= 6)) {
- allowed = 1;
- ccptr->kill_me = 1;
- }
- else {
- allowed = 0;
- }
- }
- }
- end_critical_section(S_SESSION_TABLE);
-
- if (found_it) {
- if (allowed) {
- cprintf("%d Session terminated.\n", CIT_OK);
- }
- else {
- cprintf("%d You are not allowed to do that.\n",
- ERROR + HIGHER_ACCESS_REQUIRED);
- }
- }
- else {
- cprintf("%d No such session.\n", ERROR + ILLEGAL_VALUE);
- }
-}
-
-
-
-
-
-/*
- * get the paginator prompt
- */
-void cmd_more(void) {
- cprintf("%d %s\n", CIT_OK, config.c_moreprompt);
-}
-
-/*
- * echo
- */
-void cmd_echo(char *etext)
-{
- cprintf("%d %s\n", CIT_OK, etext);
-}
-
-
-
-/*
- * identify as internal program
- */
-void cmd_ipgm(char *argbuf)
-{
- int secret;
-
- secret = extract_int(argbuf, 0);
-
- /* For security reasons, we do NOT allow this command to run
- * over the network. Local sockets only.
- */
- if (!CC->is_local_socket) {
- sleep(5);
- cprintf("%d Authentication failed.\n",
- ERROR + PASSWORD_REQUIRED);
- }
- else if (secret == config.c_ipgm_secret) {
- CC->internal_pgm = 1;
- strcpy(CC->curr_user, "<internal program>");
- CC->cs_flags = CC->cs_flags|CS_STEALTH;
- cprintf("%d Authenticated as an internal program.\n", CIT_OK);
- }
- else {
- sleep(5);
- cprintf("%d Authentication failed.\n",
- ERROR + PASSWORD_REQUIRED);
- CtdlLogPrintf(CTDL_ERR, "Warning: ipgm authentication failed.\n");
- CC->kill_me = 1;
- }
-}
-
-
-/*
- * Shut down the server
- */
-void cmd_down(char *argbuf) {
- char *Reply ="%d Shutting down server. Goodbye.\n";
-
- if (CtdlAccessCheck(ac_aide)) return;
-
- if (!IsEmptyStr(argbuf))
- {
- int state = CIT_OK;
- restart_server = extract_int(argbuf, 0);
-
- if (restart_server > 0)
- {
- Reply = "%d citserver will now shut down and automatically restart.\n";
- }
- if ((restart_server > 0) && !running_as_daemon)
- {
- CtdlLogPrintf(CTDL_ERR, "The user requested restart, but not running as daemon! Geronimooooooo!\n");
- Reply = "%d Warning: citserver is not running in daemon mode and is therefore unlikely to restart automatically.\n";
- state = ERROR;
- }
- cprintf(Reply, state);
- }
- else
- {
- cprintf(Reply, CIT_OK + SERVER_SHUTTING_DOWN);
- }
- CtdlThreadStopAll();
-}
-
-/*
- * Halt the server without exiting the server process.
- */
-void cmd_halt(void) {
-
- if (CtdlAccessCheck(ac_aide)) return;
-
- cprintf("%d Halting server. Goodbye.\n", CIT_OK);
- CtdlThreadStopAll();
- shutdown_and_halt = 1;
-}
-
-/*
- * Schedule or cancel a server shutdown
- */
-void cmd_scdn(char *argbuf)
-{
- int new_state;
- int state = CIT_OK;
- char *Reply = "%d %d\n";
-
- if (CtdlAccessCheck(ac_aide)) return;
-
- new_state = extract_int(argbuf, 0);
- if ((new_state == 2) || (new_state == 3))
- {
- restart_server = 1;
- if (!running_as_daemon)
- {
- CtdlLogPrintf(CTDL_ERR, "The user requested restart, but not running as deamon! Geronimooooooo!\n");
- Reply = "%d %d Warning, not running in deamon mode. maybe we will come up again, but don't lean on it.\n";
- state = ERROR;
- }
-
- restart_server = extract_int(argbuf, 0);
- new_state -= 2;
- }
- if ((new_state == 0) || (new_state == 1)) {
- ScheduledShutdown = new_state;
- }
- cprintf(Reply, state, ScheduledShutdown);
-}
-
-
-/*
- * Set or unset asynchronous protocol mode
- */
-void cmd_asyn(char *argbuf)
-{
- int new_state;
-
- new_state = extract_int(argbuf, 0);
- if ((new_state == 0) || (new_state == 1)) {
- CC->is_async = new_state;
- }
- cprintf("%d %d\n", CIT_OK, CC->is_async);
-}
-
-
-/*
- * Generate a "nonce" for APOP-style authentication.
- *
- * RFC 1725 et al specify a PID to be placed in front of the nonce.
- * Quoth BTX: That would be stupid.
- */
-void generate_nonce(struct CitContext *con) {
- struct timeval tv;
-
- memset(con->cs_nonce, NONCE_SIZE, 0);
- gettimeofday(&tv, NULL);
- memset(con->cs_nonce, NONCE_SIZE, 0);
- snprintf(con->cs_nonce, NONCE_SIZE, "<%d%ld@%s>",
- rand(), (long)tv.tv_usec, config.c_fqdn);
-}
-
-
-
-
-/*
- * Back-end function for starting a session
- */
-void begin_session(struct CitContext *con)
-{
- socklen_t len;
- struct sockaddr_in sin;
-
- /*
- * Initialize some variables specific to our context.
- */
- con->logged_in = 0;
- con->internal_pgm = 0;
- con->download_fp = NULL;
- con->upload_fp = NULL;
- con->FirstExpressMessage = NULL;
- time(&con->lastcmd);
- time(&con->lastidle);
- strcpy(con->lastcmdname, " ");
- strcpy(con->cs_clientname, "(unknown)");
- strcpy(con->curr_user, NLI);
- *con->net_node = '\0';
- *con->fake_username = '\0';
- *con->fake_hostname = '\0';
- *con->fake_roomname = '\0';
- generate_nonce(con);
- safestrncpy(con->cs_host, config.c_fqdn, sizeof con->cs_host);
- safestrncpy(con->cs_addr, "", sizeof con->cs_addr);
- con->cs_host[sizeof con->cs_host - 1] = 0;
- len = sizeof sin;
- if (!CC->is_local_socket) {
- if (!getpeername(con->client_socket, (struct sockaddr *) &sin, &len)) {
- locate_host(con->cs_host, sizeof con->cs_host,
- con->cs_addr, sizeof con->cs_addr,
- &sin.sin_addr
- );
- }
- }
- else {
- strcpy(con->cs_host, "");
- }
- con->cs_flags = 0;
- con->upload_type = UPL_FILE;
- con->dl_is_net = 0;
-
- con->nologin = 0;
- if (((config.c_maxsessions > 0)&&(num_sessions > config.c_maxsessions)) || CtdlWantSingleUser()) {
- con->nologin = 1;
- }
-
- if (!CC->is_local_socket) {
- CtdlLogPrintf(CTDL_NOTICE, "Session started from %s [%s].\n", con->cs_host, con->cs_addr);
- }
- else {
- CtdlLogPrintf(CTDL_NOTICE, "Session started via local socket.\n");
- }
-
- /* Run any session startup routines registered by loadable modules */
- PerformSessionHooks(EVT_START);
-}