1 #if !defined(lint) && !defined(DOS)
2 static char rcsid
[] = "$Id: imap.c 1142 2008-08-13 17:22:21Z hubert@u.washington.edu $";
6 * ========================================================================
7 * Copyright 2013-2020 Eduardo Chappa
8 * Copyright 2006-2008 University of Washington
10 * Licensed under the Apache License, Version 2.0 (the "License");
11 * you may not use this file except in compliance with the License.
12 * You may obtain a copy of the License at
14 * http://www.apache.org/licenses/LICENSE-2.0
16 * ========================================================================
19 #include "../pith/headers.h"
20 #include "../pith/imap.h"
21 #include "../pith/msgno.h"
22 #include "../pith/state.h"
23 #include "../pith/flag.h"
24 #include "../pith/pineelt.h"
25 #include "../pith/status.h"
26 #include "../pith/conftype.h"
27 #include "../pith/context.h"
28 #include "../pith/thread.h"
29 #include "../pith/mailview.h"
30 #include "../pith/mailpart.h"
31 #include "../pith/mailindx.h"
32 #include "../pith/mailcmd.h"
33 #include "../pith/save.h"
34 #include "../pith/util.h"
35 #include "../pith/stream.h"
36 #include "../pith/newmail.h"
37 #include "../pith/icache.h"
38 #include "../pith/options.h"
41 #include "../pico/osdep/mswin.h"
48 long imap_seq_exec(MAILSTREAM
*, char *, long (*)(MAILSTREAM
*, long, void *), void *);
49 long imap_seq_exec_append(MAILSTREAM
*, long, void *);
54 * Exported globals setup by searching functions to tell mm_searched
55 * where to put message numbers that matched the search criteria,
56 * and to allow mm_searched to return number of matches.
58 MAILSTREAM
*mm_search_stream
;
59 long mm_search_count
= 0L;
60 MAILSTATUS mm_status_result
;
62 MM_LIST_S
*mm_list_info
;
64 MMLOGIN_S
*mm_login_list
= NULL
;
65 MMLOGIN_S
*cert_failure_list
= NULL
;
68 * Instead of storing cached passwords in free storage, store them in this
69 * private space. This makes it easier and more reliable when we want
70 * to zero this space out. We only store passwords here (char *) so we
71 * don't need to worry about alignment.
73 static volatile char private_store
[1024];
75 static int critical_depth
= 0;
77 /* hook to hang callback on "current" message expunge */
78 void (*pith_opt_current_expunged
)(long unsigned int);
81 RETSIGTYPE (*hold_int
)(int);
85 RETSIGTYPE (*hold_term
)(int);
89 RETSIGTYPE (*hold_hup
)(int);
93 RETSIGTYPE (*hold_usr2
)(int);
98 /*----------------------------------------------------------------------
99 receive notification that search found something
101 Input: mail stream and message number of located item
103 Result: nothing, not used by pine
106 mm_searched(MAILSTREAM
*stream
, long unsigned int rawno
)
110 if(rawno
> 0L && stream
&& rawno
<= stream
->nmsgs
111 && (mc
= mail_elt(stream
, rawno
))){
113 if(stream
== mm_search_stream
)
119 /*----------------------------------------------------------------------
120 receive notification of new mail from imap daemon
122 Args: stream -- The stream the message count report is for.
123 number -- The number of messages now in folder.
125 Result: Sets value in pine state indicating new mailbox size
127 Called when the number of messages in the mailbox goes up. This
128 may also be called as a result of an expunge. It increments the
129 new_mail_count based on a the difference between the current idea of
130 the maximum number of messages and what mm_exists claims. The new mail
131 notification is done in newmail.c
133 Only worry about the cases when the number grows, as mm_expunged
138 mm_exists(MAILSTREAM
*stream
, long unsigned int number
)
140 long new_this_call
, n
;
141 int exbits
= 0, lflags
= 0;
145 if(ps_global
->debug_imap
> 1 || ps_global
->debugmem
)
146 dprint((3, "=== mm_exists(%lu,%s) called ===\n", number
,
147 !stream
? "(no stream)" : !stream
->mailbox
? "(null)" : stream
->mailbox
));
150 msgmap
= sp_msgmap(stream
);
154 if(mn_get_nmsgs(msgmap
) != (long) number
){
155 sp_set_mail_box_changed(stream
, 1);
156 /* titlebar will be affected */
157 if(ps_global
->mail_stream
== stream
)
158 ps_global
->mangled_header
= 1;
161 if(mn_get_nmsgs(msgmap
) < (long) number
){
162 new_this_call
= (long) number
- mn_get_nmsgs(msgmap
);
163 sp_set_new_mail_count(stream
,
164 sp_new_mail_count(stream
) + new_this_call
);
165 sp_set_recent_since_visited(stream
,
166 sp_recent_since_visited(stream
) + new_this_call
);
168 mn_add_raw(msgmap
, new_this_call
);
171 * Set local "recent" and "hidden" bits...
173 for(n
= 0; n
< new_this_call
; n
++, number
--){
174 if(msgno_exceptions(stream
, number
, "0", &exbits
, FALSE
))
175 exbits
|= MSG_EX_RECENT
;
177 exbits
= MSG_EX_RECENT
;
179 msgno_exceptions(stream
, number
, "0", &exbits
, TRUE
);
181 if(SORT_IS_THREADED(msgmap
))
185 * If we're zoomed, then hide this message too since
186 * it couldn't have possibly been selected yet...
188 lflags
|= (any_lflagged(msgmap
, MN_HIDE
) ? MN_HIDE
: 0);
190 set_lflag(stream
, msgmap
, mn_get_total(msgmap
) - n
, lflags
, 1);
196 /*----------------------------------------------------------------------
197 Receive notification from IMAP that a single message has been expunged
199 Args: stream -- The stream/folder the message is expunged from
200 rawno -- The raw message number that was expunged
202 mm_expunged is always called on an expunge. Simply remove all
203 reference to the expunged message, shifting internal mappings as
207 mm_expunged(MAILSTREAM
*stream
, long unsigned int rawno
)
209 MESSAGECACHE
*mc
= NULL
;
215 if(ps_global
->debug_imap
> 1 || ps_global
->debugmem
)
216 dprint((3, "mm_expunged(%s,%lu)\n",
224 msgmap
= sp_msgmap(stream
);
228 if(ps_global
->mail_stream
== stream
)
231 if((i
= mn_raw2m(msgmap
, (long) rawno
)) != 0L){
232 dprint((7, "mm_expunged: rawno=%lu msgno=%ld nmsgs=%ld max_msgno=%ld flagged_exld=%ld\n", rawno
, i
, mn_get_nmsgs(msgmap
), mn_get_total(msgmap
), msgmap
->flagged_exld
));
234 sp_set_mail_box_changed(stream
, 1);
235 sp_set_expunge_count(stream
, sp_expunge_count(stream
) + 1);
238 reset_check_point(stream
);
239 ps_global
->mangled_header
= 1;
241 /* flush invalid cache entries */
242 while(i
<= mn_get_total(msgmap
))
243 clear_index_cache_ent(stream
, i
++, 0);
245 /* let app know what happened */
246 if(pith_opt_current_expunged
)
247 (*pith_opt_current_expunged
)(rawno
);
252 "mm_expunged: rawno=%lu was excluded, flagged_exld was %d\n",
253 rawno
, msgmap
->flagged_exld
));
254 dprint((7, " nmsgs=%ld max_msgno=%ld\n",
255 mn_get_nmsgs(msgmap
), mn_get_total(msgmap
)));
256 if(rawno
> 0L && rawno
<= stream
->nmsgs
)
257 mc
= mail_elt(stream
, rawno
);
260 dprint((7, " cannot get mail_elt(%lu)\n",
263 else if(!mc
->sparep
){
264 dprint((7, " mail_elt(%lu)->sparep is NULL\n",
268 dprint((7, " mail_elt(%lu)->sparep->excluded=%d\n",
269 rawno
, (int) (((PINELT_S
*) mc
->sparep
)->excluded
)));
273 if(SORT_IS_THREADED(msgmap
)
275 || ps_global
->thread_disp_style
!= THREAD_NONE
)){
279 * When we're sorting with a threaded method an expunged
280 * message may cause the rest of the sort to be wrong. This
281 * isn't so bad if we're just looking at the index. However,
282 * it also causes the thread tree (PINETHRD_S) to become
283 * invalid, so if we're using a threading view we need to
284 * sort in order to fix the tree and to protect fetch_thread().
286 sp_set_need_to_rethread(stream
, 1);
289 * If we expunged the current message which was a member of the
290 * viewed thread, and the adjustment to current will take us
291 * out of that thread, fix it if we can, by backing current up
292 * into the thread. We'd like to just check after mn_flush_raw
293 * below but the problem is that the elts won't change until
294 * after we return from mm_expunged. So we have to manually
295 * check the other messages for CHID2 flags instead of thinking
296 * that we can expunge the current message and then check. It won't
297 * work because the elt will still refer to the expunged message.
299 if(sp_viewing_a_thread(stream
)
300 && get_lflag(stream
, NULL
, rawno
, MN_CHID2
)
301 && mn_total_cur(msgmap
) == 1
302 && mn_is_cur(msgmap
, mn_raw2m(msgmap
, (long) rawno
))
303 && (cur
= mn_get_cur(msgmap
)) > 1L
304 && cur
< mn_get_total(msgmap
)
305 && !get_lflag(stream
, msgmap
, cur
+ 1L, MN_CHID2
)
306 && get_lflag(stream
, msgmap
, cur
- 1L, MN_CHID2
))
307 mn_set_cur(msgmap
, cur
- 1L);
311 * Keep on top of our special flag counts.
313 * NOTE: This is allowed since mail_expunged releases
314 * data for this message after the callback.
316 if(rawno
> 0L && rawno
<= stream
->nmsgs
&& (mc
= mail_elt(stream
, rawno
))){
317 PINELT_S
*pelt
= (PINELT_S
*) mc
->sparep
;
321 msgmap
->flagged_hid
--;
324 msgmap
->flagged_exld
--;
327 msgmap
->flagged_tmp
--;
330 msgmap
->flagged_chid
--;
333 msgmap
->flagged_chid2
--;
336 msgmap
->flagged_coll
--;
339 msgmap
->flagged_stmp
--;
342 msgmap
->flagged_usor
--;
345 msgmap
->flagged_srch
--;
347 if(pelt
->hidden
|| pelt
->colhid
)
348 msgmap
->flagged_invisible
--;
350 free_pine_elt(&mc
->sparep
);
355 * if it's in the sort array, flush it, otherwise
356 * decrement raw sequence numbers greater than "rawno"
358 mn_flush_raw(msgmap
, (long) rawno
);
363 mm_flags(MAILSTREAM
*stream
, long unsigned int rawno
)
366 * The idea here is to clean up any data pine might have cached
367 * that has anything to do with the indicated message number.
369 if(stream
== ps_global
->mail_stream
){
373 if(scores_are_used(SCOREUSE_GET
) & SCOREUSE_STATEDEP
)
374 clear_msg_score(stream
, rawno
);
376 msgno
= mn_raw2m(sp_msgmap(stream
), (long) rawno
);
378 /* if in thread index */
380 if((thrd
= fetch_thread(stream
, rawno
))
382 && (thrd
= fetch_thread(stream
, thrd
->top
))
384 && (t
= mn_raw2m(sp_msgmap(stream
), thrd
->rawno
)))
385 clear_index_cache_ent(stream
, t
, 0);
387 else if(THREADING()){
389 clear_index_cache_ent(stream
, msgno
, 0);
392 * If a parent is collapsed, clear that parent's
395 if((thrd
= fetch_thread(stream
, rawno
)) && thrd
->parent
){
396 thrd
= fetch_thread(stream
, thrd
->parent
);
398 if(get_lflag(stream
, NULL
, thrd
->rawno
, MN_COLL
)
399 && (t
= mn_raw2m(sp_msgmap(stream
), (long) thrd
->rawno
)))
400 clear_index_cache_ent(stream
, t
, 0);
403 thrd
= fetch_thread(stream
, thrd
->parent
);
410 clear_index_cache_ent(stream
, msgno
, 0);
412 if(msgno
&& mn_is_cur(sp_msgmap(stream
), msgno
))
413 ps_global
->mangled_header
= 1;
417 * We count up flag changes here. The
418 * dont_count_flagchanges variable tries to prevent us from
419 * counting when we're just fetching flags.
421 if(!(ps_global
->dont_count_flagchanges
422 && stream
== ps_global
->mail_stream
)){
425 check_point_change(stream
);
427 /* we also note flag changes for filtering purposes */
428 if(msgno_exceptions(stream
, rawno
, "0", &exbits
, FALSE
))
429 exbits
|= MSG_EX_STATECHG
;
431 exbits
= MSG_EX_STATECHG
;
433 msgno_exceptions(stream
, rawno
, "0", &exbits
, TRUE
);
439 mm_list(MAILSTREAM
*stream
, int delimiter
, char *mailbox
, long int attributes
)
442 if(ps_global
->debug_imap
> 2 || ps_global
->debugmem
)
443 dprint((5, "mm_list \"%s\": delim: '%c', %s%s%s%s%s%s\n",
444 mailbox
? mailbox
: "?", delimiter
? delimiter
: 'X',
445 (attributes
& LATT_NOINFERIORS
) ? ", no inferiors" : "",
446 (attributes
& LATT_NOSELECT
) ? ", no select" : "",
447 (attributes
& LATT_MARKED
) ? ", marked" : "",
448 (attributes
& LATT_UNMARKED
) ? ", unmarked" : "",
449 (attributes
& LATT_HASCHILDREN
) ? ", has children" : "",
450 (attributes
& LATT_HASNOCHILDREN
) ? ", has no children" : ""));
453 if(!mm_list_info
->stream
|| stream
== mm_list_info
->stream
)
454 (*mm_list_info
->filter
)(stream
, mailbox
, delimiter
,
455 attributes
, mm_list_info
->data
,
456 mm_list_info
->options
);
461 mm_lsub(MAILSTREAM
*stream
, int delimiter
, char *mailbox
, long int attributes
)
464 if(ps_global
->debug_imap
> 2 || ps_global
->debugmem
)
465 dprint((5, "LSUB \"%s\": delim: '%c', %s%s%s%s%s%s\n",
466 mailbox
? mailbox
: "?", delimiter
? delimiter
: 'X',
467 (attributes
& LATT_NOINFERIORS
) ? ", no inferiors" : "",
468 (attributes
& LATT_NOSELECT
) ? ", no select" : "",
469 (attributes
& LATT_MARKED
) ? ", marked" : "",
470 (attributes
& LATT_UNMARKED
) ? ", unmarked" : "",
471 (attributes
& LATT_HASCHILDREN
) ? ", has children" : "",
472 (attributes
& LATT_HASNOCHILDREN
) ? ", has no children" : ""));
475 if(!mm_list_info
->stream
|| stream
== mm_list_info
->stream
)
476 (*mm_list_info
->filter
)(stream
, mailbox
, delimiter
,
477 attributes
, mm_list_info
->data
,
478 mm_list_info
->options
);
483 mm_status(MAILSTREAM
*stream
, char *mailbox
, MAILSTATUS
*status
)
486 * We implement mail_status for the #move namespace by adding a wrapper
487 * routine, pine_mail_status. It may have to call the real mail_status
488 * twice for #move folders and combine the results. It sets
489 * pine_cached_status to point to a local status variable to store the
490 * intermediate results.
493 if(pine_cached_status
!= NULL
)
494 *pine_cached_status
= *status
;
496 mm_status_result
= *status
;
501 if(pine_cached_status
)
503 "mm_status: Preliminary pass for #move\n"));
505 dprint((2, "mm_status: Mailbox \"%s\"",
506 mailbox
? mailbox
: "?"));
507 if(status
->flags
& SA_MESSAGES
)
508 dprint((2, ", %lu messages", status
->messages
));
510 if(status
->flags
& SA_RECENT
)
511 dprint((2, ", %lu recent", status
->recent
));
513 if(status
->flags
& SA_UNSEEN
)
514 dprint((2, ", %lu unseen", status
->unseen
));
516 if(status
->flags
& SA_UIDVALIDITY
)
517 dprint((2, ", %lu UID validity", status
->uidvalidity
));
519 if(status
->flags
& SA_UIDNEXT
)
520 dprint((2, ", %lu next UID", status
->uidnext
));
528 /*----------------------------------------------------------------------
529 Write imap debugging information into log file
531 Args: strings -- the string for the debug file
533 Result: message written to the debug log file
536 mm_dlog(char *string
)
538 char *p
, *q
= NULL
, save
, *continued
;
542 mswin_imaptelemetry(string
);
548 /* because string can be really long and we don't want to lose any of it */
556 continued
= "(Continuation line) ";
559 if(strlen(p
) > 63000){
567 dprint(((ps_global
->debug_imap
>= 4 && debug
< 4) ? debug
: 4,
568 "IMAP DEBUG %s%s: %s\n",
569 continued
? continued
: "",
570 debug_time(1, ps_global
->debug_timestamp
, ps_global
->signal_in_progress
), p
? p
: "?"));
578 /*----------------------------------------------------------------------
579 Ignore signals when imap is running through critical code
581 Args: stream -- The stream on which critical operation is proceeding
585 mm_critical(MAILSTREAM
*stream
)
587 stream
= stream
; /* For compiler complaints that this isn't used */
589 if(++critical_depth
== 1){
592 hold_hup
= signal(SIGHUP
, SIG_IGN
);
595 hold_usr2
= signal(SIGUSR2
, SIG_IGN
);
598 hold_int
= signal(SIGINT
, SIG_IGN
);
601 hold_term
= signal(SIGTERM
, SIG_IGN
);
605 dprint((9, "IMAP critical (depth now %d) on %s\n",
607 (stream
&& stream
->mailbox
) ? stream
->mailbox
: "<no folder>" ));
611 /*----------------------------------------------------------------------
612 Reset signals after critical imap code
615 mm_nocritical(MAILSTREAM
*stream
)
617 stream
= stream
; /* For compiler complaints that this isn't used */
619 if(--critical_depth
== 0){
622 (void)signal(SIGHUP
, hold_hup
);
625 (void)signal(SIGUSR2
, hold_usr2
);
628 (void)signal(SIGINT
, hold_int
);
631 (void)signal(SIGTERM
, hold_term
);
635 critical_depth
= MAX(critical_depth
, 0);
637 dprint((9, "Done with IMAP critical (depth now %d) on %s\n",
639 (stream
&& stream
->mailbox
) ? stream
->mailbox
: "<no folder>" ));
644 mm_fatal(char *message
)
646 alpine_panic(message
);
651 imap_referral(MAILSTREAM
*stream
, char *ref
, long int code
)
655 if(ref
&& !struncmp(ref
, "imap://", 7)){
657 imapuid_t uid_val
, uid
;
660 rv
= url_imap_folder(ref
, &folder
, &uid
, &uid_val
, NULL
, 1);
664 if((rv
& URL_IMAP_IMAILBOXLIST
) && (rv
& URL_IMAP_ISERVERONLY
))
665 buf
= cpystr(folder
);
674 case REFUNSUBSCRIBE
:
678 if(rv
& URL_IMAP_IMESSAGELIST
)
679 buf
= cpystr(folder
);
688 fs_give((void **) &folder
);
696 imap_proxycopy(MAILSTREAM
*stream
, char *sequence
, char *mailbox
, long int flags
)
701 args
.folder
= mailbox
;
704 if(pith_opt_save_size_changed_prompt
)
705 (*pith_opt_save_size_changed_prompt
)(0L, SSCP_INIT
);
707 ret
= imap_seq_exec(stream
, sequence
, imap_seq_exec_append
, &args
);
709 if(pith_opt_save_size_changed_prompt
)
710 (*pith_opt_save_size_changed_prompt
)(0L, SSCP_END
);
717 imap_seq_exec(MAILSTREAM
*stream
, char *sequence
,
718 long int (*func
)(MAILSTREAM
*, long int, void *),
723 while (*sequence
) { /* while there is something to parse */
724 if (*sequence
== '*') { /* maximum message */
725 if(!(i
= stream
->nmsgs
)){
726 mm_log ("No messages, so no maximum message number",ERROR
);
730 sequence
++; /* skip past * */
732 else if (!(i
= strtoul ((const char *) sequence
,&sequence
,10))
733 || (i
> stream
->nmsgs
)){
734 mm_log ("Sequence invalid",ERROR
);
738 switch (*sequence
) { /* see what the delimiter is */
739 case ':': /* sequence range */
740 if (*++sequence
== '*') { /* maximum message */
741 if (stream
->nmsgs
) j
= stream
->nmsgs
;
743 mm_log ("No messages, so no maximum message number",ERROR
);
747 sequence
++; /* skip past * */
749 /* parse end of range */
750 else if (!(j
= strtoul ((const char *) sequence
,&sequence
,10)) ||
751 (j
> stream
->nmsgs
)) {
752 mm_log ("Sequence range invalid",ERROR
);
756 if (*sequence
&& *sequence
++ != ',') {
757 mm_log ("Sequence range syntax error",ERROR
);
761 if (i
> j
) { /* swap the range if backwards */
766 if(!(*func
)(stream
, i
++, args
))
770 case ',': /* single message */
771 ++sequence
; /* skip the delimiter, fall into end case */
772 case '\0': /* end of sequence */
773 if(!(*func
)(stream
, i
, args
))
777 default: /* anything else is a syntax error! */
778 mm_log ("Sequence syntax error",ERROR
);
783 return T
; /* successfully parsed sequence */
788 imap_seq_exec_append(MAILSTREAM
*stream
, long int msgno
, void *args
)
790 char *save_folder
, *flags
= NULL
, date
[64];
791 CONTEXT_S
*cntxt
= NULL
;
794 MAILSTREAM
*save_stream
;
795 SE_APP_S
*sa
= (SE_APP_S
*) args
;
799 save_folder
= (strucmp(sa
->folder
, ps_global
->inbox_name
) == 0)
800 ? ps_global
->VAR_INBOX_PATH
: sa
->folder
;
802 save_stream
= save_msg_stream(cntxt
, save_folder
, &our_stream
);
804 if((so
= so_get(CharStar
, NULL
, WRITE_ACCESS
)) != NULL
){
805 /* store flags before the fetch so UNSEEN bit isn't flipped */
806 mc
= (msgno
> 0L && stream
&& msgno
<= stream
->nmsgs
)
807 ? mail_elt(stream
, msgno
) : NULL
;
809 flags
= flag_string(stream
, msgno
, F_ANS
|F_FWD
|F_FLAG
|F_SEEN
|F_KEYWORD
);
815 rv
= save_fetch_append(stream
, msgno
, NULL
,
816 save_stream
, save_folder
, NULL
,
817 mc
? mc
->rfc822_size
: 0L, flags
, date
, so
);
819 fs_give((void **) &flags
);
821 if(rv
< 0 || sp_expunge_count(stream
)){
822 cmd_cancelled("Attached message Save");
825 /* else whatever broke in save_fetch_append shoulda bitched */
830 dprint((1, "Can't allocate store for save: %s\n",
831 error_description(errno
)));
832 q_status_message(SM_ORDER
| SM_DING
, 3, 4,
833 "Problem creating space for message text.");
837 mail_close(save_stream
);
844 /*----------------------------------------------------------------------
845 Get login and password from user for IMAP login
847 Args: mb -- The mail box property struct
848 user -- Buffer to return the user name in
849 passwd -- Buffer to return the passwd in
850 trial -- The trial number or number of attempts to login
851 user is at least size NETMAXUSER
852 passwd is apparently at least MAILTMPLEN, but mrc has asked us to
853 use a max size of about 100 instead
855 Result: username and password passed back to imap
858 mm_login(NETMBX
*mb
, char *user
, char **pwd
, long int trial
)
860 mm_login_work(mb
, user
, pwd
, trial
, NULL
, NULL
);
864 mm_login_method(NETMBX
*mb
, char *user
, void *login
, long int trial
, char *method
)
866 mm_login_method_work(mb
, user
, login
, trial
, method
, NULL
, NULL
);
870 /*----------------------------------------------------------------------
871 Exported method to retrieve logged in user name associated with stream
873 Args: host -- host to find associated login name with.
878 cached_user_name(char *host
)
883 if((l
= mm_login_list
) && host
)
885 for(h
= l
->hosts
; h
; h
= h
->next
)
886 if(!strucmp(host
, h
->name
))
888 while((l
= l
->next
) != NULL
);
895 imap_same_host(STRLIST_S
*hl1
, STRLIST_S
*hl2
)
897 return imap_same_host_auth(hl1
, hl2
, NULL
);
900 /* An explanation about this function. The way this is used in the
901 * new code for XOAUTH2, makes it so that when we are checking if
902 * the hosts and orighosts are the same, we are given the mb->host
903 * and mb->orighost pointers, and we cannot transform them in any
904 * way to be sure that increasing their size will not overflow the
905 * fixed width buffer that contain them, so we cannot change that.
906 * For purposes of this function, these values come in the hl2 variable.
907 * However, for purposes of this function, the values in hl1 are the ones
908 * that come straight from the cache, and here no transformation is made,
909 * that is, we save them as we read them, so when we compare the values
910 * read from the cache, with those that we want to save, we need to make
911 * sure we "shift" the hl1 variable, but not the hl2 variable.
914 imap_same_host_auth(STRLIST_S
*hl1
, STRLIST_S
*hl2
, char *authtype
)
919 len
= authtype
? strlen(authtype
) : 0;
920 offset
= authtype
? 1 : 0;
921 for( ; hl1
; hl1
= hl1
->next
)
922 for(lp
= hl2
; lp
; lp
= lp
->next
)
923 if((len
== 0 || (!struncmp(hl1
->name
, authtype
, len
)
924 && hl1
->name
[len
] == PWDAUTHSEP
))
925 && !strucmp(hl1
->name
+ len
+ offset
, lp
->name
))
933 * For convenience, we use the same m_list structure (but a different
934 * instance) for storing a list of hosts we've asked the user about when
935 * SSL validation fails. If this function returns TRUE, that means we
936 * have previously asked the user about this host. Ok_novalidate == 1 means
937 * the user said yes, it was ok. Ok_novalidate == 0 means the user
938 * said no. Warned means we warned them already.
941 imap_get_ssl(MMLOGIN_S
*m_list
, STRLIST_S
*hostlist
, int *ok_novalidate
, int *warned
)
945 for(l
= m_list
; l
; l
= l
->next
)
946 if(imap_same_host(l
->hosts
, hostlist
)){
948 *ok_novalidate
= l
->ok_novalidate
;
961 * Just trying to guess the username the user might want to use on this
962 * host, the user will confirm.
965 imap_get_user(MMLOGIN_S
*m_list
, STRLIST_S
*hostlist
)
969 for(l
= m_list
; l
; l
= l
->next
)
970 if(imap_same_host(l
->hosts
, hostlist
))
978 * If we have a matching hostname, username, and altflag in our cache,
979 * attempt to login with the password from the cache.
982 imap_get_passwd(MMLOGIN_S
*m_list
, char **passwd
, char *user
, STRLIST_S
*hostlist
, int altflag
)
984 return imap_get_passwd_auth(m_list
, passwd
, user
, hostlist
, altflag
, NULL
);
988 imap_get_passwd_auth(MMLOGIN_S
*m_list
, char **passwd
, char *user
, STRLIST_S
*hostlist
, int altflag
, char *authtype
)
994 "imap_get_passwd: checking user=%s alt=%d host=%s%s%s\n",
995 user
? user
: "(null)",
997 hostlist
->name
? hostlist
->name
: "",
998 (hostlist
->next
&& hostlist
->next
->name
) ? ", " : "",
999 (hostlist
->next
&& hostlist
->next
->name
) ? hostlist
->next
->name
1001 len
= authtype
? strlen(authtype
) : 0;
1002 offset
= authtype
? 1 : 0;
1003 for(l
= m_list
; l
; l
= l
->next
)
1004 if(imap_same_host_auth(l
->hosts
, hostlist
, authtype
)
1006 && (len
== 0 || (!struncmp(l
->user
, authtype
, len
)
1007 && l
->user
[len
] == PWDAUTHSEP
))
1008 && !strcmp(user
, l
->user
+ len
+ offset
)
1009 && l
->altflag
== altflag
){
1011 *passwd
= cpystr(l
->passwd
+ len
+ offset
);
1012 dprint((9, "imap_get_passwd: match\n"));
1013 dprint((10, "imap_get_passwd: trying passwd=\"%s\"\n",
1014 passwd
&& *passwd
? *passwd
: "?"));
1018 dprint((9, "imap_get_passwd: no match\n"));
1025 imap_set_passwd(MMLOGIN_S
**l
, char *passwd
, char *user
, STRLIST_S
*hostlist
,
1026 int altflag
, int ok_novalidate
, int warned
)
1028 imap_set_passwd_auth(l
, passwd
, user
, hostlist
, altflag
, ok_novalidate
,
1033 imap_set_passwd_auth(MMLOGIN_S
**l
, char *passwd
, char *user
, STRLIST_S
*hostlist
,
1034 int altflag
, int ok_novalidate
, int warned
, char *authtype
)
1038 size_t authlen
, offset
;
1040 authlen
= authtype
? strlen(authtype
) : 0;
1041 offset
= authtype
? 1 : 0;
1042 dprint((9, "imap_set_passwd\n"));
1043 for(; *l
; l
= &(*l
)->next
)
1044 if((authlen
== 0 || (!struncmp((*l
)->user
, authtype
, authlen
)
1045 && (*l
)->user
[authlen
] == PWDAUTHSEP
))
1046 && !strcmp(user
, (*l
)->user
+ authlen
+ offset
)
1047 && imap_same_host_auth((*l
)->hosts
, hostlist
, authtype
)
1048 && altflag
== (*l
)->altflag
){
1049 if(strcmp(passwd
, (*l
)->passwd
+ authlen
+ offset
) ||
1050 (*l
)->ok_novalidate
!= ok_novalidate
||
1051 (*l
)->warned
!= warned
)
1058 *l
= (MMLOGIN_S
*)fs_get(sizeof(MMLOGIN_S
));
1059 memset(*l
, 0, sizeof(MMLOGIN_S
));
1062 len
= strlen(passwd
);
1063 (*l
)->passwd
= ps_get(len
+ authlen
+ offset
+ 1);
1066 sprintf((*l
)->passwd
, "%s%c%s", authtype
, PWDAUTHSEP
, passwd
);
1068 strncpy((*l
)->passwd
, passwd
, len
+1);
1070 (*l
)->altflag
= altflag
; (*l
)->ok_novalidate
= ok_novalidate
; (*l
)->warned
= warned
;
1074 (*l
)->user
= fs_get(strlen(user
) + authlen
+ offset
+ 1);
1075 sprintf((*l
)->user
, "%s%c%s", authtype
, PWDAUTHSEP
, user
);
1078 (*l
)->user
= cpystr(user
);
1081 dprint((9, "imap_set_passwd: user=%s altflag=%d\n",
1082 (*l
)->user
? (*l
)->user
: "?",
1085 for( ; hostlist
; hostlist
= hostlist
->next
){
1086 for(listp
= &(*l
)->hosts
;
1087 *listp
&& strucmp((*listp
)->name
, hostlist
->name
);
1088 listp
= &(*listp
)->next
)
1092 *listp
= new_strlist_auth(hostlist
->name
, authtype
, PWDAUTHSEP
);
1093 dprint((9, "imap_set_passwd: host=%s\n",
1094 (*listp
)->name
? (*listp
)->name
: "?"));
1098 dprint((10, "imap_set_passwd: passwd=\"%s\"\n",
1099 passwd
? passwd
: "?"));
1105 imap_flush_passwd_cache(int dumpcache
)
1110 /* equivalent of memset but can't be optimized away */
1111 len
= sizeof(private_store
);
1119 while((l
= mm_login_list
) != NULL
){
1120 mm_login_list
= mm_login_list
->next
;
1122 fs_give((void **) &l
->user
);
1124 free_strlist(&l
->hosts
);
1126 fs_give((void **) &l
);
1129 while((l
= cert_failure_list
) != NULL
){
1130 cert_failure_list
= cert_failure_list
->next
;
1132 fs_give((void **) &l
->user
);
1134 free_strlist(&l
->hosts
);
1136 fs_give((void **) &l
);
1143 * Mimics fs_get except it only works for char * (no alignment hacks), it
1144 * stores in a static array so it is easy to zero it out (that's the whole
1145 * purpose), allocations always happen at the end (no free).
1146 * If we go past array limit, we don't break, we just use free storage.
1147 * Should be awfully rare, though.
1152 static char *last
= (char *) private_store
;
1155 /* there is enough space */
1156 if(size
<= sizeof(private_store
) - (last
- (char *) private_store
)){
1162 "Out of password caching space in private_store\n"));
1164 "Using free storage instead\n"));
1165 block
= fs_get(size
);
1173 passfile_name(char *pinerc
, char *path
, size_t len
)
1179 if(!path
|| !((pinerc
&& pinerc
[0]) || ps_global
->passfile
))
1182 if(ps_global
->passfile
)
1183 strncpy(path
, ps_global
->passfile
, len
-1);
1185 if((p
= last_cmpnt(pinerc
)) && *(p
-1) && *(p
-1) != PASSFILE
[0])
1186 for(i
= 0; pinerc
< p
&& i
< len
; pinerc
++, i
++)
1191 for(j
= 0; (i
< len
) && (path
[i
] = PASSFILE
[j
]); i
++, j
++)
1198 dprint((9, "Looking for passfile \"%s\"\n",
1199 path
? path
: "?"));
1201 #if defined(DOS) || defined(OS2)
1202 return((our_stat(path
, &sbuf
) == 0
1203 && ((sbuf
.st_mode
& S_IFMT
) == S_IFREG
))
1206 /* First, make sure it's ours and not sym-linked */
1207 if(our_lstat(path
, &sbuf
) == 0
1208 && ((sbuf
.st_mode
& S_IFMT
) == S_IFREG
)
1209 && sbuf
.st_uid
== getuid()){
1210 /* if too liberal permissions, fix them */
1211 if((sbuf
.st_mode
& 077) != 0)
1212 if(our_chmod(path
, sbuf
.st_mode
& ~077) != 0)
1221 #endif /* PASSFILE */