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
25 #include "tinylogin.h"
33 /* where to start and stop deletion */
39 /* An interesting side-effect of boundary()'s
40 * implementation is that the first user (typically root)
41 * cannot be removed. Let's call it a feature. */
42 static Bounds boundary(const char *buffer, const char *login)
49 snprintf(needle, 256, "\n%s", login);
51 start = strstr(buffer, needle);
59 stop = index(start, '\n'); /* index is a BSD-ism */
60 b.start = start - buffer;
61 b.stop = stop - buffer;
65 /* grep -v ^login (except it only deletes the first match) */
66 /* ...in fact, I think I'm going to simplify this later */
67 static int del_line_matching(char *login, char *filename)
75 /* load into buffer */
76 passwd = fopen(filename, "r");
80 stat(filename, &statbuf);
81 len = statbuf.st_size;
82 buffer = (char *) malloc(len * sizeof(char));
88 fread(buffer, len, sizeof(char), passwd);
92 /* find the user to remove */
93 b = boundary(buffer, login);
99 /* write the file w/o the user */
100 passwd = fopen(filename, "w");
104 fwrite(buffer, (b.start - 1), sizeof(char), passwd);
105 fwrite(&buffer[b.stop], (len - b.stop), sizeof(char), passwd);
112 /* ________________________________________________________________________ */
113 int delgroup_main(int argc, char **argv)
115 /* int successful; */
119 usage(delgroup_usage);
122 failure = del_line_matching(argv[1], GROUP_FILE);
123 #ifdef TLG_FEATURE_SHADOWPASSWDS
124 if (access(GSHADOW_FILE, W_OK) == 0) {
125 /* EDR the |= works if the error is not 0, so he had it wrong */
126 failure |= del_line_matching(argv[1], GSHADOW_FILE);
128 #endif /* TLG_FEATURE_SHADOWPASSWDS */
129 /* if (!successful) { */
131 error_msg_and_die("%s: Group could not be removed\n", argv[1]);
135 return (EXIT_SUCCESS);
138 /* ________________________________________________________________________ */
139 int deluser_main(int argc, char **argv)
141 /* int successful; */
145 usage(deluser_usage);
148 failure = del_line_matching(argv[1], PASSWD_FILE);
149 /* if (!successful) { */
151 error_msg_and_die("%s: User could not be removed from %s\n",
152 argv[1], PASSWD_FILE);
154 #ifdef TLG_FEATURE_SHADOWPASSWDS
155 failure = del_line_matching(argv[1], SHADOW_FILE);
156 /* if (!successful) { */
158 error_msg_and_die("%s: User could not be removed from %s\n",
159 argv[1], SHADOW_FILE);
161 #endif /* TLG_FEATURE_SHADOWPASSWDS */
162 failure = del_line_matching(argv[1], GROUP_FILE);
163 /* if (!successful) { */
165 error_msg_and_die("%s: User could not be removed from %s\n",
166 argv[1], GROUP_FILE);
170 return (EXIT_SUCCESS);
173 /* $Id: deluser.c,v 1.3 2001-08-20 07:47:31 philipc Exp $ */