]> asedeno.scripts.mit.edu Git - PuTTY.git/blob - unix/uxmisc.c
Giant const-correctness patch of doom!
[PuTTY.git] / unix / uxmisc.c
1 /*
2  * PuTTY miscellaneous Unix stuff
3  */
4
5 #include <fcntl.h>
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <assert.h>
9 #include <errno.h>
10 #include <unistd.h>
11 #include <time.h>
12 #include <sys/time.h>
13 #include <sys/types.h>
14 #include <sys/stat.h>
15 #include <pwd.h>
16
17 #include "putty.h"
18
19 unsigned long getticks(void)
20 {
21     /*
22      * We want to use milliseconds rather than the microseconds or
23      * nanoseconds given by the underlying clock functions, because we
24      * need a decent number of them to fit into a 32-bit word so it
25      * can be used for keepalives.
26      */
27 #if defined HAVE_CLOCK_GETTIME && defined HAVE_DECL_CLOCK_MONOTONIC
28     {
29         /* Use CLOCK_MONOTONIC if available, so as to be unconfused if
30          * the system clock changes. */
31         struct timespec ts;
32         if (clock_gettime(CLOCK_MONOTONIC, &ts) == 0)
33             return ts.tv_sec * TICKSPERSEC +
34                 ts.tv_nsec / (1000000000 / TICKSPERSEC);
35     }
36 #endif
37     {
38         struct timeval tv;
39         gettimeofday(&tv, NULL);
40         return tv.tv_sec * TICKSPERSEC + tv.tv_usec / (1000000 / TICKSPERSEC);
41     }
42 }
43
44 Filename *filename_from_str(const char *str)
45 {
46     Filename *ret = snew(Filename);
47     ret->path = dupstr(str);
48     return ret;
49 }
50
51 Filename *filename_copy(const Filename *fn)
52 {
53     return filename_from_str(fn->path);
54 }
55
56 const char *filename_to_str(const Filename *fn)
57 {
58     return fn->path;
59 }
60
61 int filename_equal(const Filename *f1, const Filename *f2)
62 {
63     return !strcmp(f1->path, f2->path);
64 }
65
66 int filename_is_null(const Filename *fn)
67 {
68     return !fn->path[0];
69 }
70
71 void filename_free(Filename *fn)
72 {
73     sfree(fn->path);
74     sfree(fn);
75 }
76
77 int filename_serialise(const Filename *f, void *vdata)
78 {
79     char *data = (char *)vdata;
80     int len = strlen(f->path) + 1;     /* include trailing NUL */
81     if (data) {
82         strcpy(data, f->path);
83     }
84     return len;
85 }
86 Filename *filename_deserialise(void *vdata, int maxsize, int *used)
87 {
88     char *data = (char *)vdata;
89     char *end;
90     end = memchr(data, '\0', maxsize);
91     if (!end)
92         return NULL;
93     end++;
94     *used = end - data;
95     return filename_from_str(data);
96 }
97
98 #ifdef DEBUG
99 static FILE *debug_fp = NULL;
100
101 void dputs(const char *buf)
102 {
103     if (!debug_fp) {
104         debug_fp = fopen("debug.log", "w");
105     }
106
107     write(1, buf, strlen(buf));
108
109     fputs(buf, debug_fp);
110     fflush(debug_fp);
111 }
112 #endif
113
114 char *get_username(void)
115 {
116     struct passwd *p;
117     uid_t uid = getuid();
118     char *user, *ret = NULL;
119
120     /*
121      * First, find who we think we are using getlogin. If this
122      * agrees with our uid, we'll go along with it. This should
123      * allow sharing of uids between several login names whilst
124      * coping correctly with people who have su'ed.
125      */
126     user = getlogin();
127     setpwent();
128     if (user)
129         p = getpwnam(user);
130     else
131         p = NULL;
132     if (p && p->pw_uid == uid) {
133         /*
134          * The result of getlogin() really does correspond to
135          * our uid. Fine.
136          */
137         ret = user;
138     } else {
139         /*
140          * If that didn't work, for whatever reason, we'll do
141          * the simpler version: look up our uid in the password
142          * file and map it straight to a name.
143          */
144         p = getpwuid(uid);
145         if (!p)
146             return NULL;
147         ret = p->pw_name;
148     }
149     endpwent();
150
151     return dupstr(ret);
152 }
153
154 /*
155  * Display the fingerprints of the PGP Master Keys to the user.
156  * (This is here rather than in uxcons because it's appropriate even for
157  * Unix GUI apps.)
158  */
159 void pgp_fingerprints(void)
160 {
161     fputs("These are the fingerprints of the PuTTY PGP Master Keys. They can\n"
162           "be used to establish a trust path from this executable to another\n"
163           "one. See the manual for more information.\n"
164           "(Note: these fingerprints have nothing to do with SSH!)\n"
165           "\n"
166           "PuTTY Master Key (RSA), 1024-bit:\n"
167           "  " PGP_RSA_MASTER_KEY_FP "\n"
168           "PuTTY Master Key (DSA), 1024-bit:\n"
169           "  " PGP_DSA_MASTER_KEY_FP "\n", stdout);
170 }
171
172 /*
173  * Set and clear fcntl options on a file descriptor. We don't
174  * realistically expect any of these operations to fail (the most
175  * plausible error condition is EBADF, but we always believe ourselves
176  * to be passing a valid fd so even that's an assertion-fail sort of
177  * response), so we don't make any effort to return sensible error
178  * codes to the caller - we just log to standard error and die
179  * unceremoniously. However, nonblock and no_nonblock do return the
180  * previous state of O_NONBLOCK.
181  */
182 void cloexec(int fd) {
183     int fdflags;
184
185     fdflags = fcntl(fd, F_GETFD);
186     if (fdflags < 0) {
187         fprintf(stderr, "%d: fcntl(F_GETFD): %s\n", fd, strerror(errno));
188         exit(1);
189     }
190     if (fcntl(fd, F_SETFD, fdflags | FD_CLOEXEC) < 0) {
191         fprintf(stderr, "%d: fcntl(F_SETFD): %s\n", fd, strerror(errno));
192         exit(1);
193     }
194 }
195 void noncloexec(int fd) {
196     int fdflags;
197
198     fdflags = fcntl(fd, F_GETFD);
199     if (fdflags < 0) {
200         fprintf(stderr, "%d: fcntl(F_GETFD): %s\n", fd, strerror(errno));
201         exit(1);
202     }
203     if (fcntl(fd, F_SETFD, fdflags & ~FD_CLOEXEC) < 0) {
204         fprintf(stderr, "%d: fcntl(F_SETFD): %s\n", fd, strerror(errno));
205         exit(1);
206     }
207 }
208 int nonblock(int fd) {
209     int fdflags;
210
211     fdflags = fcntl(fd, F_GETFL);
212     if (fdflags < 0) {
213         fprintf(stderr, "%d: fcntl(F_GETFL): %s\n", fd, strerror(errno));
214         exit(1);
215     }
216     if (fcntl(fd, F_SETFL, fdflags | O_NONBLOCK) < 0) {
217         fprintf(stderr, "%d: fcntl(F_SETFL): %s\n", fd, strerror(errno));
218         exit(1);
219     }
220
221     return fdflags & O_NONBLOCK;
222 }
223 int no_nonblock(int fd) {
224     int fdflags;
225
226     fdflags = fcntl(fd, F_GETFL);
227     if (fdflags < 0) {
228         fprintf(stderr, "%d: fcntl(F_GETFL): %s\n", fd, strerror(errno));
229         exit(1);
230     }
231     if (fcntl(fd, F_SETFL, fdflags & ~O_NONBLOCK) < 0) {
232         fprintf(stderr, "%d: fcntl(F_SETFL): %s\n", fd, strerror(errno));
233         exit(1);
234     }
235
236     return fdflags & O_NONBLOCK;
237 }
238
239 FILE *f_open(const Filename *filename, char const *mode, int is_private)
240 {
241     if (!is_private) {
242         return fopen(filename->path, mode);
243     } else {
244         int fd;
245         assert(mode[0] == 'w');        /* is_private is meaningless for read,
246                                           and tricky for append */
247         fd = open(filename->path, O_WRONLY | O_CREAT | O_TRUNC, 0600);
248         if (fd < 0)
249             return NULL;
250         return fdopen(fd, mode);
251     }
252 }
253
254 FontSpec *fontspec_new(const char *name)
255 {
256     FontSpec *f = snew(FontSpec);
257     f->name = dupstr(name);
258     return f;
259 }
260 FontSpec *fontspec_copy(const FontSpec *f)
261 {
262     return fontspec_new(f->name);
263 }
264 void fontspec_free(FontSpec *f)
265 {
266     sfree(f->name);
267     sfree(f);
268 }
269 int fontspec_serialise(FontSpec *f, void *data)
270 {
271     int len = strlen(f->name);
272     if (data)
273         strcpy(data, f->name);
274     return len + 1;                    /* include trailing NUL */
275 }
276 FontSpec *fontspec_deserialise(void *vdata, int maxsize, int *used)
277 {
278     char *data = (char *)vdata;
279     char *end = memchr(data, '\0', maxsize);
280     if (!end)
281         return NULL;
282     *used = end - data + 1;
283     return fontspec_new(data);
284 }
285
286 char *make_dir_and_check_ours(const char *dirname)
287 {
288     struct stat st;
289
290     /*
291      * Create the directory. We might have created it before, so
292      * EEXIST is an OK error; but anything else is doom.
293      */
294     if (mkdir(dirname, 0700) < 0 && errno != EEXIST)
295         return dupprintf("%s: mkdir: %s", dirname, strerror(errno));
296
297     /*
298      * Now check that that directory is _owned by us_ and not writable
299      * by anybody else. This protects us against somebody else
300      * previously having created the directory in a way that's
301      * writable to us, and thus manipulating us into creating the
302      * actual socket in a directory they can see so that they can
303      * connect to it and use our authenticated SSH sessions.
304      */
305     if (stat(dirname, &st) < 0)
306         return dupprintf("%s: stat: %s", dirname, strerror(errno));
307     if (st.st_uid != getuid())
308         return dupprintf("%s: directory owned by uid %d, not by us",
309                          dirname, st.st_uid);
310     if ((st.st_mode & 077) != 0)
311         return dupprintf("%s: directory has overgenerous permissions %03o"
312                          " (expected 700)", dirname, st.st_mode & 0777);
313
314     return NULL;
315 }