utilvfs.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  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. #include "lib/global.h"
  32. #include "lib/unixcompat.h"
  33. #include "lib/widget.h" /* message() */
  34. #include "lib/strutil.h" /* INVALID_CONV */
  35. #include "vfs.h"
  36. #include "utilvfs.h"
  37. /*** global variables ****************************************************************************/
  38. /*** file scope macro definitions ****************************************************************/
  39. #ifndef TUNMLEN
  40. #define TUNMLEN 256
  41. #endif
  42. #ifndef TGNMLEN
  43. #define TGNMLEN 256
  44. #endif
  45. #define MC_HISTORY_VFS_PASSWORD "mc.vfs.password"
  46. /*
  47. * FIXME2, the "-993" is to reduce the chance of a hit on the first lookup.
  48. */
  49. #define GUID_DEFAULT_CONST -993
  50. /*** file scope type declarations ****************************************************************/
  51. /*** forward declarations (file scope functions) *************************************************/
  52. /*** file scope variables ************************************************************************/
  53. /* --------------------------------------------------------------------------------------------- */
  54. /*** file scope functions ************************************************************************/
  55. /* --------------------------------------------------------------------------------------------- */
  56. /* --------------------------------------------------------------------------------------------- */
  57. /*** public functions ****************************************************************************/
  58. /* --------------------------------------------------------------------------------------------- */
  59. /** Get current username
  60. *
  61. * @return g_malloc()ed string with the name of the currently logged in
  62. * user ("anonymous" if uid is not registered in the system)
  63. */
  64. char *
  65. vfs_get_local_username (void)
  66. {
  67. struct passwd *p_i;
  68. p_i = getpwuid (geteuid ());
  69. /* Unknown UID, strange */
  70. return (p_i != NULL && p_i->pw_name != NULL) ? g_strdup (p_i->pw_name) : g_strdup ("anonymous");
  71. }
  72. /* --------------------------------------------------------------------------------------------- */
  73. /**
  74. * Look up a user or group name from a uid/gid, maintaining a cache.
  75. * FIXME, for now it's a one-entry cache.
  76. * This file should be modified for non-unix systems to do something
  77. * reasonable.
  78. */
  79. int
  80. vfs_finduid (const char *uname)
  81. {
  82. static int saveuid = GUID_DEFAULT_CONST;
  83. static char saveuname[TUNMLEN] = "\0";
  84. size_t uname_len;
  85. uname_len = strlen (uname);
  86. if (uname[0] != saveuname[0] /* Quick test w/o proc call */
  87. || strncmp (uname, saveuname, MIN (uname_len, TUNMLEN - 1)) != 0)
  88. {
  89. struct passwd *pw;
  90. g_strlcpy (saveuname, uname, TUNMLEN);
  91. pw = getpwnam (uname);
  92. if (pw != NULL)
  93. saveuid = pw->pw_uid;
  94. else
  95. {
  96. static int my_uid = GUID_DEFAULT_CONST;
  97. if (my_uid < 0)
  98. my_uid = getuid ();
  99. saveuid = my_uid;
  100. }
  101. }
  102. return saveuid;
  103. }
  104. /* --------------------------------------------------------------------------------------------- */
  105. int
  106. vfs_findgid (const char *gname)
  107. {
  108. static int savegid = GUID_DEFAULT_CONST;
  109. static char savegname[TGNMLEN] = "\0";
  110. size_t gname_len;
  111. gname_len = strlen (gname);
  112. if (gname[0] != savegname[0] /* Quick test w/o proc call */
  113. || strncmp (gname, savegname, MIN (gname_len, TGNMLEN - 1)) != 0)
  114. {
  115. struct group *gr;
  116. g_strlcpy (savegname, gname, TGNMLEN);
  117. gr = getgrnam (gname);
  118. if (gr != NULL)
  119. savegid = gr->gr_gid;
  120. else
  121. {
  122. static int my_gid = GUID_DEFAULT_CONST;
  123. if (my_gid < 0)
  124. my_gid = getgid ();
  125. savegid = my_gid;
  126. }
  127. }
  128. return savegid;
  129. }
  130. /* --------------------------------------------------------------------------------------------- */
  131. /**
  132. * Create a temporary file with a name resembling the original.
  133. * This is needed e.g. for local copies requested by extfs.
  134. * Some extfs scripts may look at the extension.
  135. * We also protect stupid scripts against dangerous names.
  136. */
  137. int
  138. vfs_mkstemps (vfs_path_t **pname_vpath, const char *prefix, const char *param_basename)
  139. {
  140. const char *p;
  141. GString *suffix;
  142. int shift;
  143. int fd;
  144. /* Strip directories */
  145. p = strrchr (param_basename, PATH_SEP);
  146. if (p == NULL)
  147. p = param_basename;
  148. else
  149. p++;
  150. /* Protection against very long names */
  151. shift = strlen (p) - (MC_MAXPATHLEN - 16);
  152. if (shift > 0)
  153. p += shift;
  154. suffix = g_string_sized_new (32);
  155. /* Protection against unusual characters */
  156. for (; *p != '\0' && *p != '#'; p++)
  157. if (strchr (".-_@", *p) != NULL || g_ascii_isalnum (*p))
  158. g_string_append_c (suffix, *p);
  159. fd = mc_mkstemps (pname_vpath, prefix, suffix->str);
  160. g_string_free (suffix, TRUE);
  161. return fd;
  162. }
  163. /* --------------------------------------------------------------------------------------------- */
  164. /** Extract the hostname and username from the path
  165. *
  166. * Format of the path is [user@]hostname:port/remote-dir, e.g.:
  167. *
  168. * ftp://sunsite.unc.edu/pub/linux
  169. * ftp://miguel@sphinx.nuclecu.unam.mx/c/nc
  170. * ftp://tsx-11.mit.edu:8192/
  171. * ftp://joe@foo.edu:11321/private
  172. * ftp://joe:password@foo.se
  173. *
  174. * @param path is an input string to be parsed
  175. * @param default_port is an input default port
  176. * @param flags are parsing modifier flags (@see vfs_url_flags_t)
  177. *
  178. * @return g_malloc()ed url info.
  179. * If the user is empty, e.g. ftp://@roxanne/private, and URL_USE_ANONYMOUS
  180. * is not set, then the current login name is supplied.
  181. * Return value is a g_malloc()ed structure with the pathname relative to the
  182. * host.
  183. */
  184. vfs_path_element_t *
  185. vfs_url_split (const char *path, int default_port, vfs_url_flags_t flags)
  186. {
  187. vfs_path_element_t *path_element;
  188. char *pcopy;
  189. size_t pcopy_len;
  190. const char *pend;
  191. char *colon, *at, *rest;
  192. path_element = g_new0 (vfs_path_element_t, 1);
  193. path_element->port = default_port;
  194. pcopy_len = strlen (path);
  195. pcopy = g_strndup (path, pcopy_len);
  196. pend = pcopy + pcopy_len;
  197. if ((flags & URL_NOSLASH) == 0)
  198. {
  199. char *dir;
  200. /* locate path component */
  201. dir = strchr (pcopy, PATH_SEP);
  202. if (dir == NULL)
  203. path_element->path = g_strdup (PATH_SEP_STR);
  204. else
  205. {
  206. path_element->path = g_strndup (dir, pcopy_len - (size_t) (dir - pcopy));
  207. *dir = '\0';
  208. }
  209. }
  210. /* search for any possible user */
  211. at = strrchr (pcopy, '@');
  212. /* We have a username */
  213. if (at == NULL)
  214. rest = pcopy;
  215. else
  216. {
  217. char *inner_colon;
  218. *at = '\0';
  219. inner_colon = strchr (pcopy, ':');
  220. if (inner_colon != NULL)
  221. {
  222. *inner_colon = '\0';
  223. inner_colon++;
  224. path_element->password = g_strdup (inner_colon);
  225. }
  226. if (*pcopy != '\0')
  227. path_element->user = g_strdup (pcopy);
  228. if (pend == at + 1)
  229. rest = at;
  230. else
  231. rest = at + 1;
  232. }
  233. if ((flags & URL_USE_ANONYMOUS) == 0)
  234. {
  235. g_free (path_element->user);
  236. path_element->user = vfs_get_local_username ();
  237. }
  238. /* Check if the host comes with a port spec, if so, chop it */
  239. if (*rest != '[')
  240. colon = strchr (rest, ':');
  241. else
  242. {
  243. colon = strchr (++rest, ']');
  244. if (colon != NULL)
  245. {
  246. colon[0] = '\0';
  247. colon[1] = '\0';
  248. colon++;
  249. }
  250. else
  251. {
  252. vfs_path_element_free (path_element);
  253. g_free (pcopy);
  254. return NULL;
  255. }
  256. }
  257. if (colon != NULL)
  258. {
  259. *colon = '\0';
  260. /* cppcheck-suppress invalidscanf */
  261. if (sscanf (colon + 1, "%d", &path_element->port) == 1)
  262. {
  263. if (path_element->port <= 0 || path_element->port >= 65536)
  264. path_element->port = default_port;
  265. }
  266. else
  267. while (*(++colon) != '\0')
  268. {
  269. switch (*colon)
  270. {
  271. case 'C':
  272. path_element->port = 1;
  273. break;
  274. case 'r':
  275. path_element->port = 2;
  276. break;
  277. default:
  278. break;
  279. }
  280. }
  281. }
  282. path_element->host = g_strdup (rest);
  283. g_free (pcopy);
  284. #ifdef HAVE_CHARSET
  285. path_element->dir.converter = INVALID_CONV;
  286. #endif
  287. return path_element;
  288. }
  289. /* --------------------------------------------------------------------------------------------- */
  290. void __attribute__((noreturn)) vfs_die (const char *m)
  291. {
  292. message (D_ERROR, _("Internal error:"), "%s", m);
  293. exit (EXIT_FAILURE);
  294. }
  295. /* --------------------------------------------------------------------------------------------- */
  296. char *
  297. vfs_get_password (const char *msg)
  298. {
  299. return input_dialog (msg, _("Password:"), MC_HISTORY_VFS_PASSWORD, INPUT_PASSWORD,
  300. INPUT_COMPLETE_NONE);
  301. }
  302. /* --------------------------------------------------------------------------------------------- */