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>
18 #include <sys/types.h>
21 #include <WINNT/afsevent.h>
24 #include <afs/dirent.h>
25 #include <afs/procmgmt.h>
28 #include <netinet/in.h>
42 #include <afs/cellconfig.h>
43 #include <afs/afsutil.h>
44 #include <afs/fileutil.h>
45 #include <afs/com_err.h>
47 #include <sys/statfs.h>
52 static int AddObject(char **expPath, char *dir);
53 static int PathInDirectory(char *dir, char *path);
54 int update_SendFile(int, struct rx_call *, struct stat *);
55 int update_SendDirInfo(char *, struct rx_call *, struct stat *,
58 struct afsconf_dir *cdir;
60 char *dirName[MAXENTRIES];
61 int dirLevel[MAXENTRIES];
64 static int Quit(char *);
68 #define ADDRSPERSITE 16 /* Same global is in rx/rx_user.c */
69 afs_uint32 SHostAddrs[ADDRSPERSITE];
71 /* check whether caller is authorized to manage RX statistics */
73 update_rxstat_userok(struct rx_call *call)
75 return afsconf_SuperUser(cdir, call, NULL);
79 * PathInDirectory() -- determine if path is in directory (or is directory)
82 PathInDirectory(char *dir, char *path)
86 char dirNorm[AFSDIR_PATH_MAX], pathNorm[AFSDIR_PATH_MAX];
89 /* case-insensitive comparison of normalized, same-flavor (short) paths */
92 status = GetShortPathName(dir, dirNorm, AFSDIR_PATH_MAX);
93 if (status == 0 || status > AFSDIR_PATH_MAX) {
94 /* can't convert path to short version; just use long version */
97 FilepathNormalize(dirNorm);
99 status = GetShortPathName(path, pathNorm, 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(pathNorm, path);
104 FilepathNormalize(pathNorm);
106 dirLen = strlen(dirNorm);
108 if (_strnicmp(dirNorm, pathNorm, dirLen) == 0) {
109 /* substrings match; path must match dir or be subdirectory */
110 if (pathNorm[dirLen] == '\0' || pathNorm[dirLen] == '/') {
115 /* case-sensitive comparison of normalized paths */
116 strcpy(dirNorm, dir);
117 FilepathNormalize(dirNorm);
119 strcpy(pathNorm, path);
120 FilepathNormalize(pathNorm);
122 dirLen = strlen(dirNorm);
124 if (strncmp(dirNorm, pathNorm, dirLen) == 0) {
125 /* substrings match; path must match dir or be subdirectory */
126 if (pathNorm[dirLen] == '\0' || pathNorm[dirLen] == '/') {
130 #endif /* AFS_NT40_ENV */
135 AuthOkay(struct rx_call *call, char *name)
142 /* Must be in 'UserList' to use */
143 if (!afsconf_SuperUser(cdir, call, NULL))
146 if (rx_SecurityClassOf(rx_ConnectionOf(call)) == 2) {
147 code = rxkad_GetServerInfo(call->conn, &level, 0, 0, 0, 0, 0);
154 for (i = 0; i < nDirs; i++) {
155 if (PathInDirectory(dirName[i], name)) {
156 if (dirLevel[i] > level)
159 /* keep searching in case there's a more restrictive subtree
160 * specified later. */
163 if (nDirs && !matches)
164 return 0; /* if dirs spec., name must match */
165 return 1; /* okay or no dirs */
171 /* this sucks but it works for now.
177 #include "AFS_component_version_number.c"
181 main(int argc, char *argv[])
183 struct rx_securityClass **securityClasses;
184 afs_int32 numClasses;
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);
281 if (AFSDIR_SERVER_NETRESTRICT_FILEPATH ||
282 AFSDIR_SERVER_NETINFO_FILEPATH) {
284 ccode = parseNetFiles(SHostAddrs, NULL, NULL,
285 ADDRSPERSITE, reason,
286 AFSDIR_SERVER_NETINFO_FILEPATH,
287 AFSDIR_SERVER_NETRESTRICT_FILEPATH);
290 ccode = rx_getAllAddr(SHostAddrs, ADDRSPERSITE);
293 host = SHostAddrs[0];
296 /* Initialize Rx, telling it port number this server will use for its
298 if (rx_InitHost(host, htons(AFSCONF_UPDATEPORT)) < 0)
301 afsconf_BuildServerSecurityObjects(cdir, 0, &securityClasses, &numClasses);
303 if (securityClasses[2] == NULL)
304 Quit("rxkad_NewServerSecurityObject");
306 /* Instantiate a single UPDATE service. The rxgen-generated procedure
307 * which is called to decode requests is passed in here
308 * (UPDATE_ExecuteRequest). */
310 rx_NewServiceHost(host, 0, UPDATE_SERVICEID, "UPDATE", securityClasses,
311 numClasses, UPDATE_ExecuteRequest);
312 if (service == (struct rx_service *)0)
313 Quit("rx_NewService");
314 rx_SetMaxProcs(service, 2);
316 /* allow super users to manage RX statistics */
317 rx_SetRxStatUserOk(update_rxstat_userok);
319 rx_StartServer(1); /* Donate this process to the server process pool */
320 Quit("StartServer returned?");
324 /* fetch the file name and send it to the remote requester specified by call */
327 UPDATE_FetchFile(struct rx_call *call, char *name)
334 /* construct a local path from a canonical (wire-format) path */
335 if ((error = ConstructLocalPath(name, "/", &reqObject))) {
336 afs_com_err(whoami, error, "Unable to construct local path");
340 if (!AuthOkay(call, reqObject)) {
341 error = UPDATE_ERROR;
343 fd = open(reqObject, O_RDONLY, 0);
344 if (fd < 0 || fstat(fd, &status) < 0) {
345 printf("Failed to open %s\n", reqObject);
346 error = UPDATE_ERROR;
349 error = update_SendFile(fd, call, &status);
357 /* fetch dir info about directory name and send it to remote host associated
360 UPDATE_FetchInfo(struct rx_call *call, char *name)
366 /* construct a local path from a canonical (wire-format) path */
367 if ((error = ConstructLocalPath(name, "/", &reqObject))) {
368 afs_com_err(whoami, error, "Unable to construct local path");
372 if (!AuthOkay(call, reqObject)) {
373 error = UPDATE_ERROR;
375 /* we only need to stat the obj, not open it. */
376 if (stat(reqObject, &status) < 0) {
377 printf("Failed to open %s\n", reqObject);
378 error = UPDATE_ERROR;
380 if ((status.st_mode & S_IFMT) != S_IFDIR) {
381 printf(" file %s is not a directory \n", reqObject);
386 error = update_SendDirInfo(reqObject, call, &status, name);
395 fprintf(stderr, "%s", msg);
400 update_SendFile(register int fd, register struct rx_call *call, register struct stat *status)
402 char *buffer = (char *)0;
404 afs_int32 length, tlen;
406 struct statfs tstatfs;
411 /* Unfortunately in AIX valuable fields such as st_blksize are gone from the stat structure!! */
412 fstatfs(fd, &tstatfs);
413 blockSize = tstatfs.f_bsize;
417 blockSize = status->st_blksize;
419 length = status->st_size;
420 buffer = (char *)malloc(blockSize);
422 printf("malloc failed\n");
425 tlen = htonl(length);
426 rx_Write(call, (char *)&tlen, sizeof(afs_int32)); /* send length on fetch */
427 while (!error && length) {
428 register int nbytes = (length > blockSize ? blockSize : length);
429 nbytes = read(fd, buffer, nbytes);
431 fprintf(stderr, "File system read failed\n");
434 if (rx_Write(call, buffer, nbytes) != nbytes)
441 error = UPDATE_ERROR;
445 /* Enumerate dir (name) and write dir entry info into temp file.
448 update_SendDirInfo(char *name, /* Name of dir to enumerate */
449 register struct rx_call *call, /* rx call */
450 register struct stat *status, /* stat struct for dir */
451 char *origDir) /* orig name of dir before being localized */
457 char filename[MAXFNSIZE], dirInfoFile[MAXFNSIZE];
458 int fd, tfd, errcode, error, err;
461 dirp = opendir(name);
462 sprintf(dirInfoFile, "%s/upserver.tmp", gettmpdir());
463 stream = fopen(dirInfoFile, "w");
467 while ((dp = readdir(dirp))) {
468 strcpy(filename, name);
469 strcat(filename, "/");
470 strcat(filename, dp->d_name);
472 tfd = open(filename, O_RDONLY, 0);
473 if (tfd < 0 || fstat(tfd, &tstatus) < 0) {
474 printf("Failed to open %s\n", name);
475 error = UPDATE_ERROR;
478 if ((tstatus.st_mode & S_IFMT) != S_IFDIR) { /* not a directory */
479 char dirEntry[MAXFNSIZE];
481 strcpy(dirEntry, origDir);
482 strcat(dirEntry, "/");
483 strcat(dirEntry, dp->d_name);
485 fprintf(stream, "\"%s\" %u %u %u %u %u %u\n", dirEntry,
486 (unsigned int)tstatus.st_mtime,
487 (unsigned int)tstatus.st_size, tstatus.st_mode,
488 tstatus.st_uid, tstatus.st_gid,
489 (unsigned int)tstatus.st_atime);
495 printf("could not close file %s \n", filename);
496 error = UPDATE_ERROR;
507 error = UPDATE_ERROR;
511 fd = open(dirInfoFile, O_RDONLY, 0);
514 errcode = update_SendFile(fd, call, &tstatus);
517 error = UPDATE_ERROR;
526 /* AddObject() - Adds the object to the list of exported objects after
527 * converting to a local path.
529 * expPath : points to allocated storage in which the exportable path is
531 * dir : dir name passed in for export
535 AddObject(char **expPath, char *dir)
540 /* construct a local path from a canonical (wire-format) path */
541 if ((error = ConstructLocalPath(dir, "/", expPath))) {
542 afs_com_err(whoami, error, "Unable to construct local path");
546 /* stat the object */
547 error = stat(*expPath, &statbuf);
549 afs_com_err(whoami, error, ";Can't stat object.");
552 /* now check if the object has an exportable (file/dir) type */
553 if (!(statbuf.st_mode & S_IFDIR)) {
554 fprintf(stderr, "%s: Unacceptable object type for %s\n", whoami,