-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvcfs.c
332 lines (268 loc) · 8.7 KB
/
vcfs.c
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
#include <stdio.h>
#include <sys/types.h>
#include <sys/file.h>
#include <sys/socket.h>
#include <netdb.h>
#include <rpc/rpc.h>
#include <sys/time.h>
#include <signal.h>
#include <termios.h>
#include <unistd.h>
#include <pwd.h>
#include "nfsproto.h"
#include "vcfs.h"
#include "cvs_cmds.h"
#include "cvstool.h"
#include "utils.h"
#ifndef CVS_PASSWORD_FILE
#define CVS_PASSWORD_FILE ".cvspass"
#endif
/* Prints the usage of vcfsd */
void usage(char *msg);
/* Tries to get the password for this pserver from the cvs password
file (if it exists) */
char* get_cvs_passwd_from_file(char *user, char *hostname);
struct in_addr validhost;
void nfs_program_2();
extern void cvstool_program_1(struct svc_req *rqstp, register SVCXPRT *transp);
char *progname;
int main(int argc, char **argv)
{
int port;
struct hostent *hp;
struct sockaddr_in sin;
int svrsock;
SVCXPRT *tp;
char *pword = NULL;
register SVCXPRT *transp;
char *module;
char *root;
char *hostname;
char *user;
bool use_gzip = TRUE;
char *tag = NULL;
int opt;
bool check_cvspass = TRUE;
progname = argv[0];
port = VCFS_PORT;
if (argc < 5) {
usage(NULL);
exit(1);
}
/* Get command options */
opterr = 0;
while ((opt = getopt(argc, argv, "int:")) != -1)
{
switch (opt)
{
case 'n':
use_gzip = FALSE;
break;
case 't':
tag = optarg;
break;
case 'i':
check_cvspass = FALSE;
break;
default:
usage("Invalid option.");
exit(1);
}
}
/* Get the four required arguments */
if (optind + 4 > argc)
{
usage(NULL);
exit(1);
}
hostname = argv[optind++];
root = argv[optind++];
module = argv[optind++];
user = argv[optind];
if (check_cvspass)
{
/* Try to get the cvs password from the .cvspass file */
pword = get_cvs_passwd_from_file(user, hostname);
}
else
{
pword = NULL;
}
/* Couldn't find the password in their .cvspass file, so
* we need to ask the user for it. */
if (pword == NULL) {
/* Get the user's cvs password */
pword = getpass("Enter CVS password:");
}
/* Now register our nfs server on the localhost */
if ((hp = gethostbyname("localhost")) == NULL) {
fprintf(stderr, "Cannot resolve localhost.\n");
exit(1);
}
memset(&sin, 0, sizeof(sin));
sin.sin_family = AF_INET;
memcpy(&sin.sin_addr, hp->h_addr, hp->h_length);
validhost.s_addr = sin.sin_addr.s_addr;
sin.sin_port = htons(port);
if ((svrsock = socket(AF_INET,SOCK_DGRAM,0)) < 0) {
perror("error creating socket");
exit(1);
}
if (bind(svrsock,(struct sockaddr *)&sin,sizeof(sin)) != 0) {
perror("error binding to socket");
exit(1);
}
if ((tp = svcudp_create(svrsock)) == NULL) {
fprintf(stderr,"Unable to create UDP RPC Service\n");
exit(1);
}
/* register the service */
if (!svc_register(tp, NFS_PROGRAM, NFS_VERSION, nfs_program_2, IPPROTO_UDP)) {
fprintf(stderr,"Unable to register vcfsd NFS server\n");
exit(1);
}
pmap_unset(CVSTOOL_PROGRAM, CVSTOOL_VERSION);
transp = svcudp_create(RPC_ANYSOCK);
if (transp == NULL) {
fprintf (stderr, "cannot create udp service.");
exit(1);
}
if (!svc_register(transp, CVSTOOL_PROGRAM, CVSTOOL_VERSION, cvstool_program_1, IPPROTO_UDP)) {
fprintf (stderr, "unable to register (CVSTOOL_PROGRAM, CVSTOOL_VERSION, udp).");
exit(1);
}
transp = svctcp_create(RPC_ANYSOCK, 0, 0);
if (transp == NULL) {
fprintf (stderr, "cannot create tcp service.");
exit(1);
}
if (!svc_register(transp, CVSTOOL_PROGRAM, CVSTOOL_VERSION, cvstool_program_1, IPPROTO_TCP)) {
fprintf (stderr, "unable to register (CVSTOOL_PROGRAM, CVSTOOL_VERSION, tcp).");
exit(1);
}
UID = getuid();
GID = getgid();
cvs_init_session(hostname, root, module, user,
pword, VCFS_ROOT, use_gzip, tag);
if (cvs_pserver_connect() < 0) {
fprintf(stderr, "Authentication on CVS server failed\n");
exit(1);
}
if (!vcfs_build_project())
{
/* Error building the project */
fprintf(stderr, "Error mounting repository\n");
exit(1);
}
printf("CVS project %s successfully mounted\n", argv[3]);
svc_run();
exit(1);
}
/* Returns the password for this pserver from the cvs password file
* (if it exists), NULL otherwise
*/
char* get_cvs_passwd_from_file(char *user, char *hostname)
{
char* home = NULL; /* path to home dir*/
char* home_env = NULL; /* contents of home env */
struct passwd *pw; /* passwd struct - see man getpwuid for details*/
char* passfile = NULL; /* path to the password file */
FILE *fp; /* ptr to open cvspass file */
char *user_at_host; /* of form username@host */
int line_length;
long line = 0;
char *linebuf = NULL;
size_t linebuf_len;
char *passwd_chunk;
char *password_tmp;
char *password;
int i = 0;
/* First find their home directory */
if ((home_env = getenv("HOME")) != NULL) {
/* They had HOME set. */
home = home_env;
}
else if ((pw = (struct passwd *) getpwuid (getuid()))
&& pw->pw_dir) {
/* We got it from the passwd file */
home = strdup(pw->pw_dir);
}
else {
/* Couldn't find their home dir.
* We are outta luck */
DEBUG(DEBUG_M, "[get_cvs_passwd_from_file] Could not find home dir");
return NULL;
}
DEBUG( DEBUG_H, "[get_cvs_passwd_from_file] home dir: %s", home );
/* Make space for filename to password file - TODO: what is the +3? */
passfile = malloc( strlen(home) + strlen(CVS_PASSWORD_FILE) + 3);
strcpy(passfile, home); /* Copy the path to user's home */
strcat(passfile, "/"); /* append slash to home dir*/
strcat(passfile, CVS_PASSWORD_FILE); /* append cvs passwd file name*/
fp = fopen(passfile, "r"); /* open that file for reading */
if (fp == NULL) {
return NULL; /* Couldn't open the file */
}
/* Get mem for full name */
user_at_host = malloc( strlen(user) + strlen(hostname) + 3);
/* Form into 'user@hostname' */
strcat(user_at_host, user);
strcat(user_at_host, "@");
strcat(user_at_host, hostname);
/* We have their CVS passwd file. Let's check each line for an
appropriate entry */
while ((line_length = getline( &linebuf, &linebuf_len, fp)) >= 0) {
line++;
/* Search for the username and server in the line */
if ( (strstr(linebuf, user_at_host) != NULL) &&
/* Search for 'pserver' since that is the only method we support */
((strstr(linebuf, "pserver") != NULL)) &&
/* Search backword for first space char. */
((passwd_chunk = rindex(linebuf, ' ')) != NULL) &&
/* Then for the char A. (ex. 'Aencryptedpasswd') */
((passwd_chunk = rindex(linebuf, 'A')) != NULL) ) {
DEBUG(DEBUG_H, "[get_cvs_passwd_from_file] password line .cvspass: \n\t%s", linebuf);
/* Copy the chunk we found and trailing newline */
password_tmp = malloc( strlen(passwd_chunk) );
/* skip initial space char vvv and skip trailing \n vvvvv */
strncpy(password_tmp, passwd_chunk + 1, strlen(passwd_chunk)-2);
/* Unscrable passwd - the method is symetric */
password = scramble( password_tmp );
/* Shift the whole string one char to the left,
pushing the unwanted 'A' off the left end.
Safe, because s is null-terminated. Taken from
CVS client code */
for (i = 0; password[i]; i++)
password[i] = password[i + 1];
free(password_tmp); /* free malloc mem */
}
else {
/* Couldn't find or parse the passwd */
password = NULL;
}
}
/* Close the passwd file */
if ( fclose(fp) < 0 ) {
/* TODO: Should we warn the user we couldn't close their
passwd file?? */
}
/* release string memory */
free(home);
free(passfile);
free(user_at_host);
DEBUG(DEBUG_H, "[get_cvs_passwd_from_file] returning password: %s", password);
return password;
}
/* Prints the usage of vcfsd, with given message (if
provided). Otherwise prints the default message. */
void usage(char *msg)
{
if (msg != NULL) {
fprintf(stderr, "%s: %s\n", progname, msg);
}
fprintf(stderr, "Usage: %s [OPTION] HOSTNAME CVSROOT PROJECT USERNAME\n\n",
progname);
fprintf(stderr, "-n\tDon't gzip file contents\n");
fprintf(stderr, "-t TAG\tLoad the version of the repository specified by TAG, which is either a branch or tag name\n");
fprintf(stderr, "-i\tDon't look for password in .cvspass file\n");
}