]> asedeno.scripts.mit.edu Git - PuTTY.git/blob - unix/uxmisc.c
dd04e6f5d09c83ddf91781143d920e106a6c5d28
[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 <unistd.h>
10 #include <sys/time.h>
11 #include <sys/types.h>
12 #include <pwd.h>
13
14 #include "putty.h"
15
16 long tickcount_offset = 0;
17
18 unsigned long getticks(void)
19 {
20     struct timeval tv;
21     gettimeofday(&tv, NULL);
22     /*
23      * We want to use milliseconds rather than microseconds,
24      * because we need a decent number of them to fit into a 32-bit
25      * word so it can be used for keepalives.
26      */
27     return tv.tv_sec * 1000 + tv.tv_usec / 1000 + tickcount_offset;
28 }
29
30 Filename filename_from_str(const char *str)
31 {
32     Filename ret;
33     strncpy(ret.path, str, sizeof(ret.path));
34     ret.path[sizeof(ret.path)-1] = '\0';
35     return ret;
36 }
37
38 const char *filename_to_str(const Filename *fn)
39 {
40     return fn->path;
41 }
42
43 int filename_equal(Filename f1, Filename f2)
44 {
45     return !strcmp(f1.path, f2.path);
46 }
47
48 int filename_is_null(Filename fn)
49 {
50     return !*fn.path;
51 }
52
53 #ifdef DEBUG
54 static FILE *debug_fp = NULL;
55
56 void dputs(char *buf)
57 {
58     if (!debug_fp) {
59         debug_fp = fopen("debug.log", "w");
60     }
61
62     write(1, buf, strlen(buf));
63
64     fputs(buf, debug_fp);
65     fflush(debug_fp);
66 }
67 #endif
68
69 char *get_username(void)
70 {
71     struct passwd *p;
72     uid_t uid = getuid();
73     char *user, *ret = NULL;
74
75     /*
76      * First, find who we think we are using getlogin. If this
77      * agrees with our uid, we'll go along with it. This should
78      * allow sharing of uids between several login names whilst
79      * coping correctly with people who have su'ed.
80      */
81     user = getlogin();
82     setpwent();
83     if (user)
84         p = getpwnam(user);
85     else
86         p = NULL;
87     if (p && p->pw_uid == uid) {
88         /*
89          * The result of getlogin() really does correspond to
90          * our uid. Fine.
91          */
92         ret = user;
93     } else {
94         /*
95          * If that didn't work, for whatever reason, we'll do
96          * the simpler version: look up our uid in the password
97          * file and map it straight to a name.
98          */
99         p = getpwuid(uid);
100         if (!p)
101             return NULL;
102         ret = p->pw_name;
103     }
104     endpwent();
105
106     return dupstr(ret);
107 }
108
109 /*
110  * Display the fingerprints of the PGP Master Keys to the user.
111  * (This is here rather than in uxcons because it's appropriate even for
112  * Unix GUI apps.)
113  */
114 void pgp_fingerprints(void)
115 {
116     fputs("These are the fingerprints of the PuTTY PGP Master Keys. They can\n"
117           "be used to establish a trust path from this executable to another\n"
118           "one. See the manual for more information.\n"
119           "(Note: these fingerprints have nothing to do with SSH!)\n"
120           "\n"
121           "PuTTY Master Key (RSA), 1024-bit:\n"
122           "  " PGP_RSA_MASTER_KEY_FP "\n"
123           "PuTTY Master Key (DSA), 1024-bit:\n"
124           "  " PGP_DSA_MASTER_KEY_FP "\n", stdout);
125 }
126
127 /*
128  * Set FD_CLOEXEC on a file descriptor
129  */
130 int cloexec(int fd) {
131     int fdflags;
132
133     fdflags = fcntl(fd, F_GETFD);
134     if (fdflags == -1) return -1;
135     return fcntl(fd, F_SETFD, fdflags | FD_CLOEXEC);
136 }
137
138 FILE *f_open(struct Filename filename, char const *mode, int is_private)
139 {
140     if (!is_private) {
141         return fopen(filename.path, mode);
142     } else {
143         int fd;
144         assert(mode[0] == 'w');        /* is_private is meaningless for read,
145                                           and tricky for append */
146         fd = open(filename.path, O_WRONLY | O_CREAT | O_TRUNC,
147                       0700);
148         if (fd < 0)
149             return NULL;
150         return fdopen(fd, mode);
151     }
152 }