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