View | Details | Raw Unified | Return to bug 474 | Differences between
and this patch

Collapse All | Expand All

(-)sftp-server.c.orig (-14 / +224 lines)
Lines 40-45 Link Here
40
/* Version of client */
40
/* Version of client */
41
int version;
41
int version;
42
42
43
#ifdef SFTP_LOGGING
44
/* User information. */
45
#define CUNAME				cuname ? cuname : "UNKNOWN"
46
struct passwd *upw;
47
uid_t cuid;
48
pid_t ppid;
49
char *cuname;
50
#endif
51
43
/* portable attributes, etc. */
52
/* portable attributes, etc. */
44
53
45
typedef struct Stat Stat;
54
typedef struct Stat Stat;
Lines 81-86 Link Here
81
	return ret;
90
	return ret;
82
}
91
}
83
92
93
#ifdef SFTP_LOGGING
94
char*
95
status_to_logstr(int status)
96
{
97
	switch (status) {
98
		case SSH2_FX_OK:
99
			return "Successful";
100
		case SSH2_FX_NO_SUCH_FILE:
101
			return "No such file or directory";
102
		case SSH2_FX_PERMISSION_DENIED:
103
			return "Permission denied.";
104
		case SSH2_FX_BAD_MESSAGE:
105
			return "Bad message";
106
		default:
107
			return "Unknown error";
108
	}
109
}
110
#endif
111
84
static int
112
static int
85
flags_from_portable(int pflags)
113
flags_from_portable(int pflags)
86
{
114
{
Lines 103-108 Link Here
103
	return flags;
131
	return flags;
104
}
132
}
105
133
134
#ifdef SFTP_LOGGING
135
void
136
sflags_from_portable(char *psflags, int pflags)
137
{
138
	if (pflags & SSH2_FXF_READ)
139
		*psflags = 'r';
140
	psflags++;
141
	if (pflags & SSH2_FXF_WRITE)
142
		*psflags = 'w';
143
	psflags++;
144
	if (pflags & SSH2_FXF_APPEND)
145
		*psflags = 'a';
146
	psflags++;
147
	if (pflags & SSH2_FXF_CREAT)
148
		*psflags = 'c';
149
	psflags++;
150
	if (pflags & SSH2_FXF_TRUNC)
151
		*psflags = 't';
152
	psflags++;
153
	if (pflags & SSH2_FXF_EXCL)
154
		*psflags = 'e';
155
	return;
156
}
157
#endif
158
106
static Attrib *
159
static Attrib *
107
get_attrib(void)
160
get_attrib(void)
108
{
161
{
Lines 360-365 Link Here
360
413
361
	version = get_int();
414
	version = get_int();
362
	TRACE("client version %d", version);
415
	TRACE("client version %d", version);
416
#ifdef SFTP_LOGGING
417
	log("(%d/%d/%s) Client version %d.", ppid, cuid, CUNAME, version);
418
#endif
363
	buffer_init(&msg);
419
	buffer_init(&msg);
364
	buffer_put_char(&msg, SSH2_FXP_VERSION);
420
	buffer_put_char(&msg, SSH2_FXP_VERSION);
365
	buffer_put_int(&msg, SSH2_FILEXFER_VERSION);
421
	buffer_put_int(&msg, SSH2_FILEXFER_VERSION);
Lines 373-378 Link Here
373
	u_int32_t id, pflags;
429
	u_int32_t id, pflags;
374
	Attrib *a;
430
	Attrib *a;
375
	char *name;
431
	char *name;
432
#ifdef SFTP_LOGGING
433
	char sflags[7] = "------";
434
#endif
376
	int handle, fd, flags, mode, status = SSH2_FX_FAILURE;
435
	int handle, fd, flags, mode, status = SSH2_FX_FAILURE;
377
436
378
	id = get_int();
437
	id = get_int();
Lines 380-397 Link Here
380
	pflags = get_int();		/* portable flags */
439
	pflags = get_int();		/* portable flags */
381
	a = get_attrib();
440
	a = get_attrib();
382
	flags = flags_from_portable(pflags);
441
	flags = flags_from_portable(pflags);
442
#ifdef SFTP_LOGGING
443
	sflags_from_portable(&sflags[0], pflags);
444
#endif
383
	mode = (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) ? a->perm : 0666;
445
	mode = (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) ? a->perm : 0666;
384
	TRACE("open id %u name %s flags %d mode 0%o", id, name, pflags, mode);
446
	TRACE("open id %u name %s flags %d mode 0%o", id, name, pflags, mode);
385
	fd = open(name, flags, mode);
447
	fd = open(name, flags, mode);
386
	if (fd < 0) {
448
	if (fd < 0) {
387
		status = errno_to_portable(errno);
449
		status = errno_to_portable(errno);
450
#ifdef SFTP_LOGGING
451
		log("(%d/%d/%s) File/Dir open failed - %s: %s (%s,%04o)", ppid, cuid, CUNAME, status_to_logstr(status), name, sflags, mode);
452
#endif
388
	} else {
453
	} else {
389
		handle = handle_new(HANDLE_FILE, name, fd, NULL);
454
		handle = handle_new(HANDLE_FILE, name, fd, NULL);
390
		if (handle < 0) {
455
		if (handle < 0) {
391
			close(fd);
456
			close(fd);
457
#ifdef SFTP_LOGGING
458
			log("(%d/%d/%s) File/Dir open failed - Could not allocate SFTP handle: %s (%s,%04o).", ppid, cuid, CUNAME, name, sflags, mode);
459
#endif
392
		} else {
460
		} else {
393
			send_handle(id, handle);
461
			send_handle(id, handle);
394
			status = SSH2_FX_OK;
462
			status = SSH2_FX_OK;
463
#ifdef SFTP_LOGGING
464
			log("(%d/%d/%s) File/Dir opened: %s (%s,%04o).", ppid, cuid, CUNAME, name, sflags, mode);
465
#endif
395
		}
466
		}
396
	}
467
	}
397
	if (status != SSH2_FX_OK)
468
	if (status != SSH2_FX_OK)
Lines 580-602 Link Here
580
	TRACE("setstat id %u name %s", id, name);
651
	TRACE("setstat id %u name %s", id, name);
581
	if (a->flags & SSH2_FILEXFER_ATTR_SIZE) {
652
	if (a->flags & SSH2_FILEXFER_ATTR_SIZE) {
582
		ret = truncate(name, a->size);
653
		ret = truncate(name, a->size);
583
		if (ret == -1)
654
		if (ret == -1) {
584
			status = errno_to_portable(errno);
655
			status = errno_to_portable(errno);
656
#ifdef SFTP_LOGGING
657
			log("(%d/%d/%s) Truncate file failed - %s: %s.", ppid, cuid, CUNAME, status_to_logstr(status), name);
658
		} else {
659
			log("(%d/%d/%s) Truncated file: %s.", ppid, cuid, CUNAME, name);
660
#endif
661
		}
585
	}
662
	}
586
	if (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) {
663
	if (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) {
587
		ret = chmod(name, a->perm & 0777);
664
		ret = chmod(name, a->perm & 0777);
588
		if (ret == -1)
665
		if (ret == -1) {
589
			status = errno_to_portable(errno);
666
			status = errno_to_portable(errno);
667
#ifdef SFTP_LOGGING
668
			log("(%d/%d/%s) Set file/dir permissions failed - %s: %s (%04o).", ppid, cuid, CUNAME, status_to_logstr(status), name, a->perm & 0777);
669
		} else {
670
			log("(%d/%d/%s) Set file/dir permissions: %s (%04o).", ppid, cuid, CUNAME, name, a->perm & 0777);
671
#endif
672
		}
590
	}
673
	}
591
	if (a->flags & SSH2_FILEXFER_ATTR_ACMODTIME) {
674
	if (a->flags & SSH2_FILEXFER_ATTR_ACMODTIME) {
592
		ret = utimes(name, attrib_to_tv(a));
675
		ret = utimes(name, attrib_to_tv(a));
593
		if (ret == -1)
676
		if (ret == -1) {
594
			status = errno_to_portable(errno);
677
			status = errno_to_portable(errno);
678
#ifdef SFTP_LOGGING
679
			log("(%d/%d/%s) Set file/dir times failed - %s: %s.", ppid, cuid, CUNAME, status_to_logstr(status), name);
680
		} else {
681
			log("(%d/%d/%s) Set file/dir times: %s.", ppid, cuid, CUNAME, name);
682
#endif
683
		}
595
	}
684
	}
596
	if (a->flags & SSH2_FILEXFER_ATTR_UIDGID) {
685
	if (a->flags & SSH2_FILEXFER_ATTR_UIDGID) {
597
		ret = chown(name, a->uid, a->gid);
686
		ret = chown(name, a->uid, a->gid);
598
		if (ret == -1)
687
		if (ret == -1) {
599
			status = errno_to_portable(errno);
688
			status = errno_to_portable(errno);
689
#ifdef SFTP_LOGGING
690
			log("(%d/%d/%s) Set file/dir owner/group failed - %s: %s (%d/%d).", ppid, cuid, CUNAME, status_to_logstr(status), name, a->uid, a->gid);
691
		} else {
692
			log("(%d/%d/%s) Set file/dir owner/group: %s (%d/%d).", ppid, cuid, CUNAME, name, a->uid, a->gid);
693
#endif
694
		}
600
	}
695
	}
601
	send_status(id, status);
696
	send_status(id, status);
602
	xfree(name);
697
	xfree(name);
Lines 619-629 Link Here
619
	name = handle_to_name(handle);
714
	name = handle_to_name(handle);
620
	if (fd < 0 || name == NULL) {
715
	if (fd < 0 || name == NULL) {
621
		status = SSH2_FX_FAILURE;
716
		status = SSH2_FX_FAILURE;
717
#ifdef SFTP_LOGGING
718
		log("(%d/%d/%s) Set file/dir stats failed - could not get name from handle: %d.", ppid, cuid, CUNAME, handle);
719
#endif
622
	} else {
720
	} else {
623
		if (a->flags & SSH2_FILEXFER_ATTR_SIZE) {
721
		if (a->flags & SSH2_FILEXFER_ATTR_SIZE) {
624
			ret = ftruncate(fd, a->size);
722
			ret = ftruncate(fd, a->size);
625
			if (ret == -1)
723
			if (ret == -1) {
626
				status = errno_to_portable(errno);
724
				status = errno_to_portable(errno);
725
#ifdef SFTP_LOGGING
726
				log("(%d/%d/%s) Truncate file failed - %s: %s.", ppid, cuid, CUNAME, status_to_logstr(status), name);
727
			} else {
728
				log("(%d/%d/%s) Truncated file: %s.", ppid, cuid, CUNAME, name);
729
#endif
730
			}
627
		}
731
		}
628
		if (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) {
732
		if (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) {
629
#ifdef HAVE_FCHMOD
733
#ifdef HAVE_FCHMOD
Lines 631-638 Link Here
631
#else
735
#else
632
			ret = chmod(name, a->perm & 0777);
736
			ret = chmod(name, a->perm & 0777);
633
#endif
737
#endif
634
			if (ret == -1)
738
			if (ret == -1) {
635
				status = errno_to_portable(errno);
739
				status = errno_to_portable(errno);
740
#ifdef SFTP_LOGGING
741
				log("(%d/%d/%s) Set file/dir permissions failed - %s: %s (%04o).", ppid, cuid, CUNAME, status_to_logstr(status), name, a->perm & 0777);
742
			} else {
743
				log("(%d/%d/%s) Set file/dir permissions: %s (%04o).", ppid, cuid, CUNAME, name, a->perm & 0777);
744
#endif
745
			}
636
		}
746
		}
637
		if (a->flags & SSH2_FILEXFER_ATTR_ACMODTIME) {
747
		if (a->flags & SSH2_FILEXFER_ATTR_ACMODTIME) {
638
#ifdef HAVE_FUTIMES
748
#ifdef HAVE_FUTIMES
Lines 640-647 Link Here
640
#else
750
#else
641
			ret = utimes(name, attrib_to_tv(a));
751
			ret = utimes(name, attrib_to_tv(a));
642
#endif
752
#endif
643
			if (ret == -1)
753
			if (ret == -1) {
644
				status = errno_to_portable(errno);
754
				status = errno_to_portable(errno);
755
#ifdef SFTP_LOGGING
756
				log("(%d/%d/%s) Set file/dir times failed - %s: %s.", ppid, cuid, CUNAME, status_to_logstr(status), name);
757
			} else {
758
				log("(%d/%d/%s) Set file/dir times: %s.", ppid, cuid, CUNAME, name);
759
#endif
760
			}
645
		}
761
		}
646
		if (a->flags & SSH2_FILEXFER_ATTR_UIDGID) {
762
		if (a->flags & SSH2_FILEXFER_ATTR_UIDGID) {
647
#ifdef HAVE_FCHOWN
763
#ifdef HAVE_FCHOWN
Lines 649-656 Link Here
649
#else
765
#else
650
			ret = chown(name, a->uid, a->gid);
766
			ret = chown(name, a->uid, a->gid);
651
#endif
767
#endif
652
			if (ret == -1)
768
			if (ret == -1) {
653
				status = errno_to_portable(errno);
769
				status = errno_to_portable(errno);
770
#ifdef SFTP_LOGGING
771
				log("(%d/%d/%s) Set file/dir owner/group failed - %s: %s (%d/%d).", ppid, cuid, CUNAME, status_to_logstr(status), name, a->uid, a->gid);
772
			} else {
773
				log("(%d/%d/%s) Set file/dir owner/group: %s (%d/%d).", ppid, cuid, CUNAME, name, a->uid, a->gid);
774
#endif
775
			}
654
		}
776
		}
655
	}
777
	}
656
	send_status(id, status);
778
	send_status(id, status);
Lines 752-758 Link Here
752
	name = get_string(NULL);
874
	name = get_string(NULL);
753
	TRACE("remove id %u name %s", id, name);
875
	TRACE("remove id %u name %s", id, name);
754
	ret = unlink(name);
876
	ret = unlink(name);
755
	status = (ret == -1) ? errno_to_portable(errno) : SSH2_FX_OK;
877
	if (ret == -1) {
878
		status = errno_to_portable(errno);
879
#ifdef SFTP_LOGGING
880
		log("(%d/%d/%s) File deletion failed - %s: %s.", ppid, cuid, CUNAME, status_to_logstr(status), name);
881
#endif
882
	} else {
883
		status = SSH2_FX_OK;
884
#ifdef SFTP_LOGGING
885
		log("(%d/%d/%s) File deleted: %s.", ppid, cuid, CUNAME, name);
886
#endif
887
	}
756
	send_status(id, status);
888
	send_status(id, status);
757
	xfree(name);
889
	xfree(name);
758
}
890
}
Lines 772-778 Link Here
772
	    a->perm & 0777 : 0777;
