| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346 |
- /**
- * @file
- * @brief SMTP client library.
- * @author James Humphrey (mail@somnisoft.com)
- * @version 1.00
- *
- * This SMTP client library allows the user to send emails to an SMTP server.
- * The user can include custom headers and MIME attachments.
- *
- * This software has been placed into the public domain using CC0.
- */
- /**
- * @mainpage smtp-client
- *
- * This section contains documentation generated directly from the source
- * code.
- *
- * To view the repository details, visit the main smtp-client page at
- * <a href='https://www.somnisoft.com/smtp-client'>
- * www.somnisoft.com/smtp-client
- * </a>.
- */
- #include "cfg.h"
- #ifdef USE_SMTP
- #include <sys/types.h>
- #include <stddef.h>
- #include <stdio.h>
- #define SMTP_OPENSSL
- #ifndef SIZE_MAX
- /**
- * Maximum value of size_t type.
- */
- # define SIZE_MAX ((size_t)(-1))
- #endif /* SIZE_MAX */
- /**
- * Status codes indicating success or failure from calling any of the
- * SMTP library functions.
- *
- * This code gets returned by all functions in this header.
- */
- enum smtp_status_code{
- /**
- * Successful operation completed.
- */
- SMTP_STATUS_OK = 0,
- /**
- * Memory allocation failed.
- */
- SMTP_STATUS_NOMEM = 1,
- /**
- * Failed to connect to the mail server.
- */
- SMTP_STATUS_CONNECT = 2,
- /**
- * Failed to handshake or negotiate a TLS connection with the server.
- */
- SMTP_STATUS_HANDSHAKE = 3,
- /**
- * Failed to authenticate with the given credentials.
- */
- SMTP_STATUS_AUTH = 4,
- /**
- * Failed to send bytes to the server.
- */
- SMTP_STATUS_SEND = 5,
- /**
- * Failed to receive bytes from the server.
- */
- SMTP_STATUS_RECV = 6,
- /**
- * Failed to properly close a connection.
- */
- SMTP_STATUS_CLOSE = 7,
- /**
- * SMTP server sent back an unexpected status code.
- */
- SMTP_STATUS_SERVER_RESPONSE = 8,
- /**
- * Invalid parameter.
- */
- SMTP_STATUS_PARAM = 9,
- /**
- * Failed to open or read a local file.
- */
- SMTP_STATUS_FILE = 10,
- /**
- * Failed to get the local date and time.
- */
- SMTP_STATUS_DATE = 11,
- /**
- * Indicates the last status code in the enumeration, useful for
- * bounds checking.
- *
- * Not a valid status code.
- */
- SMTP_STATUS__LAST
- };
- /**
- * Address source and destination types.
- */
- enum smtp_address_type{
- /**
- * From address.
- */
- SMTP_ADDRESS_FROM = 0,
- /**
- * To address.
- */
- SMTP_ADDRESS_TO = 1,
- /**
- * Copy address.
- */
- SMTP_ADDRESS_CC = 2,
- /**
- * Blind copy address.
- *
- * Recipients should not see any of the BCC addresses when they receive
- * their email. However, some SMTP server implementations may copy this
- * information into the mail header, so do not assume that this will
- * always get hidden. If the BCC addresses must not get shown to the
- * receivers, then send one separate email to each BCC party and add
- * the TO and CC addresses manually as a header property using
- * @ref smtp_header_add instead of as an address using
- * @ref smtp_address_add.
- */
- SMTP_ADDRESS_BCC = 3
- };
- /**
- * Connect to the SMTP server using either an unencrypted socket or
- * TLS encryption.
- */
- enum smtp_connection_security{
- #ifdef SMTP_OPENSSL
- /**
- * First connect without encryption, then negotiate an encrypted connection
- * by issuing a STARTTLS command.
- *
- * Typically used when connecting to a mail server on port 25 or 587.
- */
- SMTP_SECURITY_STARTTLS = 0,
- /**
- * Use TLS when initially connecting to server.
- *
- * Typically used when connecting to a mail server on port 465.
- */
- SMTP_SECURITY_TLS = 1,
- #endif /* SMTP_OPENSSL */
- /**
- * Do not use TLS encryption.
- *
- * Not recommended unless connecting to the SMTP server locally.
- */
- SMTP_SECURITY_NONE = 2
- };
- /**
- * List of supported methods for authenticating a mail user account on
- * the server.
- */
- enum smtp_authentication_method{
- #ifdef SMTP_OPENSSL
- /**
- * Use HMAC-MD5.
- */
- SMTP_AUTH_CRAM_MD5 = 0,
- #endif /* SMTP_OPENSSL */
- /**
- * No authentication required.
- *
- * Some servers support this option if connecting locally.
- */
- SMTP_AUTH_NONE = 1,
- /**
- * Authenticate using base64 user and password.
- */
- SMTP_AUTH_PLAIN = 2,
- /**
- * Another base64 authentication method, similar to SMTP_AUTH_PLAIN.
- */
- SMTP_AUTH_LOGIN = 3
- };
- /**
- * Special flags defining certain behaviors for the SMTP client context.
- */
- enum smtp_flag{
- /**
- * Print client and server communication on stderr.
- */
- SMTP_DEBUG = 1 << 0,
- /**
- * Do not verify TLS certificate.
- *
- * By default, the TLS handshake function will check if a certificate
- * has expired or if using a self-signed certificate. Either of those
- * conditions will cause the connection to fail. This option allows the
- * connection to proceed even if those checks fail.
- */
- SMTP_NO_CERT_VERIFY = 1 << 1
- };
- /**
- * SMTP codes returned by the server and parsed by the client.
- */
- enum smtp_result_code{
- /**
- * Client error code which does not get set by the server.
- */
- SMTP_INTERNAL_ERROR = -1,
- /**
- * Returned when ready to begin processing next step.
- */
- SMTP_READY = 220,
- /**
- * Returned in response to QUIT.
- */
- SMTP_CLOSE = 221,
- /**
- * Returned if client successfully authenticates.
- */
- SMTP_AUTH_SUCCESS = 235,
- /**
- * Returned when some commands successfully complete.
- */
- SMTP_DONE = 250,
- /**
- * Returned for some multi-line authentication mechanisms where this code
- * indicates the next stage in the authentication step.
- */
- SMTP_AUTH_CONTINUE = 334,
- /**
- * Returned in response to DATA command.
- */
- SMTP_BEGIN_MAIL = 354
- };
- /**
- * Used for parsing out the responses from the SMTP server.
- *
- * For example, if the server sends back '250-STARTTLS', then code would
- * get set to 250, more would get set to 1, and text would get set to STARTTLS.
- */
- struct smtp_command{
- /**
- * Result code converted to an integer.
- */
- enum smtp_result_code code;
- /**
- * Indicates if more server commands follow.
- *
- * This will get set to 1 if the fourth character in the response line
- * contains a '-', otherwise this will get set to 0.
- */
- int more;
- /**
- * The text shown after the status code.
- */
- const char *text;
- };
- /**
- * Return codes for the getdelim interface which allows the caller to check
- * if more delimited lines can get processed.
- */
- enum str_getdelim_retcode{
- /**
- * An error occurred during the getdelim processing.
- */
- STRING_GETDELIMFD_ERROR = -1,
- /**
- * Found a new line and can process more lines in the next call.
- */
- STRING_GETDELIMFD_NEXT = 0,
- /**
- * Found a new line and unable to read any more lines at this time.
- */
- STRING_GETDELIMFD_DONE = 1
- };
- /**
- * Data structure for read buffer and line parsing.
- *
- * This assists with getting and parsing the server response lines.
- */
- struct str_getdelimfd{
- /**
- * Read buffer which may include bytes past the delimiter.
- */
- char *_buf;
- /**
- * Number of allocated bytes in the read buffer.
- */
- size_t _bufsz;
- /**
- * Number of actual stored bytes in the read buffer.
- */
- size_t _buf_len;
- /**
- * Current line containing the text up to the delimiter.
- */
- char *line;
- /**
- * Number of stored bytes in the line buffer.
- */
- size_t line_len;
- /**
- * Function pointer to a custom read function for the
- * @ref smtp_str_getdelimfd interface.
- *
- * This function prototype has similar semantics to the read function.
- * The @p gdfd parameter allows the custom function to pull the user_data
- * info from the @ref str_getdelimfd struct which can contain file pointer,
- * socket connection, etc.
- */
- long (*getdelimfd_read)(struct str_getdelimfd *const gdfd,
- void *buf,
- size_t count);
- /**
- * User data which gets sent to the read handler function.
- */
- void *user_data;
- /**
- * Character delimiter used for determining line separation.
- */
- int delim;
- /**
- * Padding structure to align.
- */
- char pad[4];
- };
- struct smtp;
- enum smtp_status_code
- smtp_open(const char *const server,
- const char *const port,
- enum smtp_connection_security connection_security,
- enum smtp_flag flags,
- const char *const cafile,
- struct smtp **smtp);
- enum smtp_status_code
- smtp_auth(struct smtp *const smtp,
- enum smtp_authentication_method auth_method,
- const char *const user,
- const char *const pass);
- enum smtp_status_code
- smtp_mail(struct smtp *const smtp,
- const char *const body);
- enum smtp_status_code
- smtp_close(struct smtp *smtp);
- enum smtp_status_code
- smtp_status_code_get(const struct smtp *const smtp);
- enum smtp_status_code
- smtp_status_code_clear(struct smtp *const smtp);
- enum smtp_status_code
- smtp_status_code_set(struct smtp *const smtp,
- enum smtp_status_code new_status_code);
- const char *
- smtp_status_code_errstr(enum smtp_status_code status_code);
- enum smtp_status_code
- smtp_header_add(struct smtp *const smtp,
- const char *const key,
- const char *const value);
- void
- smtp_header_clear_all(struct smtp *const smtp);
- enum smtp_status_code
- smtp_address_add(struct smtp *const smtp,
- enum smtp_address_type type,
- const char *const email,
- const char *const name);
- void
- smtp_address_clear_all(struct smtp *const smtp);
- enum smtp_status_code
- smtp_attachment_add_path(struct smtp *const smtp,
- const char *const name,
- const char *const path);
- enum smtp_status_code
- smtp_attachment_add_fp(struct smtp *const smtp,
- const char *const name,
- FILE *fp);
- enum smtp_status_code
- smtp_attachment_add_mem(struct smtp *const smtp,
- const char *const name,
- const void *const data,
- size_t datasz);
- void smtp_attachment_clear_all(struct smtp *const smtp);
- //////////////////////////////////////////////////////////////////////
- #if defined(_WIN32) || defined(WIN32)
- # define SMTP_IS_WINDOWS
- #endif /* SMTP_IS_WINDOWS */
- #ifdef SMTP_IS_WINDOWS
- # include <winsock2.h>
- # include <ws2tcpip.h>
- #else /* POSIX */
- # include <netinet/in.h>
- # include <sys/select.h>
- # include <sys/socket.h>
- # include <netdb.h>
- # include <unistd.h>
- #endif /* SMTP_IS_WINDOWS */
- #include <errno.h>
- #include <limits.h>
- #include <signal.h>
- #include <stdarg.h>
- #include <string.h>
- #include <stdlib.h>
- #include <time.h>
- #ifdef SMTP_OPENSSL
- # include <openssl/bio.h>
- # include <openssl/err.h>
- # include <openssl/ssl.h>
- # include <openssl/x509.h>
- # include <openssl/x509v3.h>
- #endif /* SMTP_OPENSSL */
- /**
- * Get access to the @ref smtp_result_code and @ref smtp_command definitions.
- */
- #define SMTP_INTERNAL_DEFINE
- #include "smtp.h"
- /*
- * The SMTP_TEST converts some library routines into special test seams which
- * allows the test program to control whether they fail. For example, we can
- * control when malloc() fails under certain conditions with an out of
- * memory condition.
- */
- #ifdef SMTP_TEST
- /**
- * Declare extern linkage on some functions so we can redefine their behavior
- * in the external test suite.
- */
- # define SMTP_LINKAGE extern
- # include "../test/seams.h"
- #else /* !(SMTP_TEST) */
- /**
- * When not testing, all functions should have static linkage except for those
- * in the header.
- */
- # define SMTP_LINKAGE static
- #endif /* SMTP_TEST */
- /**
- * Increment the read buffer size by this amount if the delimiter
- * has not been found.
- */
- #define SMTP_GETDELIM_READ_SZ 1000
- /**
- * Stores source and destination email addresses.
- */
- struct smtp_address{
- /**
- * Email address without any special formatting.
- *
- * For example: mail@example.com
- */
- char *email;
- /**
- * Description of the email address.
- */
- char *name;
- /**
- * Specify from, to, cc, bcc.
- */
- enum smtp_address_type type;
- /**
- * Padding structure to align.
- */
- char pad[4];
- };
- /**
- * Attachment data which gets placed in the MIME email section.
- */
- struct smtp_attachment{
- /**
- * File name of the attachment.
- */
- char *name;
- /**
- * Base64-encoded file data.
- */
- char *b64_data;
- };
- /**
- * List of email headers to send before the mail body.
- */
- struct smtp_header{
- /**
- * Header name which will get sorted alphabetically in the header list.
- */
- char *key;
- /**
- * Content of the corresponding header key.
- */
- char *value;
- };
- /**
- * Main data structure that holds the SMTP client context.
- */
- struct smtp{
- /**
- * Bitwise list of flags controlling the behavior of this SMTP client.
- */
- enum smtp_flag flags;
- /**
- * Standard network socket connection.
- */
- int sock;
- /**
- * Read buffer and line parsing structure.
- */
- struct str_getdelimfd gdfd;
- /**
- * List of headers to print before the mail body.
- */
- struct smtp_header *header_list;
- /**
- * Number of headers in header_list.
- */
- size_t num_headers;
- /**
- * List of from, to, cc, and bcc email addresses.
- */
- struct smtp_address *address_list;
- /**
- * Number of addresses in address_list.
- */
- size_t num_address;
- /**
- * List of attachments to send.
- */
- struct smtp_attachment *attachment_list;
- /**
- * Number of attachments in attachment_list.
- */
- size_t num_attachment;
- /**
- * Timeout in seconds to wait before returning with an error.
- *
- * This applies to both writing to and reading from a network socket.
- */
- long timeout_sec;
- /**
- * Status code indicating success/failure.
- *
- * This code gets returned by most of the header functions.
- */
- enum smtp_status_code status_code;
- /**
- * Indicates if this context has an active TLS connection.
- * - Set to 0 if TLS connection inactive.
- * - Set to 1 if TLS connection currently active.
- */
- int tls_on;
- /**
- * Path to certificate file if using self-signed or untrusted certificate
- * not in the default key store.
- */
- const char *cafile;
- #ifdef SMTP_OPENSSL
- /**
- * OpenSSL TLS object.
- */
- SSL *tls;
- /**
- * OpenSSL TLS context.
- */
- SSL_CTX *tls_ctx;
- /**
- * OpenSSL TLS I/O abstraction.
- */
- BIO *tls_bio;
- #endif /* SMTP_OPENSSL */
- };
- /**
- * Check if adding a size_t value will cause a wrap.
- *
- * @param[in] a Add this value with @p b.
- * @param[in] b Add this value with @p a.
- * @param[out] result Save the addition to this buffer. Does not
- * perform the addition if set to NULL.
- * @retval 1 Value wrapped.
- * @retval 0 Value did not wrap.
- */
- SMTP_LINKAGE int
- smtp_si_add_size_t(const size_t a,
- const size_t b,
- size_t *const result){
- int wraps;
- #ifdef SMTP_TEST
- if(smtp_test_seam_dec_err_ctr(&g_smtp_test_err_si_add_size_t_ctr)){
- return 1;
- }
- #endif /* SMTP_TEST */
- if(SIZE_MAX - a < b){
- wraps = 1;
- }
- else{
- wraps = 0;
- }
- if(result){
- *result = a + b;
- }
- return wraps;
- }
- /**
- * Check if subtracting a size_t value will cause wrap.
- *
- * @param[in] a Subtract this value by @p b.
- * @param[in] b Subtract this value from @p a.
- * @param[out] result Save the subtraction to this buffer. Does not
- * perform the subtraction if set to NULL.
- * @retval 1 Value wrapped.
- * @retval 0 Value did not wrap.
- */
- SMTP_LINKAGE int
- smtp_si_sub_size_t(const size_t a,
- const size_t b,
- size_t *const result){
- int wraps;
- #ifdef SMTP_TEST
- if(smtp_test_seam_dec_err_ctr(&g_smtp_test_err_si_sub_size_t_ctr)){
- return 1;
- }
- #endif /* SMTP_TEST */
- if(a < b){
- wraps = 1;
- }
- else{
- wraps = 0;
- }
- if(result){
- *result = a - b;
- }
- return wraps;
- }
- /**
- * Check if multiplying a size_t value will cause a wrap.
- *
- * @param[in] a Multiply this value with @p b.
- * @param[in] b Multiply this value with @p a.
- * @param[out] result Save the multiplication to this buffer. Does not
- * perform the multiplication if set to NULL.
- * @retval 1 Value wrapped.
- * @retval 0 Value did not wrap.
- */
- SMTP_LINKAGE int
- smtp_si_mul_size_t(const size_t a,
- const size_t b,
- size_t *const result){
- int wraps;
- #ifdef SMTP_TEST
- if(smtp_test_seam_dec_err_ctr(&g_smtp_test_err_si_mul_size_t_ctr)){
- return 1;
- }
- #endif /* SMTP_TEST */
- if(b != 0 && a > SIZE_MAX / b){
- wraps = 1;
- }
- else{
- wraps = 0;
- }
- if(result){
- *result = a * b;
- }
- return wraps;
- }
- /**
- * Wait until more data has been made available on the socket read end.
- *
- * @param[in] smtp SMTP client context.
- * @retval SMTP_STATUS_OK If data available to read on the socket.
- * @retval SMTP_STATUS_RECV If the connection times out before any data
- * appears on the socket.
- */
- static enum smtp_status_code
- smtp_str_getdelimfd_read_timeout(struct smtp *const smtp){
- fd_set readfds;
- struct timeval timeout;
- int sel_rc;
- FD_ZERO(&readfds);
- FD_SET(smtp->sock, &readfds);
- timeout.tv_sec = smtp->timeout_sec;
- timeout.tv_usec = 0;
- sel_rc = select(smtp->sock + 1, &readfds, NULL, NULL, &timeout);
- if(sel_rc < 1){
- return smtp_status_code_set(smtp, SMTP_STATUS_RECV);
- }
- return SMTP_STATUS_OK;
- }
- /**
- * This function gets called by the @ref smtp_str_getdelimfd interface when it
- * needs to read in more data.
- *
- * It reads using either the plain socket connection if encryption not
- * enabled, or it reads using OpenSSL if it has an active TLS connection.
- *
- * @param[in] gdfd See @ref str_getdelimfd.
- * @param[out] buf Pointer to buffer for storing bytes read.
- * @param[in] count Maximum number of bytes to try reading.
- * @retval >=0 Number of bytes read.
- * @retval -1 Failed to read from the socket.
- */
- static long
- smtp_str_getdelimfd_read(struct str_getdelimfd *const gdfd,
- void *buf,
- size_t count){
- struct smtp *smtp;
- long bytes_read;
- smtp = gdfd->user_data;
- if(smtp_str_getdelimfd_read_timeout(smtp) != SMTP_STATUS_OK){
- return -1;
- }
- bytes_read = 0;
- if(smtp->tls_on){
- #ifdef SMTP_OPENSSL
- do{
- /*
- * Count will never have a value greater than SMTP_GETDELIM_READ_SZ,
- * so we can safely convert this to an int.
- */
- bytes_read = SSL_read(smtp->tls, buf, (int)count);
- } while(bytes_read <= 0 && BIO_should_retry(smtp->tls_bio));
- #endif /* SMTP_OPENSSL */
- }
- else{
- bytes_read = recv(smtp->sock, buf, count, 0);
- }
- return bytes_read;
- }
- /**
- * Find and return the location of the delimiter character in the
- * search buffer.
- *
- * This function gets used by the main socket parsing function which
- * continually reads from the socket and expands the buffer until it
- * encounters the expected delimiter. This function provides the logic
- * to check for the delimiter character in order to simplify the code
- * in the main parse function.
- *
- * @param[in] buf Search buffer used to find the delimiter.
- * @param[in] buf_len Number of bytes to search for in buf.
- * @param[in] delim The delimiter to search for in buf.
- * @param[out] delim_pos If delimiter found in buf, return the delimiter
- * position in this parameter.
- * @retval 1 If the delimiter character found.
- * @retval 0 If the delimiter character not found.
- */
- static int
- smtp_str_getdelimfd_search_delim(const char *const buf,
- size_t buf_len,
- int delim,
- size_t *const delim_pos){
- size_t i;
- *delim_pos = 0;
- for(i = 0; i < buf_len; i++){
- if(buf[i] == delim){
- *delim_pos = i;
- return 1;
- }
- }
- return 0;
- }
- /**
- * Set the internal line buffer to the number of bytes specified.
- *
- * @param[in] gdfd See @ref str_getdelimfd.
- * @param[in] copy_len Number of bytes to copy to the internal line buffer.
- * @retval 0 Successfully allocated and copied data over to the new
- * line buffer.
- * @retval -1 Failed to allocate memory for the new line buffer.
- */
- SMTP_LINKAGE int
- smtp_str_getdelimfd_set_line_and_buf(struct str_getdelimfd *const gdfd,
- size_t copy_len){
- size_t copy_len_inc;
- size_t nbytes_to_shift;
- size_t new_buf_len;
- if(gdfd->line){
- free(gdfd->line);
- gdfd->line = NULL;
- }
- if(smtp_si_add_size_t(copy_len, 1, ©_len_inc) ||
- smtp_si_add_size_t((size_t)gdfd->_buf, copy_len_inc, NULL) ||
- smtp_si_sub_size_t(gdfd->_buf_len, copy_len, &nbytes_to_shift) ||
- (gdfd->line = calloc(1, copy_len_inc)) == NULL){
- return -1;
- }
- memcpy(gdfd->line, gdfd->_buf, copy_len);
- gdfd->line_len = copy_len;
- memmove(gdfd->_buf, gdfd->_buf + copy_len_inc, nbytes_to_shift);
- if(smtp_si_sub_size_t(nbytes_to_shift, 1, &new_buf_len) == 0){
- gdfd->_buf_len = new_buf_len;
- }
- return 0;
- }
- /**
- * Free memory in the @ref str_getdelimfd data structure.
- *
- * @param[in] gdfd Frees memory stored in this socket parsing structure.
- */
- SMTP_LINKAGE void
- smtp_str_getdelimfd_free(struct str_getdelimfd *const gdfd){
- free(gdfd->_buf);
- free(gdfd->line);
- gdfd->_buf = NULL;
- gdfd->_bufsz = 0;
- gdfd->_buf_len = 0;
- gdfd->line = NULL;
- gdfd->line_len = 0;
- }
- /**
- * Free the @ref str_getdelimfd and return the @ref STRING_GETDELIMFD_ERROR
- * error code.
- *
- * @param[in] gdfd See @ref str_getdelimfd.
- * @return See @ref str_getdelim_retcode.
- */
- static enum str_getdelim_retcode
- smtp_str_getdelimfd_throw_error(struct str_getdelimfd *const gdfd){
- smtp_str_getdelimfd_free(gdfd);
- return STRING_GETDELIMFD_ERROR;
- }
- /**
- * Read and parse a delimited string using a custom socket read function.
- *
- * This interface handles all of the logic for expanding the buffer,
- * parsing the delimiter in the buffer, and returning each "line"
- * to the caller for handling.
- *
- * @param[in] gdfd See @ref str_getdelimfd.
- * @return See @ref str_getdelim_retcode.
- */
- SMTP_LINKAGE enum str_getdelim_retcode
- smtp_str_getdelimfd(struct str_getdelimfd *const gdfd){
- size_t delim_pos;
- long bytes_read;
- void *read_buf_ptr;
- char *buf_new;
- size_t buf_sz_remaining;
- size_t buf_sz_new;
- if(gdfd->getdelimfd_read == NULL){
- return STRING_GETDELIMFD_ERROR;
- }
- bytes_read = -1;
- while(1){
- if(smtp_str_getdelimfd_search_delim(gdfd->_buf,
- gdfd->_buf_len,
- gdfd->delim,
- &delim_pos)){
- if(smtp_str_getdelimfd_set_line_and_buf(gdfd, delim_pos) < 0){
- return smtp_str_getdelimfd_throw_error(gdfd);
- }
- return STRING_GETDELIMFD_NEXT;
- }else if(bytes_read == 0){
- if(smtp_str_getdelimfd_set_line_and_buf(gdfd, gdfd->_buf_len) < 0){
- return smtp_str_getdelimfd_throw_error(gdfd);
- }
- return STRING_GETDELIMFD_DONE;
- }
- if(smtp_si_sub_size_t(gdfd->_bufsz, gdfd->_buf_len, &buf_sz_remaining)){
- return smtp_str_getdelimfd_throw_error(gdfd);
- }
- if(buf_sz_remaining < SMTP_GETDELIM_READ_SZ){
- if(smtp_si_add_size_t(buf_sz_remaining,
- SMTP_GETDELIM_READ_SZ,
- &buf_sz_new)){
- return smtp_str_getdelimfd_throw_error(gdfd);
- }
- buf_new = realloc(gdfd->_buf, buf_sz_new);
- if(buf_new == NULL){
- return smtp_str_getdelimfd_throw_error(gdfd);
- }
- gdfd->_buf = buf_new;
- gdfd->_bufsz = buf_sz_new;
- }
- if(smtp_si_add_size_t((size_t)gdfd->_buf, gdfd->_buf_len, NULL)){
- return smtp_str_getdelimfd_throw_error(gdfd);
- }
- read_buf_ptr = gdfd->_buf + gdfd->_buf_len;
- bytes_read = (*gdfd->getdelimfd_read)(gdfd,
- read_buf_ptr,
- SMTP_GETDELIM_READ_SZ);
- if(bytes_read < 0 ||
- smtp_si_add_size_t(gdfd->_buf_len,
- (size_t)bytes_read,
- &gdfd->_buf_len)){
- return smtp_str_getdelimfd_throw_error(gdfd);
- }
- }
- }
- /**
- * Copy a string and get the pointer to the end of the copied buffer.
- *
- * This function behaves similar to POSIX stpcpy(), useful for
- * concatenating multiple strings onto a buffer. It always adds a
- * null-terminated byte at the end of the string.
- *
- * @param[in] s1 Destination buffer.
- * @param[in] s2 Null-terminated source string to copy to @p s1.
- * @return Pointer to location in @p s1 after the last copied byte.
- */
- SMTP_LINKAGE char *
- smtp_stpcpy(char *s1,
- const char *s2){
- size_t i;
- i = 0;
- do{
- s1[i] = s2[i];
- } while(s2[i++] != '\0');
- return &s1[i-1];
- }
- /**
- * Reallocate memory with unsigned wrapping checks.
- *
- * @param[in] ptr Existing allocation buffer, or NULL when allocating a
- * new buffer.
- * @param[in] nmemb Number of elements to allocate.
- * @param[in] size Size of each element in @p nmemb.
- * @retval void* Pointer to a reallocated buffer containing
- * @p nmemb * @p size bytes.
- * @retval NULL Failed to reallocate memory.
- */
- SMTP_LINKAGE void *
- smtp_reallocarray(void *ptr,
- size_t nmemb,
- size_t size){
- void *alloc;
- size_t size_mul;
- if(smtp_si_mul_size_t(nmemb, size, &size_mul)){
- alloc = NULL;
- errno = ENOMEM;
- }
- else{
- alloc = realloc(ptr, size_mul);
- }
- return alloc;
- }
- /**
- * Copy a string into a new dynamically allocated buffer.
- *
- * Returns a dynamically allocated string, with the same contents as the
- * input string. The caller must free the returned string when finished.
- *
- * @param[in] s String to duplicate.
- * @retval char* Pointer to a new dynamically allocated string duplicated
- * from @p s.
- * @retval NULL Failed to allocate memory for the new duplicate string.
- */
- SMTP_LINKAGE char *
- smtp_strdup(const char *s){
- char *dup;
- size_t dup_len;
- size_t slen;
- slen = strlen(s);
- if(smtp_si_add_size_t(slen, 1, &dup_len)){
- dup = NULL;
- errno = ENOMEM;
- }
- else if((dup = malloc(dup_len)) != NULL){
- memcpy(dup, s, dup_len);
- }
- return dup;
- }
- /**
- * Search for all substrings in a string and replace each instance with a
- * replacement string.
- *
- * @param[in] search Substring to search for in @p s.
- * @param[in] replace Replace each instance of the search string with this.
- * @param[in] s Null-terminated string to search and replace.
- * @retval char* A dynamically allocated string with the replaced instances
- * as described above. The caller must free the allocated
- * memory when finished.
- * @retval NULL Memory allocation failure.
- */
- SMTP_LINKAGE char *
- smtp_str_replace(const char *const search,
- const char *const replace,
- const char *const s){
- size_t search_len;
- size_t replace_len;
- size_t replace_len_inc;
- size_t slen;
- size_t slen_inc;
- size_t s_idx;
- size_t snew_len;
- size_t snew_len_inc;
- size_t snew_sz;
- size_t snew_sz_dup;
- size_t snew_sz_plus_slen;
- size_t snew_replace_len_inc;
- char *snew;
- char *stmp;
- search_len = strlen(search);
- replace_len = strlen(replace);
- slen = strlen(s);
- s_idx = 0;
- snew = NULL;
- snew_len = 0;
- snew_sz = 0;
- if(smtp_si_add_size_t(replace_len, 1, &replace_len_inc) ||
- smtp_si_add_size_t(slen, 1, &slen_inc)){
- return NULL;
- }
- if(s[0] == '\0'){
- return smtp_strdup("");
- }
- else if(search_len < 1){
- return smtp_strdup(s);
- }
- while(s[s_idx]){
- if(smtp_si_add_size_t(snew_len, 1, &snew_len_inc) ||
- smtp_si_add_size_t(snew_sz, snew_sz, &snew_sz_dup) ||
- smtp_si_add_size_t(snew_sz, slen, &snew_sz_plus_slen)){
- free(snew);
- return NULL;
- }
- if(strncmp(&s[s_idx], search, search_len) == 0){
- if(smtp_si_add_size_t(snew_len, replace_len_inc, &snew_replace_len_inc)){
- free(snew);
- return NULL;
- }
- if(snew_replace_len_inc >= snew_sz){
- /* snew_sz += snew_sz + slen + replace_len + 1 */
- if(smtp_si_add_size_t(snew_sz_dup, slen_inc, &snew_sz) ||
- smtp_si_add_size_t(snew_sz, replace_len, &snew_sz) ||
- (stmp = realloc(snew, snew_sz)) == NULL){
- free(snew);
- return NULL;
- }
- snew = stmp;
- }
- memcpy(&snew[snew_len], replace, replace_len);
- snew_len += replace_len;
- s_idx += search_len;
- }
- else{
- if(snew_len_inc >= snew_sz){
- /* snew_sz += snew_sz + slen + snew_len + 1 */
- if(smtp_si_add_size_t(snew_sz_dup, slen, &snew_sz) ||
- smtp_si_add_size_t(snew_sz, snew_len_inc, &snew_sz) ||
- (stmp = realloc(snew, snew_sz)) == NULL){
- free(snew);
- return NULL;
- }
- snew = stmp;
- }
- snew[snew_len] = s[s_idx];
- s_idx += 1;
- snew_len = snew_len_inc;
- }
- }
- snew[snew_len] = '\0';
- return snew;
- }
- /**
- * Lookup table used to encode data into base64.
- *
- * Base64 encoding takes six bits of data and encodes those bits using this
- * table. Since 2^6 = 64, this array has 64 entries which maps directly from
- * the 6 bit value into the corresponding array value.
- */
- static char g_base64_encode_table[] = {
- 'A','B','C','D','E','F','G','H','I','J',
- 'K','L','M','N','O','P','Q','R','S','T',
- 'U','V','W','X','Y','Z',
- 'a','b','c','d','e','f','g','h','i','j',
- 'k','l','m','n','o','p','q','r','s','t',
- 'u','v','w','x','y','z',
- '0','1','2','3','4','5','6','7','8','9',
- '+','/'
- };
- /**
- * Encode a single block of binary data into base64.
- *
- * @param[in] buf Buffer with data to encode.
- * @param[in] buf_block_sz Number of bytes in buf to encode (min 1, max 3).
- * @param[out] b64 Pointer to buffer with at least 4 bytes for
- * storing the base64 encoded result.
- */
- static void
- smtp_base64_encode_block(const char *const buf,
- size_t buf_block_sz,
- char *const b64){
- unsigned char inb[3] = {0};
- unsigned char in_idx[4] = {0};
- char outb[5] = {'=', '=', '=', '=', '\0'};
- size_t i;
- memcpy(inb, buf, buf_block_sz);
- in_idx[0] = ((inb[0] >> 2)) & 0x3F;
- in_idx[1] = ((inb[0] << 4) | ((inb[1] >> 4) & 0xF)) & 0x3F;
- in_idx[2] = ((inb[1] << 2) | ((inb[2] >> 6) & 0x3)) & 0x3F;
- in_idx[3] = ((inb[2] )) & 0x3F;
- for(i = 0; i < 4; i++){
- if(i < buf_block_sz + 1){
- outb[i] = g_base64_encode_table[in_idx[i]];
- }
- b64[i] = outb[i];
- }
- }
- /**
- * Encode binary data into a base64 string.
- *
- * @param[in] buf Binary data to encode in base64.
- * @param[in] buflen Number of bytes in the @p buf parameter, or -1 if
- * null-terminated.
- * @retval char* Dynamically allocated base64 encoded string. The caller
- * must free this string when finished.
- * @retval NULL Memory allocation failure.
- */
- SMTP_LINKAGE char *
- smtp_base64_encode(const char *const buf,
- size_t buflen){
- char *b64;
- size_t b64_sz;
- size_t buf_i;
- size_t b64_i;
- size_t remaining_block_sz;
- size_t buf_block_sz;
- if(buflen == SIZE_MAX){
- buflen = strlen(buf);
- }
- /*
- * base64 size expands by 33%
- * +1 to round integer division up
- * +2 for '=' padding
- * +1 null terminator
- */
- if(smtp_si_mul_size_t(buflen, 4, NULL)){
- return NULL;
- }
- b64_sz = (4 * buflen / 3) + 1 + 2 + 1;
- if((b64 = calloc(1, b64_sz)) == NULL){
- return NULL;
- }
- if(buflen == 0){
- return b64;
- }
- buf_i = 0;
- b64_i = 0;
- remaining_block_sz = buflen;
- while(remaining_block_sz > 0){
- if(remaining_block_sz >= 3){
- buf_block_sz = 3;
- }
- else{
- buf_block_sz = remaining_block_sz;
- }
- smtp_base64_encode_block(&buf[buf_i], buf_block_sz, &b64[b64_i]);
- /*
- * Do not need to check for wrapping because these values restricted to
- * range of b64_sz, which has already been checked for wrapping above.
- */
- buf_i += 3;
- b64_i += 4;
- remaining_block_sz -= buf_block_sz;
- }
- return b64;
- }
- #ifdef SMTP_OPENSSL
- /**
- * Lookup table used to decode base64 data.
- *
- * For base64 encoding, every six bits have been encoded using only the ASCII
- * characters from @ref g_base64_encode_table. This table has entries which
- * allow the reversal of that process. It has 128 entries which map over to
- * the index value from the encoding table. If an indexing result ends up
- * with -1 during the decoding process, then that indicates an invalid base64
- * character in the encoded data.
- */
- static signed char
- g_base64_decode_table[] = {
- -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
- -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
- -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
- -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
- -1, -1, -1,
- 62, /* + */
- -1, -1, -1,
- 63, /* / */
- 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, /* 0 - 9 */
- -1, -1, -1, -1, -1, -1, -1,
- 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, /* A - J */
- 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, /* K - T */
- 20, 21, 22, 23, 24, 25, /* U - Z */
- -1, -1, -1, -1, -1, -1,
- 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, /* a - j */
- 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, /* k - t */
- 46, 47, 48, 49, 50, 51, /* u - z */
- -1, -1, -1, -1, -1
- };
- /**
- * Decodes a base64 block of up to four bytes at a time.
- *
- * @param[in] buf Buffer containing bytes to decode.
- * @param[out] decode Buffer for storing base64 decoded bytes.
- * @retval >0 Length of the decoded block.
- * @retval 0 If the block contains invalid base64 data.
- */
- static size_t
- smtp_base64_decode_block(const unsigned char *const buf,
- unsigned char *const decode){
- size_t decode_block_len;
- size_t i;
- signed char decode_table[4];
- unsigned char outb[3];
- decode_block_len = 0;
- for(i = 0; i < 4; i++){
- if(buf[i] == '='){
- decode_table[i] = 0;
- continue;
- }
- decode_table[i] = g_base64_decode_table[buf[i]];
- if(decode_table[i] < 0){
- return 0;
- }
- }
- outb[0] = ((decode_table[0] << 2) & 0xFC) | ((decode_table[1] >> 4) & 0x03);
- outb[1] = ((decode_table[1] << 4) & 0xF0) | ((decode_table[2] >> 2) & 0x0F);
- outb[2] = ((decode_table[2] << 6) & 0xC0) | ((decode_table[3] ) & 0x3F);
- decode[0] = outb[0];
- decode_block_len += 1;
- if(buf[2] == '='){
- decode[1] = '\0';
- }
- else{
- decode[1] = outb[1];
- decode_block_len += 1;
- }
- if(buf[3] == '='){
- decode[2] = '\0';
- }
- else{
- decode[2] = outb[2];
- decode_block_len += 1;
- }
- return decode_block_len;
- }
- /**
- * Decode a base64 string.
- *
- * The decode parameter will get dynamically allocated by this function
- * if it successfully completes. Therefore, the caller must free the decode
- * parameter after use.
- *
- * @param[in] buf Null-terminated base64 string.
- * @param[out] decode Pointer to buffer which will get dynamically allocated
- * and will contain the decoded binary data. This parameter
- * will get set to NULL if the memory allocation fails.
- * @retval >=0 Length of the data stored in the decode parameter.
- * @retval -1 Memory allocation failure or invalid base64 byte sequences.
- */
- SMTP_LINKAGE size_t
- smtp_base64_decode(const char *const buf,
- unsigned char **decode){
- size_t buf_len;
- size_t buf_len_inc;
- size_t buf_i;
- unsigned char *b64_decode;
- size_t decode_len;
- size_t decode_block_len;
- *decode = NULL;
- buf_len = strlen(buf);
- if(buf_len % 4 != 0){
- return SIZE_MAX;
- }
- if(smtp_si_add_size_t(buf_len, 1, &buf_len_inc) ||
- (b64_decode = calloc(1, buf_len_inc)) == NULL){
- return SIZE_MAX;
- }
- decode_len = 0;
- for(buf_i = 0; buf_i < buf_len; buf_i += 4){
- decode_block_len = smtp_base64_decode_block(
- (const unsigned char*)&buf[buf_i],
- &b64_decode[decode_len]);
- if(decode_block_len == 0){
- free(b64_decode);
- return SIZE_MAX;
- }
- decode_len += decode_block_len;
- }
- *decode = b64_decode;
- return decode_len;
- }
- /**
- * Convert binary data to lowercase hexadecimal representation.
- *
- * @param[in] s Buffer containing binary data to convert.
- * @param[in] slen Number of bytes in @p s.
- * @retval char* Dynamically allocated string consisting of a hexadecimal
- * representation of binary data in @p s. The caller must free
- * this memory when finished.
- * @retval NULL Memory allocation or encoding error.
- */
- SMTP_LINKAGE char *
- smtp_bin2hex(const unsigned char *const s,
- size_t slen){
- char *snew;
- size_t alloc_sz;
- size_t i;
- size_t j;
- unsigned hex;
- int rc;
- /* alloc_sz = slen * 2 + 1 */
- if(smtp_si_mul_size_t(slen, 2, &alloc_sz) ||
- smtp_si_add_size_t(alloc_sz, 1, &alloc_sz)){
- return NULL;
- }
- if((snew = malloc(alloc_sz)) == NULL){
- return NULL;
- }
- j = 0;
- for(i = 0; i < slen; i++){
- hex = s[i];
- rc = sprintf(&snew[j], "%02x", hex);
- if(rc < 0 || (size_t)rc >= 3){
- free(snew);
- return NULL;
- }
- j += 2;
- }
- snew[j] = '\0';
- return snew;
- }
- #endif /* SMTP_OPENSSL */
- /**
- * Get the length in bytes of a UTF-8 character.
- *
- * This consists of a very simple check and assumes the user provides a valid
- * UTF-8 byte sequence. It gets the length from the first byte in the sequence
- * and does not validate any other bytes in the character sequence or any other
- * bits in the first byte of the character sequence.
- *
- * @param[in] c The first byte in a valid UTF-8 character sequence.
- * @retval >0 Number of bytes for the current UTF-8 character sequence.
- * @retval -1 Invalid byte sequence.
- */
- SMTP_LINKAGE size_t
- smtp_utf8_charlen(char c){
- unsigned char uc;
- uc = (unsigned char)c;
- if((uc & 0x80) == 0){ /* 0XXXXXXX */
- return 1;
- }
- else if((uc & 0xE0) == 0xC0){ /* 110XXXXX */
- return 2;
- }
- else if((uc & 0xF0) == 0xE0){ /* 1110XXXX */
- return 3;
- }
- else if((uc & 0xF8) == 0xF0){ /* 11110XXX */
- return 4;
- }
- else{ /* invalid */
- return 0;
- }
- }
- /**
- * Check if a string contains non-ASCII UTF-8 characters.
- *
- * Uses the simple algorithm from @ref smtp_utf8_charlen to check for
- * non-ASCII UTF-8 characters.
- *
- * @param[in] s UTF-8 string.
- * @retval 1 String contains non-ASCII UTF-8 characters.
- * @retval 0 String contains only ASCII characters.
- */
- SMTP_LINKAGE int
- smtp_str_has_nonascii_utf8(const char *const s){
- size_t i;
- size_t charlen;
- for(i = 0; s[i]; i++){
- charlen = smtp_utf8_charlen(s[i]);
- if(charlen != 1){
- return 1;
- }
- }
- return 0;
- }
- /**
- * Get the number of bytes in a UTF-8 string, or a shorter count if
- * the string exceeds a maximum specified length.
- *
- * See @p maxlen for more information on multi-byte parsing.
- *
- * @param[in] s Null-terminated UTF-8 string.
- * @param[in] maxlen Do not check more than @p maxlen bytes of string @p s
- * except if in the middle of a multi-byte character.
- * @retval strlen(s) If length of s has less bytes than maxlen or the same
- * number of bytes as maxlen. See @p maxlen for more details.
- * @retval maxlen If length of s has more bytes than maxlen.
- * @retval -1 If @p s contains an invalid UTF-8 byte sequence.
- */
- SMTP_LINKAGE size_t
- smtp_strnlen_utf8(const char *s,
- size_t maxlen){
- size_t i;
- size_t utf8_i;
- size_t utf8_len;
- for(i = 0; *s && i < maxlen; i += utf8_len){
- utf8_len = smtp_utf8_charlen(*s);
- if(utf8_len == 0){
- return SIZE_MAX;
- }
- for(utf8_i = 0; utf8_i < utf8_len; utf8_i++){
- if(!*s){
- return SIZE_MAX;
- }
- s += 1;
- }
- }
- return i;
- }
- /**
- * Get the offset of the next whitespace block to process folding.
- *
- * If a string does not have whitespace before @p maxlen, then the index
- * will get returned past @p maxlen. Also returns the index of NULL character
- * if that fits within the next block. The caller must check for the NULL
- * index to indicate the last block. It will skip past any leading whitespace
- * even if that means going over maxlen.
- *
- * Examples:
- * @ref smtp_fold_whitespace_get_offset ("Subject: Test WS", 1/2/8/9/10/13) -> 8
- * @ref smtp_fold_whitespace_get_offset ("Subject: Test WS", 14/15) -> 13
- * @ref smtp_fold_whitespace_get_offset ("Subject: Test WS", 17/18) -> 16
- *
- * @param[in] s String to get offset from.
- * @param[in] maxlen Number of bytes for each line in the string (soft limit).
- * @return Index in @p s.
- */
- SMTP_LINKAGE size_t
- smtp_fold_whitespace_get_offset(const char *const s,
- unsigned int maxlen){
- size_t i;
- size_t offset_i;
- i = 0;
- offset_i = 0;
- while(s[i] == ' ' || s[i] == '\t'){
- i += 1;
- }
- while(s[i]){
- if(s[i] == ' ' || s[i] == '\t'){
- do{
- i += 1;
- } while(s[i] == ' ' || s[i] == '\t');
- i -= 1;
- if(i < maxlen || !offset_i){
- offset_i = i;
- }
- else{
- break;
- }
- }
- i += 1;
- }
- if(!offset_i || i < maxlen){
- offset_i = i;
- }
- return offset_i;
- }
- /**
- * Email header lines should have no more than 78 characters and must
- * not be more than 998 characters.
- */
- #define SMTP_LINE_MAX 78
- /**
- * Fold a line at whitespace characters.
- *
- * This function tries to keep the total number of characters per line under
- * @p maxlen, but does not guarantee this. For really long text with no
- * whitespace, the line will still extend beyond @p maxlen and possibly
- * beyond the RFC limit as defined in @ref SMTP_LINE_MAX. This is by design
- * and intended to keep the algorithm simpler to implement. Users sending
- * long headers with no space characters should not assume that will work,
- * but modern email systems may correctly process those headers anyways.
- *
- * Lines get folded by adding a [CR][LF] and then two space characters on the
- * beginning of the next line. For example, this Subject line:
- *
- * Subject: Email[WS][WS]Header
- *
- * Would get folded like this (assuming a small @p maxlen):
- *
- * Subject: Email[WS][CR][LF]
- * [WS][WS]Header
- *
- * @param[in] s String to fold.
- * @param[in] maxlen Number of bytes for each line in the string (soft limit).
- * The minimum value of this parameter is 3 and it will get
- * forced to 3 if the provided value is less.
- * @retval char* Pointer to an allocated string with the contents split into
- * separate lines. The caller must free this memory when done.
- * @retval NULL Memory allocation failed.
- */
- SMTP_LINKAGE char *
- smtp_fold_whitespace(const char *const s,
- unsigned int maxlen){
- const char *const SMTP_LINE_FOLD_STR = "\r\n ";
- size_t end_slen;
- size_t s_i;
- size_t buf_i;
- size_t bufsz;
- size_t ws_offset;
- char *buf;
- char *buf_new;
- if(maxlen < 3){
- maxlen = 3;
- }
- end_slen = strlen(SMTP_LINE_FOLD_STR);
- s_i = 0;
- buf_i = 0;
- bufsz = 0;
- buf = NULL;
- while(1){
- ws_offset = smtp_fold_whitespace_get_offset(&s[s_i], maxlen - 2);
- /* bufsz += ws_offset + end_slen + 1 */
- if(smtp_si_add_size_t(bufsz, ws_offset, &bufsz) ||
- smtp_si_add_size_t(bufsz, end_slen, &bufsz) ||
- smtp_si_add_size_t(bufsz, 1, &bufsz) ||
- (buf_new = realloc(buf, bufsz)) == NULL){
- free(buf);
- return NULL;
- }
- buf = buf_new;
- memcpy(&buf[buf_i], &s[s_i], ws_offset);
- buf[buf_i + ws_offset] = '\0';
- if(s[s_i + ws_offset] == '\0'){
- break;
- }
- buf_i += ws_offset;
- strcat(&buf[buf_i], SMTP_LINE_FOLD_STR);
- buf_i += end_slen;
- /* WS */
- s_i += ws_offset + 1;
- }
- return buf;
- }
- /**
- * Splits a string into smaller chunks separated by a terminating string.
- *
- * @param[in] s The string to chunk.
- * @param[in] chunklen Number of bytes for each chunk in the string.
- * @param[in] end Terminating string placed at the end of each chunk.
- * @retval char* Pointer to an allocated string with the contents split into
- * separate chunks. The caller must free this memory when done.
- * @retval NULL Memory allocation failure.
- */
- SMTP_LINKAGE char *
- smtp_chunk_split(const char *const s,
- size_t chunklen,
- const char *const end){
- char *snew;
- size_t bodylen;
- size_t bodylen_inc;
- size_t endlen;
- size_t endlen_inc;
- size_t snewlen;
- size_t chunk_i;
- size_t snew_i;
- size_t body_i;
- size_t body_copy_len;
- if(chunklen < 1){
- errno = EINVAL;
- return NULL;
- }
- bodylen = strlen(s);
- endlen = strlen(end);
- if(bodylen < 1){
- return smtp_strdup(end);
- }
- /*
- * \0
- * snewlen = bodylen + (endlen + 1) * (bodylen / chunklen + 1) + 1
- */
- if(smtp_si_add_size_t(endlen, 1, &endlen_inc) ||
- smtp_si_add_size_t(bodylen, 1, &bodylen_inc) ||
- smtp_si_mul_size_t(endlen_inc, bodylen / chunklen + 1, &snewlen) ||
- smtp_si_add_size_t(snewlen, bodylen_inc, &snewlen) ||
- (snew = calloc(1, snewlen)) == NULL){
- return NULL;
- }
- body_i = 0;
- snew_i = 0;
- for(chunk_i = 0; chunk_i < bodylen / chunklen + 1; chunk_i++){
- body_copy_len = smtp_strnlen_utf8(&s[body_i], chunklen);
- if(body_copy_len == SIZE_MAX){
- free(snew);
- return NULL;
- }
- memcpy(&snew[snew_i], &s[body_i], body_copy_len);
- snew_i += body_copy_len;
- if(s[body_i] == '\0'){
- snew_i += 1;
- }
- body_i += body_copy_len;
- if(endlen > 0){
- memcpy(&snew[snew_i], end, endlen);
- }
- snew_i += endlen;
- }
- return snew;
- }
- /**
- * Read the entire contents of a file stream and store the data into a
- * dynamically allocated buffer.
- *
- * @param[in] stream File stream already opened by the caller.
- * @param[out] bytes_read Number of bytes stored in the return buffer.
- * @retval char* A dynamically allocated buffer which contains the entire
- * contents of @p stream. The caller must free this memory
- * when done.
- * @retval NULL Memory allocation or file read error.
- */
- SMTP_LINKAGE char *
- smtp_ffile_get_contents(FILE *stream,
- size_t *bytes_read){
- char *read_buf;
- size_t bufsz;
- size_t bufsz_inc;
- char *new_buf;
- size_t bytes_read_loop;
- const size_t BUFSZ_INCREMENT = 512;
- read_buf = NULL;
- bufsz = 0;
- if(bytes_read){
- *bytes_read = 0;
- }
- do{
- if(smtp_si_add_size_t(bufsz, BUFSZ_INCREMENT, &bufsz_inc) ||
- (new_buf = realloc(read_buf, bufsz_inc)) == NULL){
- free(read_buf);
- return NULL;
- }
- read_buf = new_buf;
- bufsz = bufsz_inc;
- bytes_read_loop = fread(&read_buf[bufsz - BUFSZ_INCREMENT],
- sizeof(char),
- BUFSZ_INCREMENT,
- stream);
- if(bytes_read){
- *bytes_read += bytes_read_loop;
- }
- if(ferror(stream)){
- free(read_buf);
- return NULL;
- }
- } while(!feof(stream));
- return read_buf;
- }
- /**
- * Read the entire contents of a file from a given path, and store the data
- * into a dynamically allocated buffer.
- *
- * @param[in] filename Path of file to open and read from.
- * @param[out] bytes_read Number of bytes stored in the return buffer.
- * @retval char* A dynamically allocated buffer which has the contents of
- * the file at @p filename. The caller must free this memory when
- * done.
- * @retval NULL Memory allocation or file read error.
- */
- SMTP_LINKAGE char *
- smtp_file_get_contents(const char *const filename,
- size_t *bytes_read){
- FILE *fp;
- char *read_buf;
- if((fp = fopen(filename, "rb")) == NULL){
- return NULL;
- }
- read_buf = smtp_ffile_get_contents(fp, bytes_read);
- if(fclose(fp) == EOF){
- free(read_buf);
- read_buf = NULL;
- }
- return read_buf;
- }
- /**
- * Parse a server response line into the @ref smtp_command data structure.
- *
- * @param[in] line Server response string.
- * @param[out] cmd Structure containing the server response data broken up
- * into its separate components.
- * @return See @ref smtp_result_code.
- */
- SMTP_LINKAGE int
- smtp_parse_cmd_line(char *const line,
- struct smtp_command *const cmd){
- char *ep;
- char code_str[4];
- size_t line_len;
- unsigned long int ulcode;
- line_len = strlen(line);
- if(line_len < 5){
- cmd->code = SMTP_INTERNAL_ERROR;
- cmd->more = 0;
- cmd->text = line;
- return cmd->code;
- }
- cmd->text = &line[4];
- memcpy(code_str, line, 3);
- code_str[3] = '\0';
- ulcode = strtoul(code_str, &ep, 10);
- if(*ep != '\0' || ulcode > SMTP_BEGIN_MAIL){
- cmd->code = SMTP_INTERNAL_ERROR;
- }
- else{
- cmd->code = (enum smtp_result_code)ulcode;
- }
- if(line[3] == '-'){
- cmd->more = 1;
- }
- else{
- cmd->more = 0;
- }
- return cmd->code;
- }
- /**
- * Prints communication between the client and server to stderr only if
- * the debug flag has been set.
- *
- * @param[in] smtp SMTP client context.
- * @param[in] prefix Print this prefix before the main debug line text.
- * @param[in] str Debug text to print out.
- */
- static void
- smtp_puts_dbg(struct smtp *const smtp,
- const char *const prefix,
- const char *const str){
- char *sdup;
- size_t i;
- if(smtp->flags & SMTP_DEBUG){
- if((sdup = smtp_strdup(str)) == NULL){
- return;
- }
- /* Remove carriage return and newline when printing to stderr. */
- for(i = 0; sdup[i]; i++){
- if(sdup[i] == '\r' || sdup[i] == '\n'){
- sdup[i] = ' ';
- }
- }
- if(fprintf(stderr, "[smtp %s]: %s\n", prefix, sdup) < 0){
- /* Do not care if this fails. */
- }
- free(sdup);
- }
- }
- /**
- * Read a server response line.
- *
- * @param[in] smtp SMTP client context.
- * @return See @ref str_getdelim_retcode.
- */
- static enum str_getdelim_retcode
- smtp_getline(struct smtp *const smtp){
- enum str_getdelim_retcode rc;
- errno = 0;
- rc = smtp_str_getdelimfd(&smtp->gdfd);
- if(errno == ENOMEM){
- smtp_status_code_set(smtp, SMTP_STATUS_NOMEM);
- return rc;
- }
- else if(rc == STRING_GETDELIMFD_ERROR){
- smtp_status_code_set(smtp, SMTP_STATUS_RECV);
- return STRING_GETDELIMFD_ERROR;
- }
- if(smtp->gdfd.line_len > 0){
- /* Remove the carriage-return character ('\r'). */
- smtp->gdfd.line[smtp->gdfd.line_len - 1] = '\0';
- smtp_puts_dbg(smtp, "Server", smtp->gdfd.line);
- }
- return rc;
- }
- /**
- * Loop through all of the server response lines until the last line, and
- * then return the status code from the last response line.
- *
- * @param[in] smtp SMTP client context.
- * @return See @ref smtp_result_code.
- */
- static int
- smtp_read_and_parse_code(struct smtp *const smtp){
- struct smtp_command cmd;
- enum str_getdelim_retcode rc;
- do{
- rc = smtp_getline(smtp);
- if(rc == STRING_GETDELIMFD_ERROR){
- return SMTP_INTERNAL_ERROR;
- }
- smtp_parse_cmd_line(smtp->gdfd.line, &cmd);
- }while (rc != STRING_GETDELIMFD_DONE && cmd.more);
- return cmd.code;
- }
- /**
- * Send data to the SMTP server.
- *
- * Writes a buffer of length len into either the unencrypted TCP socket or
- * the TLS encrypted socket, depending on the current underlying mode of
- * the socket.
- *
- * @param[in] smtp SMTP client context.
- * @param[in] buf Data to send to the SMTP server.
- * @param[in] len Number of bytes in buf.
- * @return See @ref smtp_status_code.
- */
- SMTP_LINKAGE enum smtp_status_code
- smtp_write(struct smtp *const smtp,
- const char *const buf,
- size_t len){
- size_t bytes_to_send;
- long bytes_sent;
- const char *buf_offset;
- int ssl_bytes_to_send;
- smtp_puts_dbg(smtp, "Client", buf);
- bytes_to_send = len;
- buf_offset = buf;
- while(bytes_to_send){
- if(bytes_to_send > INT_MAX){
- return smtp_status_code_set(smtp, SMTP_STATUS_SEND);
- }
- if(smtp->tls_on){
- #ifdef SMTP_OPENSSL
- /* bytes_to_send <= INT_MAX */
- ssl_bytes_to_send = (int)bytes_to_send;
- bytes_sent = SSL_write(smtp->tls, buf_offset, ssl_bytes_to_send);
- if(bytes_sent <= 0){
- return smtp_status_code_set(smtp, SMTP_STATUS_SEND);
- }
- #else /* !(SMTP_OPENSSL) */
- /* unreachable */
- bytes_sent = 0;
- (void)ssl_bytes_to_send;
- #endif /* SMTP_OPENSSL */
- }
- else{
- bytes_sent = send(smtp->sock, buf_offset, bytes_to_send, 0);
- if(bytes_sent < 0){
- return smtp_status_code_set(smtp, SMTP_STATUS_SEND);
- }
- }
- bytes_to_send -= (size_t)bytes_sent;
- buf_offset += bytes_sent;
- }
- return smtp->status_code;
- }
- /**
- * Send a null-terminated string to the SMTP server.
- *
- * @param[in] smtp SMTP client context.
- * @param[in] s Null-terminated string to send to the SMTP server.
- * @return See @ref smtp_status_code and @ref smtp_write.
- */
- static enum smtp_status_code
- smtp_puts(struct smtp *const smtp,
- const char *const s){
- return smtp_write(smtp, s, strlen(s));
- }
- /**
- * Same as @ref smtp_puts except this function also appends the line
- * terminating carriage return and newline bytes at the end of the string.
- *
- * @param[in] smtp SMTP client context.
- * @param[in] s Null-terminated string to send to the SMTP server.
- * @return See @ref smtp_status_code and @ref smtp_puts.
- */
- static enum smtp_status_code
- smtp_puts_terminate(struct smtp *const smtp,
- const char *const s){
- enum smtp_status_code rc;
- char *line;
- char *concat;
- size_t slen;
- size_t allocsz;
- slen = strlen(s);
- if(smtp_si_add_size_t(slen, 3, &allocsz) ||
- (line = malloc(allocsz)) == NULL){
- return smtp_status_code_set(smtp, SMTP_STATUS_NOMEM);
- }
- concat = smtp_stpcpy(line, s);
- smtp_stpcpy(concat, "\r\n");
- rc = smtp_puts(smtp, line);
- free(line);
- return rc;
- }
- /**
- * Connect to the server using a standard TCP socket.
- *
- * This function handles the server name lookup to get an IP address
- * for the server, and then to connect to that IP using a normal TCP
- * connection.
- *
- * @param[in] smtp SMTP client context.
- * @param[in] server Mail server name or IP address.
- * @param[in] port Mail server port number.
- * @retval 0 Successfully connected to server.
- * @retval -1 Failed to connect to server.
- */
- static int
- smtp_connect(struct smtp *const smtp,
- const char *const server,
- const char *const port){
- struct addrinfo hints;
- struct addrinfo *res0;
- struct addrinfo *res;
- /*
- * Windows requires initializing the socket library before we call any
- * socket functions.
- */
- #ifdef SMTP_IS_WINDOWS
- /* Windows global network socket data structure. */
- WSADATA wsa_data;
- if(WSAStartup(MAKEWORD(2, 2), &wsa_data) != 0){
- return -1;
- }
- #endif /* SMTP_IS_WINDOWS */
- memset(&hints, 0, sizeof(hints));
- hints.ai_family = AF_UNSPEC;
- hints.ai_socktype = SOCK_STREAM;
- hints.ai_flags = 0;
- hints.ai_protocol = IPPROTO_TCP;
- if(getaddrinfo(server, port, &hints, &res0) != 0){
- return -1;
- }
- for(res = res0; res; res = res->ai_next){
- smtp->sock = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
- if(smtp->sock < 0){
- continue;
- }
- if(connect(smtp->sock, res->ai_addr, res->ai_addrlen) < 0){
- #ifdef SMTP_IS_WINDOWS
- closesocket(smtp->sock);
- #else /* POSIX */
- close(smtp->sock);
- #endif /* SMTP_IS_WINDOWS */
- smtp->sock = -1;
- }
- else{
- break;
- }
- }
- freeaddrinfo(res0);
- if(smtp->sock < 0){
- return -1;
- }
- return 0;
- }
- #ifdef SMTP_OPENSSL
- /**
- * Initialize the TLS library and establish a TLS handshake with the server
- * over the existing socket connection.
- *
- * @param[in] smtp SMTP client context.
- * @param[in] server Server name or IP address.
- * @retval 0 Successfully established a TLS connection with the server.
- * @retval -1 Failed to establish a TLS connection with the server.
- */
- static int
- smtp_tls_init(struct smtp *const smtp,
- const char *const server){
- X509 *X509_cert_peer;
- /* Do not need to check the return value since this always returns 1. */
- SSL_library_init();
- SSL_load_error_strings();
- ERR_load_BIO_strings();
- OpenSSL_add_all_algorithms();
- if((smtp->tls_ctx = SSL_CTX_new(SSLv23_client_method())) == NULL){
- return -1;
- }
- /* Disable SSLv2, SSLv3, and TLSv1.0. */
- SSL_CTX_set_options(smtp->tls_ctx,
- SSL_OP_NO_SSLv2 |
- SSL_OP_NO_SSLv3 |
- SSL_OP_NO_TLSv1);
- SSL_CTX_set_mode(smtp->tls_ctx, SSL_MODE_AUTO_RETRY);
- if((smtp->flags & SMTP_NO_CERT_VERIFY) == 0){
- SSL_CTX_set_verify(smtp->tls_ctx, SSL_VERIFY_PEER, NULL);
- }
- /*
- * Set the path to the user-provided CA file or use the default cert paths
- * if not provided.
- */
- if(smtp->cafile){
- if(SSL_CTX_load_verify_locations(smtp->tls_ctx, smtp->cafile, NULL) != 1){
- SSL_CTX_free(smtp->tls_ctx);
- return -1;
- }
- }
- else{
- X509_STORE_set_default_paths(SSL_CTX_get_cert_store(smtp->tls_ctx));
- if(ERR_peek_error() != 0){
- SSL_CTX_free(smtp->tls_ctx);
- return -1;
- }
- }
- if((smtp->tls = SSL_new(smtp->tls_ctx)) == NULL){
- SSL_CTX_free(smtp->tls_ctx);
- return -1;
- }
- if((smtp->tls_bio = BIO_new_socket(smtp->sock, 0)) == NULL){
- SSL_CTX_free(smtp->tls_ctx);
- SSL_free(smtp->tls);
- return -1;
- }
- SSL_set_bio(smtp->tls, smtp->tls_bio, smtp->tls_bio);
- SSL_set_connect_state(smtp->tls);
- if(SSL_connect(smtp->tls) != 1){
- SSL_CTX_free(smtp->tls_ctx);
- SSL_free(smtp->tls);
- return -1;
- }
- if(SSL_do_handshake(smtp->tls) != 1){
- SSL_CTX_free(smtp->tls_ctx);
- SSL_free(smtp->tls);
- return -1;
- }
- /* Verify matching subject in certificate. */
- if((smtp->flags & SMTP_NO_CERT_VERIFY) == 0){
- if((X509_cert_peer = SSL_get_peer_certificate(smtp->tls)) == NULL){
- SSL_CTX_free(smtp->tls_ctx);
- SSL_free(smtp->tls);
- return -1;
- }
- if(X509_check_host(X509_cert_peer, server, 0, 0, NULL) != 1){
- SSL_CTX_free(smtp->tls_ctx);
- SSL_free(smtp->tls);
- return -1;
- }
- X509_free(X509_cert_peer);
- }
- smtp->tls_on = 1;
- return 0;
- }
- #endif /* SMTP_OPENSSL */
- /**
- * Send the EHLO command and parse through the responses.
- *
- * Ignores all of the server extensions that get returned. If a server
- * doesn't support an extension we need, then we should receive an error
- * later on when we try to use that extension.
- *
- * @param[in] smtp SMTP client context.
- * @return See @ref smtp_status_code.
- */
- static enum smtp_status_code
- smtp_ehlo(struct smtp *const smtp){
- if(smtp_puts(smtp, "EHLO smtp\r\n") == SMTP_STATUS_OK){
- smtp_read_and_parse_code(smtp);
- }
- return smtp->status_code;
- }
- /**
- * Authenticate using the PLAIN method.
- *
- * 1. Set the text to the following format: "\0<user>\0<password>",
- * or as shown in the format string: "\0%s\0%s", email, password.
- * 2. Base64 encode the text from (1).
- * 3. Send the constructed auth text from (2) to the server:
- * "AUTH PLAIN <b64><CR><NL>".
- *
- * @param[in] smtp SMTP client context.
- * @param[in] user SMTP account user name.
- * @param[in] pass SMTP account password.
- * @retval 0 Successfully authenticated.
- * @retval -1 Failed to authenticate.
- */
- static int
- smtp_auth_plain(struct smtp *const smtp,
- const char *const user,
- const char *const pass){
- size_t user_len;
- size_t pass_len;
- char *login_str;
- size_t login_len;
- char *login_b64;
- size_t login_b64_len;
- char *login_send;
- char *concat;
- /* (1) */
- user_len = strlen(user);
- pass_len = strlen(pass);
- /* login_len = 1 + user_len + 1 + pass_len */
- if(smtp_si_add_size_t(user_len, pass_len, &login_len) ||
- smtp_si_add_size_t(login_len, 2, &login_len) ||
- (login_str = malloc(login_len)) == NULL){
- return -1;
- }
- login_str[0] = '\0';
- memcpy(&login_str[1], user, user_len);
- login_str[1 + user_len] = '\0';
- memcpy(&login_str[1 + user_len + 1], pass, pass_len);
- /* (2) */
- login_b64 = smtp_base64_encode(login_str, login_len);
- free(login_str);
- if(login_b64 == NULL){
- return -1;
- }
- /* (3) */
- login_b64_len = strlen(login_b64);
- if(smtp_si_add_size_t(login_b64_len, 14, &login_b64_len) ||
- (login_send = malloc(login_b64_len)) == NULL){
- free(login_b64);
- return -1;
- }
- concat = smtp_stpcpy(login_send, "AUTH PLAIN ");
- concat = smtp_stpcpy(concat, login_b64);
- smtp_stpcpy(concat, "\r\n");
- free(login_b64);
- smtp_puts(smtp, login_send);
- free(login_send);
- if(smtp->status_code != SMTP_STATUS_OK){
- return -1;
- }
- if(smtp_read_and_parse_code(smtp) != SMTP_AUTH_SUCCESS){
- return -1;
- }
- return 0;
- }
- /**
- * Authenticate using the LOGIN method.
- *
- * 1. Base64 encode the user name.
- * 2. Send string from (1) as part of the login:
- * "AUTH LOGIN <b64_username><CR><NL>".
- * 3. Base64 encode the password and send that by itself on a separate
- * line: "<b64_password><CR><NL>".
- *
- * @param[in] smtp SMTP client context.
- * @param[in] user SMTP account user name.
- * @param[in] pass SMTP account password.
- * @retval 0 Successfully authenticated.
- * @retval -1 Failed to authenticate.
- */
- static int
- smtp_auth_login(struct smtp *const smtp,
- const char *const user,
- const char *const pass){
- char *b64_user;
- char *b64_pass;
- size_t b64_user_len;
- char *login_str;
- char *concat;
- /* (1) */
- if((b64_user = smtp_base64_encode(user, SIZE_MAX)) == NULL){
- return -1;
- }
- /* (2) */
- b64_user_len = strlen(b64_user);
- if(smtp_si_add_size_t(b64_user_len, 14, &b64_user_len) ||
- (login_str = malloc(b64_user_len)) == NULL){
- free(b64_user);
- return -1;
- }
- concat = smtp_stpcpy(login_str, "AUTH LOGIN ");
- concat = smtp_stpcpy(concat, b64_user);
- smtp_stpcpy(concat, "\r\n");
- free(b64_user);
- smtp_puts(smtp, login_str);
- free(login_str);
- if(smtp->status_code != SMTP_STATUS_OK){
- return -1;
- }
- if(smtp_read_and_parse_code(smtp) != SMTP_AUTH_CONTINUE){
- return -1;
- }
- /* (3) */
- if((b64_pass = smtp_base64_encode(pass, SIZE_MAX)) == NULL){
- return -1;
- }
- smtp_puts_terminate(smtp, b64_pass);
- free(b64_pass);
- if(smtp->status_code != SMTP_STATUS_OK){
- return -1;
- }
- if(smtp_read_and_parse_code(smtp) != SMTP_AUTH_SUCCESS){
- return -1;
- }
- return 0;
- }
- #ifdef SMTP_OPENSSL
- /**
- * Authenticate using the CRAM-MD5 method.
- *
- * 1. Send "AUTH CRAM-MD5<CR><NL>" to the server.
- * 2. Decode the base64 challenge response from the server.
- * 3. Do an MD5 HMAC on (2) using the account password as the key.
- * 4. Convert the binary data in (3) to lowercase hex characters.
- * 5. Construct the string: "<user> <(4)>".
- * 6. Encode (5) into base64 format.
- * 7. Send the final string from (6) to the server and check the response.
- *
- * @param[in] smtp SMTP client context.
- * @param[in] user SMTP account user name.
- * @param[in] pass SMTP account password.
- * @retval 0 Successfully authenticated.
- * @retval -1 Failed to authenticate.
- */
- static int
- smtp_auth_cram_md5(struct smtp *const smtp,
- const char *const user,
- const char *const pass){
- struct smtp_command cmd;
- unsigned char *challenge_decoded;
- size_t challenge_decoded_len;
- const char *key;
- int key_len;
- unsigned char hmac[EVP_MAX_MD_SIZE];
- unsigned int hmac_len;
- unsigned char *hmac_ret;
- char *challenge_hex;
- size_t user_len;
- size_t challenge_hex_len;
- char *auth_concat;
- char *concat;
- size_t auth_concat_len;
- char *b64_auth;
- /* (1) */
- if(smtp_puts(smtp, "AUTH CRAM-MD5\r\n") != SMTP_STATUS_OK){
- return -1;
- }
- if(smtp_getline(smtp) == STRING_GETDELIMFD_ERROR){
- return -1;
- }
- if(smtp_parse_cmd_line(smtp->gdfd.line, &cmd) != SMTP_AUTH_CONTINUE){
- return -1;
- }
- /* (2) */
- challenge_decoded_len = smtp_base64_decode(cmd.text,
- &challenge_decoded);
- if(challenge_decoded_len == SIZE_MAX){
- return -1;
- }
- /* (3) */
- key = pass;
- key_len = (int)strlen(pass);/*********************cast*/
- hmac_ret = HMAC(EVP_md5(),
- key,
- key_len,
- challenge_decoded,
- challenge_decoded_len,
- hmac,
- &hmac_len);
- free(challenge_decoded);
- if(hmac_ret == NULL){
- return -1;
- }
- /* (4) */
- challenge_hex = smtp_bin2hex(hmac, hmac_len);
- if(challenge_hex == NULL){
- return -1;
- }
- /* (5) */
- user_len = strlen(user);
- challenge_hex_len = strlen(challenge_hex);
- /* auth_concat_len = user_len + 1 + challenge_hex_len + 1 */
- if(smtp_si_add_size_t(user_len, challenge_hex_len, &auth_concat_len) ||
- smtp_si_add_size_t(auth_concat_len, 2, &auth_concat_len) ||
- (auth_concat = malloc(auth_concat_len)) == NULL){
- free(challenge_hex);
- return -1;
- }
- concat = smtp_stpcpy(auth_concat, user);
- concat = smtp_stpcpy(concat, " ");
- smtp_stpcpy(concat, challenge_hex);
- free(challenge_hex);
- /* (6) */
- b64_auth = smtp_base64_encode(auth_concat, auth_concat_len - 1);
- free(auth_concat);
- if(b64_auth == NULL){
- return -1;
- }
- /* (7) */
- smtp_puts_terminate(smtp, b64_auth);
- free(b64_auth);
- if(smtp->status_code != SMTP_STATUS_OK){
- return -1;
- }
- if(smtp_read_and_parse_code(smtp) != SMTP_AUTH_SUCCESS){
- return -1;
- }
- return 0;
- }
- #endif /* SMTP_OPENSSL */
- /**
- * Set the timeout for the next socket read operation.
- *
- * @param[in] smtp SMTP client context.
- * @param[in] seconds Timeout in seconds.
- */
- static void
- smtp_set_read_timeout(struct smtp *const smtp,
- long seconds){
- smtp->timeout_sec = seconds;
- }
- /**
- * Perform a handshake with the SMTP server which includes optionally
- * setting up TLS and sending the EHLO greeting.
- *
- * At this point, the client has already connected to the SMTP server
- * through its socket connection. In this function, the client will:
- * 1. Optionally convert the connection to TLS (SMTP_SECURITY_TLS).
- * 2. Read the initial server greeting.
- * 3. Send an EHLO to the server.
- * 4. Optionally initiate STARTTLS and resend the EHLO
- * (SMTP_SECURITY_STARTTLS).
- *
- * @param[in] smtp SMTP client context.
- * @param[in] server Server name or IP address.
- * @param[in] connection_security See @ref smtp_connection_security.
- * @return See @ref smtp_status_code.
- */
- static enum smtp_status_code
- smtp_initiate_handshake(struct smtp *const smtp,
- const char *const server,
- enum smtp_connection_security connection_security){
- /* Eliminate unused warnings if not using SMTP_OPENSSL. */
- (void)server;
- (void)connection_security;
- /* (1) */
- #ifdef SMTP_OPENSSL
- if(connection_security == SMTP_SECURITY_TLS){
- if(smtp_tls_init(smtp, server) < 0){
- return smtp_status_code_set(smtp, SMTP_STATUS_HANDSHAKE);
- }
- }
- #endif /* SMTP_OPENSSL */
- /* (2) */
- /* Get initial 220 message - 5 minute timeout. */
- smtp_set_read_timeout(smtp, 60 * 5);
- if(smtp_getline(smtp) == STRING_GETDELIMFD_ERROR){
- return smtp->status_code;
- }
- /* (3) */
- if(smtp_ehlo(smtp) != SMTP_STATUS_OK){
- return smtp->status_code;
- }
- /* (4) */
- #ifdef SMTP_OPENSSL
- if(connection_security == SMTP_SECURITY_STARTTLS){
- if(smtp_puts(smtp, "STARTTLS\r\n") != SMTP_STATUS_OK){
- return smtp->status_code;
- }
- if(smtp_read_and_parse_code(smtp) != SMTP_READY){
- return smtp_status_code_set(smtp, SMTP_STATUS_HANDSHAKE);
- }
- if(smtp_tls_init(smtp, server) < 0){
- return smtp_status_code_set(smtp, SMTP_STATUS_HANDSHAKE);
- }
- if(smtp_ehlo(smtp) != SMTP_STATUS_OK){
- return smtp->status_code;
- }
- }
- #endif /* SMTP_OPENSSL */
- return smtp->status_code;
- }
- /**
- Maximum size of an RFC 2822 date string.
- @verbatim
- Thu, 21 May 1998 05:33:29 -0700
- 12345678901234567890123456789012
- 10 20 30 32 (bytes)
- @endverbatim
- Add more bytes to the 32 maximum size to silence compiler warning on the
- computed UTF offset.
- */
- #define SMTP_DATE_MAX_SZ (32 + 15)
- /**
- * Convert the time into an RFC 2822 formatted string.
- *
- * Example date format:
- * Thu, 21 May 1998 05:33:29 -0700
- *
- * @param[out] date Buffer that has at least SMTP_DATE_MAX_SZ bytes.
- * @retval 0 Successfully copied the current date to the buffer.
- * @retval -1 Failed to establish the current date or an output
- * format error occurred.
- */
- SMTP_LINKAGE int
- smtp_date_rfc_2822(char *const date){
- time_t t;
- time_t t_local;
- time_t t_utc;
- struct tm tm_local;
- struct tm tm_utc;
- long offset_utc;
- double diff_local_utc;
- int rc;
- const char weekday_abbreviation[7][4] = {
- "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
- };
- const char month_abbreviation[12][4] = {
- "Jan", "Feb", "Mar", "Apr", "May", "Jun",
- "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
- };
- if((t = time(NULL)) == (time_t)(-1)){
- return -1;
- }
- #ifdef SMTP_IS_WINDOWS
- if(localtime_s(&tm_local, &t) ||
- gmtime_s(&tm_utc, &t)){
- return -1;
- }
- #else /* POSIX */
- /* Not defined if system does not have localtime_r or gmtime_r. */
- # ifdef SMTP_TIME_NO_REENTRANT
- struct tm *tm;
- /* localtime() not thread-safe. */
- if((tm = localtime(&t)) == NULL){
- return -1;
- }
- memcpy(&tm_local, tm, sizeof(tm_local));
- /* gmtime() not thread-safe. */
- if((tm = gmtime(&t)) == NULL){
- return -1;
- }
- memcpy(&tm_utc, tm, sizeof(tm_utc));
- # else /* Reentrant versions: localtime_r() and gmtime_r(). */
- if(localtime_r(&t, &tm_local) == NULL ||
- gmtime_r(&t, &tm_utc) == NULL){
- return -1;
- }
- # endif /* SMTP_TIME_NO_REENTRANT */
- #endif /* SMTP_IS_WINDOWS */
- if((t_local = mktime(&tm_local)) == (time_t)(-1)){
- return -1;
- }
- if((t_utc = mktime(&tm_utc)) == (time_t)(-1)){
- return -1;
- }
- /*
- * After computing the offset, it will contain a maximum of 4 digits.
- * For example, PST time zone will have an offset of -800 which will get
- * formatted as -0800 in the sprintf call below.
- */
- diff_local_utc = difftime(t_local, t_utc);
- offset_utc = (long)diff_local_utc;
- offset_utc = offset_utc / 60 / 60 * 100;
- rc = sprintf(date,
- "%s, %02d %s %d %02d:%02d:%02d %0+5ld",
- weekday_abbreviation[tm_local.tm_wday],
- tm_local.tm_mday,
- month_abbreviation[tm_local.tm_mon],
- tm_local.tm_year + 1900,
- tm_local.tm_hour,
- tm_local.tm_min,
- tm_local.tm_sec, /* 0 - 60 (leap second) */
- offset_utc);
- if(rc + 1 != SMTP_DATE_MAX_SZ - 15){ /* See @ref SMTP_DATE_MAX_SZ for -5. */
- return -1;
- }
- return 0;
- }
- /**
- * Search function used by bsearch, allowing the caller to check for
- * headers with existing keys.
- *
- * @param v1 String to search for in the list.
- * @param v2 The @ref smtp_header to compare.
- * @retval 0 If the keys match.
- * @retval !0 If the keys do not match.
- */
- static int
- smtp_header_cmp_key(const void *const v1,
- const void *const v2){
- const char *key;
- const struct smtp_header *header2;
- key = v1;
- header2 = v2;
- return strcmp(key, header2->key);
- }
- /**
- * Determine if the header key has already been defined in this context.
- *
- * @param[in] smtp SMTP client context.
- * @param[in] key Header key value to search for.
- * @retval 1 If the header already exists in this context.
- * @retval 0 If the header does not exist in this context.
- */
- static int
- smtp_header_exists(const struct smtp *const smtp,
- const char *const key){
- if(bsearch(key,
- smtp->header_list,
- smtp->num_headers,
- sizeof(*smtp->header_list),
- smtp_header_cmp_key) == NULL){
- return 0;
- }
- return 1;
- }
- /**
- * Minimum length of buffer required to hold the MIME boundary test:
- * mimeXXXXXXXXXX
- * 123456789012345
- * 1 10 15 bytes
- */
- #define SMTP_MIME_BOUNDARY_LEN 15
- /**
- * Generate the MIME boundary text field and store it in a user-supplied
- * buffer.
- *
- * For example:
- * mimeXXXXXXXXXX
- * where each X gets set to a pseudo-random uppercase ASCII character.
- *
- * This uses a simple pseudo-random number generator since we only care
- * about preventing accidental boundary collisions.
- *
- * @param[out] boundary Buffer that has at least SMTP_MIME_BOUNDARY_LEN bytes.
- */
- static void
- smtp_gen_mime_boundary(char *const boundary){
- size_t i;
- unsigned int seed;
- seed = (unsigned int)time(NULL);
- srand(seed);
- strcpy(boundary, "mime");
- for(i = 4; i < SMTP_MIME_BOUNDARY_LEN - 1; i++){
- /* Modulo bias okay since we only need to prevent accidental collision. */
- boundary[i] = rand() % 26 + 'A';
- }
- boundary[SMTP_MIME_BOUNDARY_LEN - 1] = '\0';
- }
- /**
- * Print the MIME header and the MIME section containing the email body.
- *
- * @param[in] smtp SMTP client context.
- * @param[in] boundary MIME boundary text.
- * @param[in] body_dd Email body with double dots added at the
- * beginning of each line.
- * @return See @ref smtp_status_code.
- */
- static enum smtp_status_code
- smtp_print_mime_header_and_body(struct smtp *const smtp,
- const char *const boundary,
- const char *const body_dd){
- /* Buffer size for the static MIME text used below. */
- const size_t MIME_TEXT_BUFSZ = 1000;
- size_t data_double_dot_len;
- char *data_header_and_body;
- char *concat;
- data_double_dot_len = strlen(body_dd);
- if(smtp_si_add_size_t(data_double_dot_len,
- MIME_TEXT_BUFSZ,
- &data_double_dot_len) ||
- (data_header_and_body = malloc(data_double_dot_len)) == NULL){
- return smtp_status_code_set(smtp, SMTP_STATUS_NOMEM);
- }
- concat = smtp_stpcpy(data_header_and_body,
- "MIME-Version: 1.0\r\n"
- "Content-Type: multipart/mixed; boundary=");
- concat = smtp_stpcpy(concat,
- boundary);
- concat = smtp_stpcpy(concat,
- "\r\n"
- "\r\n"
- "Multipart MIME message.\r\n"
- "--");
- concat = smtp_stpcpy(concat,
- boundary);
- concat = smtp_stpcpy(concat,
- "\r\n"
- "Content-Type: text/plain; charset=\"UTF-8\"\r\n"
- "\r\n");
- concat = smtp_stpcpy(concat,
- body_dd);
- smtp_stpcpy(concat,
- "\r\n"
- "\r\n");
- smtp_puts(smtp, data_header_and_body);
- free(data_header_and_body);
- return smtp->status_code;
- }
- /**
- * Print a MIME section containing an attachment.
- *
- * @param[in] smtp SMTP client context.
- * @param[in] boundary MIME boundary text.
- * @param[in] attachment See @ref smtp_attachment.
- * @return See @ref smtp_status_code.
- */
- static enum smtp_status_code
- smtp_print_mime_attachment(struct smtp *const smtp,
- const char *const boundary,
- const struct smtp_attachment *const attachment){
- /* Buffer size for the static MIME text used below. */
- const size_t MIME_TEXT_BUFSZ = 1000;
- size_t name_len;
- size_t b64_data_len;
- size_t bufsz;
- char *mime_attach_text;
- char *concat;
- name_len = strlen(attachment->name);
- b64_data_len = strlen(attachment->b64_data);
- /*
- * bufsz = SMTP_MIME_BOUNDARY_LEN + name_len + b64_data_len + MIME_TEXT_BUFSZ
- */
- if(smtp_si_add_size_t(name_len, b64_data_len, &bufsz) ||
- smtp_si_add_size_t(bufsz,
- SMTP_MIME_BOUNDARY_LEN + MIME_TEXT_BUFSZ,
- &bufsz) ||
- (mime_attach_text = malloc(bufsz)) == NULL){
- return smtp_status_code_set(smtp, SMTP_STATUS_NOMEM);
- }
- concat = smtp_stpcpy(mime_attach_text,
- "--");
- concat = smtp_stpcpy(concat,
- boundary);
- concat = smtp_stpcpy(concat,
- "\r\n"
- "Content-Type: application/octet-stream\r\n"
- "Content-Disposition: attachment; filename=\"");
- concat = smtp_stpcpy(concat,
- attachment->name);
- concat = smtp_stpcpy(concat,
- "\"\r\n"
- "Content-Transfer-Encoding: base64\r\n"
- "\r\n");
- concat = smtp_stpcpy(concat,
- attachment->b64_data);
- smtp_stpcpy(concat,
- "\r\n"
- "\r\n");
- smtp_puts(smtp, mime_attach_text);
- free(mime_attach_text);
- return smtp->status_code;
- }
- /**
- * Prints double hyphen on both sides of the MIME boundary which indicates
- * the end of the MIME sections.
- *
- * @param[in] smtp SMTP client context.
- * @param[in] boundary MIME boundary text.
- * @return See @ref smtp_status_code and @ref smtp_puts.
- */
- static enum smtp_status_code
- smtp_print_mime_end(struct smtp *const smtp,
- const char *const boundary){
- char *concat;
- char mime_end[2 + SMTP_MIME_BOUNDARY_LEN + 4 + 1];
- concat = smtp_stpcpy(mime_end, "--");
- concat = smtp_stpcpy(concat, boundary);
- smtp_stpcpy(concat, "--\r\n");
- return smtp_puts(smtp, mime_end);
- }
- /**
- * Send the main email body to the SMTP server.
- *
- * This includes the MIME sections for the email body and attachments.
- *
- * @param[in] smtp SMTP client context.
- * @param[in] body_dd Email body with double dots added at the
- * beginning of each line.
- * @return See @ref smtp_status_code.
- */
- static enum smtp_status_code
- smtp_print_mime_email(struct smtp *const smtp,
- const char *const body_dd){
- char boundary[SMTP_MIME_BOUNDARY_LEN];
- size_t i;
- struct smtp_attachment *attachment;
- smtp_gen_mime_boundary(boundary);
- if(smtp_print_mime_header_and_body(smtp, boundary, body_dd) != SMTP_STATUS_OK){
- return smtp->status_code;
- }
- for(i = 0; i < smtp->num_attachment; i++){
- attachment = &smtp->attachment_list[i];
- if(smtp_print_mime_attachment(smtp,
- boundary,
- attachment) != SMTP_STATUS_OK){
- return smtp->status_code;
- }
- }
- return smtp_print_mime_end(smtp, boundary);
- }
- /**
- * Print the email data provided by the user without MIME formatting.
- *
- * @param[in,out] smtp SMTP client context.
- * @param[in] body_dd Email body with double dots added at the
- * beginning of each line.
- * @return See @ref smtp_status_code.
- */
- static enum smtp_status_code
- smtp_print_nomime_email(struct smtp *const smtp,
- const char *const body_dd){
- return smtp_puts_terminate(smtp, body_dd);
- }
- /**
- * Send the email body to the mail server.
- *
- * @param[in,out] smtp SMTP client context.
- * @param[in] body Email body text.
- * @return See @ref smtp_status_code.
- */
- static enum smtp_status_code
- smtp_print_email(struct smtp *const smtp,
- const char *const body){
- char *body_double_dot;
- /*
- * Insert an extra dot for each line that begins with a dot. This will
- * prevent data in the body parameter from prematurely ending the DATA
- * segment.
- */
- if((body_double_dot = smtp_str_replace("\n.", "\n..", body)) == NULL){
- return smtp_status_code_set(smtp, SMTP_STATUS_NOMEM);
- }
- if(smtp_header_exists(smtp, "Content-Type")){
- smtp_print_nomime_email(smtp, body_double_dot);
- }
- else{
- smtp_print_mime_email(smtp, body_double_dot);
- }
- free(body_double_dot);
- return smtp->status_code;
- }
- /**
- * Convert a header into an RFC 5322 formatted string and send it to the
- * SMTP server.
- *
- * This will adding proper line wrapping and indentation for long
- * header lines.
- *
- * @param[in] smtp SMTP client context.
- * @param[in] header See @ref smtp_header.
- * @return See @ref smtp_status_code.
- */
- static enum smtp_status_code
- smtp_print_header(struct smtp *const smtp,
- const struct smtp_header *const header){
- size_t key_len;
- size_t value_len;
- size_t concat_len;
- char *header_concat;
- char *concat;
- char *header_fmt;
- if(header->value == NULL){
- return smtp->status_code;
- }
- key_len = strlen(header->key);
- value_len = strlen(header->value);
- /* concat_len = key_len + 2 + value_len + 1 */
- if(smtp_si_add_size_t(key_len, value_len, &concat_len) ||
- smtp_si_add_size_t(concat_len, 3, &concat_len) ||
- (header_concat = malloc(concat_len)) == NULL){
- return smtp_status_code_set(smtp, SMTP_STATUS_NOMEM);
- }
- concat = smtp_stpcpy(header_concat, header->key);
- concat = smtp_stpcpy(concat, ": ");
- smtp_stpcpy(concat, header->value);
- header_fmt = smtp_fold_whitespace(header_concat, SMTP_LINE_MAX);
- free(header_concat);
- if(header_fmt == NULL){
- return smtp_status_code_set(smtp, SMTP_STATUS_NOMEM);
- }
- smtp_puts_terminate(smtp, header_fmt);
- free(header_fmt);
- return smtp->status_code;
- }
- /**
- * Take a FROM, TO, and CC address and add it into the email header list.
- *
- * The following example shows what the final header might look like when
- * the client sends an email to two CC addresses:
- * Cc: mail1\@example.com, mail2\@example.com
- *
- * @param[in] smtp SMTP client context.
- * @param[in] address_type See @ref smtp_address_type.
- * @param[in] key Header key value, for example, To From Cc.
- * @return See @ref smtp_status_code.
- */
- static enum smtp_status_code
- smtp_append_address_to_header(struct smtp *const smtp,
- enum smtp_address_type address_type,
- const char *const key){
- size_t i;
- size_t num_address_in_header;
- size_t header_value_sz;
- size_t name_slen;
- size_t email_slen;
- size_t concat_len;
- struct smtp_address *address;
- char *header_value;
- char *header_value_new;
- char *concat;
- num_address_in_header = 0;
- header_value_sz = 0;
- header_value = NULL;
- concat_len = 0;
- for(i = 0; i < smtp->num_address; i++){
- address = &smtp->address_list[i];
- if(address->type == address_type){
- name_slen = 0;
- if(address->name){
- name_slen = strlen(address->name);
- }
- email_slen = strlen(address->email);
- /*
- * ', "' NAME '" <' EMAIL > \0
- * header_value_sz += 3 + name_slen + 3 + email_slen + 1 + 1
- */
- if(smtp_si_add_size_t(header_value_sz, name_slen, &header_value_sz) ||
- smtp_si_add_size_t(header_value_sz, email_slen, &header_value_sz) ||
- smtp_si_add_size_t(header_value_sz, 3 + 3 + 1 + 1, &header_value_sz)||
- (header_value_new = realloc(header_value,
- header_value_sz)) == NULL){
- free(header_value);
- return smtp_status_code_set(smtp, SMTP_STATUS_NOMEM);
- }
- header_value = header_value_new;
- concat = header_value + concat_len;
- if(num_address_in_header > 0){
- concat = smtp_stpcpy(concat, ", ");
- }
- if(name_slen){
- concat = smtp_stpcpy(concat, "\"");
- concat = smtp_stpcpy(concat, address->name);
- concat = smtp_stpcpy(concat, "\" ");
- }
- concat = smtp_stpcpy(concat, "<");
- concat = smtp_stpcpy(concat, address->email);
- concat = smtp_stpcpy(concat, ">");
- num_address_in_header += 1;
- concat_len = (size_t)(concat - header_value);
- }
- }
- if(header_value){
- smtp_header_add(smtp, key, header_value);
- free(header_value);
- }
- return smtp->status_code;
- }
- /**
- * Send envelope MAIL FROM or RCPT TO header address.
- *
- * Examples:
- * MAIL FROM:<mail\@example.com>
- * RCPT TO:<mail\@example.com>
- *
- * @param[in] smtp SMTP client context.
- * @param[in] header Either "MAIL FROM" or "RCPT TO".
- * @param[in] address See @ref smtp_address -> email field.
- * @return See @ref smtp_status_code.
- */
- static enum smtp_status_code
- smtp_mail_envelope_header(struct smtp *const smtp,
- const char *const header,
- const struct smtp_address *const address){
- const char *const SMTPUTF8 = " SMTPUTF8";
- const size_t SMTPUTF8_LEN = strlen(SMTPUTF8);
- size_t email_len;
- size_t bufsz;
- char *envelope_address;
- char *concat;
- const char *smtputf8_opt;
- email_len = strlen(address->email);
- /* bufsz = 14 + email_len + SMTPUTF8_LEN + 1 */
- if(smtp_si_add_size_t(email_len, SMTPUTF8_LEN + 14 + 1, &bufsz) ||
- (envelope_address = malloc(bufsz)) == NULL){
- return smtp_status_code_set(smtp, SMTP_STATUS_NOMEM);
- }
- smtputf8_opt = "";
- if(smtp_str_has_nonascii_utf8(address->email)){
- smtputf8_opt = SMTPUTF8;
- }
- concat = smtp_stpcpy(envelope_address, header);
- concat = smtp_stpcpy(concat, ":<");
- concat = smtp_stpcpy(concat, address->email);
- concat = smtp_stpcpy(concat, ">");
- concat = smtp_stpcpy(concat, smtputf8_opt);
- smtp_stpcpy(concat, "\r\n");
- smtp_puts(smtp, envelope_address);
- free(envelope_address);
- if(smtp->status_code != SMTP_STATUS_OK){
- return smtp->status_code;
- }
- smtp_read_and_parse_code(smtp);
- return smtp->status_code;
- }
- /**
- * Comparison function for qsort which sorts headers alphabetically based
- * on the key.
- *
- * @param[in] v1 The first @ref smtp_header to compare.
- * @param[in] v2 The second @ref smtp_header to compare.
- * @retval 0 If the keys match.
- * @retval !0 If the keys do not match.
- */
- static int
- smtp_header_cmp(const void *v1,
- const void *v2){
- const struct smtp_header *header1;
- const struct smtp_header *header2;
- header1 = v1;
- header2 = v2;
- return strcmp(header1->key, header2->key);
- }
- /**
- * Validate characters in the email header key.
- *
- * Must consist only of printable US-ASCII characters except colon.
- *
- * @param[in] key Header key to validate.
- * @retval 0 Successful validation.
- * @retval -1 Failed to validate.
- */
- SMTP_LINKAGE int
- smtp_header_key_validate(const char *const key){
- unsigned char uc;
- size_t i;
- size_t keylen;
- keylen = strlen(key);
- if(keylen < 1){
- return -1;
- }
- for(i = 0; i < keylen; i++){
- uc = (unsigned char)key[i];
- if(uc <= ' ' || uc > 126 || uc == ':'){
- return -1;
- }
- }
- return 0;
- }
- /**
- * Validate characters in the email header contents.
- *
- * Must consist only of printable character, space, or horizontal tab.
- *
- * @param[in] value Header value to validate.
- * @retval 0 Successful validation.
- * @retval -1 Failed to validate.
- */
- SMTP_LINKAGE int
- smtp_header_value_validate(const char *const value){
- size_t i;
- unsigned char uc;
- for(i = 0; value[i]; i++){
- uc = (unsigned char)value[i];
- if((uc < ' ' || uc > 126) &&
- uc != '\t' &&
- uc < 0x80){ /* Allow UTF-8 byte sequence. */
- return -1;
- }
- }
- return 0;
- }
- /**
- * Validate characters in the email address.
- *
- * The email address must consist only of printable characters excluding
- * the angle brackets (<) and (>).
- *
- * @param[in] email The email address of the party.
- * @retval 0 Successful validation.
- * @retval -1 Failed to validate.
- */
- SMTP_LINKAGE int
- smtp_address_validate_email(const char *const email){
- size_t i;
- unsigned char uc;
- for(i = 0; email[i]; i++){
- uc = (unsigned char)email[i];
- if(uc <= ' ' || uc == 127 ||
- uc == '<' || uc == '>'){
- return -1;
- }
- }
- return 0;
- }
- /**
- * Validate characters in the email name.
- *
- * Email user name must consist only of printable characters, excluding the
- * double quote character.
- *
- * @param[in] name Email name to validate.
- * @retval 0 Successful validation.
- * @retval -1 Failed to validate.
- */
- SMTP_LINKAGE int
- smtp_address_validate_name(const char *const name){
- size_t i;
- unsigned char uc;
- for(i = 0; name[i]; i++){
- uc = (unsigned char)name[i];
- if(uc < ' ' || uc == 127 || uc == '\"'){
- return -1;
- }
- }
- return 0;
- }
- /**
- * Validate characters in the attachment file name.
- *
- * Must consist only of printable characters or the space character ( ), and
- * excluding the quote characters (') and (").
- *
- * @param[in] name Filename of the attachment shown to recipients.
- * @retval 0 Successful validation.
- * @retval -1 Failed to validate.
- */
- SMTP_LINKAGE int
- smtp_attachment_validate_name(const char *const name){
- size_t i;
- unsigned char uc;
- for(i = 0; name[i]; i++){
- uc = (unsigned char)name[i];
- if(uc < ' ' || uc == 127 ||
- uc == '\'' || uc == '\"'){
- return -1;
- }
- }
- return 0;
- }
- /**
- * Special flag value for the SMTP context used to determine if the initial
- * memory allocation failed to create the context.
- */
- #define SMTP_FLAG_INVALID_MEMORY (enum smtp_flag)(0xFFFFFFFF)
- /**
- * This error structure used for the single error case where we cannot
- * initially allocate memory. This makes it easier to propagate any
- * error codes when calling the other header functions because the
- * caller will always get a valid SMTP structure returned.
- */
- static struct smtp
- g_smtp_error = {
- SMTP_FLAG_INVALID_MEMORY, /* flags */
- 0, /* sock */
- { /* gdfd */
- NULL, /* _buf */
- 0, /* _bufsz */
- 0, /* _buf_len */
- NULL, /* line */
- 0, /* line_len */
- NULL, /* getdelimfd_read */
- NULL, /* user_data */
- 0, /* delim */
- {0} /* pad */
- }, /* gdfd */
- NULL, /* header_list */
- 0, /* num_headers */
- NULL, /* address_list */
- 0, /* num_address */
- NULL, /* attachment_list */
- 0, /* num_attachment */
- 0, /* timeout_sec */
- SMTP_STATUS_NOMEM, /* smtp_status_code status_code */
- 0, /* tls_on */
- NULL /* cafile */
- #ifdef SMTP_OPENSSL
- ,
- NULL, /* tls */
- NULL, /* tls_ctx */
- NULL /* tls_bio */
- #endif /* SMTP_OPENSSL */
- };
- enum smtp_status_code
- smtp_open(const char *const server,
- const char *const port,
- enum smtp_connection_security connection_security,
- enum smtp_flag flags,
- const char *const cafile,
- struct smtp **smtp){
- struct smtp *snew;
- if((snew = calloc(1, sizeof(**smtp))) == NULL){
- *smtp = &g_smtp_error;
- return smtp_status_code_get(*smtp);
- }
- *smtp = snew;
- snew->flags = flags;
- snew->sock = -1;
- snew->gdfd.delim = '\n';
- snew->gdfd.getdelimfd_read = smtp_str_getdelimfd_read;
- snew->gdfd.user_data = snew;
- snew->cafile = cafile;
- #ifndef SMTP_IS_WINDOWS
- signal(SIGPIPE, SIG_IGN);
- #endif /* !(SMTP_IS_WINDOWS) */
- if(smtp_connect(snew, server, port) < 0){
- return smtp_status_code_set(*smtp, SMTP_STATUS_CONNECT);
- }
- if(smtp_initiate_handshake(snew,
- server,
- connection_security) != SMTP_STATUS_OK){
- return smtp_status_code_set(*smtp, SMTP_STATUS_HANDSHAKE);
- }
- return snew->status_code;
- }
- enum smtp_status_code
- smtp_auth(struct smtp *const smtp,
- enum smtp_authentication_method auth_method,
- const char *const user,
- const char *const pass){
- int auth_rc;
- if(smtp->status_code != SMTP_STATUS_OK){
- return smtp->status_code;
- }
- if(auth_method == SMTP_AUTH_PLAIN){
- auth_rc = smtp_auth_plain(smtp, user, pass);
- }
- else if(auth_method == SMTP_AUTH_LOGIN){
- auth_rc = smtp_auth_login(smtp, user, pass);
- }
- #ifdef SMTP_OPENSSL
- else if(auth_method == SMTP_AUTH_CRAM_MD5){
- auth_rc = smtp_auth_cram_md5(smtp, user, pass);
- }
- #endif /* SMTP_OPENSSL */
- else if(auth_method == SMTP_AUTH_NONE){
- auth_rc = 0;
- }
- else{
- return smtp_status_code_set(smtp, SMTP_STATUS_PARAM);
- }
- if(auth_rc < 0){
- return smtp_status_code_set(smtp, SMTP_STATUS_AUTH);
- }
- return smtp->status_code;
- }
- enum smtp_status_code
- smtp_mail(struct smtp *const smtp,
- const char *const body){
- size_t i;
- int has_mail_from;
- struct smtp_address *address;
- char date[SMTP_DATE_MAX_SZ];
- if(smtp->status_code != SMTP_STATUS_OK){
- return smtp->status_code;
- }
- /* MAIL timeout 5 minutes. */
- smtp_set_read_timeout(smtp, 60 * 5);
- has_mail_from = 0;
- for(i = 0; i < smtp->num_address; i++){
- address = &smtp->address_list[i];
- if(address->type == SMTP_ADDRESS_FROM){
- if(smtp_mail_envelope_header(smtp,
- "MAIL FROM",
- address) != SMTP_STATUS_OK){
- return smtp->status_code;
- }
- has_mail_from = 1;
- break;
- }
- }
- if(!has_mail_from){
- return smtp_status_code_set(smtp, SMTP_STATUS_PARAM);
- }
- /* RCPT timeout 5 minutes. */
- smtp_set_read_timeout(smtp, 60 * 5);
- for(i = 0; i < smtp->num_address; i++){
- address = &smtp->address_list[i];
- if(address->type != SMTP_ADDRESS_FROM){
- if(smtp_mail_envelope_header(smtp,
- "RCPT TO",
- address) != SMTP_STATUS_OK){
- return smtp->status_code;
- }
- }
- }
- /* DATA timeout 2 minutes. */
- smtp_set_read_timeout(smtp, 60 * 2);
- if(smtp_puts(smtp, "DATA\r\n") != SMTP_STATUS_OK){
- return smtp->status_code;
- }
- /* 354 response to DATA must get returned before we can send the message. */
- if(smtp_read_and_parse_code(smtp) != SMTP_BEGIN_MAIL){
- return smtp_status_code_set(smtp, SMTP_STATUS_SERVER_RESPONSE);
- }
- if(!smtp_header_exists(smtp, "Date")){
- if(smtp_date_rfc_2822(date) < 0){
- return smtp_status_code_set(smtp, SMTP_STATUS_DATE);
- }
- if(smtp_header_add(smtp, "Date", date) != SMTP_STATUS_OK){
- return smtp->status_code;
- }
- }
- /* DATA block timeout 3 minutes. */
- smtp_set_read_timeout(smtp, 60 * 3);
- if(smtp_append_address_to_header(smtp,
- SMTP_ADDRESS_FROM,
- "From") != SMTP_STATUS_OK ||
- smtp_append_address_to_header(smtp,
- SMTP_ADDRESS_TO,
- "To") != SMTP_STATUS_OK ||
- smtp_append_address_to_header(smtp,
- SMTP_ADDRESS_CC,
- "Cc") != SMTP_STATUS_OK){
- return smtp->status_code;
- }
- for(i = 0; i < smtp->num_headers; i++){
- if(smtp_print_header(smtp, &smtp->header_list[i]) != SMTP_STATUS_OK){
- return smtp->status_code;
- }
- }
- if(smtp_print_email(smtp, body)){
- return smtp->status_code;
- }
- /* End of DATA segment. */
- if(smtp_puts(smtp, ".\r\n") != SMTP_STATUS_OK){
- return smtp->status_code;
- }
- /* DATA termination timeout 250 return code - 10 minutes. */
- smtp_set_read_timeout(smtp, 60 * 10);
- if(smtp_read_and_parse_code(smtp) != SMTP_DONE){
- return smtp_status_code_set(smtp, SMTP_STATUS_SERVER_RESPONSE);
- }
- return smtp->status_code;
- }
- enum smtp_status_code
- smtp_close(struct smtp *smtp){
- enum smtp_status_code status_code;
- status_code = smtp->status_code;
- if(smtp->flags == SMTP_FLAG_INVALID_MEMORY){
- return status_code;
- }
- if(smtp->sock != -1){
- /*
- * Do not error out if this fails because we still need to free the
- * SMTP client resources.
- */
- smtp->status_code = SMTP_STATUS_OK;
- smtp_puts(smtp, "QUIT\r\n");
- #ifdef SMTP_OPENSSL
- if(smtp->tls_on){
- SSL_free(smtp->tls);
- SSL_CTX_free(smtp->tls_ctx);
- }
- #endif /* SMTP_OPENSSL */
- #ifdef SMTP_IS_WINDOWS
- closesocket(smtp->sock);
- WSACleanup();
- #else /* POSIX */
- if(close(smtp->sock) < 0){
- if(smtp->status_code == SMTP_STATUS_OK){
- smtp_status_code_set(smtp, SMTP_STATUS_CLOSE);
- }
- }
- #endif /* SMTP_IS_WINDOWS */
- }
- smtp_str_getdelimfd_free(&smtp->gdfd);
- smtp_header_clear_all(smtp);
- smtp_address_clear_all(smtp);
- smtp_attachment_clear_all(smtp);
- if(status_code == SMTP_STATUS_OK){
- status_code = smtp->status_code;
- }
- free(smtp);
- return status_code;
- }
- enum smtp_status_code
- smtp_status_code_get(const struct smtp *const smtp){
- return smtp->status_code;
- }
- enum smtp_status_code
- smtp_status_code_clear(struct smtp *const smtp){
- enum smtp_status_code old_status;
- old_status = smtp_status_code_get(smtp);
- smtp_status_code_set(smtp, SMTP_STATUS_OK);
- return old_status;
- }
- enum smtp_status_code
- smtp_status_code_set(struct smtp *const smtp,
- enum smtp_status_code status_code){
- if((unsigned)status_code >= SMTP_STATUS__LAST){
- return smtp_status_code_set(smtp, SMTP_STATUS_PARAM);
- }
- smtp->status_code = status_code;
- return status_code;
- }
- const char *
- smtp_status_code_errstr(enum smtp_status_code status_code){
- const char *const status_code_err_str[] = {
- /* SMTP_STATUS_OK */
- "Success",
- /* SMTP_STATUS_NOMEM */
- "Memory allocation failed",
- /* SMTP_STATUS_CONNECT */
- "Failed to connect to the mail server",
- /* SMTP_STATUS_HANDSHAKE */
- "Failed to handshake or negotiate a TLS connection with the server",
- /* SMTP_STATUS_AUTH */
- "Failed to authenticate with the given credentials",
- /* SMTP_STATUS_SEND */
- "Failed to send bytes to the server",
- /* SMTP_STATUS_RECV */
- "Failed to receive bytes from the server",
- /* SMTP_STATUS_CLOSE */
- "Failed to properly close a connection",
- /* SMTP_STATUS_SERVER_RESPONSE */
- "SMTP server sent back an unexpected status code",
- /* SMTP_STATUS_PARAM */
- "Invalid parameter",
- /* SMTP_STATUS_FILE */
- "Failed to read or open a local file",
- /* SMTP_STATUS_DATE */
- "Failed to get the local date and time",
- /* SMTP_STATUS__LAST */
- "Unknown error"
- };
- if((unsigned)status_code > SMTP_STATUS__LAST){
- status_code = SMTP_STATUS__LAST;
- }
- return status_code_err_str[status_code];
- }
- enum smtp_status_code
- smtp_header_add(struct smtp *const smtp,
- const char *const key,
- const char *const value){
- struct smtp_header *new_header_list;
- struct smtp_header *new_header;
- size_t num_headers_inc;
- if(smtp->status_code != SMTP_STATUS_OK){
- return smtp->status_code;
- }
- if(smtp_header_key_validate(key) < 0){
- return smtp_status_code_set(smtp, SMTP_STATUS_PARAM);
- }
- if(value && smtp_header_value_validate(value) < 0){
- return smtp_status_code_set(smtp, SMTP_STATUS_PARAM);
- }
- if(smtp_si_add_size_t(smtp->num_headers, 1, &num_headers_inc) ||
- (new_header_list = smtp_reallocarray(
- smtp->header_list,
- num_headers_inc,
- sizeof(*smtp->header_list))) == NULL){
- return smtp_status_code_set(smtp, SMTP_STATUS_NOMEM);
- }
- smtp->header_list = new_header_list;
- new_header = &smtp->header_list[smtp->num_headers];
- new_header->key = smtp_strdup(key);
- if(value){
- new_header->value = smtp_strdup(value);
- }
- else{
- new_header->value = NULL;
- }
- if(new_header->key == NULL ||
- (new_header->value == NULL && value)){
- free(new_header->key);
- free(new_header->value);
- return smtp_status_code_set(smtp, SMTP_STATUS_NOMEM);
- }
- smtp->num_headers = num_headers_inc;
- qsort(smtp->header_list,
- smtp->num_headers,
- sizeof(*smtp->header_list),
- smtp_header_cmp);
- return smtp->status_code;
- }
- void
- smtp_header_clear_all(struct smtp *const smtp){
- size_t i;
- struct smtp_header *header;
- for(i = 0; i < smtp->num_headers; i++){
- header = &smtp->header_list[i];
- free(header->key);
- free(header->value);
- }
- free(smtp->header_list);
- smtp->header_list = NULL;
- smtp->num_headers = 0;
- }
- enum smtp_status_code
- smtp_address_add(struct smtp *const smtp,
- enum smtp_address_type type,
- const char *const email,
- const char *const name){
- struct smtp_address *new_address_list;
- struct smtp_address *new_address;
- size_t num_address_inc;
- if(smtp->status_code != SMTP_STATUS_OK){
- return smtp->status_code;
- }
- if(smtp_address_validate_email(email) < 0){
- return smtp_status_code_set(smtp, SMTP_STATUS_PARAM);
- }
- if(name && smtp_address_validate_name(name) < 0){
- return smtp_status_code_set(smtp, SMTP_STATUS_PARAM);
- }
- if(smtp_si_add_size_t(smtp->num_address, 1, &num_address_inc)){
- return smtp_status_code_set(smtp, SMTP_STATUS_NOMEM);
- }
- new_address_list = smtp_reallocarray(smtp->address_list,
- num_address_inc,
- sizeof(*new_address_list));
- if(new_address_list == NULL){
- return smtp_status_code_set(smtp, SMTP_STATUS_NOMEM);
- }
- new_address = &new_address_list[smtp->num_address];
- smtp->address_list = new_address_list;
- new_address->type = type;
- new_address->email = smtp_strdup(email);
- if(name){
- new_address->name = smtp_strdup(name);
- }
- else{
- new_address->name = NULL;
- }
- if(new_address->email == NULL ||
- (new_address->name == NULL && name)){
- free(new_address->email);
- free(new_address->name);
- return smtp_status_code_set(smtp, SMTP_STATUS_NOMEM);
- }
- smtp->num_address = num_address_inc;
- return smtp->status_code;
- }
- void
- smtp_address_clear_all(struct smtp *const smtp){
- size_t i;
- struct smtp_address *address;
- for(i = 0; i < smtp->num_address; i++){
- address = &smtp->address_list[i];
- free(address->email);
- free(address->name);
- }
- free(smtp->address_list);
- smtp->address_list = NULL;
- smtp->num_address = 0;
- }
- enum smtp_status_code
- smtp_attachment_add_path(struct smtp *const smtp,
- const char *const name,
- const char *const path){
- char *data;
- size_t bytes_read;
- if(smtp->status_code != SMTP_STATUS_OK){
- return smtp->status_code;
- }
- errno = 0;
- if((data = smtp_file_get_contents(path, &bytes_read)) == NULL){
- if(errno == ENOMEM){
- return smtp_status_code_set(smtp, SMTP_STATUS_NOMEM);
- }
- return smtp_status_code_set(smtp, SMTP_STATUS_FILE);
- }
- smtp_attachment_add_mem(smtp, name, data, bytes_read);
- free(data);
- return smtp->status_code;
- }
- enum smtp_status_code
- smtp_attachment_add_fp(struct smtp *const smtp,
- const char *const name,
- FILE *fp){
- char *data;
- size_t bytes_read;
- if(smtp->status_code != SMTP_STATUS_OK){
- return smtp->status_code;
- }
- errno = 0;
- if((data = smtp_ffile_get_contents(fp, &bytes_read)) == NULL){
- if(errno == ENOMEM){
- return smtp_status_code_set(smtp, SMTP_STATUS_NOMEM);
- }
- return smtp_status_code_set(smtp, SMTP_STATUS_FILE);
- }
- smtp_attachment_add_mem(smtp, name, data, bytes_read);
- free(data);
- return smtp->status_code;
- }
- enum smtp_status_code
- smtp_attachment_add_mem(struct smtp *const smtp,
- const char *const name,
- const void *const data,
- size_t datasz){
- size_t num_attachment_inc;
- struct smtp_attachment *new_attachment_list;
- struct smtp_attachment *new_attachment;
- char *b64_encode;
- if(smtp->status_code != SMTP_STATUS_OK){
- return smtp->status_code;
- }
- if(smtp_attachment_validate_name(name) < 0){
- return smtp_status_code_set(smtp, SMTP_STATUS_PARAM);
- }
- if(datasz == SIZE_MAX){
- datasz = strlen(data);
- }
- if(smtp_si_add_size_t(smtp->num_attachment, 1, &num_attachment_inc) ||
- (new_attachment_list = smtp_reallocarray(
- smtp->attachment_list,
- num_attachment_inc,
- sizeof(*new_attachment_list))) == NULL){
- return smtp_status_code_set(smtp, SMTP_STATUS_NOMEM);
- }
- smtp->attachment_list = new_attachment_list;
- new_attachment = &new_attachment_list[smtp->num_attachment];
- new_attachment->name = smtp_strdup(name);
- b64_encode = smtp_base64_encode(data, datasz);
- if(new_attachment->name == NULL || b64_encode == NULL){
- free(new_attachment->name);
- free(b64_encode);
- return smtp_status_code_set(smtp, SMTP_STATUS_NOMEM);
- }
- new_attachment->b64_data = smtp_chunk_split(b64_encode,
- SMTP_LINE_MAX,
- "\r\n");
- free(b64_encode);
- if(new_attachment->b64_data == NULL){
- free(new_attachment->name);
- return smtp_status_code_set(smtp, SMTP_STATUS_NOMEM);
- }
- smtp->num_attachment = num_attachment_inc;
- return smtp->status_code;
- }
- void
- smtp_attachment_clear_all(struct smtp *const smtp){
- size_t i;
- struct smtp_attachment *attachment;
- for(i = 0; i < smtp->num_attachment; i++){
- attachment = &smtp->attachment_list[i];
- free(attachment->name);
- free(attachment->b64_data);
- }
- free(smtp->attachment_list);
- smtp->attachment_list = NULL;
- smtp->num_attachment = 0;
- }
- /////////////////////////////////////////////////////
- #include <stdint.h>
- #include "common.h"
- #include "thread.h"
- #include "xlist.h"
- #include "elog.h"
- #include "lock.h"
- #include "sqlite_handle.h"
- #if 1
- #define LOGD log_d
- #define LOGE log_e
- #define LOGW log_w
- #else
- #define LOGD printf
- #define LOGE printf
- #define LOGW printf
- #endif
- #define MAIL_CONNECTION_SECURITY SMTP_SECURITY_STARTTLS
- #define MAIL_FLAGS (SMTP_DEBUG|SMTP_NO_CERT_VERIFY) /* Do not verify cert. */
- #define MAIL_CAFILE NULL
- #define MAIL_AUTH SMTP_AUTH_PLAIN
- static int send_mail(smtp_send_t *send, void *addr, char *sub, char *body, int multi)
- {
- int i,r=-1,flag=0;
- struct smtp *smtp=NULL;
- enum smtp_flag flags=(SMTP_DEBUG|SMTP_NO_CERT_VERIFY);
- enum smtp_connection_security security=SMTP_SECURITY_STARTTLS;
- enum smtp_authentication_method auth=SMTP_AUTH_PLAIN;
- lock_s_hold(LOCK_ID_SMTP);
- if(!sub || !body || send->mode==0) {
- goto out;
- }
- if(strcmp(send->auth, "LOGIN")==0) {
- auth = SMTP_AUTH_LOGIN;
- }
- else if(strcmp(send->auth, "CRAM-MD5")==0) {
- auth = SMTP_AUTH_CRAM_MD5;
- }
- if(strcmp(send->port,"465")==0) {
- security = SMTP_SECURITY_TLS;
- }
-
- r = smtp_open(send->server, send->port, security, flags, NULL, &smtp);
- if(r != SMTP_STATUS_OK) {
- LOGE("___ smtp_open failed, %s\n", smtp_status_code_errstr(r));
- return -1;
- }
-
- r = smtp_auth(smtp, auth, send->account, send->password);
- if(r != SMTP_STATUS_OK) {
- LOGE("___ smtp_auth failed, %s\n", smtp_status_code_errstr(r));
- goto quit;
- }
-
- r = smtp_address_add(smtp, SMTP_ADDRESS_FROM, send->account, send->account);
- if(r != SMTP_STATUS_OK) {
- LOGE("___ smtp_address_add fr failed\n, %s", smtp_status_code_errstr(r));
- goto quit;
- }
-
- if(multi) {
- smtp_recv_t *recv=(smtp_recv_t*)addr;
- for(i=0; i<SMTP_RECV_MAX; i++) {
- if(recv[i].use && strlen(recv[i].account)>0) {
- r = smtp_address_add(smtp, SMTP_ADDRESS_TO, recv[i].account, recv[i].account);
- if(r != SMTP_STATUS_OK) {
- LOGE("___ smtp_address_add to %s failed, %s\n", recv[i].account, smtp_status_code_errstr(r));
- }
- else {
- flag = 1;
- }
- }
- }
- }
- else {
- char *sendto=(char*)addr;
- r = smtp_address_add(smtp, SMTP_ADDRESS_TO, sendto, sendto);
- if(r != SMTP_STATUS_OK) {
- LOGE("___ smtp_address_add to %s failed, %s\n", sendto, smtp_status_code_errstr(r));
- }
- else {
- flag = 1;
- }
- }
-
- if(flag>0) {
- r = smtp_header_add(smtp, "Subject", sub);
- if(r != SMTP_STATUS_OK) {
- LOGE("___ smtp_header_add failed, %s\n", smtp_status_code_errstr(r));
- goto quit;
- }
-
- //r = smtp_attachment_add_mem(smtp, "test.txt", "Test email attachment.", -1);
- smtp_mail(smtp, body);
- }
-
- quit:
- smtp_close(smtp);
- if(r != SMTP_STATUS_OK){
- LOGE("___ send_mail failed, server: %s, port: %s failed\n", send->server, send->port);
- }
- out:
- lock_s_release(LOCK_ID_SMTP);
- return r;
- }
- typedef struct {
- char sub[128];
- char body[2048];
- }smtp_pkt_t;
- typedef struct {
- handle_t list;
- smtp_info_t *info;
- GlobalDeviceManager *dm;
- }smtp_handle_t;
- static smtp_handle_t smHandle;
- static void sleep_ms(int ms)
- {
- usleep(ms*1000);
- }
- static void* smtp_thread(void *arg)
- {
- int r;
- thread_handle_t *th=(thread_handle_t*)arg;
- smtp_handle_t *h=(smtp_handle_t*)th->arg;
- list_node_t *ln=NULL;
- smtp_pkt_t *pkt=NULL;
- smtp_send_t *send=&h->info->send;
- smtp_recv_t *recv=h->info->recv;
- while(th->quit==0) {
- r = xlist_take_node(h->list, &ln, 0);
- if(r==0) {
- pkt = (smtp_pkt_t*)ln->data.buf;
- send_mail(send, recv, pkt->sub, pkt->body, 1);
- }
- xlist_back_node(h->list, ln);
- if(r) {
- sleep_ms(300);
- }
- }
- pthread_exit(NULL);
- }
- #endif
- int smtp_init(void)
- {
- #ifdef USE_SMTP
- list_cfg_t lc;
- smtp_handle_t *h=&smHandle;
- GlobalDeviceManager *dm=&__globalDeviceManage;
- lc.mode = LIST_FULL_FIFO;
- lc.max = 100;
- lc.log = 0;
- h->list = xlist_init(&lc);
- h->dm = dm;
- h->info = &h->dm->smtpInfo;
- dev_smtp_init(dm->db, h->info);
- thread_start(THREAD_ID_SMTP, smtp_thread, h, 5*MB, 0);
- //smtp_test();
- #endif
- return 0;
- }
- int smtp_deinit(void)
- {
- #ifdef USE_SMTP
- smtp_handle_t *h=&smHandle;
- thread_stop(THREAD_ID_SMTP);
- xlist_free(h->list);
- #endif
- return 0;
- }
- static int post_data(char *sub, char *body)
- {
- int r=0;
- #ifdef USE_SMTP
- smtp_pkt_t pkt;
- smtp_handle_t *h=&smHandle;
- snprintf(pkt.sub, sizeof(pkt.sub), "%s", sub);
- snprintf(pkt.body, sizeof(pkt.body), "%s", body);
- r = xlist_append(h->list, 0, &pkt, sizeof(pkt));
- #endif
- return r;
- }
- int smtp_send(char *sub, char *body)
- {
- int r=0;
- #ifdef USE_SMTP
- smtp_handle_t *h=&smHandle;
- r = send_mail(&h->info->send, h->info->recv, sub, body, 1);
- #endif
- return r;
- }
- int smtp_post(char *sub, char *body)
- {
- int r=0;
- #ifdef USE_SMTP
- r = post_data(sub, body);
- #endif
- return r;
- }
- int smtp_post_alarm(int type, int subtype, int alarm_type, char *content)
- {
- int r=0;
- #ifdef USE_SMTP
- smtp_pkt_t pkt;
- pkt.sub[0] = pkt.body[0] = 0;
- switch(type) {
- case ALARM_TYPE_POWER:
- {
- switch(alarm_type) {
- case WARNING_V_UP:
- //strcpy(pkt.sub, "");
- break;
- case WARNING_V_DOWN:
- break;
- case WARNING_A_UP:
- break;
- case WARNING_W_UP:
- break;
- case WARNING_P_UP:
- break;
- case 0 ... 2:
- break;
- default:
- return -1;
- }
- }
- break;
- case ALARM_TYPE_SENSOR:
- {
- switch(subtype) {
- case SENSOR_TYPE_TEMPERATURE:
- break;
- case SENSOR_TYPE_SMOKE:
- break;
- case SENSOR_TYPE_WATER:
- break;
- case SENSOR_TYPE_DOOR:
- break;
- case SENSOR_TYPE_GAS:
- break;
- case SENSOR_TYPE_AIR_PRESS:
- break;
- case SENSOR_TYPE_DoubleT:
- break;
- default:
- return -1;
- }
- }
- break;
- case ALARM_TYPE_NETWORK:
- {
- //
- }
- break;
- default:
- return -1;
- }
- snprintf(pkt.sub, sizeof(pkt.sub), "ALARM");
- snprintf(pkt.body, sizeof(pkt.body), content);
- r = post_data(pkt.sub, pkt.body);
- #endif
- return r;
- }
- int smtp_test(void)
- {
- int r=0,cnt=0;
- char temp[200];
- smtp_handle_t *h=&smHandle;
- #ifdef USE_SMTP
- smtp_info_t info={
- .send = {
- .product_id = 0,
- .mode = 1,
- .account = "rcp064867@163.com",
- .password = "UODRIFWDFBTJTVLW",
- .server = "smtp.163.com",
- .port = "25",
- //.port = "465",
- //.port = "587",
- .auth = "PLAIN",
- //.auth = "LOGIN",
- //.auth = "CRAM-MD5",
- },
- .recv = {
- {0, 1, "1556907721@qq.com"},
- {1, 1, "rcp064867@163.com"},
- {2, 1, "cijiu0219@sina.com"},
- {3, 1, "jiuci4902@gmail.com"},
- {4, 1, "cijiu0219@hotmail.com"},
- {5, 1, "cijiu990219@outlook.com"},
- }
- };
- *h->info = info;
- char *sub="123";
- char *body="hello everyone";
- while(cnt++<5) {
- sprintf(temp, "%s %d", body, cnt);
- r = smtp_send(sub, temp);
- sleep(2);
- }
- #endif
- return r;
- }
|