/mandos/trunk

To get this branch, use:
bzr branch http://bzr.recompile.se/loggerhead/mandos/trunk

« back to all changes in this revision

Viewing changes to plugins.d/mandos-client.c

  • Committer: Teddy Hogeborn
  • Date: 2009-08-27 23:12:09 UTC
  • Revision ID: teddy@fukt.bsnet.se-20090827231209-ry9loqws0gr7ze4g
* plugins.d/mandos-client.c (quit_now): Move up declaration before
                                        first use.
  (resolve_callback, ): Check "quit_now" flag.
  (main): Renamed "interface_taken_up" to "take_down_interface" for
          clarity.  Check existence of interface before taking it up.
          Set "take_down_interface" and "tempdir_created" flags before
          any action is taken, to make sure cleanup is never missed.
          Check "quit_now" flag throughout.

Show diffs side-by-side

added added

removed removed

Lines of Context:
780
780
  return retval;
781
781
}
782
782
 
 
783
sig_atomic_t quit_now = 0;
 
784
 
783
785
static void resolve_callback(AvahiSServiceResolver *r,
784
786
                             AvahiIfIndex interface,
785
787
                             AvahiProtocol proto,
799
801
  /* Called whenever a service has been resolved successfully or
800
802
     timed out */
801
803
  
 
804
  if(quit_now){
 
805
    return;
 
806
  }
 
807
  
802
808
  switch(event){
803
809
  default:
804
810
  case AVAHI_RESOLVER_FAILURE:
875
881
  }
876
882
}
877
883
 
878
 
