📄 myproxy_server.c
字号:
response->error_string, client_name); } if (myproxy_send(attrs, server_buffer, responselen) < 0) { myproxy_log_verror(); my_failure("error in myproxy_send()\n"); } free(response->version); response->version = NULL; free(server_buffer); return;}/********************************************************************** * * Routines to handle client requests to the server. * *//* Delegate requested credentials to the client */void get_proxy(myproxy_socket_attrs_t *attrs, myproxy_creds_t *creds, myproxy_request_t *request, myproxy_response_t *response, int max_proxy_lifetime){ int lifetime = 0; if (request->proxy_lifetime > 0) { lifetime = request->proxy_lifetime; } if (creds->lifetime > 0) { if (lifetime > 0) { lifetime = MIN(lifetime, creds->lifetime); } else { lifetime = creds->lifetime; } } if (max_proxy_lifetime > 0) { if (lifetime > 0) { lifetime = MIN(lifetime, max_proxy_lifetime); } else { lifetime = max_proxy_lifetime; } } if (myproxy_init_delegation(attrs, creds->location, lifetime, request->passphrase) < 0) { myproxy_log_verror(); response->response_type = MYPROXY_ERROR_RESPONSE; response->error_string = strdup("Unable to delegate credentials.\n"); } else { myproxy_log("Delegating credentials for %s lifetime=%d", creds->owner_name, lifetime); response->response_type = MYPROXY_OK_RESPONSE; } }/* Delegate requested credentials to the client */void get_credentials(myproxy_socket_attrs_t *attrs, myproxy_creds_t *creds, myproxy_request_t *request, myproxy_response_t *response, int max_proxy_lifetime){ if (myproxy_get_credentials(attrs, creds->location) < 0) { myproxy_log_verror(); response->response_type = MYPROXY_ERROR_RESPONSE; response->error_string = strdup("Unable to retrieve credentials.\n"); } else { myproxy_log("Sent credentials for %s", creds->owner_name); response->response_type = MYPROXY_OK_RESPONSE; }}/* Accept delegated credentials from client */void put_proxy(myproxy_socket_attrs_t *attrs, myproxy_creds_t *creds, myproxy_response_t *response) { char delegfile[64]; if (myproxy_accept_delegation(attrs, delegfile, sizeof(delegfile), creds->passphrase) < 0) { myproxy_log_verror(); response->response_type = MYPROXY_ERROR_RESPONSE; response->error_string = strdup("Failed to accept credentials.\n"); return; } myproxy_debug(" Accepted delegation: %s", delegfile); creds->location = strdup(delegfile); if (myproxy_creds_store(creds) < 0) { myproxy_log_verror(); response->response_type = MYPROXY_ERROR_RESPONSE; response->error_string = strdup("Unable to store credentials.\n"); } else { response->response_type = MYPROXY_OK_RESPONSE; } /* Clean up temporary delegation */ if (ssl_proxy_file_destroy(delegfile) != SSL_SUCCESS) { myproxy_log_perror("Removal of temporary credentials file %s failed", delegfile); }}/* Accept end-entity credentials from client */void put_credentials(myproxy_socket_attrs_t *attrs, myproxy_creds_t *creds, myproxy_response_t *response){ char delegfile[64]; if (myproxy_accept_credentials(attrs, delegfile, sizeof(delegfile)) < 0) { myproxy_log_verror(); response->response_type = MYPROXY_ERROR_RESPONSE; response->error_string = strdup("Failed to accept credentials.\n"); return; } myproxy_debug(" Accepted credentials: %s", delegfile); creds->location = strdup(delegfile); if (myproxy_creds_store(creds) < 0) { myproxy_log_verror(); response->response_type = MYPROXY_ERROR_RESPONSE; response->error_string = strdup("Unable to store credentials.\n"); } else { response->response_type = MYPROXY_OK_RESPONSE; } /* Clean up temporary delegation */ if (ssl_proxy_file_destroy(delegfile) != SSL_SUCCESS) { myproxy_log_perror("Removal of temporary credentials file %s failed", delegfile); }}void info_proxy(myproxy_creds_t *creds, myproxy_response_t *response) { if (myproxy_creds_retrieve_all(creds) < 0) { myproxy_log_verror(); response->response_type = MYPROXY_ERROR_RESPONSE; response->error_string = strdup(verror_get_string()); } else { response->response_type = MYPROXY_OK_RESPONSE; response->info_creds = creds; /* beware shallow copy here */ }}void destroy_proxy(myproxy_creds_t *creds, myproxy_response_t *response) { myproxy_debug("Deleting credentials for username \"%s\"", creds->username); myproxy_debug(" Owner is \"%s\"", creds->owner_name); myproxy_debug(" Delegation lifetime is %d seconds", creds->lifetime); if (myproxy_creds_delete(creds) < 0) { myproxy_log_verror(); response->response_type = MYPROXY_ERROR_RESPONSE; response->error_string = strdup(verror_get_string()); } else { response->response_type = MYPROXY_OK_RESPONSE; } }void change_passwd(myproxy_creds_t *creds, char *new_passphrase, myproxy_response_t *response) { myproxy_debug("Changing pass phrase for username \"%s\"", creds->username); myproxy_debug(" Owner is \"%s\"", creds->owner_name); if (myproxy_creds_change_passphrase(creds, new_passphrase) < 0) { myproxy_log_verror(); response->response_type = MYPROXY_ERROR_RESPONSE; response->error_string = strdup("Unable to change pass phrase.\n"); } else { response->response_type = MYPROXY_OK_RESPONSE; } }/* * my_signal * * installs a signal handler, and returns the old handler. * This emulates the semi-standard signal() function in a * standard way using the Posix sigaction function. * * from Stevens, 1998, section 5.8 */Sigfunc *my_signal(int signo, Sigfunc *func){ struct sigaction new_action, old_action; new_action.sa_handler = func; sigemptyset( &new_action.sa_mask ); new_action.sa_flags = 0; if (signo == SIGALRM) {#ifdef SA_INTERRUPT new_action.sa_flags |= SA_INTERRUPT; /* SunOS 4.x */#endif } else { #ifdef SA_RESTART new_action.sa_flags |= SA_RESTART; /* SVR4, 4.4BSD */#endif } if (sigaction(signo, &new_action, &old_action) < 0) { return SIG_ERR; } else { return old_action.sa_handler; }} /* Signal handlers here. Beware of making library calls inside signal handlers, as we could be interrupted at any point with a signal. This means no logging! */voidsig_chld(int signo) { pid_t pid; int stat; while ( (pid = waitpid(-1, &stat, WNOHANG)) > 0); return;} void sig_exit(int signo) { exit(0);}static voidfailure(const char *failure_message) { myproxy_log_perror("Failure: %s", failure_message); exit(1);} static voidmy_failure(const char *failure_message) { myproxy_log("Failure: %s", failure_message); exit(1);} static char *timestamp(void){ time_t clock; struct tm *tmp; time(&clock); tmp = (struct tm *)localtime(&clock); return (char *)asctime(tmp);}static intbecome_daemon(myproxy_server_context_t *context){ pid_t childpid; int fd = 0; int fdlimit; /* Steps taken from UNIX Programming FAQ */ /* 1. Fork off a child so the new process is not a process group leader */ childpid = fork(); switch (childpid) { case 0: /* child */ break; case -1: /* error */ perror("Error in fork()"); return -1; default: /* exit the original process */ _exit(0); } /* 2. Set session id to become a process group and session group leader */ if (setsid() < 0) { perror("Error in setsid()"); return -1; } /* 3. Fork again so the parent, (the session group leader), can exit. This means that we, as a non-session group leader, can never regain a controlling terminal. */ signal(SIGHUP, SIG_IGN); childpid = fork(); switch (childpid) { case 0: /* child */ break; case -1: /* error */ perror("Error in fork()"); return -1; default: /* exit the original process */ _exit(0); } /* 4. `chdir("/")' to ensure that our process doesn't keep any directory in use */ chdir("/"); /* 5. `umask(0)' so that we have complete control over the permissions of anything we write */ umask(0); /* 6. Close all file descriptors */ fdlimit = sysconf(_SC_OPEN_MAX); while (fd < fdlimit) close(fd++); /* 7.Establish new open descriptors for stdin, stdout and stderr */ (void)open("/dev/null", O_RDWR); dup(0); dup(0);#ifdef TIOCNOTTY fd = open("/dev/tty", O_RDWR); if (fd >= 0) { ioctl(fd, TIOCNOTTY, 0); (void)close(fd); } #endif /* TIOCNOTTY */ return 0;}static voidwrite_pidfile(const char path[]){ FILE *f = NULL; f = fopen(path, "wb"); if (f == NULL) { myproxy_log("Couldn't create pid file \"%s\": %s", path, strerror(errno)); } else { fprintf(f, "%ld\n", (long) getpid()); fclose(f); }}/* Check authorization for all incoming requests. The authorization * rules are as follows. * RETRIEVE: * Credentials must exist. * Client DN must match server-wide authorized_key_retrievers policy. * Client DN must match credential-specific authorized_key_retrievers policy. * Also, see below. * RETRIEVE and GET with passphrase (credential retrieval): * Client DN must match server-wide authorized_retrievers policy. * Client DN must match credential-specific authorized_retrievers policy. * Passphrase in request must match passphrase for credentials. * RETRIEVE and GET with certificate (credential renewal): * Client DN must match server-wide authorized_renewers policy. * Client DN must match credential-specific authorized_renewers policy. * DN in second X.509 authentication must match owner of credentials. * Private key can not be encrypted in this case. * PUT, STORE, and DESTROY: * Client DN must match accepted_credentials. * If credentials already exist for the username, the client must own them. * INFO: * Always allow here. Ownership checking done in info_proxy(). * CHANGE_CRED_PASSPHRASE: * Client DN must match accepted_credentials. * Client DN must match credential owner. * Passphrase in request must match passphrase for credentials. */static intmyproxy_authorize_accept(myproxy_server_context_t *context, myproxy_socket_attrs_t *attrs, myproxy_request_t *client_request, char *client_name){ int credentials_exist = 0; int client_owns_credentials = 0; int authorization_ok = -1; /* 1 = success, 0 = failure, -1 = error */ int credential_renewal = 0; int trusted_retriever = 0; int return_status = -1; myproxy_creds_t creds = { 0 }; credentials_exist = myproxy_creds_exist(client_request->username, client_request->credname); if (credentials_exist == -1) { myproxy_log_verror(); verror_put_string("Error checking credential existence"); goto end; } creds.username = strdup(client_request->username); if (client_request->credname) { creds.credname = strdup(client_request->credname);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -