| 1 | /* assuan-connect.c - Establish a connection (client) |
| 2 | * Copyright (C) 2001, 2002 Free Software Foundation, Inc. |
| 3 | * |
| 4 | * This file is part of Assuan. |
| 5 | * |
| 6 | * Assuan is free software; you can redistribute it and/or modify it |
| 7 | * under the terms of the GNU Lesser General Public License as |
| 8 | * published by the Free Software Foundation; either version 2.1 of |
| 9 | * the License, or (at your option) any later version. |
| 10 | * |
| 11 | * Assuan is distributed in the hope that it will be useful, but |
| 12 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 | * Lesser General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU Lesser General Public |
| 17 | * License along with this program; if not, see <http://www.gnu.org/licenses/>. |
| 18 | */ |
| 19 | |
| 20 | #ifdef HAVE_CONFIG_H |
| 21 | #include <config.h> |
| 22 | #endif |
| 23 | |
| 24 | #include <stdlib.h> |
| 25 | #include <stdio.h> |
| 26 | #include <string.h> |
| 27 | #include <signal.h> |
| 28 | #include <unistd.h> |
| 29 | #include <errno.h> |
| 30 | #include <sys/types.h> |
| 31 | #ifndef HAVE_W32_SYSTEM |
| 32 | #include <sys/wait.h> |
| 33 | #endif |
| 34 | |
| 35 | #include "assuan-defs.h" |
| 36 | |
| 37 | /* Disconnect and release the context CTX. */ |
| 38 | void |
| 39 | assuan_disconnect (assuan_context_t ctx) |
| 40 | { |
| 41 | if (ctx) |
| 42 | { |
| 43 | assuan_write_line (ctx, "BYE"); |
| 44 | ctx->finish_handler (ctx); |
| 45 | ctx->deinit_handler (ctx); |
| 46 | ctx->deinit_handler = NULL; |
| 47 | _assuan_release_context (ctx); |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | /* Return the PID of the peer or -1 if not known. This function works |
| 52 | in some situations where assuan_get_ucred fails. */ |
| 53 | pid_t |
| 54 | assuan_get_pid (assuan_context_t ctx) |
| 55 | { |
| 56 | return (ctx && ctx->pid)? ctx->pid : -1; |
| 57 | } |
| 58 | |
| 59 | |
| 60 | #ifndef HAVE_W32_SYSTEM |
| 61 | /* Return user credentials. PID, UID and GID amy be gived as NULL if |
| 62 | you are not interested in this value. For getting the pid of the |
| 63 | peer the assuan_get_pid is usually better suited. */ |
| 64 | assuan_error_t |
| 65 | assuan_get_peercred (assuan_context_t ctx, pid_t *pid, uid_t *uid, gid_t *gid) |
| 66 | { |
| 67 | if (!ctx) |
| 68 | return _assuan_error (ASSUAN_Invalid_Value); |
| 69 | if (!ctx->peercred.valid) |
| 70 | return _assuan_error (ASSUAN_General_Error); |
| 71 | |
| 72 | #ifdef HAVE_SO_PEERCRED |
| 73 | if (pid) |
| 74 | *pid = ctx->peercred.pid; |
| 75 | if (uid) |
| 76 | *uid = ctx->peercred.uid; |
| 77 | if (gid) |
| 78 | *gid = ctx->peercred.gid; |
| 79 | #endif |
| 80 | |
| 81 | return 0; |
| 82 | } |
| 83 | #endif /* HAVE_W32_SYSTEM */ |