1 | /* Copyright 1988, 1998 by the Massachusetts Institute of Technology. |
---|
2 | * |
---|
3 | * Permission to use, copy, modify, and distribute this |
---|
4 | * software and its documentation for any purpose and without |
---|
5 | * fee is hereby granted, provided that the above copyright |
---|
6 | * notice appear in all copies and that both that copyright |
---|
7 | * notice and this permission notice appear in supporting |
---|
8 | * documentation, and that the name of M.I.T. not be used in |
---|
9 | * advertising or publicity pertaining to distribution of the |
---|
10 | * software without specific, written prior permission. |
---|
11 | * M.I.T. makes no representations about the suitability of |
---|
12 | * this software for any purpose. It is provided "as is" |
---|
13 | * without express or implied warranty. |
---|
14 | */ |
---|
15 | |
---|
16 | static const char rcsid[] = "$Id: check_viewable.c,v 1.1 1999-12-08 22:06:43 danw Exp $"; |
---|
17 | |
---|
18 | #include "globalmessage.h" |
---|
19 | #include <sys/types.h> |
---|
20 | #include <sys/file.h> |
---|
21 | #include <pwd.h> |
---|
22 | |
---|
23 | Code_t check_viewable(char *message, int checktime, int updateuser) |
---|
24 | { |
---|
25 | char *ptr, *usertfile; |
---|
26 | time_t ftime, utime; |
---|
27 | int status, usersize, ufd; |
---|
28 | char *usertfilename; |
---|
29 | |
---|
30 | if(strncmp(message, GMS_VERSION_STRING, GMS_VERSION_STRING_LEN)) { |
---|
31 | return(GMS_SERVER_VERSION); |
---|
32 | } |
---|
33 | |
---|
34 | ptr = strchr(message, '\n'); |
---|
35 | if(!ptr) { |
---|
36 | return(GMS_SERVER_GARBLED); |
---|
37 | } |
---|
38 | |
---|
39 | ftime = atol(&message[GMS_VERSION_STRING_LEN+1]); |
---|
40 | |
---|
41 | { |
---|
42 | struct passwd *pw; |
---|
43 | char *userdir; |
---|
44 | |
---|
45 | pw = getpwuid(getuid()); |
---|
46 | |
---|
47 | if(pw) { |
---|
48 | userdir = pw->pw_dir; |
---|
49 | } else { |
---|
50 | /* couldn't check user's file, probably better send it. */ |
---|
51 | return(0); |
---|
52 | } |
---|
53 | |
---|
54 | usertfilename = malloc(GMS_USERFILE_NAME_LEN+strlen(userdir)+1); |
---|
55 | strcpy(usertfilename, userdir); |
---|
56 | strcat(usertfilename, GMS_USERFILE_NAME); |
---|
57 | |
---|
58 | ufd = open(usertfilename, O_RDONLY, 0666); |
---|
59 | } |
---|
60 | |
---|
61 | if(ufd != -1) { |
---|
62 | /* read the file and close it */ |
---|
63 | status = read_to_memory(&usertfile, &usersize, ufd); |
---|
64 | /* check the version string */ |
---|
65 | if(!status) { |
---|
66 | if(!strncmp(usertfile, GMS_VERSION_STRING, GMS_VERSION_STRING_LEN)) { |
---|
67 | /* now check for end of first line */ |
---|
68 | ptr = strchr(usertfile, '\n'); |
---|
69 | if(ptr) { |
---|
70 | /* now we check the time stamp */ |
---|
71 | utime = atol(&usertfile[GMS_VERSION_STRING_LEN+1]); |
---|
72 | if(ftime <= utime) { |
---|
73 | /* user has already seen, we punt. */ |
---|
74 | free(usertfile); |
---|
75 | free(usertfilename); |
---|
76 | /* but we only punt if they have asked for it... */ |
---|
77 | if(checktime) { |
---|
78 | return(GMS_OLD_MESSAGE); |
---|
79 | } else { |
---|
80 | return(0); |
---|
81 | } |
---|
82 | } |
---|
83 | } |
---|
84 | } |
---|
85 | /* only valid if read_to_memory worked... */ |
---|
86 | free(usertfile); |
---|
87 | } |
---|
88 | } |
---|
89 | |
---|
90 | /* only write out the new time stamp if they want it. */ |
---|
91 | if(updateuser) { |
---|
92 | /* now, reopen/create the file to write new timestamp */ |
---|
93 | ufd = open(usertfilename, O_WRONLY|O_CREAT|O_TRUNC, 0666); |
---|
94 | if(ufd != -1) { |
---|
95 | char *msg = &message[GMS_VERSION_STRING_LEN]; |
---|
96 | |
---|
97 | /* write out the version number */ |
---|
98 | write(ufd, GMS_VERSION_STRING, GMS_VERSION_STRING_LEN); |
---|
99 | |
---|
100 | /* write out the timestring from the message file */ |
---|
101 | ptr = strchr(message, '\n')+1; |
---|
102 | write(ufd, msg, ptr - msg); |
---|
103 | close(ufd); |
---|
104 | } |
---|
105 | } |
---|
106 | /* filename no longer needed... */ |
---|
107 | free(usertfilename); |
---|
108 | /* if we tried to write, we want to print... */ |
---|
109 | return(0); |
---|
110 | } |
---|
111 | |
---|
112 | |
---|
113 | |
---|