File: kill.c

package info (click to toggle)
userinfo 2.5-5
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 1,712 kB
  • sloc: sh: 11,347; ansic: 2,320; makefile: 52
file content (233 lines) | stat: -rw-r--r-- 5,120 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
/*
    Copyright (C) 2001-2011 Ben Kibbey <bjk@luxsci.net>

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02110-1301  USA
*/
/*
 *
 * Try and kill off a users login process(s). This needs the userinfo utility
 * from http://userinfo.sourceforge.net/ with the login.so module loaded
 * and chained (-x) before this module.
 *
 * Compile with:
 *     gcc -O2 -g -Wall -shared -fPIC -o kill.so kill.c
 *
 * Run with:
 *     ui -x login.so -p -- -O ./kill.so --
 *
 * Or with any other options you'd want. Just make sure the first option to
 * the login module is -p.
 *
 * Ben Kibbey <bjk@luxsci.net>
 */
#include <stdio.h>
#include <stdlib.h>
#include <pwd.h>
#include <sys/types.h>
#include <signal.h>
#include <string.h>
#include <ctype.h>
#include <getopt.h>
#include <err.h>
#include <limits.h>

#define KILL_OPTION_STRING	"s:"

void add_string(char ***, char *);

static int *do_sigs;
static int sig_index;

struct signals {
    int sig;
    char *name;
};

static struct signals sigs[] = {
    {1, "SIGHUP"},
    {2, "SIGINT"},
    {3, "SIGQUIT"},
    {4, "SIGILL"},
    {6, "SIGABRT"},
    {8, "SIGFPE"},
    {9, "SIGKILL"},
    {11, "SIGSEGV"},
    {13, "SIGPIPE"},
    {14, "SIGALRM"},
    {15, "SIGTERM"},
#ifdef __i386__
    /* or PPC */
    {10, "SIGUSR1"},
    {12, "SIGUSR2"},
    {17, "SIGCHLD"},
    {18, "SIGCONT"},
    {19, "SIGSTOP"},
    {20, "SIGTSTP"},
    {21, "SIGTTIN"},
    {22, "SIGTTOU"},
#else
    /* Alpha or SPARC. */
    {30, "SIGUSR1"},
    {31, "SIGUSR2"},
    {20, "SIGCHLD"},
    {19, "SIGCONT"},
    {17, "SIGSTOP"},
    {18, "SIGTSTP"},
    {21, "SIGTTIN"},
    {22, "SIGTTOU"},
#endif
    {-1, NULL}
};

void ui_module_init(int *chainable)
{
    do_sigs = realloc(do_sigs, (sig_index + 2) * sizeof(int *));
    do_sigs[sig_index++] = 1;
    do_sigs = realloc(do_sigs, (sig_index + 2) * sizeof(int *));
    do_sigs[sig_index++] = 9;
    *chainable = 1;
    return;
}

void ui_module_exit()
{
    if (do_sigs)
	free(do_sigs);

    return;
}

void ui_module_help()
{
    printf("  Kill a login process; requires login.so -p (-s SIGHUP,9):\n");
    printf("\t-s  Send these comma separated signals (integers or strings).\n\n");
    return;
}

char *ui_module_options_init(char **defaults)
{
    *defaults = NULL;
    return KILL_OPTION_STRING;
}

int ui_module_options(int argc, char **argv)
{
    int opt;

    while ((opt = getopt(argc, argv, KILL_OPTION_STRING)) != -1) {
	char *tmp;

	switch (opt) {
	    case 's':
		sig_index = 0;

again:
		while ((tmp = strsep(&optarg, ",")) != NULL) {
		    int i;
		    int sig;

		    if (isdigit(*tmp)) {
			sig = atoi(tmp);

			for (i = 0; sigs[i].sig != -1; i++) {
			    if (sig == sigs[i].sig) {
				do_sigs = realloc(do_sigs, (sig_index + 2) * sizeof(int *));
				do_sigs[sig_index++] = sig;
				goto again;
			    }
			}
		    }

		    for (i = 0; sigs[i].name; i++) {
			if (strcasecmp(tmp, sigs[i].name) == 0) {
			    do_sigs = realloc(do_sigs, (sig_index + 2) * sizeof(int *));
			    do_sigs[sig_index++] = sigs[i].sig;
			    goto again;
			}
		    }

		    warnx("kill.so: invalid signal -- %s", tmp);
		    return 1;
		}
		break;
	    case '?':
		warnx("kill.so: invalid option -- %c", optopt);
	    default:
		return 1;
	}
    }

    return 0;
}

static int killit(pid_t pid, int sig)
{
    if (kill(pid, 0) == -1)
	return 1;

    if (kill(pid, sig) == -1)
	return 1;

    return 0;
}

int ui_module_exec(char ***result, const struct passwd *pw, const int
	multi, const int verbose, const char *tf)
{
    pid_t pid;
    char **strings = NULL;
    int i;
    char line[LINE_MAX] = {'\0'};
    char *tmp;
    char m[2] = {multi, '\0'};

    strings = *result;

    if (*result == NULL)
	add_string(&strings, "!");
    else {
	/* There may be more than one pid for multiple logins. Separated the
	 * output with the multi-string deliminator and send the signals to each
	 * pid. */
	while ((tmp = strsep(&*result[0], m)) != NULL) {
	    char *s = "!";

	    if (isdigit(*tmp) == 0) {
		strncat(line, "!", sizeof(line)-strlen(line)-1);
		strncat(line, m, sizeof(line)-strlen(line)-1);
	    }
	    else {
		pid = atol(tmp);

		for (i = 0; i < sig_index; i++) {
		    if (killit(pid, do_sigs[i]) == 0) {
			s = "1";
			break;
		    }

		    s = "0";
		}

		strncat(line, s, sizeof(line)-strlen(line)-1);
		strncat(line, m, sizeof(line)-strlen(line)-1);
	    }
	}
    }

    line[strlen(line) - 1] = '\0';
    add_string(&strings, line);
    *result = strings;
    return 0;
}