Path: utzoo!mnetor!uunet!lll-winken!lll-lcc!rutgers!cmcl2!brl-adm!umd5!mimsy!chris From: chris@mimsy.UUCP (Chris Torek) Newsgroups: comp.sources.bugs Subject: Re: Problem in Dan Heller's "dots" game Message-ID: <9982@mimsy.UUCP> Date: 30 Dec 87 20:33:06 GMT References: <7765@eddie.MIT.EDU> Organization: U of Maryland, Dept. of Computer Science, Coll. Pk., MD 20742 Lines: 88 In article <7765@eddie.MIT.EDU>, billw@oberon.lcs.mit.edu (Bill Wisner) writes: >Due to a problem with the format of /etc/utmp, dots can't handle 2-player >games against someone with an eight-letter username. The following replacement >for findem.c alleviates the problem. As it says in , `Assuming the number 8 is unwise.' There are several other potential bugs in this routine (I spotted three offhand). I would change it to something more like this: /* findem.c find the login of the person we wanna play */ #include "dots.h" #include #include #include #define UTMP "/etc/utmp" struct utmp utmp_buf; struct stat stat_buf; struct sgttyb sgtty_buf; int fd; #define NMAX sizeof(utmp_buf.ut_name) #define LMAX sizeof(utmp_buf.ut_line) #define DEVPART "/dev/" #define DEVLEN 5 /* strlen(DEVPART) */ char *ttyname(); findem(argc, argv) int argc; char **argv; { register char *login = argv[2], *where = 0, *ourtty = ttyname(0); register FILE *recipient; int found; char *to_tty[DEVLEN + LMAX + 1]; if (ourtty == 0) { fprintf(stderr, "%s: can't find your tty\n"); return 0; } ourtty += DEVLEN; if (argc > 3) { where = argv[3]; if (strcmp(where, ourtty) == 0) { fprintf(stderr, "You can't play yourself.\n"); return 0; } } if ((fd = open(UTMP, 0)) == -1) { perror(UTMP); return 0; } found = 0; while (read(fd, (char *)&utmp_buf, sizeof(utmp_buf)) > 0) { if (strncmp(utmp_buf.ut_name, login, NMAX) != 0) continue; found = -1; if (where == 0 || strncmp(utmp_buf.ut_line, where, LMAX) == 0)) { found = 1; break; } } (void) close(fd); if (found < 1) { fprintf(stderr, "%s is not logged in%s%s.\n", login, found ? " on " : "", found ? where : ""); return 0; } /* N.B.: %.*s may be dubious under dpANS */ (void) sprintf(to_tty, "%s%.*s", DEVPART, LMAX, utmp_buf.ut_line); if ((recipient = fopen(to_tty, "w")) == 0) { perror(to_tty); fprintf(stderr, "%s: Can't ask %s to play.\n", argv[0], argv[2]); return 0; } setgid(getgid());/* probably isn't necessary most of the time */ setuid(getuid());/* turns off set-uid attribute once tty is opened */ return invite(recipient); } -- In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 7690) Domain: chris@mimsy.umd.edu Path: uunet!mimsy!chris