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>
24 #include "uss_procs.h" /*Module interface */
25 #include "uss_common.h" /*Common defs & operations */
26 #include "uss_acl.h" /*ACL-related operations */
27 #include <errno.h> /*Unix error codes */
28 #include <pwd.h> /*Password info */
29 #include <sys/stat.h> /*Stat defs */
30 #include <dirent.h> /*Directory package */
31 #include <sys/file.h> /*O_EXCL, O_CREAT, etc */
38 #include <afs/kautils.h> /*MAXKTCREALMLEN*/
40 #undef USS_PROCS_DB_INSTANCE
41 #undef USS_PROCS_DB_BUILDDIR
42 #define uss_procs_MAX_SIZE 2048
49 /*-----------------------------------------------------------------------
50 * EXPORTED uss_procs_BuildDir
53 * Called from the code generated by the uss grammar.
57 *------------------------------------------------------------------------*/
60 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", a_path,
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, strerror(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", a_path, m);
122 if (chmod(a_path, m)) {
123 uss_procs_PrintErr(line,
124 "Can't chmod() directory '%s' to be '%s' : %s\n",
125 a_path, a_mode, strerror(errno));
127 } /* chmod the directory */
128 if (chown(a_path, o, -1)) {
129 uss_procs_PrintErr(line,
130 "Can't chown() directory '%s' to be owned by '%s' (uid %d): %s\n",
131 a_path, a_owner, o, strerror(errno));
133 } /*Couldn't chown */
137 "\t[Dry run: chown() directory '%s' to be owned by user]\n",
142 * Set the ACL for this new directory so that the uss_AccountCreator
143 * is the only party that has rights. This will be corrected as the
144 * final action performed on the account.
146 sprintf(buf, "%s %s all", a_path, uss_AccountCreator);
149 fprintf(stderr, "Setting ACL: '%s'\n", buf);
150 if (uss_acl_SetAccess(buf, 1, 0))
154 fprintf(stderr, "\t[Dry run: uss_acl_SetAccess(%s) on '%s']\n", buf,
159 * Use our linked list to remember this directory's true ACL setting so
160 * we may set it correctly at the tail end of the account creation.
162 new_dir = (struct uss_subdir *)malloc(sizeof(struct uss_subdir));
163 new_dir->previous = uss_currentDir;
164 new_dir->path = (char *)malloc(strlen(a_path) + 1);
165 strcpy(new_dir->path, a_path);
166 new_dir->finalACL = (char *)malloc(strlen(a_access) + 1);
167 strcpy(new_dir->finalACL, a_access);
168 uss_currentDir = new_dir;
171 * Return the happy news.
175 } /*uss_procs_BuildDir */
178 /*-----------------------------------------------------------------------
179 * EXPORTED uss_procs_CpFile
182 * Called from the code generated by the uss grammar.
186 *------------------------------------------------------------------------*/
189 uss_procs_CpFile(a_path, a_mode, a_owner, a_proto)
195 { /*uss_procs_CpFile */
202 * Don't do anything if something has already gone wrong.
208 printf("Installing '%s'\n", a_path);
211 * If we've not been given permission to overwrite things, make sure
212 * the target doesn't exist before doing anything.
214 if (!uss_OverwriteThisOne) {
215 strcpy(temp, a_path);
216 if (!stat(temp, &stbuf)) {
218 printf("\t[Entry exists, NOT overwriting it]\n");
223 sscanf(a_mode, "%o", &m);
224 o = uss_procs_GetOwner(a_owner);
226 strcpy(temp, a_proto);
228 if (stat(temp, &stbuf)) {
229 uss_procs_PrintErr(line, "Failed to stat '%s': %s\n", a_proto,
234 if (stbuf.st_mode & S_IFDIR) {
235 if ((cp = strrchr(a_path, '/')) == NULL) {
236 strcat(a_proto, "/");
237 strcat(a_proto, a_path);
240 * Append the last part (file name).
245 /*Target is a directory */
247 if (Copy(a_proto, a_path, m)) {
248 uss_procs_PrintErr(line, "Failed to copy '%s' to '%s'\n", a_proto,
254 fprintf(stderr, "\t[Dry run: Copying '%s' to '%s', mode %o]\n",
259 if (chown(a_path, o, -1)) {
260 uss_procs_PrintErr(line,
261 "Can't chown() file '%s' to be owned by '%s' (uid %d): %s\n",
262 a_path, a_owner, o, strerror(errno));
268 "\t[Dry run: chown() file '%s' to be owned by user]\n",
273 * Return the happy news.
277 } /*uss_procs_CpFile */
280 /*-----------------------------------------------------------------------
281 * EXPORTED uss_procs_EchoToFile
284 * Called from the code generated by the uss grammar.
288 *------------------------------------------------------------------------*/
291 uss_procs_EchoToFile(a_path, a_mode, a_owner, a_content)
297 { /*uss_procs_EchoToFile */
303 * Don't do anything if something has already gone wrong.
309 printf("Echoing to '%s'\n", a_path);
312 * If we've not been given permission to overwrite things, make sure
313 * the target doesn't exist before doing anything.
315 if (!uss_OverwriteThisOne) {
316 strcpy(temp, a_path);
317 if (!stat(temp, &stbuf)) {
319 printf("\t[Entry exists, NOT overwriting it]\n");
324 sscanf(a_mode, "%o", &m);
325 o = uss_procs_GetOwner(a_owner);
328 if (Echo(a_content, a_path, m)) {
329 uss_procs_PrintErr(line,
330 "Failed to echo string '%s' to file '%s'\n",
336 fprintf(stderr, "\t[Dry run: Echoing '%s' into file '%s']\n",
341 if (chown(a_path, o, -1)) {
342 uss_procs_PrintErr(line,
343 "Can't chown() file '%s' to be owned by '%s' (uid %d): %s\n",
344 a_path, a_owner, o, strerror(errno));
350 "\t[Dry run: chown() file '%s' to be owned by user]\n",
355 * Return the happy news.
359 } /*uss_procs_EchoToFile */
362 /*-----------------------------------------------------------------------
363 * EXPORTED uss_procs_Exec
366 * Called from the code generated by the uss grammar, as well as
371 *------------------------------------------------------------------------*/
374 uss_procs_Exec(a_command)
377 { /*uss_procs_Exec */
380 printf("Running '%s'\n", a_command);
383 if (system(a_command)) {
384 uss_procs_PrintErr(line, "Failed to run the '%s' command: %s\n",
385 a_command, strerror(errno));
390 fprintf(stderr, "\t[Dry run: executing '%s']\n", a_command);
394 * Return the happy news.
398 } /*uss_procs_Exec */
401 /*-----------------------------------------------------------------------
402 * EXPORTED uss_procs_SetLink
405 * Called from the code generated by the uss grammar.
409 *------------------------------------------------------------------------*/
412 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, strerror(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, strerror(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 *------------------------------------------------------------------------*/
491 uss_procs_GetOwner(a_ownerStr)
494 { /*uss_procs_GetOwner */
496 struct passwd *pw; /*Ptr to password file entry */
497 int ownerID; /*Numerical owner */
499 ownerID = atoi(a_ownerStr);
500 if ((ownerID == 0) && (a_ownerStr[0] != '0')) {
502 * The owner is not in numerical format
504 if ((pw = getpwnam(a_ownerStr)) == NULL) {
505 uss_procs_PrintErr(line, "Owner '%s' is an unknown user\n",
513 } /*uss_procs_GetOwner */
515 /*-----------------------------------------------------------------------
519 * Copies the "from" file to the "to" file and sets the mode.
522 * a_from : File to copy from.
523 * a_to : File to copy to.
524 * a_mode : New Unix mode to set.
527 * 0 if everything went well,
528 * if there was a problem.
531 * Nothing interesting.
535 *------------------------------------------------------------------------*/
538 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, strerror(errno));
566 uss_procs_PrintErr(line, "%s: Failed to open '%s': %s.\n",
567 uss_whoami, a_to, strerror(errno));
572 if ((fd2 = open(a_from, O_RDONLY, 0)) < 0) {
573 uss_procs_PrintErr(line, "%s: Error reading '%s': %s\n", uss_whoami,
574 a_from, strerror(errno));
578 while ((cnt = read(fd2, buf, BUFSIZ)) == BUFSIZ)
579 write(fd1, buf, cnt);
581 write(fd1, buf, cnt);
584 uss_procs_PrintErr(line, "Failed to close '%s' %s\n", a_to,
589 uss_procs_PrintErr(line, "Warning: Failed to close '%s': %s\n",
590 a_from, strerror(errno));
595 /*-----------------------------------------------------------------------
599 * Writes a string into a file and sets its mode.
602 * a_s : String to write.
603 * a_f : Filename to write to.
604 * a_mode : New Unix mode to set.
607 * 0 if everything went well,
608 * if there was a problem.
611 * Nothing interesting.
615 *------------------------------------------------------------------------*/
618 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, strerror(errno));
648 uss_procs_PrintErr(line, "%s: Failed to open '%s': %s. \n",
649 uss_whoami, a_f, strerror(errno));
653 write(fd, a_s, strlen(a_s));
656 uss_procs_PrintErr(line, "Failed to close '%s': %s\n", a_f,
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 *------------------------------------------------------------------------*/
689 uss_procs_PickADir(path, cp)
693 { /*uss_procs_PickADir */
695 char cd[300]; /*Current directory for search */
697 int i, count, MinIndex, mina = 10000;
702 if (uss_NumGroups == 0) {
703 fprintf(stderr, "%s: No choice yet given to replace $AUTO\n",
705 fprintf(stderr, "%s: Use the G command before $AUTO in config file\n",
710 if (uss_Auto[0] != '\0')
711 return (0); /* we have already done this for this user */
713 if (*(cp - 1) == '/') { /*it's ..../$AUTO */
714 for (i = 0; &path[i] != cp; i++)
720 "%s: $AUTO must be used to replace the whole path or the whole name of a subdirectory. Found: %s$AUTO\n",
729 * We now have the current dir (cd). Search all of the given
730 * subdirs (by G in template), count the number of entries in
731 * each and pick the minimum.
733 for (i = 0; i < uss_NumGroups; i++) {
734 sprintf(dirname, "%s/%s", cd, uss_DirPool[i]);
735 if ((dirp = opendir(dirname)) == NULL) {
736 if (errno != ENOTDIR)
738 "%s: Warning: Can't open dir '%s' (errno=%d). Skipped.\n",
739 uss_whoami, dirname, errno);
740 continue; /*Skip and continue anyway */
743 for (dp = readdir(dirp); dp != NULL; dp = readdir(dirp))
744 if (dp->d_name[0] != '.')
745 count++; /* forget about files starting with . */
747 printf("debug: Dir '%s' has %d entries\n", dirname, count);
748 #endif /* USS_PROCS_DB */
755 if (mina == 10000) { /* We found nothing */
756 fprintf(stderr, "%s: Warning: No valid choice to replace $AUTO\n",
760 strcpy(uss_Auto, uss_DirPool[MinIndex]);
762 printf("Picking dir w/minimum number of entries: '%s'\n",
767 } /*uss_procs_PickADir */
769 /*-----------------------------------------------------------------------
770 * EXPORTED uss_procs_AddToDirPool
773 * Called from the code generated by the uss grammar.
777 *------------------------------------------------------------------------*/
780 uss_procs_AddToDirPool(a_dirToAdd)
783 if (uss_NumGroups > 99) {
786 strcpy(uss_DirPool[uss_NumGroups++], a_dirToAdd);
790 /*-----------------------------------------------------------------------
791 * EXPORTED uss_procs_FindAndOpen
794 * Nothing interesting.
798 *------------------------------------------------------------------------*/
801 uss_procs_FindAndOpen(a_fileToOpen)
804 { /*uss_procs_FindAndOpen */
806 #define NUM_TPL_PATHS 3
808 FILE *rv; /*Template file descriptor */
809 int i; /*Loop counter */
810 char tmp_str[uss_MAX_SIZE]; /*Tmp string */
812 TemplatePath[NUM_TPL_PATHS][1024]; /*Template directories */
813 int cant_read; /*Can't read the file? */
816 * If a full pathname was given, just take it as is.
818 if (strchr(a_fileToOpen, '/')) {
819 strcpy(tmp_str, a_fileToOpen);
820 rv = fopen(a_fileToOpen, "r");
823 * A relative pathname was given. Try to find the file in each of
824 * the default template directories.
828 sprintf(TemplatePath[0], "%s", ".");
829 sprintf(TemplatePath[1], "/afs/%s/common/uss", uss_Cell);
830 sprintf(TemplatePath[2], "%s", "/etc");
832 for (i = 0; i < NUM_TPL_PATHS; i++) {
833 sprintf(tmp_str, "%s/%s", TemplatePath[i], a_fileToOpen);
834 if ((rv = fopen(tmp_str, "r")) != NULL)
838 * If the file was there but couldn't be opened, we have
841 if (errno != ENOENT) {
845 } /*Look in template directories */
848 * If we found and opened the file, we're happy. Otherwise,
849 * print out what went wrong.
853 fprintf(stderr, "Using template '%s'\n", tmp_str);
857 * Check to see if we specifically found the file but
861 fprintf(stderr, "%s: Can't open template '%s': %s\n",
862 uss_whoami, tmp_str, strerror(errno));
864 fprintf(stderr, "%s: Can't find template '%s' in searchlist",
865 uss_whoami, a_fileToOpen);
866 for (i = 0; i < NUM_TPL_PATHS; i++)
867 fprintf(stderr, " '%s'", TemplatePath[i]);
868 fprintf(stderr, "\n");
869 } /*Can't find template */
871 } /*Relative pathname given */
874 * Whatever happened, return what we got.
878 } /*uss_procs_FindAndOpen */
881 /*-----------------------------------------------------------------------
882 * EXPORTED uss_procs_PrintErr
885 * Nothing interesting.
889 *------------------------------------------------------------------------*/
892 uss_procs_PrintErr(a_lineNum, a_fmt, a_1, a_2, a_3, a_4, a_5)
901 { /*uss_procs_PrintErr */
904 fprintf(stderr, "%s: Template file, line %d: ", uss_whoami, a_lineNum);
905 fprintf(stderr, a_fmt, a_1, a_2, a_3, a_4, a_5);
907 } /*uss_procs_PrintErr */