904
	    a->perm & 0777 : 0777;
773
	TRACE("mkdir id %u name %s mode 0%o", id, name, mode);
905
	TRACE("mkdir id %u name %s mode 0%o", id, name, mode);
774
	ret = mkdir(name, mode);
906
	ret = mkdir(name, mode);
775
	status = (ret == -1) ? errno_to_portable(errno) : SSH2_FX_OK;
907
	if (ret == -1) {
908
		status = errno_to_portable(errno);
909
#ifdef SFTP_LOGGING
910
		log("(%d/%d/%s) Directory creation failed - %s: %s.", ppid, cuid, CUNAME, status_to_logstr(status), name);
911
#endif
912
	} else {
913
		status = SSH2_FX_OK;
914
#ifdef SFTP_LOGGING
915
		log("(%d/%d/%s) Directory created: %s.", ppid, cuid, CUNAME, name);
916
#endif
917
	}
776
	send_status(id, status);
918
	send_status(id, status);
777
	xfree(name);
919
	xfree(name);
778
}
920
}
Lines 788-794 Link Here
788
	name = get_string(NULL);
930
	name = get_string(NULL);
789
	TRACE("rmdir id %u name %s", id, name);
931
	TRACE("rmdir id %u name %s", id, name);
790
	ret = rmdir(name);
