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);
286 if (AFSDIR_SERVER_NETRESTRICT_FILEPATH ||
287 AFSDIR_SERVER_NETINFO_FILEPATH) {
289 ccode = parseNetFiles(SHostAddrs, NULL, NULL,
290 ADDRSPERSITE, reason,
291 AFSDIR_SERVER_NETINFO_FILEPATH,
292 AFSDIR_SERVER_NETRESTRICT_FILEPATH);
294 ccode = rx_getAllAddr(SHostAddrs, ADDRSPERSITE);
297 host = SHostAddrs[0];
300 /* Initialize Rx, telling it port number this server will use for its
302 if (rx_InitHost(host, htons(AFSCONF_UPDATEPORT)) < 0)
305 /* Create a single security object, in this case the null security object,
306 * for unauthenticated connections, which will be used to control security
307 * on connections made to this server. */
309 /* WHAT A JOKE! Let's use rxkad at least so we know who we're talking to,
310 * then sometimes require full encryption. */
312 /* rxnull and rxvab are no longer supported */
313 securityObjects[0] = rxnull_NewServerSecurityObject();
315 securityObjects[1] = (struct rx_securityClass *)0;
318 rxkad_NewServerSecurityObject(rxkad_clear, cdir, afsconf_GetKey, 0);
319 if (securityObjects[2] == (struct rx_securityClass *)0)
320 Quit("rxkad_NewServerSecurityObject");
322 /* Instantiate a single UPDATE service. The rxgen-generated procedure
323 * which is called to decode requests is passed in here
324 * (UPDATE_ExecuteRequest). */
326 rx_NewServiceHost(host, 0, UPDATE_SERVICEID, "UPDATE", securityObjects,
327 3, UPDATE_ExecuteRequest);
328 if (service == (struct rx_service *)0)
329 Quit("rx_NewService");
330 rx_SetMaxProcs(service, 2);
332 /* allow super users to manage RX statistics */
333 rx_SetRxStatUserOk(update_rxstat_userok);
335 rx_StartServer(1); /* Donate this process to the server process pool */
336 Quit("StartServer returned?");
340 /* fetch the file name and send it to the remote requester specified by call */
343 UPDATE_FetchFile(struct rx_call *call, char *name)
350 /* construct a local path from a canonical (wire-format) path */
351 if ((error = ConstructLocalPath(name, "/", &reqObject))) {
352 com_err(whoami, error, "Unable to construct local path");
356 if (!AuthOkay(call, reqObject)) {
357 error = UPDATE_ERROR;
359 fd = open(reqObject, O_RDONLY, 0);
360 if (fd < 0 || fstat(fd, &status) < 0) {
361 printf("Failed to open %s\n", reqObject);
362 error = UPDATE_ERROR;
365 error = update_SendFile(fd, call, &status);
373 /* fetch dir info about directory name and send it to remote host associated
376 UPDATE_FetchInfo(struct rx_call *call, char *name)
382 /* construct a local path from a canonical (wire-format) path */
383 if ((error = ConstructLocalPath(name, "/", &reqObject))) {
384 com_err(whoami, error, "Unable to construct local path");
388 if (!AuthOkay(call, reqObject)) {
389 error = UPDATE_ERROR;
391 /* we only need to stat the obj, not open it. */
392 if (stat(reqObject, &status) < 0) {
393 printf("Failed to open %s\n", reqObject);
394 error = UPDATE_ERROR;
396 if ((status.st_mode & S_IFMT) != S_IFDIR) {
397 printf(" file %s is not a directory \n", reqObject);
402 error = update_SendDirInfo(reqObject, call, &status, name);
412 fprintf(stderr, msg, a, b);
417 update_SendFile(register int fd, register struct rx_call *call, register struct stat *status)
419 char *buffer = (char *)0;
421 afs_int32 length, tlen;
423 struct statfs tstatfs;
428 /* Unfortunately in AIX valuable fields such as st_blksize are gone from the stat structure!! */
429 fstatfs(fd, &tstatfs);
430 blockSize = tstatfs.f_bsize;
434 blockSize = status->st_blksize;
436 length = status->st_size;
437 buffer = (char *)malloc(blockSize);
439 printf("malloc failed\n");
442 tlen = htonl(length);
443 rx_Write(call, &tlen, sizeof(afs_int32)); /* send length on fetch */
444 while (!error && length) {
445 register int nbytes = (length > blockSize ? blockSize : length);
446 nbytes = read(fd, buffer, nbytes);
448 fprintf(stderr, "File system read failed\n");
451 if (rx_Write(call, buffer, nbytes) != nbytes)
458 error = UPDATE_ERROR;
462 /* Enumerate dir (name) and write dir entry info into temp file.
465 update_SendDirInfo(char *name, /* Name of dir to enumerate */
466 register struct rx_call *call, /* rx call */
467 register struct stat *status, /* stat struct for dir */
468 char *origDir) /* orig name of dir before being localized */
474 char filename[MAXSIZE], dirInfoFile[MAXSIZE];
475 int fd, tfd, errcode, error, err;
478 dirp = opendir(name);
479 sprintf(dirInfoFile, "%s/upserver.tmp", gettmpdir());
480 stream = fopen(dirInfoFile, "w");
484 while ((dp = readdir(dirp))) {
485 strcpy(filename, name);
486 strcat(filename, "/");
487 strcat(filename, dp->d_name);
489 tfd = open(filename, O_RDONLY, 0);
490 if (tfd < 0 || fstat(tfd, &tstatus) < 0) {
491 printf("Failed to open %s\n", name);
492 error = UPDATE_ERROR;
495 if ((tstatus.st_mode & S_IFMT) != S_IFDIR) { /* not a directory */
496 char dirEntry[MAXSIZE];
498 strcpy(dirEntry, origDir);
499 strcat(dirEntry, "/");
500 strcat(dirEntry, dp->d_name);
502 fprintf(stream, "\"%s\" %u %u %u %u %u %u\n", dirEntry,
503 (unsigned int)tstatus.st_mtime,
504 (unsigned int)tstatus.st_size, tstatus.st_mode,
505 tstatus.st_uid, tstatus.st_gid,
506 (unsigned int)tstatus.st_atime);
512 printf("could not close file %s \n", filename);
513 error = UPDATE_ERROR;
524 error = UPDATE_ERROR;
528 fd = open(dirInfoFile, O_RDONLY, 0);
531 errcode = update_SendFile(fd, call, &tstatus);
534 error = UPDATE_ERROR;
543 /* AddObject() - Adds the object to the list of exported objects after
544 * converting to a local path.
546 * expPath : points to allocated storage in which the exportable path is
548 * dir : dir name passed in for export
552 AddObject(char **expPath, char *dir)
557 /* construct a local path from a canonical (wire-format) path */
558 if ((error = ConstructLocalPath(dir, "/", expPath))) {
559 com_err(whoami, error, "Unable to construct local path");
563 /* stat the object */
564 error = stat(*expPath, &statbuf);
566 com_err(whoami, error, ";Can't stat object.");
569 /* now check if the object has an exportable (file/dir) type */
570 if (!(statbuf.st_mode & S_IFDIR)) {
571 fprintf(stderr, "%s: Unacceptable object type for %s\n", whoami,