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>
14 #include <afs/procmgmt.h>
19 #include <WINNT/afsevent.h>
23 #include <sys/statfs.h>
29 #include <afs/cellconfig.h>
30 #include <afs/afsutil.h>
31 #include <afs/fileutil.h>
32 #include <afs/com_err.h>
37 static int AddObject(char **expPath, char *dir);
38 static int PathInDirectory(char *dir, char *path);
39 int update_SendFile(int, struct rx_call *, struct stat *);
40 int update_SendDirInfo(char *, struct rx_call *, struct stat *,
43 struct afsconf_dir *cdir;
45 char *dirName[MAXENTRIES];
46 int dirLevel[MAXENTRIES];
49 static int Quit(char *);
53 #define ADDRSPERSITE 16 /* Same global is in rx/rx_user.c */
54 afs_uint32 SHostAddrs[ADDRSPERSITE];
56 /* check whether caller is authorized to manage RX statistics */
58 update_rxstat_userok(struct rx_call *call)
60 return afsconf_SuperUser(cdir, call, NULL);
64 * PathInDirectory() -- determine if path is in directory (or is directory)
67 PathInDirectory(char *dir, char *path)
71 char dirNorm[AFSDIR_PATH_MAX], pathNorm[AFSDIR_PATH_MAX];
74 /* case-insensitive comparison of normalized, same-flavor (short) paths */
77 status = GetShortPathName(dir, dirNorm, AFSDIR_PATH_MAX);
78 if (status == 0 || status > AFSDIR_PATH_MAX) {
79 /* can't convert path to short version; just use long version */
82 FilepathNormalize(dirNorm);
84 status = GetShortPathName(path, pathNorm, AFSDIR_PATH_MAX);
85 if (status == 0 || status > AFSDIR_PATH_MAX) {
86 /* can't convert path to short version; just use long version */
87 strcpy(pathNorm, path);
89 FilepathNormalize(pathNorm);
91 dirLen = strlen(dirNorm);
93 if (_strnicmp(dirNorm, pathNorm, dirLen) == 0) {
94 /* substrings match; path must match dir or be subdirectory */
95 if (pathNorm[dirLen] == '\0' || pathNorm[dirLen] == '/') {
100 /* case-sensitive comparison of normalized paths */
101 strcpy(dirNorm, dir);
102 FilepathNormalize(dirNorm);
104 strcpy(pathNorm, path);
105 FilepathNormalize(pathNorm);
107 dirLen = strlen(dirNorm);
109 if (strncmp(dirNorm, pathNorm, dirLen) == 0) {
110 /* substrings match; path must match dir or be subdirectory */
111 if (pathNorm[dirLen] == '\0' || pathNorm[dirLen] == '/') {
115 #endif /* AFS_NT40_ENV */
120 AuthOkay(struct rx_call *call, char *name)
127 /* Must be in 'UserList' to use */
128 if (!afsconf_SuperUser(cdir, call, NULL))
131 if (rx_SecurityClassOf(rx_ConnectionOf(call)) == RX_SECIDX_KAD) {
132 code = rxkad_GetServerInfo(rx_ConnectionOf(call), &level, 0, 0, 0, 0, 0);
139 for (i = 0; i < nDirs; i++) {
140 if (PathInDirectory(dirName[i], name)) {
141 if (dirLevel[i] > level)
144 /* keep searching in case there's a more restrictive subtree
145 * specified later. */
148 if (nDirs && !matches)
149 return 0; /* if dirs spec., name must match */
150 return 1; /* okay or no dirs */
156 /* this sucks but it works for now.
162 #include "AFS_component_version_number.c"
166 main(int argc, char *argv[])
168 struct rx_securityClass **securityClasses;
169 afs_int32 numClasses;
170 struct rx_service *service;
171 afs_uint32 host = htonl(INADDR_ANY);
175 rxkad_level newLevel;
179 * The following signal action for AIX is necessary so that in case of a
180 * crash (i.e. core is generated) we can include the user's data section
181 * in the core dump. Unfortunately, by default, only a partial core is
182 * generated which, in many cases, isn't too useful.
184 struct sigaction nsa;
186 sigemptyset(&nsa.sa_mask);
187 nsa.sa_handler = SIG_DFL;
188 nsa.sa_flags = SA_FULLDUMP;
189 sigaction(SIGABRT, &nsa, NULL);
190 sigaction(SIGSEGV, &nsa, NULL);
196 /* dummy signal call to force afsprocmgmt.dll to load on NT */
197 signal(SIGUSR1, SIG_DFL);
200 /* Initialize dirpaths */
201 if (!(initAFSDirPath() & AFSDIR_SERVER_PATHS_OK)) {
203 ReportErrorEventAlt(AFSEVT_SVR_NO_INSTALL_DIR, 0, argv[0], 0);
205 fprintf(stderr, "%s: Unable to obtain AFS server directory.\n",
212 if (argc == 1) /* no arguments */
215 /* support help flag */
216 if (strcmp("-help", argv[1]) == 0)
218 if (strcmp("help", argv[1]) == 0)
221 for (a = 1; a < argc; a++) {
222 if (argv[a][0] == '-') { /* parse options */
223 if (strcmp(argv[a], "-rxbind") == 0) {
228 lcstring(arg, argv[a], sizeof(arg));
229 newLevel = rxkad_StringToLevel(&argv[a][1]);
230 if (newLevel != -1) {
231 level = newLevel; /* set new level */
236 Quit("Usage: upserver [<directory>+] [-crypt <directory>+] [-clear <directory>+] [-auth <directory>+] [-rxbind] [-help]\n");
238 if (nDirs >= sizeof(dirName) / sizeof(dirName[0]))
239 Quit("Too many dirs");
240 if (AddObject(&dirName[nDirs], argv[a])) {
241 printf("%s: Unable to export dir %s. Skipping\n", whoami,
245 dirLevel[nDirs] = level; /* remember current level */
250 if (nDirs == 0) { /* Didn't find any directories to export */
251 printf("%s: No directories to export. Quitting\n", whoami);
255 cdir = afsconf_Open(AFSDIR_SERVER_ETC_DIRPATH);
257 fprintf(stderr, "Can't get server configuration info (%s)\n",
258 AFSDIR_SERVER_ETC_DIRPATH);
264 if (AFSDIR_SERVER_NETRESTRICT_FILEPATH ||
265 AFSDIR_SERVER_NETINFO_FILEPATH) {
267 ccode = afsconf_ParseNetFiles(SHostAddrs, NULL, NULL,
268 ADDRSPERSITE, reason,
269 AFSDIR_SERVER_NETINFO_FILEPATH,
270 AFSDIR_SERVER_NETRESTRICT_FILEPATH);
273 ccode = rx_getAllAddr(SHostAddrs, ADDRSPERSITE);
276 host = SHostAddrs[0];
279 /* Initialize Rx, telling it port number this server will use for its
281 if (rx_InitHost(host, htons(AFSCONF_UPDATEPORT)) < 0)
284 afsconf_BuildServerSecurityObjects(cdir, &securityClasses, &numClasses);
286 if (securityClasses[2] == NULL)
287 Quit("rxkad_NewServerSecurityObject");
289 /* Instantiate a single UPDATE service. The rxgen-generated procedure
290 * which is called to decode requests is passed in here
291 * (UPDATE_ExecuteRequest). */
293 rx_NewServiceHost(host, 0, UPDATE_SERVICEID, "UPDATE", securityClasses,
294 numClasses, UPDATE_ExecuteRequest);
295 if (service == (struct rx_service *)0)
296 Quit("rx_NewService");
297 rx_SetMaxProcs(service, 2);
299 /* allow super users to manage RX statistics */
300 rx_SetRxStatUserOk(update_rxstat_userok);
302 rx_StartServer(1); /* Donate this process to the server process pool */
303 Quit("StartServer returned?");
307 /* fetch the file name and send it to the remote requester specified by call */
310 UPDATE_FetchFile(struct rx_call *call, char *name)
317 /* construct a local path from a canonical (wire-format) path */
318 if ((error = ConstructLocalPath(name, "/", &reqObject))) {
319 afs_com_err(whoami, error, "Unable to construct local path");
323 if (!AuthOkay(call, reqObject)) {
324 error = UPDATE_ERROR;
326 fd = open(reqObject, O_RDONLY, 0);
327 if (fd < 0 || fstat(fd, &status) < 0) {
328 printf("Failed to open %s\n", reqObject);
329 error = UPDATE_ERROR;
332 error = update_SendFile(fd, call, &status);
340 /* fetch dir info about directory name and send it to remote host associated
343 UPDATE_FetchInfo(struct rx_call *call, char *name)
349 /* construct a local path from a canonical (wire-format) path */
350 if ((error = ConstructLocalPath(name, "/", &reqObject))) {
351 afs_com_err(whoami, error, "Unable to construct local path");
355 if (!AuthOkay(call, reqObject)) {
356 error = UPDATE_ERROR;
358 /* we only need to stat the obj, not open it. */
359 if (stat(reqObject, &status) < 0) {
360 printf("Failed to open %s\n", reqObject);
361 error = UPDATE_ERROR;
363 if ((status.st_mode & S_IFMT) != S_IFDIR) {
364 printf(" file %s is not a directory \n", reqObject);
369 error = update_SendDirInfo(reqObject, call, &status, name);
378 fprintf(stderr, "%s", msg);
383 update_SendFile(int fd, struct rx_call *call, struct stat *status)
385 char *buffer = (char *)0;
387 afs_int32 length, tlen;
389 struct statfs tstatfs;
394 /* Unfortunately in AIX valuable fields such as st_blksize are gone from the stat structure!! */
395 fstatfs(fd, &tstatfs);
396 blockSize = tstatfs.f_bsize;
400 blockSize = status->st_blksize;
402 length = status->st_size;
403 buffer = malloc(blockSize);
405 printf("malloc failed\n");
408 tlen = htonl(length);
409 rx_Write(call, (char *)&tlen, sizeof(afs_int32)); /* send length on fetch */
410 while (!error && length) {
411 int nbytes = (length > blockSize ? blockSize : length);
412 nbytes = read(fd, buffer, nbytes);
414 fprintf(stderr, "File system read failed\n");
417 if (rx_Write(call, buffer, nbytes) != nbytes)
424 error = UPDATE_ERROR;
428 /* Enumerate dir (name) and write dir entry info into temp file.
431 update_SendDirInfo(char *name, /* Name of dir to enumerate */
432 struct rx_call *call, /* rx call */
433 struct stat *status, /* stat struct for dir */
434 char *origDir) /* orig name of dir before being localized */
440 char filename[MAXFNSIZE], dirInfoFile[MAXFNSIZE];
441 int fd, tfd, errcode, error, err;
444 dirp = opendir(name);
445 sprintf(dirInfoFile, "%s/upserver.tmp", gettmpdir());
446 stream = fopen(dirInfoFile, "w");
450 while ((dp = readdir(dirp))) {
451 strcpy(filename, name);
452 strcat(filename, "/");
453 strcat(filename, dp->d_name);
455 tfd = open(filename, O_RDONLY, 0);
456 if (tfd < 0 || fstat(tfd, &tstatus) < 0) {
457 printf("Failed to open %s\n", name);
458 error = UPDATE_ERROR;
461 if ((tstatus.st_mode & S_IFMT) != S_IFDIR) { /* not a directory */
462 char dirEntry[MAXFNSIZE];
464 strcpy(dirEntry, origDir);
465 strcat(dirEntry, "/");
466 strcat(dirEntry, dp->d_name);
468 fprintf(stream, "\"%s\" %u %u %u %u %u %u\n", dirEntry,
469 (unsigned int)tstatus.st_mtime,
470 (unsigned int)tstatus.st_size, tstatus.st_mode,
471 tstatus.st_uid, tstatus.st_gid,
472 (unsigned int)tstatus.st_atime);
478 printf("could not close file %s \n", filename);
479 error = UPDATE_ERROR;
490 error = UPDATE_ERROR;
494 fd = open(dirInfoFile, O_RDONLY, 0);
497 errcode = update_SendFile(fd, call, &tstatus);
500 error = UPDATE_ERROR;
509 /* AddObject() - Adds the object to the list of exported objects after
510 * converting to a local path.
512 * expPath : points to allocated storage in which the exportable path is
514 * dir : dir name passed in for export
518 AddObject(char **expPath, char *dir)
523 /* construct a local path from a canonical (wire-format) path */
524 if ((error = ConstructLocalPath(dir, "/", expPath))) {
525 afs_com_err(whoami, error, "Unable to construct local path");
529 /* stat the object */
530 error = stat(*expPath, &statbuf);
532 afs_com_err(whoami, error, ";Can't stat object.");
535 /* now check if the object has an exportable (file/dir) type */
536 if (!(statbuf.st_mode & S_IFDIR)) {
537 fprintf(stderr, "%s: Unacceptable object type for %s\n", whoami,