932
	ret = rmdir(name);
791
	status = (ret == -1) ? errno_to_portable(errno) : SSH2_FX_OK;
933
	if (ret == -1) {
934
		status = errno_to_portable(errno);
935
#ifdef SFTP_LOGGING
936
		log("(%d/%d/%s) Directory deletion failed - %s: %s.", ppid, cuid, CUNAME, status_to_logstr(status), name);
937
#endif
938
	} else {
939
		status = SSH2_FX_OK;
940
#ifdef SFTP_LOGGING
941
		log("(%d/%d/%s) Directory deleted: %s.", ppid, cuid, CUNAME, name);
942
#endif
943
	}
792
	send_status(id, status);
944
	send_status(id, status);
793
	xfree(name);
945
	xfree(name);
794
}
946
}
Lines 864-873 Link Here
864
		} else
1016
		} else
865
			status = SSH2_FX_OK;
1017
			status = SSH2_FX_OK;
866
	} else if (stat(newpath, &sb) == -1) {
1018
	} else if (stat(newpath, &sb) == -1) {
867
		if (rename(oldpath, newpath) == -1)
1019
		if (rename(oldpath, newpath) == -1) {
868
			status = errno_to_portable(errno);
1020
			status = errno_to_portable(errno);
869
		else
1021
#ifdef SFTP_LOGGING
1022
			log("(%d/%d/%s) File/Dir renaming failed - %s: %s -> %s.", ppid, cuid, CUNAME, status_to_logstr(status), oldpath, newpath);
1023
#endif
1024
		} else {
870
			status = SSH2_FX_OK;
1025
			status = SSH2_FX_OK;
1026
#ifdef SFTP_LOGGING
1027
			log("(%d/%d/%s) File/Dir renamed: %s -> %s.", ppid, cuid, CUNAME, oldpath, newpath);
1028
#endif
1029
		}
1030
#ifdef SFTP_LOGGING
1031
	} else {
1032
		log("(%d/%d/%s) File/Dir renaming failed - Target name exists: %s -> %s.", ppid, cuid, CUNAME, oldpath, newpath);
1033
#endif
871
	}
