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>
53 #include <afs/com_err.h>
55 #include <sys/statfs.h>
60 extern int UPDATE_ExecuteRequest();
62 static int AddObject(char **expPath, char *dir);
63 static int PathInDirectory(char *dir, char *path);
65 struct afsconf_dir *cdir;
67 char *dirName[MAXENTRIES];
68 int dirLevel[MAXENTRIES];
75 #define ADDRSPERSITE 16 /* Same global is in rx/rx_user.c */
76 afs_uint32 SHostAddrs[ADDRSPERSITE];
78 /* check whether caller is authorized to manage RX statistics */
80 update_rxstat_userok(struct rx_call *call)
82 return afsconf_SuperUser(cdir, call, NULL);
86 * PathInDirectory() -- determine if path is in directory (or is directory)
89 PathInDirectory(char *dir, char *path)
93 char dirNorm[AFSDIR_PATH_MAX], pathNorm[AFSDIR_PATH_MAX];
96 /* case-insensitive comparison of normalized, same-flavor (short) paths */
99 status = GetShortPathName(dir, dirNorm, AFSDIR_PATH_MAX);
100 if (status == 0 || status > AFSDIR_PATH_MAX) {
101 /* can't convert path to short version; just use long version */
102 strcpy(dirNorm, dir);
104 FilepathNormalize(dirNorm);
106 status = GetShortPathName(path, pathNorm, AFSDIR_PATH_MAX);
107 if (status == 0 || status > AFSDIR_PATH_MAX) {
108 /* can't convert path to short version; just use long version */
109 strcpy(pathNorm, path);
111 FilepathNormalize(pathNorm);
113 dirLen = strlen(dirNorm);
115 if (_strnicmp(dirNorm, pathNorm, dirLen) == 0) {
116 /* substrings match; path must match dir or be subdirectory */
117 if (pathNorm[dirLen] == '\0' || pathNorm[dirLen] == '/') {
122 /* case-sensitive comparison of normalized paths */
123 strcpy(dirNorm, dir);
124 FilepathNormalize(dirNorm);
126 strcpy(pathNorm, path);
127 FilepathNormalize(pathNorm);
129 dirLen = strlen(dirNorm);
131 if (strncmp(dirNorm, pathNorm, dirLen) == 0) {
132 /* substrings match; path must match dir or be subdirectory */
133 if (pathNorm[dirLen] == '\0' || pathNorm[dirLen] == '/') {
137 #endif /* AFS_NT40_ENV */
142 AuthOkay(struct rx_call *call, char *name)
149 /* Must be in 'UserList' to use */
150 if (!afsconf_SuperUser(cdir, call, NULL))
153 if (rx_SecurityClassOf(rx_ConnectionOf(call)) == 2) {
154 code = rxkad_GetServerInfo(call->conn, &level, 0, 0, 0, 0, 0);
161 for (i = 0; i < nDirs; i++) {
162 if (PathInDirectory(dirName[i], name)) {
163 if (dirLevel[i] > level)
166 /* keep searching in case there's a more restrictive subtree
167 * specified later. */
170 if (nDirs && !matches)
171 return 0; /* if dirs spec., name must match */
172 return 1; /* okay or no dirs */
178 /* this sucks but it works for now.
184 #include "AFS_component_version_number.c"
188 main(int argc, char *argv[])
190 struct rx_securityClass *securityObjects[3];
191 struct rx_service *service;
192 afs_uint32 host = htonl(INADDR_ANY);
196 rxkad_level newLevel;
200 * The following signal action for AIX is necessary so that in case of a
201 * crash (i.e. core is generated) we can include the user's data section
202 * in the core dump. Unfortunately, by default, only a partial core is
203 * generated which, in many cases, isn't too useful.
205 struct sigaction nsa;
207 sigemptyset(&nsa.sa_mask);
208 nsa.sa_handler = SIG_DFL;
209 nsa.sa_flags = SA_FULLDUMP;
210 sigaction(SIGABRT, &nsa, NULL);
211 sigaction(SIGSEGV, &nsa, NULL);
217 /* dummy signal call to force afsprocmgmt.dll to load on NT */
218 signal(SIGUSR1, SIG_DFL);
221 /* Initialize dirpaths */
222 if (!(initAFSDirPath() & AFSDIR_SERVER_PATHS_OK)) {
224 ReportErrorEventAlt(AFSEVT_SVR_NO_INSTALL_DIR, 0, argv[0], 0);
226 fprintf(stderr, "%s: Unable to obtain AFS server directory.\n",
233 if (argc == 1) /* no arguments */
236 /* support help flag */
237 if (strcmp("-help", argv[1]) == 0)
239 if (strcmp("help", argv[1]) == 0)
242 for (a = 1; a < argc; a++) {
243 if (argv[a][0] == '-') { /* parse options */
244 if (strcmp(argv[a], "-rxbind") == 0) {
249 lcstring(arg, argv[a], sizeof(arg));
250 newLevel = rxkad_StringToLevel(&argv[a][1]);
251 if (newLevel != -1) {
252 level = newLevel; /* set new level */
257 Quit("Usage: upserver [<directory>+] [-crypt <directory>+] [-clear <directory>+] [-auth <directory>+] [-rxbind] [-help]\n");
260 if (nDirs >= sizeof(dirName) / sizeof(dirName[0]))
261 Quit("Too many dirs");
262 dirlen = strlen(argv[a]);
263 if (AddObject(&dirName[nDirs], argv[a])) {
264 printf("%s: Unable to export dir %s. Skipping\n", whoami,
268 dirLevel[nDirs] = level; /* remember current level */
273 if (nDirs == 0) { /* Didn't find any directories to export */
274 printf("%s: No directories to export. Quitting\n", whoami);
278 cdir = afsconf_Open(AFSDIR_SERVER_ETC_DIRPATH);
280 fprintf(stderr, "Can't get server configuration info (%s)\n",
281 AFSDIR_SERVER_ETC_DIRPATH);
288 if (AFSDIR_SERVER_NETRESTRICT_FILEPATH ||
289 AFSDIR_SERVER_NETINFO_FILEPATH) {
291 ccode = parseNetFiles(SHostAddrs, NULL, NULL,
292 ADDRSPERSITE, reason,
293 AFSDIR_SERVER_NETINFO_FILEPATH,
294 AFSDIR_SERVER_NETRESTRICT_FILEPATH);
298 ccode = rx_getAllAddr(SHostAddrs, ADDRSPERSITE);
301 host = SHostAddrs[0];
304 /* Initialize Rx, telling it port number this server will use for its
306 if (rx_InitHost(host, htons(AFSCONF_UPDATEPORT)) < 0)
309 /* Create a single security object, in this case the null security object,
310 * for unauthenticated connections, which will be used to control security
311 * on connections made to this server. */
313 /* WHAT A JOKE! Let's use rxkad at least so we know who we're talking to,
314 * then sometimes require full encryption. */
316 /* rxnull and rxvab are no longer supported */
317 securityObjects[0] = rxnull_NewServerSecurityObject();
319 securityObjects[1] = (struct rx_securityClass *)0;
322 rxkad_NewServerSecurityObject(rxkad_clear, cdir, afsconf_GetKey, 0);
323 if (securityObjects[2] == (struct rx_securityClass *)0)
324 Quit("rxkad_NewServerSecurityObject");
326 /* Instantiate a single UPDATE service. The rxgen-generated procedure
327 * which is called to decode requests is passed in here
328 * (UPDATE_ExecuteRequest). */
330 rx_NewServiceHost(host, 0, UPDATE_SERVICEID, "UPDATE", securityObjects,
331 3, UPDATE_ExecuteRequest);
332 if (service == (struct rx_service *)0)
333 Quit("rx_NewService");
334 rx_SetMaxProcs(service, 2);
336 /* allow super users to manage RX statistics */
337 rx_SetRxStatUserOk(update_rxstat_userok);
339 rx_StartServer(1); /* Donate this process to the server process pool */
340 Quit("StartServer returned?");
344 /* fetch the file name and send it to the remote requester specified by call */
347 UPDATE_FetchFile(struct rx_call *call, char *name)
354 /* construct a local path from a canonical (wire-format) path */
355 if ((error = ConstructLocalPath(name, "/", &reqObject))) {
356 afs_com_err(whoami, error, "Unable to construct local path");
360 if (!AuthOkay(call, reqObject)) {
361 error = UPDATE_ERROR;
363 fd = open(reqObject, O_RDONLY, 0);
364 if (fd < 0 || fstat(fd, &status) < 0) {
365 printf("Failed to open %s\n", reqObject);
366 error = UPDATE_ERROR;
369 error = update_SendFile(fd, call, &status);
377 /* fetch dir info about directory name and send it to remote host associated
380 UPDATE_FetchInfo(struct rx_call *call, char *name)
386 /* construct a local path from a canonical (wire-format) path */
387 if ((error = ConstructLocalPath(name, "/", &reqObject))) {
388 afs_com_err(whoami, error, "Unable to construct local path");
392 if (!AuthOkay(call, reqObject)) {
393 error = UPDATE_ERROR;
395 /* we only need to stat the obj, not open it. */
396 if (stat(reqObject, &status) < 0) {
397 printf("Failed to open %s\n", reqObject);
398 error = UPDATE_ERROR;
400 if ((status.st_mode & S_IFMT) != S_IFDIR) {
401 printf(" file %s is not a directory \n", reqObject);
406 error = update_SendDirInfo(reqObject, call, &status, name);
416 fprintf(stderr, msg, a, b);
421 update_SendFile(register int fd, register struct rx_call *call, register struct stat *status)
423 char *buffer = (char *)0;
425 afs_int32 length, tlen;
427 struct statfs tstatfs;
432 /* Unfortunately in AIX valuable fields such as st_blksize are gone from the stat structure!! */
433 fstatfs(fd, &tstatfs);
434 blockSize = tstatfs.f_bsize;
438 blockSize = status->st_blksize;
440 length = status->st_size;
441 buffer = (char *)malloc(blockSize);
443 printf("malloc failed\n");
446 tlen = htonl(length);
447 rx_Write(call, &tlen, sizeof(afs_int32)); /* send length on fetch */
448 while (!error && length) {
449 register int nbytes = (length > blockSize ? blockSize : length);
450 nbytes = read(fd, buffer, nbytes);
452 fprintf(stderr, "File system read failed\n");
455 if (rx_Write(call, buffer, nbytes) != nbytes)
462 error = UPDATE_ERROR;
466 /* Enumerate dir (name) and write dir entry info into temp file.
469 update_SendDirInfo(char *name, /* Name of dir to enumerate */
470 register struct rx_call *call, /* rx call */
471 register struct stat *status, /* stat struct for dir */
472 char *origDir) /* orig name of dir before being localized */
478 char filename[MAXSIZE], dirInfoFile[MAXSIZE];
479 int fd, tfd, errcode, error, err;
482 dirp = opendir(name);
483 sprintf(dirInfoFile, "%s/upserver.tmp", gettmpdir());
484 stream = fopen(dirInfoFile, "w");
488 while ((dp = readdir(dirp))) {
489 strcpy(filename, name);
490 strcat(filename, "/");
491 strcat(filename, dp->d_name);
493 tfd = open(filename, O_RDONLY, 0);
494 if (tfd < 0 || fstat(tfd, &tstatus) < 0) {
495 printf("Failed to open %s\n", name);
496 error = UPDATE_ERROR;
499 if ((tstatus.st_mode & S_IFMT) != S_IFDIR) { /* not a directory */
500 char dirEntry[MAXSIZE];
502 strcpy(dirEntry, origDir);
503 strcat(dirEntry, "/");
504 strcat(dirEntry, dp->d_name);
506 fprintf(stream, "\"%s\" %u %u %u %u %u %u\n", dirEntry,
507 (unsigned int)tstatus.st_mtime,
508 (unsigned int)tstatus.st_size, tstatus.st_mode,
509 tstatus.st_uid, tstatus.st_gid,
510 (unsigned int)tstatus.st_atime);
516 printf("could not close file %s \n", filename);
517 error = UPDATE_ERROR;
528 error = UPDATE_ERROR;
532 fd = open(dirInfoFile, O_RDONLY, 0);
535 errcode = update_SendFile(fd, call, &tstatus);
538 error = UPDATE_ERROR;
547 /* AddObject() - Adds the object to the list of exported objects after
548 * converting to a local path.
550 * expPath : points to allocated storage in which the exportable path is
552 * dir : dir name passed in for export
556 AddObject(char **expPath, char *dir)
561 /* construct a local path from a canonical (wire-format) path */
562 if ((error = ConstructLocalPath(dir, "/", expPath))) {
563 afs_com_err(whoami, error, "Unable to construct local path");
567 /* stat the object */
568 error = stat(*expPath, &statbuf);
570 afs_com_err(whoami, error, ";Can't stat object.");
573 /* now check if the object has an exportable (file/dir) type */
574 if (!(statbuf.st_mode & S_IFDIR)) {
575 fprintf(stderr, "%s: Unacceptable object type for %s\n", whoami,