utilvfs.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  1. /*
  2. Utilities for VFS modules.
  3. Copyright (C) 1988-2024
  4. Free Software Foundation, Inc.
  5. Copyright (C) 1995, 1996 Miguel de Icaza
  6. This file is part of the Midnight Commander.
  7. The Midnight Commander is free software: you can redistribute it
  8. and/or modify it under the terms of the GNU General Public License as
  9. published by the Free Software Foundation, either version 3 of the License,
  10. or (at your option) any later version.
  11. The Midnight Commander is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. GNU General Public License for more details.
  15. You should have received a copy of the GNU General Public License
  16. along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. /**
  19. * \file
  20. * \brief Source: Utilities for VFS modules
  21. * \author Miguel de Icaza
  22. * \date 1995, 1996
  23. */
  24. #include <config.h>
  25. #include <ctype.h>
  26. #include <sys/types.h>
  27. #include <pwd.h>
  28. #include <grp.h>
  29. #include <stdlib.h>
  30. #include <string.h>
  31. #if !defined (HAVE_UTIMENSAT) && defined (HAVE_UTIME_H)
  32. #include <utime.h>
  33. #endif
  34. #include "lib/global.h"
  35. #include "lib/unixcompat.h"
  36. #include "lib/widget.h" /* message() */
  37. #include "lib/strutil.h" /* INVALID_CONV */
  38. #include "vfs.h"
  39. #include "utilvfs.h"
  40. /*** global variables ****************************************************************************/
  41. /*** file scope macro definitions ****************************************************************/
  42. #ifndef TUNMLEN
  43. #define TUNMLEN 256
  44. #endif
  45. #ifndef TGNMLEN
  46. #define TGNMLEN 256
  47. #endif
  48. #define MC_HISTORY_VFS_PASSWORD "mc.vfs.password"
  49. /*
  50. * FIXME2, the "-993" is to reduce the chance of a hit on the first lookup.
  51. */
  52. #define GUID_DEFAULT_CONST -993
  53. /*** file scope type declarations ****************************************************************/
  54. /*** forward declarations (file scope functions) *************************************************/
  55. /*** file scope variables ************************************************************************/
  56. /* --------------------------------------------------------------------------------------------- */
  57. /*** file scope functions ************************************************************************/
  58. /* --------------------------------------------------------------------------------------------- */
  59. /* --------------------------------------------------------------------------------------------- */
  60. /*** public functions ****************************************************************************/
  61. /* --------------------------------------------------------------------------------------------- */
  62. /** Get current username
  63. *
  64. * @return g_malloc()ed string with the name of the currently logged in
  65. * user ("anonymous" if uid is not registered in the system)
  66. */
  67. char *
  68. vfs_get_local_username (void)
  69. {
  70. struct passwd *p_i;
  71. p_i = getpwuid (geteuid ());
  72. /* Unknown UID, strange */
  73. return (p_i != NULL && p_i->pw_name != NULL) ? g_strdup (p_i->pw_name) : g_strdup ("anonymous");
  74. }
  75. /* --------------------------------------------------------------------------------------------- */
  76. /**
  77. * Look up a user or group name from a uid/gid, maintaining a cache.
  78. * FIXME, for now it's a one-entry cache.
  79. * This file should be modified for non-unix systems to do something
  80. * reasonable.
  81. */
  82. int
  83. vfs_finduid (const char *uname)
  84. {
  85. static int saveuid = GUID_DEFAULT_CONST;
  86. static char saveuname[TUNMLEN] = "\0";
  87. size_t uname_len;
  88. uname_len = strlen (uname);
  89. if (uname[0] != saveuname[0] /* Quick test w/o proc call */
  90. || strncmp (uname, saveuname, MIN (uname_len, TUNMLEN - 1)) != 0)
  91. {
  92. struct passwd *pw;
  93. g_strlcpy (saveuname, uname, TUNMLEN);
  94. pw = getpwnam (uname);
  95. if (pw != NULL)
  96. saveuid = pw->pw_uid;
  97. else
  98. {
  99. static int my_uid = GUID_DEFAULT_CONST;
  100. if (my_uid < 0)
  101. my_uid = getuid ();
  102. saveuid = my_uid;
  103. }
  104. }
  105. return saveuid;
  106. }
  107. /* --------------------------------------------------------------------------------------------- */
  108. int
  109. vfs_findgid (const char *gname)
  110. {
  111. static int savegid = GUID_DEFAULT_CONST;
  112. static char savegname[TGNMLEN] = "\0";
  113. size_t gname_len;
  114. gname_len = strlen (gname);
  115. if (gname[0] != savegname[0] /* Quick test w/o proc call */
  116. || strncmp (gname, savegname, MIN (gname_len, TGNMLEN - 1)) != 0)
  117. {
  118. struct group *gr;
  119. g_strlcpy (savegname, gname, TGNMLEN);
  120. gr = getgrnam (gname);
  121. if (gr != NULL)
  122. savegid = gr->gr_gid;
  123. else
  124. {
  125. static int my_gid = GUID_DEFAULT_CONST;
  126. if (my_gid < 0)
  127. my_gid = getgid ();
  128. savegid = my_gid;
  129. }
  130. }
  131. return savegid;
  132. }
  133. /* --------------------------------------------------------------------------------------------- */
  134. /**
  135. * Create a temporary file with a name resembling the original.
  136. * This is needed e.g. for local copies requested by extfs.
  137. * Some extfs scripts may look at the extension.
  138. * We also protect stupid scripts against dangerous names.
  139. */
  140. int
  141. vfs_mkstemps (vfs_path_t **pname_vpath, const char *prefix, const char *param_basename)
  142. {
  143. const char *p;
  144. GString *suffix;
  145. int shift;
  146. int fd;
  147. /* Strip directories */
  148. p = strrchr (param_basename, PATH_SEP);
  149. if (p == NULL)
  150. p = param_basename;
  151. else
  152. p++;
  153. /* Protection against very long names */
  154. shift = strlen (p) - (MC_MAXPATHLEN - 16);
  155. if (shift > 0)
  156. p += shift;
  157. suffix = g_string_sized_new (32);
  158. /* Protection against unusual characters */
  159. for (; *p != '\0' && *p != '#'; p++)
  160. if (strchr (".-_@", *p) != NULL || g_ascii_isalnum (*p))
  161. g_string_append_c (suffix, *p);
  162. fd = mc_mkstemps (pname_vpath, prefix, suffix->str);
  163. g_string_free (suffix, TRUE);
  164. return fd;
  165. }
  166. /* --------------------------------------------------------------------------------------------- */
  167. /** Extract the hostname and username from the path
  168. *
  169. * Format of the path is [user@]hostname:port/remote-dir, e.g.:
  170. *
  171. * ftp://sunsite.unc.edu/pub/linux
  172. * ftp://miguel@sphinx.nuclecu.unam.mx/c/nc
  173. * ftp://tsx-11.mit.edu:8192/
  174. * ftp://joe@foo.edu:11321/private
  175. * ftp://joe:password@foo.se
  176. *
  177. * @param path is an input string to be parsed
  178. * @param default_port is an input default port
  179. * @param flags are parsing modifier flags (@see vfs_url_flags_t)
  180. *
  181. * @return g_malloc()ed url info.
  182. * If the user is empty, e.g. ftp://@roxanne/private, and URL_USE_ANONYMOUS
  183. * is not set, then the current login name is supplied.
  184. * Return value is a g_malloc()ed structure with the pathname relative to the
  185. * host.
  186. */
  187. vfs_path_element_t *
  188. vfs_url_split (const char *path, int default_port, vfs_url_flags_t flags)
  189. {
  190. vfs_path_element_t *path_element;
  191. char *pcopy;
  192. size_t pcopy_len;
  193. const char *pend;
  194. char *colon, *at, *rest;
  195. path_element = g_new0 (vfs_path_element_t, 1);
  196. path_element->port = default_port;
  197. pcopy_len = strlen (path);
  198. pcopy = g_strndup (path, pcopy_len);
  199. pend = pcopy + pcopy_len;
  200. if ((flags & URL_NOSLASH) == 0)
  201. {
  202. char *dir;
  203. /* locate path component */
  204. dir = strchr (pcopy, PATH_SEP);
  205. if (dir == NULL)
  206. path_element->path = g_strdup (PATH_SEP_STR);
  207. else
  208. {
  209. path_element->path = g_strndup (dir, pcopy_len - (size_t) (dir - pcopy));
  210. *dir = '\0';
  211. }
  212. }
  213. /* search for any possible user */
  214. at = strrchr (pcopy, '@');
  215. /* We have a username */
  216. if (at == NULL)
  217. rest = pcopy;
  218. else
  219. {
  220. char *inner_colon;
  221. *at = '\0';
  222. inner_colon = strchr (pcopy, ':');
  223. if (inner_colon != NULL)
  224. {
  225. *inner_colon = '\0';
  226. inner_colon++;
  227. path_element->password = g_strdup (inner_colon);
  228. }
  229. if (*pcopy != '\0')
  230. path_element->user = g_strdup (pcopy);
  231. if (pend == at + 1)
  232. rest = at;
  233. else
  234. rest = at + 1;
  235. }
  236. if ((flags & URL_USE_ANONYMOUS) == 0)
  237. {
  238. g_free (path_element->user);
  239. path_element->user = vfs_get_local_username ();
  240. }
  241. /* Check if the host comes with a port spec, if so, chop it */
  242. if (*rest != '[')
  243. colon = strchr (rest, ':');
  244. else
  245. {
  246. colon = strchr (++rest, ']');
  247. if (colon != NULL)
  248. {
  249. colon[0] = '\0';
  250. colon[1] = '\0';
  251. colon++;
  252. }
  253. else
  254. {
  255. vfs_path_element_free (path_element);
  256. g_free (pcopy);
  257. return NULL;
  258. }
  259. }
  260. if (colon != NULL)
  261. {
  262. *colon = '\0';
  263. /* cppcheck-suppress invalidscanf */
  264. if (sscanf (colon + 1, "%d", &path_element->port) == 1)
  265. {
  266. if (path_element->port <= 0 || path_element->port >= 65536)
  267. path_element->port = default_port;
  268. }
  269. else
  270. while (*(++colon) != '\0')
  271. {
  272. switch (*colon)
  273. {
  274. case 'C':
  275. path_element->port = 1;
  276. break;
  277. case 'r':
  278. path_element->port = 2;
  279. break;
  280. default:
  281. break;
  282. }
  283. }
  284. }
  285. path_element->host = g_strdup (rest);
  286. g_free (pcopy);
  287. #ifdef HAVE_CHARSET
  288. path_element->dir.converter = INVALID_CONV;
  289. #endif
  290. return path_element;
  291. }
  292. /* --------------------------------------------------------------------------------------------- */
  293. void __attribute__((noreturn)) vfs_die (const char *m)
  294. {
  295. message (D_ERROR, _("Internal error:"), "%s", m);
  296. exit (EXIT_FAILURE);
  297. }
  298. /* --------------------------------------------------------------------------------------------- */
  299. char *
  300. vfs_get_password (const char *msg)
  301. {
  302. return input_dialog (msg, _("Password:"), MC_HISTORY_VFS_PASSWORD, INPUT_PASSWORD,
  303. INPUT_COMPLETE_NONE);
  304. }
  305. /* --------------------------------------------------------------------------------------------- */
  306. int
  307. vfs_utime (const char *path, mc_timesbuf_t *times)
  308. {
  309. #ifdef HAVE_UTIMENSAT
  310. return utimensat (AT_FDCWD, path, *times, AT_SYMLINK_NOFOLLOW);
  311. #else
  312. return utime (path, times);
  313. #endif
  314. }
  315. /* --------------------------------------------------------------------------------------------- */
  316. void
  317. vfs_get_timespecs_from_timesbuf (mc_timesbuf_t *times, mc_timespec_t *atime, mc_timespec_t *mtime)
  318. {
  319. #ifdef HAVE_UTIMENSAT
  320. atime->tv_sec = (*times)[0].tv_sec;
  321. atime->tv_nsec = (*times)[0].tv_nsec;
  322. mtime->tv_sec = (*times)[1].tv_sec;
  323. mtime->tv_nsec = (*times)[1].tv_nsec;
  324. #else
  325. atime->tv_sec = times->actime;
  326. atime->tv_nsec = 0;
  327. mtime->tv_sec = times->modtime;
  328. mtime->tv_nsec = 0;
  329. #endif
  330. }
  331. /* --------------------------------------------------------------------------------------------- */
  332. void
  333. vfs_get_timesbuf_from_stat (const struct stat *s, mc_timesbuf_t *times)
  334. {
  335. #ifdef HAVE_UTIMENSAT
  336. #ifdef HAVE_STRUCT_STAT_ST_MTIM
  337. /* POSIX IEEE Std 1003.1-2008 should be the preferred way
  338. *
  339. * AIX has internal type st_timespec_t conflicting with timespec, so assign per field, for details see:
  340. * https://github.com/libuv/libuv/pull/4404
  341. */
  342. (*times)[0].tv_sec = s->st_atim.tv_sec;
  343. (*times)[0].tv_nsec = s->st_atim.tv_nsec;
  344. (*times)[1].tv_sec = s->st_mtim.tv_sec;
  345. (*times)[1].tv_nsec = s->st_mtim.tv_nsec;
  346. #elif HAVE_STRUCT_STAT_ST_MTIMESPEC
  347. /* Modern BSD solution */
  348. (*times)[0] = s->st_atimespec;
  349. (*times)[1] = s->st_mtimespec;
  350. #elif HAVE_STRUCT_STAT_ST_MTIMENSEC
  351. /* Legacy BSD solution */
  352. (*times)[0].tv_sec = s->st_atime;
  353. (*times)[0].tv_nsec = s->st_atimensec;
  354. (*times)[1].tv_sec = s->st_mtime;
  355. (*times)[1].tv_nsec = s->st_mtimensec;
  356. #else
  357. #error "Found utimensat for nanosecond timestamps, but unsupported struct stat format!"
  358. #endif
  359. #else
  360. times->actime = s->st_atime;
  361. times->modtime = s->st_mtime;
  362. #endif
  363. }
  364. /* --------------------------------------------------------------------------------------------- */
  365. void
  366. vfs_copy_stat_times (const struct stat *src, struct stat *dst)
  367. {
  368. dst->st_atime = src->st_atime;
  369. dst->st_mtime = src->st_mtime;
  370. dst->st_ctime = src->st_ctime;
  371. #ifdef HAVE_STRUCT_STAT_ST_MTIM
  372. dst->st_atim.tv_nsec = src->st_atim.tv_nsec;
  373. dst->st_mtim.tv_nsec = src->st_mtim.tv_nsec;
  374. dst->st_ctim.tv_nsec = src->st_ctim.tv_nsec;
  375. #elif HAVE_STRUCT_STAT_ST_MTIMESPEC
  376. dst->st_atimespec.tv_nsec = src->st_atimespec.tv_nsec;
  377. dst->st_mtimespec.tv_nsec = src->st_mtimespec.tv_nsec;
  378. dst->st_ctimespec.tv_nsec = src->st_ctimespec.tv_nsec;
  379. #elif HAVE_STRUCT_STAT_ST_MTIMENSEC
  380. dst->st_atimensec = src->st_atimensec;
  381. dst->st_mtimensec = src->st_mtimensec;
  382. dst->st_ctimensec = src->st_ctimensec;
  383. #endif
  384. }
  385. /* --------------------------------------------------------------------------------------------- */
  386. void
  387. vfs_zero_stat_times (struct stat *s)
  388. {
  389. const struct stat empty = { 0 };
  390. vfs_copy_stat_times (&empty, s);
  391. }
  392. /* --------------------------------------------------------------------------------------------- */