1034
	}
872
	send_status(id, status);
1035
	send_status(id, status);
873
	xfree(oldpath);
1036
	xfree(oldpath);
Lines 911-917 Link Here
911
	TRACE("symlink id %u old %s new %s", id, oldpath, newpath);
1074
	TRACE("symlink id %u old %s new %s", id, oldpath, newpath);
912
	/* this will fail if 'newpath' exists */
1075
	/* this will fail if 'newpath' exists */
913
	ret = symlink(oldpath, newpath);
1076
	ret = symlink(oldpath, newpath);
914
	status = (ret == -1) ? errno_to_portable(errno) : SSH2_FX_OK;
1077
	if (ret == -1) {
1078
		status = errno_to_portable(errno);
1079
#ifdef SFTP_LOGGING
1080
		log("(%d/%d/%s) Symbolic link creation failed - %s: %s -> %s.", ppid, cuid, CUNAME, status_to_logstr(status), oldpath, newpath);
1081
#endif
1082
	} else {
1083
		status = SSH2_FX_OK;
1084
#ifdef SFTP_LOGGING
1085
		log("(%d/%d/%s) Symbolic link created: %s -> %s.", ppid, cuid, CUNAME, oldpath, newpath);
1086
#endif
1087
	}
915
	send_status(id, status);
