Deleted Added
full compact
rcp.c (104563) rcp.c (105269)
1/*
2 * Copyright (c) 1983, 1990, 1992, 1993
3 * The Regents of the University of California. All rights reserved.
4 * Copyright (c) 2002 Networks Associates Technology, Inc.
5 * All rights reserved.
6 *
7 * Portions of this software were developed for the FreeBSD Project by
8 * ThinkSec AS and NAI Labs, the Security Research Division of Network

--- 37 unchanged lines hidden (view full) ---

46
47#if 0
48#ifndef lint
49static char sccsid[] = "@(#)rcp.c 8.2 (Berkeley) 4/2/94";
50#endif /* not lint */
51#endif
52
53#include <sys/cdefs.h>
1/*
2 * Copyright (c) 1983, 1990, 1992, 1993
3 * The Regents of the University of California. All rights reserved.
4 * Copyright (c) 2002 Networks Associates Technology, Inc.
5 * All rights reserved.
6 *
7 * Portions of this software were developed for the FreeBSD Project by
8 * ThinkSec AS and NAI Labs, the Security Research Division of Network

--- 37 unchanged lines hidden (view full) ---

46
47#if 0
48#ifndef lint
49static char sccsid[] = "@(#)rcp.c 8.2 (Berkeley) 4/2/94";
50#endif /* not lint */
51#endif
52
53#include <sys/cdefs.h>
54__FBSDID("$FreeBSD: head/bin/rcp/rcp.c 104563 2002-10-06 11:14:21Z tjr $");
54__FBSDID("$FreeBSD: head/bin/rcp/rcp.c 105269 2002-10-16 16:10:46Z markm $");
55
56#include <sys/param.h>
57#include <sys/stat.h>
58#include <sys/time.h>
59#include <sys/socket.h>
60#include <netinet/in.h>
61#include <netinet/in_systm.h>
62#include <netinet/ip.h>

--- 13 unchanged lines hidden (view full) ---

76#include <stdio.h>
77#include <stdlib.h>
78#include <string.h>
79#include <string.h>
80#include <unistd.h>
81
82#include "extern.h"
83
55
56#include <sys/param.h>
57#include <sys/stat.h>
58#include <sys/time.h>
59#include <sys/socket.h>
60#include <netinet/in.h>
61#include <netinet/in_systm.h>
62#include <netinet/ip.h>

--- 13 unchanged lines hidden (view full) ---

