File: ssh.c

package info (click to toggle)
hashrat 1.8.3+dfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 1,700 kB
  • ctags: 1,677
  • sloc: ansic: 17,713; sh: 323; makefile: 157
file content (332 lines) | stat: -rw-r--r-- 7,474 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
#include "ssh.h"
#include <sys/types.h>
#include <sys/wait.h>
#include <ctype.h>

#define SSH_LOGON_DONE 1
#define SSH_ASK_PASSWD 2
#define SSH_REMOTE_HAS_HASHRAT 4
#define SSH_REMOTE_HAS_SHA1SUM 8
#define SSH_REMOTE_HAS_MD5SUM  16

char *SSHGenerateReplayTerminator(char *RetStr, const char *Command)
{
	return(FormatStr(RetStr,"%s-%lu-%lu-%lu",Command,getpid(),time(NULL),rand()));
}

void SSHRequestPasswd(char **Passwd)
{
STREAM *S;
char inchar;

S=STREAMFromFD(0);

//Turn off echo (and other things)
InitTTY(0,0,0);
fprintf(stderr,"Password: "); fflush(NULL);
inchar=STREAMReadChar(S);
while ((inchar != EOF) && (inchar != '\n') && (inchar != '\r'))
{
	*Passwd=AddCharToStr(*Passwd,inchar);
	write(2,"*",1);
	inchar=STREAMReadChar(S);
}
StripCRLF(*Passwd);
//turn echo back on
ResetTTY(0);

printf("\n");
STREAMDisassociateFromFD(S);
}



int SSHFinalizeConnection(HashratCtx *Ctx, int Flags, char **Passwd)
{
char *Tempstr=NULL, *Line=NULL;
ListNode *Dialog=NULL;
int result=TRUE;


	STREAMSetFlushType(Ctx->NetCon,FLUSH_ALWAYS,0,0);
	if (Flags & SSH_ASK_PASSWD) 
	{
		*Passwd=CopyStr(*Passwd, GetVar(Ctx->Vars,"SshPasswd"));
		if (! StrValid(*Passwd))
		{
		SSHRequestPasswd(Passwd);
		SetVar(Ctx->Vars,"SshPasswd", *Passwd);
		}
	}

	Tempstr=SSHGenerateReplayTerminator(Tempstr, "CONNECTED1");
	STREAMWriteLine("echo ",Ctx->NetCon);
	STREAMWriteLine(Tempstr,Ctx->NetCon);
	STREAMWriteLine("\n",Ctx->NetCon);

  Dialog=ListCreate();
  ExpectDialogAdd(Dialog, "Are you sure you want to continue connecting (yes/no)?", "yes\n", DIALOG_OPTIONAL);
  ExpectDialogAdd(Dialog, "Permission denied", "", DIALOG_OPTIONAL | DIALOG_FAIL);
  ExpectDialogAdd(Dialog, Tempstr, "", DIALOG_END);
	if (StrValid(*Passwd))
	{
  Tempstr=MCopyStr(Tempstr,*Passwd,"\n",NULL);
  ExpectDialogAdd(Dialog, "assword:", Tempstr, DIALOG_END);
	}
  else ExpectDialogAdd(Dialog, "assword:", "", DIALOG_OPTIONAL | DIALOG_FAIL);

  STREAMExpectDialog(Ctx->NetCon, Dialog);
  ListDestroy(Dialog,ExpectDialogDestroy);


	STREAMSetFlushType(Ctx->NetCon,FLUSH_LINE,0,0);

	//allow time for stty to take effect!
	STREAMWriteLine("PS1=\n",Ctx->NetCon); 
	STREAMWriteLine("PS2=\n",Ctx->NetCon);
	STREAMWriteLine("stty -echo\necho\n",Ctx->NetCon);
	STREAMFlush(Ctx->NetCon);


	//Do this again so we know we're beyond any crap produced by our setup of the terminal	
	Tempstr=SSHGenerateReplayTerminator(Tempstr, "CONNECTED2");
	STREAMWriteLine("echo ",Ctx->NetCon);
	STREAMWriteLine(Tempstr,Ctx->NetCon);
	STREAMWriteLine("\n",Ctx->NetCon);


	Line=STREAMReadLine(Line,Ctx->NetCon);
	while (Line)
	{
	StripTrailingWhitespace(Line);
	if (strcmp(Line,Tempstr)==0) break;
	Line=STREAMReadLine(Line,Ctx->NetCon);
	}


DestroyString(Tempstr);
DestroyString(Line);
return(result);
}




