summaryrefslogtreecommitdiff
path: root/src/tet3/xresd/tfproc.c
blob: 5eac06016b43fc0b1f735901ef3a89002ed4cddb (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
/*
 *      SCCS:  @(#)tfproc.c	1.11 (98/09/01) 
 *
 *	UniSoft Ltd., London, England
 *
 * (C) Copyright 1992 X/Open Company Limited
 * (C) Copyright 1994 UniSoft Limited
 *
 * All rights reserved.  No part of this source code may be reproduced,
 * stored in a retrieval system, or transmitted, in any form or by any
 * means, electronic, mechanical, photocopying, recording or otherwise,
 * except as stated in the end-user licence agreement, without the prior
 * permission of the copyright owners.
 *
 * X/Open and the 'X' symbol are trademarks of X/Open Company Limited in
 * the UK and other countries.
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

/************************************************************************

SCCS:   	@(#)tfproc.c	1.11 98/09/01 TETware release 3.3
NAME:		tfproc.c
PRODUCT:	TETware
AUTHOR:		Andrew Dingwall, UniSoft Ltd.
DATE CREATED:	April 1992

DESCRIPTION:
	file transfer request processing functions

MODIFICATIONS:
	Andrew Dingwall, UniSoft Ltd., November 1993
	enhancements for FIFO transport interface

	Andrew Dingwall, UniSoft Ltd., March 1998
	Avoid passing a -ve precision value to sprintf().

	Aaron Plattner, April 2010
	Fixed warnings when compiled with GCC's -Wall option.

************************************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#  include <unistd.h>
#include "dtmac.h"
#include "dtmsg.h"
#include "ptab.h"
#include "llist.h"
#include "avmsg.h"
#include "btmsg.h"
#include "valmsg.h"
#include "error.h"
#include "bstring.h"
#include "xresd.h"
#include "dtetlib.h"

#ifndef NOTRACE
#include "ltoa.h"
#endif


/*
**	Transfer file table.
**
**	An entry is allocated in the transfer file table for each transfer
**	file opened by a call to OP_TFOPEN.
**	Storage for an element is allocated by tfalloc() and freed by
**	tffree().
**	An element is added to the table by tfadd() and removed by tfrm().
*/

struct tftab {
	struct tftab *tf_next;		/* ptr to next element in list */
	struct tftab *tf_last;		/* ptr to prev element in list */
	int tf_id;			/* id for xfer requests */
	struct ptab *tf_ptab;		/* ptr to owner's ptab */
	char *tf_name;			/* xfer file name */
	int tf_mode;			/* xfer file mode */
	FILE *tf_fp;			/* fp for xfer file */
};

static struct tftab *tftab;		/* ptr to head of xfer file table */


/* static function declarations */
static int dotfclose PROTOLIST((struct tftab *));
static int op_tfo2 PROTOLIST((struct ptab *, struct tftab *, char *));
static void tfadd PROTOLIST((struct tftab *));
static struct tftab *tfalloc PROTOLIST((void));
static struct tftab *tffind PROTOLIST((int));
static void tffree PROTOLIST((struct tftab *));
static void tfrm PROTOLIST((struct tftab *));


/*
**	op_tfopen() - process an OP_TFOPEN request
*/

void op_tfopen(pp)
register struct ptab *pp;
{
	register struct avmsg *mp = (struct avmsg *) pp->ptm_data;
	register struct tftab *tp;
	char *tfname;

	/* do some sanity checks on the request message */
	if (mp->av_argc != OP_TFOPEN_ARGC || !AV_TFNAME(mp)) {
		pp->ptm_rc = ER_INVAL;
		pp->ptm_mtype = MT_NODATA;
		pp->ptm_len = 0;
		return;
	}

	TRACE2(tet_Txresd, 4, "OP_TFOPEN: file = \"%s\"", AV_TFNAME(mp));

	/* remove a leading '/' if present */
	tfname = AV_TFNAME(mp);
	while (*tfname && isdirsep(*tfname))
		tfname++;

	/* get a new tftab element for this request */
	if ((tp = tfalloc()) == (struct tftab *) 0) {
		pp->ptm_rc = ER_ERR;
		pp->ptm_mtype = MT_NODATA;
		pp->ptm_len = 0;
		return;
	}
	tp->tf_mode = (int) AV_MODE(mp);

	if ((pp->ptm_rc = op_tfo2(pp, tp, tfname)) == ER_OK) {
		tp->tf_ptab = pp;
		tfadd(tp);
		pp->ptm_mtype = MT_VALMSG;
		pp->ptm_len = valmsgsz(OP_TFOPEN_NVALUE);
	}
	else {
		tffree(tp);
		pp->ptm_mtype = MT_NODATA;
		pp->ptm_len = 0;
	}
}

/*
**	op_tfo2() - extend the op_tfopen() processing
**
**	return ER_OK if successful or other ER_* error code on error
*/

static int op_tfo2(pp, tp, tfname)
struct ptab *pp;
register struct tftab *tp;
char *tfname;
{
	register struct valmsg *rp;
	register char *p;
	int len;
	char tfpath[MAXPATH + 1];
	extern char *Tet_savedir;

	/* make sure that the savedir is accessible */
	if (tet_eaccess(Tet_savedir, 02) < 0) {
		error(errno, "can't write in", Tet_savedir);
		return(ER_ERR);
	}

	/* construct the transfer file name and store it */
	len = (int) sizeof tfpath - (int) strlen(Tet_savedir) - 2;
	(void) sprintf(tfpath, "%.*s/%.*s",
		(int) sizeof tfpath - 2, Tet_savedir, TET_MAX(len, 0), tfname);
	if ((tp->tf_name = tet_strstore(tfpath)) == (char *) 0)
		return(ER_ERR);
	TRACE2(tet_Txresd, 6, "op_tfo2(): full path = \"%s\"\n", tp->tf_name);

	/* make sure that the message buffer is big enough for the reply */
	if (BUFCHK(&pp->ptm_data, &pp->pt_mdlen, valmsgsz(OP_TFOPEN_NVALUE)) < 0)
		return(ER_ERR);
	rp = (struct valmsg *) pp->ptm_data;

	/* make any required directories */
	for (p = tp->tf_name; *p; p++)
		;
	while (--p >= tp->tf_name)
		if (isdirsep(*p))
			break;
	if (p > tp->tf_name) {
		*p = '\0';
		if (tet_mkalldirs(tp->tf_name) < 0)
			return(ER_ERR);
		*p = '/';
	}

	/* open the transfer file */
	if ((tp->tf_fp = fopen(tp->tf_name, "w")) == NULL) {
		error(errno, "can't open", tp->tf_name);
		return(ER_ERR);
	}

	/* all ok so fill in the reply message and return */
	TRACE2(tet_Txresd, 4, "OP_TFOPEN: return fid = %s", tet_i2a(tp->tf_id));
	VM_XFID(rp) = (long) tp->tf_id;
	rp->vm_nvalue = OP_TFOPEN_NVALUE;
	return(ER_OK);
}

/*
**	op_tfclose() - process an OP_TFCLOSE request
*/

void op_tfclose(pp)
register struct ptab *pp;
{
	register struct valmsg *mp = (struct valmsg *) pp->ptm_data;
	register struct tftab *tp;

	/* all reply messages have no data */
	pp->ptm_mtype = MT_NODATA;
	pp->ptm_len = 0;

	TRACE2(tet_Txresd, 4, "OP_TFCLOSE: fid = %s", tet_i2a(VM_XFID(mp)));

	/* find the tftab element */
	if ((tp = tffind((int) VM_XFID(mp))) == (struct tftab *) 0) {
		pp->ptm_rc = ER_FID;
		return;
	}

	pp->ptm_rc = dotfclose(tp);
}

/*
**	op_tfwrite() - process an OP_TFWRITE request
*/

void op_tfwrite(pp)
register struct ptab *pp;
{
	register struct btmsg *mp = (struct btmsg *) pp->ptm_data;
	register struct tftab *tp;

	/* all reply messages have no data */
	pp->ptm_mtype = MT_NODATA;
	pp->ptm_len = 0;

	TRACE3(tet_Txresd, 4, "OP_TFWRITE: fid = %s, count = %s",
		tet_i2a(mp->bt_fid), tet_i2a(mp->bt_count));

	/* find the tftab element */
	if ((tp = tffind((int) mp->bt_fid)) == (struct tftab *) 0) {
		pp->ptm_rc = ER_FID;
		return;
	}

	/* make sure that count is plausible */
	if (!mp->bt_count || mp->bt_count > sizeof mp->bt_data) {
		pp->ptm_rc = ER_INVAL;
		return;
	}

	/* write the data */
	if (fwrite(mp->bt_data, sizeof mp->bt_data[0], (int) mp->bt_count, tp->tf_fp) != (int) mp->bt_count || fflush(tp->tf_fp) == EOF) {
		error(errno, "write failed on", tp->tf_name);
		pp->ptm_rc = ER_ERR;
		return;
	}

	pp->ptm_rc = ER_OK;
	return;
}

/*
**	tfdead() - tftab processing when a connection closes
*/

void tfdead(pp)
register struct ptab *pp;
{
	register struct tftab *tp;
	register int done;

	do {
		done = 1;
		for (tp = tftab; tp; tp = tp->tf_next)
			if (tp->tf_ptab == pp) {
				(void) dotfclose(tp);
				done = 0;
				break;
			}
	} while (!done);
}

/*
**	dotfclose() - internal part of op_tfclose()
**
**	return ER_OK if successful or other ER_* error code on error
*/

static int dotfclose(tp)
register struct tftab *tp;
{
	register int rc;

	/* perform the close operation */
	if (fclose(tp->tf_fp) == EOF) {
		error(errno, "fclose failed on", tp->tf_name);
		rc = ER_ERR;
	}
	else
		rc = ER_OK;

	/* update the file mode */
	if (CHMOD(tp->tf_name, (mode_t) tp->tf_mode) < 0)
		error(errno, "warning: can't chmod", tp->tf_name);

	/* remove the xfer table entry and free it */
	tfrm(tp);
	tffree(tp);

	return(rc);
}

/*
**	tfalloc() - allocate an xfer file table element and return a pointer
**	thereto
**
**	return (struct tftab *) 0 on error
*/

static struct tftab *tfalloc()
{
	register struct tftab *tp;
	static int tfid = 0;

	if ((tp = (struct tftab *) malloc(sizeof *tp)) == (struct tftab *) 0) {
		error(errno, "can't allocate tftab element", (char *) 0);
		return((struct tftab *) 0);
	}
	TRACE2(tet_Tbuf, 6, "allocate tftab = %s", tet_i2x(tp));
	bzero((char *) tp, sizeof *tp);

	tp->tf_id = ++tfid;

	return(tp);
}

/*
**	tffree() - free an xfer file table element
*/

static void tffree(tp)
struct tftab *tp;
{
	TRACE2(tet_Tbuf, 6, "free tftab = %s", tet_i2x(tp));

	if (tp) {
		if (tp->tf_name) {
			TRACE2(tet_Tbuf, 6, "free tftab name = %s",
				tet_i2x(tp->tf_name));
			free(tp->tf_name);
		}
		free((char *) tp);
	}
}

/*
**	tfadd() - insert an element in the tftab list
*/

static void tfadd(tp)
struct tftab *tp;
{
	tet_listinsert((struct llist **) &tftab, (struct llist *) tp);
}

/*
**	tfrm() - remove an element from the tftab list
*/

static void tfrm(tp)
struct tftab *tp;
{
	tet_listremove((struct llist **) &tftab, (struct llist *) tp);
}

/*
**	tffind() - find a tftab element matching id and return a pointer
**		thereto
**
**	return (struct tftab *) 0 if none can be found
*/

static struct tftab *tffind(id)
register int id;
{
	register struct tftab *tp;

	for (tp = tftab; tp; tp = tp->tf_next)
		if (tp->tf_id == id)
			break;

	return(tp);
}