From c414a3f418f191163d5dd8813ba5d1df599d3c42 Mon Sep 17 00:00:00 2001 From: skeezix Date: Tue, 22 Dec 2009 10:31:24 -0500 Subject: [PATCH] Added a bit more debug, check_login() will ignore root as a candidate, and set default $HOME to path found in filesystem as its sometimes inaccurate too early --- apps/pndnotifyd.c | 33 +++++++++++++++++++++++++++++++++ lib/pnd_utility.c | 13 ++++++++----- 2 files changed, 41 insertions(+), 5 deletions(-) diff --git a/apps/pndnotifyd.c b/apps/pndnotifyd.c index 73fcbab..3301f2c 100644 --- a/apps/pndnotifyd.c +++ b/apps/pndnotifyd.c @@ -402,6 +402,39 @@ void consume_configuration ( void ) { pnd_log ( pndn_rem, "No pndhup found (which is fine.)\n" ); } + /* cheap hack, maybe it'll help when pndnotifyd is coming up before the rest of the system and before + * the user is formally logged in + */ + pnd_log ( pndn_rem, "Setting a default $HOME to non-root user\n" ); + { + DIR *dir; + + if ( ( dir = opendir ( "/home" ) ) ) { + struct dirent *dirent; + + while ( ( dirent = readdir ( dir ) ) ) { + pnd_log ( pndn_rem, " Found user homedir '%s'\n", dirent -> d_name ); + + // file is a .desktop? + if ( dirent -> d_name [ 0 ] == '.' ) { + continue; + } else if ( strcmp ( dirent -> d_name, "root" ) == 0 ) { + continue; + } + + // a non-root user is found + char buffer [ 200 ]; + sprintf ( buffer, "/home/%s", dirent -> d_name ); + pnd_log ( pndn_rem, " Going with '%s'\n", buffer ); + setenv ( "HOME", buffer, 1 /* overwrite */ ); + + } // while iterating through dir listing + + closedir ( dir ); + } // opendir? + + } // scope + /* handle globbing or variable substitution */ desktop_dotdesktoppath = pnd_expand_tilde ( strdup ( desktop_dotdesktoppath ) ); diff --git a/lib/pnd_utility.c b/lib/pnd_utility.c index 22f0db5..3733ad0 100644 --- a/lib/pnd_utility.c +++ b/lib/pnd_utility.c @@ -24,9 +24,12 @@ unsigned char pnd_check_login ( char *r_username, unsigned int maxlen ) { if ( f ) { + // spin until a non-root user comes along while ( fread ( &b, sizeof(struct utmp), 1, f ) == 1 ) { - if ( b.ut_type == USER_PROCESS ) { + if ( ( b.ut_type == USER_PROCESS ) && + ( strcmp ( b.ut_user, "root" ) != 0 ) ) + { // ut_user contains the username .. // now we need to find the path to that account. @@ -62,8 +65,8 @@ char *pnd_expand_tilde ( char *freeable_buffer ) { char *s = freeable_buffer; char *home = getenv ( "HOME" ); - //printf ( "expand tilde IN: '%s'\n", freeable_buffer ); - //printf ( " home was %s\n", home ); + printf ( "DEBUG: expand tilde IN: '%s'\n", freeable_buffer ); + printf ( "DEBUG: $HOME was %s\n", home ); // well, as pndnotifyd (etc) may be running as _root_, while the user is logged in // as 'pandora' or god knows what, this could be problematic. Other parts of the lib @@ -101,7 +104,7 @@ char *pnd_expand_tilde ( char *freeable_buffer ) { florp = strdup ( pw -> pw_dir ); home = florp; - printf ( " DEBUG: home is %s (from %u)\n", home, b.ut_type ); + printf ( " DEBUG: home (for %s) is %s (from %u)\n", b.ut_user, home, b.ut_type ); } // passwd entry matches the utmp entry @@ -121,7 +124,7 @@ char *pnd_expand_tilde ( char *freeable_buffer ) { return ( s ); // can't succeed } - printf ( "DEBUG: entering while (%s)\n", s ); + printf ( "DEBUG: entering while (%s) with home (%s)\n", s, home ); while ( ( p = strchr ( s, '~' ) ) ) { printf ( "DEBUG: within while (%s)\n", s ); -- 2.39.2