-# $NetBSD: Makefile,v 1.10 1998/02/18 22:37:32 jtc Exp $
+# $NetBSD: Makefile,v 1.11 2000/02/09 22:27:55 jsm Exp $
# @(#)Makefile 8.1 (Berkeley) 5/31/93
PROG= sail
HIDEGAME=hidegame
SETGIDGAME=yes
+afterinstall:
+.if !defined(UNPRIVILEGED)
+ mkdir ${DESTDIR}/var/games/sail
+ chown root.games ${DESTDIR}/var/games/sail
+ chmod 2770 ${DESTDIR}/var/games/sail
+.endif
+
.include <bsd.prog.mk>
-/* $NetBSD: dr_main.c,v 1.5 1997/10/13 19:43:54 christos Exp $ */
+/* $NetBSD: dr_main.c,v 1.6 2000/02/09 22:27:55 jsm Exp $ */
/*
* Copyright (c) 1983, 1993
#if 0
static char sccsid[] = "@(#)dr_main.c 8.2 (Berkeley) 4/16/94";
#else
-__RCSID("$NetBSD: dr_main.c,v 1.5 1997/10/13 19:43:54 christos Exp $");
+__RCSID("$NetBSD: dr_main.c,v 1.6 2000/02/09 22:27:55 jsm Exp $");
#endif
#endif /* not lint */
(void) signal(SIGINT, SIG_IGN);
(void) signal(SIGQUIT, SIG_IGN);
(void) signal(SIGTSTP, SIG_IGN);
- if (issetuid)
- (void) setuid(geteuid());
if (game < 0 || game >= NSCENE) {
fprintf(stderr, "DRIVER: Bad game number %d\n", game);
exit(1);
-/* $NetBSD: extern.h,v 1.13 1999/12/28 18:05:24 jsm Exp $ */
+/* $NetBSD: extern.h,v 1.14 2000/02/09 22:27:55 jsm Exp $ */
/*
* Copyright (c) 1983, 1993
#include <string.h>
#include <ctype.h>
#include <setjmp.h>
+#include <sys/types.h>
#include "machdep.h"
/* program mode */
extern char nobells; /* -b, don't ring bell before Signal */
/* other initial modes */
-extern char issetuid; /* running setuid */
+extern gid_t gid;
+extern gid_t egid;
#define die() ((rand() >> 3) % 6 + 1)
#define sqr(a) ((a) * (a))
-/* $NetBSD: globals.c,v 1.8 1999/12/28 18:05:24 jsm Exp $ */
+/* $NetBSD: globals.c,v 1.9 2000/02/09 22:27:56 jsm Exp $ */
/*
* Copyright (c) 1983, 1993
#if 0
static char sccsid[] = "@(#)globals.c 8.2 (Berkeley) 4/28/95";
#else
-__RCSID("$NetBSD: globals.c,v 1.8 1999/12/28 18:05:24 jsm Exp $");
+__RCSID("$NetBSD: globals.c,v 1.9 2000/02/09 22:27:56 jsm Exp $");
#endif
#endif /* not lint */
char longfmt; /* -l, print score in long format */
char nobells; /* -b, don't ring bell before Signal */
-char issetuid;
+gid_t gid;
+gid_t egid;
struct scenario *cc; /* the current scenario */
struct ship *ls; /* &cc->ship[cc->vessels] */
-/* $NetBSD: main.c,v 1.6 1997/10/13 21:03:55 christos Exp $ */
+/* $NetBSD: main.c,v 1.7 2000/02/09 22:27:56 jsm Exp $ */
/*
* Copyright (c) 1983, 1993
#if 0
static char sccsid[] = "@(#)main.c 8.2 (Berkeley) 4/28/95";
#else
-__RCSID("$NetBSD: main.c,v 1.6 1997/10/13 21:03:55 christos Exp $");
+__RCSID("$NetBSD: main.c,v 1.7 2000/02/09 22:27:56 jsm Exp $");
#endif
#endif /* not lint */
#include "extern.h"
+#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
{
char *p;
int i;
+ int fd;
+
+ gid = getgid();
+ egid = getegid();
+ setegid(gid);
+
+ fd = open("/dev/null", O_RDONLY);
+ if (fd < 3)
+ exit(1);
+ close(fd);
(void) srand(getpid());
- issetuid = getuid() != geteuid();
if ((p = strrchr(*argv, '/')) != NULL)
p++;
else
-/* $NetBSD: misc.c,v 1.5 1997/10/13 19:44:38 christos Exp $ */
+/* $NetBSD: misc.c,v 1.6 2000/02/09 22:27:56 jsm Exp $ */
/*
* Copyright (c) 1983, 1993
#if 0
static char sccsid[] = "@(#)misc.c 8.2 (Berkeley) 4/28/95";
#else
-__RCSID("$NetBSD: misc.c,v 1.5 1997/10/13 19:44:38 christos Exp $");
+__RCSID("$NetBSD: misc.c,v 1.6 2000/02/09 22:27:56 jsm Exp $");
#endif
#endif /* not lint */
float net;
struct logs *lp;
- if ((fp = fopen(_PATH_LOGFILE, "r+")) == NULL)
+ setegid(egid);
+ if ((fp = fopen(_PATH_LOGFILE, "r+")) == NULL) {
+ setegid(gid);
return;
+ }
+ setegid(gid);
#ifdef LOCK_EX
if (flock(fileno(fp), LOCK_EX) < 0)
return;
-/* $NetBSD: pathnames.h,v 1.3 1995/04/22 10:37:06 cgd Exp $ */
+/* $NetBSD: pathnames.h,v 1.4 2000/02/09 22:27:56 jsm Exp $ */
/*-
* Copyright (c) 1990, 1993
*/
#define _PATH_LOGFILE "/var/games/saillog"
+#define _PATH_SYNC "/var/games/sail/#sailsink.%d"
+#define _PATH_LOCK "/var/games/sail/#saillock.%d"
-/* $NetBSD: sync.c,v 1.13 1999/09/09 17:30:20 jsm Exp $ */
+/* $NetBSD: sync.c,v 1.14 2000/02/09 22:27:56 jsm Exp $ */
/*
* Copyright (c) 1983, 1993
#if 0
static char sccsid[] = "@(#)sync.c 8.2 (Berkeley) 4/28/95";
#else
-__RCSID("$NetBSD: sync.c,v 1.13 1999/09/09 17:30:20 jsm Exp $");
+__RCSID("$NetBSD: sync.c,v 1.14 2000/02/09 22:27:56 jsm Exp $");
#endif
#endif /* not lint */
#include <sys/stat.h>
#include <time.h>
#include "extern.h"
+#include "pathnames.h"
#define BUFSIZE 4096
+static const char SF[] = _PATH_SYNC;
+static const char LF[] = _PATH_LOCK;
static char sync_buf[BUFSIZE];
static char *sync_bp = sync_buf;
-static char sync_lock[25];
-static char sync_file[25];
+static char sync_lock[sizeof SF];
+static char sync_file[sizeof LF];
static long sync_seek;
static FILE *sync_fp;
-#define SF "/tmp/#sailsink.%d"
-#define LF "/tmp/#saillock.%d"
void
fmtship(buf, len, fmt, ship)
(void) sprintf(buf, SF, game);
(void) time(&t);
- if (stat(buf, &s) < 0)
+ setegid(egid);
+ if (stat(buf, &s) < 0) {
+ setegid(gid);
return 0;
+ }
if (s.st_mtime < t - 60*60*2) { /* 2 hours */
(void) unlink(buf);
(void) sprintf(buf, LF, game);
(void) unlink(buf);
+ setegid(gid);
return 0;
- } else
+ } else {
+ setegid(gid);
return 1;
+ }
}
int
sync_open()
{
+ struct stat tmp;
if (sync_fp != NULL)
(void) fclose(sync_fp);
(void) sprintf(sync_lock, LF, game);
(void) sprintf(sync_file, SF, game);
- if (access(sync_file, 0) < 0) {
- int omask = umask(issetuid ? 077 : 011);
+ setegid(egid);
+ if (stat(sync_file, &tmp) < 0) {
+ mode_t omask = umask(002);
sync_fp = fopen(sync_file, "w+");
(void) umask(omask);
} else
sync_fp = fopen(sync_file, "r+");
+ setegid(gid);
if (sync_fp == NULL)
return -1;
sync_seek = 0;
{
if (sync_fp != 0)
(void) fclose(sync_fp);
- if (remove)
+ if (remove) {
+ setegid(egid);
(void) unlink(sync_file);
+ setegid(gid);
+ }
}
void
if (errno != EWOULDBLOCK)
return -1;
#else
- if (link(sync_file, sync_lock) >= 0)
+ setegid(egid);
+ if (link(sync_file, sync_lock) >= 0) {
+ setegid(gid);
break;
+ }
+ setegid(gid);
if (errno != EEXIST)
return -1;
#endif
#ifdef LOCK_EX
(void) flock(fileno(sync_fp), LOCK_UN);
#else
+ setegid(egid);
(void) unlink(sync_lock);
+ setegid(gid);
#endif
(void) signal(SIGHUP, sighup);
(void) signal(SIGINT, sigint);