76#include <stdio.h>
77#include <stdlib.h>
78#include <string.h>
79#include <string.h>
80#include <unistd.h>
81
82#include "extern.h"
83
84#ifdef KERBEROS
85#include <openssl/des.h>
86#include <krb.h>
87#include "bsd_locl.h"
88
89char dst_realm_buf[REALM_SZ];
90char *dest_realm = NULL;
91int use_kerberos = 1;
92CREDENTIALS cred;
93Key_schedule schedule;
94extern char *krb_realmofhost();
95#ifdef CRYPT
96int doencrypt = 0;
97#define OPTIONS "46dfKk:prtx"
98#else
99#define OPTIONS "46dfKk:prt"
100#endif
101#else
102#define OPTIONS "46dfprt"
84#define OPTIONS "46dfprt"
103#endif
104
105struct passwd *pwd;
106u_short port;
107uid_t userid;
108int errs, rem;
109int pflag, iamremote, iamrecursive, targetshouldbedirectory;
110int family = PF_UNSPEC;
111
112static int argc_copy;
113static char **argv_copy;
114
115#define CMDNEEDS 64
116char cmd[CMDNEEDS]; /* must hold "rcp -r -p -d\0" */
117
85
86struct passwd *pwd;
87u_short port;
88uid_t userid;
89int errs, rem;
90int pflag, iamremote, iamrecursive, targetshouldbedirectory;
91int family = PF_UNSPEC;
92
93static int argc_copy;
94static char **argv_copy;
95
96#define CMDNEEDS 64
97char cmd[CMDNEEDS]; /* must hold "rcp -r -p -d\0" */
98
118#ifdef KERBEROS
119int kerberos(char **, char *, char *, char *);
120void oldw(const char *, ...) __printflike(1, 2);
121#endif
122int response(void);
123void rsource(char *, struct stat *);
124void run_err(const char *, ...) __printflike(1, 2);
125void sink(int, char *[]);
126void source(int, char *[]);
127void tolocal(int, char *[]);
128void toremote(char *, int, char *[]);
129void usage(void);
130
131int
132main(int argc, char *argv[])
133{
134 struct servent *sp;
135 int ch, fflag, i, tflag;
136 char *targ, *shell;
99int response(void);
100void rsource(char *, struct stat *);
101void run_err(const char *, ...) __printflike(1, 2);
102void sink(int, char *[]);
103void source(int, char *[]);
104void tolocal(int, char *[]);
105void toremote(char *, int, char *[]);
106void usage(void);
107
108int
109main(int argc, char *argv[])
110{
111 struct servent *sp;
112 int ch, fflag, i, tflag;
113 char *targ, *shell;
137#ifdef KERBEROS
138 char *k;
139#endif
140
141 /*
142 * Prepare for execing ourselves.
143 */
144 argc_copy = argc + 1;
145 argv_copy = malloc((argc_copy + 1) * sizeof(*argv_copy));
146 if (argv_copy == NULL)
147 err(1, "malloc");

--- 12 unchanged lines hidden (view full) ---

160 case '4':
161 family = PF_INET;
162 break;
163
164 case '6':
165 family = PF_INET6;
166 break;
167
114
115 /*
116 * Prepare for execing ourselves.
117 */
118 argc_copy = argc + 1;
119 argv_copy = malloc((argc_copy + 1) * sizeof(*argv_copy));
120 if (argv_copy == NULL)
121 err(1, "malloc");

--- 12 unchanged lines hidden (view full) ---

134 case '4':
135 family = PF_INET;
136 break;
137
138 case '6':
139 family = PF_INET6;
140 break;
141
168 case 'K':
169#ifdef KERBEROS
170 use_kerberos = 0;
171#endif
172 break;
173#ifdef KERBEROS
174 case 'k':
175 dest_realm = dst_realm_buf;
176 (void)strncpy(dst_realm_buf, optarg, REALM_SZ - 1);
177 dst_realm_buf[REALM_SZ - 1] = '\0';
178 break;
179#ifdef CRYPT
180 case 'x':
181 doencrypt = 1;
182 /* des_set_key(cred.session, schedule); */
183 break;
184#endif
185#endif
186 case 'p':
187 pflag = 1;
188 break;
189 case 'r':
190 iamrecursive = 1;
191 break;
192 /* Server options. */
193 case 'd':

--- 9 unchanged lines hidden (view full) ---

203 break;
204 case '?':
205 default:
206 usage();
207 }
208 argc -= optind;
209 argv += optind;
210
142 case 'p':
143 pflag = 1;
144 break;
145 case 'r':
146 iamrecursive = 1;
147 break;
148 /* Server options. */
149 case 'd':

--- 9 unchanged lines hidden (view full) ---

159 break;
160 case '?':
161 default:
162 usage();
163 }
164 argc -= optind;
165 argv += optind;
166
211#ifdef KERBEROS
212 k = auth_getval("auth_list");
213 if (k && !strstr(k, "kerberos"))
214 use_kerberos = 0;
215 if (use_kerberos) {
216#ifdef CRYPT
217 shell = doencrypt ? "ekshell" : "kshell";
218#else
219 shell = "kshell";
220#endif
221 if ((sp = getservbyname(shell, "tcp")) == NULL) {
222 use_kerberos = 0;
223 oldw("can't get entry for %s/tcp service", shell);
224 sp = getservbyname(shell = "shell", "tcp");
225 }
226 } else
227 sp = getservbyname(shell = "shell", "tcp");
228#else
229 sp = getservbyname(shell = "shell", "tcp");
167 sp = getservbyname(shell = "shell", "tcp");
230#endif
231 if (sp == NULL)
232 errx(1, "%s/tcp: unknown service", shell);
233 port = sp->s_port;
234
235 if ((pwd = getpwuid(userid = getuid())) == NULL)
236 errx(1, "unknown user %d", (int)userid);
237
238 rem = STDIN_FILENO; /* XXX */

--- 13 unchanged lines hidden (view full) ---

252
253 if (argc < 2)
254 usage();
255 if (argc > 2)
256 targetshouldbedirectory = 1;
257
258 rem = -1;
259 /* Command to be executed on remote system using "rsh". */
168 if (sp == NULL)
169 errx(1, "%s/tcp: unknown service", shell);
170 port = sp->s_port;
171
172 if ((pwd = getpwuid(userid = getuid())) == NULL)
173 errx(1, "unknown user %d", (int)userid);
174
175 rem = STDIN_FILENO; /* XXX */

--- 13 unchanged lines hidden (view full) ---

