3 ** hacked a bit more, removed unused code, cleaned up for gcc -Wall.
7 ** modified to call shadow utils (useradd,chage,passwd) on shadowed
8 ** systems - Cristian Gafton, gafton@sorosis.ro
11 ** shadow-adduser 1.4:
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.
21 ** shadow-adduser 1.3:
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.
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:
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.
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???)
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.
64 ** hagan@opine.cs.umass.edu
66 ** Modified to really work, look clean, and find unused uid by Chris Cappuccio
67 ** chris@slinky.cs.umass.edu
73 ** FURTHER modifications to enable shadow passwd support (kludged, but
74 ** no more so than the original) by Dan Crowson - dcrowson@mo.net
76 ** Search on DAN for all changes...
80 ** cc -O -o adduser adduser.c
81 ** Use gcc if you have it... (political reasons beyond my control) (chris)
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)
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.
101 #include <sys/types.h>
102 #include <sys/timeb.h>
103 #include <sys/time.h>
104 #include <sys/stat.h>
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
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
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;
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
139 /* The real program starts HERE! */
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");
151 if (!(grp=getgrgid(DEFAULT_GROUP))){
152 printf("Error: the default group %d does not exist on this system!\n",
154 printf("adduser must be recompiled.\n");
158 while(!correct) { /* loop until a "good" uname is chosen */
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 */
168 printf("Empty input.\n");
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");
180 }; /* done, we have a valid new user name */
182 /* all set, get the rest of the stuff */
183 printf("\nEditing information for new user [%s]\n",uname);
185 printf("\nFull Name [%s]: ",uname);
187 if (!strlen(person)) {
188 bzero(person,sizeof(person));
189 strcpy(person,uname);
194 printf("GID [%d]: ",DEFAULT_GROUP);
199 if (isdigit (*foo)) {
201 if (! (grp = getgrgid (group))) {
202 printf("unknown gid %s\n",foo);
207 if ((grp = getgrnam (foo)))
210 printf("unknown group %s\n",foo);
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");
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);
228 if (!strlen(dir)) { /* hit return */
229 sprintf(dir,"%s/%s",DEFAULT_HOME,uname);
232 if (dir[strlen(dir)-1]=='/')
233 sprintf(dir+strlen(dir),"%s",uname);
235 printf("\nShell [%s]: ",DEFAULT_SHELL);
239 sprintf(shell,"%s",DEFAULT_SHELL);
241 printf("\nMin. Password Change Days [0]: ");
245 printf("Max. Password Change Days [%d]: ",DEFAULT_MAX_PASS);
248 max_pass = atoi(foo);
250 max_pass = DEFAULT_MAX_PASS;
252 printf("Password Warning Days [%d]: ",DEFAULT_WARN_PASS);
254 warn_pass = atoi(foo);
256 warn_pass = DEFAULT_WARN_PASS;
258 printf("Days after Password Expiry for Account Locking [%d]: ",DEFAULT_USER_DIE);
260 user_die = atoi(foo);
262 user_die = DEFAULT_USER_DIE;
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]: ");
273 done=bad=correct=(foo[0]=='y'||foo[0]=='Y');
276 printf("\nUser [%s] not added\n",uname);
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);
284 printf("User add failed!\n");
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);
292 printf("There was an error setting password expire values\n");
295 bzero(cmd,sizeof(cmd));
296 sprintf(cmd,"%s %s",PASSWD_PATH,uname);