1088
	send_status(id, status);
916
	xfree(oldpath);
1089
	xfree(oldpath);
917
	xfree(newpath);
1090
	xfree(newpath);
Lines 925-930 Link Here
925
1098
926
	id = get_int();
1099
	id = get_int();
927
	request = get_string(NULL);
1100
	request = get_string(NULL);
1101
#ifdef SFTP_LOGGING
1102
	log("(%d/%d/%s) Extended operation attempted - Ignoring.", ppid, cuid, CUNAME);
1103
#endif
928
	send_status(id, SSH2_FX_OP_UNSUPPORTED);		/* MUST */
1104
	send_status(id, SSH2_FX_OP_UNSUPPORTED);		/* MUST */
929
	xfree(request);
1105
	xfree(request);
930
}
1106
}
Lines 947-952 Link Here
947
	msg_len = GET_32BIT(cp);
1123
	msg_len = GET_32BIT(cp);
948
	if (msg_len > 256 * 1024) {
1124
	if (msg_len > 256 * 1024) {
949
		error("bad message ");
1125
		error("bad message ");
1126
#ifdef SFTP_LOGGING
1127
		log("(%d/%d/%s) SFTP session closing (%s).", ppid, cuid, CUNAME, "Bad Message");
1128
#endif
950
		exit(11);
1129
		exit(11);
951
	}
1130
	}
