add --prefix option: some fixes + fixed pwd.lock file location

This commit is contained in:
fariouche 2018-03-28 21:14:12 +02:00
parent 54551c7d6e
commit 65b4f58703
3 changed files with 31 additions and 16 deletions

View File

@ -375,10 +375,11 @@ bool commonio_present (const struct commonio_db *db)
int commonio_lock_nowait (struct commonio_db *db, bool log) int commonio_lock_nowait (struct commonio_db *db, bool log)
{ {
char* file; char* file = NULL;
char* lock; char* lock = NULL;
size_t lock_file_len; size_t lock_file_len;
size_t file_len; size_t file_len;
int err;
if (db->locked) { if (db->locked) {
return 1; return 1;
@ -386,26 +387,36 @@ int commonio_lock_nowait (struct commonio_db *db, bool log)
file_len = strlen(db->filename) + 11;/* %lu max size */ file_len = strlen(db->filename) + 11;/* %lu max size */
lock_file_len = strlen(db->filename) + 6; /* sizeof ".lock" */ lock_file_len = strlen(db->filename) + 6; /* sizeof ".lock" */
file = (char*)malloc(file_len); file = (char*)malloc(file_len);
if(file == NULL) {
err = ENOMEM;
goto cleanup_ENOMEM;
}
lock = (char*)malloc(lock_file_len); lock = (char*)malloc(lock_file_len);
if(lock == NULL) {
err = ENOMEM;
goto cleanup_ENOMEM;
}
snprintf (file, file_len, "%s.%lu", snprintf (file, file_len, "%s.%lu",
db->filename, (unsigned long) getpid ()); db->filename, (unsigned long) getpid ());
snprintf (lock, lock_file_len, "%s.lock", db->filename); snprintf (lock, lock_file_len, "%s.lock", db->filename);
if (do_lock_file (file, lock, log) != 0) { if (do_lock_file (file, lock, log) != 0) {
db->locked = true; db->locked = true;
lock_count++; lock_count++;
free(file); err = 1;
free(lock);
return 1;
} }
free(file); cleanup_ENOMEM:
free(lock); if(file)
return 0; free(file);
if(lock)
free(lock);
return err;
} }
int commonio_lock (struct commonio_db *db) int commonio_lock (struct commonio_db *db)
{ {
#ifdef HAVE_LCKPWDF /*#ifdef HAVE_LCKPWDF*/ /* not compatible with prefix option*/
#if 0
/* /*
* only if the system libc has a real lckpwdf() - the one from * only if the system libc has a real lckpwdf() - the one from
* lockpw.c calls us and would cause infinite recursion! * lockpw.c calls us and would cause infinite recursion!

View File

@ -168,9 +168,8 @@ extern struct group *prefix_getgrnam(const char *name)
fclose(fg); fclose(fg);
return grp; return grp;
} }
else {
return getgrnam(name); return getgrnam(name);
}
} }
extern struct group *prefix_getgrgid(gid_t gid) extern struct group *prefix_getgrgid(gid_t gid)
@ -189,9 +188,8 @@ extern struct group *prefix_getgrgid(gid_t gid)
fclose(fg); fclose(fg);
return grp; return grp;
} }
else {
return getgrgid(gid); return getgrgid(gid);
}
} }
extern struct passwd *prefix_getpwuid(uid_t uid) extern struct passwd *prefix_getpwuid(uid_t uid)

View File

@ -835,7 +835,7 @@ static int remove_mailbox (void)
} }
len = strlen (prefix) + strlen (maildir) + strlen (user_name) + 2; len = strlen (prefix) + strlen (maildir) + strlen (user_name) + 2;
mailfile = alloca (len); mailfile = xmalloc (len);
if (prefix[0]) { if (prefix[0]) {
(void) snprintf (mailfile, len, "%s/%s/%s", (void) snprintf (mailfile, len, "%s/%s/%s",
@ -852,6 +852,7 @@ static int remove_mailbox (void)
fprintf (stderr, fprintf (stderr,
_("%s: %s mail spool (%s) not found\n"), _("%s: %s mail spool (%s) not found\n"),
Prog, user_name, mailfile); Prog, user_name, mailfile);
free(mailfile);
return 0; return 0;
} else { } else {
fprintf (stderr, fprintf (stderr,
@ -864,6 +865,7 @@ static int remove_mailbox (void)
user_name, (unsigned int) user_id, user_name, (unsigned int) user_id,
SHADOW_AUDIT_FAILURE); SHADOW_AUDIT_FAILURE);
#endif /* WITH_AUDIT */ #endif /* WITH_AUDIT */
free(mailfile);
return -1; return -1;
} }
} }
@ -892,6 +894,7 @@ static int remove_mailbox (void)
SHADOW_AUDIT_SUCCESS); SHADOW_AUDIT_SUCCESS);
} }
#endif /* WITH_AUDIT */ #endif /* WITH_AUDIT */
free(mailfile);
return errors; return errors;
} }
i = is_owner (user_id, mailfile); i = is_owner (user_id, mailfile);
@ -908,8 +911,10 @@ static int remove_mailbox (void)
user_name, (unsigned int) user_id, user_name, (unsigned int) user_id,
SHADOW_AUDIT_FAILURE); SHADOW_AUDIT_FAILURE);
#endif /* WITH_AUDIT */ #endif /* WITH_AUDIT */
free(mailfile);
return 1; return 1;
} else if (i == -1) { } else if (i == -1) {
free(mailfile);
return 0; /* mailbox doesn't exist */ return 0; /* mailbox doesn't exist */
} }
if (unlink (mailfile) != 0) { if (unlink (mailfile) != 0) {
@ -935,6 +940,7 @@ static int remove_mailbox (void)
SHADOW_AUDIT_SUCCESS); SHADOW_AUDIT_SUCCESS);
} }
#endif /* WITH_AUDIT */ #endif /* WITH_AUDIT */
free(mailfile);
return errors; return errors;
} }