f7f27309 |
1 | /* |
2 | * PuTTY miscellaneous Unix stuff |
3 | */ |
4 | |
db9b7dce |
5 | #include <fcntl.h> |
f7f27309 |
6 | #include <stdio.h> |
2ac3322e |
7 | #include <stdlib.h> |
c6940f12 |
8 | #include <assert.h> |
d0912d1f |
9 | #include <unistd.h> |
f7f27309 |
10 | #include <sys/time.h> |
799dfcfa |
11 | #include <sys/types.h> |
12 | #include <pwd.h> |
f7f27309 |
13 | |
9a30e26b |
14 | #include "putty.h" |
15 | |
2ac3322e |
16 | long tickcount_offset = 0; |
17 | |
f7f27309 |
18 | unsigned long getticks(void) |
19 | { |
20 | struct timeval tv; |
21 | gettimeofday(&tv, NULL); |
fe7b25bd |
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 | */ |
2ac3322e |
27 | return tv.tv_sec * 1000 + tv.tv_usec / 1000 + tickcount_offset; |
f7f27309 |
28 | } |
9a30e26b |
29 | |
962468d4 |
30 | Filename *filename_from_str(const char *str) |
9a30e26b |
31 | { |
962468d4 |
32 | Filename *ret = snew(Filename); |
33 | ret->path = dupstr(str); |
9a30e26b |
34 | return ret; |
35 | } |
36 | |
962468d4 |
37 | Filename *filename_copy(const Filename *fn) |
38 | { |
39 | return filename_from_str(fn->path); |
40 | } |
41 | |
9fab77dc |
42 | const char *filename_to_str(const Filename *fn) |
9a30e26b |
43 | { |
9fab77dc |
44 | return fn->path; |
9a30e26b |
45 | } |
46 | |
962468d4 |
47 | int filename_equal(const Filename *f1, const Filename *f2) |
48 | { |
49 | return !strcmp(f1->path, f2->path); |
50 | } |
51 | |
52 | int filename_is_null(const Filename *fn) |
53 | { |
54 | return !fn->path[0]; |
55 | } |
56 | |
57 | void filename_free(Filename *fn) |
9a30e26b |
58 | { |
962468d4 |
59 | sfree(fn->path); |
60 | sfree(fn); |
9a30e26b |
61 | } |
62 | |
962468d4 |
63 | int filename_serialise(const Filename *f, void *vdata) |
64 | { |
65 | char *data = (char *)vdata; |
66 | int len = strlen(f->path) + 1; /* include trailing NUL */ |
67 | if (data) { |
68 | strcpy(data, f->path); |
69 | } |
70 | return len; |
71 | } |
72 | Filename *filename_deserialise(void *vdata, int maxsize, int *used) |
9a30e26b |
73 | { |
962468d4 |
74 | char *data = (char *)vdata; |
75 | char *end; |
76 | end = memchr(data, '\0', maxsize); |
77 | if (!end) |
78 | return NULL; |
79 | end++; |
80 | *used = end - data; |
81 | return filename_from_str(data); |
9a30e26b |
82 | } |
d0912d1f |
83 | |
84 | #ifdef DEBUG |
85 | static FILE *debug_fp = NULL; |
86 | |
87 | void dputs(char *buf) |
88 | { |
89 | if (!debug_fp) { |
90 | debug_fp = fopen("debug.log", "w"); |
91 | } |
92 | |
93 | write(1, buf, strlen(buf)); |
94 | |
95 | fputs(buf, debug_fp); |
96 | fflush(debug_fp); |
97 | } |
98 | #endif |
799dfcfa |
99 | |
100 | char *get_username(void) |
101 | { |
102 | struct passwd *p; |
103 | uid_t uid = getuid(); |
104 | char *user, *ret = NULL; |
105 | |
106 | /* |
107 | * First, find who we think we are using getlogin. If this |
108 | * agrees with our uid, we'll go along with it. This should |
109 | * allow sharing of uids between several login names whilst |
110 | * coping correctly with people who have su'ed. |
111 | */ |
112 | user = getlogin(); |
113 | setpwent(); |
114 | if (user) |
115 | p = getpwnam(user); |
116 | else |
117 | p = NULL; |
118 | if (p && p->pw_uid == uid) { |
119 | /* |
120 | * The result of getlogin() really does correspond to |
121 | * our uid. Fine. |
122 | */ |
123 | ret = user; |
124 | } else { |
125 | /* |
126 | * If that didn't work, for whatever reason, we'll do |
127 | * the simpler version: look up our uid in the password |
128 | * file and map it straight to a name. |
129 | */ |
130 | p = getpwuid(uid); |
131 | if (!p) |
132 | return NULL; |
133 | ret = p->pw_name; |
134 | } |
135 | endpwent(); |
136 | |
137 | return dupstr(ret); |
138 | } |
2285d016 |
139 | |
140 | /* |
141 | * Display the fingerprints of the PGP Master Keys to the user. |
142 | * (This is here rather than in uxcons because it's appropriate even for |
143 | * Unix GUI apps.) |
144 | */ |
145 | void pgp_fingerprints(void) |
146 | { |
147 | fputs("These are the fingerprints of the PuTTY PGP Master Keys. They can\n" |
148 | "be used to establish a trust path from this executable to another\n" |
149 | "one. See the manual for more information.\n" |
150 | "(Note: these fingerprints have nothing to do with SSH!)\n" |
151 | "\n" |
152 | "PuTTY Master Key (RSA), 1024-bit:\n" |
153 | " " PGP_RSA_MASTER_KEY_FP "\n" |
154 | "PuTTY Master Key (DSA), 1024-bit:\n" |
155 | " " PGP_DSA_MASTER_KEY_FP "\n", stdout); |
156 | } |
db9b7dce |
157 | |
158 | /* |
159 | * Set FD_CLOEXEC on a file descriptor |
160 | */ |
161 | int cloexec(int fd) { |
162 | int fdflags; |
163 | |
164 | fdflags = fcntl(fd, F_GETFD); |
165 | if (fdflags == -1) return -1; |
166 | return fcntl(fd, F_SETFD, fdflags | FD_CLOEXEC); |
167 | } |
c6940f12 |
168 | |
962468d4 |
169 | FILE *f_open(const Filename *filename, char const *mode, int is_private) |
c6940f12 |
170 | { |
171 | if (!is_private) { |
962468d4 |
172 | return fopen(filename->path, mode); |
c6940f12 |
173 | } else { |
deb0e1cf |
174 | int fd; |
b078de41 |
175 | assert(mode[0] == 'w'); /* is_private is meaningless for read, |
176 | and tricky for append */ |
e41356ff |
177 | fd = open(filename->path, O_WRONLY | O_CREAT | O_TRUNC, 0600); |
c6940f12 |
178 | if (fd < 0) |
179 | return NULL; |
180 | return fdopen(fd, mode); |
181 | } |
182 | } |
ae62eaeb |
183 | |
184 | FontSpec *fontspec_new(const char *name) |
185 | { |
186 | FontSpec *f = snew(FontSpec); |
187 | f->name = dupstr(name); |
188 | return f; |
189 | } |
190 | FontSpec *fontspec_copy(const FontSpec *f) |
191 | { |
192 | return fontspec_new(f->name); |
193 | } |
194 | void fontspec_free(FontSpec *f) |
195 | { |
196 | sfree(f->name); |
197 | sfree(f); |
198 | } |
199 | int fontspec_serialise(FontSpec *f, void *data) |
200 | { |
201 | int len = strlen(f->name); |
202 | if (data) |
203 | strcpy(data, f->name); |
204 | return len + 1; /* include trailing NUL */ |
205 | } |
206 | FontSpec *fontspec_deserialise(void *vdata, int maxsize, int *used) |
207 | { |
208 | char *data = (char *)vdata; |
209 | char *end = memchr(data, '\0', maxsize); |
210 | if (!end) |
211 | return NULL; |
212 | *used = end - data + 1; |
213 | return fontspec_new(data); |
214 | } |