ssh.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. /*
  2. * This file is part of the UCB release of Plan 9. It is subject to the license
  3. * terms in the LICENSE file found in the top-level directory of this
  4. * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
  5. * part of the UCB release of Plan 9, including this file, may be copied,
  6. * modified, propagated, or distributed except according to the terms contained
  7. * in the LICENSE file.
  8. */
  9. #include <u.h>
  10. #include <libc.h>
  11. #include <mp.h>
  12. #include <auth.h>
  13. #include <libsec.h>
  14. enum /* internal debugging flags */
  15. {
  16. DBG= 1<<0,
  17. DBG_CRYPTO= 1<<1,
  18. DBG_PACKET= 1<<2,
  19. DBG_AUTH= 1<<3,
  20. DBG_PROC= 1<<4,
  21. DBG_PROTO= 1<<5,
  22. DBG_IO= 1<<6,
  23. DBG_SCP= 1<<7,
  24. };
  25. enum /* protocol packet types */
  26. {
  27. /* 0 */
  28. SSH_MSG_NONE=0,
  29. SSH_MSG_DISCONNECT,
  30. SSH_SMSG_PUBLIC_KEY,
  31. SSH_CMSG_SESSION_KEY,
  32. SSH_CMSG_USER,
  33. SSH_CMSG_AUTH_RHOSTS,
  34. SSH_CMSG_AUTH_RSA,
  35. SSH_SMSG_AUTH_RSA_CHALLENGE,
  36. SSH_CMSG_AUTH_RSA_RESPONSE,
  37. SSH_CMSG_AUTH_PASSWORD,
  38. /* 10 */
  39. SSH_CMSG_REQUEST_PTY,
  40. SSH_CMSG_WINDOW_SIZE,
  41. SSH_CMSG_EXEC_SHELL,
  42. SSH_CMSG_EXEC_CMD,
  43. SSH_SMSG_SUCCESS,
  44. SSH_SMSG_FAILURE,
  45. SSH_CMSG_STDIN_DATA,
  46. SSH_SMSG_STDOUT_DATA,
  47. SSH_SMSG_STDERR_DATA,
  48. SSH_CMSG_EOF,
  49. /* 20 */
  50. SSH_SMSG_EXITSTATUS,
  51. SSH_MSG_CHANNEL_OPEN_CONFIRMATION,
  52. SSH_MSG_CHANNEL_OPEN_FAILURE,
  53. SSH_MSG_CHANNEL_DATA,
  54. SSH_MSG_CHANNEL_INPUT_EOF,
  55. SSH_MSG_CHANNEL_OUTPUT_CLOSED,
  56. SSH_MSG_UNIX_DOMAIN_X11_FORWARDING, /* obsolete */
  57. SSH_SMSG_X11_OPEN,
  58. SSH_CMSG_PORT_FORWARD_REQUEST,
  59. SSH_MSG_PORT_OPEN,
  60. /* 30 */
  61. SSH_CMSG_AGENT_REQUEST_FORWARDING,
  62. SSH_SMSG_AGENT_OPEN,
  63. SSH_MSG_IGNORE,
  64. SSH_CMSG_EXIT_CONFIRMATION,
  65. SSH_CMSG_X11_REQUEST_FORWARDING,
  66. SSH_CMSG_AUTH_RHOSTS_RSA,
  67. SSH_MSG_DEBUG,
  68. SSH_CMSG_REQUEST_COMPRESSION,
  69. SSH_CMSG_MAX_PACKET_SIZE,
  70. SSH_CMSG_AUTH_TIS,
  71. /* 40 */
  72. SSH_SMSG_AUTH_TIS_CHALLENGE,
  73. SSH_CMSG_AUTH_TIS_RESPONSE,
  74. SSH_CMSG_AUTH_KERBEROS,
  75. SSH_SMSG_AUTH_KERBEROS_RESPONSE,
  76. SSH_CMSG_HAVE_KERBEROS_TGT,
  77. };
  78. enum /* protocol flags */
  79. {
  80. SSH_PROTOFLAG_SCREEN_NUMBER=1<<0,
  81. SSH_PROTOFLAG_HOST_IN_FWD_OPEN=1<<1,
  82. };
  83. enum /* agent protocol packet types */
  84. {
  85. SSH_AGENTC_NONE = 0,
  86. SSH_AGENTC_REQUEST_RSA_IDENTITIES,
  87. SSH_AGENT_RSA_IDENTITIES_ANSWER,
  88. SSH_AGENTC_RSA_CHALLENGE,
  89. SSH_AGENT_RSA_RESPONSE,
  90. SSH_AGENT_FAILURE,
  91. SSH_AGENT_SUCCESS,
  92. SSH_AGENTC_ADD_RSA_IDENTITY,
  93. SSH_AGENTC_REMOVE_RSA_IDENTITY,
  94. };
  95. enum /* protocol constants */
  96. {
  97. SSH_MAX_DATA = 256*1024,
  98. SSH_MAX_MSG = SSH_MAX_DATA+4,
  99. SESSKEYLEN = 32,
  100. SESSIDLEN = 16,
  101. COOKIELEN = 8,
  102. };
  103. enum /* crypto ids */
  104. {
  105. SSH_CIPHER_NONE = 0,
  106. SSH_CIPHER_IDEA,
  107. SSH_CIPHER_DES,
  108. SSH_CIPHER_3DES,
  109. SSH_CIPHER_TSS,
  110. SSH_CIPHER_RC4,
  111. SSH_CIPHER_BLOWFISH,
  112. SSH_CIPHER_TWIDDLE, /* for debugging */
  113. };
  114. enum /* auth method ids */
  115. {
  116. SSH_AUTH_RHOSTS = 1,
  117. SSH_AUTH_RSA = 2,
  118. SSH_AUTH_PASSWORD = 3,
  119. SSH_AUTH_RHOSTS_RSA = 4,
  120. SSH_AUTH_TIS = 5,
  121. SSH_AUTH_USER_RSA = 6,
  122. };
  123. typedef struct Auth Auth;
  124. typedef struct Authsrv Authsrv;
  125. typedef struct Cipher Cipher;
  126. typedef struct CipherState CipherState;
  127. typedef struct Conn Conn;
  128. typedef struct Msg Msg;
  129. #pragma incomplete CipherState
  130. struct Auth
  131. {
  132. int id;
  133. char *name;
  134. int (*fn)(Conn*);
  135. };
  136. struct Authsrv
  137. {
  138. int id;
  139. char *name;
  140. int firstmsg;
  141. AuthInfo *(*fn)(Conn*, Msg*);
  142. };
  143. struct Cipher
  144. {
  145. int id;
  146. char *name;
  147. CipherState *(*init)(Conn*, int isserver);
  148. void (*encrypt)(CipherState*, uchar*, int);
  149. void (*decrypt)(CipherState*, uchar*, int);
  150. };
  151. struct Conn
  152. {
  153. QLock;
  154. int fd[2];
  155. CipherState *cstate;
  156. uchar cookie[COOKIELEN];
  157. uchar sessid[SESSIDLEN];
  158. uchar sesskey[SESSKEYLEN];
  159. RSApub *serverkey;
  160. RSApub *hostkey;
  161. ulong flags;
  162. ulong ciphermask;
  163. Cipher *cipher; /* chosen cipher */
  164. Cipher **okcipher; /* list of acceptable ciphers */
  165. int nokcipher;
  166. ulong authmask;
  167. Auth **okauth;
  168. int nokauth;
  169. char *user;
  170. char *host;
  171. char *aliases;
  172. int interactive;
  173. Msg *unget;
  174. RSApriv *serverpriv; /* server only */
  175. RSApriv *hostpriv;
  176. Authsrv **okauthsrv;
  177. int nokauthsrv;
  178. };
  179. struct Msg
  180. {
  181. Conn *c;
  182. uchar type;
  183. ulong len; /* output: #bytes before pos, input: #bytes after pos */
  184. uchar *bp; /* beginning of allocated space */
  185. uchar *rp; /* read pointer */
  186. uchar *wp; /* write pointer */
  187. uchar *ep; /* end of allocated space */
  188. Msg *link; /* for sshnet */
  189. };
  190. #define LONG(p) (((p)[0]<<24)|((p)[1]<<16)|((p)[2]<<8)|((p)[3]))
  191. #define PLONG(p, l) \
  192. (((p)[0]=(l)>>24),((p)[1]=(l)>>16),\
  193. ((p)[2]=(l)>>8),((p)[3]=(l)))
  194. #define SHORT(p) (((p)[0]<<8)|(p)[1])
  195. #define PSHORT(p,l) \
  196. (((p)[0]=(l)>>8),((p)[1]=(l)))
  197. extern char Edecode[];
  198. extern char Eencode[];
  199. extern char Ememory[];
  200. extern char Ehangup[];
  201. extern int doabort;
  202. extern int debuglevel;
  203. extern Auth authpassword;
  204. extern Auth authrsa;
  205. extern Auth authtis;
  206. extern Authsrv authsrvpassword;
  207. extern Authsrv authsrvtis;
  208. extern Cipher cipher3des;
  209. extern Cipher cipherblowfish;
  210. extern Cipher cipherdes;
  211. extern Cipher cipherrc4;
  212. extern Cipher ciphernone;
  213. extern Cipher ciphertwiddle;
  214. /* msg.c */
  215. Msg* allocmsg(Conn*, int, int);
  216. void badmsg(Msg*, int);
  217. Msg* recvmsg(Conn*, int);
  218. void unrecvmsg(Conn*, Msg*);
  219. int sendmsg(Msg*);
  220. uchar getbyte(Msg*);
  221. ushort getshort(Msg*);
  222. ulong getlong(Msg*);
  223. char* getstring(Msg*);
  224. void* getbytes(Msg*, int);
  225. mpint* getmpint(Msg*);
  226. RSApub* getRSApub(Msg*);
  227. void putbyte(Msg*, uchar);
  228. void putshort(Msg*, ushort);
  229. void putlong(Msg*, ulong);
  230. void putstring(Msg*, char*);
  231. void putbytes(Msg*, void*, long);
  232. void putmpint(Msg*, mpint*);
  233. void putRSApub(Msg*, RSApub*);
  234. mpint* rsapad(mpint*, int);
  235. mpint* rsaunpad(mpint*);
  236. void mptoberjust(mpint*, uchar*, int);
  237. mpint* rsaencryptbuf(RSApub*, uchar*, int);
  238. /* cmsg.c */
  239. void sshclienthandshake(Conn*);
  240. void requestpty(Conn*);
  241. int readgeom(int*, int*, int*, int*);
  242. void sendwindowsize(Conn*, int, int, int, int);
  243. int rawhack;
  244. /* smsg.c */
  245. void sshserverhandshake(Conn*);
  246. /* pubkey.c */
  247. enum
  248. {
  249. KeyOk,
  250. KeyWrong,
  251. NoKey,
  252. NoKeyFile,
  253. };
  254. int appendkey(char*, char*, RSApub*);
  255. int findkey(char*, char*, RSApub*);
  256. int replacekey(char*, char*, RSApub*);
  257. /* agent.c */
  258. int startagent(Conn*);
  259. void handleagentmsg(Msg*);
  260. void handleagentopen(Msg*);
  261. void handleagentieof(Msg*);
  262. void handleagentoclose(Msg*);
  263. /* util.c */
  264. void debug(int, char*, ...);
  265. void* emalloc(long);
  266. void* erealloc(void*, long);
  267. void error(char*, ...);
  268. RSApriv* readsecretkey(char*);
  269. int readstrnl(int, char*, int);
  270. void atexitkill(int);
  271. void atexitkiller(void);
  272. void calcsessid(Conn*);
  273. void sshlog(char*, ...);
  274. void setaliases(Conn*, char*);
  275. void privatefactotum(void);
  276. #pragma varargck argpos debug 2
  277. #pragma varargck argpos error 1
  278. #pragma varargck argpos sshlog 2