On Mar 28, 2005, at 9:53 AM, Dave Steinberg wrote:
What would have really helped me would have been any of the following:

- A call to perror() after failed mkdir() / chown calls. Not checking those return values was what bubbled up into a cryptic, misleading error message.
- In r_mkdir, testing that tmpbuf either exists and is a directory, or doesn't exist. If its a file / link / pipe / whatever, then things should explode.


Sadly, I don't have time to put together a patch at this time.

What do you think about this patch (cross-posted to vpopmail, since that's where the patch was made).


This patch adds considerably more error checking to r_mkdir.

--- vpopmail.c  1 Mar 2005 01:17:37 -0000       1.28.2.17
+++ vpopmail.c  8 Apr 2005 05:16:23 -0000
@@ -31,6 +31,7 @@
 #include <time.h>
 #include <dirent.h>
 #include <pwd.h>
+#include <errno.h>
 #include "config.h"
 #include "md5.h"
 #include "vpopmail.h"
@@ -1800,19 +1801,40 @@
 int r_mkdir(char *path, uid_t uid, gid_t gid )
 {
  char tmpbuf[MAX_BUFF];
+ int err;
  int i;
+ struct stat sb;
+
+  if (*path == '\0') return 0;

- for(i=0;path[i]!=0;++i){
- if ( (i > 0) && (path[i] == '/') ) {
+ for(i=0; ;++i){
+ if ( (i > 0) && ((path[i] == '/') || (path[i] == '\0')) ) {
tmpbuf[i] = 0;
- if (mkdir(tmpbuf,VPOPMAIL_DIR_MODE) == 0)
+ err = mkdir(tmpbuf,VPOPMAIL_DIR_MODE);
+ if (err == 0)
chown(tmpbuf, uid, gid);
+ else if (errno != EEXIST) {
+ /* Note that if tmpbuf is a file, we'll catch the error on the
+ * next directory creation (ENOTDIR) or when we verify that the
+ * directory exists and is a directory at the end of the function.
+ */
+ fprintf (stderr, "Unable to create directory %s: ", tmpbuf);
+ perror("");
+ return -1;
+ }
+ if (path[i] == '\0') break;
}
tmpbuf[i] = path[i];
}
- mkdir(path,VPOPMAIL_DIR_MODE);
- chown(path, uid, gid);
- return(0);
+ if (stat (path, &sb) != 0) {
+ fprintf (stderr, "Couldn't stat %s: ", path);
+ perror ("");
+ return -1;
+ } else if (! S_ISDIR(sb.st_mode)) {
+ fprintf (stderr, "Error: %s is not a directory.\n", path);
+ return -1;
+ }
+ return 0;
}


/ ************************************************************************ /

--
Tom Collins - [EMAIL PROTECTED]
QmailAdmin: http://qmailadmin.sf.net/ Vpopmail: http://vpopmail.sf.net/
You don't need a laptop to troubleshoot high-speed Internet: sniffter.com




Reply via email to