utilvfs.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. /* Utilities for VFS modules.
  2. Copyright (C) 1988, 1992, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
  3. 2005, 2006, 2007 Free Software Foundation, Inc.
  4. Copyright (C) 1995, 1996 Miguel de Icaza
  5. This program is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU Library General Public License
  7. as published by the Free Software Foundation; either version 2 of
  8. the License, or (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU Library General Public License for more details.
  13. You should have received a copy of the GNU Library General Public
  14. License along with this program; if not, write to the Free Software
  15. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
  16. /**
  17. * \file
  18. * \brief Source: Utilities for VFS modules
  19. * \author Miguel de Icaza
  20. * \date 1995, 1996
  21. */
  22. #include <config.h>
  23. #include <ctype.h>
  24. #include <sys/types.h>
  25. #include <pwd.h>
  26. #include <grp.h>
  27. #include <stdlib.h>
  28. #include <string.h>
  29. #include <unistd.h>
  30. #include "lib/global.h"
  31. #include "lib/unixcompat.h"
  32. #include "lib/util.h" /* mc_mkstemps() */
  33. #include "lib/widget.h" /* message() */
  34. #include "vfs.h"
  35. #include "utilvfs.h"
  36. /*** global variables ****************************************************************************/
  37. /*** file scope macro definitions ****************************************************************/
  38. #ifndef TUNMLEN
  39. #define TUNMLEN 256
  40. #endif
  41. #ifndef TGNMLEN
  42. #define TGNMLEN 256
  43. #endif
  44. #define myuid ( my_uid < 0? (my_uid = getuid()): my_uid )
  45. #define mygid ( my_gid < 0? (my_gid = getgid()): my_gid )
  46. #define MC_HISTORY_VFS_PASSWORD "mc.vfs.password"
  47. /*** file scope type declarations ****************************************************************/
  48. /*** file scope variables ************************************************************************/
  49. /*** file scope functions ************************************************************************/
  50. /* --------------------------------------------------------------------------------------------- */
  51. /* --------------------------------------------------------------------------------------------- */
  52. /*** public functions ****************************************************************************/
  53. /* --------------------------------------------------------------------------------------------- */
  54. /** Get current username
  55. *
  56. * @return g_malloc()ed string with the name of the currently logged in
  57. * user ("anonymous" if uid is not registered in the system)
  58. */
  59. char *
  60. vfs_get_local_username (void)
  61. {
  62. struct passwd *p_i;
  63. p_i = getpwuid (geteuid ());
  64. return (p_i && p_i->pw_name) ? g_strdup (p_i->pw_name) : g_strdup ("anonymous"); /* Unknown UID, strange */
  65. }
  66. /* --------------------------------------------------------------------------------------------- */
  67. /**
  68. * Look up a user or group name from a uid/gid, maintaining a cache.
  69. * FIXME, for now it's a one-entry cache.
  70. * FIXME2, the "-993" is to reduce the chance of a hit on the first lookup.
  71. * This file should be modified for non-unix systems to do something
  72. * reasonable.
  73. */
  74. /* --------------------------------------------------------------------------------------------- */
  75. int
  76. vfs_finduid (const char *uname)
  77. {
  78. static int saveuid = -993;
  79. static char saveuname[TUNMLEN];
  80. static int my_uid = -993;
  81. struct passwd *pw;
  82. if (uname[0] != saveuname[0] /* Quick test w/o proc call */
  83. || 0 != strncmp (uname, saveuname, TUNMLEN))
  84. {
  85. g_strlcpy (saveuname, uname, TUNMLEN);
  86. pw = getpwnam (uname);
  87. if (pw)
  88. {
  89. saveuid = pw->pw_uid;
  90. }
  91. else
  92. {
  93. saveuid = myuid;
  94. }
  95. }
  96. return saveuid;
  97. }
  98. /* --------------------------------------------------------------------------------------------- */
  99. int
  100. vfs_findgid (const char *gname)
  101. {
  102. static int savegid = -993;
  103. static char savegname[TGNMLEN];
  104. static int my_gid = -993;
  105. struct group *gr;
  106. if (gname[0] != savegname[0] /* Quick test w/o proc call */
  107. || 0 != strncmp (gname, savegname, TUNMLEN))
  108. {
  109. g_strlcpy (savegname, gname, TUNMLEN);
  110. gr = getgrnam (gname);
  111. if (gr)
  112. {
  113. savegid = gr->gr_gid;
  114. }
  115. else
  116. {
  117. savegid = mygid;
  118. }
  119. }
  120. return savegid;
  121. }
  122. /* --------------------------------------------------------------------------------------------- */
  123. /**
  124. * Create a temporary file with a name resembling the original.
  125. * This is needed e.g. for local copies requested by extfs.
  126. * Some extfs scripts may look at the extension.
  127. * We also protect stupid scripts agains dangerous names.
  128. */
  129. int
  130. vfs_mkstemps (char **pname, const char *prefix, const char *param_basename)
  131. {
  132. const char *p;
  133. char *suffix, *q;
  134. int shift;
  135. int fd;
  136. /* Strip directories */
  137. p = strrchr (param_basename, PATH_SEP);
  138. if (!p)
  139. p = param_basename;
  140. else
  141. p++;
  142. /* Protection against very long names */
  143. shift = strlen (p) - (MC_MAXPATHLEN - 16);
  144. if (shift > 0)
  145. p += shift;
  146. suffix = g_malloc (MC_MAXPATHLEN);
  147. /* Protection against unusual characters */
  148. q = suffix;
  149. while (*p && (*p != '#'))
  150. {
  151. if (strchr (".-_@", *p) || isalnum ((unsigned char) *p))
  152. *q++ = *p;
  153. p++;
  154. }
  155. *q = 0;
  156. fd = mc_mkstemps (pname, prefix, suffix);
  157. g_free (suffix);
  158. return fd;
  159. }
  160. /* --------------------------------------------------------------------------------------------- */
  161. /** Extract the hostname and username from the path
  162. *
  163. * Format of the path is [user@]hostname:port/remote-dir, e.g.:
  164. *
  165. * ftp://sunsite.unc.edu/pub/linux
  166. * ftp://miguel@sphinx.nuclecu.unam.mx/c/nc
  167. * ftp://tsx-11.mit.edu:8192/
  168. * ftp://joe@foo.edu:11321/private
  169. * ftp://joe:password@foo.se
  170. *
  171. * @param path is an input string to be parsed
  172. * @param default_port is an input default port
  173. * @param flags are parsing modifier flags (@see vfs_url_flags_t)
  174. *
  175. * @return g_malloc()ed url info.
  176. * If the user is empty, e.g. ftp://@roxanne/private, and URL_USE_ANONYMOUS
  177. * is not set, then the current login name is supplied.
  178. * Return value is a g_malloc()ed structure with the pathname relative to the
  179. * host.
  180. */
  181. vfs_path_element_t *
  182. vfs_url_split (const char *path, int default_port, vfs_url_flags_t flags)
  183. {
  184. vfs_path_element_t *path_element;
  185. char *pcopy;
  186. const char *pend;
  187. char *dir, *colon, *inner_colon, *at, *rest;
  188. path_element = g_new0 (vfs_path_element_t, 1);
  189. path_element->port = default_port;
  190. pcopy = g_strdup (path);
  191. pend = pcopy + strlen (pcopy);
  192. dir = pcopy;
  193. if ((flags & URL_NOSLASH) == 0)
  194. {
  195. /* locate path component */
  196. while (*dir != PATH_SEP && *dir != '\0')
  197. dir++;
  198. if (*dir == '\0')
  199. path_element->path = g_strdup (PATH_SEP_STR);
  200. else
  201. {
  202. path_element->path = g_strdup (dir);
  203. *dir = '\0';
  204. }
  205. }
  206. /* search for any possible user */
  207. at = strrchr (pcopy, '@');
  208. /* We have a username */
  209. if (at == NULL)
  210. rest = pcopy;
  211. else
  212. {
  213. *at = '\0';
  214. inner_colon = strchr (pcopy, ':');
  215. if (inner_colon != NULL)
  216. {
  217. *inner_colon = '\0';
  218. inner_colon++;
  219. path_element->password = g_strdup (inner_colon);
  220. }
  221. if (*pcopy != '\0')
  222. path_element->user = g_strdup (pcopy);
  223. if (pend == at + 1)
  224. rest = at;
  225. else
  226. rest = at + 1;
  227. }
  228. if ((flags & URL_USE_ANONYMOUS) == 0)
  229. path_element->user = vfs_get_local_username ();
  230. /* Check if the host comes with a port spec, if so, chop it */
  231. if (*rest != '[')
  232. colon = strchr (rest, ':');
  233. else
  234. {
  235. colon = strchr (++rest, ']');
  236. if (colon != NULL)
  237. {
  238. colon[0] = '\0';
  239. colon[1] = '\0';
  240. colon++;
  241. }
  242. else
  243. {
  244. vfs_path_element_free (path_element);
  245. return NULL;
  246. }
  247. }
  248. if (colon != NULL)
  249. {
  250. *colon = '\0';
  251. if (sscanf (colon + 1, "%d", &path_element->port) == 1)
  252. {
  253. if (path_element->port <= 0 || path_element->port >= 65536)
  254. path_element->port = default_port;
  255. }
  256. else
  257. while (*(++colon) != '\0')
  258. {
  259. switch (*colon)
  260. {
  261. case 'C':
  262. path_element->port = 1;
  263. break;
  264. case 'r':
  265. path_element->port = 2;
  266. break;
  267. }
  268. }
  269. }
  270. path_element->host = g_strdup (rest);
  271. return path_element;
  272. }
  273. /* --------------------------------------------------------------------------------------------- */
  274. void
  275. vfs_die (const char *m)
  276. {
  277. message (D_ERROR, _("Internal error:"), "%s", m);
  278. exit (EXIT_FAILURE);
  279. }
  280. /* --------------------------------------------------------------------------------------------- */
  281. char *
  282. vfs_get_password (const char *msg)
  283. {
  284. return input_dialog (msg, _("Password:"), MC_HISTORY_VFS_PASSWORD, INPUT_PASSWORD);
  285. }
  286. /* --------------------------------------------------------------------------------------------- */