summaryrefslogtreecommitdiffstats
path: root/usr.sbin/sup/lib/supmsg.c
blob: b12c945462f57da5dc6cabae3786c57a8fa54f4d (plain)
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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
/*
 * Copyright (c) 1992 Carnegie Mellon University
 * All Rights Reserved.
 * 
 * Permission to use, copy, modify and distribute this software and its
 * documentation is hereby granted, provided that both the copyright
 * notice and this permission notice appear in all copies of the
 * software, derivative works or modified versions, and any portions
 * thereof, and that both notices appear in supporting documentation.
 *
 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
 * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
 *
 * Carnegie Mellon requests users of this software to return to
 *
 *  Software Distribution Coordinator  or  Software_Distribution@CS.CMU.EDU
 *  School of Computer Science
 *  Carnegie Mellon University
 *  Pittsburgh PA 15213-3890
 *
 * any improvements or extensions that they make and grant Carnegie Mellon
 * the rights to redistribute these changes.
 */
/*
 **********************************************************************
 * HISTORY
 *
 * 7-July-93  Nate Williams at Montana State University
 *	Modified SUP to use gzip based compression when sending files
 *	across the network to save BandWidth
 *
 * $Log: supmsg.c,v $
 * Revision 1.1.1.1  1995/12/26 04:54:47  peter
 * Import the unmodified version of the sup that we are using.
 * The heritage of this version is not clear.  It appears to be NetBSD
 * derived from some time ago.
 *
 * Revision 1.1.1.1  1993/08/21  00:46:35  jkh
 * Current sup with compression support.
 *
 * Revision 1.1.1.1  1993/05/21  14:52:19  cgd
 * initial import of CMU's SUP to NetBSD
 *
 * Revision 2.4  92/09/09  22:05:17  mrt
 * 	Moved PFI definition under __STDC__ conditional since it
 * 	is already defined in libc.h in this case.
 * 	[92/09/01            mrt]
 * 
 * Revision 2.3  92/08/11  12:08:12  mrt
 * 	Added copyright
 * 	[92/08/10            mrt]
 * 	Brad's changes: Delinted, Incorporated updated variable 
 * 	argument list usage from old msgxfer.c
 * 	[92/07/24            mrt]
 * 
 * Revision 2.2  89/08/23  15:02:56  gm0w
 * 	Created from separate message modules.
 * 	[89/08/14            gm0w]
 * 
 **********************************************************************
 */
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#if __STDC__
#include <stdarg.h>
#else
#include <varargs.h>
#endif
#include <libc.h>
#include <c.h>
#include "sup.h"
#define MSGSUBR
#define MSGFILE
#include "supmsg.h"

/*
 * signon message
 */
extern int	pgmver;			/* program version of partner */
extern int	pgmversion;		/* my program version */
extern char	*scmver;		/* scm version of partner */
extern int	fspid;			/* process id of fileserver */

int msgsignon ()
{
	register int x;

	if (server) {
		x = readmsg (MSGSIGNON);
		if (x == SCMOK)  x = readint (&protver);
		if (x == SCMOK)  x = readint (&pgmver);
		if (x == SCMOK)  x = readstring (&scmver);
		if (x == SCMOK)  x = readmend ();
	} else {
		x = writemsg (MSGSIGNON);
		if (x == SCMOK)  x = writeint (PROTOVERSION);
		if (x == SCMOK)  x = writeint (pgmversion);
		if (x == SCMOK)  x = writestring (scmversion);
		if (x == SCMOK)  x = writemend ();
	}
	return (x);
}

