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 | |
9fab77dc |
30 | Filename filename_from_str(const char *str) |
9a30e26b |
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 | |
9fab77dc |
38 | const char *filename_to_str(const Filename *fn) |
9a30e26b |
39 | { |
9fab77dc |
40 | return fn->path; |
9a30e26b |
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 | } |
d0912d1f |
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 |
799dfcfa |
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 | } |
2285d016 |
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 | } |
db9b7dce |
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 | } |
c6940f12 |
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 { |
deb0e1cf |
143 | int fd; |
b078de41 |
144 | assert(mode[0] == 'w'); /* is_private is meaningless for read, |
145 | and tricky for append */ |
deb0e1cf |
146 | fd = open(filename.path, O_WRONLY | O_CREAT | O_TRUNC, |
c6940f12 |
147 | 0700); |
148 | if (fd < 0) |
149 | return NULL; |
150 | return fdopen(fd, mode); |
151 | } |
152 | } |