Ticket #41543: JPO-ssh-agent-launchd.patch
File JPO-ssh-agent-launchd.patch, 5.6 KB (added by jpo@…, 11 years ago) |
---|
-
Makefile.in
diff --git a/Makefile.in b/Makefile.in index 92c95a9..17d210c 100644
a b PATHS= -DSSHDIR=\"$(sysconfdir)\" \ 42 42 CC=@CC@ 43 43 LD=@LD@ 44 44 CFLAGS=@CFLAGS@ 45 CPPFLAGS=-I. -I$(srcdir) @CPPFLAGS@ $(PATHS) @DEFS@ 45 CPPFLAGS=-I. -I$(srcdir) @CPPFLAGS@ $(PATHS) @DEFS@ -DAPPLE_LAUNCHD 46 46 LIBS=@LIBS@ 47 47 K5LIBS=@K5LIBS@ 48 48 GSSLIBS=@GSSLIBS@ … … ssh-add$(EXEEXT): $(LIBCOMPAT) libssh.a ssh-add.o 155 155 $(LD) -o $@ ssh-add.o $(LDFLAGS) -lssh -lopenbsd-compat $(LIBS) 156 156 157 157 ssh-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 159 159 160 160 ssh-keygen$(EXEEXT): $(LIBCOMPAT) libssh.a ssh-keygen.o 161 161 $(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 65 65 #include <time.h> 66 66 #include <string.h> 67 67 #include <unistd.h> 68 #ifdef APPLE_LAUNCHD 69 #include <launch.h> 70 #endif 68 71 69 72 #include "xmalloc.h" 70 73 #include "ssh.h" … … usage(void) 1113 1116 fprintf(stderr, " -c Generate C-shell commands on stdout.\n"); 1114 1117 fprintf(stderr, " -s Generate Bourne shell commands on stdout.\n"); 1115 1118 fprintf(stderr, " -k Kill the current agent.\n"); 1119 #ifdef APPLE_LAUNCHD 1120 fprintf(stderr, " -l Start in launchd mode.\n"); 1121 #endif 1116 1122 fprintf(stderr, " -d Debug mode.\n"); 1117 1123 fprintf(stderr, " -a socket Bind agent socket to given name.\n"); 1118 1124 fprintf(stderr, " -t life Default identity lifetime (seconds).\n"); … … usage(void) 1122 1128 int 1123 1129 main(int ac, char **av) 1124 1130 { 1131 #ifdef APPLE_LAUNCHD 1132 int c_flag = 0, d_flag = 0, k_flag = 0, s_flag = 0, l_flag = 0; 1133 #else 1125 1134 int c_flag = 0, d_flag = 0, k_flag = 0, s_flag = 0; 1135 #endif 1126 1136 int sock, fd, ch, result, saved_errno; 1127 1137 u_int nalloc; 1128 1138 char *shell, *format, *pidstr, *agentsocket = NULL; … … main(int ac, char **av) 1156 1166 __progname = ssh_get_progname(av[0]); 1157 1167 seed_rng(); 1158 1168 1169 #ifdef APPLE_LAUNCHD 1170 while ((ch = getopt(ac, av, "cdklsa:t:")) != -1) { 1171 #else 1159 1172 while ((ch = getopt(ac, av, "cdksa:t:")) != -1) { 1173 #endif 1160 1174 switch (ch) { 1161 1175 case 'c': 1162 1176 if (s_flag) … … main(int ac, char **av) 1166 1180 case 'k': 1167 1181 k_flag++; 1168 1182 break; 1183 #ifdef APPLE_LAUNCHD 1184 case 'l': 1185 l_flag++; 1186 break; 1187 #endif 1169 1188 case 's': 1170 1189 if (c_flag) 1171 1190 usage(); … … main(int ac, char **av) 1192 1211 ac -= optind; 1193 1212 av += optind; 1194 1213 1214 #ifdef APPLE_LAUNCHD 1215 if (ac > 0 && (c_flag || k_flag || s_flag || d_flag || l_flag)) 1216 #else 1195 1217 if (ac > 0 && (c_flag || k_flag || s_flag || d_flag)) 1218 #endif 1196 1219 usage(); 1197 1220 1198 1221 if (ac == 0 && !c_flag && !s_flag) { … … main(int ac, char **av) 1248 1271 * Create socket early so it will exist before command gets run from 1249 1272 * the parent. 1250 1273 */ 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 */ 1251 1328 sock = socket(AF_UNIX, SOCK_STREAM, 0); 1252 1329 if (sock < 0) { 1253 1330 perror("socket"); … … main(int ac, char **av) 1269 1346 perror("listen"); 1270 1347 cleanup_exit(1); 1271 1348 } 1349 #ifdef APPLE_LAUNCHD 1350 } 1351 #endif 1272 1352 1273 1353 /* 1274 1354 * Fork, and have the parent execute the command, if any, or present … … main(int ac, char **av) 1282 1362 printf("echo Agent pid %ld;\n", (long)parent_pid); 1283 1363 goto skip; 1284 1364 } 1365 1366 #ifdef APPLE_LAUNCHD 1367 if (l_flag) 1368 goto skip; 1369 #endif 1370 1285 1371 pid = fork(); 1286 1372 if (pid == -1) { 1287 1373 perror("fork"); … … skip: 1340 1426 #ifdef ENABLE_PKCS11 1341 1427 pkcs11_init(0); 1342 1428 #endif 1429 1430 #ifdef APPLE_LAUNCHD 1431 if (!l_flag) 1432 new_socket(AUTH_SOCKET, sock); 1433 #else 1343 1434 new_socket(AUTH_SOCKET, sock); 1435 #endif 1436 1344 1437 if (ac > 0) 1345 1438 parent_alive_interval = 10; 1346 1439 idtab_init();