1 /* update-game-score.c --- Update a score file
2 Copyright (C) 2002 Free Software Foundation, Inc.
4 This file is part of GNU Emacs.
6 GNU Emacs is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GNU Emacs is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Emacs; see the file COPYING. If not, write to
18 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
21 /* This program is allows a game to securely and atomically update a
22 score file. It should be installed setuid, owned by an appropriate
25 Alternatively, it can be compiled without HAVE_SHARED_GAME_DIR
26 defined, and in that case it will store scores in the user's home
27 directory (it should NOT be setuid).
29 Created 2002/03/22, by Colin Walters <walters@debian.org>
48 #define MAX_ATTEMPTS 5
49 #define MAX_SCORES 200
50 #define MAX_DATA_LEN 1024
52 #if !defined (__GNUC__) || __GNUC__ < 2
53 #define __attribute__(x)
56 /* Declare the prototype for a general external function. */
57 #if defined (PROTOTYPES) || defined (WINDOWSNT)
58 #define P_(proto) proto
67 fprintf(stdout
, "Usage: update-game-score [-m MAX ] [ -r ] game/scorefile SCORE DATA\n");
68 fprintf(stdout
, " update-game-score -h\n");
69 fprintf(stdout
, " -h\t\tDisplay this help.\n");
70 fprintf(stdout
, " -m MAX\t\tLimit the maximum number of scores to MAX.\n");
71 fprintf(stdout
, " -r\t\tSort the scores in increasing order.\n");
72 fprintf(stdout
, " -d DIR\t\tStore scores in DIR (only if not setuid).\n");
77 lock_file
P_((const char *filename
, void **state
));
79 unlock_file
P_((const char *filename
, void *state
));
89 read_scores
P_((const char *filename
, struct score_entry
**scores
,
92 push_score
P_((struct score_entry
**scores
, int *count
,
93 int newscore
, char *username
, char *newdata
));
95 sort_scores
P_((struct score_entry
*scores
, int count
, int reverse
));
97 write_scores
P_((const char *filename
, const struct score_entry
*scores
,
100 void lose
P_((const char *msg
))
101 __attribute__ ((noreturn
));
106 fprintf(stderr
, "%s\n", msg
);
110 void lose_syserr
P_((const char *msg
))
111 __attribute__ ((noreturn
));
113 void lose_syserr(msg
)
116 fprintf(stderr
, "%s: %s\n", msg
, strerror(errno
));
124 struct passwd
*buf
= getpwuid(getuid());
128 int uid
= (int) getuid();
132 name
= malloc(count
+1);
135 sprintf(name
, "%d", uid
);
142 get_prefix(running_suid
, user_prefix
)
146 if (!running_suid
&& user_prefix
== NULL
)
147 lose("Not using a shared game directory, and no prefix given.");
150 #ifdef HAVE_SHARED_GAME_DIR
151 return HAVE_SHARED_GAME_DIR
;
153 lose("This program was compiled without HAVE_SHARED_GAME_DIR,\n and should not be suid.");
166 char *user_id
, *scorefile
, *prefix
, *user_prefix
= NULL
;
168 struct score_entry
*scores
;
169 int newscore
, scorecount
, reverse
= 0, max
= MAX_SCORES
;
174 while ((c
= getopt(argc
, argv
, "hrm:d:")) != -1)
181 user_prefix
= optarg
;
188 if (max
> MAX_SCORES
)
195 if (optind
+3 != argc
)
198 running_suid
= (getuid() != geteuid());
200 prefix
= get_prefix(running_suid
, user_prefix
);
202 scorefile
= malloc(strlen(prefix
) + strlen(argv
[optind
]) + 2);
204 lose_syserr("Couldn't allocate score file");
206 strcpy(scorefile
, prefix
);
207 strcat(scorefile
, "/");
208 strcat(scorefile
, argv
[optind
]);
209 newscore
= atoi(argv
[optind
+1]);
210 newdata
= argv
[optind
+2];
211 if (strlen(newdata
) > MAX_DATA_LEN
)
212 newdata
[MAX_DATA_LEN
] = '\0';
214 if ((user_id
= get_user_id()) == NULL
)
215 lose_syserr("Couldn't determine user id");
217 if (stat(scorefile
, &buf
) < 0)
218 lose_syserr("Failed to access scores file");
220 if (lock_file(scorefile
, &lockstate
) < 0)
221 lose_syserr("Failed to lock scores file");
223 if (read_scores(scorefile
, &scores
, &scorecount
) < 0)
225 unlock_file(scorefile
, lockstate
);
226 lose_syserr("Failed to read scores file");
228 push_score(&scores
, &scorecount
, newscore
, user_id
, newdata
);
229 /* Limit the number of scores. If we're using reverse sorting, then
230 we should increment the beginning of the array, to skip over the
231 *smallest* scores. Otherwise, we just decrement the number of
232 scores, since the smallest will be at the end. */
233 if (scorecount
> MAX_SCORES
)
234 scorecount
-= (scorecount
- MAX_SCORES
);
236 scores
+= (scorecount
- MAX_SCORES
);
237 sort_scores(scores
, scorecount
, reverse
);
238 if (write_scores(scorefile
, scores
, scorecount
) < 0)
240 unlock_file(scorefile
, lockstate
);
241 lose_syserr("Failed to write scores file");
243 unlock_file(scorefile
, lockstate
);
250 struct score_entry
*score
;
255 while ((c
= getc(f
)) != EOF
259 score
->score
+= (c
-48);
261 while ((c
= getc(f
)) != EOF
270 if (getdelim(&score
->username
, &count
, ' ', f
) < 1
271 || score
->username
== NULL
)
274 score
->username
[strlen(score
->username
)-1] = '\0';
280 char *username
= malloc(unamelen
);
284 while ((c
= getc(f
)) != EOF
287 if (unameread
>= unamelen
-1)
288 if (!(username
= realloc(username
, unamelen
*= 2)))
290 username
[unameread
] = c
;
295 username
[unameread
] = '\0';
296 score
->username
= username
;
304 if (getline(&score
->data
, &len
, f
) < 0)
306 score
->data
[strlen(score
->data
)-1] = '\0';
312 char *buf
= malloc(len
);
315 while ((c
= getc(f
)) != EOF
320 if (!(buf
= realloc(buf
, len
*= 2)))
327 score
->data
[cur
] = '\0';
334 read_scores(filename
, scores
, count
)
335 const char *filename
;
336 struct score_entry
**scores
;
339 int readval
, scorecount
, cursize
;
340 struct score_entry
*ret
;
341 FILE *f
= fopen(filename
, "r");
346 ret
= malloc(sizeof(struct score_entry
) * cursize
);
349 while ((readval
= read_score(f
, &ret
[scorecount
])) == 0)
351 /* We encoutered an error */
355 if (scorecount
>= cursize
)
357 ret
= realloc(ret
, cursize
*= 2);
372 const struct score_entry
*sa
= (const struct score_entry
*) a
;
373 const struct score_entry
*sb
= (const struct score_entry
*) b
;
374 return (sb
->score
> sa
->score
) - (sb
->score
< sa
->score
);
378 score_compare_reverse(a
, b
)
382 const struct score_entry
*sa
= (const struct score_entry
*) a
;
383 const struct score_entry
*sb
= (const struct score_entry
*) b
;
384 return (sa
->score
> sb
->score
) - (sa
->score
< sb
->score
);
388 push_score(scores
, count
, newscore
, username
, newdata
)
389 struct score_entry
**scores
;
390 int *count
; int newscore
;
394 struct score_entry
*newscores
= realloc(*scores
,
395 sizeof(struct score_entry
) * ((*count
) + 1));
398 newscores
[*count
].score
= newscore
;
399 newscores
[*count
].username
= username
;
400 newscores
[*count
].data
= newdata
;
407 sort_scores(scores
, count
, reverse
)
408 struct score_entry
*scores
;
412 qsort(scores
, count
, sizeof(struct score_entry
),
413 reverse
? score_compare_reverse
: score_compare
);
417 write_scores(filename
, scores
, count
)
418 const char *filename
;
419 const struct score_entry
* scores
;
424 char *tempfile
= malloc(strlen(filename
) + strlen(".tempXXXXXX") + 1);
427 strcpy(tempfile
, filename
);
428 strcat(tempfile
, ".tempXXXXXX");
430 if (mkstemp(tempfile
) < 0
432 if (mktemp(tempfile
) != tempfile
434 || !(f
= fopen(tempfile
, "w")))
436 for (i
= 0; i
< count
; i
++)
437 if (fprintf(f
, "%ld %s %s\n", scores
[i
].score
, scores
[i
].username
,
441 if (rename(tempfile
, filename
) < 0)
443 if (chmod(filename
, 0644) < 0)
449 lock_file(filename
, state
)
450 const char *filename
;
456 char *lockext
= ".lockfile";
457 char *lockpath
= malloc(strlen(filename
) + strlen(lockext
) + 60);
460 strcpy(lockpath
, filename
);
461 strcat(lockpath
, lockext
);
465 /* If the lock is over an hour old, delete it. */
466 if (stat(lockpath
, &buf
) == 0
467 && (difftime(buf
.st_ctime
, time(NULL
) > 60*60)))
469 if ((fd
= open(lockpath
, O_CREAT
| O_EXCL
, 0600)) < 0)
473 /* Break the lock; we won't corrupt the file, but we might
475 if (attempts
> MAX_ATTEMPTS
)
480 sleep((rand() % 2)+1);
491 unlock_file(filename
, state
)
492 const char *filename
;
495 char *lockpath
= (char *) state
;
496 int ret
= unlink(lockpath
);
497 int saved_errno
= errno
;