2 * PuTTY miscellaneous Unix stuff
13 #include <sys/types.h>
19 unsigned long getticks(void)
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.
27 #if defined HAVE_CLOCK_GETTIME && defined HAVE_DECL_CLOCK_MONOTONIC
29 /* Use CLOCK_MONOTONIC if available, so as to be unconfused if
30 * the system clock changes. */
32 if (clock_gettime(CLOCK_MONOTONIC, &ts) == 0)
33 return ts.tv_sec * TICKSPERSEC +
34 ts.tv_nsec / (1000000000 / TICKSPERSEC);
39 gettimeofday(&tv, NULL);
40 return tv.tv_sec * TICKSPERSEC + tv.tv_usec / (1000000 / TICKSPERSEC);
44 Filename *filename_from_str(const char *str)
46 Filename *ret = snew(Filename);
47 ret->path = dupstr(str);
51 Filename *filename_copy(const Filename *fn)
53 return filename_from_str(fn->path);
56 const char *filename_to_str(const Filename *fn)
61 int filename_equal(const Filename *f1, const Filename *f2)
63 return !strcmp(f1->path, f2->path);
66 int filename_is_null(const Filename *fn)
71 void filename_free(Filename *fn)
77 int filename_serialise(const Filename *f, void *vdata)
79 char *data = (char *)vdata;
80 int len = strlen(f->path) + 1; /* include trailing NUL */
82 strcpy(data, f->path);
86 Filename *filename_deserialise(void *vdata, int maxsize, int *used)
88 char *data = (char *)vdata;
90 end = memchr(data, '\0', maxsize);
95 return filename_from_str(data);
98 char filename_char_sanitise(char c)
106 static FILE *debug_fp = NULL;
108 void dputs(const char *buf)
111 debug_fp = fopen("debug.log", "w");
114 if (write(1, buf, strlen(buf)) < 0) {} /* 'error check' to placate gcc */
116 fputs(buf, debug_fp);
121 char *get_username(void)
124 uid_t uid = getuid();
125 char *user, *ret = NULL;
128 * First, find who we think we are using getlogin. If this
129 * agrees with our uid, we'll go along with it. This should
130 * allow sharing of uids between several login names whilst
131 * coping correctly with people who have su'ed.
139 if (p && p->pw_uid == uid) {
141 * The result of getlogin() really does correspond to
147 * If that didn't work, for whatever reason, we'll do
148 * the simpler version: look up our uid in the password
149 * file and map it straight to a name.
162 * Display the fingerprints of the PGP Master Keys to the user.
163 * (This is here rather than in uxcons because it's appropriate even for
166 void pgp_fingerprints(void)
168 fputs("These are the fingerprints of the PuTTY PGP Master Keys. They can\n"
169 "be used to establish a trust path from this executable to another\n"
170 "one. See the manual for more information.\n"
171 "(Note: these fingerprints have nothing to do with SSH!)\n"
173 "PuTTY Master Key as of 2015 (RSA, 4096-bit):\n"
174 " " PGP_MASTER_KEY_FP "\n\n"
175 "Original PuTTY Master Key (RSA, 1024-bit):\n"
176 " " PGP_RSA_MASTER_KEY_FP "\n"
177 "Original PuTTY Master Key (DSA, 1024-bit):\n"
178 " " PGP_DSA_MASTER_KEY_FP "\n", stdout);
182 * Set and clear fcntl options on a file descriptor. We don't
183 * realistically expect any of these operations to fail (the most
184 * plausible error condition is EBADF, but we always believe ourselves
185 * to be passing a valid fd so even that's an assertion-fail sort of
186 * response), so we don't make any effort to return sensible error
187 * codes to the caller - we just log to standard error and die
188 * unceremoniously. However, nonblock and no_nonblock do return the
189 * previous state of O_NONBLOCK.
191 void cloexec(int fd) {
194 fdflags = fcntl(fd, F_GETFD);
196 fprintf(stderr, "%d: fcntl(F_GETFD): %s\n", fd, strerror(errno));
199 if (fcntl(fd, F_SETFD, fdflags | FD_CLOEXEC) < 0) {
200 fprintf(stderr, "%d: fcntl(F_SETFD): %s\n", fd, strerror(errno));
204 void noncloexec(int fd) {
207 fdflags = fcntl(fd, F_GETFD);
209 fprintf(stderr, "%d: fcntl(F_GETFD): %s\n", fd, strerror(errno));
212 if (fcntl(fd, F_SETFD, fdflags & ~FD_CLOEXEC) < 0) {
213 fprintf(stderr, "%d: fcntl(F_SETFD): %s\n", fd, strerror(errno));
217 int nonblock(int fd) {
220 fdflags = fcntl(fd, F_GETFL);
222 fprintf(stderr, "%d: fcntl(F_GETFL): %s\n", fd, strerror(errno));
225 if (fcntl(fd, F_SETFL, fdflags | O_NONBLOCK) < 0) {
226 fprintf(stderr, "%d: fcntl(F_SETFL): %s\n", fd, strerror(errno));
230 return fdflags & O_NONBLOCK;
232 int no_nonblock(int fd) {
235 fdflags = fcntl(fd, F_GETFL);
237 fprintf(stderr, "%d: fcntl(F_GETFL): %s\n", fd, strerror(errno));
240 if (fcntl(fd, F_SETFL, fdflags & ~O_NONBLOCK) < 0) {
241 fprintf(stderr, "%d: fcntl(F_SETFL): %s\n", fd, strerror(errno));
245 return fdflags & O_NONBLOCK;
248 FILE *f_open(const Filename *filename, char const *mode, int is_private)
251 return fopen(filename->path, mode);
254 assert(mode[0] == 'w'); /* is_private is meaningless for read,
255 and tricky for append */
256 fd = open(filename->path, O_WRONLY | O_CREAT | O_TRUNC, 0600);
259 return fdopen(fd, mode);
263 FontSpec *fontspec_new(const char *name)
265 FontSpec *f = snew(FontSpec);
266 f->name = dupstr(name);
269 FontSpec *fontspec_copy(const FontSpec *f)
271 return fontspec_new(f->name);
273 void fontspec_free(FontSpec *f)
278 int fontspec_serialise(FontSpec *f, void *data)
280 int len = strlen(f->name);
282 strcpy(data, f->name);
283 return len + 1; /* include trailing NUL */
285 FontSpec *fontspec_deserialise(void *vdata, int maxsize, int *used)
287 char *data = (char *)vdata;
288 char *end = memchr(data, '\0', maxsize);
291 *used = end - data + 1;
292 return fontspec_new(data);
295 char *make_dir_and_check_ours(const char *dirname)
300 * Create the directory. We might have created it before, so
301 * EEXIST is an OK error; but anything else is doom.
303 if (mkdir(dirname, 0700) < 0 && errno != EEXIST)
304 return dupprintf("%s: mkdir: %s", dirname, strerror(errno));
307 * Now check that that directory is _owned by us_ and not writable
308 * by anybody else. This protects us against somebody else
309 * previously having created the directory in a way that's
310 * writable to us, and thus manipulating us into creating the
311 * actual socket in a directory they can see so that they can
312 * connect to it and use our authenticated SSH sessions.
314 if (stat(dirname, &st) < 0)
315 return dupprintf("%s: stat: %s", dirname, strerror(errno));
316 if (st.st_uid != getuid())
317 return dupprintf("%s: directory owned by uid %d, not by us",
319 if ((st.st_mode & 077) != 0)
320 return dupprintf("%s: directory has overgenerous permissions %03o"
321 " (expected 700)", dirname, st.st_mode & 0777);
326 char *make_dir_path(const char *path, mode_t mode)
332 pos += strcspn(path + pos, "/");
335 prefix = dupprintf("%.*s", pos, path);
337 if (mkdir(prefix, mode) < 0 && errno != EEXIST) {
338 char *ret = dupprintf("%s: mkdir: %s",
339 prefix, strerror(errno));
349 pos += strspn(path + pos, "/");