static int getmailboxexists(void *sc, const char *extname) { script_data_t *sd = (script_data_t *)sc; char *intname = mboxname_from_external(extname, sd->ns, mbname_userid(sd->mbname)); int r = mboxlist_lookup(intname, NULL, NULL); free(intname); return r ? 0 : 1; /* 0 => exists */ }
struct backend *proxy_findinboxserver(const char *userid) { mbentry_t *mbentry = NULL; struct backend *s = NULL; char *inbox = mboxname_user_mbox(userid, NULL); int r = mboxlist_lookup(inbox, &mbentry, NULL); free(inbox); if (r) return NULL; if (mbentry->mbtype & MBTYPE_REMOTE) { s = proxy_findserver(mbentry->server, &imap_protocol, proxy_userid, &backend_cached, &backend_current, &backend_inbox, imapd_in); } mboxlist_entry_free(&mbentry); return s; }
static int user_deleteacl(char *name, int matchlen, int maycreate, void* rock) { /* deleting all references to the user is too slow right now */ char *ident = (char *) rock; int r; char *acl; char *rights, *nextid; char *origacl, *aclalloc; r = mboxlist_lookup(name, &origacl, NULL); /* setacl re-calls mboxlist_lookup and will stomp on us */ aclalloc = acl = xstrdup(origacl); while (!r && acl) { rights = strchr(acl, '\t'); if (!rights) break; *rights++ = '\0'; nextid = strchr(rights, '\t'); if (!nextid) break; *nextid++ = '\0'; if (!strcmp(acl, ident)) { /* delete ACL for ident */ if (!r) mboxlist_setacl(name, ident, (char *)0, 1, ident, NULL); } acl = nextid; } free(aclalloc); return 0; }
struct backend *proxy_findinboxserver(const char *userid) { char inbox[MAX_MAILBOX_BUFFER]; struct mboxlist_entry *mbentry = NULL; struct backend *s = NULL; int r; r = (*imapd_namespace.mboxname_tointernal)(&imapd_namespace, "INBOX", userid, inbox); if (r) return NULL; r = mboxlist_lookup(inbox, &mbentry, NULL); if (r) return NULL; if (mbentry->mbtype & MBTYPE_REMOTE) { s = proxy_findserver(mbentry->server, &imap_protocol, proxy_userid, &backend_cached, &backend_current, &backend_inbox, imapd_in); } mboxlist_entry_free(&mbentry); return s; }
/* This is called once for each mailbox we're told to index. */ static int index_one(const char *name, int blocking) { mbentry_t *mbentry = NULL; struct mailbox *mailbox = NULL; int r; int flags = 0; if (incremental_mode) flags |= SEARCH_UPDATE_INCREMENTAL; /* Convert internal name to external */ char *extname = mboxname_to_external(name, &squat_namespace, NULL); /* Skip remote mailboxes */ r = mboxlist_lookup(name, &mbentry, NULL); if (r) { if (verbose) { printf("error looking up %s: %s\n", extname, error_message(r)); } syslog(LOG_INFO, "error looking up %s: %s\n", extname, error_message(r)); free(extname); return r; } if (mbentry->mbtype & MBTYPE_REMOTE) { mboxlist_entry_free(&mbentry); free(extname); return 0; } mboxlist_entry_free(&mbentry); /* make sure the mailbox (or an ancestor) has /vendor/cmu/cyrus-imapd/squat set to "true" */ if (annotation_flag) { char buf[MAX_MAILBOX_BUFFER] = "", *p; struct buf attrib = BUF_INITIALIZER; int domainlen = 0; if (config_virtdomains && (p = strchr(name, '!'))) domainlen = p - name + 1; strlcpy(buf, name, sizeof(buf)); /* since mailboxes inherit /vendor/cmu/cyrus-imapd/squat, we need to iterate all the way up to "" (server entry) */ while (1) { r = annotatemore_lookup(buf, IMAP_ANNOT_NS "squat", "", &attrib); if (r || /* error */ attrib.s || /* found an entry */ !buf[0]) { /* done recursing */ break; } p = strrchr(buf, '.'); /* find parent mailbox */ if (p && (p - buf > domainlen)) /* don't split subdomain */ *p = '\0'; else if (!buf[domainlen]) /* server entry */ buf[0] = '\0'; else /* domain entry */ buf[domainlen] = '\0'; } if (r || !attrib.s || strcasecmp(attrib.s, "true")) { buf_free(&attrib); free(extname); return 0; } buf_free(&attrib); } again: if (blocking) r = mailbox_open_irl(name, &mailbox); else r = mailbox_open_irlnb(name, &mailbox); if (r == IMAP_MAILBOX_LOCKED) { if (verbose) syslog(LOG_INFO, "mailbox %s locked, retrying", extname); free(extname); return r; } if (r) { if (verbose) { printf("error opening %s: %s\n", extname, error_message(r)); } syslog(LOG_INFO, "error opening %s: %s\n", extname, error_message(r)); free(extname); return r; } syslog(LOG_INFO, "indexing mailbox %s... ", extname); if (verbose > 0) { printf("Indexing mailbox %s... ", extname); } r = search_update_mailbox(rx, mailbox, flags); mailbox_close(&mailbox); /* in non-blocking (rolling) mode, only do one batch per mailbox at * a time for fairness [IRIS-2471]. The squatter will re-insert the * mailbox in the queue */ if (blocking && r == IMAP_AGAIN) goto again; free(extname); return r; }
/* This handles piping of the LSUB command, because we have to figure out * what mailboxes actually exist before passing them to the end user. * * It is also needed if we are doing a FIND MAILBOXES, for that we do an * LSUB on the backend anyway, because the semantics of FIND do not allow * it to return nonexistant mailboxes (RFC1176), but we need to really dumb * down the response when this is the case. */ int pipe_lsub(struct backend *s, const char *userid, const char *tag, int force_notfatal, const char *resp) { int taglen = strlen(tag); int c; int r = PROXY_OK; int exist_r; static struct buf tagb, cmd, sep, name; struct buf flags = BUF_INITIALIZER; const char *end_strip_flags[] = { " \\NonExistent)", "\\NonExistent)", " \\Noselect)", "\\Noselect)", NULL }; const char *mid_strip_flags[] = { "\\NonExistent ", "\\Noselect ", NULL }; assert(s); assert(s->timeout); s->timeout->mark = time(NULL) + IDLE_TIMEOUT; while(1) { c = getword(s->in, &tagb); if(c == EOF) { if(s == backend_current && !force_notfatal) fatal("Lost connection to selected backend", EC_UNAVAILABLE); proxy_downserver(s); r = PROXY_NOCONNECTION; goto out; } if(!strncmp(tag, tagb.s, taglen)) { char buf[2048]; if(!prot_fgets(buf, sizeof(buf), s->in)) { if(s == backend_current && !force_notfatal) fatal("Lost connection to selected backend", EC_UNAVAILABLE); proxy_downserver(s); r = PROXY_NOCONNECTION; goto out; } /* Got the end of the response */ buf_appendcstr(&s->last_result, buf); buf_cstring(&s->last_result); switch (buf[0]) { case 'O': case 'o': r = PROXY_OK; break; case 'N': case 'n': r = PROXY_NO; break; case 'B': case 'b': r = PROXY_BAD; break; default: /* huh? no result? */ if(s == backend_current && !force_notfatal) fatal("Lost connection to selected backend", EC_UNAVAILABLE); proxy_downserver(s); r = PROXY_NOCONNECTION; break; } break; /* we're done */ } c = getword(s->in, &cmd); if(c == EOF) { if(s == backend_current && !force_notfatal) fatal("Lost connection to selected backend", EC_UNAVAILABLE); proxy_downserver(s); r = PROXY_NOCONNECTION; goto out; } if(strncasecmp("LSUB", cmd.s, 4) && strncasecmp("LIST", cmd.s, 4)) { prot_printf(imapd_out, "%s %s ", tagb.s, cmd.s); r = pipe_to_end_of_response(s, force_notfatal); if (r != PROXY_OK) goto out; } else { /* build up the response bit by bit */ int i; buf_reset(&flags); c = prot_getc(s->in); while(c != ')' && c != EOF) { buf_putc(&flags, c); c = prot_getc(s->in); } if(c != EOF) { /* terminate string */ buf_putc(&flags, ')'); buf_cstring(&flags); /* get the next character */ c = prot_getc(s->in); } if(c != ' ') { if(s == backend_current && !force_notfatal) fatal("Bad LSUB response from selected backend", EC_UNAVAILABLE); proxy_downserver(s); r = PROXY_NOCONNECTION; goto out; } /* Check for flags that we should remove * (e.g. Noselect, NonExistent) */ for(i=0; end_strip_flags[i]; i++) buf_replace_all(&flags, end_strip_flags[i], ")"); for (i=0; mid_strip_flags[i]; i++) buf_replace_all(&flags, mid_strip_flags[i], NULL); /* Get separator */ c = getastring(s->in, s->out, &sep); if(c != ' ') { if(s == backend_current && !force_notfatal) fatal("Bad LSUB response from selected backend", EC_UNAVAILABLE); proxy_downserver(s); r = PROXY_NOCONNECTION; goto out; } /* Get name */ c = getastring(s->in, s->out, &name); if(c == '\r') c = prot_getc(s->in); if(c != '\n') { if(s == backend_current && !force_notfatal) fatal("Bad LSUB response from selected backend", EC_UNAVAILABLE); proxy_downserver(s); r = PROXY_NOCONNECTION; goto out; } /* lookup name */ exist_r = 1; char *intname = mboxname_from_external(name.s, &imapd_namespace, userid); mbentry_t *mbentry = NULL; exist_r = mboxlist_lookup(intname, &mbentry, NULL); free(intname); if(!exist_r && (mbentry->mbtype & MBTYPE_RESERVE)) exist_r = IMAP_MAILBOX_RESERVED; mboxlist_entry_free(&mbentry); /* send our response */ /* we need to set \Noselect if it's not in our mailboxes.db */ if (resp[0] == 'L') { if(!exist_r) { prot_printf(imapd_out, "* %s %s \"%s\" ", resp, flags.s, sep.s); } else { prot_printf(imapd_out, "* %s (\\Noselect) \"%s\" ", resp, sep.s); } prot_printstring(imapd_out, name.s); prot_printf(imapd_out, "\r\n"); } else if(resp[0] == 'M' && !exist_r) { /* Note that it has to exist for a find response */ prot_printf(imapd_out, "* %s ", resp); prot_printastring(imapd_out, name.s); prot_printf(imapd_out, "\r\n"); } } } /* while(1) */ out: buf_free(&flags); return r; }
{ fprintf(stderr, "chk_cyrus [-C <altconfig>] partition\n"); exit(-1); } static const char *check_part = NULL; /* partition we are checking */ static int chkmbox(char *name, int matchlen __attribute__((unused)), int maycreate __attribute__((unused)), void *rock __attribute__((unused))) { int r; mbentry_t *mbentry = NULL; r = mboxlist_lookup(name, &mbentry, NULL); /* xxx reserved mailboxes? */ if (r) { fprintf(stderr, "bad mailbox %s in chkmbox\n", name); fatal("fatal error",EC_TEMPFAIL); } /* are we on the partition we are checking? */ if (check_part && strcmp(mbentry->partition, check_part)) { mboxlist_entry_free(&mbentry); return 0; } fprintf(stderr, "checking: %s\n", name);
/* * mboxlist_findall() callback function to reconstruct a mailbox */ static int do_reconstruct(struct findall_data *data, void *rock) { if (!data) return 0; strarray_t *discovered = (strarray_t *)rock; int r; static char lastname[MAX_MAILBOX_NAME] = ""; char *other; struct mailbox *mailbox = NULL; char outpath[MAX_MAILBOX_PATH]; const char *name = mbname_intname(data->mbname); signals_poll(); /* don't repeat */ if (!strcmp(name, lastname)) return 0; strncpy(lastname, name, sizeof(lastname)); lastname[sizeof(lastname)-1] = '\0'; r = mailbox_reconstruct(lastname, reconstruct_flags); if (r) { com_err(lastname, r, "%s", (r == IMAP_IOERROR) ? error_message(errno) : "Failed to reconstruct mailbox"); return 0; } r = mailbox_open_iwl(lastname, &mailbox); if (r) { com_err(lastname, r, "Failed to open after reconstruct"); return 0; } other = hash_lookup(mailbox->uniqueid, &unqid_table); if (other) { syslog (LOG_ERR, "uniqueid clash with %s for %s - changing %s", other, mailbox->uniqueid, mailbox->name); /* uniqueid change required! */ mailbox_make_uniqueid(mailbox); } hash_insert(mailbox->uniqueid, xstrdup(mailbox->name), &unqid_table); /* Convert internal name to external */ char *extname = mboxname_to_external(lastname, &recon_namespace, NULL); if (!(reconstruct_flags & RECONSTRUCT_QUIET)) printf("%s\n", extname); strncpy(outpath, mailbox_meta_fname(mailbox, META_HEADER), MAX_MAILBOX_NAME); if (setversion) { /* need to re-set the version! */ int r = mailbox_setversion(mailbox, setversion); if (r) { printf("FAILED TO REPACK %s with new version %s\n", extname, error_message(r)); } else { printf("Repacked %s to version %d\n", extname, setversion); } } mailbox_close(&mailbox); free(extname); if (discovered) { char fnamebuf[MAX_MAILBOX_PATH]; char *ptr; DIR *dirp; struct dirent *dirent; struct stat sbuf; ptr = strstr(outpath, "cyrus.header"); if (!ptr) return 0; *ptr = 0; r = chdir(outpath); if (r) return 0; /* we recurse down this directory to see if there's any mailboxes under this not in the mailboxes database */ dirp = opendir("."); if (!dirp) return 0; while ((dirent = readdir(dirp)) != NULL) { /* mailbox directories never have a dot in them */ if (strchr(dirent->d_name, '.')) continue; if (stat(dirent->d_name, &sbuf) < 0) continue; if (!S_ISDIR(sbuf.st_mode)) continue; /* ok, we found a directory that doesn't have a dot in it; is there a cyrus.header file? */ snprintf(fnamebuf, MAX_MAILBOX_PATH, "%s%s", dirent->d_name, FNAME_HEADER); if (stat(fnamebuf, &sbuf) < 0) continue; /* ok, we have a real mailbox directory */ char buf[MAX_MAILBOX_NAME]; snprintf(buf, MAX_MAILBOX_NAME, "%s.%s", name, dirent->d_name); /* does fnamebuf exist as a mailbox in mboxlist? */ do { r = mboxlist_lookup(buf, NULL, NULL); } while (r == IMAP_AGAIN); if (!r) continue; /* mailbox exists; it'll be reconstructed with a -r */ if (r != IMAP_MAILBOX_NONEXISTENT) break; /* erg? */ else r = 0; /* reset error condition */ printf("discovered %s\n", buf); strarray_append(discovered, buf); } closedir(dirp); } return 0; }
int main(int argc, char **argv) { int opt, i, r; int dousers = 0; int rflag = 0; int mflag = 0; int fflag = 0; int xflag = 0; char buf[MAX_MAILBOX_PATH+1]; strarray_t discovered = STRARRAY_INITIALIZER; char *alt_config = NULL; char *start_part = NULL; if ((geteuid()) == 0 && (become_cyrus(/*is_master*/0) != 0)) { fatal("must run as the Cyrus user", EC_USAGE); } construct_hash_table(&unqid_table, 2047, 1); while ((opt = getopt(argc, argv, "C:kp:rmfsxgGqRUMoOnV:u")) != EOF) { switch (opt) { case 'C': /* alt config file */ alt_config = optarg; break; case 'p': start_part = optarg; break; case 'r': rflag = 1; break; case 'u': dousers = 1; break; case 'm': mflag = 1; break; case 'n': reconstruct_flags &= ~RECONSTRUCT_MAKE_CHANGES; break; case 'g': fprintf(stderr, "reconstruct: deprecated option -g ignored\n"); break; case 'G': reconstruct_flags |= RECONSTRUCT_ALWAYS_PARSE; break; case 'f': fflag = 1; break; case 'x': xflag = 1; break; case 'k': fprintf(stderr, "reconstruct: deprecated option -k ignored\n"); break; case 's': reconstruct_flags &= ~RECONSTRUCT_DO_STAT; break; case 'q': reconstruct_flags |= RECONSTRUCT_QUIET; break; case 'R': reconstruct_flags |= RECONSTRUCT_GUID_REWRITE; break; case 'U': reconstruct_flags |= RECONSTRUCT_GUID_UNLINK; break; case 'o': reconstruct_flags |= RECONSTRUCT_IGNORE_ODDFILES; break; case 'O': reconstruct_flags |= RECONSTRUCT_REMOVE_ODDFILES; break; case 'M': reconstruct_flags |= RECONSTRUCT_PREFER_MBOXLIST; break; case 'V': if (!strcasecmp(optarg, "max")) setversion = MAILBOX_MINOR_VERSION; else setversion = atoi(optarg); break; default: usage(); } } cyrus_init(alt_config, "reconstruct", 0, CONFIG_NEED_PARTITION_DATA); global_sasl_init(1,0,NULL); /* Set namespace -- force standard (internal) */ if ((r = mboxname_init_namespace(&recon_namespace, 1)) != 0) { syslog(LOG_ERR, "%s", error_message(r)); fatal(error_message(r), EC_CONFIG); } sync_log_init(); if (mflag) { if (rflag || fflag || optind != argc) { cyrus_done(); usage(); } do_mboxlist(); } mboxlist_init(0); mboxlist_open(NULL); quotadb_init(0); quotadb_open(NULL); #ifdef WITH_DAV caldav_init(); carddav_init(); webdav_init(); #endif /* Deal with nonexistent mailboxes */ if (start_part) { /* We were handed a mailbox that does not exist currently */ if(optind == argc) { fprintf(stderr, "When using -p, you must specify a mailbox to attempt to reconstruct."); exit(EC_USAGE); } /* do any of the mailboxes exist in mboxlist already? */ /* Do they look like mailboxes? */ for (i = optind; i < argc; i++) { if (strchr(argv[i],'%') || strchr(argv[i],'*')) { fprintf(stderr, "Using wildcards with -p is not supported.\n"); exit(EC_USAGE); } /* Translate mailboxname */ char *intname = mboxname_from_external(argv[i], &recon_namespace, NULL); /* Does it exist */ do { r = mboxlist_lookup(intname, NULL, NULL); } while (r == IMAP_AGAIN); if (r != IMAP_MAILBOX_NONEXISTENT) { fprintf(stderr, "Mailbox %s already exists. Cannot specify -p.\n", argv[i]); exit(EC_USAGE); } free(intname); } /* None of them exist. Create them. */ for (i = optind; i < argc; i++) { /* Translate mailboxname */ char *intname = mboxname_from_external(argv[i], &recon_namespace, NULL); /* don't notify mailbox creation here */ r = mboxlist_createmailbox(intname, 0, start_part, 1, "cyrus", NULL, 0, 0, !xflag, 0, NULL); if (r) { fprintf(stderr, "could not create %s\n", argv[i]); } free(intname); } } /* Normal Operation */ if (optind == argc) { if (rflag || dousers) { fprintf(stderr, "please specify a mailbox to recurse from\n"); cyrus_done(); exit(EC_USAGE); } assert(!rflag); strlcpy(buf, "*", sizeof(buf)); mboxlist_findall(&recon_namespace, buf, 1, 0, 0, do_reconstruct, NULL); } for (i = optind; i < argc; i++) { if (dousers) { mboxlist_usermboxtree(argv[i], do_reconstruct_p, NULL, MBOXTREE_TOMBSTONES|MBOXTREE_DELETED); continue; } char *domain = NULL; /* save domain */ if (config_virtdomains) domain = strchr(argv[i], '@'); strlcpy(buf, argv[i], sizeof(buf)); /* reconstruct the first mailbox/pattern */ mboxlist_findall(&recon_namespace, buf, 1, 0, 0, do_reconstruct, fflag ? &discovered : NULL); if (rflag) { /* build a pattern for submailboxes */ char *p = strchr(buf, '@'); if (p) *p = '\0'; strlcat(buf, ".*", sizeof(buf)); /* append the domain */ if (domain) strlcat(buf, domain, sizeof(buf)); /* reconstruct the submailboxes */ mboxlist_findall(&recon_namespace, buf, 1, 0, 0, do_reconstruct, fflag ? &discovered : NULL); } } /* examine our list to see if we discovered anything */ while (discovered.count) { char *name = strarray_shift(&discovered); int r = 0; /* create p (database only) and reconstruct it */ /* partition is defined by the parent mailbox */ /* don't notify mailbox creation here */ r = mboxlist_createmailbox(name, 0, NULL, 1, "cyrus", NULL, 0, 0, !xflag, 0, NULL); if (r) { fprintf(stderr, "createmailbox %s: %s\n", name, error_message(r)); } else { mboxlist_findone(&recon_namespace, name, 1, 0, 0, do_reconstruct, &discovered); } /* may have added more things into our list */ free(name); } free_hash_table(&unqid_table, free); sync_log_done(); mboxlist_close(); mboxlist_done(); quotadb_close(); quotadb_done(); partlist_local_done(); #ifdef WITH_DAV webdav_done(); carddav_done(); caldav_done(); #endif cyrus_done(); strarray_fini(&discovered); return 0; }
int main(int argc, char **argv) { struct mboxlist_entry *mbentry = NULL; int rc, i, quiet = 0, stop_on_error=0, metadata=0; int opt; /* getopt() returns an int */ char *alt_config = NULL; char buf[MAX_MAILBOX_PATH+1]; if ((geteuid()) == 0 && (become_cyrus() != 0)) { fatal("must run as the Cyrus user", EC_USAGE); } while ((opt = getopt(argc, argv, "C:qsm")) != EOF) { switch(opt) { case 'C': /* alt config file */ alt_config = optarg; break; case 'q': quiet = 1; break; case 's': stop_on_error = 1; break; case 'm': metadata = 1; break; default: usage(); } } cyrus_init(alt_config, "mbpath", 0); if ((rc = mboxname_init_namespace(&mbpath_namespace, 1)) != 0) { fatal(error_message(rc), -1); } mboxlist_init(0); mboxlist_open(NULL); for (i = optind; i < argc; i++) { /* Translate mailboxname */ (*mbpath_namespace.mboxname_tointernal)(&mbpath_namespace, argv[i], NULL, buf); if ((rc = mboxlist_lookup(buf, &mbentry, NULL)) == 0) { if (mbentry->mbtype & MBTYPE_REMOTE) { printf("%s!%s\n", mbentry->server, mbentry->partition); } else if (metadata) { const char *path = mboxname_metapath(mbentry->partition, mbentry->name, 0, 0); printf("%s\n", path); } else { const char *path = mboxname_datapath(mbentry->partition, mbentry->name, 0); printf("%s\n", path); } mboxlist_entry_free(&mbentry); } else { if (!quiet && (rc == IMAP_MAILBOX_NONEXISTENT)) { fprintf(stderr, "Invalid mailbox name: %s\n", argv[i]); } if (stop_on_error) { if (quiet) { fatal("", -1); } else { fatal("Error in processing mailbox. Stopping\n", -1); } } } } mboxlist_close(); mboxlist_done(); cyrus_done(); return 0; }