| 1 | /* |
|---|
| 2 | * ProFTPD - FTP server daemon |
|---|
| 3 | * Copyright (c) 2009 The ProFTPD Project team |
|---|
| 4 | * |
|---|
| 5 | * This program is free software; you can redistribute it and/or modify |
|---|
| 6 | * it under the terms of the GNU General Public License as published by |
|---|
| 7 | * the Free Software Foundation; either version 2 of the License, or |
|---|
| 8 | * (at your option) any later version. |
|---|
| 9 | * |
|---|
| 10 | * This program is distributed in the hope that it will be useful, |
|---|
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 13 | * GNU General Public License for more details. |
|---|
| 14 | * |
|---|
| 15 | * You should have received a copy of the GNU General Public License |
|---|
| 16 | * along with this program; if not, write to the Free Software |
|---|
| 17 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. |
|---|
| 18 | * |
|---|
| 19 | * As a special exemption, The ProFTPD Project team and other respective |
|---|
| 20 | * copyright holders give permission to link this program with OpenSSL, and |
|---|
| 21 | * distribute the resulting executable, without including the source code for |
|---|
| 22 | * OpenSSL in the source distribution. |
|---|
| 23 | * |
|---|
| 24 | * $Id: session.c,v 1.6 2009/09/14 20:44:12 castaglia Exp $ |
|---|
| 25 | */ |
|---|
| 26 | |
|---|
| 27 | #include "conf.h" |
|---|
| 28 | |
|---|
| 29 | const char *pr_session_get_protocol(int flags) { |
|---|
| 30 | const char *sess_proto; |
|---|
| 31 | |
|---|
| 32 | sess_proto = pr_table_get(session.notes, "protocol", NULL); |
|---|
| 33 | if (sess_proto == NULL) { |
|---|
| 34 | sess_proto = "ftp"; |
|---|
| 35 | } |
|---|
| 36 | |
|---|
| 37 | if (!(flags & PR_SESS_PROTO_FL_LOGOUT)) { |
|---|
| 38 | /* Return the protocol as is. */ |
|---|
| 39 | return sess_proto; |
|---|
| 40 | } |
|---|
| 41 | |
|---|
| 42 | /* Otherwise, we need to return either "FTP" or "SSH2", for consistency. */ |
|---|
| 43 | if (strcmp(sess_proto, "ftp") == 0 || |
|---|
| 44 | strcmp(sess_proto, "ftps") == 0) { |
|---|
| 45 | return "FTP"; |
|---|
| 46 | |
|---|
| 47 | } else if (strcmp(sess_proto, "ssh2") == 0 || |
|---|
| 48 | strcmp(sess_proto, "sftp") == 0 || |
|---|
| 49 | strcmp(sess_proto, "scp") == 0 || |
|---|
| 50 | strcmp(sess_proto, "publickey") == 0) { |
|---|
| 51 | return "SSH2"; |
|---|
| 52 | } |
|---|
| 53 | |
|---|
| 54 | /* Should never reach here, but just in case... */ |
|---|
| 55 | return "unknown"; |
|---|
| 56 | } |
|---|
| 57 | |
|---|
| 58 | int pr_session_set_idle(void) { |
|---|
| 59 | char *user = NULL; |
|---|
| 60 | |
|---|
| 61 | pr_scoreboard_entry_update(session.pid, |
|---|
| 62 | PR_SCORE_BEGIN_IDLE, time(NULL), |
|---|
| 63 | PR_SCORE_CMD, "%s", "idle", NULL, NULL); |
|---|
| 64 | |
|---|
| 65 | pr_scoreboard_entry_update(session.pid, |
|---|
| 66 | PR_SCORE_CMD_ARG, "%s", "", NULL, NULL); |
|---|
| 67 | |
|---|
| 68 | if (session.user) { |
|---|
| 69 | user = session.user; |
|---|
| 70 | |
|---|
| 71 | } else { |
|---|
| 72 | user = "(authenticating)"; |
|---|
| 73 | } |
|---|
| 74 | |
|---|
| 75 | pr_proctitle_set("%s - %s: IDLE", user, session.proc_prefix); |
|---|
| 76 | return 0; |
|---|
| 77 | } |
|---|
| 78 | |
|---|
| 79 | int pr_session_set_protocol(const char *sess_proto) { |
|---|
| 80 | int count, res; |
|---|
| 81 | |
|---|
| 82 | if (sess_proto == NULL) { |
|---|
| 83 | errno = EINVAL; |
|---|
| 84 | return -1; |
|---|
| 85 | } |
|---|
| 86 | |
|---|
| 87 | count = pr_table_exists(session.notes, "protocol"); |
|---|
| 88 | if (count > 0) { |
|---|
| 89 | res = pr_table_set(session.notes, pstrdup(session.pool, "protocol"), |
|---|
| 90 | pstrdup(session.pool, sess_proto), 0); |
|---|
| 91 | |
|---|
| 92 | if (res == 0) { |
|---|
| 93 | /* Update the scoreboard entry for this session with the protocol. */ |
|---|
| 94 | pr_scoreboard_entry_update(session.pid, PR_SCORE_PROTOCOL, sess_proto, |
|---|
| 95 | NULL); |
|---|
| 96 | } |
|---|
| 97 | |
|---|
| 98 | return res; |
|---|
| 99 | } |
|---|
| 100 | |
|---|
| 101 | res = pr_table_add(session.notes, pstrdup(session.pool, "protocol"), |
|---|
| 102 | pstrdup(session.pool, sess_proto), 0); |
|---|
| 103 | |
|---|
| 104 | if (res == 0) { |
|---|
| 105 | /* Update the scoreboard entry for this session with the protocol. */ |
|---|
| 106 | pr_scoreboard_entry_update(session.pid, PR_SCORE_PROTOCOL, sess_proto, |
|---|
| 107 | NULL); |
|---|
| 108 | } |
|---|
| 109 | |
|---|
| 110 | return res; |
|---|
| 111 | } |
|---|
| 112 | |
|---|
| 113 | static const char *sess_ttyname = NULL; |
|---|
| 114 | |
|---|
| 115 | const char *pr_session_get_ttyname(pool *p) { |
|---|
| 116 | char ttybuf[32]; |
|---|
| 117 | const char *sess_proto, *tty_proto = NULL; |
|---|
| 118 | |
|---|
| 119 | if (p == NULL) { |
|---|
| 120 | errno = EINVAL; |
|---|
| 121 | return NULL; |
|---|
| 122 | } |
|---|
| 123 | |
|---|
| 124 | if (sess_ttyname) { |
|---|
| 125 | /* Return the cached name. */ |
|---|
| 126 | return pstrdup(p, sess_ttyname); |
|---|
| 127 | } |
|---|
| 128 | |
|---|
| 129 | sess_proto = pr_table_get(session.notes, "protocol", NULL); |
|---|
| 130 | if (sess_proto) { |
|---|
| 131 | if (strcmp(sess_proto, "ftp") == 0 || |
|---|
| 132 | strcmp(sess_proto, "ftps") == 0) { |
|---|
| 133 | #if (defined(BSD) && (BSD >= 199103)) |
|---|
| 134 | tty_proto = "ftp"; |
|---|
| 135 | #else |
|---|
| 136 | tty_proto = "ftpd"; |
|---|
| 137 | #endif |
|---|
| 138 | |
|---|
| 139 | } else if (strcmp(sess_proto, "ssh2") == 0 || |
|---|
| 140 | strcmp(sess_proto, "sftp") == 0 || |
|---|
| 141 | strcmp(sess_proto, "scp") == 0 || |
|---|
| 142 | strcmp(sess_proto, "publickey") == 0) { |
|---|
| 143 | |
|---|
| 144 | /* Just use the plain "ssh" string for the tty name for these cases. */ |
|---|
| 145 | tty_proto = "ssh"; |
|---|
| 146 | |
|---|
| 147 | /* Cache the originally constructed tty name for any later retrievals. */ |
|---|
| 148 | sess_ttyname = pstrdup(session.pool, tty_proto); |
|---|
| 149 | return pstrdup(p, sess_ttyname); |
|---|
| 150 | } |
|---|
| 151 | } |
|---|
| 152 | |
|---|
| 153 | if (tty_proto == NULL) { |
|---|
| 154 | #if (defined(BSD) && (BSD >= 199103)) |
|---|
| 155 | tty_proto = "ftp"; |
|---|
| 156 | #else |
|---|
| 157 | tty_proto = "ftpd"; |
|---|
| 158 | #endif |
|---|
| 159 | } |
|---|
| 160 | |
|---|
| 161 | memset(ttybuf, '\0', sizeof(ttybuf)); |
|---|
| 162 | #if (defined(BSD) && (BSD >= 199103)) |
|---|
| 163 | snprintf(ttybuf, sizeof(ttybuf), "%s%ld", tty_proto, |
|---|
| 164 | (long) (session.pid ? session.pid : getpid())); |
|---|
| 165 | #else |
|---|
| 166 | snprintf(ttybuf, sizeof(ttybuf), "%s%d", tty_proto, |
|---|
| 167 | (int) (session.pid ? session.pid : getpid())); |
|---|
| 168 | #endif |
|---|
| 169 | |
|---|
| 170 | /* Cache the originally constructed tty name for any later retrievals. */ |
|---|
| 171 | sess_ttyname = pstrdup(session.pool, ttybuf); |
|---|
| 172 | |
|---|
| 173 | return pstrdup(p, sess_ttyname); |
|---|
| 174 | } |
|---|