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>
18 #include <WINNT/afsevent.h>
22 #include <sys/statfs.h>
28 #include <afs/cellconfig.h>
29 #include <afs/afsutil.h>
30 #include <afs/fileutil.h>
31 #include <afs/com_err.h>
36 static int AddObject(char **expPath, char *dir);
37 static int PathInDirectory(char *dir, char *path);
38 int update_SendFile(int, struct rx_call *, struct stat *);
39 int update_SendDirInfo(char *, struct rx_call *, struct stat *,
42 struct afsconf_dir *cdir;
44 char *dirName[MAXENTRIES];
45 int dirLevel[MAXENTRIES];
48 static int Quit(char *);
52 #define ADDRSPERSITE 16 /* Same global is in rx/rx_user.c */
53 afs_uint32 SHostAddrs[ADDRSPERSITE];
55 /* check whether caller is authorized to manage RX statistics */
57 update_rxstat_userok(struct rx_call *call)
59 return afsconf_SuperUser(cdir, call, NULL);
63 * PathInDirectory() -- determine if path is in directory (or is directory)
66 PathInDirectory(char *dir, char *path)
70 char dirNorm[AFSDIR_PATH_MAX], pathNorm[AFSDIR_PATH_MAX];
73 /* case-insensitive comparison of normalized, same-flavor (short) paths */
76 status = GetShortPathName(dir, dirNorm, AFSDIR_PATH_MAX);
77 if (status == 0 || status > AFSDIR_PATH_MAX) {
78 /* can't convert path to short version; just use long version */
81 FilepathNormalize(dirNorm);
83 status = GetShortPathName(path, pathNorm, AFSDIR_PATH_MAX);
84 if (status == 0 || status > AFSDIR_PATH_MAX) {
85 /* can't convert path to short version; just use long version */
86 strcpy(pathNorm, path);
88 FilepathNormalize(pathNorm);
90 dirLen = strlen(dirNorm);
92 if (_strnicmp(dirNorm, pathNorm, dirLen) == 0) {
93 /* substrings match; path must match dir or be subdirectory */
94 if (pathNorm[dirLen] == '\0' || pathNorm[dirLen] == '/') {
99 /* case-sensitive comparison of normalized paths */
100 strcpy(dirNorm, dir);
101 FilepathNormalize(dirNorm);
103 strcpy(pathNorm, path);
104 FilepathNormalize(pathNorm);
106 dirLen = strlen(dirNorm);
108 if (strncmp(dirNorm, pathNorm, dirLen) == 0) {
109 /* substrings match; path must match dir or be subdirectory */
110 if (pathNorm[dirLen] == '\0' || pathNorm[dirLen] == '/') {
114 #endif /* AFS_NT40_ENV */
119 AuthOkay(struct rx_call *call, char *name)
126 /* Must be in 'UserList' to use */
127 if (!afsconf_SuperUser(cdir, call, NULL))
130 if (rx_SecurityClassOf(rx_ConnectionOf(call)) == 2) {
131 code = rxkad_GetServerInfo(call->conn, &level, 0, 0, 0, 0, 0);
138 for (i = 0; i < nDirs; i++) {
139 if (PathInDirectory(dirName[i], name)) {
140 if (dirLevel[i] > level)
143 /* keep searching in case there's a more restrictive subtree
144 * specified later. */
147 if (nDirs && !matches)
148 return 0; /* if dirs spec., name must match */
149 return 1; /* okay or no dirs */
155 /* this sucks but it works for now.
161 #include "AFS_component_version_number.c"
165 main(int argc, char *argv[])
167 struct rx_securityClass **securityClasses;
168 afs_int32 numClasses;
169 struct rx_service *service;
170 afs_uint32 host = htonl(INADDR_ANY);
174 rxkad_level newLevel;
178 * The following signal action for AIX is necessary so that in case of a
179 * crash (i.e. core is generated) we can include the user's data section
180 * in the core dump. Unfortunately, by default, only a partial core is
181 * generated which, in many cases, isn't too useful.
183 struct sigaction nsa;
185 sigemptyset(&nsa.sa_mask);
186 nsa.sa_handler = SIG_DFL;
187 nsa.sa_flags = SA_FULLDUMP;
188 sigaction(SIGABRT, &nsa, NULL);
189 sigaction(SIGSEGV, &nsa, NULL);
195 /* dummy signal call to force afsprocmgmt.dll to load on NT */
196 signal(SIGUSR1, SIG_DFL);
199 /* Initialize dirpaths */
200 if (!(initAFSDirPath() & AFSDIR_SERVER_PATHS_OK)) {
202 ReportErrorEventAlt(AFSEVT_SVR_NO_INSTALL_DIR, 0, argv[0], 0);
204 fprintf(stderr, "%s: Unable to obtain AFS server directory.\n",
211 if (argc == 1) /* no arguments */
214 /* support help flag */
215 if (strcmp("-help", argv[1]) == 0)
217 if (strcmp("help", argv[1]) == 0)
220 for (a = 1; a < argc; a++) {
221 if (argv[a][0] == '-') { /* parse options */
222 if (strcmp(argv[a], "-rxbind") == 0) {
227 lcstring(arg, argv[a], sizeof(arg));
228 newLevel = rxkad_StringToLevel(&argv[a][1]);
229 if (newLevel != -1) {
230 level = newLevel; /* set new level */
235 Quit("Usage: upserver [<directory>+] [-crypt <directory>+] [-clear <directory>+] [-auth <directory>+] [-rxbind] [-help]\n");
237 if (nDirs >= sizeof(dirName) / sizeof(dirName[0]))
238 Quit("Too many dirs");
239 if (AddObject(&dirName[nDirs], argv[a])) {
240 printf("%s: Unable to export dir %s. Skipping\n", whoami,
244 dirLevel[nDirs] = level; /* remember current level */
249 if (nDirs == 0) { /* Didn't find any directories to export */
250 printf("%s: No directories to export. Quitting\n", whoami);
254 cdir = afsconf_Open(AFSDIR_SERVER_ETC_DIRPATH);
256 fprintf(stderr, "Can't get server configuration info (%s)\n",
257 AFSDIR_SERVER_ETC_DIRPATH);
263 if (AFSDIR_SERVER_NETRESTRICT_FILEPATH ||
264 AFSDIR_SERVER_NETINFO_FILEPATH) {
266 ccode = parseNetFiles(SHostAddrs, NULL, NULL,
267 ADDRSPERSITE, reason,
268 AFSDIR_SERVER_NETINFO_FILEPATH,
269 AFSDIR_SERVER_NETRESTRICT_FILEPATH);
272 ccode = rx_getAllAddr(SHostAddrs, ADDRSPERSITE);
275 host = SHostAddrs[0];
278 /* Initialize Rx, telling it port number this server will use for its
280 if (rx_InitHost(host, htons(AFSCONF_UPDATEPORT)) < 0)
283 afsconf_BuildServerSecurityObjects(cdir, &securityClasses, &numClasses);
285 if (securityClasses[2] == NULL)
286 Quit("rxkad_NewServerSecurityObject");
288 /* Instantiate a single UPDATE service. The rxgen-generated procedure
289 * which is called to decode requests is passed in here
290 * (UPDATE_ExecuteRequest). */
292 rx_NewServiceHost(host, 0, UPDATE_SERVICEID, "UPDATE", securityClasses,
293 numClasses, UPDATE_ExecuteRequest);
294 if (service == (struct rx_service *)0)
295 Quit("rx_NewService");
296 rx_SetMaxProcs(service, 2);
298 /* allow super users to manage RX statistics */
299 rx_SetRxStatUserOk(update_rxstat_userok);
301 rx_StartServer(1); /* Donate this process to the server process pool */
302 Quit("StartServer returned?");
306 /* fetch the file name and send it to the remote requester specified by call */
309 UPDATE_FetchFile(struct rx_call *call, char *name)
316 /* construct a local path from a canonical (wire-format) path */
317 if ((error = ConstructLocalPath(name, "/", &reqObject))) {
318 afs_com_err(whoami, error, "Unable to construct local path");
322 if (!AuthOkay(call, reqObject)) {
323 error = UPDATE_ERROR;
325 fd = open(reqObject, O_RDONLY, 0);
326 if (fd < 0 || fstat(fd, &status) < 0) {
327 printf("Failed to open %s\n", reqObject);
328 error = UPDATE_ERROR;
331 error = update_SendFile(fd, call, &status);
339 /* fetch dir info about directory name and send it to remote host associated
342 UPDATE_FetchInfo(struct rx_call *call, char *name)
348 /* construct a local path from a canonical (wire-format) path */
349 if ((error = ConstructLocalPath(name, "/", &reqObject))) {
350 afs_com_err(whoami, error, "Unable to construct local path");
354 if (!AuthOkay(call, reqObject)) {
355 error = UPDATE_ERROR;
357 /* we only need to stat the obj, not open it. */
358 if (stat(reqObject, &status) < 0) {
359 printf("Failed to open %s\n", reqObject);
360 error = UPDATE_ERROR;
362 if ((status.st_mode & S_IFMT) != S_IFDIR) {
363 printf(" file %s is not a directory \n", reqObject);
368 error = update_SendDirInfo(reqObject, call, &status, name);
377 fprintf(stderr, "%s", msg);
382 update_SendFile(int fd, struct rx_call *call, struct stat *status)
384 char *buffer = (char *)0;
386 afs_int32 length, tlen;
388 struct statfs tstatfs;
393 /* Unfortunately in AIX valuable fields such as st_blksize are gone from the stat structure!! */
394 fstatfs(fd, &tstatfs);
395 blockSize = tstatfs.f_bsize;
399 blockSize = status->st_blksize;
401 length = status->st_size;
402 buffer = (char *)malloc(blockSize);
404 printf("malloc failed\n");
407 tlen = htonl(length);
408 rx_Write(call, (char *)&tlen, sizeof(afs_int32)); /* send length on fetch */
409 while (!error && length) {
410 int nbytes = (length > blockSize ? blockSize : length);
411 nbytes = read(fd, buffer, nbytes);
413 fprintf(stderr, "File system read failed\n");
416 if (rx_Write(call, buffer, nbytes) != nbytes)
423 error = UPDATE_ERROR;
427 /* Enumerate dir (name) and write dir entry info into temp file.
430 update_SendDirInfo(char *name, /* Name of dir to enumerate */
431 struct rx_call *call, /* rx call */
432 struct stat *status, /* stat struct for dir */
433 char *origDir) /* orig name of dir before being localized */
439 char filename[MAXFNSIZE], dirInfoFile[MAXFNSIZE];
440 int fd, tfd, errcode, error, err;
443 dirp = opendir(name);
444 sprintf(dirInfoFile, "%s/upserver.tmp", gettmpdir());
445 stream = fopen(dirInfoFile, "w");
449 while ((dp = readdir(dirp))) {
450 strcpy(filename, name);
451 strcat(filename, "/");
452 strcat(filename, dp->d_name);
454 tfd = open(filename, O_RDONLY, 0);
455 if (tfd < 0 || fstat(tfd, &tstatus) < 0) {
456 printf("Failed to open %s\n", name);
457 error = UPDATE_ERROR;
460 if ((tstatus.st_mode & S_IFMT) != S_IFDIR) { /* not a directory */
461 char dirEntry[MAXFNSIZE];
463 strcpy(dirEntry, origDir);
464 strcat(dirEntry, "/");
465 strcat(dirEntry, dp->d_name);
467 fprintf(stream, "\"%s\" %u %u %u %u %u %u\n", dirEntry,
468 (unsigned int)tstatus.st_mtime,
469 (unsigned int)tstatus.st_size, tstatus.st_mode,
470 tstatus.st_uid, tstatus.st_gid,
471 (unsigned int)tstatus.st_atime);
477 printf("could not close file %s \n", filename);
478 error = UPDATE_ERROR;
489 error = UPDATE_ERROR;
493 fd = open(dirInfoFile, O_RDONLY, 0);
496 errcode = update_SendFile(fd, call, &tstatus);
499 error = UPDATE_ERROR;
508 /* AddObject() - Adds the object to the list of exported objects after
509 * converting to a local path.
511 * expPath : points to allocated storage in which the exportable path is
513 * dir : dir name passed in for export
517 AddObject(char **expPath, char *dir)
522 /* construct a local path from a canonical (wire-format) path */
523 if ((error = ConstructLocalPath(dir, "/", expPath))) {
524 afs_com_err(whoami, error, "Unable to construct local path");
528 /* stat the object */
529 error = stat(*expPath, &statbuf);
531 afs_com_err(whoami, error, ";Can't stat object.");
534 /* now check if the object has an exportable (file/dir) type */
535 if (!(statbuf.st_mode & S_IFDIR)) {
536 fprintf(stderr, "%s: Unacceptable object type for %s\n", whoami,