| 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 | #ifdef DEBUG |
| 98 | static FILE *debug_fp = NULL; |
| 99 | |
| 100 | void dputs(char *buf) |
| 101 | { |
| 102 | if (!debug_fp) { |
| 103 | debug_fp = fopen("debug.log", "w"); |
| 104 | } |
| 105 | |
| 106 | write(1, buf, strlen(buf)); |
| 107 | |
| 108 | fputs(buf, debug_fp); |
| 109 | fflush(debug_fp); |
| 110 | } |
| 111 | #endif |
| 112 | |
| 113 | char *get_username(void) |
| 114 | { |
| 115 | struct passwd *p; |
| 116 | uid_t uid = getuid(); |
| 117 | char *user, *ret = NULL; |
| 118 | |
| 119 | /* |
| 120 | * First, find who we think we are using getlogin. If this |
| 121 | * agrees with our uid, we'll go along with it. This should |
| 122 | * allow sharing of uids between several login names whilst |
| 123 | * coping correctly with people who have su'ed. |
| 124 | */ |
| 125 | user = getlogin(); |
| 126 | setpwent(); |
| 127 | if (user) |
| 128 | p = getpwnam(user); |
| 129 | else |
| 130 | p = NULL; |
| 131 | if (p && p->pw_uid == uid) { |
| 132 | /* |
| 133 | * The result of getlogin() really does correspond to |
| 134 | * our uid. Fine. |
| 135 | */ |
| 136 | ret = user; |
| 137 | } else { |
| 138 | /* |
| 139 | * If that didn't work, for whatever reason, we'll do |
| 140 | * the simpler version: look up our uid in the password |
| 141 | * file and map it straight to a name. |
| 142 | */ |
| 143 | p = getpwuid(uid); |
| 144 | if (!p) |
| 145 | return NULL; |
| 146 | ret = p->pw_name; |
| 147 | } |
| 148 | endpwent(); |
| 149 | |
| 150 | return dupstr(ret); |
| 151 | } |
| 152 | |
| 153 | /* |
| 154 | * Display the fingerprints of the PGP Master Keys to the user. |
| 155 | * (This is here rather than in uxcons because it's appropriate even for |
| 156 | * Unix GUI apps.) |
| 157 | */ |
| 158 | void pgp_fingerprints(void) |
| 159 | { |
| 160 | fputs("These are the fingerprints of the PuTTY PGP Master Keys. They can\n" |
| 161 | "be used to establish a trust path from this executable to another\n" |
| 162 | "one. See the manual for more information.\n" |
| 163 | "(Note: these fingerprints have nothing to do with SSH!)\n" |
| 164 | "\n" |
| 165 | "PuTTY Master Key (RSA), 1024-bit:\n" |
| 166 | " " PGP_RSA_MASTER_KEY_FP "\n" |
| 167 | "PuTTY Master Key (DSA), 1024-bit:\n" |
| 168 | " " PGP_DSA_MASTER_KEY_FP "\n", stdout); |
| 169 | } |
| 170 | |
| 171 | /* |
| 172 | * Set and clear fcntl options on a file descriptor. We don't |
| 173 | * realistically expect any of these operations to fail (the most |
| 174 | * plausible error condition is EBADF, but we always believe ourselves |
| 175 | * to be passing a valid fd so even that's an assertion-fail sort of |
| 176 | * response), so we don't make any effort to return sensible error |
| 177 | * codes to the caller - we just log to standard error and die |
| 178 | * unceremoniously. However, nonblock and no_nonblock do return the |
| 179 | * previous state of O_NONBLOCK. |
| 180 | */ |
| 181 | void cloexec(int fd) { |
| 182 | int fdflags; |
| 183 | |
| 184 | fdflags = fcntl(fd, F_GETFD); |
| 185 | if (fdflags < 0) { |
| 186 | fprintf(stderr, "%d: fcntl(F_GETFD): %s\n", fd, strerror(errno)); |
| 187 | exit(1); |
| 188 | } |
| 189 | if (fcntl(fd, F_SETFD, fdflags | FD_CLOEXEC) < 0) { |
| 190 | fprintf(stderr, "%d: fcntl(F_SETFD): %s\n", fd, strerror(errno)); |
| 191 | exit(1); |
| 192 | } |
| 193 | } |
| 194 | void noncloexec(int fd) { |
| 195 | int fdflags; |
| 196 | |
| 197 | fdflags = fcntl(fd, F_GETFD); |
| 198 | if (fdflags < 0) { |
| 199 | fprintf(stderr, "%d: fcntl(F_GETFD): %s\n", fd, strerror(errno)); |
| 200 | exit(1); |
| 201 | } |
| 202 | if (fcntl(fd, F_SETFD, fdflags & ~FD_CLOEXEC) < 0) { |
| 203 | fprintf(stderr, "%d: fcntl(F_SETFD): %s\n", fd, strerror(errno)); |
| 204 | exit(1); |
| 205 | } |
| 206 | } |
| 207 | int nonblock(int fd) { |
| 208 | int fdflags; |
| 209 | |
| 210 | fdflags = fcntl(fd, F_GETFL); |
| 211 | if (fdflags < 0) { |
| 212 | fprintf(stderr, "%d: fcntl(F_GETFL): %s\n", fd, strerror(errno)); |
| 213 | exit(1); |
| 214 | } |
| 215 | if (fcntl(fd, F_SETFL, fdflags | O_NONBLOCK) < 0) { |
| 216 | fprintf(stderr, "%d: fcntl(F_SETFL): %s\n", fd, strerror(errno)); |
| 217 | exit(1); |
| 218 | } |
| 219 | |
| 220 | return fdflags & O_NONBLOCK; |
| 221 | } |
| 222 | int no_nonblock(int fd) { |
| 223 | int fdflags; |
| 224 | |
| 225 | fdflags = fcntl(fd, F_GETFL); |
| 226 | if (fdflags < 0) { |
| 227 | fprintf(stderr, "%d: fcntl(F_GETFL): %s\n", fd, strerror(errno)); |
| 228 | exit(1); |
| 229 | } |
| 230 | if (fcntl(fd, F_SETFL, fdflags & ~O_NONBLOCK) < 0) { |
| 231 | fprintf(stderr, "%d: fcntl(F_SETFL): %s\n", fd, strerror(errno)); |
| 232 | exit(1); |
| 233 | } |
| 234 | |
| 235 | return fdflags & O_NONBLOCK; |
| 236 | } |
| 237 | |
| 238 | FILE *f_open(const Filename *filename, char const *mode, int is_private) |
| 239 | { |
| 240 | if (!is_private) { |
| 241 | return fopen(filename->path, mode); |
| 242 | } else { |
| 243 | int fd; |
| 244 | assert(mode[0] == 'w'); /* is_private is meaningless for read, |
| 245 | and tricky for append */ |
| 246 | fd = open(filename->path, O_WRONLY | O_CREAT | O_TRUNC, 0600); |
| 247 | if (fd < 0) |
| 248 | return NULL; |
| 249 | return fdopen(fd, mode); |
| 250 | } |
| 251 | } |
| 252 | |
| 253 | FontSpec *fontspec_new(const char *name) |
| 254 | { |
| 255 | FontSpec *f = snew(FontSpec); |
| 256 | f->name = dupstr(name); |
| 257 | return f; |
| 258 | } |
| 259 | FontSpec *fontspec_copy(const FontSpec *f) |
| 260 | { |
| 261 | return fontspec_new(f->name); |
| 262 | } |
| 263 | void fontspec_free(FontSpec *f) |
| 264 | { |
| 265 | sfree(f->name); |
| 266 | sfree(f); |
| 267 | } |
| 268 | int fontspec_serialise(FontSpec *f, void *data) |
| 269 | { |
| 270 | int len = strlen(f->name); |
| 271 | if (data) |
| 272 | strcpy(data, f->name); |
| 273 | return len + 1; /* include trailing NUL */ |
| 274 | } |
| 275 | FontSpec *fontspec_deserialise(void *vdata, int maxsize, int *used) |
| 276 | { |
| 277 | char *data = (char *)vdata; |
| 278 | char *end = memchr(data, '\0', maxsize); |
| 279 | if (!end) |
| 280 | return NULL; |
| 281 | *used = end - data + 1; |
| 282 | return fontspec_new(data); |
| 283 | } |