1/*-
2 * Copyright (c) 1996 by
3 * David Nugent <davidn@blaze.net.au>
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, is permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 *    notice immediately at the beginning of the file, without modification,
11 *    this list of conditions, and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 *    notice, this list of conditions and the following disclaimer in the
14 *    documentation and/or other materials provided with the distribution.
15 * 3. This work was done expressly for inclusion into FreeBSD.  Other use
16 *    is permitted provided this notation is included.
17 * 4. Absolutely no warranty of function or purpose is made by the authors.
18 * 5. Modifications may be freely made to this file providing the above
19 *    conditions are met.
20 *
21 * Login period parsing and comparison functions.
22 */
23
24#include <sys/cdefs.h>
25__FBSDID("$FreeBSD$");
26
27#include <sys/types.h>
28#include <ctype.h>
29#include <login_cap.h>
30#include <stdlib.h>
31#include <string.h>
32#include <time.h>
33
34static struct
35{
36    const char	*dw;
37    u_char       cn;
38    u_char       fl;
39} dws[] =
40{
41    { "su", 2, LTM_SUN }, { "mo", 2, LTM_MON }, { "tu", 2, LTM_TUE },
42    { "we", 2, LTM_WED }, { "th", 2, LTM_THU }, { "fr", 2, LTM_FRI },
43    { "sa", 2, LTM_SAT }, { "any",3, LTM_ANY }, { "all",3, LTM_ANY },
44    { "wk", 2, LTM_WK  }, { "wd", 2, LTM_WD  }, { NULL, 0, 0       }
45};
46
47static char *
48parse_time(char * ptr, u_short * t)
49{
50    u_short	val;
51
52    for (val = 0; *ptr && isdigit(*ptr); ptr++)
53	val = (u_short)(val * 10 + (*ptr - '0'));
54
55    *t = (u_short)((val / 100) * 60 + (val % 100));
56
57    return (ptr);
58}
59
60
61login_time_t
62parse_lt(const char *str)
63{
64    login_time_t    t;
65
66    memset(&t, 0, sizeof t);
67    t.lt_dow = LTM_NONE;
68    if (str && *str && strcmp(str, "Never") != 0 && strcmp(str, "None") != 0) {
69	int		 i;
70	login_time_t	 m = t;
71	char		*p;
72	char		 buf[64];
73
74	/* Make local copy and force lowercase to simplify parsing */
75	strlcpy(buf, str, sizeof buf);
76	for (i = 0; buf[i]; i++)
77	    buf[i] = (char)tolower(buf[i]);
78	p = buf;
79
80	while (isalpha(*p)) {
81
82	    i = 0;
83	    while (dws[i].dw && strncmp(p, dws[i].dw, dws[i].cn) != 0)
84		i++;
85	    if (dws[i].dw == NULL)
86		break;
87	    m.lt_dow |= dws[i].fl;
88	    p += dws[i].cn;
89	}
90
91	if (m.lt_dow == LTM_NONE) /* No (valid) prefix, assume any */
92	    m.lt_dow |= LTM_ANY;
93
94	if (isdigit(*p))
95	    p = parse_time(p, &m.lt_start);
96	else
97	    m.lt_start = 0;
98	if (*p == '-')
99	    p = parse_time(p + 1, &m.lt_end);
100	else
101	    m.lt_end = 1440;
102
103	t = m;
104    }
105    return (t);
106}
107
108
109int
110in_ltm(const login_time_t *ltm, struct tm *tt, time_t *ends)
111{
112    int	    rc = 0;
113
114    if (tt != NULL) {
115	/* First, examine the day of the week */
116	if ((u_char)(0x01 << tt->tm_wday) & ltm->lt_dow) {
117	    /* Convert `current' time to minute of the day */
118	    u_short	now = (u_short)((tt->tm_hour * 60) + tt->tm_min);
119
120	    if (tt->tm_sec > 30)
121		++now;
122	    if (now >= ltm->lt_start && now < ltm->lt_end) {
123		rc = 2;
124		if (ends != NULL) {
125		    /* If requested, return ending time for this period */
126		    tt->tm_hour = (int)(ltm->lt_end / 60);
127		    tt->tm_min  = (int)(ltm->lt_end % 60);
128		    *ends = mktime(tt);
129		}
130	    }
131	}
132    }
133    return (rc);
134}
135
136
137int
138in_lt(const login_time_t *ltm, time_t *t)
139{
140
141    return (in_ltm(ltm, localtime(t), t));
142}
143
144int
145in_ltms(const login_time_t *ltm, struct tm *tm, time_t *t)
146{
147    int	    i = 0;
148
149    while (i < LC_MAXTIMES && ltm[i].lt_dow != LTM_NONE) {
150	if (in_ltm(ltm + i, tm, t))
151	    return (i);
152	i++;
153    }
154    return (-1);
155}
156
157int
158in_lts(const login_time_t *ltm, time_t *t)
159{
160
161    return (in_ltms(ltm, localtime(t), t));
162}
163