2 * session.c session management
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 * Copyright 2000 The FreeRADIUS server project
38 #ifdef HAVE_NETINET_IN_H
39 #include <netinet/in.h>
43 #include "rad_assert.h"
46 /* End a session by faking a Stop packet to all accounting modules */
47 int session_zap(REQUEST *request, uint32_t nasaddr, unsigned int port,
49 const char *sessionid, uint32_t cliaddr, char proto)
52 VALUE_PAIR *vp, *userpair;
55 stopreq = request_alloc_fake(request);
56 stopreq->packet->code = PW_ACCOUNTING_REQUEST; /* just to be safe */
57 rad_assert(stopreq != NULL);
59 /* Hold your breath */
60 #define PAIR(n,v,t,e) do { \
61 if(!(vp = paircreate(n, t))) { \
62 request_free(&stopreq); \
63 radlog(L_ERR|L_CONS, "no memory"); \
64 pairfree(&(stopreq->packet->vps)); \
68 pairadd(&(stopreq->packet->vps), vp); \
70 #define INTPAIR(n,v) PAIR(n,v,PW_TYPE_INTEGER,lvalue)
71 #define IPPAIR(n,v) PAIR(n,v,PW_TYPE_IPADDR,lvalue)
72 #define STRINGPAIR(n,v) do { \
73 if(!(vp = paircreate(n, PW_TYPE_STRING))) { \
74 request_free(&stopreq); \
75 radlog(L_ERR|L_CONS, "no memory"); \
76 pairfree(&(stopreq->packet->vps)); \
79 strNcpy((char *)vp->strvalue, v, sizeof vp->strvalue); \
80 vp->length = strlen(v); \
81 pairadd(&(stopreq->packet->vps), vp); \
84 INTPAIR(PW_ACCT_STATUS_TYPE, PW_STATUS_STOP);
85 IPPAIR(PW_NAS_IP_ADDRESS, nasaddr);
86 INTPAIR(PW_ACCT_DELAY_TIME, 0);
87 STRINGPAIR(PW_USER_NAME, user);
89 INTPAIR(PW_NAS_PORT, port);
90 STRINGPAIR(PW_ACCT_SESSION_ID, sessionid);
92 INTPAIR(PW_SERVICE_TYPE, PW_FRAMED_USER);
93 INTPAIR(PW_FRAMED_PROTOCOL, PW_PPP);
94 } else if(proto == 'S') {
95 INTPAIR(PW_SERVICE_TYPE, PW_FRAMED_USER);
96 INTPAIR(PW_FRAMED_PROTOCOL, PW_SLIP);
98 INTPAIR(PW_SERVICE_TYPE, PW_LOGIN_USER); /* A guess, really */
101 IPPAIR(PW_FRAMED_IP_ADDRESS, cliaddr);
102 INTPAIR(PW_ACCT_SESSION_TIME, 0);
103 INTPAIR(PW_ACCT_INPUT_OCTETS, 0);
104 INTPAIR(PW_ACCT_OUTPUT_OCTETS, 0);
105 INTPAIR(PW_ACCT_INPUT_PACKETS, 0);
106 INTPAIR(PW_ACCT_OUTPUT_PACKETS, 0);
108 stopreq->username = userpair;
109 stopreq->password = NULL;
111 ret = rad_accounting(stopreq);
114 * We've got to clean it up by hand, because no one else will.
116 request_free(&stopreq);
123 * Check one terminal server to see if a user is logged in.
125 int rad_check_ts(uint32_t nasaddr, unsigned int portnum, const char *user,
126 const char *session_id)
128 pid_t pid, child_pid;
138 cl = client_find(nasaddr);
141 * Unknown NAS, so trusting radutmp.
143 DEBUG2("checkrad: Unknown NAS %s, not checking",
144 ip_ntoa(address, nasaddr));
149 * No nastype, or nas type 'other', trust radutmp.
151 if ((cl->nastype[0] == '\0') ||
152 (strcmp(cl->nastype, "other") == 0)) {
153 DEBUG2("checkrad: No NAS type, or type \"other\" not checking");
160 if ((pid = rad_fork()) < 0) { /* do wait for the fork'd result */
161 radlog(L_ERR, "Accounting: Failed in fork(): Cannot run checkrad\n");
166 child_pid = rad_waitpid(pid, &status);
169 * It's taking too long. Stop waiting for it.
171 * Don't bother to kill it, as we don't care what
174 if (child_pid == 0) {
175 radlog(L_ERR, "Check-TS: timeout waiting for checkrad");
180 radlog(L_ERR, "Check-TS: unknown error in waitpid()");
184 return WEXITSTATUS(status);
190 * We don't close fd's 0, 1, and 2. If we're in debugging mode,
191 * then they should go to stdout (etc), along with the other
192 * server log messages.
194 * If we're not in debugging mode, then the code in radiusd.c
195 * takes care of connecting fd's 0, 1, and 2 to /dev/null.
198 ip_ntoa(address, nasaddr);
199 snprintf(port, 11, "%u", portnum);
202 /* OS/2 can't directly execute scripts then we call the command
203 processor to execute checkrad
205 execl(getenv("COMSPEC"), "", "/C","checkrad", cl->nastype, address, port,
206 user, session_id, NULL);
208 execl(mainconfig.checkrad, "checkrad", cl->nastype, address, port,
209 user, session_id, NULL);
211 radlog(L_ERR, "Check-TS: exec %s: %s", mainconfig.checkrad, strerror(errno));
214 * Exit - 2 means "some error occured".