ssh.h 5.9 KB

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