From d31c5b31e36a96739e526f5db657c063b697e73b Mon Sep 17 00:00:00 2001 From: tb Date: Sun, 29 Nov 2015 15:23:39 +0000 Subject: Add pledge support and move score file to $HOME. For high score entries use the same logic as in snake(6) and tetris(6): Try LOGNAME then USER then getlogin(2) and fall back to ???. For variety, atc(6) uses flock(2), so add a "flock" promise to the usual "stdio rpath rpath cpath tty" for games. --- games/atc/atc.6 | 11 ++++++++--- games/atc/log.c | 50 ++++++++++++++++++++++++++++---------------------- games/atc/main.c | 9 +++------ games/atc/pathnames.h | 3 +-- 4 files changed, 40 insertions(+), 33 deletions(-) (limited to 'games/atc') diff --git a/games/atc/atc.6 b/games/atc/atc.6 index 66975861aa7..bd26475e067 100644 --- a/games/atc/atc.6 +++ b/games/atc/atc.6 @@ -1,4 +1,4 @@ -.\" $OpenBSD: atc.6,v 1.20 2014/11/30 02:41:43 schwarze Exp $ +.\" $OpenBSD: atc.6,v 1.21 2015/11/29 15:23:38 tb Exp $ .\" .\" Copyright (c) 1990, 1993 .\" The Regents of the University of California. All rights reserved. @@ -34,7 +34,7 @@ .\" .\" Copyright (c) 1986 Ed James. All rights reserved. .\" -.Dd $Mdocdate: November 30 2014 $ +.Dd $Mdocdate: November 29 2015 $ .Dt ATC 6 .Os .Sh NAME @@ -585,13 +585,18 @@ line: [ ( 1 1 ) ( 6 6 ) ] [ ( 13 17 ) ( 28 17 ) ] [ ( 1 7 ) ( 11 7 ) ] ; .Ed +.Sh ENVIRONMENT +.Bl -tag -width Ds +.It Ev LOGNAME +Name to be recorded in high score file. +.El .Sh FILES Files are kept in a special directory, which can be shown by using the .Fl p flag. .Pp .Bl -tag -width "/usr/share/games/atc/Game_List" -compact -.It Pa /var/games/atc_score +.It Pa $HOME/.atc.scores Score file. .It Pa /usr/share/games/atc/Game_List The list of playable games. diff --git a/games/atc/log.c b/games/atc/log.c index 7ac6ea31672..575a9264f67 100644 --- a/games/atc/log.c +++ b/games/atc/log.c @@ -1,4 +1,4 @@ -/* $OpenBSD: log.c,v 1.19 2014/12/09 05:01:14 deraadt Exp $ */ +/* $OpenBSD: log.c,v 1.20 2015/11/29 15:23:38 tb Exp $ */ /* $NetBSD: log.c,v 1.3 1995/03/21 15:04:21 cgd Exp $ */ /*- @@ -94,23 +94,30 @@ int open_score_file(void) { mode_t old_mode; + char *home; + char scorefile[PATH_MAX]; + int ret; int score_fd; + home = getenv("HOME"); + if (home == NULL || *home == '\0') + err(1, "getenv"); + ret = snprintf(scorefile, sizeof(scorefile), "%s/%s", home, + ".atc.scores"); + if (ret < 0 || ret >= PATH_MAX) + errc(1, ENAMETOOLONG, "%s/%s", home, ".atc.scores"); + old_mode = umask(0); - score_fd = open(_PATH_SCORE, O_CREAT|O_RDWR, 0664); - if (score_fd < 0) { - perror(_PATH_SCORE); - return (-1); - } + score_fd = open(scorefile, O_CREAT|O_RDWR, 0644); + if (score_fd < 0) + err(1, "open"); /* * This is done to take advantage of stdio, while still * allowing a O_CREAT during the open(2) of the log file. */ score_fp = fdopen(score_fd, "r+"); - if (score_fp == NULL) { - perror(_PATH_SCORE); - return (-1); - } + if (score_fp == NULL) + err(1, "fdopen"); umask(old_mode); return (0); } @@ -119,17 +126,15 @@ int log_score(int list_em) { int i, num_scores = 0, good, changed = 0, found = 0; - struct passwd *pw; + const char *name; char *cp; char scanstr[50]; SCORE score[NUM_SCORES], thisscore; if (score_fp == NULL) return (-1); - if (flock(fileno(score_fp), LOCK_EX) < 0) { - perror("flock"); - return (-1); - } + if (flock(fileno(score_fp), LOCK_EX) < 0) + err(1, "flock"); snprintf(scanstr, 50, "%%%zus %%%zus %%d %%d %%d", sizeof(score[0].name)-1, sizeof(score[0].game)-1); for (;;) { @@ -143,13 +148,14 @@ log_score(int list_em) break; } if (!test_mode && !list_em) { - if ((pw = (struct passwd *) getpwuid(getuid())) == NULL) { - fprintf(stderr, - "getpwuid failed for uid %u. Who are you?\n", - getuid()); - return (-1); - } - strlcpy(thisscore.name, pw->pw_name, sizeof(thisscore.name)); + name = getenv("LOGNAME"); + if (name == NULL || *name == '\0') + name = getenv("USER"); + if (name == NULL || *name == '\0') + name = getlogin(); + if (name == NULL || *name == '\0') + name = " ???"; + strlcpy(thisscore.name, name, sizeof(thisscore.name)); cp = strrchr(file, '/'); if (cp == NULL) { diff --git a/games/atc/main.c b/games/atc/main.c index 032c93c7ab8..d67a5f454ad 100644 --- a/games/atc/main.c +++ b/games/atc/main.c @@ -1,4 +1,4 @@ -/* $OpenBSD: main.c,v 1.24 2015/11/20 16:58:37 tb Exp $ */ +/* $OpenBSD: main.c,v 1.25 2015/11/29 15:23:38 tb Exp $ */ /* $NetBSD: main.c,v 1.4 1995/04/27 21:22:25 mycroft Exp $ */ /*- @@ -54,15 +54,12 @@ main(int argc, char *argv[]) const char *file = NULL; char *seed; struct sigaction sa; - gid_t gid; struct itimerval itv; + if (pledge("stdio rpath wpath cpath flock tty", NULL) == -1) + err(1, "pledge"); open_score_file(); - /* revoke privs */ - gid = getgid(); - setresgid(gid, gid, gid); - start_time = time(0); makenoise = 1; seed = NULL; diff --git a/games/atc/pathnames.h b/games/atc/pathnames.h index 9c513682f8b..2f76ec10629 100644 --- a/games/atc/pathnames.h +++ b/games/atc/pathnames.h @@ -1,4 +1,4 @@ -/* $OpenBSD: pathnames.h,v 1.3 2003/06/03 03:01:38 millert Exp $ */ +/* $OpenBSD: pathnames.h,v 1.4 2015/11/29 15:23:38 tb Exp $ */ /* $NetBSD: pathnames.h,v 1.3 1995/03/21 15:04:28 cgd Exp $ */ /*- @@ -35,4 +35,3 @@ #include #define _PATH_GAMES "/usr/share/games/atc/" -#define _PATH_SCORE "/var/games/atc_score" -- cgit v1.2.3