2 #include <stdio.h> /* for FILE etc */
3 #include <stdlib.h> /* for malloc */
4 #define __USE_GNU /* for strcasestr */
5 #include <string.h> /* for making ftw.h happy */
6 #include <unistd.h> /* for fork exec */
8 #include <utmp.h> /* for expand-tilde below; see commentary for this about-turn */
9 #include <sys/types.h> /* ditto */
10 #include <pwd.h> /* ditto */
13 #include "pnd_container.h"
14 #include "pnd_utility.h"
15 #include "pnd_pndfiles.h"
16 #include "pnd_discovery.h"
18 unsigned char pnd_check_login ( char *r_username, unsigned int maxlen ) {
23 f = fopen ( "/var/run/utmp", "r" );
27 // spin until a non-root user comes along
28 while ( fread ( &b, sizeof(struct utmp), 1, f ) == 1 ) {
30 if ( ( b.ut_type == USER_PROCESS ) &&
31 ( strcmp ( b.ut_user, "root" ) != 0 ) )
34 // ut_user contains the username ..
35 // now we need to find the path to that account.
36 while ( ( pw = getpwent() ) ) {
38 if ( strcmp ( pw -> pw_name, b.ut_user ) == 0 ) {
41 strncpy ( r_username, b.ut_user, maxlen );
46 } // passwd entry matches the utmp entry
48 } // while iteratin across passwd entries
51 } // utmp entry is for a user login
61 // a generalized variable-substitution routine might be nice; for now we need a quick tilde one,
62 // so here goes. Brute force ftw!
63 char *pnd_expand_tilde ( char *freeable_buffer ) {
65 char *s = freeable_buffer;
66 char *home = getenv ( "HOME" );
68 printf ( "DEBUG: expand tilde IN: '%s'\n", freeable_buffer );
69 printf ( "DEBUG: $HOME was %s\n", home );
71 // well, as pndnotifyd (etc) may be running as _root_, while the user is logged in
72 // as 'pandora' or god knows what, this could be problematic. Other parts of the lib
73 // use wordexp() for shell-like expansion, but this funciton is (at least) used by
74 // pndnotifyd for determination of the .desktop emit path, so we need ~ to expand
75 // to the _actual user_ rather than root's homedir. Rather than run 'users' or 'who'
76 // or the like, we'll just cut to the chase..
81 static char *florp = NULL;
84 f = fopen ( "/var/run/utmp", "r" );
88 while ( fread ( &b, sizeof(struct utmp), 1, f ) == 1 ) {
90 if ( b.ut_type == USER_PROCESS ) {
92 // ut_user contains the username ..
93 // now we need to find the path to that account.
94 while ( ( pw = getpwent() ) ) {
96 if ( strcmp ( pw -> pw_name, b.ut_user ) == 0 ) {
98 // aight, we've got a logged in user and have matched it to
99 // passwd entry, so can construct the appropriate path
104 florp = strdup ( pw -> pw_dir );
107 printf ( " DEBUG: home (for %s) is %s (from %u)\n", b.ut_user, home, b.ut_type );
109 } // passwd entry matches the utmp entry
111 } // while iteratin across passwd entries
114 } // utmp entry is for a user login
124 return ( s ); // can't succeed
127 printf ( "DEBUG: entering while (%s) with home (%s)\n", s, home );
129 while ( ( p = strchr ( s, '~' ) ) ) {
130 printf ( "DEBUG: within while (%s)\n", s );
131 char *temp = malloc ( strlen ( s ) + strlen ( home ) + 1 );
132 memset ( temp, '\0', strlen ( s ) + strlen ( home ) + 1 );
133 // copy in stuff prior to ~
134 strncpy ( temp, s, p - s );
136 strcat ( temp, home );
137 // copy stuff after tilde in
138 strcat ( temp, p + 1 );
142 } // while finding matches
144 printf ( "DEBUG: expand tilde OUT: '%s'\n", s );
149 void pnd_exec_no_wait_1 ( char *fullpath, char *arg1 ) {
152 if ( ( i = fork() ) < 0 ) {
153 printf ( "ERROR: Couldn't fork()\n" );
158 return; // parent process, don't care
161 // child process, do something
163 execl ( fullpath, fullpath, arg1, (char*) NULL );
165 execl ( fullpath, fullpath, (char*) NULL );
168 // getting here is an error
169 //printf ( "Error attempting to run %s\n", fullpath );
174 pnd_pxml_handle pnd_pxml_get_by_path ( char *fullpath ) {
175 unsigned char valid = pnd_object_type_unknown;
176 pnd_pxml_handle pxmlh = 0;
178 // WARN: this is way too close to callback in pnd_disco .. should be refactored!
180 if ( strcasestr ( fullpath, PXML_FILENAME ) ) {
181 valid = pnd_object_type_directory;
182 } else if ( strcasestr ( fullpath, PND_PACKAGE_FILEEXT "\0" ) ) {
183 valid = pnd_object_type_pnd;
186 // if not a file of interest, just keep looking until we run out
191 // potentially a valid application
192 if ( valid == pnd_object_type_directory ) {
193 pxmlh = pnd_pxml_fetch ( (char*) fullpath );
195 } else if ( valid == pnd_object_type_pnd ) {
197 char pxmlbuf [ 32 * 1024 ]; // TBD: assuming 32k pxml accrual buffer is a little lame
200 f = fopen ( fullpath, "r" );
202 // try to locate the PXML portion
203 if ( ! pnd_pnd_seek_pxml ( f ) ) {
205 return ( 0 ); // pnd or not, but not to spec. Pwn'd the pnd?
208 // accrue it into a buffer
209 if ( ! pnd_pnd_accrue_pxml ( f, pxmlbuf, 32 * 1024 ) ) {
214 // by now, we have <PXML> .. </PXML>, try to parse..
215 pxmlh = pnd_pxml_fetch_buffer ( (char*) fullpath, pxmlbuf );