mirror of
https://git.tartarus.org/simon/putty.git
synced 2025-01-09 17:38:00 +00:00
a2ff884512
All the seat functions that request an interactive prompt of some kind to the user - both the main seat_get_userpass_input and the various confirmation dialogs for things like host keys - were using a simple int return value, with the general semantics of 0 = "fail", 1 = "proceed" (and in the case of seat_get_userpass_input, answers to the prompts were provided), and -1 = "request in progress, wait for a callback". In this commit I change all those functions' return types to a new struct called SeatPromptResult, whose primary field is an enum replacing those simple integer values. The main purpose is that the enum has not three but _four_ values: the "fail" result has been split into 'user abort' and 'software abort'. The distinction is that a user abort occurs as a result of an interactive UI action, such as the user clicking 'cancel' in a dialog box or hitting ^D or ^C at a terminal password prompt - and therefore, there's no need to display an error message telling the user that the interactive operation has failed, because the user already knows, because they _did_ it. 'Software abort' is from any other cause, where PuTTY is the first to know there was a problem, and has to tell the user. We already had this 'user abort' vs 'software abort' distinction in other parts of the code - the SSH backend has separate termination functions which protocol layers can call. But we assumed that any failure from an interactive prompt request fell into the 'user abort' category, which is not true. A couple of examples: if you configure a host key fingerprint in your saved session via the SSH > Host keys pane, and the server presents a host key that doesn't match it, then verify_ssh_host_key would report that the user had aborted the connection, and feel no need to tell the user what had gone wrong! Similarly, if a password provided on the command line was not accepted, then (after I fixed the semantics of that in the previous commit) the same wrong handling would occur. So now, those Seat prompt functions too can communicate whether the user or the software originated a connection abort. And in the latter case, we also provide an error message to present to the user. Result: in those two example cases (and others), error messages should no longer go missing. Implementation note: to avoid the hassle of having the error message in a SeatPromptResult being a dynamically allocated string (and hence, every recipient of one must always check whether it's non-NULL and free it on every exit path, plus being careful about copying the struct around), I've instead arranged that the structure contains a function pointer and a couple of parameters, so that the string form of the message can be constructed on demand. That way, the only users who need to free it are the ones who actually _asked_ for it in the first place, which is a much smaller set. (This is one of the rare occasions that I regret not having C++'s extra features available in this code base - a unique_ptr or shared_ptr to a string would have been just the thing here, and the compiler would have done all the hard work for me of remembering where to insert the frees!)
241 lines
7.7 KiB
C
241 lines
7.7 KiB
C
/*
|
|
* defs.h: initial definitions for PuTTY.
|
|
*
|
|
* The rule about this header file is that it can't depend on any
|
|
* other header file in this code base. This is where we define
|
|
* things, as much as we can, that other headers will want to refer
|
|
* to, such as opaque structure types and their associated typedefs,
|
|
* or macros that are used by other headers.
|
|
*/
|
|
|
|
#ifndef PUTTY_DEFS_H
|
|
#define PUTTY_DEFS_H
|
|
|
|
#ifdef NDEBUG
|
|
/*
|
|
* PuTTY is a security project, so assertions are important - if an
|
|
* assumption is violated, proceeding anyway may have far worse
|
|
* consequences than simple program termination. This check and #error
|
|
* should arrange that we don't ever accidentally compile assertions
|
|
* out.
|
|
*/
|
|
#error Do not compile this code base with NDEBUG defined!
|
|
#endif
|
|
|
|
#if HAVE_CMAKE_H
|
|
#include "cmake.h"
|
|
#endif
|
|
|
|
#include <stddef.h>
|
|
#include <stdint.h>
|
|
#include <stdio.h> /* for __MINGW_PRINTF_FORMAT */
|
|
#include <stdbool.h>
|
|
|
|
#if defined _MSC_VER && _MSC_VER < 1800
|
|
/* Work around lack of inttypes.h and strtoumax in older MSVC */
|
|
#define PRIx32 "x"
|
|
#define PRIu32 "u"
|
|
#define PRIu64 "I64u"
|
|
#define PRIdMAX "I64d"
|
|
#define PRIXMAX "I64X"
|
|
#define SCNu64 "I64u"
|
|
#define SIZEx "Ix"
|
|
#define SIZEu "Iu"
|
|
uintmax_t strtoumax(const char *nptr, char **endptr, int base);
|
|
#else
|
|
#include <inttypes.h>
|
|
/* Because we still support older MSVC libraries which don't recognise the
|
|
* standard C "z" modifier for size_t-sized integers, we must use an
|
|
* inttypes.h-style macro for those */
|
|
#define SIZEx "zx"
|
|
#define SIZEu "zu"
|
|
#endif
|
|
|
|
#if defined __GNUC__ || defined __clang__
|
|
/*
|
|
* On MinGW, the correct compiler format checking for vsnprintf() etc
|
|
* can depend on compile-time flags; these control whether you get
|
|
* ISO C or Microsoft's non-standard format strings.
|
|
* We sometimes use __attribute__ ((format)) for our own printf-like
|
|
* functions, which are ultimately interpreted by the toolchain-chosen
|
|
* printf, so we need to take that into account to get correct warnings.
|
|
*/
|
|
#ifdef __MINGW_PRINTF_FORMAT
|
|
#define PRINTF_LIKE(fmt_index, ellipsis_index) \
|
|
__attribute__ ((format (__MINGW_PRINTF_FORMAT, fmt_index, ellipsis_index)))
|
|
#else
|
|
#define PRINTF_LIKE(fmt_index, ellipsis_index) \
|
|
__attribute__ ((format (printf, fmt_index, ellipsis_index)))
|
|
#endif
|
|
#else /* __GNUC__ */
|
|
#define PRINTF_LIKE(fmt_index, ellipsis_index)
|
|
#endif /* __GNUC__ */
|
|
|
|
typedef struct conf_tag Conf;
|
|
typedef struct terminal_tag Terminal;
|
|
typedef struct term_utf8_decode term_utf8_decode;
|
|
|
|
typedef struct Filename Filename;
|
|
typedef struct FontSpec FontSpec;
|
|
|
|
typedef struct bufchain_tag bufchain;
|
|
|
|
typedef struct strbuf strbuf;
|
|
typedef struct LoadedFile LoadedFile;
|
|
|
|
typedef struct RSAKey RSAKey;
|
|
|
|
typedef struct BinarySink BinarySink;
|
|
typedef struct BinarySource BinarySource;
|
|
typedef struct stdio_sink stdio_sink;
|
|
typedef struct bufchain_sink bufchain_sink;
|
|
typedef struct handle_sink handle_sink;
|
|
|
|
typedef struct IdempotentCallback IdempotentCallback;
|
|
|
|
typedef struct SockAddr SockAddr;
|
|
|
|
typedef struct Socket Socket;
|
|
typedef struct Plug Plug;
|
|
typedef struct SocketPeerInfo SocketPeerInfo;
|
|
typedef struct DeferredSocketOpener DeferredSocketOpener;
|
|
typedef struct DeferredSocketOpenerVtable DeferredSocketOpenerVtable;
|
|
|
|
typedef struct Backend Backend;
|
|
typedef struct BackendVtable BackendVtable;
|
|
typedef struct Interactor Interactor;
|
|
typedef struct InteractorVtable InteractorVtable;
|
|
typedef struct InteractionReadySeat InteractionReadySeat;
|
|
|
|
typedef struct Ldisc_tag Ldisc;
|
|
typedef struct LogContext LogContext;
|
|
typedef struct LogPolicy LogPolicy;
|
|
typedef struct LogPolicyVtable LogPolicyVtable;
|
|
|
|
typedef struct Seat Seat;
|
|
typedef struct SeatVtable SeatVtable;
|
|
typedef struct SeatPromptResult SeatPromptResult;
|
|
|
|
typedef struct TermWin TermWin;
|
|
typedef struct TermWinVtable TermWinVtable;
|
|
|
|
typedef struct Ssh Ssh;
|
|
|
|
typedef struct mp_int mp_int;
|
|
typedef struct MontyContext MontyContext;
|
|
|
|
typedef struct WeierstrassCurve WeierstrassCurve;
|
|
typedef struct WeierstrassPoint WeierstrassPoint;
|
|
typedef struct MontgomeryCurve MontgomeryCurve;
|
|
typedef struct MontgomeryPoint MontgomeryPoint;
|
|
typedef struct EdwardsCurve EdwardsCurve;
|
|
typedef struct EdwardsPoint EdwardsPoint;
|
|
|
|
typedef struct SshServerConfig SshServerConfig;
|
|
typedef struct SftpServer SftpServer;
|
|
typedef struct SftpServerVtable SftpServerVtable;
|
|
|
|
typedef struct Channel Channel;
|
|
typedef struct SshChannel SshChannel;
|
|
typedef struct mainchan mainchan;
|
|
|
|
typedef struct ssh_sharing_state ssh_sharing_state;
|
|
typedef struct ssh_sharing_connstate ssh_sharing_connstate;
|
|
typedef struct share_channel share_channel;
|
|
|
|
typedef struct PortFwdManager PortFwdManager;
|
|
typedef struct PortFwdRecord PortFwdRecord;
|
|
typedef struct ConnectionLayer ConnectionLayer;
|
|
|
|
typedef struct prng prng;
|
|
typedef struct ssh_hashalg ssh_hashalg;
|
|
typedef struct ssh_hash ssh_hash;
|
|
typedef struct ssh_kex ssh_kex;
|
|
typedef struct ssh_kexes ssh_kexes;
|
|
typedef struct ssh_keyalg ssh_keyalg;
|
|
typedef struct ssh_key ssh_key;
|
|
typedef struct ssh_compressor ssh_compressor;
|
|
typedef struct ssh_decompressor ssh_decompressor;
|
|
typedef struct ssh_compression_alg ssh_compression_alg;
|
|
typedef struct ssh2_userkey ssh2_userkey;
|
|
typedef struct ssh2_macalg ssh2_macalg;
|
|
typedef struct ssh2_mac ssh2_mac;
|
|
typedef struct ssh_cipheralg ssh_cipheralg;
|
|
typedef struct ssh_cipher ssh_cipher;
|
|
typedef struct ssh2_ciphers ssh2_ciphers;
|
|
typedef struct dh_ctx dh_ctx;
|
|
typedef struct ecdh_key ecdh_key;
|
|
|
|
typedef struct dlgparam dlgparam;
|
|
|
|
typedef struct settings_w settings_w;
|
|
typedef struct settings_r settings_r;
|
|
typedef struct settings_e settings_e;
|
|
|
|
typedef struct SessionSpecial SessionSpecial;
|
|
|
|
typedef struct StripCtrlChars StripCtrlChars;
|
|
|
|
typedef struct BidiContext BidiContext;
|
|
|
|
/*
|
|
* A small structure wrapping up a (pointer, length) pair so that it
|
|
* can be conveniently passed to or from a function.
|
|
*/
|
|
typedef struct ptrlen {
|
|
const void *ptr;
|
|
size_t len;
|
|
} ptrlen;
|
|
|
|
typedef struct logblank_t logblank_t;
|
|
|
|
typedef struct BinaryPacketProtocol BinaryPacketProtocol;
|
|
typedef struct PacketProtocolLayer PacketProtocolLayer;
|
|
|
|
/* Do a compile-time type-check of 'to_check' (without evaluating it),
|
|
* as a side effect of returning the value 'to_return'. Note that
|
|
* although this macro double-*expands* to_return, it always
|
|
* *evaluates* exactly one copy of it, so it's side-effect safe. */
|
|
#define TYPECHECK(to_check, to_return) \
|
|
(sizeof(to_check) ? (to_return) : (to_return))
|
|
|
|
/* Return a pointer to the object of structure type 'type' whose field
|
|
* with name 'field' is pointed at by 'object'. */
|
|
#define container_of(object, type, field) \
|
|
TYPECHECK(object == &((type *)0)->field, \
|
|
((type *)(((char *)(object)) - offsetof(type, field))))
|
|
|
|
#if defined __GNUC__ || defined __clang__
|
|
#define NORETURN __attribute__((__noreturn__))
|
|
#elif defined _MSC_VER
|
|
#define NORETURN __declspec(noreturn)
|
|
#else
|
|
#define NORETURN
|
|
#endif
|
|
|
|
/*
|
|
* Standard macro definitions. STR() behaves like the preprocessor
|
|
* stringification # operator, and CAT() behaves like the token paste
|
|
* ## operator, except that each one macro-expands its argument(s)
|
|
* first, unlike the raw version. E.g.
|
|
*
|
|
* #__LINE__ -> "__LINE__"
|
|
* STR(__LINE__) -> "1234" (or whatever)
|
|
*
|
|
* and similarly,
|
|
*
|
|
* foo ## __LINE__ -> foo__LINE__
|
|
* CAT(foo, __LINE__) -> foo1234 (or whatever)
|
|
*
|
|
* The expansion is achieved by having each macro pass its arguments
|
|
* to a secondary inner macro, because parameter lists of a macro call
|
|
* get expanded before the called macro is invoked. So STR(__LINE__)
|
|
* -> STR_INNER(1234) -> #1234 -> "1234", and similarly for CAT.
|
|
*/
|
|
#define STR_INNER(x) #x
|
|
#define STR(x) STR_INNER(x)
|
|
#define CAT_INNER(x,y) x ## y
|
|
#define CAT(x,y) CAT_INNER(x,y)
|
|
|
|
#endif /* PUTTY_DEFS_H */
|