-char CtdlCheckExpress(void) {
- if (CC->FirstExpressMessage == NULL) {
- return(' ');
- }
- else {
- return('*');
- }
-}
-
-void cmd_time(char *argbuf)
-{
- time_t tv;
- struct tm tmp;
-
- tv = time(NULL);
- localtime_r(&tv, &tmp);
-
- /* timezone and daylight global variables are not portable. */
-#ifdef HAVE_STRUCT_TM_TM_GMTOFF
- cprintf("%d %ld|%ld|%d\n", CIT_OK, (long)tv, tmp.tm_gmtoff, tmp.tm_isdst);
-#else
- cprintf("%d %ld|%ld|%d\n", CIT_OK, (long)tv, timezone, tmp.tm_isdst);
-#endif
-}
-
-
-/*
- * Check originating host against the public_clients file. This determines
- * whether the client is allowed to change the hostname for this session
- * (for example, to show the location of the user rather than the location
- * of the client).
- */
-int is_public_client(void)
-{
- char buf[1024];
- char addrbuf[1024];
- FILE *fp;
- int i;
- char *public_clientspos;
- char *public_clientsend;
- char *paddr = NULL;
- struct stat statbuf;
- static time_t pc_timestamp = 0;
- static char public_clients[SIZ];
- static char public_clients_file[SIZ];
-
-#define LOCALHOSTSTR "127.0.0.1"
-
- snprintf(public_clients_file, sizeof public_clients_file, "%s/public_clients", ctdl_etc_dir);
-
- /*
- * Check the time stamp on the public_clients file. If it's been
- * updated since the last time we were here (or if this is the first
- * time we've been through the loop), read its contents and learn
- * the IP addresses of the listed hosts.
- */
- if (stat(public_clients_file, &statbuf) != 0) {
- /* No public_clients file exists, so bail out */
- syslog(LOG_WARNING, "Warning: '%s' does not exist\n",
- public_clients_file);
- return(0);
- }
-
- if (statbuf.st_mtime > pc_timestamp) {
- begin_critical_section(S_PUBLIC_CLIENTS);
- syslog(LOG_INFO, "Loading %s\n", public_clients_file);
-
- public_clientspos = &public_clients[0];
- public_clientsend = public_clientspos + SIZ;
- safestrncpy(public_clientspos, LOCALHOSTSTR, sizeof public_clients);
- public_clientspos += sizeof(LOCALHOSTSTR) - 1;
-
- if (hostname_to_dotted_quad(addrbuf, config.c_fqdn) == 0) {
- *(public_clientspos++) = '|';
- paddr = &addrbuf[0];
- while (!IsEmptyStr (paddr) &&
- (public_clientspos < public_clientsend))
- *(public_clientspos++) = *(paddr++);
- }
-
- fp = fopen(public_clients_file, "r");
- if (fp != NULL)
- while ((fgets(buf, sizeof buf, fp)!=NULL) &&
- (public_clientspos < public_clientsend)){
- char *ptr;
- ptr = buf;
- while (!IsEmptyStr(ptr)) {
- if (*ptr == '#') {
- *ptr = 0;
- break;
- }
- else ptr++;
- }
- ptr--;
- while (ptr>buf && isspace(*ptr)) {
- *(ptr--) = 0;
- }
- if (hostname_to_dotted_quad(addrbuf, buf) == 0) {
- *(public_clientspos++) = '|';
- paddr = addrbuf;
- while (!IsEmptyStr(paddr) &&
- (public_clientspos < public_clientsend)){
- *(public_clientspos++) = *(paddr++);
- }
- }
- }
- fclose(fp);
- pc_timestamp = time(NULL);
- end_critical_section(S_PUBLIC_CLIENTS);
- }
-
- syslog(LOG_DEBUG, "Checking whether %s is a local or public client\n",
- CC->cs_addr);
- for (i=0; i<num_parms(public_clients); ++i) {
- extract_token(addrbuf, public_clients, i, '|', sizeof addrbuf);
- if (!strcasecmp(CC->cs_addr, addrbuf)) {
- syslog(LOG_DEBUG, "... yes it is.\n");
- return(1);
- }
- }
-
- /* No hits. This is not a public client. */
- syslog(LOG_DEBUG, "... no it isn't.\n");
- return(0);
-}
-
-
-/*
- * the client is identifying itself to the server
- */
-void cmd_iden(char *argbuf)
-{
- int dev_code;
- int cli_code;
- int rev_level;
- char desc[128];
- char from_host[128];
-
- if (num_parms(argbuf)<4) {
- cprintf("%d usage error\n", ERROR + ILLEGAL_VALUE);
- return;
- }
-
- dev_code = extract_int(argbuf,0);
- cli_code = extract_int(argbuf,1);
- rev_level = extract_int(argbuf,2);
- extract_token(desc, argbuf, 3, '|', sizeof desc);
-
- safestrncpy(from_host, config.c_fqdn, sizeof from_host);
- from_host[sizeof from_host - 1] = 0;
- if (num_parms(argbuf)>=5) extract_token(from_host, argbuf, 4, '|', sizeof from_host);
-
- CC->cs_clientdev = dev_code;
- CC->cs_clienttyp = cli_code;
- CC->cs_clientver = rev_level;
- safestrncpy(CC->cs_clientname, desc, sizeof CC->cs_clientname);
- CC->cs_clientname[31] = 0;
-
- /* For local sockets and public clients, trust the hostname supplied by the client */
- if ( (CC->is_local_socket) || (is_public_client()) ) {
- safestrncpy(CC->cs_host, from_host, sizeof CC->cs_host);
- CC->cs_host[sizeof CC->cs_host - 1] = 0;
- CC->cs_addr[0] = 0;
- }
-
- syslog(LOG_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);
-}
-
-typedef const char *ccharp;
-/*
- * 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 ccharp*)dirs);
- if (IsEmptyStr(targ)) {
- snprintf(buf2, sizeof buf2, "%s.%d",
- buf, CC->cs_clientdev);
- mesg_locate(targ, sizeof targ, buf2, 2,
- (const ccharp*)dirs);
- if (IsEmptyStr(targ)) {
- mesg_locate(targ, sizeof targ, buf, 2,
- (const ccharp*)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 ccharp*)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,
- CitContext *viewed,
- 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_or_guest) && (CC->logged_in == 0) && (!config.c_guest_logins)) {
- cprintf("%d Not logged in.\n", ERROR + NOT_LOGGED_IN);
- 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 >= AxAideU) 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;
- int terminated = 0;
-
- session_num = extract_int(cmdbuf, 0);
-
- terminated = CtdlTerminateOtherSession(session_num);
-
- if (terminated < 0) {
- cprintf("%d You can't kill your own session.\n", ERROR + ILLEGAL_VALUE);
- return;
- }
-
- if (terminated & TERM_FOUND) {
- if (terminated == TERM_KILLED) {
- 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(char *argbuf) {
- cprintf("%d %s\n", CIT_OK, config.c_moreprompt);
-}
-
-
-/*
- * echo
- */
-void cmd_echo(char *etext)
-{
- cprintf("%d %s\n", CIT_OK, etext);
-}
-
-
-/*
- * Perform privilege escalation for an 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);
- syslog(LOG_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)
- {
- syslog(LOG_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);
- }
- CC->kill_me = 1; /* Even the DOWN command has to follow correct proceedure when disconecting */
- CtdlThreadStopAll();
-}
-
-
-/*
- * Halt the server without exiting the server process.
- */
-void cmd_halt(char *argbuf) {
-
- 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)