1 /* vi: set sw=4 ts=4: */
3 * deluser (remove lusers from the system ;) for TinyLogin
6 * Copyright (C) 1999 by Lineo, inc.
7 * Written by John Beppu <beppu@lineo.com>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
32 #define PASSWD_FILE "/etc/passwd"
33 #define GROUP_FILE "/etc/group"
35 /* where to start and stop deletion */
41 /* An interesting side-effect of boundary()'s
42 * implementation is that the first user (typically root)
43 * cannot be removed. Let's call it a feature. */
44 static Bounds boundary(const char *buffer, const char *login)
51 snprintf(needle, 256, "\n%s", login);
53 start = strstr(buffer, needle);
61 stop = index(start, '\n'); /* index is a BSD-ism */
62 b.start = start - buffer;
63 b.stop = stop - buffer;
67 /* grep -v ^login (except it only deletes the first match) */
68 /* ...in fact, I think I'm going to simplify this later */
69 static int del_line_matching(char *login, char *filename)
77 /* load into buffer */
78 passwd = fopen(filename, "r");
82 stat(filename, &statbuf);
83 len = statbuf.st_size;
84 buffer = (char *) malloc(len * sizeof(char));
90 fread(buffer, len, sizeof(char), passwd);
94 /* find the user to remove */
95 b = boundary(buffer, login);
101 /* write the file w/o the user */
102 passwd = fopen(filename, "w");
106 fwrite(buffer, (b.start - 1), sizeof(char), passwd);
107 fwrite(&buffer[b.stop], (len - b.stop), sizeof(char), passwd);
114 /* ________________________________________________________________________ */
115 int delgroup_main(int argc, char **argv)
117 /* int successful; */
124 failure = del_line_matching(argv[1], GROUP_FILE);
125 #ifdef TLG_FEATURE_SHADOWPASSWDS
126 if (access(GSHADOW_FILE, W_OK) == 0) {
127 /* EDR the |= works if the error is not 0, so he had it wrong */
128 failure |= del_line_matching(argv[1], GSHADOW_FILE);
130 #endif /* TLG_FEATURE_SHADOWPASSWDS */
131 /* if (!successful) { */
133 error_msg_and_die("%s: Group could not be removed\n", argv[1]);
137 return (EXIT_SUCCESS);
140 /* ________________________________________________________________________ */
141 int deluser_main(int argc, char **argv)
143 /* int successful; */
150 failure = del_line_matching(argv[1], PASSWD_FILE);
151 /* if (!successful) { */
153 error_msg_and_die("%s: User could not be removed from %s\n",
154 argv[1], PASSWD_FILE);
156 #ifdef TLG_FEATURE_SHADOWPASSWDS
157 failure = del_line_matching(argv[1], SHADOW_FILE);
158 /* if (!successful) { */
160 error_msg_and_die("%s: User could not be removed from %s\n",
161 argv[1], SHADOW_FILE);
163 #endif /* TLG_FEATURE_SHADOWPASSWDS */
164 failure = del_line_matching(argv[1], GROUP_FILE);
165 /* if (!successful) { */
167 error_msg_and_die("%s: User could not be removed from %s\n",
168 argv[1], GROUP_FILE);
172 return (EXIT_SUCCESS);
175 /* $Id: deluser.c,v 1.1 2001/08/21 16:18:59 andersen Exp $ */