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