queue-fix.c
author tomas@localhost
Thu, 01 Nov 2007 14:46:11 +0100
changeset 0 c045670f36e9
permissions -rw-r--r--
Imported queue-fix-1.4
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
     1
/*
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
     2
  queue-fix 1.4
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
     3
  by Eric Huss
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
     4
  e-huss@netmeridian.com
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
     5
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
     6
  reconstructs qmail's queue
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
     7
*/
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
     8
#include <stdio.h>
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
     9
#include <sys/stat.h>
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
    10
#include <pwd.h>
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
    11
#include <grp.h>
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
    12
#include "stralloc.h"
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
    13
#include "direntry.h"
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
    14
#include "fmt.h"
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
    15
#include "error.h"
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
    16
#include "subfd.h"
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
    17
#include "getln.h"
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
    18
#include "str.h"
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
    19
#include "open.h"
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
    20
#include "fifo.h"
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
    21
#include "scan.h"
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
    22
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
    23
/*change this to your qmail's conf-split value*/
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
    24
#define SPLIT_NUM 23
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
    25
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
    26
stralloc queue_dir     = {0}; /*the root queue dir with trailing slash*/
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
    27
stralloc check_dir     = {0}; /*the current directory being checked*/
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
    28
stralloc temp_filename = {0}; /*temporary used for checking individuals*/
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
    29
stralloc temp_dirname  = {0}; /*temporary used for checking individuals*/
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
    30
stralloc old_name      = {0}; /*used in rename*/
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
    31
stralloc new_name      = {0}; /*used in rename*/
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
    32
stralloc mess_dir      = {0}; /*used for renaming in mess dir*/
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
    33
stralloc query         = {0}; /*used in interactive query function*/
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
    34
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
    35
char name_num[FMT_ULONG];
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
    36
int flag_interactive=0;
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
    37
int flag_doit=1;
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
    38
int flag_dircreate=0;
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
    39
int flag_filecreate=0;
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
    40
int flag_permfix=0;
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
    41
int flag_namefix=0;
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
    42
int flag_unlink=0;
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
    43
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
    44
int qmailq_uid;
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
    45
int qmails_uid;
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
    46
int qmailr_uid;
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
    47
int qmail_gid;
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
    48
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
    49
void die_make(char * name)
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
    50
{
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
    51
printf("Failed to make %s\n"
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
    52
       "\nExiting...\n\n",name);
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
    53
exit(1);
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
    54
}
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
    55
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
    56
void die_user(char * user)
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
    57
{
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
    58
printf("Failed to determine the uid of %s\n"
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
    59
       "\nExiting...\n\n",user);
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
    60
exit(1);
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
    61
}
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
    62
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
    63
void die_group(char * group)
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
    64
{
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
    65
printf("Failed to determine the gid of %s\n"
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
    66
       "\nExiting...\n\n",group);
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
    67
exit(1);
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
    68
}
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
    69
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
    70
void die_args()
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
    71
{
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
    72
printf("Invalid arguments.\n"
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
    73
       "queue-fix [-i | -N] queue_dir\n"
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
    74
       "\nExiting...\n\n");
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
    75
exit(1);
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
    76
}
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
    77
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
    78
void die_check()
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
    79
{
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
    80
printf("Failed while checking directory structure.\n"
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
    81
       "Make sure the given queue exists and you have permission to access it.\n"
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
    82
       "\nExiting...\n\n");
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
    83
exit(1);
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
    84
}
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
    85
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
    86
void die_recon()
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
    87
{
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
    88
printf("Failed to reconstruct queue.\n"
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
    89
       "Make sure the queue exists and you have permission to modify it.\n"
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
    90
       "\nExiting...\n\n");
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
    91
exit(1);
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
    92
}
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
    93
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
    94
