Ticket #41543: JPO-ssh-agent-launchd.patch

File JPO-ssh-agent-launchd.patch, 5.6 KB (added by jpo@…, 10 years ago)
  • Makefile.in

    diff --git a/Makefile.in b/Makefile.in
    index 92c95a9..17d210c 100644
    a b PATHS= -DSSHDIR=\"$(sysconfdir)\" \ 
    4242CC=@CC@
    4343LD=@LD@
    4444CFLAGS=@CFLAGS@
    45 CPPFLAGS=-I. -I$(srcdir) @CPPFLAGS@ $(PATHS) @DEFS@
     45CPPFLAGS=-I. -I$(srcdir) @CPPFLAGS@ $(PATHS) @DEFS@ -DAPPLE_LAUNCHD
    4646LIBS=@LIBS@
    4747K5LIBS=@K5LIBS@
    4848GSSLIBS=@GSSLIBS@
    ssh-add$(EXEEXT): $(LIBCOMPAT) libssh.a ssh-add.o 
    155155        $(LD) -o $@ ssh-add.o $(LDFLAGS) -lssh -lopenbsd-compat $(LIBS)
    156156
    157157ssh-agent$(EXEEXT): $(LIBCOMPAT) libssh.a ssh-agent.o ssh-pkcs11-client.o
    158         $(LD) -o $@ ssh-agent.o ssh-pkcs11-client.o $(LDFLAGS) -lssh -lopenbsd-compat $(LIBS)
     158        $(LD) -o $@ ssh-agent.o ssh-pkcs11-client.o $(LDFLAGS) -lssh -lopenbsd-compat $(LIBS) -framework ServiceManagement
    159159
    160160ssh-keygen$(EXEEXT): $(LIBCOMPAT) libssh.a ssh-keygen.o
    161161        $(LD) -o $@ ssh-keygen.o $(LDFLAGS) -lssh -lopenbsd-compat $(LIBS)
  • ssh-agent.c

    diff --git a/ssh-agent.c b/ssh-agent.c
    index c3b1172..ff873b7 100644
    a b  
    6565#include <time.h>
    6666#include <string.h>
    6767#include <unistd.h>
     68#ifdef APPLE_LAUNCHD
     69#include <launch.h>
     70#endif
    6871
    6972#include "xmalloc.h"
    7073#include "ssh.h"
    usage(void) 
    11131116        fprintf(stderr, "  -c          Generate C-shell commands on stdout.\n");
    11141117        fprintf(stderr, "  -s          Generate Bourne shell commands on stdout.\n");
    11151118        fprintf(stderr, "  -k          Kill the current agent.\n");
     1119#ifdef APPLE_LAUNCHD
     1120        fprintf(stderr, "  -l          Start in launchd mode.\n");
     1121#endif
    11161122        fprintf(stderr, "  -d          Debug mode.\n");
    11171123        fprintf(stderr, "  -a socket   Bind agent socket to given name.\n");
    11181124        fprintf(stderr, "  -t life     Default identity lifetime (seconds).\n");
    usage(void) 
    11221128int
    11231129main(int ac, char **av)
    11241130{
     1131#ifdef APPLE_LAUNCHD
     1132        int c_flag = 0, d_flag = 0, k_flag = 0, s_flag = 0, l_flag = 0;
     1133#else
    11251134        int c_flag = 0, d_flag = 0, k_flag = 0, s_flag = 0;
     1135#endif
    11261136        int sock, fd, ch, result, saved_errno;
    11271137        u_int nalloc;
    11281138        char *shell, *format, *pidstr, *agentsocket = NULL;
    main(int ac, char **av) 
    11561166        __progname = ssh_get_progname(av[0]);
    11571167        seed_rng();
    11581168
     1169#ifdef APPLE_LAUNCHD
     1170        while ((ch = getopt(ac, av, "cdklsa:t:")) != -1) {
     1171#else
    11591172        while ((ch = getopt(ac, av, "cdksa:t:")) != -1) {
     1173#endif
    11601174                switch (ch) {
    11611175                case 'c':
    11621176                        if (s_flag)
    main(int ac, char **av) 
    11661180                case 'k':
    11671181                        k_flag++;
    11681182                        break;
     1183#ifdef APPLE_LAUNCHD
     1184                case 'l':
     1185                        l_flag++;
     1186                        break;
     1187#endif
    11691188                case 's':
    11701189                        if (c_flag)
    11711190                                usage();
    main(int ac, char **av) 
    11921211        ac -= optind;
    11931212        av += optind;
    11941213
     1214#ifdef APPLE_LAUNCHD
     1215        if (ac > 0 && (c_flag || k_flag || s_flag || d_flag || l_flag))
     1216#else
    11951217        if (ac > 0 && (c_flag || k_flag || s_flag || d_flag))
     1218#endif
    11961219                usage();
    11971220
    11981221        if (ac == 0 && !c_flag && !s_flag) {
    main(int ac, char **av) 
    12481271         * Create socket early so it will exist before command gets run from
    12491272         * the parent.
    12501273         */
     1274#ifdef APPLE_LAUNCHD
     1275        if (l_flag) {
     1276                launch_data_t checkin_request;
     1277                launch_data_t checkin_response;
     1278                launch_data_t sockets_dict;
     1279                launch_data_t listeners_dict;
     1280                size_t listeners_count;
     1281                size_t listeners_i;
     1282
     1283                checkin_request = launch_data_new_string(LAUNCH_KEY_CHECKIN);
     1284                if (checkin_request == NULL)
     1285                        fatal("unable to build launchd checkin string");
     1286
     1287                checkin_response = launch_msg(checkin_request);
     1288                if (checkin_response == NULL)
     1289                        fatal("launchd IPC failure");
     1290
     1291                if (launch_data_get_type(checkin_response) != LAUNCH_DATA_DICTIONARY)
     1292                        fatal("launchd response is not a dictionary");
     1293
     1294                sockets_dict = launch_data_dict_lookup(checkin_response, LAUNCH_JOBKEY_SOCKETS);
     1295                if (sockets_dict == NULL)
     1296                        fatal("launchd plist missing sockets");
     1297
     1298                if (launch_data_get_type(sockets_dict) != LAUNCH_DATA_DICTIONARY)
     1299                        fatal("launchd sockets is not a LAUNCH_DATA_DICTIONARY");
     1300
     1301                listeners_dict = launch_data_dict_lookup(sockets_dict, "Listeners");
     1302                if (listeners_dict == NULL)
     1303                        fatal("launchd plist missing listeners");
     1304
     1305                if (launch_data_get_type(listeners_dict) != LAUNCH_DATA_ARRAY)
     1306                        fatal("launchd listeners is not a LAUNCH_DATA_ARRAY");
     1307
     1308                listeners_count = launch_data_array_get_count(listeners_dict);
     1309                if (listeners_count <= 0)
     1310                        fatal("no sockets inherited from launchd");
     1311
     1312                for (listeners_i = 0; listeners_i < listeners_count; listeners_i++) {
     1313                        launch_data_t inherited_sock = launch_data_array_get_index(listeners_dict, listeners_i);
     1314                        if (launch_data_get_type(inherited_sock) != LAUNCH_DATA_FD)
     1315                                fatal("launchd passed a listener that is not a LAUNCH_DATA_FD");
     1316                        new_socket(AUTH_SOCKET, launch_data_get_fd(inherited_sock));
     1317                }
     1318
     1319                /*
     1320                 * I couldn't find clear documentation on launchd indicating
     1321                 * which of these functions return things that need to be freed,
     1322                 * so there may be leaks.
     1323                 */
     1324                launch_data_free(checkin_request);
     1325                launch_data_free(checkin_response);
     1326        } else {
     1327#endif /* APPLE_LAUNCHD */
    12511328        sock = socket(AF_UNIX, SOCK_STREAM, 0);
    12521329        if (sock < 0) {
    12531330                perror("socket");
    main(int ac, char **av) 
    12691346                perror("listen");
    12701347                cleanup_exit(1);
    12711348        }
     1349#ifdef APPLE_LAUNCHD
     1350        }
     1351#endif
    12721352
    12731353        /*
    12741354         * Fork, and have the parent execute the command, if any, or present
    main(int ac, char **av) 
    12821362                printf("echo Agent pid %ld;\n", (long)parent_pid);
    12831363                goto skip;
    12841364        }
     1365
     1366#ifdef APPLE_LAUNCHD
     1367        if (l_flag)
     1368                goto skip;
     1369#endif
     1370
    12851371        pid = fork();
    12861372        if (pid == -1) {
    12871373                perror("fork");
    skip: 
    13401426#ifdef ENABLE_PKCS11
    13411427        pkcs11_init(0);
    13421428#endif
     1429
     1430#ifdef APPLE_LAUNCHD
     1431        if (!l_flag)
     1432                new_socket(AUTH_SOCKET, sock);
     1433#else
    13431434        new_socket(AUTH_SOCKET, sock);
     1435#endif
     1436
    13441437        if (ac > 0)
    13451438                parent_alive_interval = 10;
    13461439        idtab_init();