Robots has a nicely implemented score file, so I decided not to touch
it, except for moving to $HOME by default.  Needs pledge("getpw")
because of that.

Index: robots/main.c
===================================================================
RCS file: /cvs/src/games/robots/main.c,v
retrieving revision 1.20
diff -u -p -r1.20 main.c
--- robots/main.c       26 Aug 2015 00:29:24 -0000      1.20
+++ robots/main.c       29 Nov 2015 13:39:57 -0000
@@ -43,22 +43,31 @@ int
 main(int ac, char *av[])
 {
        bool            show_only;
-       extern char     *Scorefile;
+       extern char     Scorefile[PATH_MAX];
        int             score_wfd;     /* high score writable file descriptor */
        int             score_err = 0; /* hold errno from score file open */
        int             ch;
+       int             ret;
        extern int      optind;
-       gid_t           gid;
+       char            *home;
 #ifdef FANCY
        char            *sp;
 #endif
 
-       if ((score_wfd = open(Scorefile, O_RDWR)) < 0)
-               score_err = errno;
+       if (pledge("stdio rpath wpath cpath getpw tty", NULL) == -1)
+               err(1, "pledge");
+
+       home = getenv("HOME");
+       if (home == NULL || *home == '\0')
+               err(1, "getenv");
 
-       /* revoke privs */
-       gid = getgid();
-       setresgid(gid, gid, gid);
+       ret = snprintf(Scorefile, sizeof(Scorefile), "%s/%s", home,
+           ".robots.scores");
+       if (ret < 0 || ret >= PATH_MAX)
+               errc(1, ENAMETOOLONG, "%s/%s", home, ".robots.scores");
+
+       if ((score_wfd = open(Scorefile, O_RDWR | O_CREAT, 0666)) < 0)
+               score_err = errno;
 
        show_only = FALSE;
        while ((ch = getopt(ac, av, "srajt")) != -1)
@@ -90,11 +99,11 @@ main(int ac, char *av[])
        if (ac > 1)
                usage();
        if (ac == 1) {
-               Scorefile = av[0];
+               strlcpy(Scorefile, av[0], sizeof(Scorefile));
                if (score_wfd >= 0)
                        close(score_wfd);
                /* This file requires no special privileges. */
-               if ((score_wfd = open(Scorefile, O_RDWR)) < 0)
+               if ((score_wfd = open(Scorefile, O_RDWR | O_CREAT, 0666)) < 0)
                        score_err = errno;
 #ifdef FANCY
                sp = strrchr(Scorefile, '/');
Index: robots/pathnames.h
===================================================================
RCS file: robots/pathnames.h
diff -N robots/pathnames.h
--- robots/pathnames.h  3 Jun 2003 03:01:41 -0000       1.3
+++ /dev/null   1 Jan 1970 00:00:00 -0000
@@ -1,35 +0,0 @@
-/*     $OpenBSD: pathnames.h,v 1.3 2003/06/03 03:01:41 millert Exp $   */
-/*     $NetBSD: pathnames.h,v 1.3 1995/04/22 10:09:01 cgd Exp $        */
-
-/*-
- * Copyright (c) 1990, 1993
- *     The Regents of the University of California.  All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * 1. Redistributions of source code must retain the above copyright
- *    notice, this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright
- *    notice, this list of conditions and the following disclaimer in the
- *    documentation and/or other materials provided with the distribution.
- * 3. Neither the name of the University nor the names of its contributors
- *    may be used to endorse or promote products derived from this software
- *    without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
- * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
- * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
- * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
- * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
- * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
- * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
- * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
- * SUCH DAMAGE.
- *
- *     @(#)pathnames.h 8.1 (Berkeley) 5/31/93
- */
-
-#define        _PATH_SCORE     "/var/games/robots_roll"
Index: robots/robots.6
===================================================================
RCS file: /cvs/src/games/robots/robots.6,v
retrieving revision 1.13
diff -u -p -r1.13 robots.6
--- robots/robots.6     8 Sep 2014 01:27:54 -0000       1.13
+++ robots/robots.6     29 Nov 2015 13:39:57 -0000
@@ -137,8 +137,8 @@ This is a little disconcerting until you
 very nice.
 .El
 .Sh FILES
-.Bl -tag -width /var/games/robots_roll -compact
-.It Pa /var/games/robots_roll
+.Bl -tag -width $HOME/.robots.scores -compact
+.It Pa $HOME/.robots.scores
 the score file
 .El
 .Sh AUTHORS
Index: robots/robots.h
===================================================================
RCS file: /cvs/src/games/robots/robots.h,v
retrieving revision 1.9
diff -u -p -r1.9 robots.h
--- robots/robots.h     26 Aug 2015 00:29:24 -0000      1.9
+++ robots/robots.h     29 Nov 2015 13:39:57 -0000
@@ -88,8 +88,6 @@ typedef struct {
        char    s_name[LOGIN_NAME_MAX];
 } SCORE;
 
-typedef struct passwd  PASSWD;
-
 /*
  * global variables
  */
Index: robots/score.c
===================================================================
RCS file: /cvs/src/games/robots/score.c,v
retrieving revision 1.11
diff -u -p -r1.11 score.c
--- robots/score.c      16 Nov 2014 04:49:48 -0000      1.11
+++ robots/score.c      29 Nov 2015 13:39:57 -0000
@@ -33,7 +33,7 @@
 #include       "robots.h"
 #include       "pathnames.h"
 
-char   *Scorefile = _PATH_SCORE;
+char   Scorefile[PATH_MAX];
 
 #ifndef MAX_PER_UID
 #define MAX_PER_UID    5
@@ -130,7 +130,7 @@ score(int score_wfd)
 void
 set_name(SCORE *scp)
 {
-       PASSWD  *pp;
+       struct passwd   *pp;
 
        if ((pp = getpwuid(scp->s_uid)) == NULL)
                pp->pw_name = "???";

Reply via email to