void die_nomem()
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
    95
{
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
    96
printf("Hmm..  Out of memory\n"
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
    97
       "\nExiting...\n\n");
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
    98
exit(1);
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
    99
}
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   100
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   101
void die_rerun()
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   102
{
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   103
printf(".tmp files exist in the queue.\n"
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   104
	   "queue-fix may have abnormally terminated in a previous run.\n"
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   105
	   "The queue must be manually cleaned of the .tmp files.\n"
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   106
	   "\nExiting...\n\n");
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   107
exit(1);
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   108
}
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   109
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   110
/*returns 1==yes, 0==no*/
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   111
int confirm()
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   112
{
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   113
int match;
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   114
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   115
	if(getln(subfdinsmall,&query,&match,'\n')) return 0;
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   116
	if(!match) return 0;
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   117
	if(query.s[0]=='y' || query.s[0]=='Y' || query.s[0]=='\n') return 1;
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   118
	return 0;
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   119
}
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   120
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   121
/*gid may be -1 on files for "unknown*/
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   122
int check_item(char * name,int uid,int gid,int perm,char type,int size)
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   123
{
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   124
struct stat st;
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   125
int fd;
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   126
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   127
	/*check for existence and proper credentials*/
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   128
	switch(type) {
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   129
	case 'd': /*directory*/
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   130
		if(stat(name,&st)) {
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   131
			if(errno!=error_noent) return -1;
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   132
			if(!flag_dircreate && flag_interactive) {
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   133
				printf("It looks like some directories don't exist, should I create them? (Y/n)\n");
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   134
				if(!confirm()) return -1;
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   135
				flag_dircreate = 1;
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   136
			}
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   137
			/*create it*/
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   138
			printf("Creating directory [%s]\n",name);
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   139
			if(flag_doit)	if(mkdir(name,perm)) die_make(name);
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   140
			printf("Changing permissions of [%s] to [%o]\n",name,perm);
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   141
			if(flag_doit)	if(chmod(name,perm)) die_make(name);
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   142
			printf("Changing ownership of [%s] to uid %u gid %u\n",name,uid,gid);
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   143
			if(flag_doit)	if(chown(name,uid,gid)) die_make(name);
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   144
			return 0;
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   145
		}
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   146
		/*check the values*/
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   147
		if(st.st_uid!=uid || st.st_gid!=gid) {
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   148
			if(!flag_permfix && flag_interactive) {
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   149
				printf("It looks like some permissions are wrong, should I fix them? (Y/n)\n");
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   150
				if(!confirm()) return -1;
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   151
				flag_permfix = 1;
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   152
			}
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   153
			/*fix it*/
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   154
			printf("Changing ownership of [%s] to uid %u gid %u\n",name,uid,gid);
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   155
			if(flag_doit)	if(chown(name,uid,gid)) die_make(name);
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   156
		}
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   157
		if((st.st_mode & 07777) != perm) {
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   158
			if(!flag_permfix && flag_interactive) {
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   159
				printf("It looks like some permissions are wrong, should I fix them? (Y/n)\n");
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   160
				if(!confirm()) return -1;
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   161
				flag_permfix = 1;
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   162
			}
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   163
			/*fix it*/
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   164
			printf("Changing permissions of [%s] to [%o]\n",name,perm);
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   165
			if(flag_doit)	if(chmod(name,perm)) die_make(name);
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   166
		}
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   167
		return 0;
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   168
	case 'f': /*regular file*/
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   169
		if(stat(name,&st)) return -1;
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   170
		/*check the values*/
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   171
		if(st.st_uid!=uid || (st.st_gid!=gid && gid!=-1)) {
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   172
			if(!flag_permfix && flag_interactive) {
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   173
				printf("It looks like some permissions are wrong, should I fix them? (Y/n)\n");
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   174
				if(!confirm()) return -1;
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   175
				flag_permfix = 1;
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   176
			}
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   177
			/*fix it*/
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   178
			printf("Changing ownership of [%s] to uid %u gid %u\n",name,uid,gid);
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   179
			if(flag_doit)	if(chown(name,uid,gid)) die_make(name);
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   180
		}
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   181
		if((st.st_mode & 07777) != perm) {
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   182
			if(!flag_permfix && flag_interactive) {
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   183
				printf("It looks like some permissions are wrong, should I fix them? (Y/n)\n");
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   184
				if(!confirm()) return -1;
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   185
				flag_permfix = 1;
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   186
			}
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   187
			/*fix it*/
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   188
			printf("Changing permissions of [%s] to [%o]\n",name,perm);
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   189
			if(flag_doit)	if(chmod(name,perm)) die_make(name);
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   190
		}
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   191
		return 0;
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   192
	case 'z': /*regular file with a size*/
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   193
		if(stat(name,&st)) {
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   194
			if(errno!=error_noent) return -1;
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   195
			if(!flag_filecreate && flag_interactive) {
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   196
				printf("It looks like some files don't exist, should I create them? (Y/n)\n");
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   197
				if(!confirm()) return -1;
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   198
				flag_filecreate = 1;
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   199
			}
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   200
			/*create it*/
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   201
			printf("Creating [%s] with size [%u]\n",name,size);
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   202
			if(flag_doit) {
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   203
				fd = open_trunc(name);
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   204
				if(fd==-1) die_make(name);
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   205
				while(size--) {
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   206
					if(write(fd,"",1)!=1) die_make(name);
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   207
				}
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   208
				close(fd);
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   209
			}
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   210
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   211
			printf("Changing permissions of [%s] to [%o]\n",name,perm);
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   212
			if(flag_doit)	if(chmod(name,perm)) die_make(name);
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   213
			printf("Changing ownership of [%s] to uid %u gid %u\n",name,uid,gid);
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   214
			if(flag_doit)	if(chown(name,uid,gid)) die_make(name);
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   215
			return 0;
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   216
		}
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   217
		/*check the values*/
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   218
		if(st.st_uid!=uid || (st.st_gid!=gid && gid!=-1)) {
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   219
			if(!flag_permfix && flag_interactive) {
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   220
				printf("It looks like some permissions are wrong, should I fix them? (Y/n)\n");
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   221
				if(!confirm()) return -1;
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   222
				flag_permfix = 1;
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   223
			}
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   224
			/*fix it*/
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   225
			printf("Changing ownership of [%s] to uid %u gid %u\n",name,uid,gid);
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   226
			if(flag_doit)	if(chown(name,uid,gid)) die_make(name);
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   227
		}
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   228
		if((st.st_mode & 07777) != perm) {
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   229
			if(!flag_permfix && flag_interactive) {
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   230
				printf("It looks like some permissions are wrong, should I fix them? (Y/n)\n");
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   231
				if(!confirm()) return -1;
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   232
				flag_permfix = 1;
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   233
			}
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   234
			/*fix it*/
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   235
			printf("Changing permissions of [%s] to [%o]\n",name,perm);
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   236
			if(flag_doit)	if(chmod(name,perm)) die_make(name);
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   237
		}
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   238
		if(st.st_size!=size) {
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   239
			printf("%s is not the right size.  I will not fix it, please investigate.\n",name);
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   240
		}
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   241
		return 0;
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   242
	case 'p': /*a named pipe*/
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   243
		if(stat(name,&st)) {
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   244
			if(errno!=error_noent) return -1;
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   245
			if(!flag_filecreate && flag_interactive) {
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   246
				printf("It looks like some files don't exist, should I create them? (Y/n)\n");
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   247
				if(!confirm()) return -1;
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   248
				flag_filecreate = 1;
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   249
			}
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   250
			/*create it*/
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   251
			printf("Creating fifo [%s]\n",name);
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   252
			if(flag_doit)	if(fifo_make(name,perm)) die_make(name);
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   253
			printf("Changing permissions of [%s] to [%o]\n",name,perm);
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   254
			if(flag_doit)	if(chmod(name,perm)) die_make(name);
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   255
			printf("Changing ownership of [%s] to uid %u gid %u\n",name,uid,gid);
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   256
			if(flag_doit)	if(chown(name,uid,gid)) die_make(name);
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   257
			return 0;
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   258
		}
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   259
		/*check the values*/
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   260
		if(st.st_uid!=uid || (st.st_gid!=gid && gid!=-1)) {
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   261
			if(!flag_permfix && flag_interactive) {
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   262
				printf("It looks like some permissions are wrong, should I fix them? (Y/n)\n");
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   263
				if(!confirm()) return -1;
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   264
				flag_permfix = 1;
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   265
			}
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   266
			/*fix it*/
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   267
			printf("Changing ownership of [%s] to uid %u gid %u\n",name,uid,gid);
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   268
			if(flag_doit)	if(chown(name,uid,gid)) die_make(name);
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   269
		}
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   270
		if((st.st_mode & 07777) != perm) {
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   271
			if(!flag_permfix && flag_interactive) {
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   272
				printf("It looks like some permissions are wrong, should I fix them? (Y/n)\n");
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   273
				if(!confirm()) return -1;
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   274
				flag_permfix = 1;
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   275
			}
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   276
			/*fix it*/
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   277
			printf("Changing permissions of [%s] to [%o]\n",name,perm);
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   278
			if(flag_doit)	if(chmod(name,perm)) die_make(name);
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   279
		}
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   280
		return 0;
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   281
	} /*end switch*/
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   282
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   283
	return 0;
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   284
}
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   285
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   286
int check_files(char * directory, int uid, int gid, int perm)
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   287
{
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   288
DIR * dir;
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   289
direntry * d;
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   290
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   291
	dir = opendir(directory);
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   292
	if(!dir) return -1;
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   293
	while((d = readdir(dir))) {
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   294
		if(d->d_name[0]=='.') continue;
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   295
		if(!stralloc_copys(&temp_filename,directory)) die_nomem();
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   296
		if(!stralloc_append(&temp_filename,"/")) die_nomem();
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   297
		if(!stralloc_cats(&temp_filename,d->d_name)) die_nomem();
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   298
		if(!stralloc_0(&temp_filename)) die_nomem();
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   299
		if(check_item(temp_filename.s,uid,gid,perm,'f',0))  { closedir(dir); return -1; }
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   300
	}
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   301
	closedir(dir);
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   302
	return 0;
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   303
}
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   304
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   305
void warn_files(char * directory)
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   306
{
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   307
DIR * dir;
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   308
direntry * d;
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   309
int found = 0;
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   310
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   311
	dir = opendir(directory);
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   312
	if(!dir) return;
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   313
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   314
	while((d = readdir(dir))) {
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   315
		if(d->d_name[0]=='.') continue;
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   316
		found = 1;
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   317
		break;
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   318
	}
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   319
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   320
	closedir(dir);
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   321
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   322
	if(found) {
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   323
		printf("Found files in %s that shouldn't be there.\n"
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   324
		       "I will not remove them. You should consider checking it out.\n\n",directory);
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   325
	}
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   326
}
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   327
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   328
int check_splits(char * directory, int dir_uid, int dir_gid, int dir_perm,
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   329
				 int file_gid, int file_perm)
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   330
{
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   331
DIR * dir;
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   332
direntry * d;
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   333
int i;
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   334
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   335
	for(i=0;i<SPLIT_NUM;i++) {
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   336
		name_num[fmt_ulong(name_num,i)]=0;
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   337
		if(!stralloc_copys(&temp_dirname,directory)) die_nomem();
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   338
		if(!stralloc_append(&temp_dirname,"/")) die_nomem();
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   339
		if(!stralloc_cats(&temp_dirname,name_num)) die_nomem();
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   340
		if(!stralloc_0(&temp_dirname)) die_nomem();
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   341
		/*check the split dir*/
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   342
		if(check_item(temp_dirname.s,dir_uid,dir_gid,dir_perm,'d',0)) return -1;
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   343
		/*check its contents*/
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   344
		dir = opendir(temp_dirname.s);
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   345
		if(!dir) return -1;
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   346
		while((d = readdir(dir))) {
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   347
			if(d->d_name[0]=='.') continue;
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   348
			if(!stralloc_copy(&temp_filename,&temp_dirname)) die_nomem();
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   349
			temp_filename.len--;		/*remove NUL*/
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   350
			if(!stralloc_append(&temp_filename,"/")) die_nomem();
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   351
			if(!stralloc_cats(&temp_filename,d->d_name)) die_nomem();
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   352
			if(!stralloc_0(&temp_filename)) die_nomem();
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   353
			if(check_item(temp_filename.s,dir_uid,file_gid,file_perm,'f',0)) { closedir(dir); return -1; }
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   354
		}
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   355
		closedir(dir);
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   356
	} /*end for*/
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   357
	return 0;
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   358
}
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   359
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   360
int rename_mess(char * dir, char * part, char * new_part, char * old_filename, char * new_filename)
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   361
{
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   362
struct stat st;
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   363
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   364
	if(flag_interactive && !flag_namefix) {
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   365
		printf("It looks like some files need to be renamed, should I rename them? (Y/n)\n");
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   366
		if(!confirm()) return -1;
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   367
		flag_namefix = 1;
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   368
	}
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   369
	/*prepare the old filename*/
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   370
	if(!stralloc_copy(&old_name,&queue_dir)) die_nomem();
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   371
	if(!stralloc_cats(&old_name,dir)) die_nomem();
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   372
	if(!stralloc_cats(&old_name,part)) die_nomem();
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   373
	if(!stralloc_append(&old_name,"/")) die_nomem();
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   374
	if(!stralloc_cats(&old_name,old_filename)) die_nomem();
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   375
	if(!stralloc_0(&old_name)) die_nomem();
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   376
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   377
	/*prepare the new filename*/
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   378
	if(!stralloc_copy(&new_name,&queue_dir)) die_nomem();
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   379
	if(!stralloc_cats(&new_name,dir)) die_nomem();
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   380
	if(!stralloc_cats(&new_name,new_part)) die_nomem();
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   381
	if(!stralloc_append(&new_name,"/")) die_nomem();
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   382
	if(!stralloc_cats(&new_name,new_filename)) die_nomem();
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   383
	if(!stralloc_0(&new_name)) die_nomem();
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   384
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   385
	/*check if destination exists*/
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   386
	if(stat(new_name.s,&st)==0) {
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   387
		/*it exists*/
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   388
		new_name.len--;			/*remove NUL*/
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   389
		/*append an extension to prevent name clash*/
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   390
		if(!stralloc_cats(&new_name,".tmp")) die_nomem();
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   391
		if(!stralloc_0(&new_name)) die_nomem();
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   392
		/*do a double check for collision*/
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   393
		if(stat(new_name.s,&st)==0) die_rerun();
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   394
	}
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   395
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   396
	printf("Renaming [%s] to [%s]\n",old_name.s,new_name.s);
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   397
	if(flag_doit) {
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   398
		if(rename(old_name.s,new_name.s)) {
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   399
			if(errno!=error_noent) {
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   400
				return -1;
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   401
			}
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   402
		}
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   403
	}
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   404
	return 0;
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   405
}
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   406
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   407
int fix_part(char * part,int part_num)
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   408
{
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   409
DIR * dir;
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   410
direntry * d;
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   411
struct stat st;
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   412
char inode[FMT_ULONG];
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   413
char new_part[FMT_ULONG];
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   414
int old_inode;
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   415
int correct_part_num;
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   416
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   417
	if(!stralloc_copy(&mess_dir,&queue_dir)) die_nomem();
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   418
	if(!stralloc_cats(&mess_dir,"mess/")) die_nomem();
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   419
	if(!stralloc_cats(&mess_dir,part)) die_nomem();
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   420
	if(!stralloc_0(&mess_dir)) die_nomem();
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   421
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   422
	dir = opendir(mess_dir.s);
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   423
	if(!dir) return -1;
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   424
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   425
	while((d = readdir(dir))) {
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   426
		if(d->d_name[0]=='.') continue;
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   427
		/*check from mess*/
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   428
		if(!stralloc_copy(&temp_filename,&mess_dir)) die_nomem();
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   429
		temp_filename.len--;			/*remove NUL*/
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   430
		if(!stralloc_append(&temp_filename,"/")) die_nomem();
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   431
		if(!stralloc_cats(&temp_filename,d->d_name)) die_nomem();
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   432
		if(!stralloc_0(&temp_filename)) die_nomem();
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   433
		if(stat(temp_filename.s,&st)) { closedir(dir); return -1; }
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   434
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   435
		/*check that filename==inode number*/
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   436
		/*check that inode%auto_split==part_num*/
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   437
		scan_ulong(d->d_name,&old_inode);
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   438
		correct_part_num = st.st_ino % SPLIT_NUM;
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   439
		if(st.st_ino != old_inode ||
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   440
			part_num!=correct_part_num) {
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   441
			/*rename*/
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   442
			inode[fmt_ulong(inode,st.st_ino)]=0;
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   443
			new_part[fmt_ulong(new_part,correct_part_num)]=0;
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   444
			if(rename_mess("mess/",part,new_part,d->d_name,inode)) { closedir(dir); return -1; }
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   445
			if(rename_mess("info/",part,new_part,d->d_name,inode)) { closedir(dir); return -1; }
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   446
			if(rename_mess("local/",part,new_part,d->d_name,inode)) { closedir(dir); return -1; }
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   447
			if(rename_mess("remote/",part,new_part,d->d_name,inode)) { closedir(dir); return -1; }
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   448
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   449
			if(rename_mess("intd","","",d->d_name,inode)) { closedir(dir); return -1; }
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   450
			if(rename_mess("todo","","",d->d_name,inode)) { closedir(dir); return -1; }
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   451
			if(rename_mess("bounce","","",d->d_name,inode)) { closedir(dir); return -1; }
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   452
		}
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   453
	}
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   454
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   455
	closedir(dir);
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   456
	return 0;
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   457
}
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   458
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   459
int clean_tmp(char * directory, char * part)
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   460
{
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   461
DIR * dir;
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   462
direntry * d;
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   463
struct stat st;
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   464
int length;
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   465
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   466
	if(!stralloc_copy(&mess_dir,&queue_dir)) die_nomem();
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   467
	if(!stralloc_cats(&mess_dir,directory)) die_nomem();
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   468
	if(!stralloc_cats(&mess_dir,part)) die_nomem();
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   469
	if(!stralloc_0(&mess_dir)) die_nomem();
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   470
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   471
	dir = opendir(mess_dir.s);
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   472
	if(!dir) return -1;
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   473
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   474
	while((d = readdir(dir))) {
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   475
		if(d->d_name[0]=='.') continue;
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   476
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   477
		/*check for tmp extension*/
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   478
		length = str_len(d->d_name);
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   479
		if(length>4) {
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   480
			if(str_equal(d->d_name+length-4,".tmp")) {
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   481
				/*remove the extension*/
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   482
				if(!stralloc_copys(&temp_filename,d->d_name)) die_nomem();
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   483
				temp_filename.len-=4;
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   484
				if(!stralloc_0(&temp_filename)) die_nomem();
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   485
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   486
				if(rename_mess(directory,part,part,d->d_name,temp_filename.s)) { closedir(dir); return -1; }
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   487
			}
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   488
		}
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   489
	}
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   490
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   491
	closedir(dir);
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   492
	return 0;
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   493
}
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   494
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   495
int fix_names()
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   496
{
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   497
int i;
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   498
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   499
	if(!stralloc_copy(&check_dir,&queue_dir)) die_nomem();
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   500
	if(!stralloc_cats(&check_dir,"mess")) die_nomem();
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   501
	if(!stralloc_0(&check_dir)) die_nomem();
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   502
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   503
	/*make the filenames match their inode*/
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   504
	for(i=0;i<SPLIT_NUM;i++) {
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   505
		name_num[fmt_ulong(name_num,i)]=0;
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   506
		if(fix_part(name_num,i)) return -1;
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   507
	}
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   508
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   509
	/*clean up any tmp files*/
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   510
	for(i=0;i<SPLIT_NUM;i++) {
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   511
		name_num[fmt_ulong(name_num,i)]=0;
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   512
		if(clean_tmp("mess/",name_num)) return -1;
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   513
		if(clean_tmp("info/",name_num)) return -1;
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   514
		if(clean_tmp("local/",name_num)) return -1;
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   515
		if(clean_tmp("remote/",name_num)) return -1;
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   516
	}
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   517
	if(clean_tmp("intd","")) return -1;
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   518
	if(clean_tmp("todo","")) return -1;
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   519
	if(clean_tmp("bounce","")) return -1;
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   520
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   521
	return 0;
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   522
}
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   523
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   524
int check_dirs()
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   525
{
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   526
	/*check root existence*/
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   527
	if(!stralloc_copy(&check_dir,&queue_dir)) die_nomem();
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   528
	if(!stralloc_0(&check_dir)) die_nomem();
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   529
	if(check_item(check_dir.s,qmailq_uid,qmail_gid,0750,'d',0)) return -1;
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   530
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   531
	/*check the big 4*/
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   532
	if(!stralloc_copy(&check_dir,&queue_dir)) die_nomem();
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   533
 	if(!stralloc_cats(&check_dir,"info")) die_nomem();
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   534
	if(!stralloc_0(&check_dir)) die_nomem();
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   535
	if(check_item(check_dir.s,qmails_uid,qmail_gid,0700,'d',0)) return -1;
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   536
	if(check_splits(check_dir.s,qmails_uid,qmail_gid,0700,qmail_gid,0600)) return -1;
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   537
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   538
	if(!stralloc_copy(&check_dir,&queue_dir)) die_nomem();
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   539
	if(!stralloc_cats(&check_dir,"mess")) die_nomem();
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   540
	if(!stralloc_0(&check_dir)) die_nomem();
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   541
	if(check_item(check_dir.s,qmailq_uid,qmail_gid,0750,'d',0)) return -1;
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   542
	if(check_splits(check_dir.s,qmailq_uid,qmail_gid,0750,-1,0644)) return -1;
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   543
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   544
	if(!stralloc_copy(&check_dir,&queue_dir)) die_nomem();
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   545
	if(!stralloc_cats(&check_dir,"remote")) die_nomem();
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   546
	if(!stralloc_0(&check_dir)) die_nomem();
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   547
	if(check_item(check_dir.s,qmails_uid,qmail_gid,0700,'d',0)) return -1;
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   548
	if(check_splits(check_dir.s,qmails_uid,qmail_gid,0700,qmail_gid,0600)) return -1;
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   549
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   550
	if(!stralloc_copy(&check_dir,&queue_dir)) die_nomem();
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   551
	if(!stralloc_cats(&check_dir,"local")) die_nomem();
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   552
	if(!stralloc_0(&check_dir)) die_nomem();
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   553
	if(check_item(check_dir.s,qmails_uid,qmail_gid,0700,'d',0)) return -1;
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   554
	if(check_splits(check_dir.s,qmails_uid,qmail_gid,0700,qmail_gid,0600)) return -1;
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   555
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   556
	/*check the others*/
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   557
	if(!stralloc_copy(&check_dir,&queue_dir)) die_nomem();
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   558
	if(!stralloc_cats(&check_dir,"todo")) die_nomem();
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   559
	if(!stralloc_0(&check_dir)) die_nomem();
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   560
	if(check_item(check_dir.s,qmailq_uid,qmail_gid,0750,'d',0)) return -1;
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   561
	if(check_files(check_dir.s,qmailq_uid,-1,0644)) return -1;
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   562
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   563
	if(!stralloc_copy(&check_dir,&queue_dir)) die_nomem();
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   564
	if(!stralloc_cats(&check_dir,"intd")) die_nomem();
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   565
	if(!stralloc_0(&check_dir)) die_nomem();
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   566
	if(check_item(check_dir.s,qmailq_uid,qmail_gid,0700,'d',0)) return -1;
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   567
	if(check_files(check_dir.s,qmailq_uid,-1,0644)) return -1;
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   568
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   569
	if(!stralloc_copy(&check_dir,&queue_dir)) die_nomem();
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   570
	if(!stralloc_cats(&check_dir,"bounce")) die_nomem();
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   571
	if(!stralloc_0(&check_dir)) die_nomem();
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   572
	if(check_item(check_dir.s,qmails_uid,qmail_gid,0700,'d',0)) return -1;
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   573
	if(check_files(check_dir.s,qmails_uid,qmail_gid,0600)) return -1;
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   574
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   575
	if(!stralloc_copy(&check_dir,&queue_dir)) die_nomem();
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   576
	if(!stralloc_cats(&check_dir,"pid")) die_nomem();
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   577
	if(!stralloc_0(&check_dir)) die_nomem();
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   578
	if(check_item(check_dir.s,qmailq_uid,qmail_gid,0700,'d',0)) return -1;
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   579
	warn_files(check_dir.s);
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   580
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   581
	/*lock has special files that must exist*/
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   582
	if(!stralloc_copy(&check_dir,&queue_dir)) die_nomem();
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   583
	if(!stralloc_cats(&check_dir,"lock")) die_nomem();
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   584
	if(!stralloc_0(&check_dir)) die_nomem();
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   585
	if(check_item(check_dir.s,qmailq_uid,qmail_gid,0750,'d',0)) return -1;
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   586
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   587
	if(!stralloc_copy(&check_dir,&queue_dir)) die_nomem();
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   588
	if(!stralloc_cats(&check_dir,"lock/sendmutex")) die_nomem();
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   589
	if(!stralloc_0(&check_dir)) die_nomem();
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   590
	if(check_item(check_dir.s,qmails_uid,qmail_gid,0600,'z',0)) return -1;
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   591
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   592
	if(!stralloc_copy(&check_dir,&queue_dir)) die_nomem();
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   593
	if(!stralloc_cats(&check_dir,"lock/tcpto")) die_nomem();
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   594
	if(!stralloc_0(&check_dir)) die_nomem();
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   595
	if(check_item(check_dir.s,qmailr_uid,qmail_gid,0644,'z',1024)) return -1;
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   596
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   597
	if(!stralloc_copy(&check_dir,&queue_dir)) die_nomem();
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   598
	if(!stralloc_cats(&check_dir,"lock/trigger")) die_nomem();
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   599
	if(!stralloc_0(&check_dir)) die_nomem();
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   600
	if(check_item(check_dir.s,qmails_uid,qmail_gid,0622,'p',0)) return -1;
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   601
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   602
	return 0;
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   603
}
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   604
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   605
int check_strays(char * directory)
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   606
{
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   607
DIR * dir;
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   608
direntry * d;
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   609
struct stat st;
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   610
int inode;
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   611
int part;
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   612
char new_part[FMT_ULONG];
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   613
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   614
	dir = opendir(directory);
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   615
	if(!dir) return -1;
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   616
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   617
	while((d = readdir(dir))) {
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   618
		if(d->d_name[0]=='.') continue;
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   619
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   620
		scan_ulong(d->d_name,&inode);
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   621
		part = inode % SPLIT_NUM;
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   622
		new_part[fmt_ulong(new_part,part)]=0;
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   623
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   624
		/*check for corresponding entry in mess dir*/
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   625
		if(!stralloc_copy(&mess_dir,&queue_dir)) die_nomem();
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   626
		if(!stralloc_cats(&mess_dir,"mess/")) die_nomem();
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   627
		if(!stralloc_cats(&mess_dir,new_part)) die_nomem();
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   628
		if(!stralloc_append(&mess_dir,"/")) die_nomem();
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   629
		if(!stralloc_cats(&mess_dir,d->d_name)) die_nomem();
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   630
		if(!stralloc_0(&mess_dir)) die_nomem();
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   631
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   632
		if(stat(mess_dir.s,&st)) {
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   633
			/*failed to find in mess*/
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   634
			if(flag_interactive && !flag_unlink) {
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   635
				printf("There are some stray files in %s\n",directory);
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   636
				printf("Should I remove them? (Y/n)\n");
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   637
				if(!confirm()) {
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   638
					closedir(dir);
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   639
					return -1;
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   640
				}
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   641
				flag_unlink = 1;
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   642
			}
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   643
			if(!stralloc_copys(&temp_filename,directory)) die_nomem();
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   644
			if(!stralloc_append(&temp_filename,"/")) die_nomem();
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   645
			if(!stralloc_cats(&temp_filename,d->d_name)) die_nomem();
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   646
			if(!stralloc_0(&temp_filename)) die_nomem();
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   647
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   648
			printf("Unlinking [%s]\n",temp_filename.s);
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   649
			if(flag_doit)	if(unlink(temp_filename.s)==-1) { closedir(dir); return -1; }
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   650
		}
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   651
	}
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   652
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   653
	closedir(dir);
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   654
	return 0;
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   655
}
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   656
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   657
int check_stray_parts()
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   658
{
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   659
int i;
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   660
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   661
	for(i=0;i<SPLIT_NUM;i++) {
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   662
		name_num[fmt_ulong(name_num,i)]=0;
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   663
		if(!stralloc_copy(&temp_dirname,&check_dir)) die_nomem();
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   664
		if(!stralloc_append(&temp_dirname,"/")) die_nomem();
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   665
		if(!stralloc_cats(&temp_dirname,name_num)) die_nomem();
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   666
		if(!stralloc_0(&temp_dirname)) die_nomem();
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   667
		/*check this dir for strays*/
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   668
		if(check_strays(temp_dirname.s)) return -1;
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   669
	}
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   670
	return 0;
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   671
}
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   672
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   673
int find_strays()
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   674
{
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   675
	if(!stralloc_copy(&check_dir,&queue_dir)) die_nomem();
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   676
 	if(!stralloc_cats(&check_dir,"info")) die_nomem();
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   677
	if(check_stray_parts()) return -1;
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   678
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   679
	if(!stralloc_copy(&check_dir,&queue_dir)) die_nomem();
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   680
 	if(!stralloc_cats(&check_dir,"local")) die_nomem();
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   681
	if(check_stray_parts()) return -1;
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   682
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   683
	if(!stralloc_copy(&check_dir,&queue_dir)) die_nomem();
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   684
 	if(!stralloc_cats(&check_dir,"remote")) die_nomem();
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   685
	if(check_stray_parts()) return -1;
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   686
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   687
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   688
	if(!stralloc_copy(&check_dir,&queue_dir)) die_nomem();
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   689
 	if(!stralloc_cats(&check_dir,"todo")) die_nomem();
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   690
	if(!stralloc_0(&check_dir)) die_nomem();
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   691
	if(check_strays(check_dir.s)) return -1;
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   692
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   693
	if(!stralloc_copy(&check_dir,&queue_dir)) die_nomem();
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   694
 	if(!stralloc_cats(&check_dir,"intd")) die_nomem();
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   695
	if(!stralloc_0(&check_dir)) die_nomem();
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   696
	if(check_strays(check_dir.s)) return -1;
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   697
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   698
	if(!stralloc_copy(&check_dir,&queue_dir)) die_nomem();
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   699
 	if(!stralloc_cats(&check_dir,"bounce")) die_nomem();
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   700
	if(!stralloc_0(&check_dir)) die_nomem();
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   701
	if(check_strays(check_dir.s)) return -1;
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   702
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   703
	return 0;
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   704
}
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   705
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   706
int main(int argc, char ** argv)
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   707
{
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   708
struct passwd * pw;
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   709
struct group * gr;
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   710
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   711
	if(argc<2 || !argv[1] || !argv[1][0]) {
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   712
		die_args();
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   713
	}
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   714
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   715
	if(str_diff(argv[1],"-i")==0) {
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   716
		flag_interactive = 1;
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   717
		argv++;
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   718
		if(!argv[1] || !argv[1][0]) die_args();
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   719
	} else if(str_diff(argv[1],"-N")==0) {
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   720
		printf("Running in test mode, no changes will be made.\n");
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   721
		flag_doit = 0;
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   722
		argv++;
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   723
		if(!argv[1] || !argv[1][0]) die_args();
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   724
	}
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   725
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   726
	if(!stralloc_copys(&queue_dir,argv[1])) die_nomem();
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   727
	if(queue_dir.s[queue_dir.len-1]!='/') {
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   728
		if(!stralloc_append(&queue_dir,"/")) die_nomem();
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   729
	}
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   730
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   731
	/*prepare the uid and gid*/
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   732
	pw = getpwnam("qmailq");
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   733
	if(!pw) die_user("qmailq");
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   734
	qmailq_uid = pw->pw_uid;
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   735
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   736
	pw = getpwnam("qmails");
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   737
	if(!pw) die_user("qmails");
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   738
	qmails_uid = pw->pw_uid;
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   739
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   740
	pw = getpwnam("qmailr");
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   741
	if(!pw) die_user("qmailr");
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   742
	qmailr_uid = pw->pw_uid;
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   743
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   744
	gr = getgrnam("qmail");
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   745
	if(!gr) die_group("qmail");
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   746
	qmail_gid = gr->gr_gid;
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   747
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   748
	/*check that all the proper directories exist with proper credentials*/
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   749
	if(check_dirs()) die_check();
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   750
	/*rename inode filenames*/
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   751
	if(fix_names()) die_check();
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   752
	/*check for stray files*/
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   753
	if(find_strays()) die_check();
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   754
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   755
	printf("queue-fix finished...\n");
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   756
	return 0;
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
   757
}