7cf2a9915bf8b388bf8dda35b31aee3078db7708
[openafs.git] / src / uss / uss_common.c
1 /*
2  * Copyright 2000, International Business Machines Corporation and others.
3  * All Rights Reserved.
4  * 
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
8  */
9
10 /*
11  *      Storage for common variables employed by the AFS user
12  *      account facility.
13  */
14
15 /*
16  * --------------------- Required definitions ---------------------
17  */
18 #include <afsconfig.h>
19 #include <afs/param.h>
20
21 RCSID
22     ("$Header$");
23
24 #include "uss_common.h"         /*Interface definitions */
25 #include <afs/kautils.h>        /*MAXKTCREALMLEN & MAXKTCNAMELEN */
26
27 #include <string.h>
28
29
30 /*
31  * ---------------------- Exported variables ----------------------
32  */
33 #define uss_AutoLen     300
34 #define uss_NumVars      10
35
36 char uss_User[uss_UserLen + 1]; /*User's account name */
37 char uss_Uid[uss_UidLen + 1];   /*User's uid */
38 char uss_Server[uss_ServerLen + 1];     /*FileServer hosting user's volume */
39 char uss_Partition[uss_PartitionLen + 1];       /*FileServer partition for above */
40 char uss_MountPoint[uss_MountPointLen + 1];     /*Mountpoint for user's volume */
41 char uss_RealName[uss_RealNameLen + 1]; /*User's full name */
42 char uss_Pwd[uss_PwdLen + 1];   /*User password */
43 char uss_PwdPath[uss_PwdPathLen + 1];   /*Curr pathname to password file */
44 char uss_PwdFormat[uss_PwdFormatLen + 1];       /*Curr password entry format */
45 char uss_RestoreDir[uss_RestoreDirLen + 1];     /*Curr directory for restore info */
46 char uss_Auto[uss_AutoLen + 1]; /*Curr choice of AUTO value */
47 char uss_Var[uss_NumVars][uss_MAX_ARG_SIZE];    /*$1, $2, ... command variables */
48 int uss_VarMax = 0;             /*Largest index in above */
49 char uss_Volume[uss_VolumeLen + 1];     /*Name of user's volume */
50 afs_int32 uss_VolumeID;         /*Numerical volume ID */
51 afs_int32 uss_ServerID;         /*Numerical server ID */
52 afs_int32 uss_PartitionID;      /*Numerical partition ID */
53 char uss_DirPool[100][uss_DirPoolLen];  /*List of all acceptable subdirs */
54
55
56 /*
57  * Values saved across invocations.
58  */
59 char uss_PwdPath_Saved[uss_PwdPathLen]; /*Pathname to saved pwd file */
60 char uss_PwdFormat_Saved[uss_PwdFormatLen];     /*Saved pwd entry format */
61 char uss_RestoreDir_Saved[uss_RestoreDirLen];   /*Saved dir for restore info */
62
63 int uss_NumGroups = 0;          /*Number of $AUTO entries */
64 int uss_SaveVolume;             /*Save current user volume? */
65 int uss_SaveVolume_Saved;       /*Saved value of above */
66 int uss_DryRun = 0;             /*Is this a dry run? */
67 int uss_SkipKaserver = 0;       /*Don't talk to kaserver */
68 int uss_Overwrite = 0;          /*Overwrite user files? */
69 int uss_OverwriteThisOne;       /*Overwrite on this pass? */
70 char uss_Administrator[64];     /*Name of admin account */
71 char uss_AccountCreator[MAXKTCNAMELEN]; /*Principal running this program */
72 afs_int32 uss_DesiredUID;       /*Uid to assign the user */
73 afs_int32 uss_Expires;          /*How long between password expires */
74 char uss_Cell[MAXKTCREALMLEN];  /*Cell in which account lives */
75 char uss_ConfDir[uss_PATH_SIZE];        /*Config directory */
76 int uss_verbose = 0;            /*Are we being verbose? */
77 int uss_ignoreFlag = 0;         /*Ignore yyparse errors? */
78 int uss_perr = 0;
79 char uss_whoami[64];            /*Program name used */
80 int uss_syntax_err = 0;         /*YACC syntax error? */
81 struct uss_subdir *uss_currentDir = NULL;       /*Current directory */
82
83
84 /*
85  * ----------------------- Private variables ----------------------
86  */
87 static int initDone = 0;        /*Have we been initialized? */
88
89
90 /*------------------------------------------------------------------------
91  * EXPORTED uss_common_Init
92  *
93  * Description:
94  *      Set up various common uss variables, especially the saved ones.
95  *
96  * Arguments:
97  *      None.
98  *
99  * Returns:
100  *      Nothing.
101  *
102  * Environment:
103  *      We only allow an initialization once.
104  *
105  * Side Effects:
106  *      As advertised.
107  *------------------------------------------------------------------------*/
108
109 void
110 uss_common_Init()
111 {                               /*uss_common_Init */
112
113     extern int line;            /*Current template line */
114
115     if (initDone)
116         return;
117
118     strcpy(uss_ConfDir, AFSDIR_CLIENT_ETC_DIRPATH);
119     uss_PwdPath_Saved[0] = '\0';
120     uss_PwdFormat_Saved[0] = '\0';
121     uss_RestoreDir_Saved[0] = '\0';
122     uss_SaveVolume_Saved = 0;
123     uss_syntax_err = 0;
124     line = 1;
125
126 }                               /*uss_common_Init */
127
128
129 /*------------------------------------------------------------------------
130  * EXPORTED uss_common_Reset
131  *
132  * Description:
133  *      Resets some of the common variables to their idle or
134  *      saved states.
135  *
136  * Arguments:
137  *      None.
138  *
139  * Returns:
140  *      Nothing.
141  *
142  * Environment:
143  *      Nothing interesting.
144  *
145  * Side Effects:
146  *      As advertised.
147  *------------------------------------------------------------------------*/
148
149 void
150 uss_common_Reset()
151 {                               /*uss_common_Reset */
152
153     extern int line;            /*Current template line */
154     int i;                      /*Loop variable */
155
156     uss_ignoreFlag = 0;
157     uss_NumGroups = 0;
158     uss_perr = 0;
159     uss_currentDir = NULL;
160     uss_DesiredUID = 0;
161     uss_VarMax = 0;
162     uss_User[0] = '\0';
163     uss_Uid[0] = '\0';
164     uss_Server[0] = '\0';
165     uss_Partition[0] = '\0';
166     uss_MountPoint[0] = '\0';
167     uss_RealName[0] = '\0';
168     sprintf(uss_Pwd, "%s", uss_DEFAULT_PASSWORD);
169     strcpy(uss_PwdPath, uss_PwdPath_Saved);
170     strcpy(uss_PwdFormat, uss_PwdFormat_Saved);
171     strcpy(uss_RestoreDir, uss_RestoreDir_Saved);
172     uss_SaveVolume = uss_SaveVolume_Saved;
173     uss_Auto[0] = '\0';
174     for (i = 1; i < 10; i++)
175         uss_Var[i][0] = '\0';
176
177     /*
178      * Reset the template line you think you're reading from, and
179      * remember you haven't seen any template file parsing errors
180      * on this run yet.
181      */
182     line = 1;
183     uss_syntax_err = 0;
184
185 }                               /*uss_common_Reset */
186
187
188 /*------------------------------------------------------------------------
189  * EXPORTED uss_common_FieldCp
190  *
191  * Environment:
192  *      Nothing interesting.
193  *
194  * Side Effects:
195  *      As advertised.
196  *------------------------------------------------------------------------*/
197
198 char *
199 uss_common_FieldCp(a_to, a_from, a_separator, a_maxChars, a_overflowP)
200      char *a_to;
201      char *a_from;
202      char a_separator;
203      int a_maxChars;
204      int *a_overflowP;
205
206 {                               /*uss_common_FieldCp */
207
208     register int chars_read;    /*Number of chars read so far */
209
210     chars_read = 0;
211     *a_overflowP = 0;
212     /*
213      * It's OK to read in the separator/null after reading a_maxChars,
214      * so we need to bump a_maxChars by one.
215      */
216     a_maxChars++;
217
218     while (*a_from != a_separator && *a_from != '\0' && *a_from != '\n') {
219         *a_to++ = *a_from++;
220         if (++chars_read >= a_maxChars) {
221             /*
222              * Bummer, we've overflowed.  Remember the sad fact, and
223              * move the destination pointer back over the extra
224              * character.  Also, skip over any garbage sitting after
225              * the characters that won't fit in to the given field.
226              */
227             *a_overflowP = 1;
228             a_to--;
229 #if 0
230             printf("*** Skipping overflow char '%c'\n", *(a_from - 1));
231 #endif /* 0 */
232             while (*a_from != a_separator && *a_from != '\0'
233                    && *a_from != '\n') {
234 #if 0
235                 printf("*** Skipping overflow char '%c'\n", *a_from);
236 #endif /* 0 */
237                 a_from++;
238             }                   /*Skip over excess chars */
239             break;
240         }                       /*Found overflow */
241     }                           /*Read til end of field */
242
243     /*
244      * Make sure we return a null-terminated string.
245      */
246     *a_to = '\0';
247
248     /*
249      * Collapse any number of blanks, should we have ended up pointing
250      * to one.
251      */
252     if (a_separator == ' ')
253         while ((*a_from == ' ') && (*(a_from + 1) == ' '))
254             a_from++;
255
256     /*
257      * Return the position of the next non-separator char or
258      * the position of the null, if that's what was left.
259      */
260     if (*a_from == '\0')
261         return (a_from);
262     else
263         return (++a_from);
264
265 }                               /*uss_common_FieldCp */