STREAM *SSHConnect(const char *URL, char **Path, HashratCtx *Ctx)
{
char *Tempstr=NULL, *Host=NULL, *PortStr=NULL, *User=NULL, *Passwd=NULL, *ptr;
int Port=22, Flags=0;

ParseURL(URL, NULL, &Host, &PortStr, &User, &Passwd, &Tempstr, NULL);
*Path=MCopyStr(*Path,"/",Tempstr,NULL);
if (! Ctx->NetCon) 
{
Ctx->NetCon=STREAMCreate();

if (StrValid(PortStr)) Port=atoi(PortStr);

Tempstr=FormatStr(Tempstr,"/usr/bin/ssh -2 -T %s@%s -p %d",User,Host,Port);

ptr=GetVar(Ctx->Vars,"SshIdFile");
if (StrValid(ptr)) Tempstr=MCatStr(Tempstr," -i ",ptr, NULL);

//Never use TTYFLAG_CANON here
Ctx->NetCon=STREAMSpawnCommand(Tempstr,"","",COMMS_BY_PTY|TTYFLAG_CRLF|TTYFLAG_IGNSIG);

if ((! StrValid(ptr)) && (! StrValid(Passwd)))  Flags |= SSH_ASK_PASSWD;
SSHFinalizeConnection(Ctx, Flags, &Passwd);

STREAMSetTimeout(Ctx->NetCon,10000);

/*
StripTrailingWhitespace(Tempstr);
StripTrailingWhitespace(User);

if ( (StrValid(User)==0) || (strcmp(Tempstr,User) !=0) )
{
	STREAMClose(S);
	S=NULL;
}
*/

}

DestroyString(User);
DestroyString(Passwd);
DestroyString(Host);
DestroyString(Tempstr);
DestroyString(PortStr);

return(Ctx->NetCon);
}





STREAM *SSHGet(HashratCtx *Ctx, char *URL)
{
char *Tempstr=NULL, *Path=NULL, *ptr;
STREAM *S;
struct timeval tv;

//collect any previous ssh's
while (waitpid(-1,NULL,WNOHANG) > 0);

//clean up bytes in the stream

S=SSHConnect(URL, &Path, Ctx);
if (S)
{
	ptr=Path;
	if (*ptr=='/') ptr++;
	Tempstr=MCopyStr(Tempstr,"cat '",ptr,"' 2>/dev/null \n",NULL);
	STREAMWriteLine(Tempstr,S); 
	STREAMFlush(S);
	tv.tv_usec=0;
	tv.tv_sec=2;
	FDSelect(S->in_fd, SELECT_READ, &tv);
}

DestroyString(Path);
DestroyString(Tempstr);

return(S);
}


