Initial commit for Tizen
[profile/extras/shadow-utils.git] / contrib / shadow-anonftp.patch
1 Hello Marek,
2
3 I have created a diffile against the 980403 release that adds
4 functionality to newusers for automatic handling of users with only
5 anonomous ftp login (using the guestgroup feature in ftpaccess, which
6 means that the users home directory looks like '/home/user/./'). It also
7 adds a commandline argument to specify an initial directory structure
8 for such users, with a tarball normally containing the bin,lib,etc
9 directories used in the chrooted environment.
10
11 I am using it to automatically create chunks of users with only ftp
12 access for a webserver.
13
14 I have tried to follow your coding standards and I believe it is bug
15 free but.. well, who knows. :) It's not much code however.
16
17 I hope you find it useful. Do what you like with it, feel free to ask if
18 anything is unclear.
19
20 Best rgds,
21   Calle Karlsson
22   ckn@kash.se
23
24 diff -uNr shadow-980403.orig/src/newusers.c shadow-980403/src/newusers.c
25 --- shadow-980403.orig/src/newusers.c   Fri Jan 30 00:22:43 1998
26 +++ shadow-980403/src/newusers.c        Fri Apr 17 16:55:33 1998
27 @@ -76,11 +76,35 @@
28  static void
29  usage(void)
30  {
31 -       fprintf(stderr, "Usage: %s [ input ]\n", Prog);
32 +       fprintf (stderr, "Usage: %s [-p prototype tarfile] [ input ]\n", Prog);
33 +       fprintf (stderr, "The prototype tarfile is only used for users\n");
34 +       fprintf (stderr, "marked as anonymous ftp users. It must be a full pathname.\n");
35         exit(1);
36  }
37  
38  /*
39 + * createuserdir - create a directory and chmod it
40 + */
41 +
42 +static int
43 +createuserdir (char * dir, int uid, int gid, int line)
44 +{
45 +       if (mkdir (dir, 0777 & ~getdef_num("UMASK", 077))) {
46 +               fprintf (stderr, "%s: line %d: mkdir %s failed\n",
47 +                        Prog, line, dir);
48 +               return -1;
49 +       }
50 +
51 +       if (chown (dir, uid, gid)) {
52 +               fprintf (stderr, "%s: line %d: chown %s failed\n",
53 +                        Prog, line, dir);
54 +               return -1;
55 +       }
56 +
57 +       return 0;
58 +}
59 +
60 +/*
61   * add_group - create a new group or add a user to an existing group
62   */
63  
64 @@ -328,6 +352,8 @@
65  main(int argc, char **argv)
66  {
67         char    buf[BUFSIZ];
68 +       char    anonproto[BUFSIZ];
69 +       int     flag;
70         char    *fields[8];
71         int     nfields;
72         char    *cp;
73 @@ -340,12 +366,23 @@
74  
75         Prog = Basename(argv[0]);
76  
77 -       if (argc > 1 && argv[1][0] == '-')
78 -               usage ();
79 +       * anonproto = '\0';
80 +
81 +       while ((flag = getopt (argc, argv, "p:h")) != EOF) {
82 +               switch (flag) {
83 +               case 'p':
84 +                       STRFCPY(anonproto, optarg);
85 +                       break;
86 +               case 'h':
87 +               default:
88 +                       usage ();
89 +                       break;
90 +               }
91 +       }
92  
93 -       if (argc == 2) {
94 -               if (! freopen (argv[1], "r", stdin)) {
95 -                       snprintf(buf, sizeof buf, "%s: %s", Prog, argv[1]);
96 +       if (optind < argc) {
97 +               if (! freopen (argv[optind], "r", stdin)) {
98 +                       snprintf(buf, sizeof buf, "%s: %s", Prog, argv[optind]);
99                         perror (buf);
100                         exit (1);
101                 }
102 @@ -499,15 +536,36 @@
103                 if (fields[6][0])
104                         newpw.pw_shell = fields[6];
105  
106 -               if (newpw.pw_dir[0] && access(newpw.pw_dir, F_OK)) {
107 -                       if (mkdir (newpw.pw_dir,
108 -                                       0777 & ~getdef_num("UMASK", 077)))
109 -                               fprintf (stderr, "%s: line %d: mkdir failed\n",
110 -                                       Prog, line);
111 -                       else if (chown (newpw.pw_dir,
112 -                                       newpw.pw_uid, newpw.pw_gid))
113 -                               fprintf (stderr, "%s: line %d: chown failed\n",
114 -                                       Prog, line);
115 +               if (newpw.pw_dir[0]) {
116 +                       char * userdir = strdup (newpw.pw_dir);
117 +                       char * anonpart;
118 +                       int rc;
119 +
120 +                       if ((anonpart = strstr (userdir, "/./"))) {
121 +                               * anonpart = '\0';
122 +                               anonpart += 2;
123 +                       }
124 +                       
125 +                       if (access(userdir, F_OK))
126 +                               rc = createuserdir (userdir, newpw.pw_uid, newpw.pw_gid, line);
127 +                       else
128 +                               rc = 0;
129 +
130 +                       if (rc == 0 && anonpart) {
131 +                               if (* anonproto) {
132 +                                       char cmdbuf [BUFSIZ];
133 +                                       snprintf(cmdbuf, sizeof cmdbuf,
134 +                                                "cd %s; tar xf %s",
135 +                                                userdir, anonproto);
136 +                                       system (cmdbuf);
137 +                               }
138 +                               if (strlen (anonpart) > 1) {
139 +                                       strcat (userdir, anonpart);
140 +                                       if (access (userdir, F_OK))
141 +                                               createuserdir (userdir, newpw.pw_uid, newpw.pw_gid, line);
142 +                               }
143 +                       }
144 +                       free (userdir);
145                 }
146  
147                 /*