summaryrefslogtreecommitdiff
path: root/web/src/pubcookie/wp_gssapi_proxy.c
blob: f23d6098b69a580da071e053989d4a03f6feb16e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
/* ========================================================================
 * Copyright 2006 University of Washington
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * ========================================================================
 */


/* #define PROTOTYPE(x) x */

#include <system.h>
#include <general.h>

#include "wp_uidmapper_lib.h"

#include <gssapi/gssapi_generic.h>
#include <gssapi/gssapi_krb5.h>

#define AUTH_GSS_PROXY_MESSAGE 1
#define AUTH_GSS_PROXY_READ 2
#define AUTH_GSS_PROXY_SUCCESS 3 
#define AUTH_GSS_PROXY_WRITE 4
#define AUTH_GSS_PROXY_WRITE_NIL 5

#define AUTH_GSSAPI_P_NONE 1
#define AUTH_GSSAPI_P_INTEGRITY 2
#define AUTH_GSSAPI_P_PRIVACY 4

#ifdef NO_UIDMAPPER
int get_calling_username(int uid,char *name,int namelen) {
  struct passwd *pw;
  unsigned long len;

  pw = getpwuid(uid);
  if(!pw) return -1;
  len = strlen(pw->pw_name);
  if(len >= namelen) len = namelen - 1;  
  memcpy(name,pw->pw_name,len);
  name[len] = 0;
  return len;
}
#else
#define get_calling_username wp_uidmapper_getname
#endif

static unsigned long read_full(int fd,void *buf,unsigned long size) {
  unsigned long total,s;
  for(total = 0; total < size; total += s) {
    s = read(fd,(char*)buf + total,size - total);
    if(s == -1) {
      if((errno == EAGAIN) || (errno == EINTR)) s = 0;
      else return -1;
    } else if(s == 0) break;
  }
  return total; 
}

static unsigned long write_full(int fd,void *buf,unsigned long size) {
  unsigned long total,s;
  for(total = 0; total < size; total += s) {
    s = write(fd,(char*)buf + total,size - total);
    if(s == -1) {
      if((errno == EAGAIN) || (errno == EINTR)) s = 0;
      else return -1;
    }
  }
  return total; 
}

int cmd_message(char *str1, ...) {
  va_list list;
  unsigned long cmd[2],size;
  char *str;
  
  for(size = 0,str = str1,va_start(list,str1); str; str = va_arg(list,char*))
    size += strlen(str);
  va_end(list);

  cmd[0] = AUTH_GSS_PROXY_MESSAGE;
  cmd[1] = size;
  if(write_full(1,cmd,sizeof(cmd)) == -1) return -1;
  for(str = str1, va_start(list,str1); str; str = va_arg(list,char*))
    if(size = strlen(str)) if(write_full(1,str,size) == -1) {
      va_end(list);
      return -1;
    }
  va_end(list);
  return 0;
}

int cmd_read(gss_buffer_desc *pbuf) {
  unsigned long cmd[2],len,size;
  void *buf;
  
  cmd[0] = AUTH_GSS_PROXY_READ;
  cmd[1] = 0;
  if(write_full(1,cmd,sizeof(cmd)) == -1) return -1;
  
  len = read_full(0,&size,sizeof(size));
  if(len != sizeof(size)) return -1;
  if(size == 0) {
    pbuf->value = 0;
    pbuf->length = 0;
    return 0;
  }   

  buf = malloc(size);
  len = read_full(0,buf,size);
  if(len != size) {
    free(buf);
    return -1;
  }
  pbuf->value = buf;
  pbuf->length = size;
  return 0;
}

int cmd_success() {
  unsigned long cmd[2];  
  cmd[0] = AUTH_GSS_PROXY_SUCCESS;
  cmd[1] = 0;
  if(write_full(1,cmd,sizeof(cmd)) == -1) return -1;
  return 0;
}  

int cmd_write(gss_buffer_desc *buf) {
  unsigned long cmd[2];  
  cmd[0] = AUTH_GSS_PROXY_WRITE;
  cmd[1] = buf->length;
  if(write_full(1,cmd,sizeof(cmd)) == -1) return -1;
  if(buf->length) if(write_full(1,buf->value,buf->length) == -1) return -1;
  return 0;
}

int cmd_write_nil() {
  unsigned long cmd[2];  
  cmd[0] = AUTH_GSS_PROXY_WRITE_NIL;
  cmd[1] = 0;
  if(write_full(1,cmd,sizeof(cmd)) == -1) return -1;
  return 0;
}  

/*
 * service principal in argv[1]
 * reqested username in argv[2]
 */
  
