OSDN Git Service

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