void Decode_LS_Output(char *Line, char **Path, struct stat *Stat)
{
char *Token=NULL, *ptr, *tptr;

//219884 -rw-r--r-- 1 root        root     56980 Feb 13 18:06 filestore.o
		memset(Stat,0,sizeof(struct stat));

		ptr=Line;
		while (isspace(*ptr)) ptr++;
    ptr=GetToken(ptr,"\\S",&Token,0);
		Stat->st_ino=atol(Token);

		ptr=GetToken(ptr,"\\S",&Token,0);

		if (StrValid(Token) > 9)
		{
		tptr=Token;
    switch (*tptr)
    {
      case 'd': Stat->st_mode |=S_IFDIR; break;
      case 'l': Stat->st_mode |=S_IFLNK; break;
      case 'c': Stat->st_mode |=S_IFCHR; break;
      case 'b': Stat->st_mode |=S_IFBLK; break;
      case 's': Stat->st_mode |=S_IFSOCK; break;
      default: Stat->st_mode |=S_IFREG; break;
    }

		tptr++;
		if (*tptr=='r') Stat->st_mode |= S_IRUSR;
		tptr++;
		if (*tptr=='w') Stat->st_mode |= S_IWUSR;
		tptr++;
		if (*tptr=='x') Stat->st_mode |= S_IXUSR;

		tptr++;
		if (*tptr=='r') Stat->st_mode |= S_IRGRP;
		tptr++;
		if (*tptr=='w') Stat->st_mode |= S_IWGRP;
		tptr++;
		if (*tptr=='x') Stat->st_mode |= S_IXGRP;

		tptr++;
		if (*tptr=='r') Stat->st_mode |= S_IROTH;
		tptr++;
		if (*tptr=='w') Stat->st_mode |= S_IWOTH;
		tptr++;
		if (*tptr=='x') Stat->st_mode |= S_IXOTH;
		}

    ptr=GetToken(ptr,"\\S",&Token,0);

		//user id
    ptr=GetToken(ptr,"\\S",&Token,0);
		Stat->st_uid=atol(Token);

		//group id
    ptr=GetToken(ptr,"\\S",&Token,0);
		Stat->st_gid=atol(Token);

		//Size
    ptr=GetToken(ptr,"\\S",&Token,0);
		Stat->st_size=atol(Token);

		/*
    StripTrailingWhitespace(Token);
    FI->Size=atoi(Token);
		*/

		//Date Str
    ptr=GetToken(ptr,"\\S",&Token,0);
    ptr=GetToken(ptr,"\\S",&Token,0);
//    DateStr=MCatStr(DateStr," ",Token,NULL);
    ptr=GetToken(ptr,"\\S",&Token,0);
 //   DateStr=MCatStr(DateStr," ",Token,NULL);

		*Path=CopyStr(*Path,ptr);

	DestroyString(Token);
}



int SSHGlob(HashratCtx *Ctx, char *URL, ListNode *Files)
{
char *Tempstr=NULL, *Path=NULL, *TermLine=NULL, *ptr;
STREAM *S;
int count=0;
struct stat *Stat;


S=SSHConnect(URL, &Tempstr, Ctx);
if (S)
{
	ptr=Tempstr;
	if (*ptr == '/') ptr++;
	
	Path=QuoteCharsInStr(Path,ptr," 	;&'`\"");
	TermLine=SSHGenerateReplayTerminator(TermLine, "LIST");
	Tempstr=MCopyStr(Tempstr,"ls -Llidn ",Path," 2> /dev/null\necho ",TermLine,"\n",NULL);
	STREAMWriteLine(Tempstr,S); STREAMFlush(S);

	Tempstr=STREAMReadLine(Tempstr,S);
	while (Tempstr)
	{
		StripTrailingWhitespace(Tempstr);
		if (strcmp(Tempstr,TermLine)==0) break;
		Stat=(struct stat *) calloc(1,sizeof(struct stat));
		Decode_LS_Output(Tempstr, &Path, Stat);
		Tempstr=CopyStr(Tempstr,URL);
		ptr=Tempstr+4; //go past 'ssh:'
		while (*ptr=='/') ptr++;
		ptr=strchr(ptr,'/');
		if (ptr) *ptr='\0';

		if (StrValid(Path))
		{
			Tempstr=MCatStr(Tempstr,"/",Path,NULL);
			if (Files) ListAddNamedItem(Files,Tempstr, Stat);
			else free(Stat);
			count++;
		}
		else free(Stat);
		Tempstr=STREAMReadLine(Tempstr,S);
	}

}

//Don't close 'S', it is reused as Ctx->NetCon
//STREAMClose(S);
DestroyString(Path);
DestroyString(Tempstr);
DestroyString(TermLine);

return(count);
}