comparison svr-authpubkey.c @ 165:0cfba3034be5

Fixed DEBUG_TRACE macro so that we don't get semicolons left about the place
author Matt Johnston <matt@ucc.asn.au>
date Sun, 02 Jan 2005 20:25:56 +0000
parents 364a75cfebab
children 161557a9dde8
comparison
equal deleted inserted replaced
161:b9d3f725e00b 165:0cfba3034be5
58 buffer * signbuf = NULL; 58 buffer * signbuf = NULL;
59 sign_key * key = NULL; 59 sign_key * key = NULL;
60 char* fp = NULL; 60 char* fp = NULL;
61 int type = -1; 61 int type = -1;
62 62
63 TRACE(("enter pubkeyauth")); 63 TRACE(("enter pubkeyauth"))
64 64
65 /* 0 indicates user just wants to check if key can be used, 1 is an 65 /* 0 indicates user just wants to check if key can be used, 1 is an
66 * actual attempt*/ 66 * actual attempt*/
67 testkey = (buf_getbyte(ses.payload) == 0); 67 testkey = (buf_getbyte(ses.payload) == 0);
68 68
125 } 125 }
126 if (key) { 126 if (key) {
127 sign_key_free(key); 127 sign_key_free(key);
128 key = NULL; 128 key = NULL;
129 } 129 }
130 TRACE(("leave pubkeyauth")); 130 TRACE(("leave pubkeyauth"))
131 } 131 }
132 132
133 /* Reply that the key is valid for auth, this is sent when the user sends 133 /* Reply that the key is valid for auth, this is sent when the user sends
134 * a straight copy of their pubkey to test, to avoid having to perform 134 * a straight copy of their pubkey to test, to avoid having to perform
135 * expensive signing operations with a worthless key */ 135 * expensive signing operations with a worthless key */
136 static void send_msg_userauth_pk_ok(unsigned char* algo, unsigned int algolen, 136 static void send_msg_userauth_pk_ok(unsigned char* algo, unsigned int algolen,
137 unsigned char* keyblob, unsigned int keybloblen) { 137 unsigned char* keyblob, unsigned int keybloblen) {
138 138
139 TRACE(("enter send_msg_userauth_pk_ok")); 139 TRACE(("enter send_msg_userauth_pk_ok"))
140 CHECKCLEARTOWRITE(); 140 CHECKCLEARTOWRITE();
141 141
142 buf_putbyte(ses.writepayload, SSH_MSG_USERAUTH_PK_OK); 142 buf_putbyte(ses.writepayload, SSH_MSG_USERAUTH_PK_OK);
143 buf_putstring(ses.writepayload, algo, algolen); 143 buf_putstring(ses.writepayload, algo, algolen);
144 buf_putstring(ses.writepayload, keyblob, keybloblen); 144 buf_putstring(ses.writepayload, keyblob, keybloblen);
145 145
146 encrypt_packet(); 146 encrypt_packet();
147 TRACE(("leave send_msg_userauth_pk_ok")); 147 TRACE(("leave send_msg_userauth_pk_ok"))
148 148
149 } 149 }
150 150
151 /* Checks whether a specified publickey (and associated algorithm) is an 151 /* Checks whether a specified publickey (and associated algorithm) is an
152 * acceptable key for authentication */ 152 * acceptable key for authentication */
158 char * filename = NULL; 158 char * filename = NULL;
159 int ret = DROPBEAR_FAILURE; 159 int ret = DROPBEAR_FAILURE;
160 buffer * line = NULL; 160 buffer * line = NULL;
161 unsigned int len, pos; 161 unsigned int len, pos;
162 162
163 TRACE(("enter checkpubkey")); 163 TRACE(("enter checkpubkey"))
164 164
165 /* check that we can use the algo */ 165 /* check that we can use the algo */
166 if (have_algo(algo, algolen, sshhostkey) == DROPBEAR_FAILURE) { 166 if (have_algo(algo, algolen, sshhostkey) == DROPBEAR_FAILURE) {
167 dropbear_log(LOG_WARNING, 167 dropbear_log(LOG_WARNING,
168 "pubkey auth attempt with unknown algo for '%s' from %s", 168 "pubkey auth attempt with unknown algo for '%s' from %s",
170 goto out; 170 goto out;
171 } 171 }
172 172
173 /* check file permissions, also whether file exists */ 173 /* check file permissions, also whether file exists */
174 if (checkpubkeyperms() == DROPBEAR_FAILURE) { 174 if (checkpubkeyperms() == DROPBEAR_FAILURE) {
175 TRACE(("bad authorized_keys permissions, or file doesn't exist")); 175 TRACE(("bad authorized_keys permissions, or file doesn't exist"))
176 goto out; 176 goto out;
177 } 177 }
178 178
179 /* we don't need to check pw and pw_dir for validity, since 179 /* we don't need to check pw and pw_dir for validity, since
180 * its been done in checkpubkeyperms. */ 180 * its been done in checkpubkeyperms. */
188 /* open the file */ 188 /* open the file */
189 authfile = fopen(filename, "r"); 189 authfile = fopen(filename, "r");
190 if (authfile == NULL) { 190 if (authfile == NULL) {
191 goto out; 191 goto out;
192 } 192 }
193 TRACE(("checkpubkey: opened authorized_keys OK")); 193 TRACE(("checkpubkey: opened authorized_keys OK"))
194 194
195 line = buf_new(MAX_AUTHKEYS_LINE); 195 line = buf_new(MAX_AUTHKEYS_LINE);
196 196
197 /* iterate through the lines */ 197 /* iterate through the lines */
198 do { 198 do {
199 199
200 if (buf_getline(line, authfile) == DROPBEAR_FAILURE) { 200 if (buf_getline(line, authfile) == DROPBEAR_FAILURE) {
201 /* EOF reached */ 201 /* EOF reached */
202 TRACE(("checkpubkey: authorized_keys EOF reached")); 202 TRACE(("checkpubkey: authorized_keys EOF reached"))
203 break; 203 break;
204 } 204 }
205 205
206 if (line->len < MIN_AUTHKEYS_LINE) { 206 if (line->len < MIN_AUTHKEYS_LINE) {
207 TRACE(("checkpubkey: line too short")); 207 TRACE(("checkpubkey: line too short"))
208 continue; /* line is too short for it to be a valid key */ 208 continue; /* line is too short for it to be a valid key */
209 } 209 }
210 210
211 /* check the key type - this also stops us from using keys 211 /* check the key type - this also stops us from using keys
212 * which have options with them */ 212 * which have options with them */
215 } 215 }
216 buf_incrpos(line, algolen); 216 buf_incrpos(line, algolen);
217 217
218 /* check for space (' ') character */ 218 /* check for space (' ') character */
219 if (buf_getbyte(line) != ' ') { 219 if (buf_getbyte(line) != ' ') {
220 TRACE(("checkpubkey: space character expected, isn't there")); 220 TRACE(("checkpubkey: space character expected, isn't there"))
221 continue; 221 continue;
222 } 222 }
223 223
224 /* truncate the line at the space after the base64 data */ 224 /* truncate the line at the space after the base64 data */
225 pos = line->pos; 225 pos = line->pos;
227 if (buf_getbyte(line) == ' ') break; 227 if (buf_getbyte(line) == ' ') break;
228 } 228 }
229 buf_setpos(line, pos); 229 buf_setpos(line, pos);
230 buf_setlen(line, line->pos + len); 230 buf_setlen(line, line->pos + len);
231 231
232 TRACE(("checkpubkey: line pos = %d len = %d", line->pos, line->len)); 232 TRACE(("checkpubkey: line pos = %d len = %d", line->pos, line->len))
233 233
234 ret = cmp_base64_key(keyblob, keybloblen, algo, algolen, line); 234 ret = cmp_base64_key(keyblob, keybloblen, algo, algolen, line);
235 if (ret == DROPBEAR_SUCCESS) { 235 if (ret == DROPBEAR_SUCCESS) {
236 break; 236 break;
237 } 237 }
246 } 246 }
247 if (line) { 247 if (line) {
248 buf_free(line); 248 buf_free(line);
249 } 249 }
250 m_free(filename); 250 m_free(filename);
251 TRACE(("leave checkpubkey: ret=%d", ret)); 251 TRACE(("leave checkpubkey: ret=%d", ret))
252 return ret; 252 return ret;
253 } 253 }
254 254
255 255
256 /* Returns DROPBEAR_SUCCESS if file permissions for pubkeys are ok, 256 /* Returns DROPBEAR_SUCCESS if file permissions for pubkeys are ok,
262 262
263 char* filename = NULL; 263 char* filename = NULL;
264 int ret = DROPBEAR_FAILURE; 264 int ret = DROPBEAR_FAILURE;
265 unsigned int len; 265 unsigned int len;
266 266
267 TRACE(("enter checkpubkeyperms")); 267 TRACE(("enter checkpubkeyperms"))
268 268
269 assert(ses.authstate.pw); 269 assert(ses.authstate.pw);
270 if (ses.authstate.pw->pw_dir == NULL) { 270 if (ses.authstate.pw->pw_dir == NULL) {
271 goto out; 271 goto out;
272 } 272 }
301 ret = DROPBEAR_SUCCESS; 301 ret = DROPBEAR_SUCCESS;
302 302
303 out: 303 out:
304 m_free(filename); 304 m_free(filename);
305 305
306 TRACE(("leave checkpubkeyperms")); 306 TRACE(("leave checkpubkeyperms"))
307 return ret; 307 return ret;
308 } 308 }
309 309
310 /* Checks that a file is owned by the user or root, and isn't writable by 310 /* Checks that a file is owned by the user or root, and isn't writable by
311 * group or other */ 311 * group or other */
312 /* returns DROPBEAR_SUCCESS or DROPBEAR_FAILURE */ 312 /* returns DROPBEAR_SUCCESS or DROPBEAR_FAILURE */
313 static int checkfileperm(char * filename) { 313 static int checkfileperm(char * filename) {
314 struct stat filestat; 314 struct stat filestat;
315 315
316 TRACE(("enter checkfileperm(%s)", filename)); 316 TRACE(("enter checkfileperm(%s)", filename))
317 317
318 if (stat(filename, &filestat) != 0) { 318 if (stat(filename, &filestat) != 0) {
319 TRACE(("leave checkfileperm: stat() != 0")); 319 TRACE(("leave checkfileperm: stat() != 0"))
320 return DROPBEAR_FAILURE; 320 return DROPBEAR_FAILURE;
321 } 321 }
322 /* check ownership - user or root only*/ 322 /* check ownership - user or root only*/
323 if (filestat.st_uid != ses.authstate.pw->pw_uid 323 if (filestat.st_uid != ses.authstate.pw->pw_uid
324 && filestat.st_uid != 0) { 324 && filestat.st_uid != 0) {
325 TRACE(("leave checkfileperm: wrong ownership")); 325 TRACE(("leave checkfileperm: wrong ownership"))
326 return DROPBEAR_FAILURE; 326 return DROPBEAR_FAILURE;
327 } 327 }
328 /* check permissions - don't want group or others +w */ 328 /* check permissions - don't want group or others +w */
329 if (filestat.st_mode & (S_IWGRP | S_IWOTH)) { 329 if (filestat.st_mode & (S_IWGRP | S_IWOTH)) {
330 TRACE(("leave checkfileperm: wrong perms")); 330 TRACE(("leave checkfileperm: wrong perms"))
331 return DROPBEAR_FAILURE; 331 return DROPBEAR_FAILURE;
332 } 332 }
333 TRACE(("leave checkfileperm: success")); 333 TRACE(("leave checkfileperm: success"))
334 return DROPBEAR_SUCCESS; 334 return DROPBEAR_SUCCESS;
335 } 335 }
336 336
337 337
338 #endif 338 #endif