static int eat_args (imap4d_tokbuf_t tok) { int n = IMAP4_ARG_1; char *p; p = imap4d_tokbuf_getarg (tok, n++); if (!p) return RESP_BAD; if (mu_c_strcasecmp (p, "NIL") == 0) { if (imap4d_tokbuf_getarg (tok, n)) return RESP_BAD; return RESP_OK; } else if (p[0] != '(') return RESP_BAD; /* Collect arguments */ while ((p = imap4d_tokbuf_getarg (tok, n++))) { if (p[0] == ')') { if (imap4d_tokbuf_getarg (tok, n)) return RESP_BAD; return RESP_OK; } } return RESP_BAD; }
int imap4d_uid (struct imap4d_command *command, imap4d_tokbuf_t tok) { char *cmd; int rc = RESP_NO; char *err_text = "Completed"; if (imap4d_tokbuf_argc (tok) < 3) return util_finish (command, RESP_BAD, "Invalid arguments"); cmd = imap4d_tokbuf_getarg (tok, IMAP4_ARG_1); if (mu_c_strcasecmp (cmd, "FETCH") == 0) rc = imap4d_fetch0 (tok, 1, &err_text); else if (mu_c_strcasecmp (cmd, "COPY") == 0) rc = imap4d_copy0 (tok, 1, &err_text); else if (mu_c_strcasecmp (cmd, "STORE") == 0) rc = imap4d_store0 (tok, 1, &err_text); else if (mu_c_strcasecmp (cmd, "SEARCH") == 0) rc = imap4d_search0 (tok, 1, &err_text); else { err_text = "Uknown uid command"; rc = RESP_BAD; } return util_finish (command, rc, "%s %s", cmd, err_text); }
/* 6.3.6. SUBSCRIBE Command Arguments: mailbox Responses: no specific responses for this command Result: OK - subscribe completed NO - subscribe failure: can't subscribe to that name BAD - command unknown or arguments invalid */ int imap4d_subscribe (struct imap4d_session *session, struct imap4d_command *command, imap4d_tokbuf_t tok) { int rc; char *name; mu_property_t prop; if (imap4d_tokbuf_argc (tok) != 3) return io_completion_response (command, RESP_BAD, "Invalid arguments"); name = imap4d_tokbuf_getarg (tok, IMAP4_ARG_1); prop = open_subscription (); if (!prop) return io_completion_response (command, RESP_NO, "Cannot subscribe"); rc = mu_property_set_value (prop, name, "", 1); if (rc) mu_diag_funcall (MU_DIAG_ERROR, "mu_property_set_value", name, rc); else { rc = mu_property_save (prop); if (rc) mu_diag_funcall (MU_DIAG_ERROR, "mu_property_save", NULL, rc); } mu_property_destroy (&prop); if (rc) return io_completion_response (command, RESP_NO, "Cannot subscribe"); return io_completion_response (command, RESP_OK, "Completed"); }
int imap4d_select (struct imap4d_session *session, struct imap4d_command *command, imap4d_tokbuf_t tok) { if (imap4d_tokbuf_argc (tok) != 3) return io_completion_response (command, RESP_BAD, "Invalid arguments"); return imap4d_select0 (command, imap4d_tokbuf_getarg (tok, IMAP4_ARG_1), MU_STREAM_RDWR); }
int imap4d_authenticate (struct imap4d_session *session, struct imap4d_command *command, imap4d_tokbuf_t tok) { char *auth_type; struct imap4d_auth adata; enum imap4d_auth_result res; if (imap4d_tokbuf_argc (tok) != 3) return io_completion_response (command, RESP_BAD, "Invalid arguments"); auth_type = imap4d_tokbuf_getarg (tok, IMAP4_ARG_1); if (tls_required) return io_completion_response (command, RESP_NO, "Command disabled: Use STARTTLS first"); adata.command = command; adata.auth_type = auth_type; adata.username = NULL; res = mu_list_foreach (imap_auth_list, _auth_try, &adata); switch (res) { case imap4d_auth_nosup: return io_completion_response (command, RESP_NO, "Authentication mechanism not supported"); case imap4d_auth_ok: return 0; case imap4d_auth_resp: if (adata.response == RESP_OK && adata.username) { if (imap4d_session_setup (adata.username)) return io_completion_response (command, RESP_NO, "User name or passwd rejected"); else return io_completion_response (command, RESP_OK, "%s authentication successful", auth_type); } /* fall through */ case imap4d_auth_fail: adata.response = RESP_NO; break; } return io_completion_response (command, adata.response, "%s authentication failed", auth_type); }
int imap4d_authenticate (struct imap4d_command *command, imap4d_tokbuf_t tok) { char *auth_type; struct auth_data adata; if (imap4d_tokbuf_argc (tok) != 3) return util_finish (command, RESP_BAD, "Invalid arguments"); auth_type = imap4d_tokbuf_getarg (tok, IMAP4_ARG_1); if (tls_required) return util_finish (command, RESP_NO, "Command disabled: Use STARTTLS first"); adata.command = command; adata.auth_type = auth_type; adata.arg = NULL; adata.username = NULL; if (mu_list_do (imap_auth_list, _auth_try, &adata) == 0) return util_finish (command, RESP_NO, "Authentication mechanism not supported"); if (adata.result == RESP_OK && adata.username) { if (imap4d_session_setup (adata.username)) return util_finish (command, RESP_NO, "User name or passwd rejected"); else return util_finish (command, RESP_OK, "%s authentication successful", auth_type); } return util_finish (command, adata.result, "%s authentication failed", auth_type); }
int imap4d_list (struct imap4d_session *session, struct imap4d_command *command, imap4d_tokbuf_t tok) { char *ref; char *wcard; if (imap4d_tokbuf_argc (tok) != 4) return io_completion_response (command, RESP_BAD, "Invalid arguments"); ref = imap4d_tokbuf_getarg (tok, IMAP4_ARG_1); wcard = imap4d_tokbuf_getarg (tok, IMAP4_ARG_2); /* If wildcard is empty, it is a special case: we have to return the hierarchy. */ if (*wcard == '\0') { if (*ref) io_untagged_response (RESP_NONE, "LIST (\\NoSelect) \"%c\" \"%c\"", MU_HIERARCHY_DELIMITER, MU_HIERARCHY_DELIMITER); else io_untagged_response (RESP_NONE, "LIST (\\NoSelect) \"%c\" \"\"", MU_HIERARCHY_DELIMITER); } /* There is only one mailbox in the "INBOX" hierarchy ... INBOX. */ else if (mu_c_strcasecmp (ref, "INBOX") == 0 || (ref[0] == 0 && mu_c_strcasecmp (wcard, "INBOX") == 0)) { io_untagged_response (RESP_NONE, "LIST (\\NoInferiors) NIL INBOX"); } else { int status; mu_folder_t folder; char *cwd; char *p, *q; struct refinfo refinfo; switch (*wcard) { /* Absolute Path in wcard, dump the old ref. */ case '/': { ref = calloc (2, 1); ref[0] = *wcard; wcard++; } break; /* Absolute Path, but take care of things like ~guest/Mail, ref becomes ref = ~guest. */ case '~': { char *s = strchr (wcard, '/'); if (s) { ref = calloc (s - wcard + 1, 1); memcpy (ref, wcard, s - wcard); ref [s - wcard] = '\0'; wcard = s + 1; } else { ref = mu_strdup (wcard); wcard += strlen (wcard); } } break; default: ref = mu_strdup (ref); } /* Move any directory not containing a wildcard into the reference So (ref = ~guest, wcard = Mail/folder1/%.vf) --> (ref = ~guest/Mail/folder1, wcard = %.vf). */ for (p = wcard; (q = strpbrk (p, "/%*")) && *q == '/'; p = q + 1) ; if (p > wcard) { size_t seglen = p - wcard; size_t reflen = strlen (ref); int addslash = (reflen > 0 && ref[reflen-1] != '/'); size_t len = seglen + reflen + addslash + 1; ref = realloc (ref, len); if (addslash) ref[reflen++] = '/'; memcpy (ref + reflen, wcard, seglen); ref[reflen + seglen] = 0; wcard += seglen; } /* Allocates. */ cwd = namespace_checkfullpath (ref, wcard, NULL); if (!cwd) { free (ref); return io_completion_response (command, RESP_NO, "The requested item could not be found."); } status = mu_folder_create (&folder, cwd); if (status) { free (ref); free (cwd); return io_completion_response (command, RESP_NO, "The requested item could not be found."); } /* Force the right matcher */ mu_folder_set_match (folder, mu_folder_imap_match); memset (&refinfo, 0, sizeof refinfo); refinfo.refptr = ref; refinfo.reflen = strlen (ref); refinfo.pfxlen = strlen (cwd); refinfo.homelen = strlen (imap4d_homedir); /* The special name INBOX is included in the output from LIST, if INBOX is supported by this server for this user and if the uppercase string "INBOX" matches the interpreted reference and mailbox name arguments with wildcards as described above. The criteria for omitting INBOX is whether SELECT INBOX will return failure; it is not relevant whether the user's real INBOX resides on this or some other server. */ if (!*ref && (mu_imap_wildmatch (wcard, "INBOX", MU_HIERARCHY_DELIMITER) == 0 || mu_imap_wildmatch (wcard, "inbox", MU_HIERARCHY_DELIMITER) == 0)) io_untagged_response (RESP_NONE, "LIST (\\NoInferiors) NIL INBOX"); mu_folder_enumerate (folder, NULL, wcard, 0, 0, NULL, list_fun, &refinfo); mu_folder_destroy (&folder); free (refinfo.buf); free (cwd); free (ref); } return io_completion_response (command, RESP_OK, "Completed"); }
/* FIXME: How do we do this ??????: IF a new mailbox is created with the same name as a mailbox which was deleted, its unique identifiers MUST be greater than any unique identifiers used in the previous incarnation of the mailbox. */ int imap4d_create (struct imap4d_session *session, struct imap4d_command *command, imap4d_tokbuf_t tok) { char *name; int isdir = 0; int ns; int rc = RESP_OK; const char *msg = "Completed"; if (imap4d_tokbuf_argc (tok) != 3) return io_completion_response (command, RESP_BAD, "Invalid arguments"); name = imap4d_tokbuf_getarg (tok, IMAP4_ARG_1); if (*name == '\0') return io_completion_response (command, RESP_BAD, "Too few arguments"); /* Creating, "Inbox" should always fail. */ if (mu_c_strcasecmp (name, "INBOX") == 0) return io_completion_response (command, RESP_BAD, "Already exist"); /* RFC 3501: If the mailbox name is suffixed with the server's hierarchy separator character, this is a declaration that the client intends to create mailbox names under this name in the hierarchy. The trailing delimiter will be removed by namespace normalizer, so test for it now. */ if (name[strlen (name) - 1] == MU_HIERARCHY_DELIMITER) isdir = 1; /* Allocates memory. */ name = namespace_getfullpath (name, &ns); if (!name) return io_completion_response (command, RESP_NO, "Cannot create mailbox"); /* It will fail if the mailbox already exists. */ if (access (name, F_OK) != 0) { if (make_interdir (name, MU_HIERARCHY_DELIMITER, MKDIR_PERMISSIONS)) { rc = RESP_NO; msg = "Cannot create mailbox"; } if (rc == RESP_OK && !isdir) { mu_mailbox_t mbox; rc = mu_mailbox_create_default (&mbox, name); if (rc) { mu_diag_output (MU_DIAG_ERR, _("Cannot create mailbox %s: %s"), name, mu_strerror (rc)); rc = RESP_NO; msg = "Cannot create mailbox"; } else if ((rc = mu_mailbox_open (mbox, MU_STREAM_RDWR | MU_STREAM_CREAT | mailbox_mode[ns]))) { mu_diag_output (MU_DIAG_ERR, _("Cannot open mailbox %s: %s"), name, mu_strerror (rc)); rc = RESP_NO; msg = "Cannot create mailbox"; } else { mu_mailbox_close (mbox); mu_mailbox_destroy (&mbox); rc = RESP_OK; } } } else { rc = RESP_NO; msg = "already exists"; } return io_completion_response (command, rc, "%s", msg); }