189
190 if (argc < 2)
191 usage();
192 if (argc > 2)
193 targetshouldbedirectory = 1;
194
195 rem = -1;
196 /* Command to be executed on remote system using "rsh". */
260#ifdef KERBEROS
261 (void)snprintf(cmd, sizeof(cmd),
262 "rcp%s%s%s%s", iamrecursive ? " -r" : "",
263#ifdef CRYPT
264 (doencrypt && use_kerberos ? " -x" : ""),
265#else
266 "",
267#endif
268 pflag ? " -p" : "", targetshouldbedirectory ? " -d" : "");
269#else
270 (void)snprintf(cmd, sizeof(cmd), "rcp%s%s%s",
271 iamrecursive ? " -r" : "", pflag ? " -p" : "",
272 targetshouldbedirectory ? " -d" : "");
197 (void)snprintf(cmd, sizeof(cmd), "rcp%s%s%s",
198 iamrecursive ? " -r" : "", pflag ? " -p" : "",
199 targetshouldbedirectory ? " -d" : "");
273#endif
274
275 (void)signal(SIGPIPE, lostconn);
276
277 if ((targ = colon(argv[argc - 1]))) /* Dest is remote host. */
278 toremote(targ, argc, argv);
279 else {
280 tolocal(argc, argv); /* Dest is local host. */
281 if (targetshouldbedirectory)

--- 61 unchanged lines hidden (view full) ---

343 (void)free(bp);
344 } else { /* local to remote */
345 if (rem == -1) {
346 len = strlen(targ) + CMDNEEDS + 20;
347 if (!(bp = malloc(len)))
348 err(1, "malloc");
349 (void)snprintf(bp, len, "%s -t %s", cmd, targ);
350 host = thost;
200
201 (void)signal(SIGPIPE, lostconn);
202
203 if ((targ = colon(argv[argc - 1]))) /* Dest is remote host. */
204 toremote(targ, argc, argv);
205 else {
206 tolocal(argc, argv); /* Dest is local host. */
207 if (targetshouldbedirectory)

--- 61 unchanged lines hidden (view full) ---

269 (void)free(bp);
270 } else { /* local to remote */
271 if (rem == -1) {
272 len = strlen(targ) + CMDNEEDS + 20;
273 if (!(bp = malloc(len)))
274 err(1, "malloc");
275 (void)snprintf(bp, len, "%s -t %s", cmd, targ);
276 host = thost;
351#ifdef KERBEROS
352 if (use_kerberos)
353 rem = kerberos(&host, bp,
354 pwd->pw_name,
355 tuser ? tuser : pwd->pw_name);
356 else
357#endif
358 rem = rcmd_af(&host, port,
359 pwd->pw_name,
360 tuser ? tuser : pwd->pw_name,
361 bp, 0, family);
277 rem = rcmd_af(&host, port,
278 pwd->pw_name,
279 tuser ? tuser : pwd->pw_name,
280 bp, 0, family);
362 if (rem < 0)
363 exit(1);
364 if (family == PF_INET) {
365 tos = IPTOS_THROUGHPUT;
366 if (setsockopt(rem, IPPROTO_IP, IP_TOS,
367 &tos, sizeof(int)) < 0)
368 warn("TOS (ignored)");
369 }

--- 42 unchanged lines hidden (view full) ---

412 ++errs;
413 continue;
414 }
415 }
416 len = strlen(src) + CMDNEEDS + 20;
417 if ((bp = malloc(len)) == NULL)
418 err(1, "malloc");
419 (void)snprintf(bp, len, "%s -f %s", cmd, src);
281 if (rem < 0)
282 exit(1);
283 if (family == PF_INET) {
284 tos = IPTOS_THROUGHPUT;
285 if (setsockopt(rem, IPPROTO_IP, IP_TOS,
286 &tos, sizeof(int)) < 0)
287 warn("TOS (ignored)");
288 }

--- 42 unchanged lines hidden (view full) ---

331 ++errs;
332 continue;
333 }
334 }
335 len = strlen(src) + CMDNEEDS + 20;
336 if ((bp = malloc(len)) == NULL)
337 err(1, "malloc");
338 (void)snprintf(bp, len, "%s -f %s", cmd, src);
420 rem =
421#ifdef KERBEROS
422 use_kerberos ?
423 kerberos(&host, bp, pwd->pw_name, suser) :
424#endif
425 rcmd_af(&host, port, pwd->pw_name, suser, bp, 0,
339 rem = rcmd_af(&host, port, pwd->pw_name, suser, bp, 0,
426 family);
427 (void)free(bp);
428 if (rem < 0) {
429 ++errs;
430 continue;
431 }
432 (void)seteuid(userid);
433 if (family == PF_INET) {

--- 375 unchanged lines hidden (view full) ---

809 break;
810 }
811 }
812screwup:
813 run_err("protocol error: %s", why);
814 exit(1);
815}
816
340 family);
341 (void)free(bp);
342 if (rem < 0) {
343 ++errs;
344 continue;
345 }
346 (void)seteuid(userid);
347 if (family == PF_INET) {

--- 375 unchanged lines hidden (view full) ---

723 break;
724 }
725 }
726screwup:
727 run_err("protocol error: %s", why);
728 exit(1);
729}
730
817#ifdef KERBEROS
818int
731int
819kerberos(char **host, char *bp, char *locuser, char *user)
820{
821 if (use_kerberos) {
822 setuid(getuid());
823 rem = KSUCCESS;
824 errno = 0;
825 if (dest_realm == NULL)
826 dest_realm = krb_realmofhost(*host);
827 rem =
828#ifdef CRYPT
829 doencrypt ?
830 krcmd_mutual(host,
831 port, user, bp, 0, dest_realm, &cred, schedule) :
832#endif
833 krcmd(host, port, user, bp, 0, dest_realm);
834
835 if (rem < 0) {
836 if (errno == ECONNREFUSED)
837 oldw("remote host doesn't support Kerberos");
838 else if (errno == ENOENT)
839 oldw("can't provide Kerberos authentication data");
840 execv(_PATH_RCP, argv_copy);
841 err(1, "execv: %s", _PATH_RCP);
842 }
843 } else {
844#ifdef CRYPT
845 if (doencrypt)
846 errx(1,
847 "the -x option requires Kerberos authentication");
848#endif
849 rem = rcmd_af(host, port, locuser, user, bp, 0, family);
850 }
851 return (rem);
852}
853#endif /* KERBEROS */
854
855int
856response(void)
857{
858 char ch, *cp, resp, rbuf[BUFSIZ];
859
860 if (read(rem, &resp, sizeof(resp)) != sizeof(resp))
861 lostconn(0);
862
863 cp = rbuf;

--- 19 unchanged lines hidden (view full) ---

883 exit(1);
884 }
885 /* NOTREACHED */
886}
887
888void
889usage(void)
890{
732response(void)
733{
734 char ch, *cp, resp, rbuf[BUFSIZ];
735
736 if (read(rem, &resp, sizeof(resp)) != sizeof(resp))
737 lostconn(0);
738
739 cp = rbuf;

--- 19 unchanged lines hidden (view full) ---

759 exit(1);
760 }
761 /* NOTREACHED */
762}
763
764void
765usage(void)
766{
891#ifdef KERBEROS
892#ifdef CRYPT
893 (void)fprintf(stderr, "%s\n%s\n",
767 (void)fprintf(stderr, "%s\n%s\n",
894 "usage: rcp [-46Kpx] [-k realm] f1 f2",
895 " rcp [-46Kprx] [-k realm] f1 ... fn directory");
896#else
897 (void)fprintf(stderr, "%s\n%s\n",
898 "usage: rcp [-46Kp] [-k realm] f1 f2",
899 " rcp [-46Kpr] [-k realm] f1 ... fn directory");
900#endif
901#else
902 (void)fprintf(stderr, "%s\n%s\n",
903 "usage: rcp [-46p] f1 f2",
904 " rcp [-46pr] f1 ... fn directory");
768 "usage: rcp [-46p] f1 f2",
769 " rcp [-46pr] f1 ... fn directory");
905#endif
906 exit(1);
907}
908
909#include <stdarg.h>
910
770 exit(1);
771}
772
773#include <stdarg.h>
774
911#ifdef KERBEROS
912void
775void
913oldw(const char *fmt, ...)
914{
915 va_list ap;
916 va_start(ap, fmt);
917 (void)fprintf(stderr, "rcp: ");
918 (void)vfprintf(stderr, fmt, ap);
919 (void)fprintf(stderr, ", using standard rcp\n");
920 va_end(ap);
921}
922#endif
923
924void
925run_err(const char *fmt, ...)
926{
927 static FILE *fp;
928 va_list ap;
929
930 ++errs;
931 if (fp == NULL && !(fp = fdopen(rem, "w")))
932 return;

--- 16 unchanged lines hidden ---
776run_err(const char *fmt, ...)
777{
778 static FILE *fp;
779 va_list ap;
780
781 ++errs;
782 if (fp == NULL && !(fp = fdopen(rem, "w")))
783 return;

--- 16 unchanged lines hidden ---