int msgsignonack ()
{
	register int x;

	if (server) {
		x = writemsg (MSGSIGNONACK);
		if (x == SCMOK)  x = writeint (PROTOVERSION);
		if (x == SCMOK)  x = writeint (pgmversion);
		if (x == SCMOK)  x = writestring (scmversion);
		if (x == SCMOK)  x = writeint (fspid);
		if (x == SCMOK)  x = writemend ();
	} else {
		x = readmsg (MSGSIGNONACK);
		if (x == SCMOK)  x = readint (&protver);
		if (x == SCMOK)  x = readint (&pgmver);
		if (x == SCMOK)  x = readstring (&scmver);
		if (x == SCMOK)  x = readint (&fspid);
		if (x == SCMOK)  x = readmend ();
	}
	return (x);
}

/*
 * setup message
 */
extern int	xpatch;			/* setup crosspatch to a new client */
extern char	*xuser;			/* user,group,acct for crosspatch */
extern char	*collname;		/* base directory */
extern char	*basedir;		/* base directory */
extern int	basedev;		/* base directory device */
extern int	baseino;		/* base directory inode */
extern long	lasttime;		/* time of last upgrade */
extern int	listonly;		/* only listing files, no data xfer */
extern int	newonly;		/* only send new files */
extern char	*release;		/* release name */
extern int	setupack;		/* ack return value for setup */

int msgsetup ()
{
	register int x;

	if (server) {
		x = readmsg (MSGSETUP);
		if (x != SCMOK)  return (x);
		if (protver >= 7) {
			x = readint (&xpatch);
			if (x != SCMOK)  return (x);
		} else
			xpatch = FALSE;
		if (xpatch) {
			x = readstring (&xuser);
			if (x != SCMOK)  return (x);
			return (readmend ());
		}
		x = readstring (&collname);
		if (x == SCMOK)  x = readint ((int *)&lasttime);
		if (x == SCMOK)  x = readstring (&basedir);
		if (x == SCMOK)  x = readint (&basedev);
		if (x == SCMOK)  x = readint (&baseino);
		if (x == SCMOK)  x = readint (&listonly);
		if (x == SCMOK)  x = readint (&newonly);
		if (x == SCMOK)
			if (protver < 6)
				release = (char *)NULL;
			else
				x = readstring (&release);
		if (x == SCMOK)  x = readmend ();
	} else {
		x = writemsg (MSGSETUP);
		if (x != SCMOK)  return (x);
		if (protver >= 7) {
			x = writeint (xpatch);
			if (x != SCMOK)  return (x);
		}
		if (xpatch) {
			x = writestring (xuser);
			if (x != SCMOK)  return (x);
			return (writemend ());
		}
		if (x == SCMOK)  x = writestring (collname);
		if (x == SCMOK)  x = writeint ((int)lasttime);
		if (x == SCMOK)  x = writestring (basedir);
		if (x == SCMOK)  x = writeint (basedev);
		if (x == SCMOK)  x = writeint (baseino);
		if (x == SCMOK)  x = writeint (listonly);
		if (x == SCMOK)  x = writeint (newonly);
		if (x == SCMOK && protver >= 6)  x = writestring (release);
		if (x == SCMOK)  x = writemend ();
	}
	return (x);
}

int msgsetupack ()
{
	if (server)
		return (writemint (MSGSETUPACK,setupack));
	return (readmint (MSGSETUPACK,&setupack));
}

/*
 * crypt test message
 */
extern char	*crypttest;		/* encryption test string */

int msgcrypt ()
{
	if (server)
		return (readmstr (MSGCRYPT,&crypttest));
	return (writemstr (MSGCRYPT,crypttest));
}

int msgcryptok ()
{
	if (server)
		return (writemnull (MSGCRYPTOK));
	return (readmnull (MSGCRYPTOK));
}

/*
 * login message
 */
extern char	*logcrypt;		/* login encryption test */
extern char	*loguser;		/* login username */
extern char	*logpswd;		/* password for login */
extern int	logack;			/* login ack status */
extern char	*logerror;		/* error from login */

