Initial commit for Tizen
[profile/extras/shadow-utils.git] / contrib / adduser-old.c
1 /****
2 ** 03/17/96
3 ** hacked a bit more, removed unused code, cleaned up for gcc -Wall.
4 ** --marekm
5 **
6 ** 02/26/96
7 ** modified to call shadow utils (useradd,chage,passwd) on shadowed 
8 ** systems - Cristian Gafton, gafton@sorosis.ro
9 **
10 ** 6/27/95
11 ** shadow-adduser 1.4:
12 **
13 ** now it copies the /etc/skel dir into the person's dir, 
14 ** makes the mail folders, changed some defaults and made a 'make 
15 ** install' just for the hell of it.
16 **
17 ** Greg Gallagher
18 ** CIN.Net
19 **
20 ** 1/28/95
21 ** shadow-adduser 1.3:
22 ** 
23 ** Basically a bug-fix on my additions in 1.2.  Thanx to Terry Stewart 
24 ** (stew@texas.net) for pointing out one of the many idiotic bugs I introduced.
25 ** It was such a stupid bug that I would have never seen it myself.
26 **
27 **                                Brandon
28 *****
29 ** 01/27/95
30 ** 
31 ** shadow-adduser 1.2:
32 ** I took the C source from adduser-shadow (credits are below) and made
33 ** it a little more worthwhile.  Many small changes... Here's
34 ** the ones I can remember:
35 ** 
36 ** Removed support for non-shadowed systems (if you don't have shadow,
37 **     use the original adduser, don't get this shadow version!)
38 ** Added support for the correct /etc/shadow fields (Min days before
39 **     password change, max days before password change, Warning days,
40 **     and how many days from expiry date does the account go invalid)
41 **     The previous version just left all of those fields blank.
42 **     There is still one field left (expiry date for the account, period)
43 **     which I have left blank because I do not use it and didn't want to
44 **     spend any more time on this.  I'm sure someone will put it in and
45 **     tack another plethora of credits on here. :)
46 ** Added in the password date field, which should always reflect the last
47 **     date the password was changed, for expiry purposes.  "passwd" always
48 **     updates this field, so the adduser program should set it up right
49 **     initially (or a user could keep thier initial password forever ;)
50 **     The number is in days since Jan 1st, 1970.
51 **
52 **                       Have fun with it, and someone please make
53 **                       a real version(this is still just a hack)
54 **                       for us all to use (and Email it to me???)
55 **
56 **                               Brandon
57 **                                  photon@usis.com
58 **
59 ***** 
60 ** adduser 1.0: add a new user account (For systems not using shadow)
61 ** With a nice little interface and a will to do all the work for you.
62 **
63 ** Craig Hagan
64 ** hagan@opine.cs.umass.edu
65 **
66 ** Modified to really work, look clean, and find unused uid by Chris Cappuccio
67 ** chris@slinky.cs.umass.edu
68 **
69 *****
70 **
71 ** 01/19/95
72 **
73 ** FURTHER modifications to enable shadow passwd support (kludged, but
74 ** no more so than the original)  by Dan Crowson - dcrowson@mo.net
75 **
76 ** Search on DAN for all changes...
77 **
78 *****
79 **
80 ** cc -O -o adduser adduser.c
81 ** Use gcc if you have it... (political reasons beyond my control) (chris)
82 **
83 ** I've gotten this program to work with success under Linux (without
84 ** shadow) and SunOS 4.1.3. I would assume it should work pretty well
85 ** on any system that uses no shadow. (chris)
86 **
87 ** If you have no crypt() then try
88 ** cc -DNO_CRYPT -O -o adduser adduser.c xfdes.c
89 ** I'm not sure how login operates with no crypt()... I guess
90 ** the same way we're doing it here.
91 */
92
93 #include <pwd.h>
94 #include <grp.h>
95 #include <ctype.h>
96 #include <stdio.h>
97 #include <stdlib.h>
98 #include <string.h>
99 #include <unistd.h>
100 #include <time.h>
101 #include <sys/types.h>
102 #include <sys/timeb.h>
103 #include <sys/time.h>
104 #include <sys/stat.h>
105
106 #define DEFAULT_SHELL   "/bin/bash"  /* because BASH is your friend */
107 #define DEFAULT_HOME    "/home"
108 #define USERADD_PATH    "/usr/sbin/useradd"
109 #define CHAGE_PATH      "/usr/sbin/chage"
110 #define PASSWD_PATH     "/usr/bin/passwd"
111 #define DEFAULT_GROUP   100
112
113 #define DEFAULT_MAX_PASS 60
114 #define DEFAULT_WARN_PASS 10
115 /* if you use this feature, you will get a lot of complaints from users
116    who rarely use their accounts :)  (something like 3 months would be
117    more reasonable)  --marekm */
118 #define DEFAULT_USER_DIE /* 10 */ 0
119
120 void main()
121 {
122         char foo[32];                   
123         char uname[9],person[32],dir[32],shell[32];
124         unsigned int group,min_pass,max_pass,warn_pass,user_die;
125         /* the group and uid of the new user */
126         int bad=0,done=0,correct=0,gets_warning=0;
127         char cmd[255];
128         struct group *grp;
129         
130         /* flags, in order:
131         * bad to see if the username is in /etc/passwd, or if strange stuff has
132         * been typed if the user might be put in group 0
133         * done allows the program to exit when a user has been added
134         * correct loops until a password is found that isn't in /etc/passwd
135         * gets_warning allows the fflush to be skipped for the first gets
136         * so that output is still legible
137         */
138
139         /* The real program starts HERE! */
140   
141         if(geteuid()!=0)
142         {
143                 printf("It seems you don't have access to add a new user.  Try\n");
144                 printf("logging in as root or su root to gain super-user access.\n");
145                 exit(1);
146         }
147   
148         /* Sanity checks
149         */
150         
151         if (!(grp=getgrgid(DEFAULT_GROUP))){
152                 printf("Error: the default group %d does not exist on this system!\n",
153                                 DEFAULT_GROUP);
154                 printf("adduser must be recompiled.\n");
155                 exit(1);
156         }; 
157  
158         while(!correct) {               /* loop until a "good" uname is chosen */
159                 while(!done) {
160                         printf("\nLogin to add (^C to quit): ");
161                         if(gets_warning)        /* if the warning was already shown */
162                                 fflush(stdout); /* fflush stdout, otherwise set the flag */
163                         else
164                                 gets_warning=1;
165
166                         gets(uname);
167                         if(!strlen(uname)) {
168                                 printf("Empty input.\n");
169                                 done=0;
170                                 continue;
171                         };
172
173                         /* what I saw here before made me think maybe I was running DOS */
174                         /* might this be a solution? (chris) */
175                         if (getpwnam(uname) != NULL) {
176                                 printf("That name is in use, choose another.\n");
177                                 done=0;
178                         } else
179                                 done=1;
180                 }; /* done, we have a valid new user name */
181                 
182                 /* all set, get the rest of the stuff */
183                 printf("\nEditing information for new user [%s]\n",uname);
184   
185                 printf("\nFull Name [%s]: ",uname);
186                 gets(person);
187                 if (!strlen(person)) {
188                         bzero(person,sizeof(person));
189                         strcpy(person,uname);
190                 };
191       
192                 do {
193                         bad=0; 
194                         printf("GID [%d]: ",DEFAULT_GROUP);
195                         gets(foo);
196                         if (!strlen(foo))
197                                 group=DEFAULT_GROUP;
198                         else
199                                 if (isdigit (*foo)) {
200                                         group = atoi(foo);
201                                         if (! (grp = getgrgid (group))) {
202                                                 printf("unknown gid %s\n",foo);
203                                                 group=DEFAULT_GROUP;
204                                                 bad=1;
205                                         };
206                         } else
207                                 if ((grp = getgrnam (foo)))
208                                         group = grp->gr_gid;
209                                 else {
210                                         printf("unknown group %s\n",foo);
211                                         group=DEFAULT_GROUP;
212                                         bad=1;
213                                 }
214                         if (group==0){  /* You're not allowed to make root group users! */
215                                 printf("Creation of root group users not allowed (must be done by hand)\n");
216                                 group=DEFAULT_GROUP;
217                                 bad=1;
218                         };
219                 } while(bad);
220
221
222                 fflush(stdin);
223       
224                 printf("\nIf home dir ends with a / then [%s] will be appended to it\n",uname);
225                 printf("Home Directory [%s/%s]: ",DEFAULT_HOME,uname);
226                 fflush(stdout);
227                 gets(dir);
228                 if (!strlen(dir)) { /* hit return */
229                         sprintf(dir,"%s/%s",DEFAULT_HOME,uname);
230                         fflush(stdin);
231                 } else
232                         if (dir[strlen(dir)-1]=='/')
233                                 sprintf(dir+strlen(dir),"%s",uname);
234
235                 printf("\nShell [%s]: ",DEFAULT_SHELL);
236                 fflush(stdout);
237                 gets(shell);
238                 if (!strlen(shell))
239                         sprintf(shell,"%s",DEFAULT_SHELL);
240       
241                 printf("\nMin. Password Change Days [0]: ");
242                 gets(foo);
243                 min_pass=atoi(foo);
244             
245                 printf("Max. Password Change Days [%d]: ",DEFAULT_MAX_PASS);
246                 gets(foo);
247                 if (strlen(foo) > 1)
248                         max_pass = atoi(foo);
249                 else
250                         max_pass = DEFAULT_MAX_PASS;
251             
252                 printf("Password Warning Days [%d]: ",DEFAULT_WARN_PASS);
253                 gets(foo);
254                 warn_pass = atoi(foo);
255                 if (warn_pass==0)
256                         warn_pass = DEFAULT_WARN_PASS;
257             
258                 printf("Days after Password Expiry for Account Locking [%d]: ",DEFAULT_USER_DIE);
259                 gets(foo);
260                 user_die = atoi(foo);
261                 if (user_die == 0)
262                         user_die = DEFAULT_USER_DIE;
263       
264                 printf("\nInformation for new user [%s] [%s]:\n",uname,person);
265                 printf("Home directory: [%s] Shell: [%s]\n",dir,shell);
266                 printf("GID: [%d]\n",group);
267                 printf("MinPass: [%d] MaxPass: [%d] WarnPass: [%d] UserExpire: [%d]\n",
268                                 min_pass,max_pass,warn_pass,user_die);
269                 printf("\nIs this correct? [y/N]: ");
270                 fflush(stdout);
271                 gets(foo);
272
273                 done=bad=correct=(foo[0]=='y'||foo[0]=='Y');
274
275                 if(bad!=1)
276                         printf("\nUser [%s] not added\n",uname);
277     }
278
279         bzero(cmd,sizeof(cmd));
280         sprintf(cmd,"%s -g %d -d %s -s %s -c \"%s\" -m -k /etc/skel %s",
281                         USERADD_PATH,group,dir,shell,person,uname);
282         printf("Calling useradd to add new user:\n%s\n",cmd);  
283         if(system(cmd)){
284                 printf("User add failed!\n");
285                 exit(errno);
286         };
287         bzero(cmd,sizeof(cmd));
288         sprintf(cmd,"%s -m %d -M %d -W %d -I %d %s", CHAGE_PATH,
289                         min_pass,max_pass,warn_pass,user_die,uname);
290         printf("%s\n",cmd);
291         if(system(cmd)){
292                 printf("There was an error setting password expire values\n");
293                 exit(errno);
294         };
295         bzero(cmd,sizeof(cmd));
296         sprintf(cmd,"%s %s",PASSWD_PATH,uname);
297         system(cmd);
298         printf("\nDone.\n");
299 }
300