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>
13 #include <afs/procmgmt.h>
20 #include <sys/types.h>
23 #include <WINNT/afsevent.h>
26 #include <afs/procmgmt.h>
29 #include <netinet/in.h>
43 #include <afs/cellconfig.h>
44 #include <afs/afsutil.h>
45 #include <afs/fileutil.h>
46 #include <afs/com_err.h>
48 #include <sys/statfs.h>
53 static int AddObject(char **expPath, char *dir);
54 static int PathInDirectory(char *dir, char *path);
55 int update_SendFile(int, struct rx_call *, struct stat *);
56 int update_SendDirInfo(char *, struct rx_call *, struct stat *,
59 struct afsconf_dir *cdir;
61 char *dirName[MAXENTRIES];
62 int dirLevel[MAXENTRIES];
65 static int Quit(char *);
69 #define ADDRSPERSITE 16 /* Same global is in rx/rx_user.c */
70 afs_uint32 SHostAddrs[ADDRSPERSITE];
72 /* check whether caller is authorized to manage RX statistics */
74 update_rxstat_userok(struct rx_call *call)
76 return afsconf_SuperUser(cdir, call, NULL);
80 * PathInDirectory() -- determine if path is in directory (or is directory)
83 PathInDirectory(char *dir, char *path)
87 char dirNorm[AFSDIR_PATH_MAX], pathNorm[AFSDIR_PATH_MAX];
90 /* case-insensitive comparison of normalized, same-flavor (short) paths */
93 status = GetShortPathName(dir, dirNorm, AFSDIR_PATH_MAX);
94 if (status == 0 || status > AFSDIR_PATH_MAX) {
95 /* can't convert path to short version; just use long version */
98 FilepathNormalize(dirNorm);
100 status = GetShortPathName(path, pathNorm, AFSDIR_PATH_MAX);
101 if (status == 0 || status > AFSDIR_PATH_MAX) {
102 /* can't convert path to short version; just use long version */
103 strcpy(pathNorm, path);
105 FilepathNormalize(pathNorm);
107 dirLen = strlen(dirNorm);
109 if (_strnicmp(dirNorm, pathNorm, dirLen) == 0) {
110 /* substrings match; path must match dir or be subdirectory */
111 if (pathNorm[dirLen] == '\0' || pathNorm[dirLen] == '/') {
116 /* case-sensitive comparison of normalized paths */
117 strcpy(dirNorm, dir);
118 FilepathNormalize(dirNorm);
120 strcpy(pathNorm, path);
121 FilepathNormalize(pathNorm);
123 dirLen = strlen(dirNorm);
125 if (strncmp(dirNorm, pathNorm, dirLen) == 0) {
126 /* substrings match; path must match dir or be subdirectory */
127 if (pathNorm[dirLen] == '\0' || pathNorm[dirLen] == '/') {
131 #endif /* AFS_NT40_ENV */
136 AuthOkay(struct rx_call *call, char *name)
143 /* Must be in 'UserList' to use */
144 if (!afsconf_SuperUser(cdir, call, NULL))
147 if (rx_SecurityClassOf(rx_ConnectionOf(call)) == 2) {
148 code = rxkad_GetServerInfo(call->conn, &level, 0, 0, 0, 0, 0);
155 for (i = 0; i < nDirs; i++) {
156 if (PathInDirectory(dirName[i], name)) {
157 if (dirLevel[i] > level)
160 /* keep searching in case there's a more restrictive subtree
161 * specified later. */
164 if (nDirs && !matches)
165 return 0; /* if dirs spec., name must match */
166 return 1; /* okay or no dirs */
172 /* this sucks but it works for now.
178 #include "AFS_component_version_number.c"
182 main(int argc, char *argv[])
184 struct rx_securityClass **securityClasses;
185 afs_int32 numClasses;
186 struct rx_service *service;
187 afs_uint32 host = htonl(INADDR_ANY);
191 rxkad_level newLevel;
195 * The following signal action for AIX is necessary so that in case of a
196 * crash (i.e. core is generated) we can include the user's data section
197 * in the core dump. Unfortunately, by default, only a partial core is
198 * generated which, in many cases, isn't too useful.
200 struct sigaction nsa;
202 sigemptyset(&nsa.sa_mask);
203 nsa.sa_handler = SIG_DFL;
204 nsa.sa_flags = SA_FULLDUMP;
205 sigaction(SIGABRT, &nsa, NULL);
206 sigaction(SIGSEGV, &nsa, NULL);
212 /* dummy signal call to force afsprocmgmt.dll to load on NT */
213 signal(SIGUSR1, SIG_DFL);
216 /* Initialize dirpaths */
217 if (!(initAFSDirPath() & AFSDIR_SERVER_PATHS_OK)) {
219 ReportErrorEventAlt(AFSEVT_SVR_NO_INSTALL_DIR, 0, argv[0], 0);
221 fprintf(stderr, "%s: Unable to obtain AFS server directory.\n",
228 if (argc == 1) /* no arguments */
231 /* support help flag */
232 if (strcmp("-help", argv[1]) == 0)
234 if (strcmp("help", argv[1]) == 0)
237 for (a = 1; a < argc; a++) {
238 if (argv[a][0] == '-') { /* parse options */
239 if (strcmp(argv[a], "-rxbind") == 0) {
244 lcstring(arg, argv[a], sizeof(arg));
245 newLevel = rxkad_StringToLevel(&argv[a][1]);
246 if (newLevel != -1) {
247 level = newLevel; /* set new level */
252 Quit("Usage: upserver [<directory>+] [-crypt <directory>+] [-clear <directory>+] [-auth <directory>+] [-rxbind] [-help]\n");
255 if (nDirs >= sizeof(dirName) / sizeof(dirName[0]))
256 Quit("Too many dirs");
257 dirlen = strlen(argv[a]);
258 if (AddObject(&dirName[nDirs], argv[a])) {
259 printf("%s: Unable to export dir %s. Skipping\n", whoami,
263 dirLevel[nDirs] = level; /* remember current level */
268 if (nDirs == 0) { /* Didn't find any directories to export */
269 printf("%s: No directories to export. Quitting\n", whoami);
273 cdir = afsconf_Open(AFSDIR_SERVER_ETC_DIRPATH);
275 fprintf(stderr, "Can't get server configuration info (%s)\n",
276 AFSDIR_SERVER_ETC_DIRPATH);
282 if (AFSDIR_SERVER_NETRESTRICT_FILEPATH ||
283 AFSDIR_SERVER_NETINFO_FILEPATH) {
285 ccode = parseNetFiles(SHostAddrs, NULL, NULL,
286 ADDRSPERSITE, reason,
287 AFSDIR_SERVER_NETINFO_FILEPATH,
288 AFSDIR_SERVER_NETRESTRICT_FILEPATH);
291 ccode = rx_getAllAddr(SHostAddrs, ADDRSPERSITE);
294 host = SHostAddrs[0];
297 /* Initialize Rx, telling it port number this server will use for its
299 if (rx_InitHost(host, htons(AFSCONF_UPDATEPORT)) < 0)
302 afsconf_BuildServerSecurityObjects(cdir, 0, &securityClasses, &numClasses);
304 if (securityClasses[2] == NULL)
305 Quit("rxkad_NewServerSecurityObject");
307 /* Instantiate a single UPDATE service. The rxgen-generated procedure
308 * which is called to decode requests is passed in here
309 * (UPDATE_ExecuteRequest). */
311 rx_NewServiceHost(host, 0, UPDATE_SERVICEID, "UPDATE", securityClasses,
312 numClasses, UPDATE_ExecuteRequest);
313 if (service == (struct rx_service *)0)
314 Quit("rx_NewService");
315 rx_SetMaxProcs(service, 2);
317 /* allow super users to manage RX statistics */
318 rx_SetRxStatUserOk(update_rxstat_userok);
320 rx_StartServer(1); /* Donate this process to the server process pool */
321 Quit("StartServer returned?");
325 /* fetch the file name and send it to the remote requester specified by call */
328 UPDATE_FetchFile(struct rx_call *call, char *name)
335 /* construct a local path from a canonical (wire-format) path */
336 if ((error = ConstructLocalPath(name, "/", &reqObject))) {
337 afs_com_err(whoami, error, "Unable to construct local path");
341 if (!AuthOkay(call, reqObject)) {
342 error = UPDATE_ERROR;
344 fd = open(reqObject, O_RDONLY, 0);
345 if (fd < 0 || fstat(fd, &status) < 0) {
346 printf("Failed to open %s\n", reqObject);
347 error = UPDATE_ERROR;
350 error = update_SendFile(fd, call, &status);
358 /* fetch dir info about directory name and send it to remote host associated
361 UPDATE_FetchInfo(struct rx_call *call, char *name)
367 /* construct a local path from a canonical (wire-format) path */
368 if ((error = ConstructLocalPath(name, "/", &reqObject))) {
369 afs_com_err(whoami, error, "Unable to construct local path");
373 if (!AuthOkay(call, reqObject)) {
374 error = UPDATE_ERROR;
376 /* we only need to stat the obj, not open it. */
377 if (stat(reqObject, &status) < 0) {
378 printf("Failed to open %s\n", reqObject);
379 error = UPDATE_ERROR;
381 if ((status.st_mode & S_IFMT) != S_IFDIR) {
382 printf(" file %s is not a directory \n", reqObject);
387 error = update_SendDirInfo(reqObject, call, &status, name);
396 fprintf(stderr, "%s", msg);
401 update_SendFile(int fd, struct rx_call *call, struct stat *status)
403 char *buffer = (char *)0;
405 afs_int32 length, tlen;
407 struct statfs tstatfs;
412 /* Unfortunately in AIX valuable fields such as st_blksize are gone from the stat structure!! */
413 fstatfs(fd, &tstatfs);
414 blockSize = tstatfs.f_bsize;
418 blockSize = status->st_blksize;
420 length = status->st_size;
421 buffer = (char *)malloc(blockSize);
423 printf("malloc failed\n");
426 tlen = htonl(length);
427 rx_Write(call, (char *)&tlen, sizeof(afs_int32)); /* send length on fetch */
428 while (!error && length) {
429 int nbytes = (length > blockSize ? blockSize : length);
430 nbytes = read(fd, buffer, nbytes);
432 fprintf(stderr, "File system read failed\n");
435 if (rx_Write(call, buffer, nbytes) != nbytes)
442 error = UPDATE_ERROR;
446 /* Enumerate dir (name) and write dir entry info into temp file.
449 update_SendDirInfo(char *name, /* Name of dir to enumerate */
450 struct rx_call *call, /* rx call */
451 struct stat *status, /* stat struct for dir */
452 char *origDir) /* orig name of dir before being localized */
458 char filename[MAXFNSIZE], dirInfoFile[MAXFNSIZE];
459 int fd, tfd, errcode, error, err;
462 dirp = opendir(name);
463 sprintf(dirInfoFile, "%s/upserver.tmp", gettmpdir());
464 stream = fopen(dirInfoFile, "w");
468 while ((dp = readdir(dirp))) {
469 strcpy(filename, name);
470 strcat(filename, "/");
471 strcat(filename, dp->d_name);
473 tfd = open(filename, O_RDONLY, 0);
474 if (tfd < 0 || fstat(tfd, &tstatus) < 0) {
475 printf("Failed to open %s\n", name);
476 error = UPDATE_ERROR;
479 if ((tstatus.st_mode & S_IFMT) != S_IFDIR) { /* not a directory */
480 char dirEntry[MAXFNSIZE];
482 strcpy(dirEntry, origDir);
483 strcat(dirEntry, "/");
484 strcat(dirEntry, dp->d_name);
486 fprintf(stream, "\"%s\" %u %u %u %u %u %u\n", dirEntry,
487 (unsigned int)tstatus.st_mtime,
488 (unsigned int)tstatus.st_size, tstatus.st_mode,
489 tstatus.st_uid, tstatus.st_gid,
490 (unsigned int)tstatus.st_atime);
496 printf("could not close file %s \n", filename);
497 error = UPDATE_ERROR;
508 error = UPDATE_ERROR;
512 fd = open(dirInfoFile, O_RDONLY, 0);
515 errcode = update_SendFile(fd, call, &tstatus);
518 error = UPDATE_ERROR;
527 /* AddObject() - Adds the object to the list of exported objects after
528 * converting to a local path.
530 * expPath : points to allocated storage in which the exportable path is
532 * dir : dir name passed in for export
536 AddObject(char **expPath, char *dir)
541 /* construct a local path from a canonical (wire-format) path */
542 if ((error = ConstructLocalPath(dir, "/", expPath))) {
543 afs_com_err(whoami, error, "Unable to construct local path");
547 /* stat the object */
548 error = stat(*expPath, &statbuf);
550 afs_com_err(whoami, error, ";Can't stat object.");
553 /* now check if the object has an exportable (file/dir) type */
554 if (!(statbuf.st_mode & S_IFDIR)) {
555 fprintf(stderr, "%s: Unacceptable object type for %s\n", whoami,