summaryrefslogtreecommitdiffstats
path: root/lib/libutil/login_ok.c
blob: 4c4724e62120c991ff3c2ee5939488ec0b74ed4b (plain)
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
/*-
 * Copyright (c) 1996 by
 * David Nugent <davidn@blaze.net.au>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, is permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice immediately at the beginning of the file, without modification,
 *    this list of conditions, and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. This work was done expressly for inclusion into FreeBSD.  Other use
 *    is permitted provided this notation is included.
 * 4. Absolutely no warranty of function or purpose is made by the authors.
 * 5. Modifications may be freely made to this file providing the above
 *    conditions are met.
 *
 * Support allow/deny lists in login class capabilities
 *
 * $FreeBSD$
 */

#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <ttyent.h>
#include <fnmatch.h>

#include <sys/types.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <sys/param.h>
#include <login_cap.h>


/* -- support functions -- */

/*
 * login_strinlist()
 * This function is intentionally public - reused by TAS.
 * Returns TRUE (non-zero) if a string matches a pattern
 * in a given array of patterns. 'flags' is passed directly
 * to fnmatch(3).
 */

int
login_strinlist(char **list, char const *str, int flags)
{
    int rc = 0;

    if (str != NULL && *str != '\0') {
	int	i = 0;

	while (rc == 0 && list[i] != NULL)
	    rc = fnmatch(list[i++], str, flags) == 0;
    }
    return rc;
}


/*
 * login_str2inlist()
 * Locate either or two strings in a given list
 */

int
login_str2inlist(char **ttlst, const char *str1, const char *str2, int flags)
{
    int	    rc = 0;

    if (login_strinlist(ttlst, str1, flags))
	rc = 1;
    else if (login_strinlist(ttlst, str2, flags))
	rc = 1;
    return rc;
}


/*
 * login_timelist()
 * This function is intentinoally public - reused by TAS.
 * Returns an allocated list of time periods given an array
 * of time periods in ascii form.
 */

login_time_t *
login_timelist(login_cap_t *lc, char const *cap, int *ltno,
	       login_time_t **ltptr)
{
    int			j = 0;
    struct login_time	*lt = NULL;
    char		**tl;

    if ((tl = login_getcaplist(lc, cap, NULL)) != NULL) {

	while (tl[j++] != NULL)
	    ;
	if (*ltno >= j)
	    lt = *ltptr;
	else if ((lt = realloc(*ltptr, j * sizeof(struct login_time))) != NULL) {
	    *ltno = j;
	    *ltptr = lt;
	}
	if (lt != NULL) {
	    int	    i = 0;

	    for (--j; i < j; i++)
		lt[i] = parse_lt(tl[i]);
	    lt[i].lt_dow = LTM_NONE;
	}
    }
    return lt;
}


/*
 * login_ttyok()
 * This function is a variation of auth_ttyok(), but it checks two
 * arbitrary capability lists not necessarily related to access.
 * This hook is provided for the accounted/exclude accounting lists.
 */

int
login_ttyok(login_cap_t *lc, const char *tty, const char *allowcap,
	    const char *denycap)
{
    int	    rc = 1;

    if (lc != NULL && tty != NULL && *tty != '\0') {
	struct ttyent	*te;
	char		*grp;
	char		**ttl;

	te = getttynam(tty);  /* Need group name */
	grp = te ? te->ty_group : NULL;
	ttl = login_getcaplist(lc, allowcap, NULL);

	if (ttl != NULL && !login_str2inlist(ttl, tty, grp, 0))
	    rc = 0;	/* tty or ttygroup not in allow list */
	else {

	    ttl = login_getcaplist(lc, denycap, NULL);
	    if (ttl != NULL && login_str2inlist(ttl, tty, grp, 0))
		rc = 0; /* tty or ttygroup in deny list */
	}
    }

    return rc;
}


/*
 * auth_ttyok()
 * Determine whether or not login on a tty is accessible for
 * a login class
 */

int
auth_ttyok(login_cap_t *lc, const char * tty)
{
    return login_ttyok(lc, tty, "ttys.allow", "ttys.deny");
}


/*
 * login_hostok()
 * This function is a variation of auth_hostok(), but it checks two
 * arbitrary capability lists not necessarily related to access.
 * This hook is provided for the accounted/exclude accounting lists.
 */

int
login_hostok(login_cap_t *lc, const char *host, const char *ip,
	     const char *allowcap, const char *denycap)
{
    int	    rc = 1; /* Default is ok */

    if (lc != NULL &&
	((host != NULL && *host != '\0') || (ip != NULL && *ip != '\0'))) {
	char	**hl;

	hl = login_getcaplist(lc, allowcap, NULL);
	if (hl != NULL && !login_str2inlist(hl, host, ip, FNM_CASEFOLD))
	    rc = 0;	/* host or IP not in allow list */
	else {

	    hl = login_getcaplist(lc, denycap, NULL);
	    if (hl != NULL && login_str2inlist(hl, host, ip, FNM_CASEFOLD))
		rc = 0; /* host or IP in deny list */
	}
    }

    return rc;
}


/*
 * auth_hostok()
 * Determine whether or not login from a host is ok
 */

int
auth_hostok(login_cap_t *lc, const char *host, const char *ip)
{
    return login_hostok(lc, host, ip, "host.allow", "host.deny");
}


/*
 * auth_timeok()
 * Determine whether or not login is ok at a given time
 */

int
auth_timeok(login_cap_t *lc, time_t t)
{
    int	    rc = 1; /* Default is ok */

    if (lc != NULL && t != (time_t)0 && t != (time_t)-1) {
	struct tm	*tptr;

	static int 	ltimesno = 0;
	static struct login_time *ltimes = NULL;

	if ((tptr = localtime(&t)) != NULL) {
	    struct login_time	*lt;

	  lt = login_timelist(lc, "times.allow", &ltimesno, &ltimes);
	  if (lt != NULL && in_ltms(lt, tptr, NULL) == -1)
	      rc = 0;	  /* not in allowed times list */
	  else {

	      lt = login_timelist(lc, "times.deny", &ltimesno, &ltimes);
	      if (lt != NULL && in_ltms(lt, tptr, NULL) != -1)
		  rc = 0; /* in deny times list */
	  }
	  if (ltimes) {
	      free(ltimes);
	      ltimes = NULL;
	      ltimesno = 0;
	  }
	}
    }

    return rc;
}
OpenPOWER on IntegriCloud