int msglogin ()
{
	register int x;
	if (server) {
		x = readmsg (MSGLOGIN);
		if (x == SCMOK)  x = readstring (&logcrypt);
		if (x == SCMOK)  x = readstring (&loguser);
		if (x == SCMOK)  x = readstring (&logpswd);
		if (x == SCMOK)  x = readmend ();
	} else {
		x = writemsg (MSGLOGIN);
		if (x == SCMOK)  x = writestring (logcrypt);
		if (x == SCMOK)  x = writestring (loguser);
		if (x == SCMOK)  x = writestring (logpswd);
		if (x == SCMOK)  x = writemend ();
	}
	return (x);
}

int msglogack ()
{
	register int x;
	if (server) {
		x = writemsg (MSGLOGACK);
		if (x == SCMOK)  x = writeint (logack);
		if (x == SCMOK)  x = writestring (logerror);
		if (x == SCMOK)  x = writemend ();
	} else {
		x = readmsg (MSGLOGACK);
		if (x == SCMOK)  x = readint (&logack);
		if (x == SCMOK)  x = readstring (&logerror);
		if (x == SCMOK)  x = readmend ();
	}
	return (x);
}

/*
 * refuse list message
 */
extern TREE	*refuseT;		/* tree of files to refuse */

static int refuseone (t)
register TREE *t;
{
	return (writestring (t->Tname));
}

int msgrefuse ()
{
	register int x;
	if (server) {
		char *name;
		x = readmsg (MSGREFUSE);
		if (x == SCMOK)  x = readstring (&name);
		while (x == SCMOK) {
			if (name == NULL)  break;
			(void) Tinsert (&refuseT,name,FALSE);
			free (name);
			x = readstring (&name);
		}
		if (x == SCMOK)  x = readmend ();
	} else {
		x = writemsg (MSGREFUSE);
		if (x == SCMOK)  x = Tprocess (refuseT,refuseone);
		if (x == SCMOK)  x = writestring ((char *)NULL);
		if (x == SCMOK)  x = writemend ();
	}
	return (x);
}

/*
 * list files message
 */
extern TREE	*listT;			/* tree of files to list */
extern TREE     *renameT;               /* tree of rename target files */
extern long	scantime;		/* time that collection was scanned */

static int listone (t)
register TREE *t;
{
	register int x;

	x = writestring (t->Tname);
	if ( protver > 8 )
		if (x == SCMOK)  x = writestring (t->Tnewname);
	if (x == SCMOK)  x = writeint ((int)t->Tmode);
	if (x == SCMOK)  x = writeint ((int)t->Tflags);
	if (x == SCMOK)  x = writeint (t->Tmtime);
	return (x);
}

int msglist ()
{
	register int x;
	if (server) {
		x = writemsg (MSGLIST);
		if (x == SCMOK)  x = Tprocess (listT,listone);
		if (x == SCMOK)  x = writestring ((char *)NULL);
		if (x == SCMOK)  x = writeint ((int)scantime);
		if (x == SCMOK)  x = writemend ();
	} else {
		char *name, *newname = NULL;
		int mode,flags,mtime;
		register TREE *t;
		x = readmsg (MSGLIST);
		if (x == SCMOK)  x = readstring (&name);
		while (x == SCMOK) {
			if (name == NULL)  break;
			if (protver > 8){
				x = readstring (&newname);
				if (x == SCMOK)  x = readint (&mode);
			}
			else
				x = readint (&mode);
			if (x == SCMOK)  x = readint (&flags);
			if (x == SCMOK)  x = readint (&mtime);
			if (x != SCMOK)  break;
			t = Tinsert (&listT,name,TRUE);
			free (name);
			t->Tnewname = newname;
			t->Tmode = mode;
			t->Tflags = flags;
			t->Tmtime = mtime;
			x = readstring (&name);
		}
		if (x == SCMOK)  x = readint ((int *)&scantime);
		if (x == SCMOK)  x = readmend ();
	}
	return (x);
}

/*
 * files needed message
 */
extern TREE	*needT;			/* tree of files to need */