int main(int argc,char *argv[])
{
  char *user = 0;
  char userbuf[WP_BUF_SIZE];
  char *prog;
  OM_uint32 smj,smn,dsmn,mctx;
  gss_name_t crname = GSS_C_NO_NAME;
  gss_ctx_id_t ctx = GSS_C_NO_CONTEXT;
  gss_buffer_desc chal,resp,buf;
  int conf,i;
  gss_qop_t qop;
  gss_OID oid;
  int calling_uid,eff_uid;

  if((prog = strrchr(argv[0], '/')) == NULL)
    prog = argv[0];
  else
    prog++;

  openlog(prog,LOG_PID,LOG_MAIL);

  calling_uid = getuid();
  eff_uid = geteuid();
#ifdef DEBUG
  syslog(LOG_INFO,"uid = %d, euid=%d\n",calling_uid,eff_uid);
#endif

#ifdef WEBSERVER_UID
  /* if euid != uid, change to web server user */
  if(calling_uid != eff_uid) if(setuid(WEBSERVER_UID)) {
    syslog(LOG_ERR,"setuid ((%d != %d) -> %d) failed: %s",
	   calling_uid,eff_uid,WEBSERVER_UID,strerror(errno));
    cmd_write_nil();
    goto cleanup;
  }
#endif

  if(argc < 2) {
    syslog(LOG_WARNING,"not enough arguments");
    cmd_write_nil();
    goto cleanup;
  } 
  if(get_calling_username(calling_uid,userbuf,WP_BUF_SIZE) == -1) {
    syslog(LOG_WARNING,"cannot determine calling username");
    cmd_write_nil();
    goto cleanup;
  }
  if(argc == 2) {
    user = userbuf;
#ifdef DEBUG
    syslog(LOG_INFO,"calling=%s\n",user);
#endif
  } else if(argc > 2) {
    user = argv[2];
#ifdef DEBUG
    syslog(LOG_INFO,"requested=%s calling=%s\n",user,userbuf);
#endif
#ifndef NO_NAME_CHECK
    if(strcmp(user,userbuf)) {
      syslog(LOG_WARNING,"cannot act on behalf of user %s (%s)",user,userbuf);
      cmd_write_nil();
      goto cleanup;
    }
#endif
  }
  
  /* expect empty challenge from server */
  if(cmd_read(&chal)) {
    syslog(LOG_WARNING,"cmd_read[initial] failed");
    goto cleanup;
  } else if(chal.length) {
    free(chal.value);
    syslog(LOG_WARNING,"cmd_read[initial] not empty");
    goto cleanup;
  }  

  /*
   * obtain credentials for requested service
   */
  
  buf.value = argv[1];
  buf.length = strlen(argv[1]);
  if(gss_import_name (&smn,&buf,gss_nt_service_name,&crname) != 
     GSS_S_COMPLETE) {
    syslog(LOG_WARNING,"gss_import_name(%s) failed",buf.value);
    cmd_write_nil();
    goto cleanup;
  }
  
  /* initial init_sec_context call, and send data */
  memcpy(&oid,&gss_mech_krb5,sizeof(oid));  
  smj = gss_init_sec_context
    (&smn,GSS_C_NO_CREDENTIAL,&ctx,crname,oid,
     GSS_C_MUTUAL_FLAG | GSS_C_REPLAY_FLAG,0,GSS_C_NO_CHANNEL_BINDINGS,
     GSS_C_NO_BUFFER,0,&resp,0,0);
  if((smj == GSS_S_COMPLETE) || (smj == GSS_S_CONTINUE_NEEDED)) {   
    i = cmd_write(&resp);
    gss_release_buffer (&smn,&resp);
    if(i) {
      syslog(LOG_WARNING,"cmd_write[init_sec_context] failed");
      goto cleanup;
    }    
  }

  /* loop until init_sec_context is done */
  while(smj == GSS_S_CONTINUE_NEEDED) {
    if(cmd_read(&chal)) {
      syslog(LOG_WARNING,"cmd_read[init_sec_context] failed");
      goto cleanup;
    } else if(!chal.length) {
      syslog(LOG_WARNING,"cmd_read[init_sec_context] empty");
      goto cleanup;
    } else {
      smj = gss_init_sec_context (&smn,GSS_C_NO_CREDENTIAL,&ctx,crname,
				  GSS_C_NO_OID,
				  GSS_C_MUTUAL_FLAG|GSS_C_REPLAY_FLAG,0,
				  GSS_C_NO_CHANNEL_BINDINGS,&chal,0,
				  &resp,0,0);
      if(chal.value) free(chal.value);
      if((smj == GSS_S_COMPLETE) || (smj == GSS_S_CONTINUE_NEEDED)) {
	i = cmd_write(&resp);
	gss_release_buffer (&smn,&resp);
	if(i) {
	  syslog(LOG_WARNING,"cmd_write[init_sec_context] failed");	
	  goto cleanup;
	}
      }
    }
  }

  switch(smj) {
  case GSS_S_COMPLETE:
    /* get challenge and unwrap it */
    if(cmd_read(&chal)) {
      syslog(LOG_WARNING,"cmd_read[gss_unwrap] failed");
      goto cleanup;
    }
    smj = gss_unwrap (&smn,ctx,&chal,&resp,&conf,&qop);
    if(chal.value) free(chal.value);
    if(smj != GSS_S_COMPLETE) {
      syslog(LOG_WARNING,"gss_unwrap failed");
      cmd_write_nil();
      goto cleanup;
    } else if(resp.length < 4) {
      syslog(LOG_WARNING,"challenge too short");
      gss_release_buffer (&smn,&resp);
      cmd_write_nil();
      goto cleanup;
    } else if(!( ((char*)resp.value)[0] & AUTH_GSSAPI_P_NONE)) {
      syslog(LOG_WARNING,"invalid challenge");
      gss_release_buffer (&smn,&resp);
      cmd_write_nil();
      goto cleanup;
    }
    
    /* prepare response to challenge */ 
    buf.length = 4 + (user ? strlen(user) : 0);
    buf.value = malloc(buf.length);    
    memcpy (buf.value,resp.value,4);
    gss_release_buffer (&smn,&resp);
    *(char*)buf.value = AUTH_GSSAPI_P_NONE;
    if(user) memcpy((char*)buf.value + 4, user, buf.length - 4);
    
    /* wrap response and send */
    smj = gss_wrap (&smn,ctx,0,qop,&buf,&conf,&resp);
    free(buf.value);
    if(smj != GSS_S_COMPLETE) {
      syslog(LOG_WARNING,"gss_unwrap failed");
      cmd_write_nil();
      goto cleanup;
    }
    i = cmd_write(&resp);
    gss_release_buffer (&smn,&resp);
    if(i) {
      syslog(LOG_WARNING,"cmd_write[gss_wrap] failed");
      goto cleanup;
    }
    
    /* success! */
    if(cmd_success()) syslog(LOG_WARNING,"cmd_success failed");
    goto cleanup;
    
  case GSS_S_CREDENTIALS_EXPIRED:
#ifdef DEBUG
    syslog(LOG_INFO,"Kerberos credentials expired (try running kinit)");
#endif
    if(cmd_message("Kerberos credentials expired (try running kinit)",0)) {
      syslog(LOG_WARNING,"cmd_message[credentials expired] failed");
      goto cleanup;
    }      
    cmd_write_nil();     
    goto cleanup;
    
  case GSS_S_FAILURE:
    if (smn == (OM_uint32) KRB5_FCC_NOFILE) {
#ifdef DEBUG
      syslog(LOG_INFO,"No credentials cache found (try running kinit)");
#endif
      if(cmd_message("No credentials cache found (try running kinit)",0)) {
	syslog(LOG_WARNING,"cmd_message[no cache file found] failed");
	goto cleanup;
      }
    } else {
      mctx = 0;
      do {
	gss_display_status (&dsmn,smn,GSS_C_MECH_CODE,
			    GSS_C_NO_OID,&mctx,&resp);
#ifdef DEBUG
	syslog(LOG_INFO,"GSSAPI failure: %s",resp.value);
#endif
	i = cmd_message("GSSAPI failure: ",resp.value,0);
	gss_release_buffer (&dsmn,&resp);
	if(i) {
	  syslog(LOG_WARNING,"cmd_message[failure] failed");
	  goto cleanup;
	}
      } while(mctx);
    }
    cmd_write_nil();     
    goto cleanup;
    
  default:
    mctx = 0;
    do {
      gss_display_status (&dsmn,smn,GSS_C_GSS_CODE,
			  GSS_C_NO_OID,&mctx,&resp);
#ifdef DEBUG
      syslog(LOG_INFO,"GSSAPI failure: %s",resp.value);
#endif
      i = cmd_message("Unknown GSSAPI failure: ",resp.value,0);
      gss_release_buffer (&dsmn,&resp);
      if(i) {
	syslog(LOG_WARNING,"cmd_message[unknown failure] failed");
	goto cleanup;
      }
    } while(mctx);
    if(!mctx) do {
      gss_display_status (&dsmn,smn,GSS_C_MECH_CODE,
			  GSS_C_NO_OID,&mctx,&resp);
#ifdef DEBUG
      syslog(LOG_INFO,"GSSAPI mechanism status: %s",resp.value);
#endif
      i = cmd_message("GSSAPI mechanism status: ",resp.value,0);
      gss_release_buffer (&dsmn,&resp);
      if(i) {
	syslog(LOG_WARNING,"cmd_message[unknown failure 2] failed");
	goto cleanup;
      }
    } while(mctx);
    cmd_write_nil();     
    goto cleanup;
  }  
  
 cleanup:
  if(ctx != GSS_C_NO_CONTEXT) gss_delete_sec_context (&smn,&ctx,0);
  if(crname != GSS_C_NO_NAME) gss_release_name (&smn,&crname);
  closelog();
  exit(0);
  return 0;
}