int do_dangling_aliases(void) { int count = 0; int result = 0; GList *aliases = NULL; if (no_to_all) qprintf("\nCounting aliases with nonexistent delivery userid's...\n"); if (yes_to_all) qprintf("\nRemoving aliases with nonexistent delivery userid's...\n"); aliases = auth_get_known_aliases(); aliases = g_list_dedup(aliases, (GCompareFunc)strcmp, TRUE); aliases = g_list_first(aliases); while (aliases) { char deliver_to[21]; GList *dangling = find_dangling_aliases(aliases->data); dangling = g_list_first(dangling); while (dangling) { count++; g_snprintf(deliver_to, 21, "%llu", *(u64_t *)dangling->data); qverbosef("Dangling alias [%s] delivers to nonexistent user [%s]\n", (char *)aliases->data, deliver_to); if (yes_to_all) { if (auth_removealias_ext(aliases->data, deliver_to) < 0) { qerrorf("Error: could not remove alias [%s] deliver to [%s] \n", (char *)aliases->data, deliver_to); serious_errors = 1; result = -1; } } if (!g_list_next(dangling)) break; dangling = g_list_next(dangling); } g_list_destroy(g_list_first(dangling)); if (! g_list_next(aliases)) break; aliases = g_list_next(aliases); } g_list_destroy(g_list_first(aliases)); if (count > 0) { qerrorf("Ok. Found [%d] dangling aliases.\n", count); has_errors = 1; } else { qprintf("Ok. Found [%d] dangling aliases.\n", count); } return result; }
int do_header_cache(void) { time_t start, stop; GList *lost = NULL; if (do_rfc_size()) { serious_errors = 1; return -1; } if (do_envelope()) { serious_errors = 1; return -1; } if (no_to_all) qprintf("\nChecking DBMAIL for cached header values...\n"); if (yes_to_all) qprintf("\nRepairing DBMAIL for cached header values...\n"); time(&start); if (db_icheck_headercache(&lost) < 0) { qerrorf("Failed. An error occured. Please check log.\n"); serious_errors = 1; return -1; } if (g_list_length(lost) > 0) { qerrorf("Ok. Found [%d] un-cached physmessages.\n", g_list_length(lost)); has_errors = 1; } else { qprintf("Ok. Found [%d] un-cached physmessages.\n", g_list_length(lost)); } if (yes_to_all) { if (db_set_headercache(lost) < 0) { qerrorf("Error caching the header values "); serious_errors = 1; } } g_list_free(lost); time(&stop); qverbosef("--- checking cached headervalues took %g seconds\n", difftime(stop, start)); return 0; }
static int do_envelope(void) { time_t start, stop; GList *lost = NULL; if (no_to_all) { qprintf("\nChecking DBMAIL for cached envelopes...\n"); } if (yes_to_all) { qprintf("\nRepairing DBMAIL for cached envelopes...\n"); } time(&start); if (db_icheck_envelope(&lost) < 0) { qerrorf("Failed. An error occured. Please check log.\n"); serious_errors = 1; return -1; } if (g_list_length(lost) > 0) { qerrorf("Ok. Found [%d] missing envelope values.\n", g_list_length(lost)); has_errors = 1; } else { qprintf("Ok. Found [%d] missing envelope values.\n", g_list_length(lost)); } if (yes_to_all) { if (db_set_envelope(lost) < 0) { qerrorf("Error setting the envelope cache"); has_errors = 1; } } g_list_free(g_list_first(lost)); time(&stop); qverbosef("--- checking envelope cache took %g seconds\n", difftime(stop, start)); return 0; }
static int do_rfc_size(void) { time_t start, stop; GList *lost = NULL; if (no_to_all) { qprintf("\nChecking DBMAIL for rfcsize field...\n"); } if (yes_to_all) { qprintf("\nRepairing DBMAIL for rfcsize field...\n"); } time(&start); if (db_icheck_rfcsize(&lost) < 0) { qerrorf("Failed. An error occured. Please check log.\n"); serious_errors = 1; return -1; } if (g_list_length(lost) > 0) { qerrorf("Ok. Found [%d] missing rfcsize values.\n", g_list_length(lost)); has_errors = 1; } else { qprintf("Ok. Found [%d] missing rfcsize values.\n", g_list_length(lost)); } if (yes_to_all) { if (db_update_rfcsize(lost) < 0) { qerrorf("Error setting the rfcsize values"); has_errors = 1; } } g_list_destroy(lost); time(&stop); qverbosef("--- checking rfcsize field took %g seconds\n", difftime(stop, start)); return 0; }
int main(int argc, char *argv[]) { int check_integrity = 0; int check_iplog = 0, check_replycache = 0; char *timespec_iplog = NULL, *timespec_replycache = NULL; int vacuum_db = 0, purge_deleted = 0, set_deleted = 0, dangling_aliases = 0, rehash = 0; int show_help = 0; int do_nothing = 1; int is_header = 0; int migrate = 0, migrate_limit = 10000; static struct option long_options[] = { { "rehash", 0, 0, 0 }, { 0, 0, 0, 0 } }; int opt_index = 0; int opt; g_mime_init(0); openlog(PNAME, LOG_PID, LOG_MAIL); setvbuf(stdout, 0, _IONBF, 0); /* get options */ opterr = 0; /* suppress error message from getopt() */ while ((opt = getopt_long(argc, argv, "-acbtl:r:pudsMm:" "i" "f:qnyvVh", long_options, &opt_index)) != -1) { /* The initial "-" of optstring allows unaccompanied * options and reports them as the optarg to opt 1 (not '1') */ switch (opt) { case 0: do_nothing = 0; if (strcmp(long_options[opt_index].name,"rehash")==0) rehash = 1; break; case 'a': /* This list should be kept up to date. */ vacuum_db = 1; purge_deleted = 1; set_deleted = 1; dangling_aliases = 1; check_integrity = 1; is_header = 1; do_nothing = 0; break; case 'c': vacuum_db = 1; do_nothing = 0; break; case 'b': is_header = 1; do_nothing = 0; break; case 'p': purge_deleted = 1; do_nothing = 0; break; case 'd': set_deleted = 1; do_nothing = 0; break; case 's': dangling_aliases = 1; do_nothing = 0; break; case 't': check_integrity = 1; do_nothing = 0; break; case 'u': /* deprecated */ break; case 'l': check_iplog = 1; do_nothing = 0; if (optarg) timespec_iplog = g_strdup(optarg); break; case 'r': check_replycache = 1; do_nothing = 0; if (optarg) timespec_replycache = g_strdup(optarg); break; case 'M': migrate = 1; do_nothing = 0; break; case 'm': if (optarg) migrate_limit = atoi(optarg); break; case 'i': qerrorf("Interactive console is not supported in this release.\n"); return 1; /* Common options */ case 'h': show_help = 1; do_nothing = 0; break; case 'n': no_to_all = 1; break; case 'y': yes_to_all = 1; break; case 'q': /* If we get q twice, be really quiet! */ if (quiet) reallyquiet = 1; if (!verbose) quiet = 1; break; case 'f': if (optarg && strlen(optarg) > 0) configFile = optarg; else { qerrorf("dbmail-util: -f requires a filename\n\n" ); return 1; } break; case 'v': verbose = 1; break; case 'V': PRINTF_THIS_IS_DBMAIL; return 1; default: printf("unrecognized option [%c]\n", optopt); show_help = 1; break; } } if (do_nothing || show_help || (no_to_all && yes_to_all)) { do_showhelp(); return 1; } /* Don't make any changes unless specifically authorized. */ if (!yes_to_all) { qprintf("Choosing dry-run mode. No changes will be made at this time.\n"); no_to_all = 1; } config_read(configFile); SetTraceLevel("DBMAIL"); GetDBParams(); qverbosef("Opening connection to database... \n"); if (db_connect() != 0) { qerrorf("Failed. An error occured. Please check log.\n"); return -1; } qverbosef("Opening connection to authentication... \n"); if (auth_connect() != 0) { qerrorf("Failed. An error occured. Please check log.\n"); return -1; } qverbosef("Ok. Connected.\n"); if (check_integrity) do_check_integrity(); if (purge_deleted) do_purge_deleted(); if (is_header) do_header_cache(); if (set_deleted) do_set_deleted(); if (dangling_aliases) do_dangling_aliases(); if (check_iplog) do_check_iplog(timespec_iplog); if (check_replycache) do_check_replycache(timespec_replycache); if (vacuum_db) do_vacuum_db(); if (rehash) do_rehash(); if (migrate) do_migrate(migrate_limit); if (!has_errors && !serious_errors) { qprintf("\nMaintenance done. No errors found.\n"); } else { qerrorf("\nMaintenance done. Errors were found"); if (serious_errors) { qerrorf(" but not fixed due to failures.\n"); qerrorf("Please check the logs for further details, " "turning up the trace level as needed.\n"); // Indicate that something went really wrong has_errors = 3; } else if (no_to_all) { qerrorf(" but not fixed.\n"); qerrorf("Run again with the '-y' option to " "repair the errors.\n"); // Indicate that the program should be run with -y has_errors = 2; } else if (yes_to_all) { qerrorf(" and fixed.\n"); qerrorf("We suggest running dbmail-util again to " "confirm that all errors were repaired.\n"); // Indicate that the program should be run again has_errors = 1; } } auth_disconnect(); db_disconnect(); config_free(); g_mime_shutdown(); return has_errors; }
int do_check_integrity(void) { time_t start, stop; GList *lost = NULL; const char *action; gboolean cleanup; long count = 0; if (yes_to_all) { action = "Repairing"; cleanup = TRUE; } else { action = "Checking"; cleanup = FALSE; } qprintf("\n%s DBMAIL message integrity...\n", action); /* This is what we do: 3. Check for loose physmessages 4. Check for loose partlists 5. Check for loose mimeparts */ /* part 3 */ start = stop; qprintf("\n%s DBMAIL physmessage integrity...\n", action); if ((count = db_icheck_physmessages(cleanup)) < 0) { qerrorf("Failed. An error occurred. Please check log.\n"); serious_errors = 1; return -1; } if (count > 0) { qerrorf("Ok. Found [%ld] unconnected physmessages.\n", count); if (cleanup) { qerrorf("Ok. Orphaned physmessages deleted.\n"); } } else { qprintf("Ok. Found [%ld] unconnected physmessages.\n", count); } time(&stop); qverbosef("--- %s unconnected physmessages took %g seconds\n", action, difftime(stop, start)); /* end part 3 */ /* part 4 */ start = stop; qprintf("\n%s DBMAIL partlists integrity...\n", action); if ((count = db_icheck_partlists(cleanup)) < 0) { qerrorf("Failed. An error occurred. Please check log.\n"); serious_errors = 1; return -1; } if (count > 0) { qerrorf("Ok. Found [%ld] unconnected partlists.\n", count); if (cleanup) { qerrorf("Ok. Orphaned partlists deleted.\n"); } } else { qprintf("Ok. Found [%ld] unconnected partlists.\n", count); } time(&stop); qverbosef("--- %s unconnected partlists took %g seconds\n", action, difftime(stop, start)); /* end part 4 */ /* part 5 */ start = stop; qprintf("\n%s DBMAIL mimeparts integrity...\n", action); if ((count = db_icheck_mimeparts(cleanup)) < 0) { qerrorf("Failed. An error occurred. Please check log.\n"); serious_errors = 1; return -1; } if (count > 0) { qerrorf("Ok. Found [%ld] unconnected mimeparts.\n", count); if (cleanup) { qerrorf("Ok. Orphaned mimeparts deleted.\n"); } } else { qprintf("Ok. Found [%ld] unconnected mimeparts.\n", count); } time(&stop); qverbosef("--- %s unconnected mimeparts took %g seconds\n", action, difftime(stop, start)); /* end part 5 */ g_list_destroy(lost); lost = NULL; time(&stop); qverbosef("--- %s block integrity took %g seconds\n", action, difftime(stop, start)); /* end part 6 */ return 0; }
int do_check_integrity(void) { time_t start, stop; GList *lost = NULL; const char *action; gboolean cleanup; long count = 0; if (yes_to_all) { action = "Repairing"; cleanup = TRUE; } else { action = "Checking"; cleanup = FALSE; } qprintf("\n%s DBMAIL message integrity...\n", action); /* This is what we do: 3. Check for loose physmessages 4. Check for loose partlists 5. Check for loose mimeparts 6. Check for loose headernames 7. Check for loose headervalues */ /* part 3 */ time(&start); qprintf("\n%s DBMAIL physmessage integrity...\n", action); if ((count = db_icheck_physmessages(cleanup)) < 0) { qerrorf("Failed. An error occurred. Please check log.\n"); serious_errors = 1; return -1; } if (count > 0) { qerrorf("Ok. Found [%ld] unconnected physmessages.\n", count); if (cleanup) { qerrorf("Ok. Orphaned physmessages deleted.\n"); } } else { qprintf("Ok. Found [%ld] unconnected physmessages.\n", count); } time(&stop); qverbosef("--- %s unconnected physmessages took %g seconds\n", action, difftime(stop, start)); /* end part 3 */ /* part 4 */ start = stop; qprintf("\n%s DBMAIL partlists integrity...\n", action); if ((count = db_icheck_partlists(cleanup)) < 0) { qerrorf("Failed. An error occurred. Please check log.\n"); serious_errors = 1; return -1; } if (count > 0) { qerrorf("Ok. Found [%ld] unconnected partlists.\n", count); if (cleanup) { qerrorf("Ok. Orphaned partlists deleted.\n"); } } else { qprintf("Ok. Found [%ld] unconnected partlists.\n", count); } time(&stop); qverbosef("--- %s unconnected partlists took %g seconds\n", action, difftime(stop, start)); /* end part 4 */ /* part 5 */ start = stop; qprintf("\n%s DBMAIL mimeparts integrity...\n", action); if ((count = db_icheck_mimeparts(cleanup)) < 0) { qerrorf("Failed. An error occurred. Please check log.\n"); serious_errors = 1; return -1; } if (count > 0) { qerrorf("Ok. Found [%ld] unconnected mimeparts.\n", count); if (cleanup) { qerrorf("Ok. Orphaned mimeparts deleted.\n"); } } else { qprintf("Ok. Found [%ld] unconnected mimeparts.\n", count); } time(&stop); qverbosef("--- %s unconnected mimeparts took %g seconds\n", action, difftime(stop, start)); /* end part 5 */ /* part 6 */ Field_T config; bool cache_readonly = false; config_get_value("header_cache_readonly", "DBMAIL", config); if (strlen(config)) { if (MATCH(config, "true") || MATCH(config, "yes")) { cache_readonly = true; } } if (! cache_readonly) { start = stop; qprintf("\n%s DBMAIL headernames integrity...\n", action); if ((count = db_icheck_headernames(cleanup)) < 0) { qerrorf("Failed. An error occurred. Please check log.\n"); serious_errors = 1; return -1; } qprintf("Ok. Found [%ld] unconnected headernames.\n", count); if (count > 0 && cleanup) { qerrorf("Ok. Orphaned headernames deleted.\n"); } time(&stop); qverbosef("--- %s unconnected headernames took %g seconds\n", action, difftime(stop, start)); } /* end part 6 */ /* part 7 */ start = stop; qprintf("\n%s DBMAIL headervalues integrity...\n", action); if ((count = db_icheck_headervalues(cleanup)) < 0) { qerrorf("Failed. An error occurred. Please check log.\n"); serious_errors = 1; return -1; } qprintf("Ok. Found [%ld] unconnected headervalues.\n", count); if (count > 0 && cleanup) { qerrorf("Ok. Orphaned headervalues deleted.\n"); } time(&stop); qverbosef("--- %s unconnected headervalues took %g seconds\n", action, difftime(stop, start)); /* end part 7 */ g_list_destroy(lost); lost = NULL; time(&stop); qverbosef("--- %s block integrity took %g seconds\n", action, difftime(stop, start)); return 0; }