source: filezilla/trunk/fuentes/src/putty/unix/uxmisc.c @ 130

Last change on this file since 130 was 130, checked in by jrpelegrina, 3 years ago

First release to xenial

File size: 8.2 KB
Line 
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
19unsigned 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
44Filename *filename_from_str(const char *str)
45{
46    Filename *ret = snew(Filename);
47    ret->path = dupstr(str);
48    return ret;
49}
50
51Filename *filename_copy(const Filename *fn)
52{
53    return filename_from_str(fn->path);
54}
55
56const char *filename_to_str(const Filename *fn)
57{
58    return fn->path;
59}
60
61int filename_equal(const Filename *f1, const Filename *f2)
62{
63    return !strcmp(f1->path, f2->path);
64}
65
66int filename_is_null(const Filename *fn)
67{
68    return !fn->path[0];
69}
70
71void filename_free(Filename *fn)
72{
73    sfree(fn->path);
74    sfree(fn);
75}
76
77int 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}
86Filename *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
98char filename_char_sanitise(char c)
99{
100    if (c == '/')
101        return '.';
102    return c;
103}
104
105#ifdef DEBUG
106static FILE *debug_fp = NULL;
107
108void dputs(const char *buf)
109{
110    if (!debug_fp) {
111        debug_fp = fopen("debug.log", "w");
112    }
113
114    if (write(1, buf, strlen(buf)) < 0) {} /* 'error check' to placate gcc */
115
116    fputs(buf, debug_fp);
117    fflush(debug_fp);
118}
119#endif
120
121char *get_username(void)
122{
123    struct passwd *p;
124    uid_t uid = getuid();
125    char *user, *ret = NULL;
126
127    /*
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.
132     */
133    user = getlogin();
134    setpwent();
135    if (user)
136        p = getpwnam(user);
137    else
138        p = NULL;
139    if (p && p->pw_uid == uid) {
140        /*
141         * The result of getlogin() really does correspond to
142         * our uid. Fine.
143         */
144        ret = user;
145    } else {
146        /*
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.
150         */
151        p = getpwuid(uid);
152        if (!p)
153            return NULL;
154        ret = p->pw_name;
155    }
156    endpwent();
157
158    return dupstr(ret);
159}
160
161/*
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
164 * Unix GUI apps.)
165 */
166void pgp_fingerprints(void)
167{
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"
172          "\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);
179}
180
181/*
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.
190 */
191void cloexec(int fd) {
192    int fdflags;
193
194    fdflags = fcntl(fd, F_GETFD);
195    if (fdflags < 0) {
196        fprintf(stderr, "%d: fcntl(F_GETFD): %s\n", fd, strerror(errno));
197        exit(1);
198    }
199    if (fcntl(fd, F_SETFD, fdflags | FD_CLOEXEC) < 0) {
200        fprintf(stderr, "%d: fcntl(F_SETFD): %s\n", fd, strerror(errno));
201        exit(1);
202    }
203}
204void noncloexec(int fd) {
205    int fdflags;
206
207    fdflags = fcntl(fd, F_GETFD);
208    if (fdflags < 0) {
209        fprintf(stderr, "%d: fcntl(F_GETFD): %s\n", fd, strerror(errno));
210        exit(1);
211    }
212    if (fcntl(fd, F_SETFD, fdflags & ~FD_CLOEXEC) < 0) {
213        fprintf(stderr, "%d: fcntl(F_SETFD): %s\n", fd, strerror(errno));
214        exit(1);
215    }
216}
217int nonblock(int fd) {
218    int fdflags;
219
220    fdflags = fcntl(fd, F_GETFL);
221    if (fdflags < 0) {
222        fprintf(stderr, "%d: fcntl(F_GETFL): %s\n", fd, strerror(errno));
223        exit(1);
224    }
225    if (fcntl(fd, F_SETFL, fdflags | O_NONBLOCK) < 0) {
226        fprintf(stderr, "%d: fcntl(F_SETFL): %s\n", fd, strerror(errno));
227        exit(1);
228    }
229
230    return fdflags & O_NONBLOCK;
231}
232int no_nonblock(int fd) {
233    int fdflags;
234
235    fdflags = fcntl(fd, F_GETFL);
236    if (fdflags < 0) {
237        fprintf(stderr, "%d: fcntl(F_GETFL): %s\n", fd, strerror(errno));
238        exit(1);
239    }
240    if (fcntl(fd, F_SETFL, fdflags & ~O_NONBLOCK) < 0) {
241        fprintf(stderr, "%d: fcntl(F_SETFL): %s\n", fd, strerror(errno));
242        exit(1);
243    }
244
245    return fdflags & O_NONBLOCK;
246}
247
248FILE *f_open(const Filename *filename, char const *mode, int is_private)
249{
250    if (!is_private) {
251        return fopen(filename->path, mode);
252    } else {
253        int fd;
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);
257        if (fd < 0)
258            return NULL;
259        return fdopen(fd, mode);
260    }
261}
262
263FontSpec *fontspec_new(const char *name)
264{
265    FontSpec *f = snew(FontSpec);
266    f->name = dupstr(name);
267    return f;
268}
269FontSpec *fontspec_copy(const FontSpec *f)
270{
271    return fontspec_new(f->name);
272}
273void fontspec_free(FontSpec *f)
274{
275    sfree(f->name);
276    sfree(f);
277}
278int fontspec_serialise(FontSpec *f, void *data)
279{
280    int len = strlen(f->name);
281    if (data)
282        strcpy(data, f->name);
283    return len + 1;                    /* include trailing NUL */
284}
285FontSpec *fontspec_deserialise(void *vdata, int maxsize, int *used)
286{
287    char *data = (char *)vdata;
288    char *end = memchr(data, '\0', maxsize);
289    if (!end)
290        return NULL;
291    *used = end - data + 1;
292    return fontspec_new(data);
293}
294
295char *make_dir_and_check_ours(const char *dirname)
296{
297    struct stat st;
298
299    /*
300     * Create the directory. We might have created it before, so
301     * EEXIST is an OK error; but anything else is doom.
302     */
303    if (mkdir(dirname, 0700) < 0 && errno != EEXIST)
304        return dupprintf("%s: mkdir: %s", dirname, strerror(errno));
305
306    /*
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.
313     */
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",
318                         dirname, st.st_uid);
319    if ((st.st_mode & 077) != 0)
320        return dupprintf("%s: directory has overgenerous permissions %03o"
321                         " (expected 700)", dirname, st.st_mode & 0777);
322
323    return NULL;
324}
Note: See TracBrowser for help on using the repository browser.