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);
99 static FILE *debug_fp = NULL;
101 void dputs(const char *buf)
104 debug_fp = fopen("debug.log", "w");
107 if (write(1, buf, strlen(buf)) < 0) {} /* 'error check' to placate gcc */
109 fputs(buf, debug_fp);
114 char *get_username(void)
117 uid_t uid = getuid();
118 char *user, *ret = NULL;
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.
132 if (p && p->pw_uid == uid) {
134 * The result of getlogin() really does correspond to
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.
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
159 void pgp_fingerprints(void)
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"
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);
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.
182 void cloexec(int fd) {
185 fdflags = fcntl(fd, F_GETFD);
187 fprintf(stderr, "%d: fcntl(F_GETFD): %s\n", fd, strerror(errno));
190 if (fcntl(fd, F_SETFD, fdflags | FD_CLOEXEC) < 0) {
191 fprintf(stderr, "%d: fcntl(F_SETFD): %s\n", fd, strerror(errno));
195 void noncloexec(int fd) {
198 fdflags = fcntl(fd, F_GETFD);
200 fprintf(stderr, "%d: fcntl(F_GETFD): %s\n", fd, strerror(errno));
203 if (fcntl(fd, F_SETFD, fdflags & ~FD_CLOEXEC) < 0) {
204 fprintf(stderr, "%d: fcntl(F_SETFD): %s\n", fd, strerror(errno));
208 int nonblock(int fd) {
211 fdflags = fcntl(fd, F_GETFL);
213 fprintf(stderr, "%d: fcntl(F_GETFL): %s\n", fd, strerror(errno));
216 if (fcntl(fd, F_SETFL, fdflags | O_NONBLOCK) < 0) {
217 fprintf(stderr, "%d: fcntl(F_SETFL): %s\n", fd, strerror(errno));
221 return fdflags & O_NONBLOCK;
223 int no_nonblock(int fd) {
226 fdflags = fcntl(fd, F_GETFL);
228 fprintf(stderr, "%d: fcntl(F_GETFL): %s\n", fd, strerror(errno));
231 if (fcntl(fd, F_SETFL, fdflags & ~O_NONBLOCK) < 0) {
232 fprintf(stderr, "%d: fcntl(F_SETFL): %s\n", fd, strerror(errno));
236 return fdflags & O_NONBLOCK;
239 FILE *f_open(const Filename *filename, char const *mode, int is_private)
242 return fopen(filename->path, mode);
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);
250 return fdopen(fd, mode);
254 FontSpec *fontspec_new(const char *name)
256 FontSpec *f = snew(FontSpec);
257 f->name = dupstr(name);
260 FontSpec *fontspec_copy(const FontSpec *f)
262 return fontspec_new(f->name);
264 void fontspec_free(FontSpec *f)
269 int fontspec_serialise(FontSpec *f, void *data)
271 int len = strlen(f->name);
273 strcpy(data, f->name);
274 return len + 1; /* include trailing NUL */
276 FontSpec *fontspec_deserialise(void *vdata, int maxsize, int *used)
278 char *data = (char *)vdata;
279 char *end = memchr(data, '\0', maxsize);
282 *used = end - data + 1;
283 return fontspec_new(data);
286 char *make_dir_and_check_ours(const char *dirname)
291 * Create the directory. We might have created it before, so
292 * EEXIST is an OK error; but anything else is doom.
294 if (mkdir(dirname, 0700) < 0 && errno != EEXIST)
295 return dupprintf("%s: mkdir: %s", dirname, strerror(errno));
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.
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",
310 if ((st.st_mode & 077) != 0)
311 return dupprintf("%s: directory has overgenerous permissions %03o"
312 " (expected 700)", dirname, st.st_mode & 0777);