static int needone (t)
register TREE *t;
{
	register int x;
	x = writestring (t->Tname);
	if (x == SCMOK)  x = writeint ((t->Tflags&FUPDATE) != 0);
	return (x);
}

int msgneed ()
{
	register int x;
	if (server) {
		char *name;
		int update;
		register TREE *t;
		x = readmsg (MSGNEED);
		if (x == SCMOK)  x = readstring (&name);
		while (x == SCMOK) {
			if (name == NULL)  break;
			x = readint (&update);
			if (x != SCMOK)  break;
			t = Tinsert (&needT,name,TRUE);
			free (name);
			if (update)  t->Tflags |= FUPDATE;
			x = readstring (&name);
		}
		if (x == SCMOK)  x = readmend ();
	} else {
		x = writemsg (MSGNEED);
		if (x == SCMOK)  x = Tprocess (needT,needone);
		if (x == SCMOK)  x = writestring ((char *)NULL);
		if (x == SCMOK)  x = writemend ();
	}
	return (x);
}

/*
 * files denied message
 */
extern TREE	*denyT;			/* tree of files to deny */

static int denyone (t)
register TREE *t;
{
	return (writestring (t->Tname));
}

int msgdeny ()
{
	register int x;
	if (server) {
		x = writemsg (MSGDENY);
		if (x == SCMOK)  x = Tprocess (denyT,denyone);
		if (x == SCMOK)  x = writestring ((char *)NULL);
		if (x == SCMOK)  x = writemend ();
	} else {
		char *name;
		x = readmsg (MSGDENY);
		if (x == SCMOK)  x = readstring (&name);
		while (x == SCMOK) {
			if (name == NULL)  break;
			(void) Tinsert (&denyT,name,FALSE);
			free (name);
			x = readstring (&name);
		}
		if (x == SCMOK)  x = readmend ();
	}
	return (x);
}

/*
 * send file message
 */
int msgsend ()
{
	if (server)
		return (readmnull (MSGSEND));
	return (writemnull (MSGSEND));
}

/*
 * receive file message
 */
extern TREE	*upgradeT;		/* pointer to file being upgraded */

static int writeone (t)
register TREE *t;
{
	return (writestring (t->Tname));
}


#if __STDC__
int msgrecv (PFI xferfile,...)
#else
/*VARARGS*//*ARGSUSED*/
int msgrecv (va_alist)
va_dcl
#endif
{
#if !__STDC__
	typedef int (*PFI)();
	PFI xferfile;
#endif
	va_list args;
	register int x;
	register TREE *t = upgradeT;
#if __STDC__
	va_start(args,xferfile);
#else
	va_start(args);
	xferfile = va_arg(args, PFI);
#endif
	if (server) {
		x = writemsg (MSGRECV);
		if (t == NULL) {
			if (x == SCMOK)  x = writestring ((char *)NULL);
			if (x == SCMOK)  x = writemend ();
			return (x);
		}
		if (x == SCMOK)  x = writestring (t->Tname);
		if (protver > 8)
			if (x == SCMOK)  x = writestring (t->Tnewname);
		if (x == SCMOK)  x = writeint (t->Tmode);
		if (t->Tmode == 0) {
			if (x == SCMOK)  x = writemend ();
			return (x);
		}
		if (x == SCMOK)  x = writeint (t->Tflags);
		if (x == SCMOK)  x = writestring (t->Tuser);
		if (x == SCMOK)  x = writestring (t->Tgroup);
		if (x == SCMOK)  x = writeint (t->Tmtime);
		if (x == SCMOK)  x = Tprocess (t->Tlink,writeone);
		if (x == SCMOK)  x = writestring ((char *)NULL);
		if (x == SCMOK)  x = Tprocess (t->Texec,writeone);
		if (x == SCMOK)  x = writestring ((char *)NULL);
		if (x == SCMOK)  x = (*xferfile) (t,args);
		if (x == SCMOK)  x = writemend ();
	} else {
		char *linkname,*execcmd;
		if (t == NULL)  return (SCMERR);
		x = readmsg (MSGRECV);
		if (x == SCMOK)  x = readstring (&t->Tname);
		if (x == SCMOK && t->Tname == NULL) {
			x = readmend ();
			if (x == SCMOK)  x = (*xferfile) (NULL,args);
			return (x);
		}
		if (protver > 8)
			if (x == SCMOK)  x = readstring (&t->Tnewname);
		if (x == SCMOK)  x = readint (&t->Tmode);
		if (t->Tmode == 0) {
			x = readmend ();
			if (x == SCMOK)  x = (*xferfile) (t,args);
			return (x);
		}
		if (x == SCMOK)  x = readint (&t->Tflags);
		if (x == SCMOK)  x = readstring (&t->Tuser);
		if (x == SCMOK)  x = readstring (&t->Tgroup);
		if (x == SCMOK)  x = readint (&t->Tmtime);
		t->Tlink = NULL;
		if (x == SCMOK)  x = readstring (&linkname);
		while (x == SCMOK) {
			if (linkname == NULL)  break;
			(void) Tinsert (&t->Tlink,linkname,FALSE);
			free (linkname);
			x = readstring (&linkname);
		}
		t->Texec = NULL;
		if (x == SCMOK)  x = readstring (&execcmd);
		while (x == SCMOK) {
			if (execcmd == NULL)  break;
			(void) Tinsert (&t->Texec,execcmd,FALSE);
			free (execcmd);
			x = readstring (&execcmd);
		}
		if (x == SCMOK)  x = (*xferfile) (t,args);
		if (x == SCMOK)  x = readmend ();
	}
	va_end(args);
	return (x);
}

