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
11 * Implementation of basic procedures for the AFS user account
16 * --------------------- Required definitions ---------------------
18 #include <afsconfig.h>
19 #include <afs/param.h>
23 #include "uss_procs.h" /*Module interface*/
24 #include "uss_common.h" /*Common defs & operations*/
25 #include "uss_acl.h" /*ACL-related operations*/
26 #include <errno.h> /*Unix error codes*/
27 #include <pwd.h> /*Password info*/
28 #include <sys/stat.h> /*Stat defs*/
29 #include <dirent.h> /*Directory package*/
30 #include <sys/file.h> /*O_EXCL, O_CREAT, etc*/
34 #include <afs/kautils.h> /*MAXKTCREALMLEN*/
39 #undef USS_PROCS_DB_INSTANCE
40 #undef USS_PROCS_DB_BUILDDIR
42 #define uss_procs_MAX_SIZE 2048
50 /*-----------------------------------------------------------------------
51 * EXPORTED uss_procs_BuildDir
54 * Called from the code generated by the uss grammar.
58 *------------------------------------------------------------------------*/
60 afs_int32 uss_procs_BuildDir(a_path, a_mode, a_owner, a_access)
66 { /*uss_procs_BuildDir*/
70 struct uss_subdir *new_dir;
74 * Don't do anything if there's already a problem.
80 printf("Building directory '%s'; owner: '%s', ACL: '%s'\n",
81 a_path, a_owner, a_access);
84 * If we've not been given permission to overwrite things, make sure
85 * the target doesn't exist before doing anything.
87 if (!uss_OverwriteThisOne) {
89 if (!stat(temp, &stbuf)) {
91 printf("\t[Directory exists, NOT overwriting it]\n");
96 sscanf(a_mode, "%o", &m);
97 o = uss_procs_GetOwner(a_owner);
98 #ifdef USS_PROCS_DB_BUILDDIR
99 printf("DEBUGGING: Owner '%s' is uid %d\n", a_owner, o);
100 #endif /* USS_PROCS_DB_BUILDDIR */
103 if (mkdir(a_path, m)) {
105 * Can't make the directory. Complain if the directory doesn't
108 if (errno != EEXIST) {
109 uss_procs_PrintErr(line,
110 "Failed to create directory '%s': %s\n",
111 a_path, sys_errlist[errno]);
113 } /*Directory didn't exist*/
114 } /*Create the directory*/
117 if (uss_OverwriteThisOne)
118 fprintf(stderr, "\t[Dry run: mkdir %s, mode %o]\n",
123 if (chmod(a_path, m)) {
124 uss_procs_PrintErr(line,
125 "Can't chmod() directory '%s' to be '%s' : %s\n",
126 a_path, a_mode, sys_errlist[errno]);
128 } /* chmod the directory */
129 if (chown(a_path, o, -1)) {
130 uss_procs_PrintErr(line,
131 "Can't chown() directory '%s' to be owned by '%s' (uid %d): %s\n",
132 a_path, a_owner, o, sys_errlist[errno]);
138 "\t[Dry run: chown() directory '%s' to be owned by user]\n",
143 * Set the ACL for this new directory so that the uss_AccountCreator
144 * is the only party that has rights. This will be corrected as the
145 * final action performed on the account.
147 sprintf(buf, "%s %s all", a_path, uss_AccountCreator);
151 "Setting ACL: '%s'\n", buf);
152 if (uss_acl_SetAccess(buf, 1, 0))
156 fprintf(stderr, "\t[Dry run: uss_acl_SetAccess(%s) on '%s']\n",
161 * Use our linked list to remember this directory's true ACL setting so
162 * we may set it correctly at the tail end of the account creation.
164 new_dir = (struct uss_subdir *) malloc(sizeof(struct uss_subdir));
165 new_dir->previous = uss_currentDir;
166 new_dir->path = (char *) malloc(strlen(a_path)+1);
167 strcpy(new_dir->path, a_path);
168 new_dir->finalACL = (char *) malloc(strlen(a_access)+1);
169 strcpy(new_dir->finalACL, a_access);
170 uss_currentDir = new_dir;
173 * Return the happy news.
177 } /*uss_procs_BuildDir*/
180 /*-----------------------------------------------------------------------
181 * EXPORTED uss_procs_CpFile
184 * Called from the code generated by the uss grammar.
188 *------------------------------------------------------------------------*/
190 afs_int32 uss_procs_CpFile(a_path, a_mode, a_owner, a_proto)
196 { /*uss_procs_CpFile*/
203 * Don't do anything if something has already gone wrong.
209 printf("Installing '%s'\n", a_path);
212 * If we've not been given permission to overwrite things, make sure
213 * the target doesn't exist before doing anything.
215 if (!uss_OverwriteThisOne) {
216 strcpy(temp, a_path);
217 if (!stat(temp, &stbuf)) {
219 printf("\t[Entry exists, NOT overwriting it]\n");
224 sscanf(a_mode, "%o", &m);
225 o = uss_procs_GetOwner(a_owner);
227 strcpy(temp, a_proto);
229 if (stat(temp, &stbuf)) {
230 uss_procs_PrintErr(line, "Failed to stat '%s': %s\n",
231 a_proto, sys_errlist[errno]);
235 if (stbuf.st_mode & S_IFDIR) {
236 if ((cp = strrchr(a_path, '/')) == NULL) {
237 strcat(a_proto, "/");
238 strcat(a_proto, a_path);
242 * Append the last part (file name).
246 } /*Target is a directory*/
249 if (Copy(a_proto, a_path, m)) {
250 uss_procs_PrintErr(line,
251 "Failed to copy '%s' to '%s'\n",
257 fprintf(stderr, "\t[Dry run: Copying '%s' to '%s', mode %o]\n",
262 if (chown(a_path, o, -1)) {
263 uss_procs_PrintErr(line,
264 "Can't chown() file '%s' to be owned by '%s' (uid %d): %s\n",
265 a_path, a_owner, o, sys_errlist[errno]);
270 fprintf(stderr, "\t[Dry run: chown() file '%s' to be owned by user]\n",
275 * Return the happy news.
279 } /*uss_procs_CpFile*/
282 /*-----------------------------------------------------------------------
283 * EXPORTED uss_procs_EchoToFile
286 * Called from the code generated by the uss grammar.
290 *------------------------------------------------------------------------*/
292 afs_int32 uss_procs_EchoToFile(a_path, a_mode, a_owner, a_content)
298 { /*uss_procs_EchoToFile*/
304 * Don't do anything if something has already gone wrong.
310 printf("Echoing to '%s'\n", a_path);
313 * If we've not been given permission to overwrite things, make sure
314 * the target doesn't exist before doing anything.
316 if (!uss_OverwriteThisOne) {
317 strcpy(temp, a_path);
318 if (!stat(temp, &stbuf)) {
320 printf("\t[Entry exists, NOT overwriting it]\n");
325 sscanf(a_mode, "%o", &m);
326 o = uss_procs_GetOwner(a_owner);
329 if (Echo(a_content, a_path, m)){
330 uss_procs_PrintErr(line,
331 "Failed to echo string '%s' to file '%s'\n",
337 fprintf(stderr, "\t[Dry run: Echoing '%s' into file '%s']\n",
342 if (chown(a_path, o, -1)){
343 uss_procs_PrintErr(line,
344 "Can't chown() file '%s' to be owned by '%s' (uid %d): %s\n",
345 a_path, a_owner, o, sys_errlist[errno]);
351 "\t[Dry run: chown() file '%s' to be owned by user]\n",
356 * Return the happy news.
360 } /*uss_procs_EchoToFile*/
363 /*-----------------------------------------------------------------------
364 * EXPORTED uss_procs_Exec
367 * Called from the code generated by the uss grammar, as well as
372 *------------------------------------------------------------------------*/
374 afs_int32 uss_procs_Exec(a_command)
380 printf("Running '%s'\n", a_command);
383 if (system(a_command)) {
384 uss_procs_PrintErr(line,
385 "Failed to run the '%s' command: %s\n",
386 a_command, sys_errlist[errno]);
391 fprintf(stderr, "\t[Dry run: executing '%s']\n", a_command);
395 * Return the happy news.
402 /*-----------------------------------------------------------------------
403 * EXPORTED uss_procs_SetLink
406 * Called from the code generated by the uss grammar.
410 *------------------------------------------------------------------------*/
412 afs_int32 uss_procs_SetLink(a_path1, a_path2, a_type)
417 { /*uss_procs_SetLink*/
422 printf("Setting link '%s' to '%s'\n", a_path1, a_path2);
425 * If we've not been given permission to overwrite things, make sure
426 * the target doesn't exist before doing anything.
428 if (!uss_OverwriteThisOne) {
429 strcpy(temp, a_path2);
430 if (!stat(temp, &stbuf)) {
432 printf("\t[Entry exists, NOT overwriting it]\n");
442 if (symlink(a_path1, a_path2)) {
443 uss_procs_PrintErr(line,
444 "Failed to make symlink '%s' to '%s': %s\n",
445 a_path1, a_path2, sys_errlist[errno]);
450 fprintf(stderr, "\t[Dry run: Making symlink '%s' to '%s']\n",
459 if (link(a_path1, a_path2)) {
460 uss_procs_PrintErr(line,
461 "Failed to make hard link '%s' to '%s': %s\n",
462 a_path1, a_path2, sys_errlist[errno]);
467 fprintf(stderr, "\t[Dry run: Making hard link '%s' to '%s']\n",
473 * Return the happy news.
477 } /*uss_procs_SetLink*/
480 /*-----------------------------------------------------------------------
481 * EXPORTED uss_procs_GetOwner
484 * Nothing interesting.
488 *------------------------------------------------------------------------*/
490 int uss_procs_GetOwner(a_ownerStr)
493 { /*uss_procs_GetOwner*/
495 struct passwd *pw; /*Ptr to password file entry*/
496 int ownerID; /*Numerical owner*/
498 ownerID = atoi(a_ownerStr);
499 if ((ownerID == 0) && (a_ownerStr[0] != '0')) {
501 * The owner is not in numerical format
503 if ((pw = getpwnam(a_ownerStr)) == NULL) {
504 uss_procs_PrintErr(line,
505 "Owner '%s' is an unknown user\n",
514 } /*uss_procs_GetOwner*/
516 /*-----------------------------------------------------------------------
520 * Copies the "from" file to the "to" file and sets the mode.
523 * a_from : File to copy from.
524 * a_to : File to copy to.
525 * a_mode : New Unix mode to set.
528 * 0 if everything went well,
529 * if there was a problem.
532 * Nothing interesting.
536 *------------------------------------------------------------------------*/
538 static int Copy(a_from, a_to, a_mode)
545 register int fd1, fd2;
550 fd1 = open(a_to, O_EXCL | O_CREAT | O_WRONLY, a_mode);
552 if (errno == EEXIST) {
554 * The file exists. Since we can only be called when overwriting
555 * has been enabled, we back off and open the file normally (not
556 * supplying O_EXCL), then continue normally.
558 fd1 = open(a_to, O_CREAT | O_WRONLY, a_mode);
560 uss_procs_PrintErr(line,
561 "%s: Failed to open '%s' for overwrite: %s.\n",
562 uss_whoami, a_to, sys_errlist[errno]);
566 uss_procs_PrintErr(line, "%s: Failed to open '%s': %s.\n",
567 uss_whoami, a_to, sys_errlist[errno]);
572 if ((fd2 = open(a_from, O_RDONLY, 0)) < 0) {
573 uss_procs_PrintErr(line, "%s: Error reading '%s': %s\n",
574 uss_whoami, a_from, sys_errlist[errno]);
578 while ((cnt = read(fd2, buf, BUFSIZ)) == BUFSIZ)
579 write(fd1, buf, cnt);
581 write(fd1, buf, cnt);
584 uss_procs_PrintErr(line,
585 "Failed to close '%s' %s\n",
586 a_to, sys_errlist[errno]);
590 uss_procs_PrintErr(line, "Warning: Failed to close '%s': %s\n",
591 a_from, sys_errlist[errno]);
596 /*-----------------------------------------------------------------------
600 * Writes a string into a file and sets its mode.
603 * a_s : String to write.
604 * a_f : Filename to write to.
605 * a_mode : New Unix mode to set.
608 * 0 if everything went well,
609 * if there was a problem.
612 * Nothing interesting.
616 *------------------------------------------------------------------------*/
618 static int Echo(a_s, a_f, a_mode)
628 fd = open(a_f, O_EXCL | O_CREAT | O_WRONLY, a_mode);
630 if (errno == EEXIST) {
632 * The file exists. Since we can only be called when
633 * overwriting has been enabled, we back off and open the
634 * file normally (not supplying the O_EXCL), then continue
635 * normally. Notice that we must truncate the file, since
636 * if the original file is longer than the stuff we're about
637 * to put in, all the old data past the current write will
640 fd = open(a_f, O_TRUNC | O_WRONLY, a_mode);
642 uss_procs_PrintErr(line,
643 "%s: Failed to open '%s' for overwrite: %s.\n",
644 uss_whoami, a_f, sys_errlist[errno]);
648 uss_procs_PrintErr(line, "%s: Failed to open '%s': %s. \n",
649 uss_whoami, a_f, sys_errlist[errno]);
653 write(fd, a_s, strlen(a_s));
656 uss_procs_PrintErr(line, "Failed to close '%s': %s\n",
657 a_f, sys_errlist[errno]);
664 /*-----------------------------------------------------------------------
665 * static uss_procs_PickADir
668 * Tries to replace $AUTO by a subdir. Subdirs are given by means
669 * of "G" in the configuration file. Each of the directories is
670 * examined, and the one with the inimum number of entries is
678 * 0 if everything went well,
679 * -1 if there was a problem.
682 * Called with THREE parameters from lex.c!
686 *------------------------------------------------------------------------*/
688 afs_int32 uss_procs_PickADir(path, cp)
692 { /*uss_procs_PickADir*/
694 char cd[300]; /*Current directory for search*/
696 int i, count, MinIndex, mina = 10000;
701 if (uss_NumGroups == 0){
702 fprintf(stderr,"%s: No choice yet given to replace $AUTO\n",
704 fprintf(stderr,"%s: Use the G command before $AUTO in config file\n",
709 if (uss_Auto[0] != '\0')
710 return(0); /* we have already done this for this user */
712 if (*(cp-1) == '/') { /*it's ..../$AUTO*/
713 for (i = 0; &path[i] != cp; i++)
719 fprintf(stderr,"%s: $AUTO must be used to replace the whole path or the whole name of a subdirectory. Found: %s$AUTO\n", uss_whoami, path);
722 cd[0] = '/'; cd[1] = '\0';
726 * We now have the current dir (cd). Search all of the given
727 * subdirs (by G in template), count the number of entries in
728 * each and pick the minimum.
730 for (i=0; i < uss_NumGroups; i++) {
731 sprintf(dirname, "%s/%s", cd, uss_DirPool[i]);
732 if ((dirp = opendir(dirname)) == NULL) {
733 if (errno != ENOTDIR)
735 "%s: Warning: Can't open dir '%s' (errno=%d). Skipped.\n",
736 uss_whoami, dirname, errno);
737 continue; /*Skip and continue anyway*/
740 for (dp = readdir(dirp); dp != NULL; dp = readdir(dirp))
741 if (dp->d_name[0] != '.')
742 count++; /* forget about files starting with .*/
744 printf("debug: Dir '%s' has %d entries\n", dirname, count);
745 #endif /* USS_PROCS_DB */
752 if (mina == 10000) { /* We found nothing */
753 fprintf(stderr,"%s: Warning: No valid choice to replace $AUTO\n",
758 strcpy(uss_Auto, uss_DirPool[MinIndex]);
760 printf("Picking dir w/minimum number of entries: '%s'\n",
765 } /*uss_procs_PickADir*/
767 /*-----------------------------------------------------------------------
768 * EXPORTED uss_procs_AddToDirPool
771 * Called from the code generated by the uss grammar.
775 *------------------------------------------------------------------------*/
777 int uss_procs_AddToDirPool(a_dirToAdd)
780 if (uss_NumGroups > 99) {
783 strcpy(uss_DirPool[uss_NumGroups++], a_dirToAdd);
787 /*-----------------------------------------------------------------------
788 * EXPORTED uss_procs_FindAndOpen
791 * Nothing interesting.
795 *------------------------------------------------------------------------*/
797 FILE *uss_procs_FindAndOpen(a_fileToOpen)
800 { /*uss_procs_FindAndOpen*/
802 #define NUM_TPL_PATHS 3
804 FILE *rv; /*Template file descriptor*/
805 int i; /*Loop counter*/
806 char tmp_str[uss_MAX_SIZE]; /*Tmp string*/
808 TemplatePath[NUM_TPL_PATHS][1024]; /*Template directories*/
809 int cant_read; /*Can't read the file?*/
812 * If a full pathname was given, just take it as is.
814 if (strchr(a_fileToOpen, '/')) {
815 strcpy(tmp_str, a_fileToOpen);
816 rv = fopen(a_fileToOpen, "r");
820 * A relative pathname was given. Try to find the file in each of
821 * the default template directories.
825 sprintf(TemplatePath[0], "%s", ".");
826 sprintf(TemplatePath[1], "/afs/%s/common/uss", uss_Cell);
827 sprintf(TemplatePath[2], "%s", "/etc");
829 for (i = 0; i < NUM_TPL_PATHS; i++) {
830 sprintf(tmp_str, "%s/%s", TemplatePath[i], a_fileToOpen);
831 if ((rv = fopen(tmp_str, "r")) != NULL)
835 * If the file was there but couldn't be opened, we have
838 if (errno != ENOENT) {
842 } /*Look in template directories*/
845 * If we found and opened the file, we're happy. Otherwise,
846 * print out what went wrong.
850 fprintf(stderr, "Using template '%s'\n", tmp_str);
854 * Check to see if we specifically found the file but
859 "%s: Can't open template '%s': %s\n",
860 uss_whoami, tmp_str, sys_errlist[errno]);
863 "%s: Can't find template '%s' in searchlist",
864 uss_whoami, a_fileToOpen);
865 for (i = 0; i < NUM_TPL_PATHS; i++)
866 fprintf(stderr, " '%s'", TemplatePath[i]);
867 fprintf(stderr, "\n");
868 } /*Can't find template*/
870 } /*Relative pathname given*/
873 * Whatever happened, return what we got.
877 } /*uss_procs_FindAndOpen*/
880 /*-----------------------------------------------------------------------
881 * EXPORTED uss_procs_PrintErr
884 * Nothing interesting.
888 *------------------------------------------------------------------------*/
890 void uss_procs_PrintErr(a_lineNum, a_fmt, a_1, a_2, a_3, a_4, a_5)
899 { /*uss_procs_PrintErr*/
902 fprintf(stderr,"%s: Template file, line %d: ", uss_whoami, a_lineNum);
903 fprintf(stderr, a_fmt, a_1, a_2, a_3, a_4, a_5);
905 } /*uss_procs_PrintErr*/