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>
50 #include <afs/cellconfig.h>
51 #include <afs/afsutil.h>
52 #include <afs/fileutil.h>
54 #include <sys/statfs.h>
59 extern int UPDATE_ExecuteRequest();
61 static int AddObject(char **expPath, char *dir);
62 static int PathInDirectory(char *dir, char *path);
64 struct afsconf_dir *cdir;
66 char *dirName[MAXENTRIES];
67 int dirLevel[MAXENTRIES];
74 #define ADDRSPERSITE 16 /* Same global is in rx/rx_user.c */
75 afs_uint32 SHostAddrs[ADDRSPERSITE];
77 /* check whether caller is authorized to manage RX statistics */
79 update_rxstat_userok(struct rx_call *call)
81 return afsconf_SuperUser(cdir, call, NULL);
85 * PathInDirectory() -- determine if path is in directory (or is directory)
88 PathInDirectory(char *dir, char *path)
92 char dirNorm[AFSDIR_PATH_MAX], pathNorm[AFSDIR_PATH_MAX];
95 /* case-insensitive comparison of normalized, same-flavor (short) paths */
98 status = GetShortPathName(dir, dirNorm, AFSDIR_PATH_MAX);
99 if (status == 0 || status > AFSDIR_PATH_MAX) {
100 /* can't convert path to short version; just use long version */
101 strcpy(dirNorm, dir);
103 FilepathNormalize(dirNorm);
105 status = GetShortPathName(path, pathNorm, AFSDIR_PATH_MAX);
106 if (status == 0 || status > AFSDIR_PATH_MAX) {
107 /* can't convert path to short version; just use long version */
108 strcpy(pathNorm, path);
110 FilepathNormalize(pathNorm);
112 dirLen = strlen(dirNorm);
114 if (_strnicmp(dirNorm, pathNorm, dirLen) == 0) {
115 /* substrings match; path must match dir or be subdirectory */
116 if (pathNorm[dirLen] == '\0' || pathNorm[dirLen] == '/') {
121 /* case-sensitive comparison of normalized paths */
122 strcpy(dirNorm, dir);
123 FilepathNormalize(dirNorm);
125 strcpy(pathNorm, path);
126 FilepathNormalize(pathNorm);
128 dirLen = strlen(dirNorm);
130 if (strncmp(dirNorm, pathNorm, dirLen) == 0) {
131 /* substrings match; path must match dir or be subdirectory */
132 if (pathNorm[dirLen] == '\0' || pathNorm[dirLen] == '/') {
136 #endif /* AFS_NT40_ENV */
141 AuthOkay(struct rx_call *call, char *name)
148 /* Must be in 'UserList' to use */
149 if (!afsconf_SuperUser(cdir, call, NULL))
152 if (rx_SecurityClassOf(rx_ConnectionOf(call)) == 2) {
153 code = rxkad_GetServerInfo(call->conn, &level, 0, 0, 0, 0, 0);
160 for (i = 0; i < nDirs; i++) {
161 if (PathInDirectory(dirName[i], name)) {
162 if (dirLevel[i] > level)
165 /* keep searching in case there's a more restrictive subtree
166 * specified later. */
169 if (nDirs && !matches)
170 return 0; /* if dirs spec., name must match */
171 return 1; /* okay or no dirs */
177 /* this sucks but it works for now.
183 #include "AFS_component_version_number.c"
187 main(int argc, char *argv[])
189 struct rx_securityClass *securityObjects[3];
190 struct rx_service *service;
191 afs_uint32 host = htonl(INADDR_ANY);
195 rxkad_level newLevel;
199 * The following signal action for AIX is necessary so that in case of a
200 * crash (i.e. core is generated) we can include the user's data section
201 * in the core dump. Unfortunately, by default, only a partial core is
202 * generated which, in many cases, isn't too useful.
204 struct sigaction nsa;
206 sigemptyset(&nsa.sa_mask);
207 nsa.sa_handler = SIG_DFL;
208 nsa.sa_flags = SA_FULLDUMP;
209 sigaction(SIGABRT, &nsa, NULL);
210 sigaction(SIGSEGV, &nsa, NULL);
216 /* dummy signal call to force afsprocmgmt.dll to load on NT */
217 signal(SIGUSR1, SIG_DFL);
220 /* Initialize dirpaths */
221 if (!(initAFSDirPath() & AFSDIR_SERVER_PATHS_OK)) {
223 ReportErrorEventAlt(AFSEVT_SVR_NO_INSTALL_DIR, 0, argv[0], 0);
225 fprintf(stderr, "%s: Unable to obtain AFS server directory.\n",
232 if (argc == 1) /* no arguments */
235 /* support help flag */
236 if (strcmp("-help", argv[1]) == 0)
238 if (strcmp("help", argv[1]) == 0)
241 for (a = 1; a < argc; a++) {
242 if (argv[a][0] == '-') { /* parse options */
243 if (strcmp(argv[a], "-rxbind") == 0) {
248 lcstring(arg, argv[a], sizeof(arg));
249 newLevel = rxkad_StringToLevel(&argv[a][1]);
250 if (newLevel != -1) {
251 level = newLevel; /* set new level */
256 Quit("Usage: upserver [<directory>+] [-crypt <directory>+] [-clear <directory>+] [-auth <directory>+] [-rxbind] [-help]\n");
259 if (nDirs >= sizeof(dirName) / sizeof(dirName[0]))
260 Quit("Too many dirs");
261 dirlen = strlen(argv[a]);
262 if (AddObject(&dirName[nDirs], argv[a])) {
263 printf("%s: Unable to export dir %s. Skipping\n", whoami,
267 dirLevel[nDirs] = level; /* remember current level */
272 if (nDirs == 0) { /* Didn't find any directories to export */
273 printf("%s: No directories to export. Quitting\n", whoami);
277 cdir = afsconf_Open(AFSDIR_SERVER_ETC_DIRPATH);
279 fprintf(stderr, "Can't get server configuration info (%s)\n",
280 AFSDIR_SERVER_ETC_DIRPATH);
287 if (AFSDIR_SERVER_NETRESTRICT_FILEPATH ||
288 AFSDIR_SERVER_NETINFO_FILEPATH) {
290 ccode = parseNetFiles(SHostAddrs, NULL, NULL,
291 ADDRSPERSITE, reason,
292 AFSDIR_SERVER_NETINFO_FILEPATH,
293 AFSDIR_SERVER_NETRESTRICT_FILEPATH);
297 ccode = rx_getAllAddr(SHostAddrs, ADDRSPERSITE);
300 host = SHostAddrs[0];
303 /* Initialize Rx, telling it port number this server will use for its
305 if (rx_InitHost(host, htons(AFSCONF_UPDATEPORT)) < 0)
308 /* Create a single security object, in this case the null security object,
309 * for unauthenticated connections, which will be used to control security
310 * on connections made to this server. */
312 /* WHAT A JOKE! Let's use rxkad at least so we know who we're talking to,
313 * then sometimes require full encryption. */
315 /* rxnull and rxvab are no longer supported */
316 securityObjects[0] = rxnull_NewServerSecurityObject();
318 securityObjects[1] = (struct rx_securityClass *)0;
321 rxkad_NewServerSecurityObject(rxkad_clear, cdir, afsconf_GetKey, 0);
322 if (securityObjects[2] == (struct rx_securityClass *)0)
323 Quit("rxkad_NewServerSecurityObject");
325 /* Instantiate a single UPDATE service. The rxgen-generated procedure
326 * which is called to decode requests is passed in here
327 * (UPDATE_ExecuteRequest). */
329 rx_NewServiceHost(host, 0, UPDATE_SERVICEID, "UPDATE", securityObjects,
330 3, UPDATE_ExecuteRequest);
331 if (service == (struct rx_service *)0)
332 Quit("rx_NewService");
333 rx_SetMaxProcs(service, 2);
335 /* allow super users to manage RX statistics */
336 rx_SetRxStatUserOk(update_rxstat_userok);
338 rx_StartServer(1); /* Donate this process to the server process pool */
339 Quit("StartServer returned?");
343 /* fetch the file name and send it to the remote requester specified by call */
346 UPDATE_FetchFile(struct rx_call *call, char *name)
353 /* construct a local path from a canonical (wire-format) path */
354 if ((error = ConstructLocalPath(name, "/", &reqObject))) {
355 com_err(whoami, error, "Unable to construct local path");
359 if (!AuthOkay(call, reqObject)) {
360 error = UPDATE_ERROR;
362 fd = open(reqObject, O_RDONLY, 0);
363 if (fd < 0 || fstat(fd, &status) < 0) {
364 printf("Failed to open %s\n", reqObject);
365 error = UPDATE_ERROR;
368 error = update_SendFile(fd, call, &status);
376 /* fetch dir info about directory name and send it to remote host associated
379 UPDATE_FetchInfo(struct rx_call *call, char *name)
385 /* construct a local path from a canonical (wire-format) path */
386 if ((error = ConstructLocalPath(name, "/", &reqObject))) {
387 com_err(whoami, error, "Unable to construct local path");
391 if (!AuthOkay(call, reqObject)) {
392 error = UPDATE_ERROR;
394 /* we only need to stat the obj, not open it. */
395 if (stat(reqObject, &status) < 0) {
396 printf("Failed to open %s\n", reqObject);
397 error = UPDATE_ERROR;
399 if ((status.st_mode & S_IFMT) != S_IFDIR) {
400 printf(" file %s is not a directory \n", reqObject);
405 error = update_SendDirInfo(reqObject, call, &status, name);
415 fprintf(stderr, msg, a, b);
420 update_SendFile(register int fd, register struct rx_call *call, register struct stat *status)
422 char *buffer = (char *)0;
424 afs_int32 length, tlen;
426 struct statfs tstatfs;
431 /* Unfortunately in AIX valuable fields such as st_blksize are gone from the stat structure!! */
432 fstatfs(fd, &tstatfs);
433 blockSize = tstatfs.f_bsize;
437 blockSize = status->st_blksize;
439 length = status->st_size;
440 buffer = (char *)malloc(blockSize);
442 printf("malloc failed\n");
445 tlen = htonl(length);
446 rx_Write(call, &tlen, sizeof(afs_int32)); /* send length on fetch */
447 while (!error && length) {
448 register int nbytes = (length > blockSize ? blockSize : length);
449 nbytes = read(fd, buffer, nbytes);
451 fprintf(stderr, "File system read failed\n");
454 if (rx_Write(call, buffer, nbytes) != nbytes)
461 error = UPDATE_ERROR;
465 /* Enumerate dir (name) and write dir entry info into temp file.
468 update_SendDirInfo(char *name, /* Name of dir to enumerate */
469 register struct rx_call *call, /* rx call */
470 register struct stat *status, /* stat struct for dir */
471 char *origDir) /* orig name of dir before being localized */
477 char filename[MAXSIZE], dirInfoFile[MAXSIZE];
478 int fd, tfd, errcode, error, err;
481 dirp = opendir(name);
482 sprintf(dirInfoFile, "%s/upserver.tmp", gettmpdir());
483 stream = fopen(dirInfoFile, "w");
487 while ((dp = readdir(dirp))) {
488 strcpy(filename, name);
489 strcat(filename, "/");
490 strcat(filename, dp->d_name);
492 tfd = open(filename, O_RDONLY, 0);
493 if (tfd < 0 || fstat(tfd, &tstatus) < 0) {
494 printf("Failed to open %s\n", name);
495 error = UPDATE_ERROR;
498 if ((tstatus.st_mode & S_IFMT) != S_IFDIR) { /* not a directory */
499 char dirEntry[MAXSIZE];
501 strcpy(dirEntry, origDir);
502 strcat(dirEntry, "/");
503 strcat(dirEntry, dp->d_name);
505 fprintf(stream, "\"%s\" %u %u %u %u %u %u\n", dirEntry,
506 (unsigned int)tstatus.st_mtime,
507 (unsigned int)tstatus.st_size, tstatus.st_mode,
508 tstatus.st_uid, tstatus.st_gid,
509 (unsigned int)tstatus.st_atime);
515 printf("could not close file %s \n", filename);
516 error = UPDATE_ERROR;
527 error = UPDATE_ERROR;
531 fd = open(dirInfoFile, O_RDONLY, 0);
534 errcode = update_SendFile(fd, call, &tstatus);
537 error = UPDATE_ERROR;
546 /* AddObject() - Adds the object to the list of exported objects after
547 * converting to a local path.
549 * expPath : points to allocated storage in which the exportable path is
551 * dir : dir name passed in for export
555 AddObject(char **expPath, char *dir)
560 /* construct a local path from a canonical (wire-format) path */
561 if ((error = ConstructLocalPath(dir, "/", expPath))) {
562 com_err(whoami, error, "Unable to construct local path");
566 /* stat the object */
567 error = stat(*expPath, &statbuf);
569 com_err(whoami, error, ";Can't stat object.");
572 /* now check if the object has an exportable (file/dir) type */
573 if (!(statbuf.st_mode & S_IFDIR)) {
574 fprintf(stderr, "%s: Unacceptable object type for %s\n", whoami,