952
	if (buf_len < msg_len + 4)
1131
	if (buf_len < msg_len + 4)
Lines 1041-1046 Link Here
1041
	__progname = ssh_get_progname(av[0]);
1220
	__progname = ssh_get_progname(av[0]);
1042
	handle_init();
1221
	handle_init();
1043
1222
1223
#ifdef SFTP_LOGGING
1224
	/* Initialize the username of the user running the process. */
1225
	cuid = getuid();
1226
	if ((upw = getpwuid(cuid)) == NULL) {
1227
		cuname = NULL;
1228
	} else {
1229
		cuname = xstrdup(upw->pw_name);
1230
	}
1231
1232
	/* Initialize the parent process ID. */
1233
	ppid = getppid();
1234
1235
	/* Initialize the logfile. */
1236
	log_init("sftp-server", SYSLOG_LEVEL_INFO, SYSLOG_FACILITY_AUTH, 0);
1237
1238
	/* Log session start. */
1239
	log("(%d/%d/%s) SFTP session started.", ppid, cuid, CUNAME);
1240
#endif
1241
1044
#ifdef DEBUG_SFTP_SERVER
1242
#ifdef DEBUG_SFTP_SERVER
1045
	log_init("sftp-server", SYSLOG_LEVEL_DEBUG1, SYSLOG_FACILITY_AUTH, 0);
1243
	log_init("sftp-server", SYSLOG_LEVEL_DEBUG1, SYSLOG_FACILITY_AUTH, 0);
1046
#endif
1244
#endif
Lines 1078-1083 Link Here
1078
		if (select(max+1, rset, wset, NULL, NULL) < 0) {
1276
		if (select(max+1, rset, wset, NULL, NULL) < 0) {
1079
			if (errno == EINTR)
1277
			if (errno == EINTR)
1080
				continue;
1278
				continue;
1279
#ifdef SFTP_LOGGING
1280
			log("(%d/%d/%s) SFTP session closing (%s).", ppid, cuid, CUNAME, "Select Error");
1281
#endif
1081
			exit(2);
1282
			exit(2);
1082
		}
1283
		}
1083
1284
Lines 1087-1095 Link Here
1087
			len = read(in, buf, sizeof buf);
1288
			len = read(in, buf, sizeof buf);
1088
			if (len == 0) {
1289
			if (len == 0) {
1089
				debug("read eof");
1290
				debug("read eof");
1291
#ifdef SFTP_LOGGING
1292
				log("(%d/%d/%s) SFTP session closing (%s).", ppid, cuid, CUNAME, "EOF");
1293
#endif
1090
				exit(0);
1294
				exit(0);
1091
			} else if (len < 0) {
1295
			} else if (len < 0) {
1092
				error("read error");
1296
				error("read error");
1297
#ifdef SFTP_LOGGING
1298
				log("(%d/%d/%s) SFTP session closing (%s).", ppid, cuid, CUNAME, "Read Error");
1299
#endif
1093
				exit(1);
1300
				exit(1);
1094
			} else {
1301
			} else {
1095
				buffer_append(&iqueue, buf, len);
1302
				buffer_append(&iqueue, buf, len);
Lines 1100-1105 Link Here
1100
			len = write(out, buffer_ptr(&oqueue), olen);
1307
			len = write(out, buffer_ptr(&oqueue), olen);
1101
			if (len < 0) {
1308
			if (len < 0) {
1102
				error("write error");
1309
				error("write error");
1310
#ifdef SFTP_LOGGING
1311
				log("(%d/%d/%s) SFTP session closing (%s).", ppid, cuid, CUNAME, "Write Error");
1312
#endif
1103
				exit(1);
1313
				exit(1);
1104
			} else {
1314
			} else {
1105
				buffer_consume(&oqueue, len);
1315
				buffer_consume(&oqueue, len);

Return to bug 474