Dump entries can have the same timestamp, so accept those as well.
[dragonfly.git] / sys / netproto / ncp / ncp_login.c
blobea142ce6994e63c45199bced5c0acd3928d3af9a
1 /*
2 * Copyright (c) 1999, Boris Popov
3 * All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by Boris Popov.
16 * 4. Neither the name of the author nor the names of any co-contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
32 * $FreeBSD: src/sys/netncp/ncp_login.c,v 1.2 1999/10/12 10:36:59 bp Exp $
33 * $DragonFly: src/sys/netproto/ncp/ncp_login.c,v 1.8 2006/12/22 23:57:54 swildner Exp $
35 #include <sys/param.h>
36 #include <sys/errno.h>
37 #include <sys/malloc.h>
38 #include <sys/systm.h>
39 #include <sys/proc.h>
40 #include <sys/socket.h>
42 #include "ncp.h"
43 #include "ncp_conn.h"
44 #include "ncp_subr.h"
45 #include "ncp_ncp.h"
46 #include "ncp_rq.h"
47 #include "ncp_nls.h"
48 #include "nwerror.h"
50 static int ncp_login_encrypted(struct ncp_conn *conn, struct ncp_bindery_object *object,
51 unsigned char *key, unsigned char *passwd,
52 struct thread *td, struct ucred *cred);
53 static int ncp_login_unencrypted(struct ncp_conn *conn, u_int16_t object_type,
54 char *object_name, unsigned char *passwd,
55 struct thread *td, struct ucred *cred);
56 static int ncp_sign_start(struct ncp_conn *conn, char *logindata);
57 static int ncp_get_encryption_key(struct ncp_conn *conn, char *target);
60 * Initialize packet signatures. They a slightly modified MD4.
61 * The first 16 bytes of logindata are the shuffled password,
62 * the last 8 bytes the encryption key as received from the server.
64 int
65 ncp_sign_start(struct ncp_conn *conn, char *logindata) {
66 char msg[64];
67 u_int32_t state[4];
69 memcpy(msg, logindata, 24);
70 memcpy(msg + 24, "Authorized NetWare Client", 25);
71 bzero(msg + 24 + 25, sizeof(msg) - 24 - 25);
73 conn->sign_state[0] = 0x67452301;
74 conn->sign_state[1] = 0xefcdab89;
75 conn->sign_state[2] = 0x98badcfe;
76 conn->sign_state[3] = 0x10325476;
77 ncp_sign(conn->sign_state, msg, state);
78 conn->sign_root[0] = state[0];
79 conn->sign_root[1] = state[1];
80 conn->flags |= NCPFL_SIGNACTIVE;
81 return 0;
85 * target is a 8-byte buffer
87 int
88 ncp_get_encryption_key(struct ncp_conn *conn, char *target) {
89 int error;
90 DECLARE_RQ;
92 NCP_RQ_HEAD_S(23, 23, conn->td, conn->ucred);
93 checkbad(ncp_request(conn,rqp));
94 if (rqp->rpsize < 8) {
95 NCPFATAL("rpsize=%d < 8\n", rqp->rpsize);
96 return EIO;
98 ncp_rp_mem(rqp, target, 8);
99 NCP_RQ_EXIT;
100 return error;
104 ncp_login_object(struct ncp_conn *conn, unsigned char *username,
105 int login_type, unsigned char *password,
106 struct thread *td,struct ucred *cred)
108 int error;
109 unsigned char ncp_key[8];
110 struct ncp_bindery_object user;
112 if ((error = ncp_get_encryption_key(conn, ncp_key)) != 0) {
113 kprintf("%s: Warning: use unencrypted login\n", __func__);
114 return ncp_login_unencrypted(conn, login_type, username, password,td,cred);
116 if ((error = ncp_get_bindery_object_id(conn, login_type, username, &user,td,cred)) != 0) {
117 return error;
119 error = ncp_login_encrypted(conn, &user, ncp_key, password,td,cred);
120 return error;
124 ncp_login_encrypted(struct ncp_conn *conn, struct ncp_bindery_object *object,
125 unsigned char *key, unsigned char *passwd,
126 struct thread *td,struct ucred *cred) {
127 u_int32_t tmpID = htonl(object->object_id);
128 u_char buf[16 + 8];
129 u_char encrypted[8];
130 int error;
131 DECLARE_RQ;
133 nw_keyhash((u_char*)&tmpID, passwd, strlen(passwd), buf);
134 nw_encrypt(key, buf, encrypted);
136 NCP_RQ_HEAD_S(23,24,td,cred);
137 ncp_rq_mem(rqp, encrypted, 8);
138 ncp_rq_word_hl(rqp, object->object_type);
139 ncp_rq_pstring(rqp, object->object_name);
140 error = ncp_request(conn, rqp);
141 NCP_RQ_EXIT_NB;
142 if (conn->flags & NCPFL_SIGNWANTED) {
143 if (error == 0 || error == NWE_PASSWORD_EXPIRED) {
144 memcpy(buf + 16, key, 8);
145 error = ncp_sign_start(conn, buf);
148 return error;
152 ncp_login_unencrypted(struct ncp_conn *conn, u_int16_t object_type,
153 char *object_name, unsigned char *passwd,
154 struct thread *td, struct ucred *cred)
156 int error;
157 DECLARE_RQ;
159 NCP_RQ_HEAD_S(23, 20, conn->td, conn->ucred);
160 ncp_rq_word_hl(rqp, object_type);
161 ncp_rq_pstring(rqp, object_name);
162 ncp_rq_pstring(rqp, passwd);
163 error = ncp_request(conn,rqp);
164 NCP_RQ_EXIT_NB;
165 return error;
169 * Login to specified server with username and password.
170 * conn should be locked.
173 ncp_login(struct ncp_conn *conn, const char *user, int objtype,
174 const char *password, struct thread *td, struct ucred *cred)
176 int error;
178 if (ncp_suser(cred) != 0 && cred->cr_uid != conn->nc_owner->cr_uid)
179 return EACCES;
180 if (conn->flags & NCPFL_LOGGED) return EALREADY;
181 if ((conn->flags & NCPFL_ATTACHED) == 0) return ENOTCONN;
182 conn->li.user = ncp_str_dup(user);
183 conn->li.password = ncp_str_dup(password);
184 if (conn->li.user == NULL || conn->li.password == NULL) {
185 error = EINVAL;
186 goto bad;
188 ncp_str_upper(conn->li.user);
189 if ((conn->li.opt & NCP_OPT_NOUPCASEPASS) == 0)
190 ncp_str_upper(conn->li.password);
191 checkbad(ncp_login_object(conn, conn->li.user, objtype, conn->li.password,td,cred));
192 conn->li.objtype = objtype;
193 conn->flags |= NCPFL_LOGGED;
194 return 0;
195 bad:
196 if (conn->li.user) kfree(conn->li.user, M_NCPDATA);
197 if (conn->li.password) kfree(conn->li.password, M_NCPDATA);
198 conn->li.user = conn->li.password = NULL;
199 return error;