]> git.cameronkatri.com Git - bsdgames-darwin.git/blob - dm/dm.c
dm should be suid games. note that perms on /usr/games are wrong,
[bsdgames-darwin.git] / dm / dm.c
1 /*
2 * Copyright (c) 1987 Regents of the University of California.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
34 #ifndef lint
35 char copyright[] =
36 "@(#) Copyright (c) 1987 Regents of the University of California.\n\
37 All rights reserved.\n";
38 #endif /* not lint */
39
40 #ifndef lint
41 /*static char sccsid[] = "from: @(#)dm.c 5.16 (Berkeley) 2/28/91";*/
42 static char rcsid[] = "$Id: dm.c,v 1.3 1993/08/01 18:55:03 mycroft Exp $";
43 #endif /* not lint */
44
45 #include <sys/param.h>
46 #include <sys/file.h>
47 #include <sys/time.h>
48 #include <sys/resource.h>
49 #include <pwd.h>
50 #include <utmp.h>
51 #include <nlist.h>
52 #include <stdio.h>
53 #include <ctype.h>
54 #include <string.h>
55 #include "pathnames.h"
56
57 extern int errno;
58 static time_t now; /* current time value */
59 static int priority = 0; /* priority game runs at */
60 static char *game, /* requested game */
61 *gametty; /* from tty? */
62
63 /*ARGSUSED*/
64 main(argc, argv)
65 int argc;
66 char **argv;
67 {
68 char *cp, *rindex(), *ttyname();
69 time_t time();
70
71 nogamefile();
72 game = (cp = rindex(*argv, '/')) ? ++cp : *argv;
73
74 if (!strcmp(game, "dm"))
75 exit(0);
76
77 gametty = ttyname(0);
78 (void)time(&now);
79 read_config();
80 #ifdef LOG
81 logfile();
82 #endif
83 play(argv);
84 /*NOTREACHED*/
85 }
86
87 /*
88 * play --
89 * play the game
90 */
91 play(args)
92 char **args;
93 {
94 char pbuf[MAXPATHLEN];
95
96 (void)strcpy(pbuf, _PATH_HIDE);
97 (void)strcpy(pbuf + sizeof(_PATH_HIDE) - 1, game);
98 if (priority > 0) /* < 0 requires root */
99 (void)setpriority(PRIO_PROCESS, 0, priority);
100 setgid(getgid()); /* we run setgid kmem; lose it */
101 execv(pbuf, args);
102 (void)fprintf(stderr, "dm: %s: %s\n", pbuf, strerror(errno));
103 exit(1);
104 }
105
106 /*
107 * read_config --
108 * read through config file, looking for key words.
109 */
110 read_config()
111 {
112 FILE *cfp;
113 char lbuf[BUFSIZ], f1[40], f2[40], f3[40], f4[40], f5[40];
114
115 if (!(cfp = fopen(_PATH_CONFIG, "r")))
116 return;
117 while (fgets(lbuf, sizeof(lbuf), cfp))
118 switch(*lbuf) {
119 case 'b': /* badtty */
120 if (sscanf(lbuf, "%s%s", f1, f2) != 2 ||
121 strcasecmp(f1, "badtty"))
122 break;
123 c_tty(f2);
124 break;
125 case 'g': /* game */
126 if (sscanf(lbuf, "%s%s%s%s%s",
127 f1, f2, f3, f4, f5) != 5 || strcasecmp(f1, "game"))
128 break;
129 c_game(f2, f3, f4, f5);
130 break;
131 case 't': /* time */
132 if (sscanf(lbuf, "%s%s%s%s", f1, f2, f3, f4) != 4 ||
133 strcasecmp(f1, "time"))
134 break;
135 c_day(f2, f3, f4);
136 }
137 (void)fclose(cfp);
138 }
139
140 /*
141 * c_day --
142 * if day is today, see if okay to play
143 */
144 c_day(s_day, s_start, s_stop)
145 char *s_day, *s_start, *s_stop;
146 {
147 static char *days[] = {
148 "sunday", "monday", "tuesday", "wednesday",
149 "thursday", "friday", "saturday",
150 };
151 static struct tm *ct;
152 int start, stop;
153
154 if (!ct)
155 ct = localtime(&now);
156 if (strcasecmp(s_day, days[ct->tm_wday]))
157 return;
158 if (!isdigit(*s_start) || !isdigit(*s_stop))
159 return;
160 start = atoi(s_start);
161 stop = atoi(s_stop);
162 if (ct->tm_hour >= start && ct->tm_hour < stop) {
163 fputs("dm: Sorry, games are not available from ", stderr);
164 hour(start);
165 fputs(" to ", stderr);
166 hour(stop);
167 fputs(" today.\n", stderr);
168 exit(0);
169 }
170 }
171
172 /*
173 * c_tty --
174 * decide if this tty can be used for games.
175 */
176 c_tty(tty)
177 char *tty;
178 {
179 static int first = 1;
180 static char *p_tty;
181 char *rindex();
182
183 if (first) {
184 p_tty = rindex(gametty, '/');
185 first = 0;
186 }
187
188 if (!strcmp(gametty, tty) || p_tty && !strcmp(p_tty, tty)) {
189 fprintf(stderr, "dm: Sorry, you may not play games on %s.\n", gametty);
190 exit(0);
191 }
192 }
193
194 /*
195 * c_game --
196 * see if game can be played now.
197 */
198 c_game(s_game, s_load, s_users, s_priority)
199 char *s_game, *s_load, *s_users, *s_priority;
200 {
201 static int found;
202 double load();
203
204 if (found)
205 return;
206 if (strcmp(game, s_game) && strcasecmp("default", s_game))
207 return;
208 ++found;
209 if (isdigit(*s_load) && atoi(s_load) < load()) {
210 fputs("dm: Sorry, the load average is too high right now.\n", stderr);
211 exit(0);
212 }
213 if (isdigit(*s_users) && atoi(s_users) <= users()) {
214 fputs("dm: Sorry, there are too many users logged on right now.\n", stderr);
215 exit(0);
216 }
217 if (isdigit(*s_priority))
218 priority = atoi(s_priority);
219 }
220
221 /*
222 * load --
223 * return 15 minute load average
224 */
225 double
226 load()
227 {
228 double avenrun[3];
229
230 if (getloadavg(avenrun, sizeof(avenrun)/sizeof(avenrun[0])) < 0) {
231 fputs("dm: getloadavg() failed.\n", stderr);
232 exit(1);
233 }
234 return(avenrun[2]);
235 }
236
237 /*
238 * users --
239 * return current number of users
240 * todo: check idle time; if idle more than X minutes, don't
241 * count them.
242 */
243 users()
244 {
245
246 register int nusers, utmp;
247 struct utmp buf;
248
249 if ((utmp = open(_PATH_UTMP, O_RDONLY, 0)) < 0) {
250 (void)fprintf(stderr, "dm: %s: %s\n",
251 _PATH_UTMP, strerror(errno));
252 exit(1);
253 }
254 for (nusers = 0; read(utmp, (char *)&buf, sizeof(struct utmp)) > 0;)
255 if (buf.ut_name[0] != '\0')
256 ++nusers;
257 return(nusers);
258 }
259
260 nogamefile()
261 {
262 register int fd, n;
263 char buf[BUFSIZ];
264
265 if ((fd = open(_PATH_NOGAMES, O_RDONLY, 0)) >= 0) {
266 #define MESG "Sorry, no games right now.\n\n"
267 (void)write(2, MESG, sizeof(MESG) - 1);
268 while ((n = read(fd, buf, sizeof(buf))) > 0)
269 (void)write(2, buf, n);
270 exit(1);
271 }
272 }
273
274 /*
275 * hour --
276 * print out the hour in human form
277 */
278 hour(h)
279 int h;
280 {
281 switch(h) {
282 case 0:
283 fputs("midnight", stderr);
284 break;
285 case 12:
286 fputs("noon", stderr);
287 break;
288 default:
289 if (h > 12)
290 fprintf(stderr, "%dpm", h - 12);
291 else
292 fprintf(stderr, "%dam", h);
293 }
294 }
295
296 #ifdef LOG
297 /*
298 * logfile --
299 * log play of game
300 */
301 logfile()
302 {
303 struct passwd *pw, *getpwuid();
304 FILE *lp;
305 uid_t uid;
306 int lock_cnt;
307 char *ctime();
308
309 if (lp = fopen(_PATH_LOG, "a")) {
310 for (lock_cnt = 0;; ++lock_cnt) {
311 if (!flock(fileno(lp), LOCK_EX))
312 break;
313 if (lock_cnt == 4) {
314 perror("dm: log lock");
315 (void)fclose(lp);
316 return;
317 }
318 sleep((u_int)1);
319 }
320 if (pw = getpwuid(uid = getuid()))
321 fputs(pw->pw_name, lp);
322 else
323 fprintf(lp, "%u", uid);
324 fprintf(lp, "\t%s\t%s\t%s", game, gametty, ctime(&now));
325 (void)fclose(lp);
326 (void)flock(fileno(lp), LOCK_UN);
327 }
328 }
329 #endif /* LOG */