1 /* makeweb.c - let a user create a web subdirectory
3 ** Copyright © 1995 by Jef Poskanzer <jef@mail.acme.com>.
4 ** All rights reserved.
6 ** Redistribution and use in source and binary forms, with or without
7 ** modification, are permitted provided that the following conditions
9 ** 1. Redistributions of source code must retain the above copyright
10 ** notice, this list of conditions and the following disclaimer.
11 ** 2. Redistributions in binary form must reproduce the above copyright
12 ** notice, this list of conditions and the following disclaimer in the
13 ** documentation and/or other materials provided with the distribution.
15 ** THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 ** ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 ** IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 ** ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 ** FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 ** DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 ** OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 ** HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 ** LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 ** OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 /* This is intended to be installed setgid to a group that has
29 ** write access to the system web directory. It allows any user
30 ** to create a subdirectory there. It also makes a symbolic link
31 ** in the user's home directory pointing at the new web subdir.
35 #include "../config.h"
43 #include <sys/types.h>
47 #define LINK "public_html"
53 check_room( int size, int len )
57 (void) fprintf( stderr, "%s: internal error, out of room\n", argv0 );
64 end_with_slash( char* str )
66 if ( str[strlen( str ) - 1] != '/' )
67 (void) strcat( str, "/" );
72 check_dir( char* dirname, uid_t uid, gid_t gid )
76 /* Check the directory. */
77 if ( stat( dirname, &sb ) < 0 )
79 if ( errno != ENOENT )
84 /* Doesn't exist. Try to make it. */
85 if ( mkdir( dirname, 0755 ) < 0 )
87 if ( errno == ENOENT )
89 Some part of the path %s does not exist.\n\
90 This is probably a configuration error.\n", dirname );
95 (void) printf( "Created web directory %s\n", dirname );
96 /* Try to change the group of the new dir to the user's group. */
97 (void) chown( dirname, -1, gid );
101 /* The directory already exists. Well, check that it is in
104 if ( ! S_ISDIR( sb.st_mode ) )
107 "%s already exists but is not a directory!\n", dirname );
110 if ( sb.st_uid != uid )
113 "%s already exists but you don't own it!\n", dirname );
116 (void) printf( "Web directory %s already existed.\n", dirname );
122 main( int argc, char** argv )
137 (void) fprintf( stderr, "usage: %s\n", argv0 );
141 pwd = getpwuid( getuid() );
142 if ( pwd == (struct passwd*) 0 )
144 (void) fprintf( stderr, "%s: can't find your username\n", argv0 );
147 username = pwd->pw_name;
148 homedir = pwd->pw_dir;
152 /* All we have to do for the TILDE_MAP_2 case is make sure there's
153 ** a public_html subdirectory.
156 sizeof(dirname), strlen( homedir ) + strlen( TILDE_MAP_2 ) + 2 );
157 (void) strcpy( dirname, homedir );
158 end_with_slash( dirname );
159 (void) strcat( dirname, TILDE_MAP_2 );
161 check_dir( dirname, pwd->pw_uid, pwd->pw_gid );
163 #else /* TILDE_MAP_2 */
165 /* Gather the pieces. */
168 prefix = TILDE_MAP_1;
169 #else /* TILDE_MAP_1 */
171 #endif /* TILDE_MAP_1 */
173 /* Assemble the directory name. Be paranoid cause we're sgid. */
176 strlen( webdir ) + strlen( prefix ) + strlen( username ) + 3 );
177 (void) strcpy( dirname, webdir );
178 end_with_slash( dirname );
179 if ( strlen( prefix ) != 0 )
181 (void) strcat( dirname, prefix );
182 end_with_slash( dirname );
184 (void) strcat( dirname, username );
186 /* Assemble the link name. */
187 check_room( sizeof(linkname), strlen( homedir ) + strlen( LINK ) + 2 );
188 (void) strcpy( linkname, homedir );
189 end_with_slash( linkname );
190 (void) strcat( linkname, LINK );
192 check_dir( dirname, pwd->pw_uid, pwd->pw_gid );
194 /* Check the symlink. */
196 if ( lstat( linkname, &sb ) < 0 )
198 if ( errno != ENOENT )
203 /* Doesn't exist. Try to make it. */
204 if ( symlink( dirname, linkname ) < 0 )
206 if ( errno == ENOENT )
208 Some part of the path %s does not exist.\n\
209 This is probably a configuration error.\n", linkname );
214 (void) printf( "Created symbolic link %s\n", linkname );
218 /* The link already exists. Well, check that it is in
221 if ( ! S_ISLNK( sb.st_mode ) )
224 %s already exists but is not a\n\
225 symbolic link! Perhaps you have a real web subdirectory in your\n\
226 home dir from a previous web server configuration? You may have\n\
227 to rename it, run %s again, and then copy in the old\n\
228 contents.\n", linkname, argv0 );
231 /* Check the existing link's contents. */
232 if ( readlink( linkname, linkbuf, sizeof(linkbuf) ) < 0 )
237 if ( strcmp( dirname, linkbuf ) == 0 )
238 (void) printf( "Symbolic link %s already existed.\n", linkname );
242 Symbolic link %s already existed\n\
243 but it points to the wrong place! Attempting to remove and\n\
244 recreate it.\n", linkname );
245 if ( unlink( linkname ) < 0 )
253 #endif /* TILDE_MAP_2 */