/*
 * protocol done message
 */
extern int	doneack;
extern char	*donereason;

int msgdone ()
{
	register int x;

	if (protver < 6) {
		printf ("Error, msgdone should not have been called.");
		return (SCMERR);
	}
	if (server) {
		x = readmsg (MSGDONE);
		if (x == SCMOK)  x = readint (&doneack);
		if (x == SCMOK)  x = readstring (&donereason);
		if (x == SCMOK)  x = readmend ();
	} else {
		x = writemsg (MSGDONE);
		if (x == SCMOK)  x = writeint (doneack);
		if (x == SCMOK)  x = writestring (donereason);
		if (x == SCMOK)  x = writemend ();
	}
	return (x);
}

/*
 * go away message
 */
extern char	*goawayreason;		/* reason for goaway */

int msggoaway ()
{
	return (writemstr (MSGGOAWAY,goawayreason));
}

/*
 * cross-patch protocol message
 */
extern int	xargc;			/* arg count for crosspatch */
extern char	**xargv;		/* arg array for crosspatch */

int msgxpatch ()
{
	register int x;
	register int i;

	if (server) {
		x = readmsg (MSGXPATCH);
		if (x != SCMOK)  return (x);
		x = readint (&xargc);
		if (x != SCMOK)  return (x);
		xargc += 2;
		xargv = (char **)calloc (sizeof (char *),(unsigned)xargc+1);
		if (xargv == NULL)
			return (SCMERR);
		for (i = 2; i < xargc; i++) {
			x = readstring (&xargv[i]);
			if (x != SCMOK)  return (x);
		}
		x = readmend ();
	} else {
		x = writemsg (MSGXPATCH);
		if (x != SCMOK)  return (x);
		x = writeint (xargc);
		if (x != SCMOK)  return (x);
		for (i = 0; i < xargc; i++) {
			x = writestring (xargv[i]);
			if (x != SCMOK)  return (x);
		}
		x = writemend ();
	}
	return (x);
}

/*
 * Compression check protocol message
 */
extern int	docompress;		/* Compress file before sending? */

int msgcompress ()
{
	if (server)
		return (readmint (MSGCOMPRESS,&docompress));
	return (writemint (MSGCOMPRESS, docompress));
}
OpenPOWER on IntegriCloud