2 * Copyright 2000, International Business Machines Corporation and others.
5 * This software has been released under the terms of the IBM Public
6 * License. For details, see the LICENSE file in the top-level source
7 * directory or online at http://www.openafs.org/dl/license10.html
10 #include <afsconfig.h>
11 #include <afs/param.h>
20 #include <sys/types.h>
23 #include <WINNT/afsevent.h>
26 #include <afs/dirent.h>
27 #include <afs/procmgmt.h>
30 #include <netinet/in.h>
44 #include <afs/cellconfig.h>
45 #include <afs/afsutil.h>
46 #include <afs/fileutil.h>
47 #include <afs/com_err.h>
49 #include <sys/statfs.h>
54 extern int UPDATE_ExecuteRequest();
56 static int AddObject(char **expPath, char *dir);
57 static int PathInDirectory(char *dir, char *path);
59 struct afsconf_dir *cdir;
61 char *dirName[MAXENTRIES];
62 int dirLevel[MAXENTRIES];
69 #define ADDRSPERSITE 16 /* Same global is in rx/rx_user.c */
70 afs_uint32 SHostAddrs[ADDRSPERSITE];
72 /* check whether caller is authorized to manage RX statistics */
74 update_rxstat_userok(struct rx_call *call)
76 return afsconf_SuperUser(cdir, call, NULL);
80 * PathInDirectory() -- determine if path is in directory (or is directory)
83 PathInDirectory(char *dir, char *path)
87 char dirNorm[AFSDIR_PATH_MAX], pathNorm[AFSDIR_PATH_MAX];
90 /* case-insensitive comparison of normalized, same-flavor (short) paths */
93 status = GetShortPathName(dir, dirNorm, AFSDIR_PATH_MAX);
94 if (status == 0 || status > AFSDIR_PATH_MAX) {
95 /* can't convert path to short version; just use long version */
98 FilepathNormalize(dirNorm);
100 status = GetShortPathName(path, pathNorm, AFSDIR_PATH_MAX);
101 if (status == 0 || status > AFSDIR_PATH_MAX) {
102 /* can't convert path to short version; just use long version */
103 strcpy(pathNorm, path);
105 FilepathNormalize(pathNorm);
107 dirLen = strlen(dirNorm);
109 if (_strnicmp(dirNorm, pathNorm, dirLen) == 0) {
110 /* substrings match; path must match dir or be subdirectory */
111 if (pathNorm[dirLen] == '\0' || pathNorm[dirLen] == '/') {
116 /* case-sensitive comparison of normalized paths */
117 strcpy(dirNorm, dir);
118 FilepathNormalize(dirNorm);
120 strcpy(pathNorm, path);
121 FilepathNormalize(pathNorm);
123 dirLen = strlen(dirNorm);
125 if (strncmp(dirNorm, pathNorm, dirLen) == 0) {
126 /* substrings match; path must match dir or be subdirectory */
127 if (pathNorm[dirLen] == '\0' || pathNorm[dirLen] == '/') {
131 #endif /* AFS_NT40_ENV */
136 AuthOkay(struct rx_call *call, char *name)
143 /* Must be in 'UserList' to use */
144 if (!afsconf_SuperUser(cdir, call, NULL))
147 if (rx_SecurityClassOf(rx_ConnectionOf(call)) == 2) {
148 code = rxkad_GetServerInfo(call->conn, &level, 0, 0, 0, 0, 0);
155 for (i = 0; i < nDirs; i++) {
156 if (PathInDirectory(dirName[i], name)) {
157 if (dirLevel[i] > level)
160 /* keep searching in case there's a more restrictive subtree
161 * specified later. */
164 if (nDirs && !matches)
165 return 0; /* if dirs spec., name must match */
166 return 1; /* okay or no dirs */
172 /* this sucks but it works for now.
178 #include "AFS_component_version_number.c"
182 main(int argc, char *argv[])
184 struct rx_securityClass *securityObjects[3];
185 struct rx_service *service;
186 afs_uint32 host = htonl(INADDR_ANY);
190 rxkad_level newLevel;
194 * The following signal action for AIX is necessary so that in case of a
195 * crash (i.e. core is generated) we can include the user's data section
196 * in the core dump. Unfortunately, by default, only a partial core is
197 * generated which, in many cases, isn't too useful.
199 struct sigaction nsa;
201 sigemptyset(&nsa.sa_mask);
202 nsa.sa_handler = SIG_DFL;
203 nsa.sa_flags = SA_FULLDUMP;
204 sigaction(SIGABRT, &nsa, NULL);
205 sigaction(SIGSEGV, &nsa, NULL);
211 /* dummy signal call to force afsprocmgmt.dll to load on NT */
212 signal(SIGUSR1, SIG_DFL);
215 /* Initialize dirpaths */
216 if (!(initAFSDirPath() & AFSDIR_SERVER_PATHS_OK)) {
218 ReportErrorEventAlt(AFSEVT_SVR_NO_INSTALL_DIR, 0, argv[0], 0);
220 fprintf(stderr, "%s: Unable to obtain AFS server directory.\n",
227 if (argc == 1) /* no arguments */
230 /* support help flag */
231 if (strcmp("-help", argv[1]) == 0)
233 if (strcmp("help", argv[1]) == 0)
236 for (a = 1; a < argc; a++) {
237 if (argv[a][0] == '-') { /* parse options */
238 if (strcmp(argv[a], "-rxbind") == 0) {
243 lcstring(arg, argv[a], sizeof(arg));
244 newLevel = rxkad_StringToLevel(&argv[a][1]);
245 if (newLevel != -1) {
246 level = newLevel; /* set new level */
251 Quit("Usage: upserver [<directory>+] [-crypt <directory>+] [-clear <directory>+] [-auth <directory>+] [-rxbind] [-help]\n");
254 if (nDirs >= sizeof(dirName) / sizeof(dirName[0]))
255 Quit("Too many dirs");
256 dirlen = strlen(argv[a]);
257 if (AddObject(&dirName[nDirs], argv[a])) {
258 printf("%s: Unable to export dir %s. Skipping\n", whoami,
262 dirLevel[nDirs] = level; /* remember current level */
267 if (nDirs == 0) { /* Didn't find any directories to export */
268 printf("%s: No directories to export. Quitting\n", whoami);
272 cdir = afsconf_Open(AFSDIR_SERVER_ETC_DIRPATH);
274 fprintf(stderr, "Can't get server configuration info (%s)\n",
275 AFSDIR_SERVER_ETC_DIRPATH);
282 if (AFSDIR_SERVER_NETRESTRICT_FILEPATH ||
283 AFSDIR_SERVER_NETINFO_FILEPATH) {
285 ccode = parseNetFiles(SHostAddrs, NULL, NULL,
286 ADDRSPERSITE, reason,
287 AFSDIR_SERVER_NETINFO_FILEPATH,
288 AFSDIR_SERVER_NETRESTRICT_FILEPATH);
292 ccode = rx_getAllAddr(SHostAddrs, ADDRSPERSITE);
295 host = SHostAddrs[0];
298 /* Initialize Rx, telling it port number this server will use for its
300 if (rx_InitHost(host, htons(AFSCONF_UPDATEPORT)) < 0)
303 /* Create a single security object, in this case the null security object,
304 * for unauthenticated connections, which will be used to control security
305 * on connections made to this server. */
307 /* WHAT A JOKE! Let's use rxkad at least so we know who we're talking to,
308 * then sometimes require full encryption. */
310 /* rxnull and rxvab are no longer supported */
311 securityObjects[0] = rxnull_NewServerSecurityObject();
313 securityObjects[1] = (struct rx_securityClass *)0;
316 rxkad_NewServerSecurityObject(rxkad_clear, cdir, afsconf_GetKey, 0);
317 if (securityObjects[2] == (struct rx_securityClass *)0)
318 Quit("rxkad_NewServerSecurityObject");
320 /* Instantiate a single UPDATE service. The rxgen-generated procedure
321 * which is called to decode requests is passed in here
322 * (UPDATE_ExecuteRequest). */
324 rx_NewServiceHost(host, 0, UPDATE_SERVICEID, "UPDATE", securityObjects,
325 3, UPDATE_ExecuteRequest);
326 if (service == (struct rx_service *)0)
327 Quit("rx_NewService");
328 rx_SetMaxProcs(service, 2);
330 /* allow super users to manage RX statistics */
331 rx_SetRxStatUserOk(update_rxstat_userok);
333 rx_StartServer(1); /* Donate this process to the server process pool */
334 Quit("StartServer returned?");
338 /* fetch the file name and send it to the remote requester specified by call */
341 UPDATE_FetchFile(struct rx_call *call, char *name)
348 /* construct a local path from a canonical (wire-format) path */
349 if ((error = ConstructLocalPath(name, "/", &reqObject))) {
350 afs_com_err(whoami, error, "Unable to construct local path");
354 if (!AuthOkay(call, reqObject)) {
355 error = UPDATE_ERROR;
357 fd = open(reqObject, O_RDONLY, 0);
358 if (fd < 0 || fstat(fd, &status) < 0) {
359 printf("Failed to open %s\n", reqObject);
360 error = UPDATE_ERROR;
363 error = update_SendFile(fd, call, &status);
371 /* fetch dir info about directory name and send it to remote host associated
374 UPDATE_FetchInfo(struct rx_call *call, char *name)
380 /* construct a local path from a canonical (wire-format) path */
381 if ((error = ConstructLocalPath(name, "/", &reqObject))) {
382 afs_com_err(whoami, error, "Unable to construct local path");
386 if (!AuthOkay(call, reqObject)) {
387 error = UPDATE_ERROR;
389 /* we only need to stat the obj, not open it. */
390 if (stat(reqObject, &status) < 0) {
391 printf("Failed to open %s\n", reqObject);
392 error = UPDATE_ERROR;
394 if ((status.st_mode & S_IFMT) != S_IFDIR) {
395 printf(" file %s is not a directory \n", reqObject);
400 error = update_SendDirInfo(reqObject, call, &status, name);
410 fprintf(stderr, msg, a, b);
415 update_SendFile(register int fd, register struct rx_call *call, register struct stat *status)
417 char *buffer = (char *)0;
419 afs_int32 length, tlen;
421 struct statfs tstatfs;
426 /* Unfortunately in AIX valuable fields such as st_blksize are gone from the stat structure!! */
427 fstatfs(fd, &tstatfs);
428 blockSize = tstatfs.f_bsize;
432 blockSize = status->st_blksize;
434 length = status->st_size;
435 buffer = (char *)malloc(blockSize);
437 printf("malloc failed\n");
440 tlen = htonl(length);
441 rx_Write(call, &tlen, sizeof(afs_int32)); /* send length on fetch */
442 while (!error && length) {
443 register int nbytes = (length > blockSize ? blockSize : length);
444 nbytes = read(fd, buffer, nbytes);
446 fprintf(stderr, "File system read failed\n");
449 if (rx_Write(call, buffer, nbytes) != nbytes)
456 error = UPDATE_ERROR;
460 /* Enumerate dir (name) and write dir entry info into temp file.
463 update_SendDirInfo(char *name, /* Name of dir to enumerate */
464 register struct rx_call *call, /* rx call */
465 register struct stat *status, /* stat struct for dir */
466 char *origDir) /* orig name of dir before being localized */
472 char filename[MAXSIZE], dirInfoFile[MAXSIZE];
473 int fd, tfd, errcode, error, err;
476 dirp = opendir(name);
477 sprintf(dirInfoFile, "%s/upserver.tmp", gettmpdir());
478 stream = fopen(dirInfoFile, "w");
482 while ((dp = readdir(dirp))) {
483 strcpy(filename, name);
484 strcat(filename, "/");
485 strcat(filename, dp->d_name);
487 tfd = open(filename, O_RDONLY, 0);
488 if (tfd < 0 || fstat(tfd, &tstatus) < 0) {
489 printf("Failed to open %s\n", name);
490 error = UPDATE_ERROR;
493 if ((tstatus.st_mode & S_IFMT) != S_IFDIR) { /* not a directory */
494 char dirEntry[MAXSIZE];
496 strcpy(dirEntry, origDir);
497 strcat(dirEntry, "/");
498 strcat(dirEntry, dp->d_name);
500 fprintf(stream, "\"%s\" %u %u %u %u %u %u\n", dirEntry,
501 (unsigned int)tstatus.st_mtime,
502 (unsigned int)tstatus.st_size, tstatus.st_mode,
503 tstatus.st_uid, tstatus.st_gid,
504 (unsigned int)tstatus.st_atime);
510 printf("could not close file %s \n", filename);
511 error = UPDATE_ERROR;
522 error = UPDATE_ERROR;
526 fd = open(dirInfoFile, O_RDONLY, 0);
529 errcode = update_SendFile(fd, call, &tstatus);
532 error = UPDATE_ERROR;
541 /* AddObject() - Adds the object to the list of exported objects after
542 * converting to a local path.
544 * expPath : points to allocated storage in which the exportable path is
546 * dir : dir name passed in for export
550 AddObject(char **expPath, char *dir)
555 /* construct a local path from a canonical (wire-format) path */
556 if ((error = ConstructLocalPath(dir, "/", expPath))) {
557 afs_com_err(whoami, error, "Unable to construct local path");
561 /* stat the object */
562 error = stat(*expPath, &statbuf);
564 afs_com_err(whoami, error, ";Can't stat object.");
567 /* now check if the object has an exportable (file/dir) type */
568 if (!(statbuf.st_mode & S_IFDIR)) {
569 fprintf(stderr, "%s: Unacceptable object type for %s\n", whoami,