2004-01-04 03:54:01 +01:00
|
|
|
/* dgamelaunch.c
|
2003-12-30 22:30:32 +01:00
|
|
|
*
|
2004-01-25 23:34:35 +01:00
|
|
|
* (c)2001-4 M. Drew Streib <dtype@dtype.org>
|
|
|
|
* also parts (c) 2003-4 Joshua Kwan <joshk@triplehelix.org>,
|
2004-01-04 03:54:01 +01:00
|
|
|
* Brett Carrington <brettcar@segvio.org>,
|
|
|
|
* Jilles Tjoelker <jilles@stack.nl>
|
|
|
|
*
|
2003-12-30 22:30:32 +01:00
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
2004-01-04 03:54:01 +01:00
|
|
|
* See this program in action at http://alt.org/nethack/
|
2003-12-30 22:30:32 +01:00
|
|
|
*
|
|
|
|
* This is a little wrapper for nethack (and soon other programs) that
|
|
|
|
* will allow them to be run from a telnetd session, chroot, shed privs,
|
|
|
|
* make a simple login, then play the game.
|
|
|
|
*/
|
|
|
|
|
2004-01-02 05:35:46 +01:00
|
|
|
#define _GNU_SOURCE
|
2004-01-30 03:49:29 +01:00
|
|
|
|
2003-12-31 03:39:10 +01:00
|
|
|
#include "dgamelaunch.h"
|
2004-01-30 03:49:29 +01:00
|
|
|
#include "ttyplay.h"
|
|
|
|
#include "ttyrec.h"
|
2003-12-31 03:39:10 +01:00
|
|
|
|
2003-12-30 22:30:32 +01:00
|
|
|
/* a request from the author: please leave some remnance of
|
|
|
|
* 'based on dgamelaunch version xxx' in any derivative works, or
|
|
|
|
* even keep the line the same altogether. I'm probably happy
|
|
|
|
* to make any changes you need. */
|
|
|
|
|
|
|
|
/* ************************************************************* */
|
|
|
|
/* ************************************************************* */
|
|
|
|
/* ************************************************************* */
|
|
|
|
|
|
|
|
/* program stuff */
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
2004-01-02 21:47:53 +01:00
|
|
|
#include <sys/time.h>
|
2004-01-23 19:14:48 +01:00
|
|
|
#include <sys/wait.h>
|
2004-01-25 23:34:35 +01:00
|
|
|
#include <sys/ioctl.h> /* ttyrec */
|
|
|
|
#include <sys/stat.h>
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <curses.h>
|
2004-01-02 21:47:53 +01:00
|
|
|
|
|
|
|
#ifndef __FreeBSD__
|
|
|
|
# include <crypt.h>
|
|
|
|
#else
|
|
|
|
# include <libutil.h>
|
|
|
|
#endif
|
|
|
|
|
2004-01-02 22:00:54 +01:00
|
|
|
#ifdef __linux__
|
|
|
|
# include <pty.h>
|
2004-01-03 03:10:35 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef ARRAY_SIZE
|
2004-01-31 09:41:00 +01:00
|
|
|
# define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0]))
|
2004-01-02 22:00:54 +01:00
|
|
|
#endif
|
|
|
|
|
2004-01-12 18:32:47 +01:00
|
|
|
#include <fcntl.h>
|
2004-01-05 02:25:38 +01:00
|
|
|
#include <pwd.h>
|
2004-01-02 22:00:54 +01:00
|
|
|
#include <grp.h>
|
2004-01-02 21:47:53 +01:00
|
|
|
#include <time.h>
|
2003-12-30 22:30:32 +01:00
|
|
|
#include <errno.h>
|
|
|
|
#include <dirent.h>
|
|
|
|
#include <string.h>
|
2004-01-02 21:47:53 +01:00
|
|
|
#include <signal.h>
|
2003-12-30 22:30:32 +01:00
|
|
|
#include <assert.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <termios.h>
|
|
|
|
|
2004-01-05 02:25:38 +01:00
|
|
|
#include "y.tab.h"
|
|
|
|
extern FILE* yyin;
|
|
|
|
extern int yyparse ();
|
|
|
|
|
2004-01-25 23:44:34 +01:00
|
|
|
extern int editor_main (int argc, char **argv);
|
2003-12-30 22:30:32 +01:00
|
|
|
|
|
|
|
/* global variables */
|
|
|
|
|
2004-01-05 02:25:38 +01:00
|
|
|
struct dg_config *myconfig = NULL;
|
|
|
|
char* config = NULL;
|
2004-01-31 07:23:02 +01:00
|
|
|
int silent = 0;
|
2004-01-05 02:25:38 +01:00
|
|
|
|
|
|
|
struct dg_config defconfig = {
|
2004-01-30 18:32:12 +01:00
|
|
|
/* chroot = */ "/var/lib/dgamelaunch/",
|
|
|
|
/* nethack = */ "/bin/nethack",
|
|
|
|
/* dglroot = */ "/dgldir/",
|
|
|
|
/* lockfile = */ "/dgl-lock",
|
|
|
|
/* passwd = */ "/dgl-login",
|
|
|
|
/* banner = */ "/dgl-banner",
|
|
|
|
/* rcfile = */ "/dgl-default-rcfile",
|
|
|
|
/* spool = */ "/var/mail/",
|
|
|
|
/* shed_user = */ "games",
|
|
|
|
/* shed_group = */ "games",
|
|
|
|
/* shed_uid = */ 5,
|
|
|
|
/* shed_gid = */ 60, /* games:games in Debian */
|
|
|
|
/* max = */ 64000
|
2004-01-05 02:25:38 +01:00
|
|
|
};
|
|
|
|
|
2004-02-01 09:20:28 +01:00
|
|
|
int set_max = 0; /* XXX */
|
2003-12-30 22:30:32 +01:00
|
|
|
int loggedin = 0;
|
|
|
|
char rcfilename[80];
|
2003-12-31 02:58:56 +01:00
|
|
|
char *chosen_name;
|
2003-12-30 22:30:32 +01:00
|
|
|
|
|
|
|
int f_num = 0;
|
2003-12-31 02:58:56 +01:00
|
|
|
struct dg_user **users = NULL;
|
|
|
|
struct dg_user *me = NULL;
|
2003-12-31 20:47:49 +01:00
|
|
|
struct dg_banner banner;
|
2003-12-30 22:30:32 +01:00
|
|
|
|
2004-01-16 03:02:40 +01:00
|
|
|
#if !(defined(__linux__) || defined(BSD))
|
2004-01-16 03:00:47 +01:00
|
|
|
int
|
|
|
|
mysetenv (const char* name, const char* value, int overwrite)
|
|
|
|
{
|
|
|
|
int retval;
|
|
|
|
char *buf = NULL;
|
|
|
|
|
|
|
|
if (getenv(name) == NULL || overwrite)
|
|
|
|
{
|
|
|
|
size_t len = strlen(name) + 1 + strlen(value) + 1; /* NAME=VALUE\0 */
|
|
|
|
buf = malloc(len);
|
|
|
|
snprintf(buf, len, "%s=%s", name, value);
|
|
|
|
retval = putenv(buf);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
retval = -1;
|
|
|
|
|
|
|
|
return retval;
|
|
|
|
}
|
2004-01-23 17:40:51 +01:00
|
|
|
#else /* use native setenv */
|
|
|
|
# define mysetenv setenv
|
2004-01-16 03:02:40 +01:00
|
|
|
#endif /* !linux && !bsd */
|
2004-01-16 03:00:47 +01:00
|
|
|
|
2004-01-05 02:25:38 +01:00
|
|
|
void
|
|
|
|
create_config ()
|
|
|
|
{
|
|
|
|
FILE *config_file = NULL;
|
|
|
|
|
|
|
|
if (config)
|
|
|
|
{
|
|
|
|
if ((config_file = fopen(config, "r")) != NULL)
|
|
|
|
{
|
|
|
|
yyin = config_file;
|
|
|
|
yyparse();
|
|
|
|
fclose(config_file);
|
|
|
|
free (config);
|
|
|
|
}
|
2004-01-06 06:03:30 +01:00
|
|
|
else
|
|
|
|
{
|
2004-01-26 17:50:13 +01:00
|
|
|
fprintf(stderr, "ERROR: can't find or open %s for reading\n", config);
|
|
|
|
graceful_exit(104);
|
2004-01-06 06:03:30 +01:00
|
|
|
return;
|
|
|
|
}
|
2004-01-05 02:25:38 +01:00
|
|
|
|
2004-02-01 10:25:22 +01:00
|
|
|
if (!myconfig) /* a parse error occurred */
|
|
|
|
{
|
|
|
|
myconfig = &defconfig;
|
|
|
|
return;
|
|
|
|
}
|
2004-01-05 02:25:38 +01:00
|
|
|
/* Fill the rest with defaults */
|
2004-01-21 02:37:16 +01:00
|
|
|
if (!myconfig->shed_user && myconfig->shed_uid == -1)
|
2004-01-05 02:25:38 +01:00
|
|
|
{
|
|
|
|
struct passwd *pw;
|
|
|
|
if ((pw = getpwnam(defconfig.shed_user)))
|
|
|
|
myconfig->shed_uid = pw->pw_uid;
|
|
|
|
else
|
|
|
|
myconfig->shed_uid = defconfig.shed_uid;
|
|
|
|
}
|
|
|
|
|
2004-01-21 02:37:16 +01:00
|
|
|
if (!myconfig->shed_group && myconfig->shed_gid == -1)
|
2004-01-05 02:25:38 +01:00
|
|
|
{
|
|
|
|
struct group *gr;
|
|
|
|
if ((gr = getgrnam(defconfig.shed_group)))
|
|
|
|
myconfig->shed_gid = gr->gr_gid;
|
|
|
|
else
|
|
|
|
myconfig->shed_gid = defconfig.shed_gid;
|
|
|
|
}
|
|
|
|
|
2004-02-01 09:20:28 +01:00
|
|
|
if (myconfig->max == 0 && !set_max) myconfig->max = defconfig.max;
|
2004-01-21 17:47:30 +01:00
|
|
|
if (!myconfig->banner) myconfig->banner = defconfig.banner;
|
|
|
|
if (!myconfig->chroot) myconfig->chroot = defconfig.chroot;
|
|
|
|
if (!myconfig->nethack) myconfig->nethack = defconfig.nethack;
|
|
|
|
if (!myconfig->dglroot) myconfig->dglroot = defconfig.dglroot;
|
|
|
|
if (!myconfig->rcfile) myconfig->rcfile = defconfig.rcfile;
|
|
|
|
if (!myconfig->spool) myconfig->spool = defconfig.spool;
|
|
|
|
if (!myconfig->passwd) myconfig->passwd = defconfig.passwd;
|
|
|
|
if (!myconfig->lockfile) myconfig->lockfile = defconfig.lockfile;
|
2004-01-05 02:25:38 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
myconfig = &defconfig;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-12-30 22:30:32 +01:00
|
|
|
/* ************************************************************* */
|
|
|
|
/* for ttyrec */
|
|
|
|
|
2004-01-05 02:25:38 +01:00
|
|
|
|
2003-12-30 22:30:32 +01:00
|
|
|
void
|
|
|
|
ttyrec_getmaster ()
|
|
|
|
{
|
2003-12-31 20:47:49 +01:00
|
|
|
(void) tcgetattr (0, &tt);
|
|
|
|
(void) ioctl (0, TIOCGWINSZ, (char *) &win);
|
2004-01-02 21:47:53 +01:00
|
|
|
#ifdef USE_OPENPTY
|
2004-01-03 04:51:29 +01:00
|
|
|
if (openpty (&master, &slave, NULL, &tt, &win) == -1)
|
2004-01-02 21:47:53 +01:00
|
|
|
#else
|
2003-12-31 20:47:49 +01:00
|
|
|
if ((master = open ("/dev/ptmx", O_RDWR)) < 0)
|
2004-01-02 21:47:53 +01:00
|
|
|
#endif
|
2004-01-04 02:45:21 +01:00
|
|
|
graceful_exit (62);
|
2003-12-30 22:30:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* ************************************************************* */
|
|
|
|
|
2004-01-25 23:34:35 +01:00
|
|
|
char*
|
2003-12-30 22:30:32 +01:00
|
|
|
gen_ttyrec_filename ()
|
|
|
|
{
|
2003-12-31 20:47:49 +01:00
|
|
|
time_t rawtime;
|
|
|
|
struct tm *ptm;
|
2004-01-25 23:34:35 +01:00
|
|
|
char *ttyrec_filename = calloc(100, sizeof(char));
|
2003-12-31 20:47:49 +01:00
|
|
|
|
|
|
|
/* append time to filename */
|
|
|
|
time (&rawtime);
|
|
|
|
ptm = gmtime (&rawtime);
|
|
|
|
snprintf (ttyrec_filename, 100, "%04i-%02i-%02i.%02i:%02i:%02i.ttyrec",
|
|
|
|
ptm->tm_year + 1900, ptm->tm_mon + 1, ptm->tm_mday,
|
|
|
|
ptm->tm_hour, ptm->tm_min, ptm->tm_sec);
|
2004-01-25 23:34:35 +01:00
|
|
|
return ttyrec_filename;
|
2003-12-30 22:30:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* ************************************************************* */
|
|
|
|
|
2004-01-28 17:31:37 +01:00
|
|
|
char*
|
2004-01-25 23:34:35 +01:00
|
|
|
gen_inprogress_lock (pid_t pid, char* ttyrec_filename)
|
2003-12-30 22:30:32 +01:00
|
|
|
{
|
2004-01-28 17:31:37 +01:00
|
|
|
char *lockfile = NULL, pidbuf[16];
|
2003-12-31 20:47:49 +01:00
|
|
|
int fd;
|
2004-01-28 17:31:37 +01:00
|
|
|
size_t len;
|
2004-01-30 18:32:12 +01:00
|
|
|
struct flock fl = { 0 };
|
2004-01-04 01:59:48 +01:00
|
|
|
|
2004-01-04 03:35:40 +01:00
|
|
|
snprintf (pidbuf, 16, "%d", pid);
|
2004-01-04 03:19:12 +01:00
|
|
|
|
2004-01-30 18:32:12 +01:00
|
|
|
fl.l_type = F_WRLCK;
|
|
|
|
fl.l_whence = SEEK_SET;
|
|
|
|
fl.l_start = 0;
|
|
|
|
fl.l_len = 0;
|
|
|
|
|
2004-01-28 17:31:37 +01:00
|
|
|
len = strlen(myconfig->dglroot) + strlen(me->username) + strlen(ttyrec_filename) + 13;
|
|
|
|
lockfile = calloc(len, sizeof(char));
|
|
|
|
|
|
|
|
snprintf (lockfile, len, "%sinprogress/%s:%s", myconfig->dglroot,
|
2003-12-31 20:47:49 +01:00
|
|
|
me->username, ttyrec_filename);
|
2003-12-30 22:30:32 +01:00
|
|
|
|
2003-12-31 20:47:49 +01:00
|
|
|
fd = open (lockfile, O_WRONLY | O_CREAT, 0644);
|
2004-01-04 01:59:48 +01:00
|
|
|
if (fcntl (fd, F_SETLKW, &fl) == -1)
|
2004-01-04 02:45:21 +01:00
|
|
|
graceful_exit (68);
|
2004-01-04 03:03:26 +01:00
|
|
|
|
2004-01-04 03:19:12 +01:00
|
|
|
write (fd, pidbuf, strlen (pidbuf));
|
2004-01-28 17:31:37 +01:00
|
|
|
|
|
|
|
return lockfile;
|
2003-12-30 22:30:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* ************************************************************* */
|
|
|
|
|
|
|
|
void
|
2004-01-02 07:14:34 +01:00
|
|
|
catch_sighup (int signum)
|
2003-12-30 22:30:32 +01:00
|
|
|
{
|
2004-01-25 23:34:35 +01:00
|
|
|
if (child)
|
2003-12-31 20:47:49 +01:00
|
|
|
{
|
|
|
|
sleep (10);
|
2004-01-25 23:34:35 +01:00
|
|
|
kill (child, SIGHUP);
|
2003-12-31 20:47:49 +01:00
|
|
|
sleep (5);
|
|
|
|
}
|
2004-01-04 02:45:21 +01:00
|
|
|
graceful_exit (2);
|
2003-12-30 22:30:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* ************************************************************* */
|
|
|
|
|
2003-12-31 20:47:49 +01:00
|
|
|
void
|
|
|
|
loadbanner (struct dg_banner *ban)
|
|
|
|
{
|
2003-12-31 21:21:00 +01:00
|
|
|
FILE *bannerfile;
|
2003-12-31 20:47:49 +01:00
|
|
|
char buf[80];
|
|
|
|
|
|
|
|
memset (buf, 0, 80);
|
|
|
|
|
2004-01-05 02:55:08 +01:00
|
|
|
bannerfile = fopen (myconfig->banner, "r");
|
2003-12-31 20:47:49 +01:00
|
|
|
|
2003-12-31 21:21:00 +01:00
|
|
|
if (!bannerfile)
|
2003-12-31 20:47:49 +01:00
|
|
|
{
|
2004-01-05 02:55:08 +01:00
|
|
|
size_t len;
|
2003-12-31 20:47:49 +01:00
|
|
|
ban->len = 2;
|
|
|
|
ban->lines = malloc (sizeof (char *));
|
|
|
|
ban->lines[0] =
|
|
|
|
strdup ("### dgamelaunch " VERSION
|
|
|
|
" - network console game launcher");
|
2004-01-05 02:55:08 +01:00
|
|
|
len = strlen(myconfig->banner) + ARRAY_SIZE("### NOTE: administrator has not installed a file");
|
|
|
|
ban->lines[1] = malloc(len);
|
|
|
|
snprintf(ban->lines[1], len, "### NOTE: administrator has not installed a %s file", myconfig->banner);
|
2003-12-31 20:47:49 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
ban->len = 0;
|
|
|
|
|
2003-12-31 21:21:00 +01:00
|
|
|
while (fgets (buf, 80, bannerfile) != NULL)
|
2003-12-31 20:47:49 +01:00
|
|
|
{
|
2004-01-06 03:08:53 +01:00
|
|
|
char *loc, *b = buf;
|
|
|
|
char bufnew[80];
|
|
|
|
|
|
|
|
memset (bufnew, 0, 80);
|
2003-12-31 20:47:49 +01:00
|
|
|
|
|
|
|
ban->len++;
|
|
|
|
ban->lines = realloc (ban->lines, sizeof (char *) * ban->len);
|
|
|
|
|
2004-01-06 03:08:53 +01:00
|
|
|
if (strstr(b, "$VERSION"))
|
|
|
|
{
|
|
|
|
int i = 0;
|
|
|
|
while ((loc = strstr (b, "$VERSION")) != NULL)
|
|
|
|
{
|
|
|
|
for (; i < 80; i++)
|
2003-12-31 20:47:49 +01:00
|
|
|
{
|
|
|
|
if (loc != b)
|
|
|
|
bufnew[i] = *(b++);
|
|
|
|
else
|
|
|
|
{
|
2004-01-06 03:08:53 +01:00
|
|
|
strlcat (bufnew, VERSION, 80);
|
2003-12-31 20:47:49 +01:00
|
|
|
b += 8; /* skip the whole $VERSION string */
|
2004-01-06 03:08:53 +01:00
|
|
|
i += ARRAY_SIZE (VERSION) - 1;
|
|
|
|
break;
|
2003-12-31 20:47:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (strlen (b) == 0)
|
|
|
|
break;
|
2004-01-06 03:08:53 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (*b)
|
|
|
|
strlcat(bufnew, b, 80);
|
|
|
|
|
|
|
|
ban->lines[ban->len - 1] = strdup (bufnew);
|
|
|
|
}
|
2003-12-31 20:47:49 +01:00
|
|
|
else
|
|
|
|
ban->lines[ban->len - 1] = strdup (buf);
|
|
|
|
|
|
|
|
memset (buf, 0, 80);
|
2004-01-01 03:06:19 +01:00
|
|
|
|
2004-02-05 20:38:17 +01:00
|
|
|
if (ban->len == 11) /* menu itself needs 13 lines, 24 - 11 */
|
2004-01-06 06:03:30 +01:00
|
|
|
break;
|
|
|
|
}
|
2004-01-01 03:06:19 +01:00
|
|
|
|
|
|
|
fclose (bannerfile);
|
2003-12-31 20:47:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
drawbanner (unsigned int start_line, unsigned int howmany)
|
|
|
|
{
|
|
|
|
static short loaded_banner = 0;
|
|
|
|
unsigned int i;
|
|
|
|
|
|
|
|
if (!loaded_banner)
|
|
|
|
{
|
|
|
|
loadbanner (&banner);
|
|
|
|
loaded_banner = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (howmany > banner.len || howmany == 0)
|
|
|
|
howmany = banner.len;
|
|
|
|
|
|
|
|
for (i = 0; i < howmany; i++)
|
|
|
|
mvaddstr (start_line + i, 1, banner.lines[i]);
|
|
|
|
}
|
|
|
|
|
2004-01-02 05:35:46 +01:00
|
|
|
struct dg_game **
|
|
|
|
populate_games (int *l)
|
2003-12-30 22:30:32 +01:00
|
|
|
{
|
2004-01-02 05:35:46 +01:00
|
|
|
int fd, len;
|
2003-12-31 20:47:49 +01:00
|
|
|
DIR *pdir;
|
|
|
|
struct dirent *pdirent;
|
|
|
|
struct stat pstat;
|
2004-01-02 05:35:46 +01:00
|
|
|
char fullname[130], ttyrecname[130];
|
2004-01-05 02:55:08 +01:00
|
|
|
char *replacestr, *dir;
|
2004-01-02 05:35:46 +01:00
|
|
|
struct dg_game **games = NULL;
|
2004-01-30 18:32:12 +01:00
|
|
|
struct flock fl = { 0 };
|
2004-01-05 02:55:08 +01:00
|
|
|
size_t slen;
|
2004-01-04 01:59:48 +01:00
|
|
|
|
2004-01-30 18:32:12 +01:00
|
|
|
fl.l_type = F_WRLCK;
|
|
|
|
fl.l_whence = SEEK_SET;
|
|
|
|
fl.l_start = 0;
|
|
|
|
fl.l_len = 0;
|
|
|
|
|
2004-01-02 05:35:46 +01:00
|
|
|
len = 0;
|
2004-01-05 02:55:08 +01:00
|
|
|
|
|
|
|
slen = strlen(myconfig->dglroot) + ARRAY_SIZE("inprogress/") + 1;
|
|
|
|
dir = malloc(slen);
|
|
|
|
snprintf(dir, slen, "%sinprogress/", myconfig->dglroot);
|
2004-01-02 05:35:46 +01:00
|
|
|
|
2004-01-05 02:55:08 +01:00
|
|
|
if (!(pdir = opendir (dir)))
|
2004-01-04 02:45:21 +01:00
|
|
|
graceful_exit (140);
|
2004-01-02 05:35:46 +01:00
|
|
|
|
|
|
|
while ((pdirent = readdir (pdir)))
|
|
|
|
{
|
2004-01-04 03:19:12 +01:00
|
|
|
if (!strcmp (pdirent->d_name, ".") || !strcmp (pdirent->d_name, ".."))
|
|
|
|
continue;
|
2004-01-04 01:59:48 +01:00
|
|
|
|
2004-01-05 02:55:08 +01:00
|
|
|
snprintf (fullname, 130, "%sinprogress/%s", myconfig->dglroot, pdirent->d_name);
|
2004-01-02 05:35:46 +01:00
|
|
|
|
|
|
|
fd = 0;
|
2004-01-04 01:59:48 +01:00
|
|
|
/* O_RDWR here should be O_RDONLY, but we need to test for
|
|
|
|
* an exclusive lock */
|
|
|
|
fd = open (fullname, O_RDWR);
|
2004-01-04 03:19:12 +01:00
|
|
|
if ((fd > 0) && fcntl (fd, F_SETLK, &fl) == -1)
|
2004-01-02 05:35:46 +01:00
|
|
|
{
|
|
|
|
|
|
|
|
/* stat to check idle status */
|
2004-01-05 02:55:08 +01:00
|
|
|
snprintf (ttyrecname, 130, "%sttyrec/%s", myconfig->dglroot, pdirent->d_name);
|
2004-01-02 05:35:46 +01:00
|
|
|
replacestr = strchr (ttyrecname, ':');
|
|
|
|
if (!replacestr)
|
2004-01-04 02:45:21 +01:00
|
|
|
graceful_exit (145);
|
2004-01-02 05:35:46 +01:00
|
|
|
replacestr[0] = '/';
|
|
|
|
if (!stat (ttyrecname, &pstat))
|
|
|
|
{
|
|
|
|
/* now it's a valid game for sure */
|
|
|
|
games = realloc (games, sizeof (struct dg_game) * (len + 1));
|
|
|
|
games[len] = malloc (sizeof (struct dg_game));
|
|
|
|
games[len]->ttyrec_fn = strdup (pdirent->d_name);
|
|
|
|
|
|
|
|
if (!(replacestr = strchr (pdirent->d_name, ':')))
|
2004-01-04 02:45:21 +01:00
|
|
|
graceful_exit (146);
|
2004-01-02 05:35:46 +01:00
|
|
|
else
|
|
|
|
*replacestr = '\0';
|
|
|
|
|
|
|
|
games[len]->name = malloc (strlen (pdirent->d_name) + 1);
|
2004-01-04 02:05:22 +01:00
|
|
|
strlcpy (games[len]->name, pdirent->d_name,
|
2004-01-02 05:35:46 +01:00
|
|
|
strlen (pdirent->d_name) + 1);
|
|
|
|
|
|
|
|
games[len]->date = malloc (11);
|
2004-01-04 02:05:22 +01:00
|
|
|
strlcpy (games[len]->date, replacestr + 1, 11);
|
2004-01-02 05:35:46 +01:00
|
|
|
|
|
|
|
games[len]->time = malloc (9);
|
2004-01-04 02:05:22 +01:00
|
|
|
strlcpy (games[len]->time, replacestr + 12, 9);
|
2004-01-02 05:35:46 +01:00
|
|
|
|
|
|
|
games[len]->idle_time = pstat.st_mtime;
|
|
|
|
|
|
|
|
len++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* clean dead ones */
|
|
|
|
unlink (fullname);
|
|
|
|
}
|
2004-02-14 00:15:03 +01:00
|
|
|
close (fd);
|
2004-01-27 10:05:39 +01:00
|
|
|
|
|
|
|
fl.l_type = F_WRLCK;
|
2004-01-02 05:35:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
closedir (pdir);
|
|
|
|
*l = len;
|
|
|
|
return games;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
inprogressmenu ()
|
|
|
|
{
|
|
|
|
int i, menuchoice, len = 20, offset = 0;
|
2003-12-31 20:47:49 +01:00
|
|
|
time_t ctime;
|
2004-01-02 05:35:46 +01:00
|
|
|
struct dg_game **games;
|
|
|
|
char ttyrecname[130], *replacestr = NULL;
|
2003-12-31 20:47:49 +01:00
|
|
|
|
2004-01-02 05:35:46 +01:00
|
|
|
games = populate_games (&len);
|
2003-12-31 20:47:49 +01:00
|
|
|
|
2004-01-02 05:35:46 +01:00
|
|
|
while (1)
|
2003-12-31 20:47:49 +01:00
|
|
|
{
|
|
|
|
clear ();
|
|
|
|
drawbanner (1, 1);
|
|
|
|
mvprintw (3, 1,
|
2004-02-03 03:55:24 +01:00
|
|
|
"During playback, hit 'q' to return here, 'm' to send mail (requires login),");
|
2003-12-31 20:47:49 +01:00
|
|
|
mvaddstr (4, 1,
|
2004-02-03 03:55:24 +01:00
|
|
|
"'s' to toggle graphic-set stripping for DEC, IBM, and none.");
|
2003-12-31 20:47:49 +01:00
|
|
|
mvaddstr (5, 1, "The following games are in progress:");
|
|
|
|
|
|
|
|
/* clean old games and list good ones */
|
|
|
|
i = 0;
|
|
|
|
|
2004-01-02 05:35:46 +01:00
|
|
|
for (i = 0; i < 14; i++)
|
2003-12-31 20:47:49 +01:00
|
|
|
{
|
2004-01-02 05:35:46 +01:00
|
|
|
if (i + offset >= len)
|
|
|
|
break;
|
|
|
|
|
|
|
|
mvprintw (7 + i, 1, "%c) %-15s %s %s (%ldm %lds idle)",
|
|
|
|
i + 97, games[i + offset]->name,
|
|
|
|
games[i + offset]->date, games[i + offset]->time,
|
|
|
|
(time (&ctime) - games[i + offset]->idle_time) / 60,
|
|
|
|
(time (&ctime) - games[i + offset]->idle_time) % 60);
|
2003-12-31 20:47:49 +01:00
|
|
|
}
|
|
|
|
|
2004-01-02 05:35:46 +01:00
|
|
|
mvaddstr (23, 1,
|
2004-02-07 09:42:26 +01:00
|
|
|
"Watch which game? (any key refreshes, 'q' quits, '>'/'<' for more/less) => ");
|
2003-12-31 20:47:49 +01:00
|
|
|
refresh ();
|
|
|
|
|
2004-01-02 05:35:46 +01:00
|
|
|
switch ((menuchoice = tolower (getch ())))
|
2003-12-31 20:47:49 +01:00
|
|
|
{
|
2004-01-03 04:51:29 +01:00
|
|
|
case '>':
|
2004-01-02 05:35:46 +01:00
|
|
|
if ((offset + 14) >= len)
|
|
|
|
break;
|
2003-12-31 20:47:49 +01:00
|
|
|
else
|
2004-01-02 05:35:46 +01:00
|
|
|
offset += 14;
|
|
|
|
break;
|
2004-01-01 20:57:58 +01:00
|
|
|
|
2004-01-03 04:51:29 +01:00
|
|
|
case '<':
|
2004-01-02 05:35:46 +01:00
|
|
|
if ((offset - 14) < 0)
|
|
|
|
break;
|
|
|
|
else
|
|
|
|
offset -= 14;
|
|
|
|
break;
|
2004-01-01 20:57:58 +01:00
|
|
|
|
2004-01-02 05:35:46 +01:00
|
|
|
case 'q':
|
|
|
|
return;
|
2004-01-01 03:06:19 +01:00
|
|
|
|
2004-01-02 05:35:46 +01:00
|
|
|
default:
|
|
|
|
if ((menuchoice - 97) >= 0 && (menuchoice - 97) < i)
|
|
|
|
{
|
|
|
|
/* valid choice has been made */
|
2004-01-05 02:55:08 +01:00
|
|
|
snprintf (ttyrecname, 130, "%sttyrec/%s", myconfig->dglroot,
|
2004-01-02 05:35:46 +01:00
|
|
|
games[menuchoice - 97]->ttyrec_fn);
|
|
|
|
chosen_name = strdup (games[menuchoice - 97 + offset]->name);
|
2003-12-31 20:47:49 +01:00
|
|
|
|
2004-01-02 05:35:46 +01:00
|
|
|
/* reuse thie char* */
|
|
|
|
replacestr = strchr (ttyrecname, ':');
|
|
|
|
|
|
|
|
if (!replacestr)
|
2004-01-04 02:45:21 +01:00
|
|
|
graceful_exit (145);
|
2004-01-02 05:35:46 +01:00
|
|
|
|
|
|
|
replacestr[0] = '/';
|
|
|
|
|
|
|
|
clear ();
|
|
|
|
refresh ();
|
|
|
|
endwin ();
|
2004-02-03 03:55:24 +01:00
|
|
|
ttyplay_main (ttyrecname, 1);
|
2004-01-26 09:29:04 +01:00
|
|
|
initcurses ();
|
2004-01-02 05:35:46 +01:00
|
|
|
}
|
2003-12-31 20:47:49 +01:00
|
|
|
}
|
|
|
|
|
2004-01-02 05:35:46 +01:00
|
|
|
games = populate_games (&len);
|
2003-12-31 20:47:49 +01:00
|
|
|
}
|
2003-12-30 22:30:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* ************************************************************* */
|
|
|
|
|
2004-02-05 20:38:17 +01:00
|
|
|
void
|
|
|
|
change_email ()
|
|
|
|
{
|
|
|
|
char buf[81];
|
|
|
|
|
|
|
|
clear();
|
|
|
|
|
2004-02-05 20:50:58 +01:00
|
|
|
for (;;)
|
2004-02-05 20:38:17 +01:00
|
|
|
{
|
|
|
|
drawbanner(1,1);
|
|
|
|
|
|
|
|
mvprintw(3, 1, "Your current email is: %s", me->email);
|
|
|
|
mvaddstr(4, 1, "Please enter a new one (max 80 chars; blank line aborts)");
|
|
|
|
mvaddstr(6, 1, "=> ");
|
|
|
|
|
|
|
|
mygetnstr (buf, 80, 1);
|
|
|
|
|
|
|
|
if (buf && *buf == '\0')
|
|
|
|
return;
|
|
|
|
else if (!strcmp(me->email, buf))
|
|
|
|
{
|
|
|
|
clear();
|
|
|
|
mvaddstr (8, 1, "That's the same one as before. Try again?");
|
|
|
|
move(1,1);
|
|
|
|
}
|
|
|
|
/* rudimentary check for validity */
|
|
|
|
else if (strchr(buf, '@') < strrchr(buf, '.'))
|
|
|
|
{
|
|
|
|
mvprintw (8, 1, "Changing email address to '%s'. Confirm (y/n): ", buf);
|
|
|
|
if (getch() == 'y')
|
|
|
|
{
|
|
|
|
free(me->email);
|
|
|
|
me->email = strdup(buf);
|
|
|
|
writefile(0);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
mvaddstr(9, 1, "No changes made. Press any key to continue...");
|
|
|
|
getch();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
clear();
|
|
|
|
mvaddstr (8, 1, "That doesn't look like an email address to me.");
|
|
|
|
move(1,1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-01-04 13:16:05 +01:00
|
|
|
int
|
2004-02-13 23:30:59 +01:00
|
|
|
changepw (int dowrite)
|
2003-12-30 22:30:32 +01:00
|
|
|
{
|
2003-12-31 20:47:49 +01:00
|
|
|
char buf[21];
|
|
|
|
int error = 2;
|
|
|
|
|
2004-01-03 04:51:29 +01:00
|
|
|
/* A precondition is that struct `me' exists because we can be not-yet-logged-in. */
|
2003-12-31 20:47:49 +01:00
|
|
|
if (!me)
|
2004-01-04 02:45:21 +01:00
|
|
|
graceful_exit (122); /* Die. */
|
2003-12-31 20:47:49 +01:00
|
|
|
|
|
|
|
while (error)
|
|
|
|
{
|
|
|
|
char repeatbuf[21];
|
|
|
|
clear ();
|
|
|
|
|
|
|
|
drawbanner (1, 1);
|
|
|
|
|
|
|
|
mvprintw (5, 1,
|
|
|
|
"Please enter a%s password. Remember that this is sent over the net",
|
|
|
|
loggedin ? " new" : "");
|
|
|
|
mvaddstr (6, 1,
|
|
|
|
"in plaintext, so make it something new and expect it to be relatively");
|
|
|
|
mvaddstr (7, 1, "insecure.");
|
|
|
|
mvaddstr (8, 1,
|
|
|
|
"20 character max. No ':' characters. Blank line to abort.");
|
|
|
|
mvaddstr (10, 1, "=> ");
|
|
|
|
|
|
|
|
if (error == 1)
|
|
|
|
{
|
|
|
|
mvaddstr (15, 1, "Sorry, the passwords don't match. Try again.");
|
|
|
|
move (10, 4);
|
|
|
|
}
|
|
|
|
|
|
|
|
refresh ();
|
|
|
|
|
2004-01-19 12:58:31 +01:00
|
|
|
mygetnstr (buf, 20, 0);
|
2003-12-31 20:47:49 +01:00
|
|
|
|
|
|
|
if (buf && *buf == '\0')
|
2004-01-04 13:16:05 +01:00
|
|
|
return 0;
|
2003-12-31 20:47:49 +01:00
|
|
|
|
2004-01-01 03:06:19 +01:00
|
|
|
if (strchr (buf, ':') != NULL)
|
2004-01-04 02:45:21 +01:00
|
|
|
graceful_exit (112);
|
2003-12-31 20:47:49 +01:00
|
|
|
|
|
|
|
mvaddstr (12, 1, "And again:");
|
|
|
|
mvaddstr (13, 1, "=> ");
|
|
|
|
|
2004-01-19 12:58:31 +01:00
|
|
|
mygetnstr (repeatbuf, 20, 0);
|
2003-12-31 20:47:49 +01:00
|
|
|
|
|
|
|
if (!strcmp (buf, repeatbuf))
|
|
|
|
error = 0;
|
|
|
|
else
|
|
|
|
error = 1;
|
|
|
|
}
|
|
|
|
|
2004-02-05 20:38:17 +01:00
|
|
|
free(me->password);
|
2003-12-31 20:47:49 +01:00
|
|
|
me->password = strdup (crypt (buf, buf));
|
2004-02-13 23:30:59 +01:00
|
|
|
|
2004-02-13 23:36:17 +01:00
|
|
|
if (dowrite)
|
2004-02-13 23:30:59 +01:00
|
|
|
writefile (0);
|
2004-01-04 13:16:05 +01:00
|
|
|
|
|
|
|
return 1;
|
2003-12-30 22:30:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* ************************************************************* */
|
|
|
|
|
|
|
|
void
|
|
|
|
domailuser (char *username)
|
|
|
|
{
|
2003-12-31 20:47:49 +01:00
|
|
|
unsigned int len, i;
|
|
|
|
char *spool_fn, message[80];
|
|
|
|
FILE *user_spool = NULL;
|
|
|
|
time_t now;
|
|
|
|
int mail_empty = 1;
|
2004-01-30 18:32:12 +01:00
|
|
|
struct flock fl = { 0 };
|
|
|
|
|
|
|
|
fl.l_type = F_WRLCK;
|
|
|
|
fl.l_whence = SEEK_SET;
|
|
|
|
fl.l_start = 0;
|
|
|
|
fl.l_len = 0;
|
2003-12-31 20:47:49 +01:00
|
|
|
|
|
|
|
assert (loggedin);
|
|
|
|
|
2004-01-05 02:55:08 +01:00
|
|
|
len = strlen(myconfig->spool) + strlen (username) + 1;
|
2003-12-31 20:47:49 +01:00
|
|
|
spool_fn = malloc (len + 1);
|
|
|
|
time (&now);
|
2004-01-06 14:16:31 +01:00
|
|
|
snprintf (spool_fn, len + 1, "%s/%s", myconfig->spool, username);
|
2003-12-31 20:47:49 +01:00
|
|
|
|
|
|
|
/* print the enter your message line */
|
|
|
|
clear ();
|
|
|
|
drawbanner (1, 1);
|
|
|
|
mvaddstr (5, 1,
|
|
|
|
"Enter your message here. It is to be one line only and 80 characters or less.");
|
|
|
|
mvaddstr (7, 1, "=> ");
|
|
|
|
|
2004-01-19 12:58:31 +01:00
|
|
|
mygetnstr (message, 80, 1);
|
2003-12-31 20:47:49 +01:00
|
|
|
|
|
|
|
for (i = 0; i < strlen (message); i++)
|
|
|
|
{
|
|
|
|
if (message[i] != ' ' && message[i] != '\n' && message[i] != '\t')
|
|
|
|
mail_empty = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mail_empty)
|
|
|
|
{
|
|
|
|
mvaddstr (9, 1, "This scroll appears to be blank.--More--");
|
|
|
|
mvaddstr (10, 1, "(Aborting your message.)");
|
|
|
|
getch ();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((user_spool = fopen (spool_fn, "a")) == NULL)
|
|
|
|
{
|
|
|
|
mvaddstr (9, 1,
|
|
|
|
"You fall into the water! You sink like a rock.--More--");
|
|
|
|
mvprintw (10, 1,
|
|
|
|
"(I couldn't open %s'%c spool file for some reason, so I'm giving up.)",
|
|
|
|
username, (username[strlen (username) - 1] != 's') ? 's' : 0);
|
|
|
|
getch ();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
mvaddstr (9, 1, "Getting a lock on the mailspool...");
|
|
|
|
refresh ();
|
|
|
|
|
2004-01-04 01:21:39 +01:00
|
|
|
while (fcntl (fileno (user_spool), F_SETLK, &fl) == -1)
|
|
|
|
{
|
2004-01-04 03:19:12 +01:00
|
|
|
if (errno != EAGAIN)
|
|
|
|
{
|
|
|
|
mvaddstr (10, 1,
|
|
|
|
"Received a weird error from fcntl, so I'm giving up.");
|
|
|
|
getch ();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
sleep (1);
|
2004-01-04 01:21:39 +01:00
|
|
|
}
|
2003-12-31 20:47:49 +01:00
|
|
|
|
|
|
|
fprintf (user_spool, "%s:%s\n", me->username, message);
|
|
|
|
|
2004-01-06 14:35:38 +01:00
|
|
|
/*
|
|
|
|
* Don't unlock the file ourselves, this way it will be done automatically
|
|
|
|
* after all data has been written. (Using file locking with stdio is icky.)
|
|
|
|
*/
|
2003-12-31 20:47:49 +01:00
|
|
|
|
|
|
|
fclose (user_spool);
|
|
|
|
|
2004-01-06 14:35:38 +01:00
|
|
|
mvaddstr (9, 1, "Message sent successfully ");
|
2004-01-27 18:31:26 +01:00
|
|
|
move(9, 26); /* Pedantry! */
|
2004-01-06 14:35:38 +01:00
|
|
|
refresh ();
|
|
|
|
sleep (2);
|
|
|
|
|
2003-12-31 20:47:49 +01:00
|
|
|
return;
|
2003-12-30 22:30:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
drawmenu ()
|
|
|
|
{
|
2003-12-31 20:47:49 +01:00
|
|
|
static int flood = 0;
|
|
|
|
|
|
|
|
clear ();
|
|
|
|
|
|
|
|
drawbanner (1, 0);
|
|
|
|
|
|
|
|
if (loggedin)
|
|
|
|
{
|
|
|
|
mvprintw (banner.len + 2, 1, "Logged in as: %s", me->username);
|
|
|
|
mvaddstr (banner.len + 4, 1, "c) Change password");
|
2004-02-05 20:38:17 +01:00
|
|
|
mvaddstr (banner.len + 5, 1, "e) Change email address");
|
|
|
|
mvaddstr (banner.len + 6, 1, "o) Edit option file");
|
|
|
|
mvaddstr (banner.len + 7, 1, "w) Watch games in progress");
|
|
|
|
mvaddstr (banner.len + 8, 1, "p) Play nethack!");
|
|
|
|
mvaddstr (banner.len + 9, 1, "q) Quit");
|
|
|
|
mvaddstr (banner.len + 11, 1, "=> ");
|
2003-12-31 20:47:49 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
mvaddstr (banner.len + 2, 1, "Not logged in.");
|
|
|
|
mvaddstr (banner.len + 4, 1, "l) Login");
|
|
|
|
mvaddstr (banner.len + 5, 1, "r) Register new user");
|
|
|
|
mvaddstr (banner.len + 6, 1, "w) Watch games in progress");
|
|
|
|
mvaddstr (banner.len + 7, 1, "q) Quit");
|
|
|
|
mvaddstr (banner.len + 9, 1, "=> ");
|
|
|
|
}
|
|
|
|
|
|
|
|
refresh ();
|
|
|
|
|
|
|
|
/* for retarded clients */
|
|
|
|
flood++;
|
|
|
|
if (flood >= 20)
|
2004-01-05 02:25:38 +01:00
|
|
|
{
|
|
|
|
endwin();
|
2004-01-04 02:45:21 +01:00
|
|
|
graceful_exit (119);
|
2004-01-05 02:25:38 +01:00
|
|
|
}
|
2003-12-30 22:30:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* ************************************************************* */
|
|
|
|
|
|
|
|
void
|
|
|
|
freefile ()
|
|
|
|
{
|
2003-12-31 20:47:49 +01:00
|
|
|
int i;
|
|
|
|
|
|
|
|
/* free existing mem, clear existing entries */
|
|
|
|
for (i = 0; i < f_num; i++)
|
|
|
|
{
|
2004-01-26 09:29:04 +01:00
|
|
|
if (users[i] != me)
|
|
|
|
{
|
|
|
|
free (users[i]->password);
|
|
|
|
free (users[i]->username);
|
|
|
|
free (users[i]->email);
|
|
|
|
free (users[i]->env);
|
|
|
|
free (users[i]);
|
|
|
|
}
|
2003-12-31 20:47:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (users)
|
|
|
|
free (users);
|
|
|
|
|
|
|
|
users = NULL;
|
|
|
|
f_num = 0;
|
2003-12-30 22:30:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* ************************************************************* */
|
|
|
|
|
|
|
|
void
|
2004-01-26 09:29:04 +01:00
|
|
|
initcurses ()
|
2003-12-30 22:30:32 +01:00
|
|
|
{
|
2003-12-31 20:47:49 +01:00
|
|
|
initscr ();
|
|
|
|
cbreak ();
|
2004-01-19 12:58:31 +01:00
|
|
|
noecho ();
|
2003-12-31 20:47:49 +01:00
|
|
|
nonl ();
|
|
|
|
intrflush (stdscr, FALSE);
|
|
|
|
keypad (stdscr, TRUE);
|
2003-12-30 22:30:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* ************************************************************* */
|
|
|
|
|
|
|
|
void
|
2004-01-31 04:04:58 +01:00
|
|
|
loginprompt (int from_ttyplay)
|
2003-12-30 22:30:32 +01:00
|
|
|
{
|
2003-12-31 20:47:49 +01:00
|
|
|
char user_buf[22], pw_buf[22];
|
|
|
|
int error = 2, me_index = -1;
|
2003-12-30 22:30:32 +01:00
|
|
|
|
2003-12-31 20:47:49 +01:00
|
|
|
loggedin = 0;
|
2003-12-30 22:30:32 +01:00
|
|
|
|
2003-12-31 20:47:49 +01:00
|
|
|
while (error)
|
|
|
|
{
|
|
|
|
clear ();
|
2003-12-30 22:30:32 +01:00
|
|
|
|
2003-12-31 20:47:49 +01:00
|
|
|
drawbanner (1, 1);
|
2003-12-30 22:30:32 +01:00
|
|
|
|
2004-01-31 04:04:58 +01:00
|
|
|
if (from_ttyplay == 1)
|
|
|
|
mvaddstr (4, 1, "This operation requires you to be logged in.");
|
|
|
|
|
2003-12-31 20:47:49 +01:00
|
|
|
mvaddstr (5, 1,
|
2004-01-31 04:04:58 +01:00
|
|
|
"Please enter your username. (blank entry aborts)");
|
2003-12-31 20:47:49 +01:00
|
|
|
mvaddstr (7, 1, "=> ");
|
2003-12-30 22:30:32 +01:00
|
|
|
|
2003-12-31 20:47:49 +01:00
|
|
|
if (error == 1)
|
|
|
|
{
|
|
|
|
mvaddstr (9, 1, "There was a problem with your last entry.");
|
|
|
|
move (7, 4);
|
|
|
|
}
|
2003-12-30 22:30:32 +01:00
|
|
|
|
2003-12-31 20:47:49 +01:00
|
|
|
refresh ();
|
2003-12-30 22:30:32 +01:00
|
|
|
|
2004-01-19 12:58:31 +01:00
|
|
|
mygetnstr (user_buf, 20, 1);
|
2003-12-30 22:30:32 +01:00
|
|
|
|
2003-12-31 20:47:49 +01:00
|
|
|
if (user_buf && *user_buf == '\0')
|
|
|
|
return;
|
2003-12-30 22:30:32 +01:00
|
|
|
|
2003-12-31 20:47:49 +01:00
|
|
|
error = 1;
|
2003-12-30 22:30:32 +01:00
|
|
|
|
2003-12-31 20:47:49 +01:00
|
|
|
if ((me_index = userexist (user_buf)) != -1)
|
|
|
|
{
|
2004-01-26 09:29:04 +01:00
|
|
|
me = users[me_index];
|
2003-12-31 20:47:49 +01:00
|
|
|
error = 0;
|
|
|
|
}
|
|
|
|
}
|
2003-12-30 22:30:32 +01:00
|
|
|
|
2003-12-31 20:47:49 +01:00
|
|
|
clear ();
|
2003-12-30 22:30:32 +01:00
|
|
|
|
2003-12-31 20:47:49 +01:00
|
|
|
drawbanner (1, 1);
|
2003-12-30 22:30:32 +01:00
|
|
|
|
2003-12-31 20:47:49 +01:00
|
|
|
mvaddstr (5, 1, "Please enter your password.");
|
|
|
|
mvaddstr (7, 1, "=> ");
|
2003-12-30 22:30:32 +01:00
|
|
|
|
2003-12-31 20:47:49 +01:00
|
|
|
refresh ();
|
2003-12-30 22:30:32 +01:00
|
|
|
|
2004-01-19 12:58:31 +01:00
|
|
|
mygetnstr (pw_buf, 20, 0);
|
2003-12-30 22:30:32 +01:00
|
|
|
|
2004-01-02 22:00:54 +01:00
|
|
|
if (passwordgood (pw_buf))
|
2003-12-31 20:47:49 +01:00
|
|
|
{
|
|
|
|
loggedin = 1;
|
2004-01-05 02:55:08 +01:00
|
|
|
snprintf (rcfilename, 80, "%srcfiles/%s.nethackrc", myconfig->dglroot, me->username);
|
2003-12-31 20:47:49 +01:00
|
|
|
}
|
2004-01-31 04:10:03 +01:00
|
|
|
else if (from_ttyplay == 1)
|
|
|
|
{
|
|
|
|
mvaddstr(9, 1, "Login failed. Returning to game.");
|
|
|
|
refresh();
|
|
|
|
sleep(2);
|
|
|
|
}
|
2003-12-30 22:30:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* ************************************************************* */
|
|
|
|
|
|
|
|
void
|
|
|
|
newuser ()
|
|
|
|
{
|
2004-02-03 00:28:17 +01:00
|
|
|
char buf[1024], dirname[100];
|
2003-12-31 20:47:49 +01:00
|
|
|
int error = 2;
|
|
|
|
unsigned int i;
|
|
|
|
|
|
|
|
loggedin = 0;
|
2003-12-30 22:30:32 +01:00
|
|
|
|
2004-01-21 17:31:40 +01:00
|
|
|
if (f_num >= myconfig->max)
|
|
|
|
{
|
|
|
|
clear ();
|
|
|
|
|
|
|
|
drawbanner (1, 1);
|
|
|
|
|
|
|
|
mvaddstr (5, 1, "Sorry, too many users have registered now.");
|
|
|
|
mvaddstr (6, 1, "You might email the server administrator.");
|
|
|
|
mvaddstr (7, 1, "Press return to return to the menu. ");
|
|
|
|
getch ();
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2003-12-31 20:47:49 +01:00
|
|
|
if (me)
|
|
|
|
free (me);
|
2003-12-30 22:30:32 +01:00
|
|
|
|
2003-12-31 20:47:49 +01:00
|
|
|
me = malloc (sizeof (struct dg_user));
|
2003-12-31 02:58:56 +01:00
|
|
|
|
2003-12-31 20:47:49 +01:00
|
|
|
while (error)
|
|
|
|
{
|
|
|
|
clear ();
|
2003-12-30 23:41:47 +01:00
|
|
|
|
2003-12-31 20:47:49 +01:00
|
|
|
drawbanner (1, 1);
|
2003-12-30 22:30:32 +01:00
|
|
|
|
2003-12-31 20:47:49 +01:00
|
|
|
mvaddstr (5, 1, "Welcome new user. Please enter a username.");
|
|
|
|
mvaddstr (6, 1,
|
|
|
|
"Only characters and numbers are allowed, with no spaces.");
|
|
|
|
mvaddstr (7, 1, "20 character max.");
|
|
|
|
mvaddstr (9, 1, "=> ");
|
2003-12-30 22:30:32 +01:00
|
|
|
|
2003-12-31 20:47:49 +01:00
|
|
|
if (error == 1)
|
|
|
|
{
|
|
|
|
mvaddstr (11, 1, "There was a problem with your last entry.");
|
|
|
|
move (9, 4);
|
|
|
|
}
|
2003-12-30 22:30:32 +01:00
|
|
|
|
2003-12-31 20:47:49 +01:00
|
|
|
refresh ();
|
2003-12-30 22:30:32 +01:00
|
|
|
|
2004-01-19 12:58:31 +01:00
|
|
|
mygetnstr (buf, 20, 1);
|
2003-12-31 20:47:49 +01:00
|
|
|
if (userexist (buf) == -1)
|
|
|
|
error = 0;
|
|
|
|
else
|
|
|
|
error = 1;
|
2003-12-30 22:30:32 +01:00
|
|
|
|
2003-12-31 20:47:49 +01:00
|
|
|
for (i = 0; i < strlen (buf); i++)
|
|
|
|
{
|
2004-01-16 03:00:47 +01:00
|
|
|
if (!isalnum((int)buf[i]))
|
2003-12-31 20:47:49 +01:00
|
|
|
error = 1;
|
|
|
|
}
|
2003-12-30 22:30:32 +01:00
|
|
|
|
2003-12-31 20:47:49 +01:00
|
|
|
if (strlen (buf) < 2)
|
|
|
|
error = 1;
|
2003-12-30 22:30:32 +01:00
|
|
|
|
2003-12-31 20:47:49 +01:00
|
|
|
if (strlen (buf) == 0)
|
2004-01-05 02:25:38 +01:00
|
|
|
{
|
|
|
|
free(me);
|
2003-12-31 20:47:49 +01:00
|
|
|
return;
|
2004-01-05 02:25:38 +01:00
|
|
|
}
|
2003-12-31 20:47:49 +01:00
|
|
|
}
|
2003-12-30 22:30:32 +01:00
|
|
|
|
2003-12-31 20:47:49 +01:00
|
|
|
me->username = strdup (buf);
|
2003-12-30 22:30:32 +01:00
|
|
|
|
2003-12-31 20:47:49 +01:00
|
|
|
/* password step */
|
2003-12-30 22:30:32 +01:00
|
|
|
|
2003-12-31 20:47:49 +01:00
|
|
|
clear ();
|
2003-12-30 22:30:32 +01:00
|
|
|
|
2004-02-13 23:30:59 +01:00
|
|
|
if (!changepw (0)) /* Calling changepw instead to prompt twice. */
|
2004-01-04 13:16:05 +01:00
|
|
|
{
|
2004-01-05 02:25:38 +01:00
|
|
|
free(me->username);
|
2004-01-04 13:16:05 +01:00
|
|
|
free(me);
|
|
|
|
me = NULL;
|
|
|
|
return;
|
|
|
|
}
|
2003-12-30 22:30:32 +01:00
|
|
|
|
2003-12-31 20:47:49 +01:00
|
|
|
/* email step */
|
2003-12-30 22:30:32 +01:00
|
|
|
|
2003-12-31 20:47:49 +01:00
|
|
|
clear ();
|
2003-12-30 22:30:32 +01:00
|
|
|
|
2003-12-31 20:47:49 +01:00
|
|
|
drawbanner (1, 1);
|
2003-12-30 22:30:32 +01:00
|
|
|
|
2003-12-31 20:47:49 +01:00
|
|
|
mvaddstr (5, 1, "Please enter your email address.");
|
|
|
|
mvaddstr (6, 1,
|
|
|
|
"This is sent _nowhere_ but will be used if you ask the sysadmin for lost");
|
|
|
|
mvaddstr (7, 1,
|
|
|
|
"password help. Please use a correct one. It only benefits you.");
|
2004-01-05 02:25:38 +01:00
|
|
|
mvaddstr (8, 1, "80 character max. No ':' characters. Blank line aborts.");
|
2003-12-31 20:47:49 +01:00
|
|
|
mvaddstr (10, 1, "=> ");
|
2003-12-30 22:30:32 +01:00
|
|
|
|
2003-12-31 20:47:49 +01:00
|
|
|
refresh ();
|
2004-01-19 12:58:31 +01:00
|
|
|
mygetnstr (buf, 80, 1);
|
2003-12-30 22:30:32 +01:00
|
|
|
|
2004-01-01 03:06:19 +01:00
|
|
|
if (strchr (buf, ':') != NULL)
|
2004-01-04 02:45:21 +01:00
|
|
|
graceful_exit (113);
|
2003-12-30 22:30:32 +01:00
|
|
|
|
2004-01-05 02:25:38 +01:00
|
|
|
if (buf && *buf == '\0')
|
|
|
|
{
|
|
|
|
free (me->username);
|
|
|
|
free (me->password);
|
|
|
|
free (me);
|
|
|
|
me = NULL;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2003-12-31 20:47:49 +01:00
|
|
|
me->email = strdup (buf);
|
|
|
|
me->env = calloc (1, 1);
|
2003-12-30 22:30:32 +01:00
|
|
|
|
2003-12-31 20:47:49 +01:00
|
|
|
loggedin = 1;
|
2003-12-30 22:30:32 +01:00
|
|
|
|
2004-01-05 02:55:08 +01:00
|
|
|
snprintf (rcfilename, 80, "%srcfiles/%s.nethackrc", myconfig->dglroot, me->username);
|
2003-12-31 20:47:49 +01:00
|
|
|
write_canned_rcfile (rcfilename);
|
2003-12-30 22:30:32 +01:00
|
|
|
|
2004-02-03 00:28:17 +01:00
|
|
|
/* create their ttyrec dir */
|
|
|
|
snprintf (dirname, 100, "%sttyrec/%s", myconfig->dglroot, me->username);
|
|
|
|
|
|
|
|
if (access (dirname, F_OK) != 0)
|
|
|
|
mkdir (dirname, 0755);
|
2003-12-31 20:47:49 +01:00
|
|
|
writefile (1);
|
2003-12-30 22:30:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* ************************************************************* */
|
|
|
|
|
|
|
|
int
|
2004-01-02 22:00:54 +01:00
|
|
|
passwordgood (char *cpw)
|
2003-12-30 22:30:32 +01:00
|
|
|
{
|
2004-01-02 22:00:54 +01:00
|
|
|
if (me == NULL)
|
|
|
|
return 1;
|
|
|
|
|
2003-12-31 20:47:49 +01:00
|
|
|
if (!strncmp (crypt (cpw, cpw), me->password, 13))
|
|
|
|
return 1;
|
|
|
|
if (!strncmp (cpw, me->password, 20))
|
|
|
|
return 1;
|
2003-12-30 22:30:32 +01:00
|
|
|
|
2003-12-31 20:47:49 +01:00
|
|
|
return 0;
|
2003-12-30 22:30:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* ************************************************************* */
|
|
|
|
|
|
|
|
int
|
|
|
|
readfile (int nolock)
|
|
|
|
{
|
2003-12-31 20:47:49 +01:00
|
|
|
FILE *fp = NULL, *fpl = NULL;
|
|
|
|
char buf[1200];
|
2004-01-30 18:32:12 +01:00
|
|
|
struct flock fl = { 0 };
|
2004-01-04 01:59:48 +01:00
|
|
|
|
2004-01-30 18:32:12 +01:00
|
|
|
fl.l_type = F_RDLCK;
|
|
|
|
fl.l_whence = SEEK_SET;
|
|
|
|
fl.l_start = 0;
|
|
|
|
fl.l_len = 0;
|
|
|
|
|
|
|
|
memset (buf, 1024, 0);
|
2003-12-31 20:47:49 +01:00
|
|
|
|
|
|
|
/* read new stuff */
|
|
|
|
|
|
|
|
if (!nolock)
|
|
|
|
{
|
2004-01-21 02:57:44 +01:00
|
|
|
fpl = fopen (myconfig->lockfile, "r");
|
2003-12-31 20:47:49 +01:00
|
|
|
if (!fpl)
|
2004-01-04 02:45:21 +01:00
|
|
|
graceful_exit (106);
|
2004-01-04 03:19:12 +01:00
|
|
|
if (fcntl (fileno (fpl), F_SETLKW, &fl) == -1)
|
2004-01-04 02:45:21 +01:00
|
|
|
graceful_exit (114);
|
2003-12-31 20:47:49 +01:00
|
|
|
}
|
|
|
|
|
2004-01-21 02:57:44 +01:00
|
|
|
fp = fopen (myconfig->passwd, "r");
|
2003-12-31 20:47:49 +01:00
|
|
|
if (!fp)
|
2004-01-04 02:45:21 +01:00
|
|
|
graceful_exit (106);
|
2003-12-31 20:47:49 +01:00
|
|
|
|
|
|
|
/* once per name in the file */
|
|
|
|
while (fgets (buf, 1200, fp))
|
|
|
|
{
|
|
|
|
char *b = buf, *n = buf;
|
|
|
|
|
|
|
|
users = realloc (users, sizeof (struct dg_user *) * (f_num + 1));
|
|
|
|
users[f_num] = malloc (sizeof (struct dg_user));
|
|
|
|
users[f_num]->username = (char *) calloc (22, sizeof (char));
|
|
|
|
users[f_num]->email = (char *) calloc (82, sizeof (char));
|
|
|
|
users[f_num]->password = (char *) calloc (22, sizeof (char));
|
|
|
|
users[f_num]->env = (char *) calloc (1026, sizeof (char));
|
|
|
|
|
|
|
|
/* name field, must be valid */
|
|
|
|
while (*b != ':')
|
|
|
|
{
|
2004-01-16 03:00:47 +01:00
|
|
|
if (!isalnum((int)*b))
|
2003-12-31 20:47:49 +01:00
|
|
|
return 1;
|
|
|
|
users[f_num]->username[(b - n)] = *b;
|
|
|
|
b++;
|
|
|
|
if ((b - n) >= 21)
|
2004-01-04 02:45:21 +01:00
|
|
|
graceful_exit (100);
|
2003-12-31 20:47:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* advance to next field */
|
|
|
|
n = b + 1;
|
|
|
|
b = n;
|
|
|
|
|
|
|
|
/* email field */
|
|
|
|
while (*b != ':')
|
|
|
|
{
|
|
|
|
users[f_num]->email[(b - n)] = *b;
|
|
|
|
b++;
|
|
|
|
if ((b - n) > 80)
|
2004-01-04 02:45:21 +01:00
|
|
|
graceful_exit (101);
|
2003-12-31 20:47:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* advance to next field */
|
|
|
|
n = b + 1;
|
|
|
|
b = n;
|
|
|
|
|
|
|
|
/* pw field */
|
|
|
|
while (*b != ':')
|
|
|
|
{
|
|
|
|
users[f_num]->password[(b - n)] = *b;
|
|
|
|
b++;
|
|
|
|
if ((b - n) >= 20)
|
2004-01-04 02:45:21 +01:00
|
|
|
graceful_exit (102);
|
2003-12-31 20:47:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* advance to next field */
|
|
|
|
n = b + 1;
|
|
|
|
b = n;
|
|
|
|
|
|
|
|
/* env field */
|
|
|
|
while ((*b != '\n') && (*b != 0) && (*b != EOF))
|
|
|
|
{
|
|
|
|
users[f_num]->env[(b - n)] = *b;
|
|
|
|
b++;
|
|
|
|
if ((b - n) >= 1024)
|
2004-01-04 02:45:21 +01:00
|
|
|
graceful_exit (102);
|
2003-12-31 20:47:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
f_num++;
|
|
|
|
/* prevent a buffer overrun here */
|
2004-01-21 17:31:40 +01:00
|
|
|
if (f_num > myconfig->max)
|
2004-02-01 09:20:28 +01:00
|
|
|
{
|
|
|
|
fprintf(stderr,"ERROR: number of users in database exceeds maximum. Exiting.\n");
|
2004-01-04 02:45:21 +01:00
|
|
|
graceful_exit (109);
|
2004-02-01 09:20:28 +01:00
|
|
|
}
|
2003-12-31 20:47:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!nolock)
|
|
|
|
fclose (fpl);
|
|
|
|
fclose (fp);
|
|
|
|
return 0;
|
2003-12-30 22:30:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* ************************************************************* */
|
|
|
|
|
|
|
|
int
|
|
|
|
userexist (char *cname)
|
|
|
|
{
|
2003-12-31 20:47:49 +01:00
|
|
|
int i;
|
2003-12-30 22:30:32 +01:00
|
|
|
|
2003-12-31 20:47:49 +01:00
|
|
|
for (i = 0; i < f_num; i++)
|
|
|
|
{
|
|
|
|
if (!strncasecmp (cname, users[i]->username, 20))
|
|
|
|
return i;
|
|
|
|
}
|
2003-12-30 22:30:32 +01:00
|
|
|
|
2003-12-31 20:47:49 +01:00
|
|
|
return -1;
|
2003-12-30 22:30:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* ************************************************************* */
|
|
|
|
|
|
|
|
void
|
2003-12-31 02:58:56 +01:00
|
|
|
write_canned_rcfile (char *target)
|
2003-12-30 22:30:32 +01:00
|
|
|
{
|
2003-12-31 20:47:49 +01:00
|
|
|
FILE *canned, *newfile;
|
2004-01-05 02:55:08 +01:00
|
|
|
char buf[1024], *rfn;
|
|
|
|
size_t bytes, len;
|
|
|
|
|
2004-01-23 19:33:35 +01:00
|
|
|
len = strlen(myconfig->rcfile) + 2;
|
2004-01-05 02:55:08 +01:00
|
|
|
rfn = malloc(len);
|
2004-01-23 19:33:35 +01:00
|
|
|
snprintf (rfn, len, "/%s", myconfig->rcfile);
|
2003-12-31 20:47:49 +01:00
|
|
|
|
|
|
|
if (!(newfile = fopen (target, "w")))
|
|
|
|
{
|
|
|
|
bail:
|
|
|
|
mvaddstr (13, 1,
|
2004-02-01 09:10:00 +01:00
|
|
|
"You don't know how to write that! You write \"%s was here\" and the scroll disappears.");
|
2003-12-31 20:47:49 +01:00
|
|
|
mvaddstr (14, 1,
|
|
|
|
"(Sorry, but I couldn't open one of the nethackrc files. This is a bug.)");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2004-01-05 02:55:08 +01:00
|
|
|
if (!(canned = fopen (rfn, "r")))
|
2003-12-31 20:47:49 +01:00
|
|
|
goto bail;
|
|
|
|
|
2004-01-05 02:55:08 +01:00
|
|
|
free(rfn);
|
|
|
|
|
2003-12-31 20:47:49 +01:00
|
|
|
while ((bytes = fread (buf, 1, 1024, canned)) > 0)
|
|
|
|
{
|
|
|
|
if (fwrite (buf, 1, bytes, newfile) != bytes)
|
|
|
|
{
|
|
|
|
if (ferror (newfile))
|
|
|
|
{
|
|
|
|
mvaddstr (13, 1, "Your hand slips while engraving.");
|
|
|
|
mvaddstr (14, 1,
|
|
|
|
"(Encountered a problem writing the new file. This is a bug.)");
|
|
|
|
fclose (canned);
|
|
|
|
fclose (newfile);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fclose (canned);
|
|
|
|
fclose (newfile);
|
2003-12-30 22:30:32 +01:00
|
|
|
}
|
2003-12-31 02:58:56 +01:00
|
|
|
|
2003-12-30 22:30:32 +01:00
|
|
|
|
|
|
|
void
|
|
|
|
editoptions ()
|
|
|
|
{
|
2003-12-31 20:47:49 +01:00
|
|
|
FILE *rcfile;
|
|
|
|
char *myargv[3];
|
2004-01-23 19:14:48 +01:00
|
|
|
pid_t editor;
|
2003-12-30 22:30:32 +01:00
|
|
|
|
2003-12-31 20:47:49 +01:00
|
|
|
rcfile = fopen (rcfilename, "r");
|
|
|
|
if (!rcfile) /* should not really happen except for old users */
|
|
|
|
write_canned_rcfile (rcfilename);
|
2003-12-31 02:58:56 +01:00
|
|
|
|
2004-01-31 04:04:58 +01:00
|
|
|
/* use whatever editor_main to edit */
|
2003-12-30 22:30:32 +01:00
|
|
|
|
2003-12-31 20:47:49 +01:00
|
|
|
myargv[0] = "";
|
|
|
|
myargv[1] = rcfilename;
|
|
|
|
myargv[2] = 0;
|
2003-12-30 22:30:32 +01:00
|
|
|
|
2003-12-31 20:47:49 +01:00
|
|
|
endwin ();
|
2004-01-23 19:14:48 +01:00
|
|
|
|
|
|
|
editor = fork();
|
|
|
|
|
|
|
|
if (editor == -1)
|
|
|
|
{
|
|
|
|
perror("fork");
|
|
|
|
graceful_exit(114);
|
|
|
|
}
|
|
|
|
else if (editor == 0)
|
|
|
|
{
|
2004-01-25 23:44:34 +01:00
|
|
|
editor_main (2, myargv);
|
2004-01-23 19:14:48 +01:00
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
waitpid(editor, NULL, 0);
|
|
|
|
|
2003-12-31 20:47:49 +01:00
|
|
|
refresh ();
|
2003-12-30 22:30:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* ************************************************************* */
|
|
|
|
|
|
|
|
void
|
|
|
|
writefile (int requirenew)
|
|
|
|
{
|
2003-12-31 20:47:49 +01:00
|
|
|
FILE *fp, *fpl;
|
|
|
|
int i = 0;
|
|
|
|
int my_done = 0;
|
2004-01-30 18:32:12 +01:00
|
|
|
struct flock fl = { 0 };
|
|
|
|
|
|
|
|
fl.l_type = F_WRLCK;
|
|
|
|
fl.l_whence = SEEK_SET;
|
|
|
|
fl.l_start = 0;
|
|
|
|
fl.l_len = 0;
|
2003-12-31 20:47:49 +01:00
|
|
|
|
2004-01-21 02:57:44 +01:00
|
|
|
fpl = fopen (myconfig->lockfile, "r+");
|
2003-12-31 20:47:49 +01:00
|
|
|
if (!fpl)
|
2004-01-04 02:45:21 +01:00
|
|
|
graceful_exit (115);
|
2004-01-12 18:32:47 +01:00
|
|
|
if (fcntl (fileno (fpl), F_SETLK, &fl))
|
2004-01-04 02:45:21 +01:00
|
|
|
graceful_exit (107);
|
2003-12-31 20:47:49 +01:00
|
|
|
|
2004-01-12 18:32:47 +01:00
|
|
|
fl.l_type = F_UNLCK;
|
|
|
|
|
2003-12-31 20:47:49 +01:00
|
|
|
freefile ();
|
|
|
|
readfile (1);
|
|
|
|
|
2004-01-21 02:57:44 +01:00
|
|
|
fp = fopen (myconfig->passwd, "w");
|
2003-12-31 20:47:49 +01:00
|
|
|
if (!fp)
|
2004-01-04 02:45:21 +01:00
|
|
|
graceful_exit (104);
|
2003-12-31 20:47:49 +01:00
|
|
|
|
|
|
|
for (i = 0; i < f_num; i++)
|
|
|
|
{
|
2004-01-05 18:37:21 +01:00
|
|
|
if (loggedin && !strncmp (me->username, users[i]->username, 20))
|
2003-12-31 20:47:49 +01:00
|
|
|
{
|
|
|
|
if (requirenew)
|
|
|
|
{
|
|
|
|
/* this is if someone managed to register at the same time
|
|
|
|
* as someone else. just die. */
|
2004-01-04 02:45:21 +01:00
|
|
|
graceful_exit (111);
|
2003-12-31 20:47:49 +01:00
|
|
|
}
|
|
|
|
fprintf (fp, "%s:%s:%s:%s\n", me->username, me->email, me->password,
|
|
|
|
me->env);
|
|
|
|
my_done = 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
fprintf (fp, "%s:%s:%s:%s\n", users[i]->username, users[i]->email,
|
|
|
|
users[i]->password, users[i]->env);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (loggedin && !my_done)
|
|
|
|
{ /* new entry */
|
2004-01-21 17:31:40 +01:00
|
|
|
if (f_num < myconfig->max)
|
|
|
|
fprintf (fp, "%s:%s:%s:%s\n", me->username, me->email, me->password,
|
|
|
|
me->env);
|
|
|
|
else /* Oops, someone else registered the last available slot first */
|
|
|
|
graceful_exit (116);
|
2003-12-31 20:47:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
fclose (fp);
|
|
|
|
fclose (fpl);
|
2003-12-30 22:30:32 +01:00
|
|
|
}
|
|
|
|
|
2004-01-04 02:45:21 +01:00
|
|
|
/* ************************************************************* */
|
|
|
|
|
|
|
|
void
|
|
|
|
graceful_exit (int status)
|
|
|
|
{
|
|
|
|
/*FILE *fp;
|
|
|
|
if (status != 1)
|
|
|
|
{
|
|
|
|
fp = fopen ("/crash.log", "a");
|
|
|
|
char buf[100];
|
|
|
|
sprintf (buf, "graceful_exit called with status %d", status);
|
|
|
|
fputs (buf, fp);
|
|
|
|
}
|
|
|
|
This doesn't work. Ever.
|
|
|
|
*/
|
|
|
|
exit (status);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* ************************************************************* */
|
|
|
|
|
2003-12-30 22:30:32 +01:00
|
|
|
/* ************************************************************* */
|
|
|
|
/* ************************************************************* */
|
|
|
|
/* ************************************************************* */
|
|
|
|
|
2004-01-04 21:16:59 +01:00
|
|
|
|
|
|
|
/* TODO: Some of the messages here (sorry no nethack for you!) are nethack specific
|
|
|
|
* as may be some code... don't think so though. Globalize it. */
|
2004-01-28 17:31:37 +01:00
|
|
|
int
|
2004-01-04 03:03:26 +01:00
|
|
|
purge_stale_locks (void)
|
|
|
|
{
|
2004-01-04 03:19:12 +01:00
|
|
|
DIR *pdir;
|
2004-01-04 03:03:26 +01:00
|
|
|
struct dirent *dent;
|
2004-01-05 02:55:08 +01:00
|
|
|
char* dir;
|
|
|
|
size_t len;
|
2004-01-28 17:31:37 +01:00
|
|
|
short firsttime = 1;
|
2004-01-05 02:55:08 +01:00
|
|
|
|
|
|
|
len = strlen(myconfig->dglroot) + ARRAY_SIZE("inprogress/") + 1;
|
|
|
|
dir = malloc(len);
|
|
|
|
snprintf(dir, len, "%sinprogress/", myconfig->dglroot);
|
|
|
|
|
|
|
|
if (!(pdir = opendir (dir)))
|
2004-01-04 03:19:12 +01:00
|
|
|
graceful_exit (200);
|
|
|
|
|
2004-01-05 02:55:08 +01:00
|
|
|
free(dir);
|
|
|
|
|
2004-01-04 03:19:12 +01:00
|
|
|
while ((dent = readdir (pdir)) != NULL)
|
|
|
|
{
|
|
|
|
FILE *ipfile;
|
2004-01-04 03:35:40 +01:00
|
|
|
char *colon, *fn;
|
2004-01-04 03:19:12 +01:00
|
|
|
char buf[16];
|
|
|
|
pid_t pid;
|
2004-01-04 03:35:40 +01:00
|
|
|
size_t len;
|
|
|
|
int seconds = 0;
|
2004-01-28 17:31:37 +01:00
|
|
|
|
2004-01-04 23:49:29 +01:00
|
|
|
if (!strcmp (dent->d_name, ".") || !strcmp (dent->d_name, ".."))
|
|
|
|
continue;
|
2004-01-04 03:28:57 +01:00
|
|
|
|
2004-01-04 03:19:12 +01:00
|
|
|
colon = strchr (dent->d_name, ':');
|
|
|
|
/* should never happen */
|
|
|
|
if (!colon)
|
|
|
|
graceful_exit (201);
|
|
|
|
|
|
|
|
if (strncmp (dent->d_name, me->username, colon - dent->d_name))
|
|
|
|
continue;
|
2004-01-04 03:03:26 +01:00
|
|
|
|
2004-01-05 02:55:08 +01:00
|
|
|
len = strlen (dent->d_name) + strlen(myconfig->dglroot) + 12;
|
2004-01-04 03:35:40 +01:00
|
|
|
fn = malloc (len);
|
|
|
|
|
2004-01-05 02:55:08 +01:00
|
|
|
snprintf (fn, len, "%sinprogress/%s", myconfig->dglroot, dent->d_name);
|
2004-01-04 23:49:29 +01:00
|
|
|
|
2004-01-04 03:35:40 +01:00
|
|
|
if (!(ipfile = fopen (fn, "r")))
|
2004-01-04 03:19:12 +01:00
|
|
|
graceful_exit (202);
|
2004-01-04 03:03:26 +01:00
|
|
|
|
2004-01-04 03:19:12 +01:00
|
|
|
if (fgets (buf, 16, ipfile) == NULL)
|
|
|
|
graceful_exit (203);
|
2004-01-04 03:03:26 +01:00
|
|
|
|
2004-01-04 03:19:12 +01:00
|
|
|
fclose (ipfile);
|
2004-01-04 03:35:40 +01:00
|
|
|
|
2004-01-28 17:31:37 +01:00
|
|
|
if (firsttime)
|
|
|
|
{
|
|
|
|
clear ();
|
|
|
|
drawbanner (1, 1);
|
|
|
|
|
2004-02-01 09:10:00 +01:00
|
|
|
#define HUP_WAIT 10 /* seconds before HUPPING */
|
|
|
|
mvprintw (3, 1,
|
|
|
|
"There are some stale Nethack processes, will recover in %d seconds.", HUP_WAIT);
|
2004-01-28 17:31:37 +01:00
|
|
|
mvaddstr (4, 1,
|
|
|
|
"Press a key NOW if you don't want this to happen!");
|
|
|
|
|
|
|
|
move (3, 58); /* pedantry */
|
|
|
|
halfdelay(10);
|
|
|
|
|
2004-02-01 09:10:00 +01:00
|
|
|
for (seconds = HUP_WAIT - 1; seconds >= 0; seconds--)
|
2004-01-28 17:31:37 +01:00
|
|
|
{
|
|
|
|
if (getch() != ERR)
|
|
|
|
{
|
|
|
|
nocbreak(); /* leave half-delay */
|
|
|
|
cbreak();
|
|
|
|
return 0;
|
|
|
|
}
|
2004-02-01 09:10:00 +01:00
|
|
|
mvprintw (3, 57, "%d%s", seconds, (seconds > 9) ? "" : " ");
|
2004-01-28 17:31:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
nocbreak();
|
|
|
|
cbreak();
|
|
|
|
|
|
|
|
firsttime = 0;
|
|
|
|
}
|
|
|
|
|
2004-01-30 01:56:46 +01:00
|
|
|
clear ();
|
2004-01-04 03:56:09 +01:00
|
|
|
refresh ();
|
2004-01-04 03:03:26 +01:00
|
|
|
|
2004-01-04 03:19:12 +01:00
|
|
|
pid = atoi (buf);
|
2004-01-04 03:03:26 +01:00
|
|
|
|
2004-01-04 03:19:12 +01:00
|
|
|
kill (pid, SIGHUP);
|
2004-01-04 03:03:26 +01:00
|
|
|
|
2004-01-04 03:19:12 +01:00
|
|
|
errno = 0;
|
2004-01-04 03:03:26 +01:00
|
|
|
|
2004-01-04 03:19:12 +01:00
|
|
|
/* Wait for it to stop running */
|
2004-01-04 03:31:00 +01:00
|
|
|
seconds = 0;
|
2004-01-04 03:19:12 +01:00
|
|
|
while (kill (pid, 0) == 0)
|
|
|
|
{
|
|
|
|
seconds++;
|
|
|
|
sleep (1);
|
|
|
|
if (seconds == 10)
|
|
|
|
{
|
|
|
|
mvaddstr (3, 1,
|
|
|
|
"Couldn't terminate one of your stale Nethack processes gracefully.");
|
|
|
|
mvaddstr (4, 1, "Force its termination? [yn] ");
|
|
|
|
if (tolower (getch ()) == 'y')
|
|
|
|
{
|
|
|
|
kill (pid, SIGTERM);
|
|
|
|
break;
|
|
|
|
}
|
2004-01-04 23:49:29 +01:00
|
|
|
else
|
|
|
|
{
|
|
|
|
endwin ();
|
|
|
|
fprintf (stderr, "Sorry, no nethack for you now, please "
|
|
|
|
"contact the admin.\n");
|
|
|
|
graceful_exit (1);
|
|
|
|
}
|
2004-01-04 03:19:12 +01:00
|
|
|
}
|
|
|
|
}
|
2004-01-04 03:56:09 +01:00
|
|
|
|
|
|
|
/* Don't remove the lock file until the process is dead. */
|
|
|
|
unlink (fn);
|
|
|
|
free (fn);
|
2004-01-04 03:19:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
closedir (pdir);
|
2004-01-28 17:31:37 +01:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
menuloop (void)
|
|
|
|
{
|
|
|
|
int userchoice = 0;
|
|
|
|
while ((userchoice != 'p') | (!loggedin))
|
|
|
|
{
|
|
|
|
drawmenu ();
|
|
|
|
userchoice = getch ();
|
|
|
|
switch (tolower (userchoice))
|
|
|
|
{
|
|
|
|
case 'c':
|
|
|
|
if (loggedin)
|
2004-02-13 23:30:59 +01:00
|
|
|
changepw (1);
|
2004-01-28 17:31:37 +01:00
|
|
|
break;
|
2004-02-05 20:38:17 +01:00
|
|
|
case 'e':
|
|
|
|
if (loggedin)
|
|
|
|
change_email();
|
|
|
|
break;
|
2004-01-28 17:31:37 +01:00
|
|
|
case 'w':
|
|
|
|
inprogressmenu ();
|
|
|
|
break;
|
|
|
|
case 'o':
|
|
|
|
if (loggedin)
|
|
|
|
editoptions ();
|
|
|
|
break;
|
|
|
|
case 'q':
|
|
|
|
endwin ();
|
|
|
|
graceful_exit(0);
|
|
|
|
/* break; */
|
|
|
|
case 'r':
|
|
|
|
if (!loggedin) /*not visible to loggedin */
|
|
|
|
newuser ();
|
|
|
|
break;
|
|
|
|
case 'l':
|
|
|
|
if (!loggedin) /* not visible to loggedin */
|
2004-01-31 04:04:58 +01:00
|
|
|
loginprompt (0);
|
2004-01-28 17:31:37 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2004-01-04 03:03:26 +01:00
|
|
|
}
|
|
|
|
|
2003-12-30 22:30:32 +01:00
|
|
|
int
|
2004-01-05 02:57:25 +01:00
|
|
|
main (int argc, char** argv)
|
2003-12-30 22:30:32 +01:00
|
|
|
{
|
2003-12-31 20:47:49 +01:00
|
|
|
/* for chroot and program execution */
|
|
|
|
char atrcfilename[81], *spool;
|
|
|
|
unsigned int len;
|
2004-02-01 21:53:08 +01:00
|
|
|
int c;
|
2003-12-31 20:47:49 +01:00
|
|
|
|
2004-02-03 00:28:17 +01:00
|
|
|
while ((c = getopt(argc, argv, "qh:pp:f:")) != -1)
|
2004-01-31 07:23:02 +01:00
|
|
|
{
|
2004-02-01 21:53:08 +01:00
|
|
|
switch (c)
|
2004-02-01 21:46:08 +01:00
|
|
|
{
|
2004-02-01 21:53:08 +01:00
|
|
|
case 'q':
|
|
|
|
silent = 1; break;
|
|
|
|
|
2004-02-02 23:57:54 +01:00
|
|
|
case 'f':
|
|
|
|
if (config)
|
|
|
|
{
|
|
|
|
if (!silent)
|
|
|
|
fprintf(stderr, "warning: using %s\n", argv[optind]);
|
|
|
|
free(config);
|
|
|
|
}
|
|
|
|
|
|
|
|
config = strdup(optarg);
|
|
|
|
break;
|
|
|
|
|
2004-02-01 21:53:08 +01:00
|
|
|
default:
|
|
|
|
break; /*ignore */
|
2004-02-01 21:46:08 +01:00
|
|
|
}
|
2004-02-01 21:53:08 +01:00
|
|
|
}
|
|
|
|
|
2004-01-05 02:55:08 +01:00
|
|
|
create_config();
|
|
|
|
|
2003-12-31 20:47:49 +01:00
|
|
|
/* signal handlers */
|
|
|
|
signal (SIGHUP, catch_sighup);
|
|
|
|
|
|
|
|
/* get master tty just before chroot (lives in /dev) */
|
|
|
|
ttyrec_getmaster ();
|
2004-01-02 21:47:53 +01:00
|
|
|
#ifndef USE_OPENPTY
|
2003-12-31 20:47:49 +01:00
|
|
|
grantpt (master);
|
|
|
|
unlockpt (master);
|
|
|
|
if ((slave = open ((const char *) ptsname (master), O_RDWR)) < 0)
|
|
|
|
{
|
2004-01-04 02:45:21 +01:00
|
|
|
graceful_exit (65);
|
2003-12-31 20:47:49 +01:00
|
|
|
}
|
2004-01-02 21:47:53 +01:00
|
|
|
#endif
|
2003-12-31 20:47:49 +01:00
|
|
|
|
|
|
|
|
|
|
|
/* chroot */
|
2004-01-05 02:55:08 +01:00
|
|
|
if (chroot (myconfig->chroot))
|
2003-12-31 20:47:49 +01:00
|
|
|
{
|
|
|
|
perror ("cannot change root directory");
|
2004-01-04 02:45:21 +01:00
|
|
|
graceful_exit (1);
|
2003-12-31 20:47:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (chdir ("/"))
|
|
|
|
{
|
|
|
|
perror ("cannot chdir to root directory");
|
2004-01-04 02:45:21 +01:00
|
|
|
graceful_exit (1);
|
2003-12-31 20:47:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* shed privs. this is done immediately after chroot. */
|
2004-01-05 02:55:08 +01:00
|
|
|
if (setgroups (1, &myconfig->shed_gid) == -1)
|
2004-01-03 04:51:29 +01:00
|
|
|
{
|
|
|
|
perror ("setgroups");
|
2004-01-04 02:45:21 +01:00
|
|
|
graceful_exit (1);
|
2004-01-03 04:51:29 +01:00
|
|
|
}
|
2004-01-02 21:39:02 +01:00
|
|
|
|
2004-01-05 02:55:08 +01:00
|
|
|
if (setgid (myconfig->shed_gid) == -1)
|
2004-01-03 04:51:29 +01:00
|
|
|
{
|
|
|
|
perror ("setgid");
|
2004-01-04 02:45:21 +01:00
|
|
|
graceful_exit (1);
|
2004-01-03 04:51:29 +01:00
|
|
|
}
|
2004-01-02 21:39:02 +01:00
|
|
|
|
2004-01-05 02:55:08 +01:00
|
|
|
if (setuid (myconfig->shed_uid) == -1)
|
2004-01-03 04:51:29 +01:00
|
|
|
{
|
|
|
|
perror ("setuid");
|
2004-01-04 02:45:21 +01:00
|
|
|
graceful_exit (1);
|
2004-01-03 04:51:29 +01:00
|
|
|
}
|
2003-12-31 20:47:49 +01:00
|
|
|
|
|
|
|
/* simple login routine, uses ncurses */
|
|
|
|
if (readfile (0))
|
2004-01-04 02:45:21 +01:00
|
|
|
graceful_exit (110);
|
2003-12-31 20:47:49 +01:00
|
|
|
|
2004-01-26 09:29:04 +01:00
|
|
|
initcurses ();
|
2004-01-28 17:31:37 +01:00
|
|
|
menuloop();
|
2003-12-31 20:47:49 +01:00
|
|
|
|
|
|
|
assert (loggedin);
|
|
|
|
|
2004-01-28 17:31:37 +01:00
|
|
|
while (!purge_stale_locks())
|
|
|
|
menuloop();
|
2004-01-04 03:31:00 +01:00
|
|
|
|
2004-01-04 03:20:54 +01:00
|
|
|
endwin ();
|
2004-01-06 15:27:56 +01:00
|
|
|
signal(SIGWINCH, SIG_DFL);
|
2004-01-04 03:19:12 +01:00
|
|
|
|
2003-12-31 20:47:49 +01:00
|
|
|
/* environment */
|
|
|
|
snprintf (atrcfilename, 81, "@%s", rcfilename);
|
|
|
|
|
2004-01-05 02:55:08 +01:00
|
|
|
len = strlen(myconfig->spool) + strlen (me->username) + 1;
|
2003-12-31 20:47:49 +01:00
|
|
|
spool = malloc (len + 1);
|
2004-01-06 14:11:07 +01:00
|
|
|
snprintf (spool, len + 1, "%s/%s", myconfig->spool, me->username);
|
2003-12-31 20:47:49 +01:00
|
|
|
|
2004-01-16 03:00:47 +01:00
|
|
|
mysetenv ("NETHACKOPTIONS", atrcfilename, 1);
|
|
|
|
mysetenv ("MAIL", spool, 1);
|
|
|
|
mysetenv ("SIMPLEMAIL", "1", 1);
|
2003-12-31 20:47:49 +01:00
|
|
|
|
|
|
|
/* don't let the mail file grow */
|
|
|
|
if (access (spool, F_OK) == 0)
|
|
|
|
unlink (spool);
|
|
|
|
|
|
|
|
free (spool);
|
|
|
|
|
|
|
|
/* launch program */
|
2004-01-25 23:34:35 +01:00
|
|
|
ttyrec_main (me->username, gen_ttyrec_filename());
|
2003-12-31 20:47:49 +01:00
|
|
|
|
|
|
|
/* NOW we can safely kill this */
|
|
|
|
freefile ();
|
|
|
|
|
|
|
|
if (me)
|
|
|
|
free (me);
|
|
|
|
|
2004-01-04 02:45:21 +01:00
|
|
|
graceful_exit (1);
|
2003-12-31 20:47:49 +01:00
|
|
|
return 1;
|
2003-12-30 22:30:32 +01:00
|
|
|
}
|