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 */
11 #include <sys/stat.h> /* for fstat */
12 #include <dirent.h> /* for opendir */
15 #include "pnd_container.h"
16 #include "pnd_utility.h"
17 #include "pnd_pndfiles.h"
18 #include "pnd_discovery.h"
20 unsigned char pnd_check_login ( char *r_username, unsigned int maxlen ) {
25 f = fopen ( "/var/run/utmp", "r" );
29 // spin until a non-root user comes along
30 while ( fread ( &b, sizeof(struct utmp), 1, f ) == 1 ) {
32 if ( ( b.ut_type == USER_PROCESS ) &&
33 ( strcmp ( b.ut_user, "root" ) != 0 ) )
36 // ut_user contains the username ..
37 // now we need to find the path to that account.
38 while ( ( pw = getpwent() ) ) {
40 if ( strcmp ( pw -> pw_name, b.ut_user ) == 0 ) {
43 strncpy ( r_username, b.ut_user, maxlen );
48 } // passwd entry matches the utmp entry
50 } // while iteratin across passwd entries
53 } // utmp entry is for a user login
63 // a generalized variable-substitution routine might be nice; for now we need a quick tilde one,
64 // so here goes. Brute force ftw!
65 char *pnd_expand_tilde ( char *freeable_buffer ) {
67 char *s = freeable_buffer;
68 char *home = getenv ( "HOME" );
70 //printf ( "DEBUG: expand tilde IN: '%s'\n", freeable_buffer );
71 //printf ( "DEBUG: $HOME was %s\n", home );
73 // well, as pndnotifyd (etc) may be running as _root_, while the user is logged in
74 // as 'pandora' or god knows what, this could be problematic. Other parts of the lib
75 // use wordexp() for shell-like expansion, but this funciton is (at least) used by
76 // pndnotifyd for determination of the .desktop emit path, so we need ~ to expand
77 // to the _actual user_ rather than root's homedir. Rather than run 'users' or 'who'
78 // or the like, we'll just cut to the chase..
83 static char *florp = NULL;
86 f = fopen ( "/var/run/utmp", "r" );
90 while ( fread ( &b, sizeof(struct utmp), 1, f ) == 1 ) {
92 if ( b.ut_type == USER_PROCESS ) {
94 // ut_user contains the username ..
95 // now we need to find the path to that account.
96 while ( ( pw = getpwent() ) ) {
98 if ( strcmp ( pw -> pw_name, b.ut_user ) == 0 ) {
100 // aight, we've got a logged in user and have matched it to
101 // passwd entry, so can construct the appropriate path
106 florp = strdup ( pw -> pw_dir );
109 //printf ( " DEBUG: home (for %s) is %s (from %u)\n", b.ut_user, home, b.ut_type );
111 } // passwd entry matches the utmp entry
113 } // while iteratin across passwd entries
116 } // utmp entry is for a user login
126 return ( s ); // can't succeed
129 //printf ( "DEBUG: entering while (%s) with home (%s)\n", s, home );
131 while ( ( p = strchr ( s, '~' ) ) ) {
132 //printf ( "DEBUG: within while (%s)\n", s );
133 char *temp = malloc ( strlen ( s ) + strlen ( home ) + 1 );
134 memset ( temp, '\0', strlen ( s ) + strlen ( home ) + 1 );
135 // copy in stuff prior to ~
136 strncpy ( temp, s, p - s );
138 strcat ( temp, home );
139 // copy stuff after tilde in
140 strcat ( temp, p + 1 );
144 } // while finding matches
146 //printf ( "DEBUG: expand tilde OUT: '%s'\n", s );
151 void pnd_exec_no_wait_1 ( char *fullpath, char *arg1 ) {
154 if ( ( i = fork() ) < 0 ) {
155 printf ( "ERROR: Couldn't fork()\n" );
160 return; // parent process, don't care
163 // child process, do something
165 execl ( fullpath, fullpath, arg1, (char*) NULL );
167 execl ( fullpath, fullpath, (char*) NULL );
170 // error invoking something, and we're the child process, so just die before all hell breaks lose with us thinking we're the (second!) parent on return!
173 // getting here is an error
174 //printf ( "Error attempting to run %s\n", fullpath );
179 pnd_pxml_handle *pnd_pxml_get_by_path ( char *fullpath ) {
180 unsigned char valid = pnd_object_type_unknown;
181 pnd_pxml_handle *pxmlapps = 0;
183 // WARN: this is way too close to callback in pnd_disco .. should be refactored!
185 if ( strcasestr ( fullpath, PXML_FILENAME ) ) {
186 valid = pnd_object_type_directory;
187 } else if ( strcasestr ( fullpath, PND_PACKAGE_FILEEXT "\0" ) ) {
188 valid = pnd_object_type_pnd;
191 // if not a file of interest, just keep looking until we run out
196 // potentially a valid application
197 if ( valid == pnd_object_type_directory ) {
198 pxmlapps = pnd_pxml_fetch ( (char*) fullpath );
200 } else if ( valid == pnd_object_type_pnd ) {
202 char pxmlbuf [ 32 * 1024 ]; // TBD: assuming 32k pxml accrual buffer is a little lame
205 f = fopen ( fullpath, "r" );
207 // try to locate the PXML portion
208 if ( ! pnd_pnd_seek_pxml ( f ) ) {
210 return ( 0 ); // pnd or not, but not to spec. Pwn'd the pnd?
213 // accrue it into a buffer
214 if ( ! pnd_pnd_accrue_pxml ( f, pxmlbuf, 32 * 1024 ) ) {
219 // by now, we have <PXML> .. </PXML>, try to parse..
220 pxmlapps = pnd_pxml_fetch_buffer ( (char*) fullpath, pxmlbuf );
232 unsigned char pnd_determine_mountpoint ( char *fullpath, char *r_mountpoint, unsigned int mountpoint_len ) {
234 // just cheap it, and call df like an idiot.
236 // Filesystem 1K-blocks Used Available Use% Mounted on
238 char cmd [ PATH_MAX ];
240 char inbuf [ PATH_MAX ];
242 sprintf ( cmd, "/bin/df %s 2>/dev/null", fullpath );
244 if ( ( p = popen ( cmd, "r" ) ) ) {
246 // ignore title line; we really shoudl analyze it to figure out which column, but we make assumptions..
247 fgets ( inbuf, PATH_MAX, p );
249 if ( ! fgets ( inbuf, PATH_MAX, p ) ) {
257 char mount [ PATH_MAX ];
258 if ( sscanf ( inbuf, "%*s %*s %*s %*s %*s %s", mount ) != 1 ) {
262 if ( strlen ( mount ) < mountpoint_len ) {
263 strcpy ( r_mountpoint, mount );
274 // can we even stat this file?
275 if ( stat ( fullpath, &fooby ) == 0 ) {
276 //dev_t st_dev; /* ID of device containing file */
277 //dev_t st_rdev; /* device ID (if special file) */
279 dev_t mount = fooby.st_dev;
281 DIR *d = opendir ( "/dev" );
285 char path [ FILENAME_MAX ];
287 while ( de = readdir ( d ) ) {
288 sprintf ( path, "/dev/%s", de -> d_name );
290 if ( stat ( path, &fooby ) == 0 ) {
292 // finally, if we find the same major/minor pair in /dev, as we found for the target file, it means we found the right device
293 if ( fooby.st_rdev == mount ) {
294 printf ( "Device: %s\n", path );
309 unsigned char pnd_filecopy ( char *sourcepath, char *targetpath ) {
310 #define BITLEN (64*1024)
311 FILE *pnd, *target; // pnd == from, since I cribbed the code from pnd_desktop.c :/
312 unsigned char bits [ BITLEN ];
315 pnd = fopen ( sourcepath, "rb" );
323 target = fopen ( targetpath, "wb" );
330 fseek ( pnd, 0, SEEK_END );
332 fseek ( pnd, 0, SEEK_SET );
336 if ( len > (BITLEN) ) {
342 if ( fread ( bits, bitlen, 1, pnd ) != 1 ) {
345 unlink ( targetpath );
349 if ( fwrite ( bits, bitlen, 1, target ) != 1 ) {
352 unlink ( targetpath );