sig_atomic_t quit_now = 0;
879
 
 
880
884
/* stop main loop after sigterm has been called */
881
885
static void handle_sigterm(__attribute__((unused)) int sig){
882
886
  if(quit_now){
900
904
  const char *interface = "eth0";
901
905
  struct ifreq network;
902
906
  int sd = -1;
903
 
  bool interface_taken_up = false;
 
907
  bool take_down_interface = false;
904
908
  uid_t uid;
905
909
  gid_t gid;
906
910
  char *connect_to = NULL;
1058
1062
  
1059
1063
  /* If the interface is down, bring it up */
1060
1064
  if(interface[0] != '\0'){
 
1065
    if_index = (AvahiIfIndex) if_nametoindex(interface);
 
1066
    if(if_index == 0){
 
1067
      fprintf(stderr, "No such interface: \"%s\"\n", interface);
 
1068
      exitcode = EXIT_FAILURE;
 
1069
      goto end;
 
1070
    }
 
1071
    
 
1072
    if(quit_now){
 
1073
      goto end;
 
1074
    }
 
1075
    
1061
1076
#ifdef __linux__
1062
1077
    /* Lower kernel loglevel to KERN_NOTICE to avoid KERN_INFO
1063
1078
       messages to mess up the prompt */
1100
1115
    }
1101
1116
    if((network.ifr_flags & IFF_UP) == 0){
1102
1117
      network.ifr_flags |= IFF_UP;
 
1118
      take_down_interface = true;
1103
1119
      ret = ioctl(sd, SIOCSIFFLAGS, &network);
1104
1120
      if(ret == -1){
 
1121
        take_down_interface = false;
1105
1122
        perror("ioctl SIOCSIFFLAGS");
1106
1123
        exitcode = EXIT_FAILURE;
1107
1124
#ifdef __linux__
1114
1131
#endif  /* __linux__ */
1115
1132
        goto end;
1116
1133
      }
1117
 
      interface_taken_up = true;
1118
1134
    }
1119
1135
    /* sleep checking until interface is running */
1120
1136
    for(int i=0; i < delay * 4; i++){
1130
1146
        perror("nanosleep");
1131
1147
      }
1132
1148
    }
1133
 
    if(not interface_taken_up){
 
1149
    if(not take_down_interface){
 
1150
      /* We won't need the socket anymore */
1134
1151
      ret = (int)TEMP_FAILURE_RETRY(close(sd));
1135
1152
      if(ret == -1){
1136
1153
        perror("close");
1147
1164
#endif  /* __linux__ */
1148
1165
  }
1149
1166
  
 
1167
  if(quit_now){
 
1168
    goto end;
 
1169
  }
 
1170
  
1150
1171
  uid = getuid();
1151
1172
  gid = getgid();
1152
1173
  
1161
1182
    perror("setuid");
1162
1183
  }
1163
1184
  
 
1185
  if(quit_now){
 
1186
    goto end;
 
1187
  }
 
1188
  
1164
1189
  ret = init_gnutls_global(pubkey, seckey);
1165
1190
  if(ret == -1){
1166
1191
    fprintf(stderr, "init_gnutls_global failed\n");
1170
1195
    gnutls_initialized = true;
1171
1196
  }
1172
1197
  
 
1198
  if(quit_now){
 
1199
    goto end;
 
1200
  }
 
1201
  
 
1202
  tempdir_created = true;
1173
1203
  if(mkdtemp(tempdir) == NULL){
 
1204
    tempdir_created = false;
1174
1205
    perror("mkdtemp");
1175
1206
    goto end;
1176
1207
  }
1177
 
  tempdir_created = true;
 
1208
  
 
1209
  if(quit_now){
 
1210
    goto end;
 
1211
  }
1178
1212
  
1179
1213
  if(not init_gpgme(pubkey, seckey, tempdir)){
1180
1214
    fprintf(stderr, "init_gpgme failed\n");
1184
1218
    gpgme_initialized = true;
1185
1219
  }
1186
1220
  
1187
 
  if(interface[0] != '\0'){
1188
 
    if_index = (AvahiIfIndex) if_nametoindex(interface);
1189
 
    if(if_index == 0){
1190
 
      fprintf(stderr, "No such interface: \"%s\"\n", interface);
1191
 
      exitcode = EXIT_FAILURE;
1192
 
      goto end;
1193
 
    }
 
1221
  if(quit_now){
 
1222
    goto end;
1194
1223
  }
1195
1224
  
1196
1225
  if(connect_to != NULL){
1202
1231
      exitcode = EXIT_FAILURE;
1203
1232
      goto end;
1204
1233
    }
 
1234
    
 
1235
    if(quit_now){
 
1236
      goto end;
 
1237
    }
 
1238
    
1205
1239
    uint16_t port;
1206
1240
    errno = 0;
1207
1241
    tmpmax = strtoimax(address+1, &tmp, 10);
1211
1245
      exitcode = EXIT_FAILURE;
1212
1246
      goto end;
1213
1247
    }
 
1248
  
 
1249
    if(quit_now){
 
1250
      goto end;
 
1251
    }
 
1252
    
1214
1253
    port = (uint16_t)tmpmax;
1215
1254
    *address = '\0';
1216
1255
    address = connect_to;
1221
1260
    } else {
1222
1261
      af = AF_INET;
1223
1262
    }
 
1263
    
 
1264
    if(quit_now){
 
1265
      goto end;
 
1266
    }
 
1267
    
1224
1268
    ret = start_mandos_communication(address, port, if_index, af);
1225
1269
    if(ret < 0){
1226
1270
      exitcode = EXIT_FAILURE;
1229
1273
    }
1230
1274
    goto end;
1231
1275
  }
1232
 
    
 
1276
  
 
1277
  if(quit_now){
 
1278
    goto end;
 
1279
  }
 
1280
  
1233
1281
  {
1234
1282
    AvahiServerConfig config;
1235
1283
    /* Do not publish any local Zeroconf records */
1256
1304
    goto end;
1257
1305
  }
1258
1306
  
 
1307
  if(quit_now){
 
1308
    goto end;
 
1309
  }
 
1310
  
1259
1311
  /* Create the Avahi service browser */
1260
1312
  sb = avahi_s_service_browser_new(mc.server, if_index,
1261
1313
                                   AVAHI_PROTO_UNSPEC, "_mandos._tcp",
1267
1319
    goto end;
1268
1320
  }
1269
1321
  
 
1322
  if(quit_now){
 
1323
    goto end;
 
1324
  }
 
1325
  
1270
1326
  /* Run the main loop */
1271
1327
  
1272
1328
  if(debug){
1302
1358
  }
1303
1359
  
1304
1360
  /* Take down the network interface */
1305
 
  if(interface_taken_up){
 
1361
  if(take_down_interface){
1306
1362
    ret = ioctl(sd, SIOCGIFFLAGS, &network);
1307
1363
    if(ret == -1){
1308
1364
      perror("ioctl SIOCGIFFLAGS");