656
648
/* HOLDS: g_utils_global_lock */
658
g_get_any_init_do (void)
649
static UserDatabaseEntry *
650
g_get_user_database_entry (void)
662
g_tmp_dir = g_strdup (g_getenv ("TMPDIR"));
664
if (g_tmp_dir == NULL || *g_tmp_dir == '\0')
667
g_tmp_dir = g_strdup (g_getenv ("TMP"));
670
if (g_tmp_dir == NULL || *g_tmp_dir == '\0')
673
g_tmp_dir = g_strdup (g_getenv ("TEMP"));
677
if (g_tmp_dir == NULL || *g_tmp_dir == '\0')
680
g_tmp_dir = get_windows_directory_root ();
685
if (g_tmp_dir == NULL || *g_tmp_dir == '\0')
689
g_tmp_dir = g_strdup (P_tmpdir);
690
k = strlen (g_tmp_dir);
691
if (k > 1 && G_IS_DIR_SEPARATOR (g_tmp_dir[k - 1]))
692
g_tmp_dir[k - 1] = '\0';
696
if (g_tmp_dir == NULL || *g_tmp_dir == '\0')
699
g_tmp_dir = g_strdup (g_getenv ("/tmp"));
701
#endif /* !G_OS_WIN32 */
704
/* We check $HOME first for Win32, though it is a last resort for Unix
705
* where we prefer the results of getpwuid().
707
g_home_dir = g_strdup (g_getenv ("HOME"));
709
/* Only believe HOME if it is an absolute path and exists */
712
if (!(g_path_is_absolute (g_home_dir) &&
713
g_file_test (g_home_dir, G_FILE_TEST_IS_DIR)))
720
/* In case HOME is Unix-style (it happens), convert it to
726
while ((p = strchr (g_home_dir, '/')) != NULL)
732
/* USERPROFILE is probably the closest equivalent to $HOME? */
733
if (g_getenv ("USERPROFILE") != NULL)
734
g_home_dir = g_strdup (g_getenv ("USERPROFILE"));
738
g_home_dir = get_special_folder (CSIDL_PROFILE);
741
g_home_dir = get_windows_directory_root ();
742
#endif /* G_OS_WIN32 */
652
static UserDatabaseEntry *entry;
654
if (g_once_init_enter (&entry))
656
static UserDatabaseEntry e;
744
658
#ifdef HAVE_PWD_H
746
struct passwd *pw = NULL;
747
gpointer buffer = NULL;
660
struct passwd *pw = NULL;
661
gpointer buffer = NULL;
751
665
# if defined (HAVE_POSIX_GETPWUID_R) || defined (HAVE_NONPOSIX_GETPWUID_R)
753
# ifdef _SC_GETPW_R_SIZE_MAX
754
/* This reurns the maximum length */
755
glong bufsize = sysconf (_SC_GETPW_R_SIZE_MAX);
667
# ifdef _SC_GETPW_R_SIZE_MAX
668
/* This reurns the maximum length */
669
glong bufsize = sysconf (_SC_GETPW_R_SIZE_MAX);
759
673
# else /* _SC_GETPW_R_SIZE_MAX */
761
675
# endif /* _SC_GETPW_R_SIZE_MAX */
763
logname = (gchar *) g_getenv ("LOGNAME");
768
/* we allocate 6 extra bytes to work around a bug in
769
* Mac OS < 10.3. See #156446
771
buffer = g_malloc (bufsize + 6);
677
logname = (gchar *) g_getenv ("LOGNAME");
682
/* we allocate 6 extra bytes to work around a bug in
683
* Mac OS < 10.3. See #156446
685
buffer = g_malloc (bufsize + 6);
774
688
# ifdef HAVE_POSIX_GETPWUID_R
776
error = getpwnam_r (logname, &pwd, buffer, bufsize, &pw);
777
if (!pw || (pw->pw_uid != getuid ())) {
778
/* LOGNAME is lying, fall back to looking up the uid */
779
error = getpwuid_r (getuid (), &pwd, buffer, bufsize, &pw);
782
error = getpwuid_r (getuid (), &pwd, buffer, bufsize, &pw);
784
error = error < 0 ? errno : error;
690
error = getpwnam_r (logname, &pwd, buffer, bufsize, &pw);
691
if (!pw || (pw->pw_uid != getuid ())) {
692
/* LOGNAME is lying, fall back to looking up the uid */
693
error = getpwuid_r (getuid (), &pwd, buffer, bufsize, &pw);
696
error = getpwuid_r (getuid (), &pwd, buffer, bufsize, &pw);
698
error = error < 0 ? errno : error;
785
699
# else /* HAVE_NONPOSIX_GETPWUID_R */
786
/* HPUX 11 falls into the HAVE_POSIX_GETPWUID_R case */
700
/* HPUX 11 falls into the HAVE_POSIX_GETPWUID_R case */
787
701
# if defined(_AIX) || defined(__hpux)
788
error = getpwuid_r (getuid (), &pwd, buffer, bufsize);
789
pw = error == 0 ? &pwd : NULL;
702
error = getpwuid_r (getuid (), &pwd, buffer, bufsize);
703
pw = error == 0 ? &pwd : NULL;
790
704
# else /* !_AIX */
792
pw = getpwnam_r (logname, &pwd, buffer, bufsize);
793
if (!pw || (pw->pw_uid != getuid ())) {
794
/* LOGNAME is lying, fall back to looking up the uid */
795
pw = getpwuid_r (getuid (), &pwd, buffer, bufsize);
798
pw = getpwuid_r (getuid (), &pwd, buffer, bufsize);
800
error = pw ? 0 : errno;
706
pw = getpwnam_r (logname, &pwd, buffer, bufsize);
707
if (!pw || (pw->pw_uid != getuid ())) {
708
/* LOGNAME is lying, fall back to looking up the uid */
709
pw = getpwuid_r (getuid (), &pwd, buffer, bufsize);
712
pw = getpwuid_r (getuid (), &pwd, buffer, bufsize);
714
error = pw ? 0 : errno;
802
716
# endif /* HAVE_NONPOSIX_GETPWUID_R */
806
/* we bail out prematurely if the user id can't be found
807
* (should be pretty rare case actually), or if the buffer
808
* should be sufficiently big and lookups are still not
811
if (error == 0 || error == ENOENT)
813
g_warning ("getpwuid_r(): failed due to unknown user id (%lu)",
817
if (bufsize > 32 * 1024)
819
g_warning ("getpwuid_r(): failed due to: %s.",
720
/* we bail out prematurely if the user id can't be found
721
* (should be pretty rare case actually), or if the buffer
722
* should be sufficiently big and lookups are still not
725
if (error == 0 || error == ENOENT)
727
g_warning ("getpwuid_r(): failed due to unknown user id (%lu)",
731
if (bufsize > 32 * 1024)
733
g_warning ("getpwuid_r(): failed due to: %s.",
828
742
# endif /* HAVE_POSIX_GETPWUID_R || HAVE_NONPOSIX_GETPWUID_R */
833
pw = getpwuid (getuid ());
838
g_user_name = g_strdup (pw->pw_name);
840
if (pw->pw_gecos && *pw->pw_gecos != '\0')
842
gchar **gecos_fields;
845
/* split the gecos field and substitute '&' */
846
gecos_fields = g_strsplit (pw->pw_gecos, ",", 0);
847
name_parts = g_strsplit (gecos_fields[0], "&", 0);
848
pw->pw_name[0] = g_ascii_toupper (pw->pw_name[0]);
849
g_real_name = g_strjoinv (pw->pw_name, name_parts);
850
g_strfreev (gecos_fields);
851
g_strfreev (name_parts);
855
g_home_dir = g_strdup (pw->pw_dir);
747
pw = getpwuid (getuid ());
752
e.user_name = g_strdup (pw->pw_name);
754
if (pw->pw_gecos && *pw->pw_gecos != '\0')
756
gchar **gecos_fields;
759
/* split the gecos field and substitute '&' */
760
gecos_fields = g_strsplit (pw->pw_gecos, ",", 0);
761
name_parts = g_strsplit (gecos_fields[0], "&", 0);
762
pw->pw_name[0] = g_ascii_toupper (pw->pw_name[0]);
763
e.real_name = g_strjoinv (pw->pw_name, name_parts);
764
g_strfreev (gecos_fields);
765
g_strfreev (name_parts);
769
e.home_dir = g_strdup (pw->pw_dir);
860
774
#else /* !HAVE_PWD_H */
862
776
#ifdef G_OS_WIN32
865
wchar_t buffer[UNLEN+1];
867
if (GetUserNameW (buffer, (LPDWORD) &len))
869
g_user_name = g_utf16_to_utf8 (buffer, -1, NULL, NULL, NULL);
870
g_real_name = g_strdup (g_user_name);
779
wchar_t buffer[UNLEN+1];
781
if (GetUserNameW (buffer, (LPDWORD) &len))
783
e.user_name = g_utf16_to_utf8 (buffer, -1, NULL, NULL, NULL);
784
e.real_name = g_strdup (e.user_name);
873
787
#endif /* G_OS_WIN32 */
875
789
#endif /* !HAVE_PWD_H */
879
g_home_dir = g_strdup (g_getenv ("HOME"));
883
/* change '\\' in %HOME% to '/' */
884
g_strdelimit (g_home_dir, "\\",'/');
887
g_user_name = g_strdup ("somebody");
889
g_real_name = g_strdup ("Unknown");
893
gboolean hostname_fail = (gethostname (hostname, sizeof (hostname)) == -1);
895
DWORD size = sizeof (hostname);
896
gboolean hostname_fail = (!GetComputerName (hostname, &size));
898
g_host_name = g_strdup (hostname_fail ? "localhost" : hostname);
902
g_tmp_dir_cp = g_locale_from_utf8 (g_tmp_dir, -1, NULL, NULL, NULL);
903
g_user_name_cp = g_locale_from_utf8 (g_user_name, -1, NULL, NULL, NULL);
904
g_real_name_cp = g_locale_from_utf8 (g_real_name, -1, NULL, NULL, NULL);
907
g_tmp_dir_cp = g_strdup ("\\");
909
g_user_name_cp = g_strdup ("somebody");
911
g_real_name_cp = g_strdup ("Unknown");
913
/* home_dir might be NULL, unlike tmp_dir, user_name and
917
g_home_dir_cp = g_locale_from_utf8 (g_home_dir, -1, NULL, NULL, NULL);
919
g_home_dir_cp = NULL;
920
#endif /* G_OS_WIN32 */
924
g_get_any_init (void)
927
g_get_any_init_do ();
931
g_get_any_init_locked (void)
933
G_LOCK (g_utils_global);
935
G_UNLOCK (g_utils_global);
792
/* change '\\' in %HOME% to '/' */
793
g_strdelimit (e.home_dir, "\\",'/');
796
e.user_name = g_strdup ("somebody");
798
e.real_name = g_strdup ("Unknown");
800
g_once_init_leave (&entry, &e);
940
807
* g_get_user_name:
968
838
g_get_real_name (void)
970
g_get_any_init_locked ();
840
UserDatabaseEntry *entry;
842
entry = g_get_user_database_entry ();
844
return entry->real_name;
975
848
* g_get_home_dir:
977
* Gets the current user's home directory as defined in the
980
* Note that in contrast to traditional UNIX tools, this function
981
* prefers <filename>passwd</filename> entries over the <envar>HOME</envar>
982
* environment variable.
984
* One of the reasons for this decision is that applications in many
985
* cases need special handling to deal with the case where
986
* <envar>HOME</envar> is
988
* <member>Not owned by the user</member>
989
* <member>Not writeable</member>
990
* <member>Not even readable</member>
992
* Since applications are in general <emphasis>not</emphasis> written
993
* to deal with these situations it was considered better to make
994
* g_get_home_dir() not pay attention to <envar>HOME</envar> and to
995
* return the real home directory for the user. If applications
996
* want to pay attention to <envar>HOME</envar>, they can do:
998
* const char *homedir = g_getenv ("HOME");
1000
* homedir = g_get_home_dir (<!-- -->);
850
* Gets the current user's home directory.
852
* As with most UNIX tools, this function will return the value of the
853
* <envar>HOME</envar> environment variable if it is set to an existing
854
* absolute path name, falling back to the <filename>passwd</filename>
855
* file in the case that it is unset.
857
* If the path given in <envar>HOME</envar> is non-absolute, does not
858
* exist, or is not a directory, the result is undefined.
861
* Before version 2.36 this function would ignore the
862
* <envar>HOME</envar> environment variable, taking the value from the
863
* <filename>passwd</filename> database instead. This was changed to
864
* increase the compatibility of GLib with other programs (and the XDG
865
* basedir specification) and to increase testability of programs
866
* based on GLib (by making it easier to run them from test
869
* If your program has a strong requirement for either the new or the
870
* old behaviour (and if you don't wish to increase your GLib
871
* dependency to ensure that the new behaviour is in effect) then you
872
* should either directly check the <envar>HOME</envar> environment
873
* variable yourself or unset it before calling any functions in GLib.
1003
876
* Returns: the current user's home directory
1006
879
g_get_home_dir (void)
1008
g_get_any_init_locked ();
881
static gchar *home_dir;
883
if (g_once_init_enter (&home_dir))
887
/* We first check HOME and use it if it is set */
888
tmp = g_strdup (g_getenv ("HOME"));
891
/* Only believe HOME if it is an absolute path and exists.
893
* We only do this check on Windows for a couple of reasons.
894
* Historically, we only did it there because we used to ignore $HOME
895
* on UNIX. There are concerns about enabling it now on UNIX because
896
* of things like autofs. In short, if the user has a bogus value in
897
* $HOME then they get what they pay for...
901
if (!(g_path_is_absolute (tmp) &&
902
g_file_test (tmp, G_FILE_TEST_IS_DIR)))
909
/* In case HOME is Unix-style (it happens), convert it to
915
while ((p = strchr (tmp, '/')) != NULL)
921
/* USERPROFILE is probably the closest equivalent to $HOME? */
922
if (g_getenv ("USERPROFILE") != NULL)
923
tmp = g_strdup (g_getenv ("USERPROFILE"));
927
tmp = get_special_folder (CSIDL_PROFILE);
930
tmp = get_windows_directory_root ();
931
#endif /* G_OS_WIN32 */
935
/* If we didn't get it from any of those methods, we will have
936
* to read the user database entry.
938
UserDatabaseEntry *entry;
940
entry = g_get_user_database_entry ();
942
/* Strictly speaking, we should copy this, but we know that
943
* neither will ever be freed, so don't bother...
945
tmp = entry->home_dir;
948
g_once_init_leave (&home_dir, tmp);
2353
#if defined (G_OS_WIN32) && !defined (_WIN64)
2357
#if defined (G_OS_WIN32)
2355
2359
/* Binary compatibility versions. Not for newly compiled code. */
2357
#undef g_find_program_in_path
2360
g_find_program_in_path (const gchar *program)
2362
gchar *utf8_program = g_locale_to_utf8 (program, -1, NULL, NULL, NULL);
2363
gchar *utf8_retval = g_find_program_in_path_utf8 (utf8_program);
2366
g_free (utf8_program);
2367
if (utf8_retval == NULL)
2369
retval = g_locale_from_utf8 (utf8_retval, -1, NULL, NULL, NULL);
2370
g_free (utf8_retval);
2375
#undef g_get_user_name
2378
g_get_user_name (void)
2380
g_get_any_init_locked ();
2381
return g_user_name_cp;
2384
#undef g_get_real_name
2387
g_get_real_name (void)
2389
g_get_any_init_locked ();
2390
return g_real_name_cp;
2393
#undef g_get_home_dir
2396
g_get_home_dir (void)
2398
g_get_any_init_locked ();
2399
return g_home_dir_cp;
2402
#undef g_get_tmp_dir
2405
g_get_tmp_dir (void)
2407
g_get_any_init_locked ();
2408
return g_tmp_dir_cp;
2361
_GLIB_EXTERN const gchar *g_get_user_name_utf8 (void);
2362
_GLIB_EXTERN const gchar *g_get_real_name_utf8 (void);
2363
_GLIB_EXTERN const gchar *g_get_home_dir_utf8 (void);
2364
_GLIB_EXTERN const gchar *g_get_tmp_dir_utf8 (void);
2365
_GLIB_EXTERN gchar *g_find_program_in_path_utf8 (const gchar *program);
2368
g_find_program_in_path_utf8 (const gchar *program)
2370
return g_find_program_in_path (program);
2373
const gchar *g_get_user_name_utf8 (void) { return g_get_user_name (); }
2374
const gchar *g_get_real_name_utf8 (void) { return g_get_real_name (); }
2375
const gchar *g_get_home_dir_utf8 (void) { return g_get_home_dir (); }
2376
const gchar *g_get_tmp_dir_utf8 (void) { return g_get_tmp_dir (); }
2382
* Returns %TRUE if the current process was executed as setuid (or an
2383
* equivalent __libc_enable_secure is available). See:
2384
* http://osdir.com/ml/linux.lfs.hardened/2007-04/msg00032.html
2387
g_check_setuid (void)
2389
/* TODO: get __libc_enable_secure exported from glibc.
2390
* See http://www.openwall.com/lists/owl-dev/2012/08/14/1
2392
#if 0 && defined(HAVE_LIBC_ENABLE_SECURE)
2394
/* See glibc/include/unistd.h */
2395
extern int __libc_enable_secure;
2396
return __libc_enable_secure;
2398
#elif defined(HAVE_ISSETUGID)
2399
/* BSD: http://www.freebsd.org/cgi/man.cgi?query=issetugid&sektion=2 */
2400
return issetugid ();
2401
#elif defined(G_OS_UNIX)
2402
uid_t ruid, euid, suid; /* Real, effective and saved user ID's */
2403
gid_t rgid, egid, sgid; /* Real, effective and saved group ID's */
2405
static gsize check_setuid_initialised;
2406
static gboolean is_setuid;
2408
if (g_once_init_enter (&check_setuid_initialised))
2410
#ifdef HAVE_GETRESUID
2411
/* These aren't in the header files, so we prototype them here.
2413
int getresuid(uid_t *ruid, uid_t *euid, uid_t *suid);
2414
int getresgid(gid_t *rgid, gid_t *egid, gid_t *sgid);
2416
if (getresuid (&ruid, &euid, &suid) != 0 ||
2417
getresgid (&rgid, &egid, &sgid) != 0)
2418
#endif /* HAVE_GETRESUID */
2420
suid = ruid = getuid ();
2421
sgid = rgid = getgid ();
2426
is_setuid = (ruid != euid || ruid != suid ||
2427
rgid != egid || rgid != sgid);
2429
g_once_init_leave (&check_setuid_initialised, 1);