This source file includes following definitions.
- vfs_get_local_username
- vfs_finduid
- vfs_findgid
- vfs_mkstemps
- vfs_url_split
- vfs_die
- vfs_get_password
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32 #include <config.h>
33
34 #include <ctype.h>
35 #include <sys/types.h>
36 #include <pwd.h>
37 #include <grp.h>
38 #include <stdlib.h>
39 #include <string.h>
40
41 #include "lib/global.h"
42 #include "lib/unixcompat.h"
43 #include "lib/widget.h"
44 #include "lib/strutil.h"
45
46 #include "vfs.h"
47 #include "utilvfs.h"
48
49
50
51
52
53 #ifndef TUNMLEN
54 #define TUNMLEN 256
55 #endif
56 #ifndef TGNMLEN
57 #define TGNMLEN 256
58 #endif
59
60 #define MC_HISTORY_VFS_PASSWORD "mc.vfs.password"
61
62
63
64
65 #define GUID_DEFAULT_CONST -993
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86 char *
87 vfs_get_local_username (void)
88 {
89 struct passwd *p_i;
90
91 p_i = getpwuid (geteuid ());
92
93
94 return (p_i != NULL && p_i->pw_name != NULL) ? g_strdup (p_i->pw_name) : g_strdup ("anonymous");
95 }
96
97
98
99
100
101
102
103
104
105 int
106 vfs_finduid (const char *uname)
107 {
108 static int saveuid = GUID_DEFAULT_CONST;
109 static char saveuname[TUNMLEN] = "\0";
110
111 size_t uname_len;
112
113 uname_len = strlen (uname);
114
115 if (uname[0] != saveuname[0]
116 || strncmp (uname, saveuname, MIN (uname_len, TUNMLEN - 1)) != 0)
117 {
118 struct passwd *pw;
119
120 g_strlcpy (saveuname, uname, TUNMLEN);
121 pw = getpwnam (uname);
122 if (pw != NULL)
123 saveuid = pw->pw_uid;
124 else
125 {
126 static int my_uid = GUID_DEFAULT_CONST;
127
128 if (my_uid < 0)
129 my_uid = getuid ();
130
131 saveuid = my_uid;
132 }
133 }
134
135 return saveuid;
136 }
137
138
139
140 int
141 vfs_findgid (const char *gname)
142 {
143 static int savegid = GUID_DEFAULT_CONST;
144 static char savegname[TGNMLEN] = "\0";
145
146 size_t gname_len;
147
148 gname_len = strlen (gname);
149
150 if (gname[0] != savegname[0]
151 || strncmp (gname, savegname, MIN (gname_len, TGNMLEN - 1)) != 0)
152 {
153 struct group *gr;
154
155 g_strlcpy (savegname, gname, TGNMLEN);
156 gr = getgrnam (gname);
157 if (gr != NULL)
158 savegid = gr->gr_gid;
159 else
160 {
161 static int my_gid = GUID_DEFAULT_CONST;
162
163 if (my_gid < 0)
164 my_gid = getgid ();
165
166 savegid = my_gid;
167 }
168 }
169
170 return savegid;
171 }
172
173
174
175
176
177
178
179
180
181 int
182 vfs_mkstemps (vfs_path_t ** pname_vpath, const char *prefix, const char *param_basename)
183 {
184 const char *p;
185 GString *suffix;
186 int shift;
187 int fd;
188
189
190 p = strrchr (param_basename, PATH_SEP);
191 if (p == NULL)
192 p = param_basename;
193 else
194 p++;
195
196
197 shift = strlen (p) - (MC_MAXPATHLEN - 16);
198 if (shift > 0)
199 p += shift;
200
201 suffix = g_string_sized_new (32);
202
203
204 for (; *p != '\0' && *p != '#'; p++)
205 if (strchr (".-_@", *p) != NULL || g_ascii_isalnum (*p))
206 g_string_append_c (suffix, *p);
207
208 fd = mc_mkstemps (pname_vpath, prefix, suffix->str);
209 g_string_free (suffix, TRUE);
210
211 return fd;
212 }
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236 vfs_path_element_t *
237 vfs_url_split (const char *path, int default_port, vfs_url_flags_t flags)
238 {
239 vfs_path_element_t *path_element;
240
241 char *pcopy;
242 size_t pcopy_len;
243 const char *pend;
244 char *colon, *at, *rest;
245
246 path_element = g_new0 (vfs_path_element_t, 1);
247 path_element->port = default_port;
248
249 pcopy_len = strlen (path);
250 pcopy = g_strndup (path, pcopy_len);
251 pend = pcopy + pcopy_len;
252
253 if ((flags & URL_NOSLASH) == 0)
254 {
255 char *dir;
256
257
258 dir = strchr (pcopy, PATH_SEP);
259
260 if (dir == NULL)
261 path_element->path = g_strdup (PATH_SEP_STR);
262 else
263 {
264 path_element->path = g_strndup (dir, pcopy_len - (size_t) (dir - pcopy));
265 *dir = '\0';
266 }
267 }
268
269
270 at = strrchr (pcopy, '@');
271
272
273 if (at == NULL)
274 rest = pcopy;
275 else
276 {
277 char *inner_colon;
278
279 *at = '\0';
280 inner_colon = strchr (pcopy, ':');
281 if (inner_colon != NULL)
282 {
283 *inner_colon = '\0';
284 inner_colon++;
285 path_element->password = g_strdup (inner_colon);
286 }
287
288 if (*pcopy != '\0')
289 path_element->user = g_strdup (pcopy);
290
291 if (pend == at + 1)
292 rest = at;
293 else
294 rest = at + 1;
295 }
296
297 if ((flags & URL_USE_ANONYMOUS) == 0)
298 {
299 g_free (path_element->user);
300 path_element->user = vfs_get_local_username ();
301 }
302
303 if (*rest != '[')
304 colon = strchr (rest, ':');
305 else
306 {
307 colon = strchr (++rest, ']');
308 if (colon != NULL)
309 {
310 colon[0] = '\0';
311 colon[1] = '\0';
312 colon++;
313 }
314 else
315 {
316 vfs_path_element_free (path_element);
317 g_free (pcopy);
318 return NULL;
319 }
320 }
321
322 if (colon != NULL)
323 {
324 *colon = '\0';
325
326 if (sscanf (colon + 1, "%d", &path_element->port) == 1)
327 {
328 if (path_element->port <= 0 || path_element->port >= 65536)
329 path_element->port = default_port;
330 }
331 else
332 while (*(++colon) != '\0')
333 {
334 switch (*colon)
335 {
336 case 'C':
337 path_element->port = 1;
338 break;
339 case 'r':
340 path_element->port = 2;
341 break;
342 default:
343 break;
344 }
345 }
346 }
347
348 path_element->host = g_strdup (rest);
349 g_free (pcopy);
350 #ifdef HAVE_CHARSET
351 path_element->dir.converter = INVALID_CONV;
352 #endif
353
354 return path_element;
355 }
356
357
358
359 void __attribute__ ((noreturn)) vfs_die (const char *m)
360 {
361 message (D_ERROR, _("Internal error:"), "%s", m);
362 exit (EXIT_FAILURE);
363 }
364
365
366
367 char *
368 vfs_get_password (const char *msg)
369 {
370 return input_dialog (msg, _("Password:"), MC_HISTORY_VFS_PASSWORD, INPUT_PASSWORD,
371 INPUT_COMPLETE_NONE);
372 }
373
374