123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115 |
- #include "curl_setup.h"
- #ifndef CURL_DISABLE_IMAP
- #ifdef HAVE_NETINET_IN_H
- #include <netinet/in.h>
- #endif
- #ifdef HAVE_ARPA_INET_H
- #include <arpa/inet.h>
- #endif
- #ifdef HAVE_NETDB_H
- #include <netdb.h>
- #endif
- #ifdef __VMS
- #include <in.h>
- #include <inet.h>
- #endif
- #include <curl/curl.h>
- #include "urldata.h"
- #include "sendf.h"
- #include "hostip.h"
- #include "progress.h"
- #include "transfer.h"
- #include "escape.h"
- #include "http.h"
- #include "socks.h"
- #include "imap.h"
- #include "mime.h"
- #include "strtoofft.h"
- #include "strcase.h"
- #include "vtls/vtls.h"
- #include "cfilters.h"
- #include "connect.h"
- #include "select.h"
- #include "multiif.h"
- #include "url.h"
- #include "bufref.h"
- #include "curl_sasl.h"
- #include "warnless.h"
- #include "curl_ctype.h"
- #include "curl_printf.h"
- #include "curl_memory.h"
- #include "memdebug.h"
- static CURLcode imap_regular_transfer(struct Curl_easy *data, bool *done);
- static CURLcode imap_do(struct Curl_easy *data, bool *done);
- static CURLcode imap_done(struct Curl_easy *data, CURLcode status,
- bool premature);
- static CURLcode imap_connect(struct Curl_easy *data, bool *done);
- static CURLcode imap_disconnect(struct Curl_easy *data,
- struct connectdata *conn, bool dead);
- static CURLcode imap_multi_statemach(struct Curl_easy *data, bool *done);
- static int imap_getsock(struct Curl_easy *data, struct connectdata *conn,
- curl_socket_t *socks);
- static CURLcode imap_doing(struct Curl_easy *data, bool *dophase_done);
- static CURLcode imap_setup_connection(struct Curl_easy *data,
- struct connectdata *conn);
- static char *imap_atom(const char *str, bool escape_only);
- static CURLcode imap_sendf(struct Curl_easy *data, const char *fmt, ...)
- CURL_PRINTF(2, 3);
- static CURLcode imap_parse_url_options(struct connectdata *conn);
- static CURLcode imap_parse_url_path(struct Curl_easy *data);
- static CURLcode imap_parse_custom_request(struct Curl_easy *data);
- static CURLcode imap_perform_authenticate(struct Curl_easy *data,
- const char *mech,
- const struct bufref *initresp);
- static CURLcode imap_continue_authenticate(struct Curl_easy *data,
- const char *mech,
- const struct bufref *resp);
- static CURLcode imap_cancel_authenticate(struct Curl_easy *data,
- const char *mech);
- static CURLcode imap_get_message(struct Curl_easy *data, struct bufref *out);
- const struct Curl_handler Curl_handler_imap = {
- "imap",
- imap_setup_connection,
- imap_do,
- imap_done,
- ZERO_NULL,
- imap_connect,
- imap_multi_statemach,
- imap_doing,
- imap_getsock,
- imap_getsock,
- ZERO_NULL,
- ZERO_NULL,
- imap_disconnect,
- ZERO_NULL,
- ZERO_NULL,
- ZERO_NULL,
- ZERO_NULL,
- PORT_IMAP,
- CURLPROTO_IMAP,
- CURLPROTO_IMAP,
- PROTOPT_CLOSEACTION|
- PROTOPT_URLOPTIONS
- };
- #ifdef USE_SSL
- const struct Curl_handler Curl_handler_imaps = {
- "imaps",
- imap_setup_connection,
- imap_do,
- imap_done,
- ZERO_NULL,
- imap_connect,
- imap_multi_statemach,
- imap_doing,
- imap_getsock,
- imap_getsock,
- ZERO_NULL,
- ZERO_NULL,
- imap_disconnect,
- ZERO_NULL,
- ZERO_NULL,
- ZERO_NULL,
- ZERO_NULL,
- PORT_IMAPS,
- CURLPROTO_IMAPS,
- CURLPROTO_IMAP,
- PROTOPT_CLOSEACTION | PROTOPT_SSL |
- PROTOPT_URLOPTIONS
- };
- #endif
- #define IMAP_RESP_OK 1
- #define IMAP_RESP_NOT_OK 2
- #define IMAP_RESP_PREAUTH 3
- static const struct SASLproto saslimap = {
- "imap",
- imap_perform_authenticate,
- imap_continue_authenticate,
- imap_cancel_authenticate,
- imap_get_message,
- 0,
- '+',
- IMAP_RESP_OK,
- SASL_AUTH_DEFAULT,
- SASL_FLAG_BASE64
- };
- #ifdef USE_SSL
- static void imap_to_imaps(struct connectdata *conn)
- {
-
- conn->handler = &Curl_handler_imaps;
-
- conn->bits.tls_upgraded = TRUE;
- }
- #else
- #define imap_to_imaps(x) Curl_nop_stmt
- #endif
- static bool imap_matchresp(const char *line, size_t len, const char *cmd)
- {
- const char *end = line + len;
- size_t cmd_len = strlen(cmd);
-
- line += 2;
-
- if(line < end && ISDIGIT(*line)) {
-
- do
- line++;
- while(line < end && ISDIGIT(*line));
-
- if(line == end || *line != ' ')
- return FALSE;
- line++;
- }
-
- if(line + cmd_len <= end && strncasecompare(line, cmd, cmd_len) &&
- (line[cmd_len] == ' ' || line + cmd_len + 2 == end))
- return TRUE;
- return FALSE;
- }
- static bool imap_endofresp(struct Curl_easy *data, struct connectdata *conn,
- char *line, size_t len, int *resp)
- {
- struct IMAP *imap = data->req.p.imap;
- struct imap_conn *imapc = &conn->proto.imapc;
- const char *id = imapc->resptag;
- size_t id_len = strlen(id);
-
- if(len >= id_len + 1 && !memcmp(id, line, id_len) && line[id_len] == ' ') {
- line += id_len + 1;
- len -= id_len + 1;
- if(len >= 2 && !memcmp(line, "OK", 2))
- *resp = IMAP_RESP_OK;
- else if(len >= 7 && !memcmp(line, "PREAUTH", 7))
- *resp = IMAP_RESP_PREAUTH;
- else
- *resp = IMAP_RESP_NOT_OK;
- return TRUE;
- }
-
- if(len >= 2 && !memcmp("* ", line, 2)) {
- switch(imapc->state) {
-
- case IMAP_CAPABILITY:
- if(!imap_matchresp(line, len, "CAPABILITY"))
- return FALSE;
- break;
- case IMAP_LIST:
- if((!imap->custom && !imap_matchresp(line, len, "LIST")) ||
- (imap->custom && !imap_matchresp(line, len, imap->custom) &&
- (!strcasecompare(imap->custom, "STORE") ||
- !imap_matchresp(line, len, "FETCH")) &&
- !strcasecompare(imap->custom, "SELECT") &&
- !strcasecompare(imap->custom, "EXAMINE") &&
- !strcasecompare(imap->custom, "SEARCH") &&
- !strcasecompare(imap->custom, "EXPUNGE") &&
- !strcasecompare(imap->custom, "LSUB") &&
- !strcasecompare(imap->custom, "UID") &&
- !strcasecompare(imap->custom, "GETQUOTAROOT") &&
- !strcasecompare(imap->custom, "NOOP")))
- return FALSE;
- break;
- case IMAP_SELECT:
-
- break;
- case IMAP_FETCH:
- if(!imap_matchresp(line, len, "FETCH"))
- return FALSE;
- break;
- case IMAP_SEARCH:
- if(!imap_matchresp(line, len, "SEARCH"))
- return FALSE;
- break;
-
- default:
- return FALSE;
- }
- *resp = '*';
- return TRUE;
- }
-
- if(imap && !imap->custom && ((len == 3 && line[0] == '+') ||
- (len >= 2 && !memcmp("+ ", line, 2)))) {
- switch(imapc->state) {
-
- case IMAP_AUTHENTICATE:
- case IMAP_APPEND:
- *resp = '+';
- break;
- default:
- failf(data, "Unexpected continuation response");
- *resp = -1;
- break;
- }
- return TRUE;
- }
- return FALSE;
- }
- static CURLcode imap_get_message(struct Curl_easy *data, struct bufref *out)
- {
- char *message = Curl_dyn_ptr(&data->conn->proto.imapc.pp.recvbuf);
- size_t len = data->conn->proto.imapc.pp.nfinal;
- if(len > 2) {
-
- len -= 2;
- for(message += 2; *message == ' ' || *message == '\t'; message++, len--)
- ;
-
- while(len--)
- if(message[len] != '\r' && message[len] != '\n' && message[len] != ' ' &&
- message[len] != '\t')
- break;
-
- message[++len] = '\0';
- Curl_bufref_set(out, message, len, NULL);
- }
- else
-
- Curl_bufref_set(out, "", 0, NULL);
- return CURLE_OK;
- }
- static void imap_state(struct Curl_easy *data, imapstate newstate)
- {
- struct imap_conn *imapc = &data->conn->proto.imapc;
- #if defined(DEBUGBUILD) && !defined(CURL_DISABLE_VERBOSE_STRINGS)
-
- static const char * const names[]={
- "STOP",
- "SERVERGREET",
- "CAPABILITY",
- "STARTTLS",
- "UPGRADETLS",
- "AUTHENTICATE",
- "LOGIN",
- "LIST",
- "SELECT",
- "FETCH",
- "FETCH_FINAL",
- "APPEND",
- "APPEND_FINAL",
- "SEARCH",
- "LOGOUT",
-
- };
- if(imapc->state != newstate)
- infof(data, "IMAP %p state change from %s to %s",
- (void *)imapc, names[imapc->state], names[newstate]);
- #endif
- imapc->state = newstate;
- }
- static CURLcode imap_perform_capability(struct Curl_easy *data,
- struct connectdata *conn)
- {
- CURLcode result = CURLE_OK;
- struct imap_conn *imapc = &conn->proto.imapc;
- imapc->sasl.authmechs = SASL_AUTH_NONE;
- imapc->sasl.authused = SASL_AUTH_NONE;
- imapc->tls_supported = FALSE;
-
- result = imap_sendf(data, "CAPABILITY");
- if(!result)
- imap_state(data, IMAP_CAPABILITY);
- return result;
- }
- static CURLcode imap_perform_starttls(struct Curl_easy *data)
- {
-
- CURLcode result = imap_sendf(data, "STARTTLS");
- if(!result)
- imap_state(data, IMAP_STARTTLS);
- return result;
- }
- static CURLcode imap_perform_upgrade_tls(struct Curl_easy *data,
- struct connectdata *conn)
- {
-
- struct imap_conn *imapc = &conn->proto.imapc;
- CURLcode result;
- bool ssldone = FALSE;
- if(!Curl_conn_is_ssl(conn, FIRSTSOCKET)) {
- result = Curl_ssl_cfilter_add(data, conn, FIRSTSOCKET);
- if(result)
- goto out;
- }
- result = Curl_conn_connect(data, FIRSTSOCKET, FALSE, &ssldone);
- if(!result) {
- imapc->ssldone = ssldone;
- if(imapc->state != IMAP_UPGRADETLS)
- imap_state(data, IMAP_UPGRADETLS);
- if(imapc->ssldone) {
- imap_to_imaps(conn);
- result = imap_perform_capability(data, conn);
- }
- }
- out:
- return result;
- }
- static CURLcode imap_perform_login(struct Curl_easy *data,
- struct connectdata *conn)
- {
- CURLcode result = CURLE_OK;
- char *user;
- char *passwd;
-
- if(!data->state.aptr.user) {
- imap_state(data, IMAP_STOP);
- return result;
- }
-
- user = imap_atom(conn->user, FALSE);
- passwd = imap_atom(conn->passwd, FALSE);
-
- result = imap_sendf(data, "LOGIN %s %s", user ? user : "",
- passwd ? passwd : "");
- free(user);
- free(passwd);
- if(!result)
- imap_state(data, IMAP_LOGIN);
- return result;
- }
- static CURLcode imap_perform_authenticate(struct Curl_easy *data,
- const char *mech,
- const struct bufref *initresp)
- {
- CURLcode result = CURLE_OK;
- const char *ir = (const char *) Curl_bufref_ptr(initresp);
- if(ir) {
-
- result = imap_sendf(data, "AUTHENTICATE %s %s", mech, ir);
- }
- else {
-
- result = imap_sendf(data, "AUTHENTICATE %s", mech);
- }
- return result;
- }
- static CURLcode imap_continue_authenticate(struct Curl_easy *data,
- const char *mech,
- const struct bufref *resp)
- {
- struct imap_conn *imapc = &data->conn->proto.imapc;
- (void)mech;
- return Curl_pp_sendf(data, &imapc->pp,
- "%s", (const char *) Curl_bufref_ptr(resp));
- }
- static CURLcode imap_cancel_authenticate(struct Curl_easy *data,
- const char *mech)
- {
- struct imap_conn *imapc = &data->conn->proto.imapc;
- (void)mech;
- return Curl_pp_sendf(data, &imapc->pp, "*");
- }
- static CURLcode imap_perform_authentication(struct Curl_easy *data,
- struct connectdata *conn)
- {
- CURLcode result = CURLE_OK;
- struct imap_conn *imapc = &conn->proto.imapc;
- saslprogress progress;
-
- if(imapc->preauth ||
- !Curl_sasl_can_authenticate(&imapc->sasl, data)) {
- imap_state(data, IMAP_STOP);
- return result;
- }
-
- result = Curl_sasl_start(&imapc->sasl, data, imapc->ir_supported, &progress);
- if(!result) {
- if(progress == SASL_INPROGRESS)
- imap_state(data, IMAP_AUTHENTICATE);
- else if(!imapc->login_disabled && (imapc->preftype & IMAP_TYPE_CLEARTEXT))
-
- result = imap_perform_login(data, conn);
- else {
-
- infof(data, "No known authentication mechanisms supported");
- result = CURLE_LOGIN_DENIED;
- }
- }
- return result;
- }
- static CURLcode imap_perform_list(struct Curl_easy *data)
- {
- CURLcode result = CURLE_OK;
- struct IMAP *imap = data->req.p.imap;
- if(imap->custom)
-
- result = imap_sendf(data, "%s%s", imap->custom,
- imap->custom_params ? imap->custom_params : "");
- else {
-
- char *mailbox = imap->mailbox ? imap_atom(imap->mailbox, TRUE)
- : strdup("");
- if(!mailbox)
- return CURLE_OUT_OF_MEMORY;
-
- result = imap_sendf(data, "LIST \"%s\" *", mailbox);
- free(mailbox);
- }
- if(!result)
- imap_state(data, IMAP_LIST);
- return result;
- }
- static CURLcode imap_perform_select(struct Curl_easy *data)
- {
- CURLcode result = CURLE_OK;
- struct connectdata *conn = data->conn;
- struct IMAP *imap = data->req.p.imap;
- struct imap_conn *imapc = &conn->proto.imapc;
- char *mailbox;
-
- Curl_safefree(imapc->mailbox);
- Curl_safefree(imapc->mailbox_uidvalidity);
-
- if(!imap->mailbox) {
- failf(data, "Cannot SELECT without a mailbox.");
- return CURLE_URL_MALFORMAT;
- }
-
- mailbox = imap_atom(imap->mailbox, FALSE);
- if(!mailbox)
- return CURLE_OUT_OF_MEMORY;
-
- result = imap_sendf(data, "SELECT %s", mailbox);
- free(mailbox);
- if(!result)
- imap_state(data, IMAP_SELECT);
- return result;
- }
- static CURLcode imap_perform_fetch(struct Curl_easy *data)
- {
- CURLcode result = CURLE_OK;
- struct IMAP *imap = data->req.p.imap;
-
- if(imap->uid) {
-
- if(imap->partial)
- result = imap_sendf(data, "UID FETCH %s BODY[%s]<%s>",
- imap->uid, imap->section ? imap->section : "",
- imap->partial);
- else
- result = imap_sendf(data, "UID FETCH %s BODY[%s]",
- imap->uid, imap->section ? imap->section : "");
- }
- else if(imap->mindex) {
-
- if(imap->partial)
- result = imap_sendf(data, "FETCH %s BODY[%s]<%s>",
- imap->mindex, imap->section ? imap->section : "",
- imap->partial);
- else
- result = imap_sendf(data, "FETCH %s BODY[%s]",
- imap->mindex, imap->section ? imap->section : "");
- }
- else {
- failf(data, "Cannot FETCH without a UID.");
- return CURLE_URL_MALFORMAT;
- }
- if(!result)
- imap_state(data, IMAP_FETCH);
- return result;
- }
- static CURLcode imap_perform_append(struct Curl_easy *data)
- {
- CURLcode result = CURLE_OK;
- struct IMAP *imap = data->req.p.imap;
- char *mailbox;
-
- if(!imap->mailbox) {
- failf(data, "Cannot APPEND without a mailbox.");
- return CURLE_URL_MALFORMAT;
- }
- #ifndef CURL_DISABLE_MIME
-
- if(data->set.mimepost.kind != MIMEKIND_NONE) {
-
- data->set.mimepost.flags &= ~(unsigned int)MIME_BODY_ONLY;
-
- curl_mime_headers(&data->set.mimepost, data->set.headers, 0);
- result = Curl_mime_prepare_headers(data, &data->set.mimepost, NULL,
- NULL, MIMESTRATEGY_MAIL);
- if(!result)
- if(!Curl_checkheaders(data, STRCONST("Mime-Version")))
- result = Curl_mime_add_header(&data->set.mimepost.curlheaders,
- "Mime-Version: 1.0");
- if(!result)
- result = Curl_creader_set_mime(data, &data->set.mimepost);
- if(result)
- return result;
- data->state.infilesize = Curl_creader_client_length(data);
- }
- else
- #endif
- {
- result = Curl_creader_set_fread(data, data->state.infilesize);
- if(result)
- return result;
- }
-
- if(data->state.infilesize < 0) {
- failf(data, "Cannot APPEND with unknown input file size");
- return CURLE_UPLOAD_FAILED;
- }
-
- mailbox = imap_atom(imap->mailbox, FALSE);
- if(!mailbox)
- return CURLE_OUT_OF_MEMORY;
-
- result = imap_sendf(data, "APPEND %s (\\Seen) {%" FMT_OFF_T "}",
- mailbox, data->state.infilesize);
- free(mailbox);
- if(!result)
- imap_state(data, IMAP_APPEND);
- return result;
- }
- static CURLcode imap_perform_search(struct Curl_easy *data)
- {
- CURLcode result = CURLE_OK;
- struct IMAP *imap = data->req.p.imap;
-
- if(!imap->query) {
- failf(data, "Cannot SEARCH without a query string.");
- return CURLE_URL_MALFORMAT;
- }
-
- result = imap_sendf(data, "SEARCH %s", imap->query);
- if(!result)
- imap_state(data, IMAP_SEARCH);
- return result;
- }
- static CURLcode imap_perform_logout(struct Curl_easy *data)
- {
-
- CURLcode result = imap_sendf(data, "LOGOUT");
- if(!result)
- imap_state(data, IMAP_LOGOUT);
- return result;
- }
- static CURLcode imap_state_servergreet_resp(struct Curl_easy *data,
- int imapcode,
- imapstate instate)
- {
- struct connectdata *conn = data->conn;
- (void)instate;
- if(imapcode == IMAP_RESP_PREAUTH) {
-
- struct imap_conn *imapc = &conn->proto.imapc;
- imapc->preauth = TRUE;
- infof(data, "PREAUTH connection, already authenticated");
- }
- else if(imapcode != IMAP_RESP_OK) {
- failf(data, "Got unexpected imap-server response");
- return CURLE_WEIRD_SERVER_REPLY;
- }
- return imap_perform_capability(data, conn);
- }
- static CURLcode imap_state_capability_resp(struct Curl_easy *data,
- int imapcode,
- imapstate instate)
- {
- CURLcode result = CURLE_OK;
- struct connectdata *conn = data->conn;
- struct imap_conn *imapc = &conn->proto.imapc;
- const char *line = Curl_dyn_ptr(&imapc->pp.recvbuf);
- (void)instate;
-
- if(imapcode == '*') {
- line += 2;
-
- for(;;) {
- size_t wordlen;
- while(*line &&
- (*line == ' ' || *line == '\t' ||
- *line == '\r' || *line == '\n')) {
- line++;
- }
- if(!*line)
- break;
-
- for(wordlen = 0; line[wordlen] && line[wordlen] != ' ' &&
- line[wordlen] != '\t' && line[wordlen] != '\r' &&
- line[wordlen] != '\n';)
- wordlen++;
-
- if(wordlen == 8 && !memcmp(line, "STARTTLS", 8))
- imapc->tls_supported = TRUE;
-
- else if(wordlen == 13 && !memcmp(line, "LOGINDISABLED", 13))
- imapc->login_disabled = TRUE;
-
- else if(wordlen == 7 && !memcmp(line, "SASL-IR", 7))
- imapc->ir_supported = TRUE;
-
- else if(wordlen > 5 && !memcmp(line, "AUTH=", 5)) {
- size_t llen;
- unsigned short mechbit;
- line += 5;
- wordlen -= 5;
-
- mechbit = Curl_sasl_decode_mech(line, wordlen, &llen);
- if(mechbit && llen == wordlen)
- imapc->sasl.authmechs |= mechbit;
- }
- line += wordlen;
- }
- }
- else if(data->set.use_ssl && !Curl_conn_is_ssl(conn, FIRSTSOCKET)) {
-
- if(imapcode == IMAP_RESP_OK && imapc->tls_supported && !imapc->preauth) {
-
- result = imap_perform_starttls(data);
- }
- else if(data->set.use_ssl <= CURLUSESSL_TRY)
- result = imap_perform_authentication(data, conn);
- else {
- failf(data, "STARTTLS not available.");
- result = CURLE_USE_SSL_FAILED;
- }
- }
- else
- result = imap_perform_authentication(data, conn);
- return result;
- }
- static CURLcode imap_state_starttls_resp(struct Curl_easy *data,
- int imapcode,
- imapstate instate)
- {
- CURLcode result = CURLE_OK;
- struct connectdata *conn = data->conn;
- (void)instate;
-
- if(data->conn->proto.imapc.pp.overflow)
- return CURLE_WEIRD_SERVER_REPLY;
- if(imapcode != IMAP_RESP_OK) {
- if(data->set.use_ssl != CURLUSESSL_TRY) {
- failf(data, "STARTTLS denied");
- result = CURLE_USE_SSL_FAILED;
- }
- else
- result = imap_perform_authentication(data, conn);
- }
- else
- result = imap_perform_upgrade_tls(data, conn);
- return result;
- }
- static CURLcode imap_state_auth_resp(struct Curl_easy *data,
- struct connectdata *conn,
- int imapcode,
- imapstate instate)
- {
- CURLcode result = CURLE_OK;
- struct imap_conn *imapc = &conn->proto.imapc;
- saslprogress progress;
- (void)instate;
- result = Curl_sasl_continue(&imapc->sasl, data, imapcode, &progress);
- if(!result)
- switch(progress) {
- case SASL_DONE:
- imap_state(data, IMAP_STOP);
- break;
- case SASL_IDLE:
- if((!imapc->login_disabled) && (imapc->preftype & IMAP_TYPE_CLEARTEXT))
-
- result = imap_perform_login(data, conn);
- else {
- failf(data, "Authentication cancelled");
- result = CURLE_LOGIN_DENIED;
- }
- break;
- default:
- break;
- }
- return result;
- }
- static CURLcode imap_state_login_resp(struct Curl_easy *data,
- int imapcode,
- imapstate instate)
- {
- CURLcode result = CURLE_OK;
- (void)instate;
- if(imapcode != IMAP_RESP_OK) {
- failf(data, "Access denied. %c", imapcode);
- result = CURLE_LOGIN_DENIED;
- }
- else
-
- imap_state(data, IMAP_STOP);
- return result;
- }
- static CURLcode imap_state_listsearch_resp(struct Curl_easy *data,
- int imapcode,
- imapstate instate)
- {
- CURLcode result = CURLE_OK;
- char *line = Curl_dyn_ptr(&data->conn->proto.imapc.pp.recvbuf);
- size_t len = data->conn->proto.imapc.pp.nfinal;
- (void)instate;
- if(imapcode == '*')
- result = Curl_client_write(data, CLIENTWRITE_BODY, line, len);
- else if(imapcode != IMAP_RESP_OK)
- result = CURLE_QUOTE_ERROR;
- else
-
- imap_state(data, IMAP_STOP);
- return result;
- }
- static CURLcode imap_state_select_resp(struct Curl_easy *data, int imapcode,
- imapstate instate)
- {
- CURLcode result = CURLE_OK;
- struct connectdata *conn = data->conn;
- struct IMAP *imap = data->req.p.imap;
- struct imap_conn *imapc = &conn->proto.imapc;
- const char *line = Curl_dyn_ptr(&data->conn->proto.imapc.pp.recvbuf);
- (void)instate;
- if(imapcode == '*') {
-
- if(checkprefix("OK [UIDVALIDITY ", line + 2)) {
- size_t len = 0;
- const char *p = &line[2] + strlen("OK [UIDVALIDITY ");
- while((len < 20) && p[len] && ISDIGIT(p[len]))
- len++;
- if(len && (p[len] == ']')) {
- struct dynbuf uid;
- Curl_dyn_init(&uid, 20);
- if(Curl_dyn_addn(&uid, p, len))
- return CURLE_OUT_OF_MEMORY;
- Curl_safefree(imapc->mailbox_uidvalidity);
- imapc->mailbox_uidvalidity = Curl_dyn_ptr(&uid);
- }
- }
- }
- else if(imapcode == IMAP_RESP_OK) {
-
- if(imap->uidvalidity && imapc->mailbox_uidvalidity &&
- !strcasecompare(imap->uidvalidity, imapc->mailbox_uidvalidity)) {
- failf(data, "Mailbox UIDVALIDITY has changed");
- result = CURLE_REMOTE_FILE_NOT_FOUND;
- }
- else {
-
- DEBUGASSERT(!imapc->mailbox);
- imapc->mailbox = strdup(imap->mailbox);
- if(!imapc->mailbox)
- return CURLE_OUT_OF_MEMORY;
- if(imap->custom)
- result = imap_perform_list(data);
- else if(imap->query)
- result = imap_perform_search(data);
- else
- result = imap_perform_fetch(data);
- }
- }
- else {
- failf(data, "Select failed");
- result = CURLE_LOGIN_DENIED;
- }
- return result;
- }
- static CURLcode imap_state_fetch_resp(struct Curl_easy *data,
- struct connectdata *conn, int imapcode,
- imapstate instate)
- {
- CURLcode result = CURLE_OK;
- struct imap_conn *imapc = &conn->proto.imapc;
- struct pingpong *pp = &imapc->pp;
- const char *ptr = Curl_dyn_ptr(&data->conn->proto.imapc.pp.recvbuf);
- size_t len = data->conn->proto.imapc.pp.nfinal;
- bool parsed = FALSE;
- curl_off_t size = 0;
- (void)instate;
- if(imapcode != '*') {
- Curl_pgrsSetDownloadSize(data, -1);
- imap_state(data, IMAP_STOP);
- return CURLE_REMOTE_FILE_NOT_FOUND;
- }
-
- ptr = memchr(ptr, '{', len);
- if(ptr) {
- char *endptr;
- if(!curlx_strtoofft(ptr + 1, &endptr, 10, &size) &&
- (endptr - ptr > 1 && *endptr == '}'))
- parsed = TRUE;
- }
- if(parsed) {
- infof(data, "Found %" FMT_OFF_T " bytes to download", size);
- Curl_pgrsSetDownloadSize(data, size);
- if(pp->overflow) {
-
- size_t chunk = pp->overflow;
-
- Curl_dyn_tail(&pp->recvbuf, chunk);
- pp->nfinal = 0;
- if(chunk > (size_t)size)
-
- chunk = (size_t)size;
- if(!chunk) {
-
- imap_state(data, IMAP_STOP);
- return CURLE_OK;
- }
- result = Curl_client_write(data, CLIENTWRITE_BODY,
- Curl_dyn_ptr(&pp->recvbuf), chunk);
- if(result)
- return result;
- infof(data, "Written %zu bytes, %" FMT_OFF_TU
- " bytes are left for transfer", chunk, size - chunk);
-
- if(pp->overflow > chunk) {
-
- pp->overflow -= chunk;
- Curl_dyn_tail(&pp->recvbuf, pp->overflow);
- }
- else {
- pp->overflow = 0;
-
- Curl_dyn_reset(&pp->recvbuf);
- }
- }
- if(data->req.bytecount == size)
-
- Curl_xfer_setup_nop(data);
- else {
-
- data->req.maxdownload = size;
-
- data->state.select_bits = CURL_CSELECT_IN;
- Curl_xfer_setup1(data, CURL_XFER_RECV, size, FALSE);
- }
- }
- else {
-
- failf(data, "Failed to parse FETCH response.");
- result = CURLE_WEIRD_SERVER_REPLY;
- }
-
- imap_state(data, IMAP_STOP);
- return result;
- }
- static CURLcode imap_state_fetch_final_resp(struct Curl_easy *data,
- int imapcode,
- imapstate instate)
- {
- CURLcode result = CURLE_OK;
- (void)instate;
- if(imapcode != IMAP_RESP_OK)
- result = CURLE_WEIRD_SERVER_REPLY;
- else
-
- imap_state(data, IMAP_STOP);
- return result;
- }
- static CURLcode imap_state_append_resp(struct Curl_easy *data, int imapcode,
- imapstate instate)
- {
- CURLcode result = CURLE_OK;
- (void)instate;
- if(imapcode != '+') {
- result = CURLE_UPLOAD_FAILED;
- }
- else {
-
- Curl_pgrsSetUploadSize(data, data->state.infilesize);
-
- Curl_xfer_setup1(data, CURL_XFER_SEND, -1, FALSE);
-
- imap_state(data, IMAP_STOP);
- }
- return result;
- }
- static CURLcode imap_state_append_final_resp(struct Curl_easy *data,
- int imapcode,
- imapstate instate)
- {
- CURLcode result = CURLE_OK;
- (void)instate;
- if(imapcode != IMAP_RESP_OK)
- result = CURLE_UPLOAD_FAILED;
- else
-
- imap_state(data, IMAP_STOP);
- return result;
- }
- static CURLcode imap_statemachine(struct Curl_easy *data,
- struct connectdata *conn)
- {
- CURLcode result = CURLE_OK;
- int imapcode;
- struct imap_conn *imapc = &conn->proto.imapc;
- struct pingpong *pp = &imapc->pp;
- size_t nread = 0;
- (void)data;
-
- if(imapc->state == IMAP_UPGRADETLS)
- return imap_perform_upgrade_tls(data, conn);
-
- if(pp->sendleft)
- return Curl_pp_flushsend(data, pp);
- do {
-
- result = Curl_pp_readresp(data, FIRSTSOCKET, pp, &imapcode, &nread);
- if(result)
- return result;
-
- if(imapcode == -1)
- return CURLE_WEIRD_SERVER_REPLY;
- if(!imapcode)
- break;
-
- switch(imapc->state) {
- case IMAP_SERVERGREET:
- result = imap_state_servergreet_resp(data, imapcode, imapc->state);
- break;
- case IMAP_CAPABILITY:
- result = imap_state_capability_resp(data, imapcode, imapc->state);
- break;
- case IMAP_STARTTLS:
- result = imap_state_starttls_resp(data, imapcode, imapc->state);
- break;
- case IMAP_AUTHENTICATE:
- result = imap_state_auth_resp(data, conn, imapcode, imapc->state);
- break;
- case IMAP_LOGIN:
- result = imap_state_login_resp(data, imapcode, imapc->state);
- break;
- case IMAP_LIST:
- case IMAP_SEARCH:
- result = imap_state_listsearch_resp(data, imapcode, imapc->state);
- break;
- case IMAP_SELECT:
- result = imap_state_select_resp(data, imapcode, imapc->state);
- break;
- case IMAP_FETCH:
- result = imap_state_fetch_resp(data, conn, imapcode, imapc->state);
- break;
- case IMAP_FETCH_FINAL:
- result = imap_state_fetch_final_resp(data, imapcode, imapc->state);
- break;
- case IMAP_APPEND:
- result = imap_state_append_resp(data, imapcode, imapc->state);
- break;
- case IMAP_APPEND_FINAL:
- result = imap_state_append_final_resp(data, imapcode, imapc->state);
- break;
- case IMAP_LOGOUT:
- default:
-
- imap_state(data, IMAP_STOP);
- break;
- }
- } while(!result && imapc->state != IMAP_STOP && Curl_pp_moredata(pp));
- return result;
- }
- static CURLcode imap_multi_statemach(struct Curl_easy *data, bool *done)
- {
- CURLcode result = CURLE_OK;
- struct connectdata *conn = data->conn;
- struct imap_conn *imapc = &conn->proto.imapc;
- if((conn->handler->flags & PROTOPT_SSL) && !imapc->ssldone) {
- bool ssldone = FALSE;
- result = Curl_conn_connect(data, FIRSTSOCKET, FALSE, &ssldone);
- imapc->ssldone = ssldone;
- if(result || !ssldone)
- return result;
- }
- result = Curl_pp_statemach(data, &imapc->pp, FALSE, FALSE);
- *done = (imapc->state == IMAP_STOP);
- return result;
- }
- static CURLcode imap_block_statemach(struct Curl_easy *data,
- struct connectdata *conn,
- bool disconnecting)
- {
- CURLcode result = CURLE_OK;
- struct imap_conn *imapc = &conn->proto.imapc;
- while(imapc->state != IMAP_STOP && !result)
- result = Curl_pp_statemach(data, &imapc->pp, TRUE, disconnecting);
- return result;
- }
- static CURLcode imap_init(struct Curl_easy *data)
- {
- CURLcode result = CURLE_OK;
- struct IMAP *imap;
- imap = data->req.p.imap = calloc(1, sizeof(struct IMAP));
- if(!imap)
- result = CURLE_OUT_OF_MEMORY;
- return result;
- }
- static int imap_getsock(struct Curl_easy *data,
- struct connectdata *conn,
- curl_socket_t *socks)
- {
- return Curl_pp_getsock(data, &conn->proto.imapc.pp, socks);
- }
- static CURLcode imap_connect(struct Curl_easy *data, bool *done)
- {
- CURLcode result = CURLE_OK;
- struct connectdata *conn = data->conn;
- struct imap_conn *imapc = &conn->proto.imapc;
- struct pingpong *pp = &imapc->pp;
- *done = FALSE;
-
- connkeep(conn, "IMAP default");
- PINGPONG_SETUP(pp, imap_statemachine, imap_endofresp);
-
- imapc->preftype = IMAP_TYPE_ANY;
- Curl_sasl_init(&imapc->sasl, data, &saslimap);
- Curl_dyn_init(&imapc->dyn, DYN_IMAP_CMD);
- Curl_pp_init(pp);
-
- result = imap_parse_url_options(conn);
- if(result)
- return result;
-
- imap_state(data, IMAP_SERVERGREET);
-
- strcpy(imapc->resptag, "*");
- result = imap_multi_statemach(data, done);
- return result;
- }
- static CURLcode imap_done(struct Curl_easy *data, CURLcode status,
- bool premature)
- {
- CURLcode result = CURLE_OK;
- struct connectdata *conn = data->conn;
- struct IMAP *imap = data->req.p.imap;
- (void)premature;
- if(!imap)
- return CURLE_OK;
- if(status) {
- connclose(conn, "IMAP done with bad status");
- result = status;
- }
- else if(!data->set.connect_only && !imap->custom &&
- (imap->uid || imap->mindex || data->state.upload ||
- IS_MIME_POST(data))) {
-
- if(!data->state.upload && !IS_MIME_POST(data))
- imap_state(data, IMAP_FETCH_FINAL);
- else {
-
- result = Curl_pp_sendf(data, &conn->proto.imapc.pp, "%s", "");
- if(!result)
- imap_state(data, IMAP_APPEND_FINAL);
- }
-
- if(!result)
- result = imap_block_statemach(data, conn, FALSE);
- }
-
- Curl_safefree(imap->mailbox);
- Curl_safefree(imap->uidvalidity);
- Curl_safefree(imap->uid);
- Curl_safefree(imap->mindex);
- Curl_safefree(imap->section);
- Curl_safefree(imap->partial);
- Curl_safefree(imap->query);
- Curl_safefree(imap->custom);
- Curl_safefree(imap->custom_params);
-
- imap->transfer = PPTRANSFER_BODY;
- return result;
- }
- static CURLcode imap_perform(struct Curl_easy *data, bool *connected,
- bool *dophase_done)
- {
-
- CURLcode result = CURLE_OK;
- struct connectdata *conn = data->conn;
- struct IMAP *imap = data->req.p.imap;
- struct imap_conn *imapc = &conn->proto.imapc;
- bool selected = FALSE;
- DEBUGF(infof(data, "DO phase starts"));
- if(data->req.no_body) {
-
- imap->transfer = PPTRANSFER_INFO;
- }
- *dophase_done = FALSE;
-
- if(imap->mailbox && imapc->mailbox &&
- strcasecompare(imap->mailbox, imapc->mailbox) &&
- (!imap->uidvalidity || !imapc->mailbox_uidvalidity ||
- strcasecompare(imap->uidvalidity, imapc->mailbox_uidvalidity)))
- selected = TRUE;
-
- if(data->state.upload || IS_MIME_POST(data))
-
- result = imap_perform_append(data);
- else if(imap->custom && (selected || !imap->mailbox))
-
- result = imap_perform_list(data);
- else if(!imap->custom && selected && (imap->uid || imap->mindex))
-
- result = imap_perform_fetch(data);
- else if(!imap->custom && selected && imap->query)
-
- result = imap_perform_search(data);
- else if(imap->mailbox && !selected &&
- (imap->custom || imap->uid || imap->mindex || imap->query))
-
- result = imap_perform_select(data);
- else
-
- result = imap_perform_list(data);
- if(result)
- return result;
-
- result = imap_multi_statemach(data, dophase_done);
- *connected = Curl_conn_is_connected(conn, FIRSTSOCKET);
- if(*dophase_done)
- DEBUGF(infof(data, "DO phase is complete"));
- return result;
- }
- static CURLcode imap_do(struct Curl_easy *data, bool *done)
- {
- CURLcode result = CURLE_OK;
- *done = FALSE;
-
- result = imap_parse_url_path(data);
- if(result)
- return result;
-
- result = imap_parse_custom_request(data);
- if(result)
- return result;
- result = imap_regular_transfer(data, done);
- return result;
- }
- static CURLcode imap_disconnect(struct Curl_easy *data,
- struct connectdata *conn, bool dead_connection)
- {
- struct imap_conn *imapc = &conn->proto.imapc;
- (void)data;
-
-
- if(!dead_connection && conn->bits.protoconnstart) {
- if(!imap_perform_logout(data))
- (void)imap_block_statemach(data, conn, TRUE);
- }
-
- Curl_pp_disconnect(&imapc->pp);
- Curl_dyn_free(&imapc->dyn);
-
- Curl_sasl_cleanup(conn, imapc->sasl.authused);
-
- Curl_safefree(imapc->mailbox);
- Curl_safefree(imapc->mailbox_uidvalidity);
- return CURLE_OK;
- }
- static CURLcode imap_dophase_done(struct Curl_easy *data, bool connected)
- {
- struct IMAP *imap = data->req.p.imap;
- (void)connected;
- if(imap->transfer != PPTRANSFER_BODY)
-
- Curl_xfer_setup_nop(data);
- return CURLE_OK;
- }
- static CURLcode imap_doing(struct Curl_easy *data, bool *dophase_done)
- {
- CURLcode result = imap_multi_statemach(data, dophase_done);
- if(result)
- DEBUGF(infof(data, "DO phase failed"));
- else if(*dophase_done) {
- result = imap_dophase_done(data, FALSE );
- DEBUGF(infof(data, "DO phase is complete"));
- }
- return result;
- }
- static CURLcode imap_regular_transfer(struct Curl_easy *data,
- bool *dophase_done)
- {
- CURLcode result = CURLE_OK;
- bool connected = FALSE;
-
- data->req.size = -1;
-
- Curl_pgrsSetUploadCounter(data, 0);
- Curl_pgrsSetDownloadCounter(data, 0);
- Curl_pgrsSetUploadSize(data, -1);
- Curl_pgrsSetDownloadSize(data, -1);
-
- result = imap_perform(data, &connected, dophase_done);
-
- if(!result && *dophase_done)
- result = imap_dophase_done(data, connected);
- return result;
- }
- static CURLcode imap_setup_connection(struct Curl_easy *data,
- struct connectdata *conn)
- {
-
- CURLcode result = imap_init(data);
- if(result)
- return result;
-
- conn->bits.tls_upgraded = FALSE;
- return CURLE_OK;
- }
- static CURLcode imap_sendf(struct Curl_easy *data, const char *fmt, ...)
- {
- CURLcode result = CURLE_OK;
- struct imap_conn *imapc = &data->conn->proto.imapc;
- DEBUGASSERT(fmt);
-
- msnprintf(imapc->resptag, sizeof(imapc->resptag), "%c%03d",
- 'A' + curlx_sltosi((long)(data->conn->connection_id % 26)),
- ++imapc->cmdid);
-
- Curl_dyn_reset(&imapc->dyn);
-
- result = Curl_dyn_addf(&imapc->dyn, "%s %s", imapc->resptag, fmt);
- if(!result) {
- va_list ap;
- va_start(ap, fmt);
- #ifdef __clang__
- #pragma clang diagnostic push
- #pragma clang diagnostic ignored "-Wformat-nonliteral"
- #endif
- result = Curl_pp_vsendf(data, &imapc->pp, Curl_dyn_ptr(&imapc->dyn), ap);
- #ifdef __clang__
- #pragma clang diagnostic pop
- #endif
- va_end(ap);
- }
- return result;
- }
- static char *imap_atom(const char *str, bool escape_only)
- {
- struct dynbuf line;
- size_t nclean;
- size_t len;
- if(!str)
- return NULL;
- len = strlen(str);
- nclean = strcspn(str, "() {%*]\\\"");
- if(len == nclean)
-
- return strdup(str);
- Curl_dyn_init(&line, 2000);
- if(!escape_only && Curl_dyn_addn(&line, "\"", 1))
- return NULL;
- while(*str) {
- if((*str == '\\' || *str == '"') &&
- Curl_dyn_addn(&line, "\\", 1))
- return NULL;
- if(Curl_dyn_addn(&line, str, 1))
- return NULL;
- str++;
- }
- if(!escape_only && Curl_dyn_addn(&line, "\"", 1))
- return NULL;
- return Curl_dyn_ptr(&line);
- }
- static bool imap_is_bchar(char ch)
- {
-
- if(ISALNUM(ch))
- return TRUE;
- switch(ch) {
-
- case ':': case '@': case '/':
-
- case '&': case '=':
-
- case '-': case '.': case '_': case '~':
-
- case '!': case '$': case '\'': case '(': case ')': case '*':
- case '+': case ',':
-
- case '%':
- return TRUE;
- default:
- return FALSE;
- }
- }
- static CURLcode imap_parse_url_options(struct connectdata *conn)
- {
- CURLcode result = CURLE_OK;
- struct imap_conn *imapc = &conn->proto.imapc;
- const char *ptr = conn->options;
- bool prefer_login = FALSE;
- while(!result && ptr && *ptr) {
- const char *key = ptr;
- const char *value;
- while(*ptr && *ptr != '=')
- ptr++;
- value = ptr + 1;
- while(*ptr && *ptr != ';')
- ptr++;
- if(strncasecompare(key, "AUTH=+LOGIN", 11)) {
-
- prefer_login = TRUE;
- imapc->sasl.prefmech = SASL_AUTH_NONE;
- }
- else if(strncasecompare(key, "AUTH=", 5)) {
- prefer_login = FALSE;
- result = Curl_sasl_parse_url_auth_option(&imapc->sasl,
- value, ptr - value);
- }
- else {
- prefer_login = FALSE;
- result = CURLE_URL_MALFORMAT;
- }
- if(*ptr == ';')
- ptr++;
- }
- if(prefer_login)
- imapc->preftype = IMAP_TYPE_CLEARTEXT;
- else {
- switch(imapc->sasl.prefmech) {
- case SASL_AUTH_NONE:
- imapc->preftype = IMAP_TYPE_NONE;
- break;
- case SASL_AUTH_DEFAULT:
- imapc->preftype = IMAP_TYPE_ANY;
- break;
- default:
- imapc->preftype = IMAP_TYPE_SASL;
- break;
- }
- }
- return result;
- }
- static CURLcode imap_parse_url_path(struct Curl_easy *data)
- {
-
- CURLcode result = CURLE_OK;
- struct IMAP *imap = data->req.p.imap;
- const char *begin = &data->state.up.path[1];
- const char *ptr = begin;
-
- while(imap_is_bchar(*ptr))
- ptr++;
- if(ptr != begin) {
-
- const char *end = ptr;
- if(end > begin && end[-1] == '/')
- end--;
- result = Curl_urldecode(begin, end - begin, &imap->mailbox, NULL,
- REJECT_CTRL);
- if(result)
- return result;
- }
- else
- imap->mailbox = NULL;
-
- while(*ptr == ';') {
- char *name;
- char *value;
- size_t valuelen;
-
- begin = ++ptr;
- while(*ptr && *ptr != '=')
- ptr++;
- if(!*ptr)
- return CURLE_URL_MALFORMAT;
-
- result = Curl_urldecode(begin, ptr - begin, &name, NULL,
- REJECT_CTRL);
- if(result)
- return result;
-
- begin = ++ptr;
- while(imap_is_bchar(*ptr))
- ptr++;
-
- result = Curl_urldecode(begin, ptr - begin, &value, &valuelen,
- REJECT_CTRL);
- if(result) {
- free(name);
- return result;
- }
- DEBUGF(infof(data, "IMAP URL parameter '%s' = '%s'", name, value));
-
- if(strcasecompare(name, "UIDVALIDITY") && !imap->uidvalidity) {
- if(valuelen > 0 && value[valuelen - 1] == '/')
- value[valuelen - 1] = '\0';
- imap->uidvalidity = value;
- value = NULL;
- }
- else if(strcasecompare(name, "UID") && !imap->uid) {
- if(valuelen > 0 && value[valuelen - 1] == '/')
- value[valuelen - 1] = '\0';
- imap->uid = value;
- value = NULL;
- }
- else if(strcasecompare(name, "MAILINDEX") && !imap->mindex) {
- if(valuelen > 0 && value[valuelen - 1] == '/')
- value[valuelen - 1] = '\0';
- imap->mindex = value;
- value = NULL;
- }
- else if(strcasecompare(name, "SECTION") && !imap->section) {
- if(valuelen > 0 && value[valuelen - 1] == '/')
- value[valuelen - 1] = '\0';
- imap->section = value;
- value = NULL;
- }
- else if(strcasecompare(name, "PARTIAL") && !imap->partial) {
- if(valuelen > 0 && value[valuelen - 1] == '/')
- value[valuelen - 1] = '\0';
- imap->partial = value;
- value = NULL;
- }
- else {
- free(name);
- free(value);
- return CURLE_URL_MALFORMAT;
- }
- free(name);
- free(value);
- }
-
- if(imap->mailbox && !imap->uid && !imap->mindex) {
-
- (void)curl_url_get(data->state.uh, CURLUPART_QUERY, &imap->query,
- CURLU_URLDECODE);
- }
-
- if(*ptr)
- return CURLE_URL_MALFORMAT;
- return CURLE_OK;
- }
- static CURLcode imap_parse_custom_request(struct Curl_easy *data)
- {
- CURLcode result = CURLE_OK;
- struct IMAP *imap = data->req.p.imap;
- const char *custom = data->set.str[STRING_CUSTOMREQUEST];
- if(custom) {
-
- result = Curl_urldecode(custom, 0, &imap->custom, NULL, REJECT_CTRL);
-
- if(!result) {
- const char *params = imap->custom;
- while(*params && *params != ' ')
- params++;
- if(*params) {
- imap->custom_params = strdup(params);
- imap->custom[params - imap->custom] = '\0';
- if(!imap->custom_params)
- result = CURLE_OUT_OF_MEMORY;
- }
- }
- }
- return result;
- }
- #endif
|