]> asedeno.scripts.mit.edu Git - PuTTY.git/blob - unix/uxmisc.c
Key rollover: put the new Master Key fingerprint in the tools.
[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     if (write(1, buf, strlen(buf)) < 0) {} /* 'error check' to placate gcc */
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 as of 2015 (RSA, 4096-bit):\n"
167           "  " PGP_MASTER_KEY_FP "\n\n"
168           "Original PuTTY Master Key (RSA, 1024-bit):\n"
169           "  " PGP_RSA_MASTER_KEY_FP "\n"
170           "Original PuTTY Master Key (DSA, 1024-bit):\n"
171           "  " PGP_DSA_MASTER_KEY_FP "\n", stdout);
172 }
173
174 /*
175  * Set and clear fcntl options on a file descriptor. We don't
176  * realistically expect any of these operations to fail (the most
177  * plausible error condition is EBADF, but we always believe ourselves
178  * to be passing a valid fd so even that's an assertion-fail sort of
179  * response), so we don't make any effort to return sensible error
180  * codes to the caller - we just log to standard error and die
181  * unceremoniously. However, nonblock and no_nonblock do return the
182  * previous state of O_NONBLOCK.
183  */
184 void cloexec(int fd) {
185     int fdflags;
186
187     fdflags = fcntl(fd, F_GETFD);
188     if (fdflags < 0) {
189         fprintf(stderr, "%d: fcntl(F_GETFD): %s\n", fd, strerror(errno));
190         exit(1);
191     }
192     if (fcntl(fd, F_SETFD, fdflags | FD_CLOEXEC) < 0) {
193         fprintf(stderr, "%d: fcntl(F_SETFD): %s\n", fd, strerror(errno));
194         exit(1);
195     }
196 }
197 void noncloexec(int fd) {
198     int fdflags;
199
200     fdflags = fcntl(fd, F_GETFD);
201     if (fdflags < 0) {
202         fprintf(stderr, "%d: fcntl(F_GETFD): %s\n", fd, strerror(errno));
203         exit(1);
204     }
205     if (fcntl(fd, F_SETFD, fdflags & ~FD_CLOEXEC) < 0) {
206         fprintf(stderr, "%d: fcntl(F_SETFD): %s\n", fd, strerror(errno));
207         exit(1);
208     }
209 }
210 int nonblock(int fd) {
211     int fdflags;
212
213     fdflags = fcntl(fd, F_GETFL);
214     if (fdflags < 0) {
215         fprintf(stderr, "%d: fcntl(F_GETFL): %s\n", fd, strerror(errno));
216         exit(1);
217     }
218     if (fcntl(fd, F_SETFL, fdflags | O_NONBLOCK) < 0) {
219         fprintf(stderr, "%d: fcntl(F_SETFL): %s\n", fd, strerror(errno));
220         exit(1);
221     }
222
223     return fdflags & O_NONBLOCK;
224 }
225 int no_nonblock(int fd) {
226     int fdflags;
227
228     fdflags = fcntl(fd, F_GETFL);
229     if (fdflags < 0) {
230         fprintf(stderr, "%d: fcntl(F_GETFL): %s\n", fd, strerror(errno));
231         exit(1);
232     }
233     if (fcntl(fd, F_SETFL, fdflags & ~O_NONBLOCK) < 0) {
234         fprintf(stderr, "%d: fcntl(F_SETFL): %s\n", fd, strerror(errno));
235         exit(1);
236     }
237
238     return fdflags & O_NONBLOCK;
239 }
240
241 FILE *f_open(const Filename *filename, char const *mode, int is_private)
242 {
243     if (!is_private) {
244         return fopen(filename->path, mode);
245     } else {
246         int fd;
247         assert(mode[0] == 'w');        /* is_private is meaningless for read,
248                                           and tricky for append */
249         fd = open(filename->path, O_WRONLY | O_CREAT | O_TRUNC, 0600);
250         if (fd < 0)
251             return NULL;
252         return fdopen(fd, mode);
253     }
254 }
255
256 FontSpec *fontspec_new(const char *name)
257 {
258     FontSpec *f = snew(FontSpec);
259     f->name = dupstr(name);
260     return f;
261 }
262 FontSpec *fontspec_copy(const FontSpec *f)
263 {
264     return fontspec_new(f->name);
265 }
266 void fontspec_free(FontSpec *f)
267 {
268     sfree(f->name);
269     sfree(f);
270 }
271 int fontspec_serialise(FontSpec *f, void *data)
272 {
273     int len = strlen(f->name);
274     if (data)
275         strcpy(data, f->name);
276     return len + 1;                    /* include trailing NUL */
277 }
278 FontSpec *fontspec_deserialise(void *vdata, int maxsize, int *used)
279 {
280     char *data = (char *)vdata;
281     char *end = memchr(data, '\0', maxsize);
282     if (!end)
283         return NULL;
284     *used = end - data + 1;
285     return fontspec_new(data);
286 }
287
288 char *make_dir_and_check_ours(const char *dirname)
289 {
290     struct stat st;
291
292     /*
293      * Create the directory. We might have created it before, so
294      * EEXIST is an OK error; but anything else is doom.
295      */
296     if (mkdir(dirname, 0700) < 0 && errno != EEXIST)
297         return dupprintf("%s: mkdir: %s", dirname, strerror(errno));
298
299     /*
300      * Now check that that directory is _owned by us_ and not writable
301      * by anybody else. This protects us against somebody else
302      * previously having created the directory in a way that's
303      * writable to us, and thus manipulating us into creating the
304      * actual socket in a directory they can see so that they can
305      * connect to it and use our authenticated SSH sessions.
306      */
307     if (stat(dirname, &st) < 0)
308         return dupprintf("%s: stat: %s", dirname, strerror(errno));
309     if (st.st_uid != getuid())
310         return dupprintf("%s: directory owned by uid %d, not by us",
311                          dirname, st.st_uid);
312     if ((st.st_mode & 077) != 0)
313         return dupprintf("%s: directory has overgenerous permissions %03o"
314                          " (expected 700)", dirname, st.st_mode & 0777);
315
316     return NULL;
317 }