source: zephyr.c @ 601733d

debianrelease-1.4release-1.5release-1.6release-1.7release-1.8release-1.9
Last change on this file since 601733d was ddacb9c, checked in by Alejandro R. Sedeño <asedeno@mit.edu>, 12 years ago
Fix a segfault on retrieving zephyr subs when the user doesn't have any or has expired tickets.
  • Property mode set to 100644
File size: 29.0 KB
RevLine 
[7d4fbcd]1#include <stdlib.h>
2#include <unistd.h>
3#include <sys/types.h>
4#include <sys/wait.h>
[4357be8]5#include <sys/stat.h>
[7d4fbcd]6#include <string.h>
7#include "owl.h"
8
[1aee7d9]9static const char fileIdent[] = "$Id$";
10
[09489b89]11#ifdef HAVE_LIBZEPHYR
[8262340]12Code_t ZResetAuthentication();
[09489b89]13#endif
[8262340]14
[be0a79f]15int owl_zephyr_initialize()
16{
[09489b89]17#ifdef HAVE_LIBZEPHYR
[be0a79f]18  int ret;
[02f55dc]19  ZNotice_t notice;
20
21  /* Stat the zhm manually, with a shorter timeout */
22  if ((ret = ZOpenPort(NULL)) != ZERR_NONE)
23    return(ret);
24
25  if ((ret = owl_zhm_stat(&notice)) != ZERR_NONE)
26    return(ret);
27
28  ZClosePort();
29
[be0a79f]30  if ((ret = ZInitialize()) != ZERR_NONE) {
31    com_err("owl",ret,"while initializing");
32    return(1);
33  }
34  if ((ret = ZOpenPort(NULL)) != ZERR_NONE) {
35    com_err("owl",ret,"while opening port");
36    return(1);
37  }
[09489b89]38#endif
39  return(0);
40}
41
[02f55dc]42#ifdef HAVE_LIBZEPHYR
43#define HM_SVC_FALLBACK         htons((unsigned short) 2104)
44
45/*
46 * Code modified from libzephyr's ZhmStat.c
47 *
48 * Modified to only wait one second to time out if there is no
49 * hostmanager present, rather than a rather excessive 10 seconds.
50 */
51Code_t owl_zhm_stat(ZNotice_t *notice) {
52  struct servent *sp;
53  struct sockaddr_in sin;
54  ZNotice_t req;
55  Code_t code;
56  struct timeval tv;
57  fd_set readers;
58
59  (void) memset((char *)&sin, 0, sizeof(struct sockaddr_in));
60
61  sp = getservbyname(HM_SVCNAME, "udp");
62
63  sin.sin_port = (sp) ? sp->s_port : HM_SVC_FALLBACK;
64  sin.sin_family = AF_INET;
65
66  sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
67
68  (void) memset((char *)&req, 0, sizeof(req));
69  req.z_kind = STAT;
70  req.z_port = 0;
71  req.z_class = HM_STAT_CLASS;
72  req.z_class_inst = HM_STAT_CLIENT;
73  req.z_opcode = HM_GIMMESTATS;
74  req.z_sender = "";
75  req.z_recipient = "";
76  req.z_default_format = "";
77  req.z_message_len = 0;
78       
79  if ((code = ZSetDestAddr(&sin)) != ZERR_NONE)
80    return(code);
81
82  if ((code = ZSendNotice(&req, ZNOAUTH)) != ZERR_NONE)
83    return(code);
84
85  /* Wait up to 1 second for a response. */
86  FD_ZERO(&readers);
87  FD_SET(ZGetFD(), &readers);
88  tv.tv_sec = 1;
89  tv.tv_usec = 0;
90  code = select(ZGetFD() + 1, &readers, NULL, NULL, &tv);
91  if (code < 0 && errno != EINTR)
92    return(errno);
93  if (code == 0 || (code < 0 && errno == EINTR) || ZPending() == 0)
94    return(ZERR_HMDEAD);
95
96  return(ZReceiveNotice(notice, (struct sockaddr_in *) 0));
97}
98
99#endif
100
[09489b89]101int owl_zephyr_shutdown()
102{
103#ifdef HAVE_LIBZEPHYR
[bfbf590]104  if(owl_global_is_havezephyr(&g)) {
105    unsuball();
106    ZClosePort();
107  }
[09489b89]108#endif
[be0a79f]109  return(0);
110}
111
112int owl_zephyr_zpending()
113{
114#ifdef HAVE_LIBZEPHYR
[bfbf590]115  if(owl_global_is_havezephyr(&g))
116    return(ZPending());
117  else
118    return 0;
[be0a79f]119#else
120  return(0);
121#endif
122}
123
[09489b89]124char *owl_zephyr_get_realm()
125{
126#ifdef HAVE_LIBZEPHYR
127  return(ZGetRealm());
128#else
129  return("");
130#endif
131}
132
133char *owl_zephyr_get_sender()
134{
135#ifdef HAVE_LIBZEPHYR
136  return(ZGetSender());
137#else
138  return("");
139#endif
140}
141
[f6050ee]142#ifdef HAVE_LIBZEPHYR
[93e883d]143int owl_zephyr_loadsubs_helper(ZSubscription_t subs[], int count)
144{
145  int i, ret = 0;
146  /* sub without defaults */
147  if (ZSubscribeToSansDefaults(subs,count,0) != ZERR_NONE) {
148    owl_function_error("Error subscribing to zephyr notifications.");
149    ret=-2;
150  }
151
152  /* free stuff */
153  for (i=0; i<count; i++) {
154    owl_free(subs[i].zsub_class);
155    owl_free(subs[i].zsub_classinst);
156    owl_free(subs[i].zsub_recipient);
157  }
[bb2c60d]158
[93e883d]159  return ret;
160}
[f6050ee]161#endif
[93e883d]162
[95474d7]163/* Load zephyr subscriptions form 'filename'.  If 'filename' is NULL,
164 * the default file $HOME/.zephyr.subs will be used.
165 *
166 * Returns 0 on success.  If the file does not exist, return -1 if
167 * 'error_on_nofile' is 1, otherwise return 0.  Return -1 if the file
168 * exists but can not be read.  Return -2 if there is a failure from
169 * zephyr to load the subscriptions.
[2de4f20]170 */
[95474d7]171int owl_zephyr_loadsubs(char *filename, int error_on_nofile)
[31e48a3]172{
[be0a79f]173#ifdef HAVE_LIBZEPHYR
[7d4fbcd]174  FILE *file;
175  char *tmp, *start;
176  char buffer[1024], subsfile[1024];
[bb2c60d]177  ZSubscription_t *subs;
178  int subSize = 1024;
[93e883d]179  int count, ret;
[4357be8]180  struct stat statbuff;
[7d4fbcd]181
[bb2c60d]182  subs = owl_malloc(sizeof(ZSubscription_t) * subSize);
[7d4fbcd]183  if (filename==NULL) {
184    sprintf(subsfile, "%s/%s", owl_global_get_homedir(&g), ".zephyr.subs");
185  } else {
186    strcpy(subsfile, filename);
187  }
[8262340]188
[4357be8]189  ret=stat(subsfile, &statbuff);
[95474d7]190  if (ret) {
191    if (error_on_nofile==1) return(-1);
192    return(0);
193  }
[4357be8]194
[8262340]195  ZResetAuthentication();
[7d4fbcd]196  count=0;
197  file=fopen(subsfile, "r");
[95474d7]198  if (!file) return(-1);
199  while ( fgets(buffer, 1024, file)!=NULL ) {
200    if (buffer[0]=='#' || buffer[0]=='\n' || buffer[0]=='\n') continue;
201   
202    if (buffer[0]=='-') {
203      start=buffer+1;
204    } else {
205      start=buffer;
[7d4fbcd]206    }
[95474d7]207   
[bb2c60d]208    if (count >= subSize) {
209      ZSubscription_t* newsubs;
210      newsubs = owl_realloc(subs, sizeof(ZSubscription_t) * subSize * 2);
211      if (NULL == newsubs) {
212        /* If realloc fails, load what we've got, clear, and continue */
213        ret = owl_zephyr_loadsubs_helper(subs, count);
214        if (ret != 0) {
215          fclose(file);
216          return(ret);
217        }
218        count=0;
219      }
220      else {
221        subs = newsubs;
222        subSize *= 2;
[93e883d]223      }
224    }
[95474d7]225   
226    /* add it to the list of subs */
227    if ((tmp=(char *) strtok(start, ",\n\r"))==NULL) continue;
228    subs[count].zsub_class=owl_strdup(tmp);
229    if ((tmp=(char *) strtok(NULL, ",\n\r"))==NULL) continue;
230    subs[count].zsub_classinst=owl_strdup(tmp);
231    if ((tmp=(char *) strtok(NULL, " \t\n\r"))==NULL) continue;
232    subs[count].zsub_recipient=owl_strdup(tmp);
233   
[bb2c60d]234    /* if it started with '-' then add it to the global punt list, and
235     * remove it from the list of subs. */
[95474d7]236    if (buffer[0]=='-') {
237      owl_function_zpunt(subs[count].zsub_class, subs[count].zsub_classinst, subs[count].zsub_recipient, 0);
[bb2c60d]238      owl_free(subs[count].zsub_class);
239      owl_free(subs[count].zsub_classinst);
240      owl_free(subs[count].zsub_recipient);
241    }
242    else {
243      count++;
[95474d7]244    }
[7d4fbcd]245  }
[95474d7]246  fclose(file);
[7d4fbcd]247
[93e883d]248  ret=owl_zephyr_loadsubs_helper(subs, count);
[bb2c60d]249  owl_free(subs);
[7d4fbcd]250  return(ret);
[be0a79f]251#else
252  return(0);
253#endif
[7d4fbcd]254}
255
[4357be8]256int owl_zephyr_loaddefaultsubs()
257{
[40d834a]258#ifdef HAVE_LIBZEPHYR
[4357be8]259  ZSubscription_t subs[10];
260   
261  if (ZSubscribeTo(subs,0,0) != ZERR_NONE) {
262    owl_function_error("Error subscribing to default zephyr notifications.");
263    return(-1);
264  }
265  return(0);
[40d834a]266#else
267  return(0);
268#endif
[4357be8]269}
270
[31e48a3]271int owl_zephyr_loadloginsubs(char *filename)
272{
[be0a79f]273#ifdef HAVE_LIBZEPHYR
[7d4fbcd]274  FILE *file;
275  ZSubscription_t subs[3001];
276  char subsfile[1024], buffer[1024];
277  int count, ret, i;
[4357be8]278  struct stat statbuff;
[7d4fbcd]279
280  if (filename==NULL) {
281    sprintf(subsfile, "%s/%s", owl_global_get_homedir(&g), ".anyone");
282  } else {
283    strcpy(subsfile, filename);
284  }
[4357be8]285 
286  ret=stat(subsfile, &statbuff);
287  if (ret) return(0);
288
289  ret=0;
[7d4fbcd]290
[8262340]291  ZResetAuthentication();
[7d4fbcd]292  /* need to redo this to do chunks, not just bag out after 3000 */
293  count=0;
294  file=fopen(subsfile, "r");
295  if (file) {
296    while ( fgets(buffer, 1024, file)!=NULL ) {
297      if (buffer[0]=='#' || buffer[0]=='\n' || buffer[0]=='\n') continue;
298     
299      if (count >= 3000) break; /* also tell the user */
300
301      buffer[strlen(buffer)-1]='\0';
302      subs[count].zsub_class="login";
303      subs[count].zsub_recipient="*";
304      if (strchr(buffer, '@')) {
305        subs[count].zsub_classinst=owl_strdup(buffer);
306      } else {
[1c6c4d3]307        subs[count].zsub_classinst=owl_sprintf("%s@%s", buffer, ZGetRealm());
[7d4fbcd]308      }
309
310      count++;
311    }
312    fclose(file);
313  } else {
314    count=0;
315    ret=-1;
316  }
317
318  /* sub with defaults */
[7933748]319  if (ZSubscribeToSansDefaults(subs,count,0) != ZERR_NONE) {
[252a5c2]320    owl_function_error("Error subscribing to zephyr notifications.");
[7d4fbcd]321    ret=-2;
322  }
323
324  /* free stuff */
325  for (i=0; i<count; i++) {
326    owl_free(subs[i].zsub_classinst);
327  }
328
329  return(ret);
[be0a79f]330#else
331  return(0);
332#endif
[7d4fbcd]333}
334
[31e48a3]335void unsuball()
336{
[be0a79f]337#if HAVE_LIBZEPHYR
[7d4fbcd]338  int ret;
[8262340]339
340  ZResetAuthentication();
[7d4fbcd]341  ret=ZCancelSubscriptions(0);
342  if (ret != ZERR_NONE) {
343    com_err("owl",ret,"while unsubscribing");
344  }
[be0a79f]345#endif
[7d4fbcd]346}
347
[31e48a3]348int owl_zephyr_sub(char *class, char *inst, char *recip)
349{
[be0a79f]350#ifdef HAVE_LIBZEPHYR
[7d4fbcd]351  ZSubscription_t subs[5];
352
353  subs[0].zsub_class=class;
354  subs[0].zsub_classinst=inst;
355  subs[0].zsub_recipient=recip;
356
[8262340]357  ZResetAuthentication();
[7d4fbcd]358  if (ZSubscribeTo(subs,1,0) != ZERR_NONE) {
[97cd00be]359    owl_function_error("Error subbing to <%s,%s,%s>", class, inst, recip);
360    return(-2);
[7d4fbcd]361  }
362  return(0);
[be0a79f]363#else
364  return(0);
365#endif
[7d4fbcd]366}
367
368
[31e48a3]369int owl_zephyr_unsub(char *class, char *inst, char *recip)
370{
[be0a79f]371#ifdef HAVE_LIBZEPHYR
[7d4fbcd]372  ZSubscription_t subs[5];
373
374  subs[0].zsub_class=class;
375  subs[0].zsub_classinst=inst;
376  subs[0].zsub_recipient=recip;
377
[8262340]378  ZResetAuthentication();
[7d4fbcd]379  if (ZUnsubscribeTo(subs,1,0) != ZERR_NONE) {
[97cd00be]380    owl_function_error("Error unsubbing from <%s,%s,%s>", class, inst, recip);
381    return(-2);
[7d4fbcd]382  }
383  return(0);
[be0a79f]384#else
385  return(0);
386#endif
[7d4fbcd]387}
388
[b0430a6]389/* return a pointer to the data in the Jth field, (NULL terminated by
390 * definition).  Caller must free the return.
391 */
[be0a79f]392#ifdef HAVE_LIBZEPHYR
[b0430a6]393char *owl_zephyr_get_field(ZNotice_t *n, int j)
[31e48a3]394{
[7d4fbcd]395  int i, count, save;
[b0430a6]396  char *out;
[7d4fbcd]397
[128171a]398  /* If there's no message here, just run along now */
399  if (n->z_message_len == 0)
400    return(owl_strdup(""));
401
[7d4fbcd]402  count=save=0;
403  for (i=0; i<n->z_message_len; i++) {
404    if (n->z_message[i]=='\0') {
405      count++;
406      if (count==j) {
407        /* just found the end of the field we're looking for */
[b0430a6]408        return(owl_strdup(n->z_message+save));
[7d4fbcd]409      } else {
410        save=i+1;
411      }
412    }
413  }
[b0430a6]414  /* catch the last field, which might not be null terminated */
[7d4fbcd]415  if (count==j-1) {
[b0430a6]416    out=owl_malloc(n->z_message_len-save+5);
417    memcpy(out, n->z_message+save, n->z_message_len-save);
418    out[n->z_message_len-save]='\0';
419    return(out);
[7d4fbcd]420  }
[b0430a6]421
422  return(owl_strdup(""));
[7d4fbcd]423}
[5376a95]424
425char *owl_zephyr_get_field_as_utf8(ZNotice_t *n, int j)
426{
427  int i, count, save;
428
429  /* If there's no message here, just run along now */
430  if (n->z_message_len == 0)
431    return(owl_strdup(""));
432
433  count=save=0;
434  for (i = 0; i < n->z_message_len; i++) {
435    if (n->z_message[i]=='\0') {
436      count++;
437      if (count == j) {
438        /* just found the end of the field we're looking for */
[6201646]439        return(owl_validate_or_convert(n->z_message + save));
[5376a95]440      } else {
441        save = i + 1;
442      }
443    }
444  }
445  /* catch the last field, which might not be null terminated */
446  if (count == j - 1) {
[6201646]447    char *tmp, *out;
448    tmp = owl_malloc(n->z_message_len-save+5);
449    memcpy(tmp, n->z_message+save, n->z_message_len-save);
450    tmp[n->z_message_len-save]='\0';
451    out = owl_validate_or_convert(tmp);
452    owl_free(tmp);
453    return out;
[5376a95]454  }
455
456  return(owl_strdup(""));
457}
[09489b89]458#else
[701a184]459char *owl_zephyr_get_field(void *n, int j)
[09489b89]460{
[b0430a6]461  return(owl_strdup(""));
[09489b89]462}
[5577606]463char *owl_zephyr_get_field_as_utf8(void *n, int j)
[5376a95]464{
465  return owl_zephyr_get_field(n, j);
466}
[be0a79f]467#endif
[7d4fbcd]468
[b0430a6]469
[be0a79f]470#ifdef HAVE_LIBZEPHYR
[31e48a3]471int owl_zephyr_get_num_fields(ZNotice_t *n)
472{
[7d4fbcd]473  int i, fields;
474
[50e29e3]475  if(n->z_message_len == 0)
476    return 0;
477
[7d4fbcd]478  fields=1;
479  for (i=0; i<n->z_message_len; i++) {
480    if (n->z_message[i]=='\0') fields++;
481  }
482 
483  return(fields);
484}
[09489b89]485#else
486int owl_zephyr_get_num_fields(void *n)
487{
488  return(0);
489}
[be0a79f]490#endif
[7d4fbcd]491
[be0a79f]492#ifdef HAVE_LIBZEPHYR
[bf73bdd]493/* return a pointer to the message, place the message length in k
494 * caller must free the return
495 */
[85d1795]496char *owl_zephyr_get_message(ZNotice_t *n, owl_message *m)
[31e48a3]497{
[405d5e6]498  /* don't let ping messages have a body */
[7d4fbcd]499  if (!strcasecmp(n->z_opcode, "ping")) {
[bf73bdd]500    return(owl_strdup(""));
[7d4fbcd]501  }
502
[a1bb198]503  /* deal with MIT NOC messages */
[85d1795]504  if (!strcasecmp(n->z_default_format, "@center(@bold(NOC Message))\n\n@bold(Sender:) $1 <$sender>\n@bold(Time:  ) $time\n\n@italic($opcode service on $instance $3.) $4\n")) {
505    char *msg, *field3, *field4;
[a1bb198]506
507    field3 = owl_zephyr_get_field(n, 3);
508    field4 = owl_zephyr_get_field(n, 4);
509
[85d1795]510    msg = owl_sprintf("%s service on %s %s\n%s", n->z_opcode, n->z_class_inst, field3, field4);
[272a4a0]511    owl_free(field3);
512    owl_free(field4);
[a1bb198]513    if (msg) {
514      return msg;
515    }
516  }
[fba0f96]517  /* deal with MIT Discuss messages */
[85d1795]518  else if (!strcasecmp(n->z_default_format, "New transaction [$1] entered in $2\nFrom: $3 ($5)\nSubject: $4")) {
[fba0f96]519    char *msg, *field1, *field2, *field3, *field4, *field5;
520   
521    field1 = owl_zephyr_get_field(n, 1);
522    field2 = owl_zephyr_get_field(n, 2);
523    field3 = owl_zephyr_get_field(n, 3);
524    field4 = owl_zephyr_get_field(n, 4);
525    field5 = owl_zephyr_get_field(n, 5);
526   
527    msg = owl_sprintf("New transaction [%s] entered in %s\nFrom: %s (%s)\nSubject: %s", field1, field2, field3, field5, field4);
528    owl_free(field1);
529    owl_free(field2);
530    owl_free(field3);
531    owl_free(field4);
532    owl_free(field5);
533    if (msg) {
534      return msg;
535    }
536  }
[85d1795]537  /* deal with MIT Moira messages */
538  else if (!strcasecmp(n->z_default_format, "MOIRA $instance on $fromhost:\n $message\n")) {
539    char *msg, *field1;
540   
541    field1 = owl_zephyr_get_field(n, 1);
542   
543    msg = owl_sprintf("MOIRA %s on %s: %s", n->z_class_inst, owl_message_get_hostname(m), field1);
544    owl_free(field1);
545    if (msg) {
546      return msg;
547    }
548  }
[405d5e6]549
[85d1795]550  if (owl_zephyr_get_num_fields(n) == 1) {
551    return(owl_zephyr_get_field(n, 1));
552  }
553  else {
554    return(owl_zephyr_get_field(n, 2));
555  }
[7d4fbcd]556}
[be0a79f]557#endif
[7d4fbcd]558
[be0a79f]559#ifdef HAVE_LIBZEPHYR
[31e48a3]560char *owl_zephyr_get_zsig(ZNotice_t *n, int *k)
561{
[7d4fbcd]562  /* return a pointer to the zsig if there is one */
563
[405d5e6]564  /* message length 0? No zsig */
[7d4fbcd]565  if (n->z_message_len==0) {
566    *k=0;
567    return("");
568  }
[405d5e6]569
[85d1795]570  /* If there's only one field, no zsig */
571  if (owl_zephyr_get_num_fields(n) == 1) {
572    *k=0;
[405d5e6]573    return("");
574  }
575
576  /* Everything else is field 1 */
[7d4fbcd]577  *k=strlen(n->z_message);
578  return(n->z_message);
579}
[09489b89]580#else
581char *owl_zephyr_get_zsig(void *n, int *k)
582{
583  return("");
584}
[be0a79f]585#endif
[7d4fbcd]586
[31e48a3]587int send_zephyr(char *opcode, char *zsig, char *class, char *instance, char *recipient, char *message)
588{
[be0a79f]589#ifdef HAVE_LIBZEPHYR
[7d4fbcd]590  int ret;
591  ZNotice_t notice;
592   
593  memset(&notice, 0, sizeof(notice));
594
[8262340]595  ZResetAuthentication();
[8ba37ec]596
597  if (!zsig) zsig="";
[8262340]598 
[7d4fbcd]599  notice.z_kind=ACKED;
600  notice.z_port=0;
601  notice.z_class=class;
602  notice.z_class_inst=instance;
603  if (!strcmp(recipient, "*") || !strcmp(recipient, "@")) {
604    notice.z_recipient="";
605  } else {
606    notice.z_recipient=recipient;
607  }
608  notice.z_default_format="Class $class, Instance $instance:\nTo: @bold($recipient) at $time $date\nFrom: @bold{$1 <$sender>}\n\n$2";
[247cbc9]609  if (*owl_global_get_zsender(&g))
610      notice.z_sender=owl_global_get_zsender(&g);
611  else
612      notice.z_sender=NULL;
[7d4fbcd]613  if (opcode) notice.z_opcode=opcode;
614
[56330ff]615  notice.z_message_len=strlen(zsig)+1+strlen(message);
[7d4fbcd]616  notice.z_message=owl_malloc(notice.z_message_len+10);
[56330ff]617  strcpy(notice.z_message, zsig);
618  memcpy(notice.z_message+strlen(zsig)+1, message, strlen(message));
[7d4fbcd]619
620  /* ret=ZSendNotice(&notice, ZAUTH); */
621  ret=ZSrvSendNotice(&notice, ZAUTH, send_zephyr_helper);
622 
623  /* free then check the return */
624  owl_free(notice.z_message);
625  ZFreeNotice(&notice);
626  if (ret!=ZERR_NONE) {
[ec6ff52]627    owl_function_error("Error sending zephyr");
[7d4fbcd]628    return(ret);
629  }
630  return(0);
[be0a79f]631#else
632  return(0);
633#endif
[7d4fbcd]634}
635
[be0a79f]636#ifdef HAVE_LIBZEPHYR
[31e48a3]637Code_t send_zephyr_helper(ZNotice_t *notice, char *buf, int len, int wait)
638{
[7d4fbcd]639  return(ZSendPacket(buf, len, 0));
640}
[be0a79f]641#endif
[7d4fbcd]642
[31e48a3]643void send_ping(char *to)
644{
[be0a79f]645#ifdef HAVE_LIBZEPHYR
[7d4fbcd]646  send_zephyr("PING", "", "MESSAGE", "PERSONAL", to, "");
[be0a79f]647#endif
[7d4fbcd]648}
649
[be0a79f]650#ifdef HAVE_LIBZEPHYR
[31e48a3]651void owl_zephyr_handle_ack(ZNotice_t *retnotice)
652{
[7d4fbcd]653  char *tmp;
654 
655  /* if it's an HMACK ignore it */
656  if (retnotice->z_kind == HMACK) return;
[aecf3e6]657
[7d4fbcd]658  if (retnotice->z_kind == SERVNAK) {
[ec6ff52]659    owl_function_error("Authorization failure sending zephyr");
[7d4fbcd]660  } else if ((retnotice->z_kind != SERVACK) || !retnotice->z_message_len) {
[ec6ff52]661    owl_function_error("Detected server failure while receiving acknowledgement");
[7d4fbcd]662  } else if (!strcmp(retnotice->z_message, ZSRVACK_SENT)) {
663    if (!strcasecmp(retnotice->z_opcode, "ping")) {
664      return;
665    } else if (!strcasecmp(retnotice->z_class, "message") &&
666               !strcasecmp(retnotice->z_class_inst, "personal")) {
[4b464a4]667      tmp=short_zuser(retnotice->z_recipient);
[b4db911]668      owl_function_makemsg("Message sent to %s.", tmp);
[7d4fbcd]669      free(tmp);
670    } else {
[b4db911]671      owl_function_makemsg("Message sent to -c %s -i %s\n", retnotice->z_class, retnotice->z_class_inst);
[7d4fbcd]672    }
673  } else if (!strcmp(retnotice->z_message, ZSRVACK_NOTSENT)) {
[1151f0b]674    #define BUFFLEN 1024
[44f32fb]675    if (retnotice->z_recipient == NULL
[60c2e1e]676        || *retnotice->z_recipient == 0
[44f32fb]677        || *retnotice->z_recipient == '@') {
[1151f0b]678      char buff[BUFFLEN];
[44f32fb]679      owl_function_error("No one subscribed to class %s", retnotice->z_class);
[1151f0b]680      snprintf(buff, BUFFLEN, "Could not send message to class %s: no one subscribed.\n", retnotice->z_class);
[9119a47]681      owl_function_adminmsg("", buff);
[7d4fbcd]682    } else {
[1151f0b]683      char buff[BUFFLEN];
[4b464a4]684      tmp = short_zuser(retnotice->z_recipient);
[44f32fb]685      owl_function_error("%s: Not logged in or subscribing.", tmp);
686      if(strcmp(retnotice->z_class, "message")) {
[1151f0b]687        snprintf(buff, BUFFLEN,
[10e3963]688                 "Could not send message to %s: "
689                 "not logged in or subscribing to class %s, instance %s.\n", 
690                 tmp,
[1151f0b]691                 retnotice->z_class,
692                 retnotice->z_class_inst);
[44f32fb]693      } else {
[1151f0b]694        snprintf(buff, BUFFLEN,
[10e3963]695                 "Could not send message to %s: "
696                 "not logged in or subscribing to messages.\n",
697                 tmp);
[44f32fb]698      }
[9119a47]699      owl_function_adminmsg("", buff);
[180cd15]700      owl_log_outgoing_zephyr_error(tmp, buff);
[1c6c4d3]701      owl_free(tmp);
[7d4fbcd]702    }
703  } else {
[ec6ff52]704    owl_function_error("Internal error on ack (%s)", retnotice->z_message);
[7d4fbcd]705  }
706}
[09489b89]707#else
708void owl_zephyr_handle_ack(void *retnotice)
709{
710}
[be0a79f]711#endif
[7d4fbcd]712
[be0a79f]713#ifdef HAVE_LIBZEPHYR
[31e48a3]714int owl_zephyr_notice_is_ack(ZNotice_t *n)
715{
[7d4fbcd]716  if (n->z_kind == SERVNAK || n->z_kind == SERVACK || n->z_kind == HMACK) {
717    if (!strcasecmp(n->z_class, LOGIN_CLASS)) return(0);
718    return(1);
719  }
720  return(0);
721}
[09489b89]722#else
723int owl_zephyr_notice_is_ack(void *n)
724{
725  return(0);
726}
[be0a79f]727#endif
[7d4fbcd]728 
[31e48a3]729void owl_zephyr_zaway(owl_message *m)
730{
[be0a79f]731#ifdef HAVE_LIBZEPHYR
[7c8060d0]732  char *tmpbuff, *myuser, *to;
[15b34fd]733  owl_message *mout;
[7d4fbcd]734 
[aa2f6364]735  /* bail if it doesn't look like a message we should reply to.  Some
[2de4f20]736   * of this defined by the way zaway(1) works
737   */
[7d4fbcd]738  if (strcasecmp(owl_message_get_class(m), "message")) return;
739  if (strcasecmp(owl_message_get_recipient(m), ZGetSender())) return;
740  if (!strcasecmp(owl_message_get_sender(m), "")) return;
741  if (!strcasecmp(owl_message_get_opcode(m), "ping")) return;
742  if (!strcasecmp(owl_message_get_opcode(m), "auto")) return;
[d023c25]743  if (!strcasecmp(owl_message_get_zsig(m), "Automated reply:")) return;
[7d4fbcd]744  if (!strcasecmp(owl_message_get_sender(m), ZGetSender())) return;
[9854278]745  if (owl_message_get_attribute_value(m, "isauto")) return;
[7d4fbcd]746
[7c8060d0]747  if (owl_global_is_smartstrip(&g)) {
[e3d9c77]748    to=owl_zephyr_smartstripped_user(owl_message_get_sender(m));
[7c8060d0]749  } else {
750    to=owl_strdup(owl_message_get_sender(m));
751  }
752
[7d4fbcd]753  send_zephyr("",
754              "Automated reply:",
755              owl_message_get_class(m),
756              owl_message_get_instance(m),
[7c8060d0]757              to,
[7d4fbcd]758              owl_global_get_zaway_msg(&g));
759
[7c8060d0]760  myuser=short_zuser(to);
[aa2f6364]761  if (!strcasecmp(owl_message_get_instance(m), "personal")) {
762    tmpbuff = owl_sprintf("zwrite %s", myuser);
763  } else {
764    tmpbuff = owl_sprintf("zwrite -i %s %s", owl_message_get_instance(m), myuser);
765  }
766  owl_free(myuser);
[7c8060d0]767  owl_free(to);
[aa2f6364]768
[7d4fbcd]769  /* display the message as an admin message in the receive window */
[15b34fd]770  mout=owl_function_make_outgoing_zephyr(owl_global_get_zaway_msg(&g), tmpbuff, "Automated reply:");
[13a3c1db]771  owl_global_messagequeue_addmsg(&g, mout);
[7d4fbcd]772  owl_free(tmpbuff);
[be0a79f]773#endif
[7d4fbcd]774}
775
[be0a79f]776#ifdef HAVE_LIBZEPHYR
[31e48a3]777void owl_zephyr_hackaway_cr(ZNotice_t *n)
778{
[7d4fbcd]779  /* replace \r's with ' '.  Gross-ish */
780  int i;
781
782  for (i=0; i<n->z_message_len; i++) {
783    if (n->z_message[i]=='\r') {
784      n->z_message[i]=' ';
785    }
786  }
787}
[be0a79f]788#endif
[7d4fbcd]789
[31e48a3]790void owl_zephyr_zlocate(char *user, char *out, int auth)
791{
[be0a79f]792#ifdef HAVE_LIBZEPHYR
[7d4fbcd]793  int ret, numlocs;
794  int one = 1;
795  ZLocations_t locations;
796  char *myuser;
797 
798  strcpy(out, "");
[8262340]799  ZResetAuthentication();
[7d4fbcd]800  ret=ZLocateUser(user,&numlocs,auth?ZAUTH:ZNOAUTH);
801  if (ret != ZERR_NONE) {
[667a1b6]802    sprintf(out, "Error locating user %s\n", user);
803    return;
[7d4fbcd]804  }
805
806  if (numlocs==0) {
[2527615]807    myuser=short_zuser(user);
808    sprintf(out, "%s: Hidden or not logged-in\n", myuser);
809    owl_free(myuser);
[7d4fbcd]810    return;
811  }
812   
813  for (;numlocs;numlocs--) {
814    ZGetLocations(&locations,&one);
[4b464a4]815    myuser=short_zuser(user);
[667a1b6]816    sprintf(out, "%s%s: %s\t%s\t%s\n", out, myuser,
817            locations.host ? locations.host : "?",
818            locations.tty ? locations.tty : "?",
819            locations.time ? locations.time : "?");
[7d4fbcd]820    owl_free(myuser);
821  }
[be0a79f]822#endif
[7d4fbcd]823}
[bde7714]824
[31e48a3]825void owl_zephyr_addsub(char *filename, char *class, char *inst, char *recip)
826{
[be0a79f]827#ifdef HAVE_LIBZEPHYR
[bde7714]828  char *line, subsfile[LINE], buff[LINE];
829  FILE *file;
830
831  line=owl_zephyr_makesubline(class, inst, recip);
832
833  if (filename==NULL) {
834    sprintf(subsfile, "%s/%s", owl_global_get_homedir(&g), ".zephyr.subs");
835  } else {
836    strcpy(subsfile, filename);
837  }
838
[74037d9]839  /* if the file already exists, check to see if the sub is already there */
[bde7714]840  file=fopen(subsfile, "r");
[74037d9]841  if (file) {
842    while (fgets(buff, LINE, file)!=NULL) {
843      if (!strcasecmp(buff, line)) {
844        owl_function_error("Subscription already present in %s", subsfile);
845        owl_free(line);
[e78397d]846        fclose(file);
[74037d9]847        return;
848      }
[bde7714]849    }
[99dabee]850    fclose(file);
[bde7714]851  }
852
853  /* if we get here then we didn't find it */
854  file=fopen(subsfile, "a");
855  if (!file) {
[ec6ff52]856    owl_function_error("Error opening file %s for writing", subsfile);
[bde7714]857    owl_free(line);
858    return;
859  }
860  fputs(line, file);
861  fclose(file);
862  owl_function_makemsg("Subscription added");
863 
864  owl_free(line);
[be0a79f]865#endif
[bde7714]866}
867
[31e48a3]868void owl_zephyr_delsub(char *filename, char *class, char *inst, char *recip)
869{
[be0a79f]870#ifdef HAVE_LIBZEPHYR
[b2a91b6]871  char *line, *subsfile;
[bde7714]872 
873  line=owl_zephyr_makesubline(class, inst, recip);
[b2a91b6]874  line[strlen(line)-1]='\0';
[bde7714]875
[b2a91b6]876  if (!filename) {
877    subsfile=owl_sprintf("%s/.zephyr.subs", owl_global_get_homedir(&g));
[bde7714]878  } else {
[b2a91b6]879    subsfile=owl_strdup(filename);
[bde7714]880  }
[b2a91b6]881 
882  owl_util_file_deleteline(subsfile, line, 1);
883  owl_free(subsfile);
[bde7714]884  owl_free(line);
885  owl_function_makemsg("Subscription removed");
[be0a79f]886#endif
[bde7714]887}
888
[b2a91b6]889/* caller must free the return */
[31e48a3]890char *owl_zephyr_makesubline(char *class, char *inst, char *recip)
891{
[bde7714]892  char *out;
893
894  out=owl_malloc(strlen(class)+strlen(inst)+strlen(recip)+30);
895  sprintf(out, "%s,%s,%s\n", class, inst, !strcmp(recip, "") ? "*" : recip);
896  return(out);
897}
[31e48a3]898
899
900void owl_zephyr_zlog_in(void)
901{
[be0a79f]902#ifdef HAVE_LIBZEPHYR
[31e48a3]903  char *exposure, *eset;
904  int ret;
905
906  ZResetAuthentication();
907   
908  eset=EXPOSE_REALMVIS;
909  exposure=ZGetVariable("exposure");
910  if (exposure==NULL) {
911    eset=EXPOSE_REALMVIS;
912  } else if (!strcasecmp(exposure,EXPOSE_NONE)) {
913    eset = EXPOSE_NONE;
914  } else if (!strcasecmp(exposure,EXPOSE_OPSTAFF)) {
915    eset = EXPOSE_OPSTAFF;
916  } else if (!strcasecmp(exposure,EXPOSE_REALMVIS)) {
917    eset = EXPOSE_REALMVIS;
918  } else if (!strcasecmp(exposure,EXPOSE_REALMANN)) {
919    eset = EXPOSE_REALMANN;
920  } else if (!strcasecmp(exposure,EXPOSE_NETVIS)) {
921    eset = EXPOSE_NETVIS;
922  } else if (!strcasecmp(exposure,EXPOSE_NETANN)) {
923    eset = EXPOSE_NETANN;
924  }
925   
926  ret=ZSetLocation(eset);
927  if (ret != ZERR_NONE) {
928    /*
929      char buff[LINE];
930      sprintf(buff, "Error setting location: %s", error_message(ret));
931      owl_function_makemsg(buff);
932    */
933  }
[be0a79f]934#endif
[31e48a3]935}
936
937void owl_zephyr_zlog_out(void)
938{
[be0a79f]939#ifdef HAVE_LIBZEPHYR
[31e48a3]940  int ret;
941
942  ZResetAuthentication();
943  ret=ZUnsetLocation();
944  if (ret != ZERR_NONE) {
945    /*
946      char buff[LINE];
947      sprintf(buff, "Error unsetting location: %s", error_message(ret));
948      owl_function_makemsg(buff);
949    */
950  }
[be0a79f]951#endif
[31e48a3]952}
953
[65ad073]954void owl_zephyr_addbuddy(char *name)
955{
956  char *filename;
957  FILE *file;
958 
959  filename=owl_sprintf("%s/.anyone", owl_global_get_homedir(&g));
960  file=fopen(filename, "a");
961  owl_free(filename);
962  if (!file) {
[ec6ff52]963    owl_function_error("Error opening zephyr buddy file for append");
[65ad073]964    return;
965  }
966  fprintf(file, "%s\n", name);
967  fclose(file);
968}
969
970void owl_zephyr_delbuddy(char *name)
971{
972  char *filename;
973
974  filename=owl_sprintf("%s/.anyone", owl_global_get_homedir(&g));
975  owl_util_file_deleteline(filename, name, 0);
976  owl_free(filename);
977}
[9381782]978
979/* return auth string */
[09489b89]980#ifdef HAVE_LIBZEPHYR
[9381782]981char *owl_zephyr_get_authstr(ZNotice_t *n)
982{
983
984  if (!n) return("UNKNOWN");
985
986  if (n->z_auth == ZAUTH_FAILED) {
987    return ("FAILED");
988  } else if (n->z_auth == ZAUTH_NO) {
989    return ("NO");
990  } else if (n->z_auth == ZAUTH_YES) {
991    return ("YES");
992  } else {
993    return ("UNKNOWN");
994  }           
995}
[09489b89]996#else
997char *owl_zephyr_get_authstr(void *n)
998{
999  return("");
1000}
1001#endif
[9381782]1002
[2de4f20]1003/* Returns a buffer of subscriptions or an error message.  Caller must
1004 * free the return.
[09489b89]1005 */
1006char *owl_zephyr_getsubs()
1007{
1008#ifdef HAVE_LIBZEPHYR
1009  int ret, num, i, one;
[ddacb9c]1010  int buffsize;
[09489b89]1011  ZSubscription_t sub;
[ddacb9c]1012  char *out;
[09489b89]1013  one=1;
1014
1015  ret=ZRetrieveSubscriptions(0, &num);
1016  if (ret==ZERR_TOOMANYSUBS) {
[c8735aa]1017    return(owl_strdup("Zephyr: too many subscriptions\n"));
[ddacb9c]1018  } else if (ret || (num <= 0)) {
[c8735aa]1019    return(owl_strdup("Zephyr: error retriving subscriptions\n"));
[09489b89]1020  }
1021
[ddacb9c]1022  buffsize = (num + 1) * 50;
1023  out=owl_malloc(buffsize);
[09489b89]1024  strcpy(out, "");
1025  for (i=0; i<num; i++) {
1026    if ((ret = ZGetSubscriptions(&sub, &one)) != ZERR_NONE) {
1027      owl_free(out);
1028      ZFlushSubscriptions();
1029      out=owl_strdup("Error while getting subscriptions\n");
1030      return(out);
1031    } else {
[ddacb9c]1032      int tmpbufflen;
1033      char *tmpbuff;
1034      tmpbuff = owl_sprintf("<%s,%s,%s>\n%s", sub.zsub_class, sub.zsub_classinst, sub.zsub_recipient, out);
1035      tmpbufflen = strlen(tmpbuff) + 1;
1036      if (tmpbufflen > buffsize) {
1037        char *out2;
1038        buffsize = tmpbufflen * 2;
1039        out2 = owl_realloc(out, buffsize);
1040        if (out2 == NULL) {
1041          owl_free(out);
1042          owl_free(tmpbuff);
1043          ZFlushSubscriptions();
1044          out=owl_strdup("Realloc error while getting subscriptions\n");
1045          return(out);   
1046        }
1047        out = out2;
1048      }
[09489b89]1049      strcpy(out, tmpbuff);
[ddacb9c]1050      owl_free(tmpbuff);
[09489b89]1051    }
1052  }
1053
1054  ZFlushSubscriptions();
1055  return(out);
1056#else
[12c35df]1057  return(owl_strdup("Zephyr not available"));
[09489b89]1058#endif
1059}
1060
1061char *owl_zephyr_get_variable(char *var)
1062{
1063#ifdef HAVE_LIBZEPHYR
1064  return(ZGetVariable(var));
1065#else
1066  return("");
1067#endif
1068}
1069
1070void owl_zephyr_set_locationinfo(char *host, char *val)
1071{
1072#ifdef HAVE_LIBZEPHYR
1073  ZInitLocationInfo(host, val);
1074#endif
1075}
1076 
[e3d9c77]1077/* Strip a local realm fron the zephyr user name.
1078 * The caller must free the return
1079 */
1080char *short_zuser(char *in)
1081{
1082  char *out, *ptr;
1083
1084  out=owl_strdup(in);
1085  ptr=strchr(out, '@');
1086  if (ptr) {
1087    if (!strcasecmp(ptr+1, owl_zephyr_get_realm())) {
1088      *ptr='\0';
1089    }
1090  }
1091  return(out);
1092}
1093
1094/* Append a local realm to the zephyr user name if necessary.
1095 * The caller must free the return.
1096 */
1097char *long_zuser(char *in)
1098{
[1971b59]1099  if (strchr(in, '@')) {
1100    return(owl_strdup(in));
[e3d9c77]1101  }
[1971b59]1102  return(owl_sprintf("%s@%s", in, owl_zephyr_get_realm()));
[e3d9c77]1103}
1104
1105/* strip out the instance from a zsender's principal.  Preserves the
1106 * realm if present.  daemon.webzephyr is a special case.  The
1107 * caller must free the return
1108 */
1109char *owl_zephyr_smartstripped_user(char *in)
1110{
1111  char *ptr, *realm, *out;
1112
1113  out=owl_strdup(in);
1114
1115  /* bail immeaditly if we don't have to do any work */
1116  ptr=strchr(in, '.');
1117  if (!strchr(in, '/') && !ptr) {
1118    /* no '/' and no '.' */
1119    return(out);
1120  }
1121  if (ptr && strchr(in, '@') && (ptr > strchr(in, '@'))) {
1122    /* There's a '.' but it's in the realm */
1123    return(out);
1124  }
1125  if (!strncasecmp(in, OWL_WEBZEPHYR_PRINCIPAL, strlen(OWL_WEBZEPHYR_PRINCIPAL))) {
1126    return(out);
1127  }
1128
1129  /* remove the realm from ptr, but hold on to it */
1130  realm=strchr(out, '@');
1131  if (realm) realm[0]='\0';
1132
1133  /* strip */
1134  ptr=strchr(out, '.');
1135  if (!ptr) ptr=strchr(out, '/');
1136  ptr[0]='\0';
1137
1138  /* reattach the realm if we had one */
1139  if (realm) {
1140    strcat(out, "@");
1141    strcat(out, realm+1);
1142  }
1143
1144  return(out);
1145}
[5a95b69]1146
1147/* read the list of users in 'filename' as a .anyone file, and put the
1148 * names of the zephyr users in the list 'in'.  If 'filename' is NULL,
1149 * use the default .anyone file in the users home directory.  Returns
1150 * -1 on failure, 0 on success.
1151 */
1152int owl_zephyr_get_anyone_list(owl_list *in, char *filename)
1153{
1154#ifdef HAVE_LIBZEPHYR
1155  char *ourfile, *tmp, buff[LINE];
1156  FILE *f;
1157
1158  if (filename==NULL) {
1159    tmp=owl_global_get_homedir(&g);
1160    ourfile=owl_sprintf("%s/.anyone", owl_global_get_homedir(&g));
1161  } else {
1162    ourfile=owl_strdup(filename);
1163  }
1164 
1165  f=fopen(ourfile, "r");
1166  if (!f) {
1167    owl_function_error("Error opening file %s: %s", ourfile, strerror(errno) ? strerror(errno) : "");
1168    owl_free(ourfile);
1169    return(-1);
1170  }
1171
1172  while (fgets(buff, LINE, f)!=NULL) {
1173    /* ignore comments, blank lines etc. */
1174    if (buff[0]=='#') continue;
1175    if (buff[0]=='\n') continue;
1176    if (buff[0]=='\0') continue;
1177   
1178    /* strip the \n */
1179    buff[strlen(buff)-1]='\0';
1180   
1181    /* ingore from # on */
1182    tmp=strchr(buff, '#');
1183    if (tmp) tmp[0]='\0';
1184   
1185    /* ingore from SPC */
1186    tmp=strchr(buff, ' ');
1187    if (tmp) tmp[0]='\0';
1188   
1189    /* stick on the local realm. */
1190    if (!strchr(buff, '@')) {
1191      strcat(buff, "@");
1192      strcat(buff, ZGetRealm());
1193    }
1194    owl_list_append_element(in, owl_strdup(buff));
1195  }
1196  fclose(f);
1197  owl_free(ourfile);
1198  return(0);
1199#else
1200  return(-1);
1201#endif
1202}
[13a3c1db]1203
1204#ifdef HAVE_LIBZEPHYR
1205void owl_zephyr_process_events() {
1206  int zpendcount=0;
1207  ZNotice_t notice;
1208  struct sockaddr_in from;
1209  owl_message *m=NULL;
1210
1211  while(owl_zephyr_zpending() && zpendcount < 20) {
1212    if (owl_zephyr_zpending()) {
1213      ZReceiveNotice(&notice, &from);
1214      zpendcount++;
1215
1216      /* is this an ack from a zephyr we sent? */
1217      if (owl_zephyr_notice_is_ack(&notice)) {
1218        owl_zephyr_handle_ack(&notice);
1219        continue;
1220      }
1221
1222      /* if it's a ping and we're not viewing pings then skip it */
1223      if (!owl_global_is_rxping(&g) && !strcasecmp(notice.z_opcode, "ping")) {
1224        continue;
1225      }
1226
1227      /* create the new message */
1228      m=owl_malloc(sizeof(owl_message));
1229      owl_message_create_from_znotice(m, &notice);
1230
1231      owl_global_messagequeue_addmsg(&g, m);
1232    }
1233  }
1234}
1235
1236#else
1237void owl_zephyr_process_events() {
1238 
1239}
1240#endif
Note: See TracBrowser for help on using the repository browser.