source: zephyr.c @ ecd5dc5

barnowl_perlaimdebianowlrelease-1.10release-1.4release-1.5release-1.6release-1.7release-1.8release-1.9
Last change on this file since ecd5dc5 was 8262340, checked in by James M. Kretchmar <kretch@mit.edu>, 22 years ago
Added ZResetAuthentication in a number of places to fix problems with stale tickets Added some hooks for malloc debugging
  • Property mode set to 100644
File size: 13.5 KB
Line 
1#include <stdlib.h>
2#include <unistd.h>
3#include <sys/types.h>
4#include <sys/wait.h>
5#include <string.h>
6#include <zephyr/zephyr.h>
7#include <com_err.h>
8#include "owl.h"
9
10static const char fileIdent[] = "$Id$";
11
12Code_t ZResetAuthentication();
13
14int owl_zephyr_loadsubs(char *filename) {
15  /* return 0  on success
16   *        -1 on file error
17   *        -2 on subscription error
18   */
19  FILE *file;
20  char *tmp, *start;
21  char buffer[1024], subsfile[1024];
22  ZSubscription_t subs[3001];
23  int count, ret, i;
24
25  ret=0;
26 
27  if (filename==NULL) {
28    sprintf(subsfile, "%s/%s", owl_global_get_homedir(&g), ".zephyr.subs");
29  } else {
30    strcpy(subsfile, filename);
31  }
32
33  ZResetAuthentication();
34  /* need to redo this to do chunks, not just bail after 3000 */
35  count=0;
36  file=fopen(subsfile, "r");
37  if (file) {
38    while ( fgets(buffer, 1024, file)!=NULL ) {
39      if (buffer[0]=='#' || buffer[0]=='\n' || buffer[0]=='\n') continue;
40
41      if (buffer[0]=='-') {
42        start=buffer+1;
43      } else {
44        start=buffer;
45      }
46     
47      if (count >= 3000) break; /* also tell the user */
48
49      /* add it to the list of subs */
50      if ((tmp=(char *) strtok(start, ",\n\r"))==NULL) continue;
51      subs[count].zsub_class=owl_strdup(tmp);
52      if ((tmp=(char *) strtok(NULL, ",\n\r"))==NULL) continue;
53      subs[count].zsub_classinst=owl_strdup(tmp);
54      if ((tmp=(char *) strtok(NULL, " \t\n\r"))==NULL) continue;
55      subs[count].zsub_recipient=owl_strdup(tmp);
56
57      /* if it started with '-' then add it to the global punt list */
58      if (buffer[0]=='-') {
59        owl_function_zpunt(subs[count].zsub_class, subs[count].zsub_classinst, subs[count].zsub_recipient, 0);
60      }
61     
62      count++;
63    }
64    fclose(file);
65  } else {
66    count=0;
67    ret=-1;
68  }
69
70  /* sub with defaults */
71  if (ZSubscribeTo(subs,count,0) != ZERR_NONE) {
72    fprintf(stderr, "Error subbing\n");
73    ret=-2;
74  }
75
76  /* free stuff */
77  for (i=0; i<count; i++) {
78    owl_free(subs[i].zsub_class);
79    owl_free(subs[i].zsub_classinst);
80    owl_free(subs[i].zsub_recipient);
81  }
82
83  return(ret);
84}
85
86int owl_zephyr_loadloginsubs(char *filename) {
87  FILE *file;
88  ZSubscription_t subs[3001];
89  char subsfile[1024], buffer[1024];
90  int count, ret, i;
91
92  ret=0;
93
94  if (filename==NULL) {
95    sprintf(subsfile, "%s/%s", owl_global_get_homedir(&g), ".anyone");
96  } else {
97    strcpy(subsfile, filename);
98  }
99
100  ZResetAuthentication();
101  /* need to redo this to do chunks, not just bag out after 3000 */
102  count=0;
103  file=fopen(subsfile, "r");
104  if (file) {
105    while ( fgets(buffer, 1024, file)!=NULL ) {
106      if (buffer[0]=='#' || buffer[0]=='\n' || buffer[0]=='\n') continue;
107     
108      if (count >= 3000) break; /* also tell the user */
109
110      buffer[strlen(buffer)-1]='\0';
111      subs[count].zsub_class="login";
112      subs[count].zsub_recipient="*";
113      if (strchr(buffer, '@')) {
114        subs[count].zsub_classinst=owl_strdup(buffer);
115      } else {
116        subs[count].zsub_classinst=owl_sprintf("%s@%s", buffer, ZGetRealm());
117      }
118
119      count++;
120    }
121    fclose(file);
122  } else {
123    count=0;
124    ret=-1;
125  }
126
127  /* sub with defaults */
128  if (ZSubscribeToSansDefaults(subs,count,0) != ZERR_NONE) {
129    fprintf(stderr, "Error subbing\n");
130    ret=-2;
131  }
132
133  /* free stuff */
134  for (i=0; i<count; i++) {
135    owl_free(subs[i].zsub_classinst);
136  }
137
138  return(ret);
139}
140
141void unsuball() {
142  int ret;
143
144  ZResetAuthentication();
145  ret=ZCancelSubscriptions(0);
146  if (ret != ZERR_NONE) {
147    com_err("owl",ret,"while unsubscribing");
148  }
149}
150
151int owl_zephyr_sub(char *class, char *inst, char *recip) {
152  ZSubscription_t subs[5];
153  int ret;
154
155  subs[0].zsub_class=class;
156  subs[0].zsub_classinst=inst;
157  subs[0].zsub_recipient=recip;
158
159  ZResetAuthentication();
160  if (ZSubscribeTo(subs,1,0) != ZERR_NONE) {
161    fprintf(stderr, "Error subbing\n");
162    ret=-2;
163  }
164  return(0);
165}
166
167
168int owl_zephyr_unsub(char *class, char *inst, char *recip) {
169  ZSubscription_t subs[5];
170  int ret;
171
172  subs[0].zsub_class=class;
173  subs[0].zsub_classinst=inst;
174  subs[0].zsub_recipient=recip;
175
176  ZResetAuthentication();
177  if (ZUnsubscribeTo(subs,1,0) != ZERR_NONE) {
178    fprintf(stderr, "Error unsubbing\n");
179    ret=-2;
180  }
181  return(0);
182}
183
184
185char *owl_zephyr_get_field(ZNotice_t *n, int j, int *k) {
186  /* return a pointer to the Jth field, place the length in k.  If the
187     field doesn't exist return an emtpy string */
188  int i, count, save;
189
190  count=save=0;
191  for (i=0; i<n->z_message_len; i++) {
192    if (n->z_message[i]=='\0') {
193      count++;
194      if (count==j) {
195        /* just found the end of the field we're looking for */
196        *k=i-save;
197        return(n->z_message+save);
198      } else {
199        save=i+1;
200      }
201    }
202  }
203  /* catch the last field */
204  if (count==j-1) {
205    *k=n->z_message_len-save;
206    return(n->z_message+save);
207  }
208 
209  *k=0;
210  return("");
211}
212
213
214int owl_zephyr_get_num_fields(ZNotice_t *n) {
215  int i, fields;
216
217  fields=1;
218  for (i=0; i<n->z_message_len; i++) {
219    if (n->z_message[i]=='\0') fields++;
220  }
221 
222  return(fields);
223}
224
225
226char *owl_zephyr_get_message(ZNotice_t *n, int *k) {
227  /* return a pointer to the message, place the message length in k */
228  if (!strcasecmp(n->z_opcode, "ping")) {
229    *k=0;
230    return("");
231  }
232
233  return(owl_zephyr_get_field(n, 2, k));
234}
235
236
237char *owl_zephyr_get_zsig(ZNotice_t *n, int *k) {
238  /* return a pointer to the zsig if there is one */
239
240  if (n->z_message_len==0) {
241    *k=0;
242    return("");
243  }
244  *k=strlen(n->z_message);
245  return(n->z_message);
246}
247
248
249int send_zephyr(char *opcode, char *zsig, char *class, char *instance, char *recipient, char *message) {
250  int ret;
251  ZNotice_t notice;
252   
253  memset(&notice, 0, sizeof(notice));
254
255  ZResetAuthentication();
256 
257  notice.z_kind=ACKED;
258  notice.z_port=0;
259  notice.z_class=class;
260  notice.z_class_inst=instance;
261  if (!strcmp(recipient, "*") || !strcmp(recipient, "@")) {
262    notice.z_recipient="";
263  } else {
264    notice.z_recipient=recipient;
265  }
266  notice.z_default_format="Class $class, Instance $instance:\nTo: @bold($recipient) at $time $date\nFrom: @bold{$1 <$sender>}\n\n$2";
267  notice.z_sender=NULL;
268  if (opcode) notice.z_opcode=opcode;
269
270  notice.z_message_len=strlen(zsig)+1+strlen(message);
271  notice.z_message=owl_malloc(notice.z_message_len+10);
272  strcpy(notice.z_message, zsig);
273  memcpy(notice.z_message+strlen(zsig)+1, message, strlen(message));
274
275  /* ret=ZSendNotice(&notice, ZAUTH); */
276  ret=ZSrvSendNotice(&notice, ZAUTH, send_zephyr_helper);
277 
278  /* free then check the return */
279  owl_free(notice.z_message);
280  ZFreeNotice(&notice);
281  if (ret!=ZERR_NONE) {
282    owl_function_makemsg("Error sending zephyr");
283    return(ret);
284  }
285
286  return(0);
287}
288
289Code_t send_zephyr_helper(ZNotice_t *notice, char *buf, int len, int wait) {
290  return(ZSendPacket(buf, len, 0));
291}
292
293void send_ping(char *to) {
294  send_zephyr("PING", "", "MESSAGE", "PERSONAL", to, "");
295}
296
297void owl_zephyr_handle_ack(ZNotice_t *retnotice) {
298  char *tmp;
299 
300  /* if it's an HMACK ignore it */
301  if (retnotice->z_kind == HMACK) return;
302
303  if (retnotice->z_kind == SERVNAK) {
304    owl_function_makemsg("Authorization failure sending zephyr");
305  } else if ((retnotice->z_kind != SERVACK) || !retnotice->z_message_len) {
306    owl_function_makemsg("Detected server failure while receiving acknowledgement");
307  } else if (!strcmp(retnotice->z_message, ZSRVACK_SENT)) {
308    if (!strcasecmp(retnotice->z_opcode, "ping")) {
309      return;
310    } else if (!strcasecmp(retnotice->z_class, "message") &&
311               !strcasecmp(retnotice->z_class_inst, "personal")) {
312      tmp=short_zuser(retnotice->z_recipient);
313      owl_function_makemsg("Message sent to %s.", tmp);
314      free(tmp);
315    } else {
316      owl_function_makemsg("Message sent to -c %s -i %s\n", retnotice->z_class, retnotice->z_class_inst);
317    }
318  } else if (!strcmp(retnotice->z_message, ZSRVACK_NOTSENT)) {
319    if (strcasecmp(retnotice->z_class, "message")) {
320      owl_function_makemsg("Not logged in or not subscribing to class %s, instance %s",
321                           retnotice->z_class, retnotice->z_class_inst);
322    } else {
323      tmp = short_zuser(retnotice->z_recipient);
324      owl_function_makemsg("%s: Not logged in or subscribing to messages.", 
325                           tmp);
326      owl_free(tmp);
327    }
328  } else {
329    owl_function_makemsg("Internal error on ack (%s)", retnotice->z_message);
330  }
331}
332
333int owl_zephyr_notice_is_ack(ZNotice_t *n) {
334  if (n->z_kind == SERVNAK || n->z_kind == SERVACK || n->z_kind == HMACK) {
335    if (!strcasecmp(n->z_class, LOGIN_CLASS)) return(0);
336    return(1);
337  }
338  return(0);
339}
340 
341void owl_zephyr_zaway(owl_message *m) {
342  char *tmpbuff, *myuser, *to;
343 
344  /* bail if it doesn't look like a message we should reply to.  Some
345     of this defined by the way zaway(1) works */
346  if (strcasecmp(owl_message_get_class(m), "message")) return;
347  if (strcasecmp(owl_message_get_recipient(m), ZGetSender())) return;
348  if (!strcasecmp(owl_message_get_sender(m), "")) return;
349  if (!strcasecmp(owl_message_get_opcode(m), "ping")) return;
350  if (!strcasecmp(owl_message_get_opcode(m), "auto")) return;
351  if (!strcasecmp(owl_message_get_zsig(m), "Automated reply:")) return;
352  if (!strcasecmp(owl_message_get_sender(m), ZGetSender())) return;
353
354  if (owl_global_is_smartstrip(&g)) {
355    to=owl_util_smartstripped_user(owl_message_get_sender(m));
356  } else {
357    to=owl_strdup(owl_message_get_sender(m));
358  }
359
360  send_zephyr("",
361              "Automated reply:",
362              owl_message_get_class(m),
363              owl_message_get_instance(m),
364              to,
365              owl_global_get_zaway_msg(&g));
366
367  myuser=short_zuser(to);
368  if (!strcasecmp(owl_message_get_instance(m), "personal")) {
369    tmpbuff = owl_sprintf("zwrite %s", myuser);
370  } else {
371    tmpbuff = owl_sprintf("zwrite -i %s %s", owl_message_get_instance(m), myuser);
372  }
373  owl_free(myuser);
374  owl_free(to);
375
376  /* display the message as an admin message in the receive window */
377  owl_function_make_outgoing_zephyr(owl_global_get_zaway_msg(&g), tmpbuff, "Automated reply:");
378  owl_free(tmpbuff);
379}
380
381
382void owl_zephyr_hackaway_cr(ZNotice_t *n) {
383  /* replace \r's with ' '.  Gross-ish */
384  int i;
385
386  for (i=0; i<n->z_message_len; i++) {
387    if (n->z_message[i]=='\r') {
388      n->z_message[i]=' ';
389    }
390  }
391}
392
393void owl_zephyr_zlocate(char *user, char *out, int auth) {
394  int ret, numlocs;
395  int one = 1;
396  ZLocations_t locations;
397  char *myuser;
398 
399  strcpy(out, "");
400  ZResetAuthentication();
401  ret=ZLocateUser(user,&numlocs,auth?ZAUTH:ZNOAUTH);
402  if (ret != ZERR_NONE) {
403    owl_function_makemsg("Error locating user");
404  }
405
406  if (numlocs==0) {
407    myuser=short_zuser(user);
408    sprintf(out, "%s: Hidden or not logged-in\n", myuser);
409    owl_free(myuser);
410    return;
411  }
412   
413  for (;numlocs;numlocs--) {
414    ZGetLocations(&locations,&one);
415    myuser=short_zuser(user);
416    sprintf(out, "%s%s: %s\t%s\t%s\n", out, myuser, locations.host, locations.tty, locations.time);
417    owl_free(myuser);
418  }
419}
420
421void owl_zephyr_addsub(char *filename, char *class, char *inst, char *recip) {
422  char *line, subsfile[LINE], buff[LINE];
423  FILE *file;
424
425  line=owl_zephyr_makesubline(class, inst, recip);
426
427  if (filename==NULL) {
428    sprintf(subsfile, "%s/%s", owl_global_get_homedir(&g), ".zephyr.subs");
429  } else {
430    strcpy(subsfile, filename);
431  }
432
433  /* first check if it exists already */
434  file=fopen(subsfile, "r");
435  if (!file) {
436    owl_function_makemsg("Error opening file %s", subsfile);
437    owl_free(line);
438    return;
439  }
440  while (fgets(buff, LINE, file)!=NULL) {
441    if (!strcasecmp(buff, line)) {
442      owl_function_makemsg("Subscription already present in %s", subsfile);
443      owl_free(line);
444      return;
445    }
446  }
447
448  /* if we get here then we didn't find it */
449  fclose(file);
450  file=fopen(subsfile, "a");
451  if (!file) {
452    owl_function_makemsg("Error opening file %s for writing", subsfile);
453    owl_free(line);
454    return;
455  }
456  fputs(line, file);
457  fclose(file);
458  owl_function_makemsg("Subscription added");
459 
460  owl_free(line);
461}
462
463void owl_zephyr_delsub(char *filename, char *class, char *inst, char *recip) {
464  char *line, subsfile[LINE], buff[LINE], *text;
465  char backupfilename[LINE];
466  FILE *file, *backupfile;
467  int size;
468 
469  line=owl_zephyr_makesubline(class, inst, recip);
470
471  /* open the subsfile for reading */
472  if (filename==NULL) {
473    sprintf(subsfile, "%s/%s", owl_global_get_homedir(&g), ".zephyr.subs");
474  } else {
475    strcpy(subsfile, filename);
476  }
477  file=fopen(subsfile, "r");
478  if (!file) {
479    owl_function_makemsg("Error opening file %s", subsfile);
480    owl_free(line);
481    return;
482  }
483
484  /* open the backup file for writing */
485  sprintf(backupfilename, "%s.backup", subsfile);
486  backupfile=fopen(backupfilename, "w");
487  if (!backupfile) {
488    owl_function_makemsg("Error opening file %s for writing", backupfilename);
489    owl_free(line);
490    return;
491  }
492
493  /* we'll read the entire file into memory, minus the line we don't want and
494   * and at the same time create a backup file */
495  text=owl_malloc(LINE);
496  strcpy(text, "");
497  size=LINE;
498  while (fgets(buff, LINE, file)!=NULL) {
499    /* if we don't match the line, add to text */
500    if (strcasecmp(buff, line)) {
501      size+=LINE;
502      text=owl_realloc(text, size);
503      strcat(text, buff);
504    }
505
506    /* write to backupfile */
507    fputs(buff, backupfile);
508  }
509  fclose(backupfile);
510  fclose(file);
511
512  /* now open the original subs file for writing and write out the
513   * subs */
514  file=fopen(subsfile, "w");
515  if (!file) {
516    owl_function_makemsg("WARNING: Error opening %s to rewrite subscriptions.  Use %s to restore", subsfile, backupfilename);
517    owl_function_beep();
518    owl_free(line);
519    return;
520  }
521
522  fputs(text, file);
523  fclose(file);
524  owl_free(line);
525
526  owl_function_makemsg("Subscription removed");
527}
528
529char *owl_zephyr_makesubline(char *class, char *inst, char *recip) {
530  /* caller must free the return */
531  char *out;
532
533  out=owl_malloc(strlen(class)+strlen(inst)+strlen(recip)+30);
534  sprintf(out, "%s,%s,%s\n", class, inst, !strcmp(recip, "") ? "*" : recip);
535  return(out);
536}
Note: See TracBrowser for help on using the repository browser.