From: Frédéric Marchal Date: Thu, 31 Dec 2009 07:25:17 +0000 (+0000) Subject: Directories deleted without using the rm system command X-Git-Tag: v2_2_7~38 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=51465d0855a1aaaf470d0ecdd65edbbb4785d3cf;p=thirdparty%2Fsarg.git Directories deleted without using the rm system command --- diff --git a/ChangeLog b/ChangeLog index 7b064f6..00fdc2b 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,6 +1,6 @@ SARG ChangeLog -Dec-27-2009 Version 2.2.7 +Dec-31-2009 Version 2.2.7 - Extra compile and run time protection (FORTIFY_SOURCE) fixed in configure. - Use tabulations as columns separator in intermediary files to avoid problems when a field of the log contains a space. - Input log file type detection partly rewritten to clearly distinguish which type is processed where. @@ -8,25 +8,26 @@ Dec-27-2009 Version 2.2.7 - Use string pointers in getword instead of copying the strings over and over. - Use LC_TIME to format some dates and times in the report according to the selected locale. - Sarg.conf can list up to 255 access.log files. - - Downloaded files suffixes are matched against a sorted list and use a dicchotomic search. + - Downloaded files suffixes are matched against a sorted list and use a dichotomic search. - Added getword_atoll to read a number directly from a file without an intermediary string storage. - Use boolean to enable the options instead of string compares. - Accept an absolute path for the language file in sarg.conf. - Experimental: Can show the backtrace of the program when a getword loop is detected to help in locating the origin of the error. - Protect the creation of the index against invalid directories. - Only copy the files when creating the directory with the images to link the reports to. + - Directories deleted without using the rm system command. Dec-17-2009 Version 2.2.6.1 - - Remove unecessary dependency on off_t. + - Remove unnecessary dependency on off_t. - Configuration doesn't fail if rlim_t is not available. - Test for the availability of -Werror=implicit-function-declaration and -Werror=format flags in gcc (thanks to Murilo Moreira de Oliveira and pjetko). - Fix sort delimiter when reading useragent log (thanks to Maxim Britov). - Fix user agent report font size and statistics on user agent. - Test for the availability of -Werror=format-security in gcc (thanks to Maxim Britov). - - Test the existence of bzero with autoconf and don't redefine it (tkanks to Maxim Britov). + - Test the existence of bzero with autoconf and don't redefine it (thanks to Maxim Britov). - Remove warnings if libgd is not available. - The date stored in sarg-date is now stored in a more machine readable form (thanks to rcastanheira for pointing this out). - - The date read from sarg-date was not properly parsed and would produce a wrongly sorted index accross a year change (thanks to rcastanheira for pointing this out). + - The date read from sarg-date was not properly parsed and would produce a wrongly sorted index across a year change (thanks to rcastanheira for pointing this out). - my_mkdir changed to avoid the use of getword, be simpler and, maybe, work reliably on windows. Oct-14-2009 Version 2.2.6 @@ -55,7 +56,7 @@ Oct-14-2009 Version 2.2.6 - Replaced the ifdef FOPEN64 by a single call to MY_FOPEN for code clarity. - User IP/ID is searched only in the first column of the usertab file. - Some of indentation fixes. - - Speed up by removing two uncessary ip resolutions. + - Speed up by removing two unnecessary ip resolutions. - Applied debian patches: sarg-polish.patch, sarg-turkish.patch - Applied debian patch: opensuse_2_enlarge_report_buffers.patch - Applied debian patch: opensuse_3_too_small_font_buffer.patch @@ -63,7 +64,7 @@ Oct-14-2009 Version 2.2.6 - Applied and extended debian patch: opensuse_5_limit_sprintf.patch - Function debuga format the output string itself, so there is no need to use sprintf. - Applied debian patch: opensuse_7_year_assertion.patch - - Removed a few unecessary strings copy. + - Removed a few unnecessary strings copy. - Embryo of documentation for the code with doxygen. - Changed subs to replace the string in place and avoid allocating a memory that was freed on only one occasion. - A few improvements on strings manipulations. @@ -94,7 +95,7 @@ Oct-14-2009 Version 2.2.6 - Compute the report date without resorting to the system command. - Allow the use of spaces in the temporary directory. - Fixed bug #2863485. - - Removed missplaced off_t as suggested in bug #2864425. + - Removed misplaced off_t as suggested in bug #2864425. - Z files are uncompressed by zcat to avoid deleting the original log file. - Compressed log files are uncompressed in /tmp/sarg to delete the file when the process completes. - Fixed several memory leaks. @@ -123,7 +124,7 @@ Jan-02-2007 version 2.2.3.1 - wrong results when userid contain '%20' character - previous reports info wrong in index.html -Dez/23/2006 Version 2.2.3 +Dec/23/2006 Version 2.2.3 - gd graph font change from FreeSans.ttf to DejaVuSans.ttf - minor fixes when only denied records in access.log file. - ulimit tag changed. If you run sarg with a low privilege user, @@ -139,7 +140,7 @@ Dez/23/2006 Version 2.2.3 - after reading the squidGuard configuration, the data read from the usertab file was corrupted. Thanks to Daniel Vogel - - milisec values showed as bytes. Thanks to Carlos + - millisec values showed as bytes. Thanks to Carlos - with squidGuard, you can log groups in only one log file. We must parse each log files only one time. Thanks to Christophe BADINA @@ -157,10 +158,10 @@ Dez/23/2006 Version 2.2.3 - temporary file names changed to sarg-... to avoid conflicts with usernames. Thanks to Ukrainian Elephant - SARG support Japanese wordset but not support charset. - submited by anonymous at sourceforge.net Many thanks. + submitted by anonymous at sourceforge.net Many thanks. - support for utf-8 character set added by Peter Warasin . Many thanks. - - if all the data is at the hour each day, totaliza_day() treates it all as being + - if all the data is at the hour each day, totaliza_day() treats it all as being on the last day. This is observed for users which only download via a cron job at the same time each day. Now the date and time will be checked to see if changed. @@ -170,7 +171,7 @@ Dez/23/2006 Version 2.2.3 - Russian_koi8 and Russian_windows1251 languages updated by Stas Degteff . Many thanks - realtime reporting is sorted by URL, and not by Date/Time. - Thanks tp Puce-Xcit + Thanks to Puce-Xcit Aug/29/2006 Version 2.2.2 - malloc test implemented to avoid high-memory consuming. @@ -201,7 +202,7 @@ May/17/2006 Version 2.2 Thanks to Antonio F. Zago to share your Fedora Core 5 64 bits with me. - fixed conflict types for 'saverecs2' in .configure - - fixed no index.html when lastlog tag is greather then 0, + - fixed no index.html when lastlog tag is greater then 0, caused by some debug messages. - removed some development debug message - user_limit_block script added to block users when limit exceeded @@ -215,7 +216,7 @@ May/17/2006 Version 2.2 . strcpy+strcat is faster than sprintf (fixed in some places); . unused variables removed; . fixed headers look and feel (); - . fixed word 'BYTES' localisation for datetime user report; + . fixed word 'BYTES' localization for datetime user report; . fixed corrupted top header on index, denied, download and tt-reports; . fixed "No" table header in topsites; . fixed user report table look; @@ -238,7 +239,7 @@ Nov/29/2005 version 2.1 TAG: realtime_access_log_lines num TAG: realtime_types: GET,PUT,CONNECT,ICP_QUERY,POST TAG: realtime_unauthenticated_records: ignore|show - - garbage in topuser report with unitialized variable. + - garbage in topuser report with uninitialized variable. Thanks to Craig Brockmeier - memory leaks caused by a wrong variable size Fixed by Klaus Singvogel . Thanks. @@ -252,14 +253,14 @@ Nov/29/2005 version 2.1 Thanks to Fabio Lo Votrico - bug fixed in squidguard report module - squidguard_ignore_date on|off tag added to sarg.conf file - You can ignore the squidguard log record date if outisde of + You can ignore the squidguard log record date if outside of date range in squid access.log file. - alternate squidguard log added using -L option on sarg command line. Thanks to Dave Karlson - - fixed malloc withou free. + - fixed malloc without free. - datafile-url ip|name added to sarg.conf file - saves ip address or name in url when using datafile tag. Thanks to Calvin Muller - - wronk link point in Generated by sarg-2.. + - wrong link point in Generated by sarg-2.. Thanks to Markus Hoffmann - Russian_UTF-8 language added by Alex Deiter @@ -291,7 +292,7 @@ May/29/2005 version 2.0.8 added. Thanks to Filippo Grassilli - Spanish language fixed by Jos� Luis Hern�ndez L�pez. Thanks. - - url variable size changed to acomodate big urls with coded symbols and some + - url variable size changed to accommodate big urls with coded symbols and some minor changes in util.c file. Thanks to Oleg - download suffixes improved by Oleg . Thanks. @@ -313,20 +314,20 @@ May/02/2005 version 2.0.7 - exclude_users ignored in some situations. - Fixes by Sapon Oleg from Russia: . there are two equal lines about Evren Yurtesen in CONTRIBUTORS file. - . Internationalisation of SARG user graph added, using iconv function, which + . Internationalization of SARG user graph added, using iconv function, which is presented only in Linux, so I add required #ifdef and other stuff to clean compile code on other platforms. . Fixed two issues: - '?' symbol problems for apache and other cgi-supporting web server which - doesn't support '?' symbol in links (all of them interprete this like + doesn't support '?' symbol in links (all of them interpret this like parameter to cgi script); - some good optimization to all three similar cicles in these files by reducing false checks in 'if ...' strings. - . Fixes segfault, produced by inproper use of strncpy functions, look - strncpy + . Fixes segfault, produced by improper use of strncpy functions, look - strncpy doesn't copy leading '\0' symbol! - . Just localisation support for repday report. + . Just localization support for repday report. . Support usertab IP->USERNAME change in siteuser report. - . Just proper Russian koi8 localisation. + . Just proper Russian koi8 localization. Apr/22/2005 version 2.0.6 - Greek language by Antonis Maglaras @@ -362,13 +363,13 @@ Feb/17/2005 version 2.0.4 vrfydir(): fix segfault if sub-directory "images" is not exists function builddia() don't check the parameters. Result: segmentation fault in some cases. Thanks to Stas Degteff stas_degteff@users.sourceforge.net for the fixes. - - fixed: wrond date period in squidguard_log when using european date format. + - fixed: wrong date period in squidguard_log when using European date format. Thanks to Guenther Mair - ulimit tag added on sarg.conf to avoid "Too many open files" error. Thanks to Paulo Pires -Feb/02/2005 verion 2.0.3 +Feb/02/2005 version 2.0.3 - squidguard parse logs method changed. Thanks to Joao Mendes Thanks to Guenther Mair @@ -381,7 +382,7 @@ Nov/22/2004: version 2.0.2 - grepday with invalid font path. Thanks to Marcelo Ricardo Leitner - -v option added to display Sarg version on console - - Segmenation fault caused by an unclosed file. + - Segmentation fault caused by an unclosed file. Thanks to Pustovalov Leonid - SquidGuard log formats added. Thanks to Kolotov Alexandr @@ -415,12 +416,12 @@ Sep/29/2004: - version 2.0 - Report limits implemented - Download report implemented - Sarg logo by Osamu Matsuzaki added. - - Internal mkdir added to easy port to various plataform. + - Internal mkdir added to easy port to various platform. - long url now show only accessed site and module name. - now you can add a user report url to a flat file to be blocked by some Squid acl. Request by Francesco Collini - - remove temporary files, if already exist, to avoid conflits with a + - remove temporary files, if already exist, to avoid conflicts with a previous sarg process.. Thanks to Renato Leon - squidGuard improved @@ -514,10 +515,10 @@ Apr/03/2002: - bug fixed: cannot create temporary file when url contain '/' and using long_url Thanks to Enrico Sorge -Dez/10/2001: - Added: sarg now shows statistics when readding the log file +Dez/10/2001: - Added: sarg now shows statistics when reading the log file if running with -x option -Dez/05/2001: - bug fixed: - loop proccess when using "tmp" chars in usernames +Dez/05/2001: - bug fixed: - loop process when using "tmp" chars in usernames Thanks to Jasper vd Heijden new tag added: show_successful_message yes Show "Successful report generated on dir" message at end of process. @@ -532,7 +533,7 @@ Dez/05/2001: - bug fixed: - loop proccess when using "tmp" chars in usernames topuser_num Now you can choose which fields must be in Topuser report and how many users. Requested by Hidde Beumer - bug fiexed: auth_failures report missing + bug fixed: auth_failures report missing Thanks to Clary Harridge Polish language added by Wszebor Boksa Thank you. @@ -595,9 +596,9 @@ Apr/02/2001: - implementation of --enable-bindir and --enable-sysconfdir in conf - Latvian languages by Juris Valdovskis Thank you -Mar/22/2001: - Now te user can choose the language using the language TAG in /usr/local/sarg/sarg.conf file. +Mar/22/2001: - Now the user can choose the language using the language TAG in /usr/local/sarg/sarg.conf file. There is no more --enable-language in configure script. - - New report added: Autentication Failures + - New report added: Authentication Failures Requested by Sergei Dushenkov - NONE/400 (bad request) is excluded by default (/usr/local/sarg/exclude_codes) - Now sarg support big numbers (64 bits) in any platform. @@ -612,7 +613,7 @@ Mar/22/2001: - Now te user can choose the language using the language TAG in /us Thanks to vrush_22@softhome.net - bug fixed: per_user_limit file with duplicate users. - Russian languages fixed by Nikolai V. Ivanyushin . Thank you. - - charset TAG added to sarg.conf file. The following charsets are avaiable: + - charset TAG added to sarg.conf file. The following charsets are available: Latin1 (West European), Latin2 (East European), Latin3 (South European) Latin4 (North European), Cyrillic, Arabic, Greek, Hebrew Latin5 (Turkish), Latin6 (Nordic) @@ -706,7 +707,7 @@ Mar/15/2000: Japanese language added by Thank you. Bug fixed: wrong sort field when lower case used in sort tags. - Thaks to Scott Larsen + Thanks to Scott Larsen and to WOLETZ, Dusan Abuse report removed. Changed: report1_sort_field TAG in sqmgrlog.conf @@ -715,7 +716,7 @@ Mar/15/2000: Japanese language added by changed to user_sort_field Added: topsites sort field options added: # TAG: topsites_sort_order CONNECT|BYTES A|D - # Sort for topsites report, where A=Ascendent, D=Descendent + # Sort for topsites report, where A=Ascendant, D=Descendant # #topsite_sort_oder CONNECT D Implementation requested by @@ -788,7 +789,7 @@ Sep/10/1999: Spanish language added by Leonardo A. D'Angelo Aug/18/1999: New fields added to sqmgrlog.conf: @@ -800,15 +801,15 @@ Aug/18/1999: New fields added to sqmgrlog.conf: Thank you for your help. - remove_temp_files yes|no Remove temporary files: geral, usuarios, top, periodo from root report directory. - Impementation requested by Fred Pacquier + Implementation requested by Fred Pacquier - index yes|no|only - Usage: index yes - will genereate the main index.html in reports + Usage: index yes - will generate the main index.html in reports index no - dont generate the main index.html in reports index only - generate only the main index.html in reports Implementation asked by Fred Pacquier - overwrite_report yes|no - If yes and the report already exist for that date, the reports will be overwrited. - If no and the report alread exist, that report will be renamed to filename.n, filename.n+1 + If yes and the report already exist for that date, the reports will be overwritten. + If no and the report already exist, that report will be renamed to filename.n, filename.n+1 Implementation requested by Eugeny Kuzakov - records_without_userid ignore|ip|everybody What can I do with records without user id (no authentication) in access.log file ? @@ -825,7 +826,7 @@ Aug/18/1999: New fields added to sqmgrlog.conf: Added: now every squid code that contain the MISS word will be counted as out cache. Implementation requested by Eugeny Kuzakov Thank you for your help and for share your FreeBSD with me. - Changed: the field url was changed to 8192 bytes to suport big urls. + Changed: the field url was changed to 8192 bytes to support big urls. Thanks to Eugeny Kuzakov Changed: the header "TIME SPENT" changed to "USED TIME" in topuser report. Changed: no more nouser will be in reports but IP address instead. @@ -866,7 +867,7 @@ Jun/14/1999: German language added by Andreas Piesk Jun/09/1999: Bug fixed: to send reports via mail, use mail instead mailx in FreeBSD. Thanks to Evren Yurtesen to share your FreeBSD with me. - Bug fixed: wrong userid when using smb_auth autenticator. + Bug fixed: wrong userid when using smb_auth authenticator. The userid will be changed from NTDOMAIN\username to NTDOMAIN.username. Thanks to Jouko Ylikulju @@ -973,7 +974,7 @@ Dec/07/1998: Version 2.1 released Nov/09/1998: Version 2.0 released -p option added. Allow redirect output report to email address. - sqmgrlog.conf with colors, headers, title, font size, backgound color, text color, + sqmgrlog.conf with colors, headers, title, font size, background color, text color, background image, font face, etc... added. Reports will be generated only for users in passwd file (tag password in sqmgrlog.conf). Many changes in html output layout. @@ -985,7 +986,7 @@ Oct/19/1998: Bug fixed by Maxim Berlin for long long Sep/28/1998: Bug fixed for log with emulate http on (Squid 1.2.beta) Thanks to Alp - -p option added. Generated reports with IP Address insted Userid. + -p option added. Generated reports with IP Address instead Userid. Use this option if you do not use Proxy authentication on Squid. Implementation requested by Szalma Zoltan diff --git a/documentation/util.txt b/documentation/util.txt index 4b032e9..15ce186 100644 --- a/documentation/util.txt +++ b/documentation/util.txt @@ -826,3 +826,15 @@ Write a string in a file and replace the problematic ASCII characters by their e \param fp_ou The handle of the output file. \param str The string to output. */ + + + + + +/*! \fn void unlinkdir(const char *dir,int contentonly) +Delete a directory and its content. + +\param dir The name of the directory to delete. +\param contentonly \c True to delete only the content of the directory and leave the directory +itself in place. If set to \c zero, the directory is removed too. +*/ diff --git a/email.c b/email.c index da36e10..3a53b59 100644 --- a/email.c +++ b/email.c @@ -349,16 +349,11 @@ int geramail(const char *dirname, int debug, const char *outdir, int userip, con } } - if (snprintf(csort,sizeof(csort),"rm -rf \"%s/sarg\"",TempDir)>=sizeof(csort)) { - fprintf(stderr,"SARG: Temporary directory name too long: %s\n",TempDir); - exit(1); - } - cstatus=system(csort); - if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) { - fprintf(stderr, "SARG: command return status %d\n",WEXITSTATUS(cstatus)); - fprintf(stderr, "SARG: command: %s\n",csort); + if (snprintf(warea,sizeof(warea),"%s/sarg",TempDir)>=sizeof(warea)) { + fprintf(stderr,"SARG: Temporary directory name too long: %s\n",warea); exit(1); } + unlinkdir(warea,0); return (0); } diff --git a/include/defs.h b/include/defs.h index cb1a645..89e3bac 100755 --- a/include/defs.h +++ b/include/defs.h @@ -180,3 +180,4 @@ char *get_param_value(const char *param,char *line); void read_usertab(const char *UserTabFile); void get_usertab_name(const char *user,char *name,int namelen); int compar( const void *, const void * ); +void unlinkdir(const char *dir,int contentonly); diff --git a/lastlog.c b/lastlog.c index 3131c06..31fea55 100644 --- a/lastlog.c +++ b/lastlog.c @@ -103,17 +103,11 @@ void mklastlog(const char *outdir) if(debug) debuga("%s: %s",text[81],gwarea.current); - if (snprintf(temp,sizeof(temp),"rm -r \"%s%s\"",outdir,gwarea.current)>=sizeof(temp)) { + if (snprintf(temp,sizeof(temp),"%s%s",outdir,gwarea.current)>=sizeof(temp)) { fprintf(stderr,"SARG: Directory name too long: %s%s\n",outdir,gwarea.current); exit(1); } - cstatus=system(temp); - if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) { - fprintf(stderr, "SARG: command return status %d\n",WEXITSTATUS(cstatus)); - fprintf(stderr, "SARG: command: %s\n",temp); - exit(1); - } - unlink(temp); + unlinkdir(temp,0); ftot--; } diff --git a/log.c b/log.c index a159326..72b02ec 100644 --- a/log.c +++ b/log.c @@ -541,21 +541,10 @@ int main(int argc,char *argv[]) strcat(tmp2,"/sarg.log"); - sprintf(warea,"%s/sarg",tmp); - if(access(warea, R_OK) == 0) { - if (snprintf(tmp3,sizeof(tmp3),"rm -rf \"%s\"",warea)>=sizeof(tmp3)) { - fprintf(stderr,"SARG: Directory name too long: %s\n",warea); - exit(1); - } - cstatus=system(tmp3); - if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) { - fprintf(stderr, "SARG: command return status %d\n",WEXITSTATUS(cstatus)); - fprintf(stderr, "SARG: command: %s\n",tmp3); - exit(1); - } - } - sprintf(tmp3,"%s/sarg",tmp); + if(access(tmp3, R_OK) == 0) { + unlinkdir(tmp3,1); + } my_mkdir(tmp3); strcpy(tmp4,tmp3); strcpy(tmp5,tmp3); @@ -1581,16 +1570,7 @@ int main(int argc,char *argv[]) // else unlink(arq); if(strcmp(tmp,"/tmp") != 0) { - if (snprintf(tmp4,sizeof(tmp4),"rm -rf \"%s\"",tmp)>=sizeof(tmp4)) { - fprintf(stderr,"SARG: Directory name too long: %s\n",tmp); - exit(1); - } - cstatus=system(tmp4); - if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) { - fprintf(stderr, "SARG: command return status %d\n",WEXITSTATUS(cstatus)); - fprintf(stderr, "SARG: command: %s\n",tmp4); - exit(1); - } + unlinkdir(tmp,0); } free_excludecodes(); diff --git a/util.c b/util.c index f956378..0d0f031 100644 --- a/util.c +++ b/util.c @@ -865,7 +865,6 @@ void vrfydir(const char *per1, const char *addr, const char *site, const char *u char m1[8], m2[8]; time_t curtime; struct tm *loctm; - int cstatus; if(strcmp(IndexTree,"date") == 0) { bzero(y1,sizeof(y1)); @@ -932,8 +931,7 @@ void vrfydir(const char *per1, const char *addr, const char *site, const char *u strcat(wdir,site); } - if(strcmp(dirname,wdir) != 0) - strcpy(dirname,wdir); + strcpy(dirname,wdir); if(strcmp(IndexTree,"date") != 0) { if(!OverwriteReport) { @@ -953,14 +951,8 @@ void vrfydir(const char *per1, const char *addr, const char *site, const char *u rename(dirname,wdir); } } else { - if(access(wdir,R_OK) == 0) { - sprintf(csort,"rm -r \"%s\"",wdir); - cstatus=system(csort); - if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) { - fprintf(stderr, "SARG: command return status %d\n",WEXITSTATUS(cstatus)); - fprintf(stderr, "SARG: command: %s\n",csort); - exit(1); - } + if(access(dirname,R_OK) == 0) { + unlinkdir(dirname,1); } } my_mkdir(dirname); @@ -985,13 +977,7 @@ void vrfydir(const char *per1, const char *addr, const char *site, const char *u } } else { if(access(wdir,R_OK) == 0) { - sprintf(csort,"rm -r \"%s\"",wdir); - cstatus=system(csort); - if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) { - fprintf(stderr, "SARG: command return status %d\n",WEXITSTATUS(cstatus)); - fprintf(stderr, "SARG: command: %s\n",csort); - exit(1); - } + unlinkdir(wdir,1); } } @@ -1479,8 +1465,7 @@ void output_html_string(FILE *fp_ou,const char *str) void baddata(void) { - char cmd[1024]; - int cstatus; + char dir[1024]; printf("SARG: ------------------------------------------------------------------------------\n"); printf("SARG: MALICIUS CODE DETECTED.\n"); @@ -1489,26 +1474,12 @@ void baddata(void) printf("SARG: process stoped. No actions taken.\n"); printf("SARG: ------------------------------------------------------------------------------\n"); - if (snprintf(cmd,sizeof(cmd),"rm -rf \"%s/sarg\"",tmp)>=sizeof(cmd)) { + if (snprintf(dir,sizeof(dir),"%s/sarg",tmp)>=sizeof(dir)) { fprintf(stderr,"SARG: temporary directory too long: %s/sarg\n",tmp); exit(1); } - cstatus=system(cmd); - if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) { - fprintf(stderr, "SARG: command return status %d\n",WEXITSTATUS(cstatus)); - fprintf(stderr, "SARG: command: %s\n",cmd); - exit(1); - } - if (snprintf(cmd,sizeof(cmd),"rm -rf \"%s\"",dirname)>=sizeof(cmd)) { - fprintf(stderr,"SARG: directory to delete too long: %s\n",dirname); - exit(1); - } - cstatus=system(cmd); - if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) { - fprintf(stderr, "SARG: command return status %d\n",WEXITSTATUS(cstatus)); - fprintf(stderr, "SARG: command: %s\n",cmd); - exit(1); - } + unlinkdir(dir,0); + unlinkdir(dirname,0); exit(1); } @@ -1645,3 +1616,45 @@ void get_usertab_name(const char *user,char *name,int namelen) } } } + +void unlinkdir(const char *dir,int contentonly) +{ + struct stat st; + DIR *dirp; + struct dirent *direntp; + char dname[MAXLEN]; + + dirp=opendir(dir); + if (!dirp) return; + while ((direntp = readdir(dirp)) != NULL) { + if (direntp->d_name[0] == '.' && (direntp->d_name[1] == '\0' || + (direntp->d_name[1] == '.' && direntp->d_name[2] == '\0'))) + continue; + if (snprintf(dname,sizeof(dname),"%s/%s",dir,direntp->d_name)>=sizeof(dname)) { + fprintf(stderr,"SARG: directory name to delete too long: %s/%s\n",dir,direntp->d_name); + exit(1); + } + if (lstat(dname,&st)) { + fprintf(stderr,"SARG: cannot stat %s\n",dname); + exit(1); + } + if (S_ISREG(st.st_mode)) { + if (unlink(dname)) { + fprintf(stderr,"SARG: cannot delete %s - %s\n",dname,strerror(errno)); + exit(1); + } + } else if (S_ISDIR(st.st_mode)) { + unlinkdir(dname,1); + } else { + fprintf(stderr,"SARG: unknown path type %s\n",dname); + } + } + closedir(dirp); + + if (!contentonly) { + if (rmdir(dir)) { + fprintf(stderr,"SARG: cannot delete %s - %s\n",dir,strerror(errno)); + exit(1); + } + } +}