]> andersk Git - moira.git/blobdiff - server/qrtn.qc
Avoid aborting a transaction that has been deadlocked; and
[moira.git] / server / qrtn.qc
index 10e31d36ddaaec52f4cb470f846815dfaf27c772..12bbfa1d7013bbae91597a5b369ee4a23f53d653 100644 (file)
@@ -3,44 +3,7 @@
  *     $Author$
  *     $Header$
  *
- *     Copyright (C) 1987 by the Massachusetts Institute of Technology
- *
- *     $Log$
- *     Revision 1.12  1987-09-12 20:42:11  wesommer
- *     Clean up after Gretzinger: cl->kname is not valid unless cl->clname is
- *     non-NULL.
- *
- * Revision 1.12  87/09/12  20:06:46  wesommer
- * Fix security hole/null dereference bug: if clname is NULL, return
- * permission denied in get_client.
- * 
- * Revision 1.11  87/09/01  16:10:01  wesommer
- * This change was made by Mike, who didn't feel like checking it in.
- * Temp hack: ignore instances.
- * 
- * Revision 1.10  87/08/28  14:57:51  mike
- * Modified sms_query to not enclose RETRIEVE queries in begin/end transaction.
- * This was necessary to allow get_all_poboxes and get_groups_of_all_users
- * to temporarily change the Ingres lockmode.
- * 
- * Revision 1.9  87/08/22  17:47:38  wesommer
- * Cleanup (these changes were by Mike).
- * 
- * Revision 1.8  87/08/10  16:22:26  mike
- * wesommer modified error reporting.
- * 
- * Revision 1.7  87/08/04  01:49:20  wesommer
- * Rearranged messages.
- * 
- * Revision 1.6  87/08/04  01:30:54  wesommer
- * Mike's changes; checked in prior to working over messages.
- * 
- * Revision 1.5  87/06/21  16:37:58  wesommer
- * Changed include files, reindented things.
- * 
- * 
- * Revision 1.4  87/06/08  05:03:27  wesommer
- * Reindented; added header and trailer.
+ *     Copyright (C) 1987, 1988 by the Massachusetts Institute of Technology
  * 
  */
 
@@ -51,12 +14,15 @@ static char *rcsid_qrtn_qc = "$Header$";
 #include "query.h"
 #include "sms_server.h"
 
-#define SMS_SUCCESS 0
-
 char *Argv[16];
 
-static int ingres_errno = 0;
+int ingres_errno = 0;
 extern char *whoami;
+extern FILE *journal;
+
+#define INGRES_BAD_INT 4111
+#define INGRES_BAD_DATE 4302
+#define INGRES_DEADLOCK 4700
 
 /*
  * ingerr: (supposedly) called when Ingres indicates an error.
@@ -67,18 +33,35 @@ extern char *whoami;
 static int ingerr(num)
     int *num;
 {
-    ingres_errno = SMS_INGRES_ERR;
-    com_err(whoami, SMS_INGRES_ERR, " code %d\n", ingres_errno);
-    return *num;
+    char buf[256];
+
+    switch (*num) {
+    case INGRES_BAD_INT:
+       ingres_errno = SMS_INTEGER;
+       break;
+    case INGRES_BAD_DATE:
+       ingres_errno = SMS_DATE;
+       break;
+    case INGRES_DEADLOCK:
+       ingres_errno = SMS_DEADLOCK;
+       break;
+    default:
+       ingres_errno = SMS_INGRES_ERR;
+       com_err(whoami, SMS_INGRES_ERR, " code %d\n", *num);
+       critical_alert("SMS", "SMS server encountered INGRES ERROR %d", *num);
+       return (*num);
+    }
+    return (0);
 }
 
 int sms_open_database()
 {
     register int i;
+    char *malloc();
 
     /* initialize local argv */
     for (i = 0; i < 16; i++)
-       Argv[i] = (char *)malloc(128);
+       Argv[i] = malloc(ARGLEN);
 
     IIseterr(ingerr);
        
@@ -86,6 +69,7 @@ int sms_open_database()
        
     /* open the database */
 ##  ingres sms
+##  set lockmode session where readlock = exclusive
     return ingres_errno;
 }
 
@@ -100,52 +84,14 @@ sms_check_access(cl, name, argc, argv_ro)
     int argc;
     char *argv_ro[];
 {
-    register struct query *q;
-    register int argreq;
-    register int status;
-    register struct validate *v;
-    register int i;
-    register int privileged;
+    struct query *q;
     struct query *get_query_by_name();
-    int access_user();
-    int access_pop();
-    int access_list();
 
-    q = get_query_by_name(name);
-    if (q == (struct query *)0) return(SMS_NO_HANDLE);
-    v = q->validate;
+    q = get_query_by_name(name, cl->args->sms_version_no);
+    if (q == (struct query *)0)
+       return(SMS_NO_HANDLE);
 
-    /* copy the arguments into a local argv that we can modify */
-    for (i = 0; i < argc; i++)
-       strcpy(Argv[i], argv_ro[i]);
-
-    /* check initial query access */
-    status = check_query_access(q, Argv, cl);
-    privileged = (status == SMS_SUCCESS) ? 1 : 0;
-    if (status != SMS_SUCCESS && !(v && (v->pre_rtn == access_user ||
-                                        v->pre_rtn == access_pop ||
-                                        v->pre_rtn == access_list))) 
-       return(status);
-
-    /* check argument count */
-    argreq = q->argc;
-    if (q->type == UPDATE || q->type == APPEND) argreq += q->vcnt;
-    if (argc != argreq) return(SMS_ARGS);
-
-    /* validate arguments */
-    if (v && v->valobj) {
-       status = validate_fields(q, Argv, v->valobj, v->objcnt);
-       if (status != SMS_SUCCESS) return(status);
-    }
-
-    /* perform special query access check */
-    if (v && v->pre_rtn) {
-       status = (*v->pre_rtn)(q, Argv, cl, 1);
-       if (status != SMS_SUCCESS && (status != SMS_PERM || !privileged)) 
-           return(status);
-    }
-
-    return(SMS_SUCCESS);
+    return(sms_verify_query(cl, q, argc, argv_ro));
 }
 
 sms_process_query(cl, name, argc, argv_ro, action, actarg)
@@ -157,11 +103,8 @@ sms_process_query(cl, name, argc, argv_ro, action, actarg)
     char *actarg;
 {
     register struct query *q;
-    register int i;
     register int status;
-    register int argreq;
     register struct validate *v;
-    int privileged;
     char qual[256];
     char sort[32];
     char *pqual;
@@ -172,63 +115,43 @@ sms_process_query(cl, name, argc, argv_ro, action, actarg)
     int sq_save_args();
     struct save_queue *sq_create();
     char *build_sort();
-    int access_user();
-    int access_pop();
-    int access_list();
-
-    /* copy the arguments into a local argv that we can modify */
-    for (i = 0; i < argc; i++)
-       strcpy(Argv[i], argv_ro[i]);
 
     /* list queries command */
     if (!strcmp(name, "_list_queries")) {
-       list_queries(action, actarg);
+       list_queries(cl->args->sms_version_no, action, actarg);
        return(SMS_SUCCESS);
     }
 
     /* help query command */
     if (!strcmp(name, "_help")) {
-       q = get_query_by_name(Argv[0]);
+       if (argc < 1)
+           return(SMS_ARGS);
+       q = get_query_by_name(argv_ro[0], cl->args->sms_version_no);
        if (q == (struct query *)0) return(SMS_NO_HANDLE);
        help_query(q, action, actarg);
        return(SMS_SUCCESS);
     }
 
     /* get query structure, return error if named query does not exist */
-    q = get_query_by_name(name);
+    q = get_query_by_name(name, cl->args->sms_version_no);
     if (q == (struct query *)0) return(SMS_NO_HANDLE);
     v = q->validate;
 
-    /* check query access */
-    status = check_query_access(q, Argv, cl);
-    privileged = (status == SMS_SUCCESS) ? 1 : 0;
-    if (!privileged && !(status == SMS_PERM &&
-                        (v && (v->pre_rtn == access_user ||
-                               v->pre_rtn == access_pop ||
-                               v->pre_rtn == access_list))))
-       return(status);
-
-    /* check argument count */
-    argreq = q->argc;
-    if (q->type == UPDATE || q->type == APPEND) argreq += q->vcnt;
-    if (argc != argreq) return(SMS_ARGS);
+    ingres_errno = 0;
+    if (q->type != RETRIEVE)
+##      begin transaction
 
-    /* validate arguments */
-    if (v && v->valobj) {
-       status = validate_fields(q, Argv, v->valobj, v->objcnt);
-       if (status != SMS_SUCCESS) return(status);
-    }
+    /* setup argument vector, verify access and arguments */
+    if ((status = sms_verify_query(cl, q, argc, argv_ro)) != SMS_SUCCESS)
+       goto out;
 
     /* perform any special query pre-processing */
     if (v && v->pre_rtn) {
        status = (*v->pre_rtn)(q, Argv, cl, 0);
-       if (status != SMS_SUCCESS && (status != SMS_PERM || !privileged))
-           return(status);
+       if (status != SMS_SUCCESS)
+           goto out;
     }
 
-    if (q->type != RETRIEVE)
-##      begin transaction
-
     switch (q->type) {
     case RETRIEVE:
        /* for queries that do not permit wildcarding, check if row
@@ -264,17 +187,14 @@ sms_process_query(cl, name, argc, argv_ro, action, actarg)
                    sq_destroy(sq);
                    break;
                }
-               status = (*v->post_rtn)(q, sq, v, action, actarg);
+               status = (*v->post_rtn)(q, sq, v, action, actarg, cl);
            } else {
                /* normal retrieve */
                status = do_retrieve(q, pqual, psort, action, actarg);
            }
            if (status != SMS_SUCCESS) break;
-           table = q->rtable;
-##          repeat replace tblstats (retrieves = tblstats.retrieves + 1)
-##                 where tblstats.#table = @table
        } else {
-           status = (*v->post_rtn)(q, Argv, action, actarg);
+           status = (*v->post_rtn)(q, Argv, cl, action, actarg);
        }
 
        break;
@@ -293,13 +213,15 @@ sms_process_query(cl, name, argc, argv_ro, action, actarg)
            status = do_update(q, &Argv[q->argc], qual, action, actarg);
            if (status != SMS_SUCCESS) break;
            table = q->rtable;
-##         repeat replace tblstats (updates = tblstats.updates + 1,
-##                                  modtime = "now")
-##             where tblstats.#table = @table
+           if (strcmp(q->shortname, "sshi") && strcmp(q->shortname, "ssif")) {
+##             repeat replace tblstats (updates = tblstats.updates + 1,
+##                                      modtime = "now")
+##                 where tblstats.#table = @table
+           }
        }
 
        /* execute followup routine (if any) */
-       if (v->post_rtn) status = (*v->post_rtn)(q, Argv);
+       if (v->post_rtn) status = (*v->post_rtn)(q, Argv, cl);
 
        break;
 
@@ -311,7 +233,10 @@ sms_process_query(cl, name, argc, argv_ro, action, actarg)
        }
 
        /* increment id number if necessary */
-       if (v->object_id) set_next_object_id(v->object_id);
+       if (v->object_id) {
+           status = set_next_object_id(v->object_id, q->rtable);
+           if (status != SMS_SUCCESS) break;
+       }
 
        /* build "where" clause if needed */
        if (q->qual) {
@@ -333,7 +258,7 @@ sms_process_query(cl, name, argc, argv_ro, action, actarg)
        }
        
        /* execute followup routine */
-       if (v->post_rtn) status = (*v->post_rtn)(q, Argv);
+       if (v->post_rtn) status = (*v->post_rtn)(q, Argv, cl);
        break;
 
     case DELETE:
@@ -356,16 +281,37 @@ sms_process_query(cl, name, argc, argv_ro, action, actarg)
        }
 
        /* execute followup routine */
-       if (v->post_rtn) status = (*v->post_rtn)(q, Argv);
+       if (v->post_rtn) status = (*v->post_rtn)(q, Argv, cl);
        break;
 
     }
 
+out:
     if (q->type != RETRIEVE) {
         if (status == SMS_SUCCESS) {
-##          end transaction
+##          end transaction    /* commit to this */
+           if (journal) {
+               char buf[1024], *bp;
+               int i;
+               extern time_t now;
+
+               fprintf(journal, "%% %s %s %s",
+                       cl->clname, cl->entity, ctime(&now));
+               fprintf(journal, "%s[%d] ", q->name, cl->args->sms_version_no);
+               for (i = 0; i < argc; i++) {
+                   if (i != 0) {
+                       putc(' ', journal);
+                   }
+                   requote(buf, argv_ro[i], sizeof(buf));
+                   fputs(buf, journal);
+               }
+               putc('\n', journal);
+               fflush(journal);
+           }
        } else {
-##          abort
+           if (status != SMS_DEADLOCK) {
+##             abort           /* it never happened */
+           }
        }
     }
 
@@ -383,10 +329,11 @@ build_qual(fmt, argc, argv, qual)
     register char *c;
     register int i;
     char *args[4];
+    char *index();
 
     c = fmt;
     for (i = 0; i < argc; i++) {
-       c = (char *)index(c, '%');
+       c = index(c, '%');
        if (c++ == (char *)0) return(SMS_ARGS);
        if (*c == 's')
            args[i] = argv[i];
@@ -417,6 +364,7 @@ build_qual(fmt, argc, argv, qual)
        sprintf(qual, fmt, args[0], args[1], args[2], args[3]);
        break;
     }
+    return(SMS_SUCCESS);
 }
 
 char *
@@ -444,6 +392,59 @@ build_sort(v, sort)
     return ((*sort) ? sort : 0);
 }
 
+
+/* Build arguement vector, verify query and arguments */
+
+sms_verify_query(cl, q, argc, argv_ro)
+    client *cl;
+    struct query *q;
+    int argc;
+    char *argv_ro[];
+{
+    register int argreq;
+    register int status;
+    register struct validate *v = q->validate;
+    register int i;
+    register int privileged = 0;
+
+    /* copy the arguments into a local argv that we can modify */
+    for (i = 0; i < argc; i++) {
+       if (strlen(argv_ro[i]) < ARGLEN)
+           strcpy(Argv[i], argv_ro[i]);
+       else
+           return(SMS_ARG_TOO_LONG);
+    }
+
+    /* check initial query access */
+    status = check_query_access(q, Argv, cl);
+    if (status != SMS_SUCCESS && status != SMS_PERM)
+       return(status);
+    if (status == SMS_SUCCESS)
+       privileged++;
+
+    /* check argument count */
+    argreq = q->argc;
+    if (q->type == UPDATE || q->type == APPEND) argreq += q->vcnt;
+    if (argc != argreq) return(SMS_ARGS);
+
+    /* validate arguments */
+    if (v && v->valobj) {
+       status = validate_fields(q, Argv, v->valobj, v->objcnt);
+       if (status != SMS_SUCCESS) return(status);
+    }
+
+    /* perform special query access check */
+    if (!privileged && v && v->acs_rtn) {
+       status = (*v->acs_rtn)(q, Argv, cl);
+       if (status != SMS_SUCCESS && status != SMS_PERM)
+           return(status);
+       if (status == SMS_SUCCESS)
+           privileged++;
+    }
+
+    return(privileged ? SMS_SUCCESS : SMS_PERM);
+}
+
 check_query_access(q, argv, cl)
     struct query *q;
     char *argv[];
@@ -482,7 +483,7 @@ check_query_access(q, argv, cl)
     if (status != SMS_SUCCESS) return(status);
 
     /* see if client is in the list (or any of its sub-lists) */
-    exists = find_member(acl_id, client_type, client_id, 0);
+    exists = find_member("LIST", acl_id, client_type, client_id, 0);
     return ((exists) ? SMS_SUCCESS : SMS_PERM);
 ##}
 
@@ -504,26 +505,27 @@ get_client(cl, client_type, client_id)
 
     /* if client is from local realm, get users_id */
     if (!strcmp(krb->realm, krb_realm)) {
-       name = krb->name;
-##     repeat retrieve (member_id = users.users_id) where users.login = @name
+       *client_id = cl->users_id;
        *client_type = "USER";
-    } else {
-       /* otherwise use string_id */
-       name = cl->clname;
-##     repeat retrieve (member_id = strings.string_id) 
-##          where strings.string = @name
-       *client_type = "STRING";
+       return(SMS_SUCCESS);
     }
+
+    /* otherwise use string_id */
+    name = cl->clname;
+##  repeat retrieve (member_id = strings.string_id) 
+##      where strings.string = @name
        
     /* make sure we found a users or string id */
 ##  inquire_equel (rowcount = "rowcount")
     if (rowcount == 0) return(SMS_PERM);
 
+    *client_type = "STRING";
     *client_id = member_id;
     return(SMS_SUCCESS);
 ##}
 
-##find_member(list_id, member_type, member_id, sq)
+##find_member(list_type, list_id, member_type, member_id, sq)
+    char *list_type;
 ##  int list_id;
 ##  char *member_type;
 ##  int member_id;
@@ -534,6 +536,10 @@ get_client(cl, client_type, client_id)
     int child;
     struct save_queue *sq_create();
 
+    if (!strcmp(strtrim(list_type), strtrim(member_type)) &&
+       list_id == member_id)
+       return(1);
+
     /* see if client is a direct member of list */
 ##  repeat retrieve (exists = any(m.#member_id where 
 ##                               m.#list_id = @list_id and
@@ -564,18 +570,21 @@ get_client(cl, client_type, client_id)
         sq_save_unique_data(sq, sublist);
 ##  }
 
-    if (child) return;
+    if (child) return(0);
 
     /* at top-level, check sub-lists for client (breadth-first search) */
     while (sq_get_data(sq, &sublist)) {
-       exists = find_member(sublist, member_type, member_id, sq);
+       exists = find_member(list_type, sublist, member_type, member_id, sq);
        if (exists) {
            sq_destroy(sq);
            return(1);
        }
     }
+    sq_destroy(sq);
+    return(0);
 ##}
 
+
 do_retrieve(q, pqual, psort, action, actarg)
     register struct query *q;
     char *pqual;
@@ -589,6 +598,22 @@ do_retrieve(q, pqual, psort, action, actarg)
 ##  char *csort;
 ##  int rowcount;
 ##  int errorno;
+    static char **vaddrs = (char **)NULL;
+
+    if (!vaddrs) {
+       register int i;
+
+       if ((vaddrs = (char **)malloc(sizeof(char *) * QMAXARGS)) == NULL) {
+           com_err(whoami, SMS_NO_MEM, "setting up static argv");
+           exit(1);
+       }
+       for (i = 0; i < QMAXARGS; i++) {
+           if ((vaddrs[i] = malloc(QMAXARGSIZE)) == NULL) {
+               com_err(whoami, SMS_NO_MEM, "setting up static argv");
+               exit(1);
+           }
+       }
+    }
 
     if (q->rvar) {
        rvar = q->rvar;
@@ -600,30 +625,30 @@ do_retrieve(q, pqual, psort, action, actarg)
        csort = psort;
        if (pqual) {
            cqual = pqual;
-##          retrieve unique (param (q->tlist, q->vaddr)) where cqual
+##          retrieve unique (param (q->tlist, vaddrs)) where cqual
 ##                   sort by csort
 ##          {
-                (*action)(q->vcnt, q->vaddr, actarg);
+                (*action)(q->vcnt, vaddrs, actarg);
 ##          }
        } else {
-##          retrieve unique (param (q->tlist, q->vaddr))
+##          retrieve unique (param (q->tlist, vaddrs))
 ##                   sort by csort
 ##          {
-                (*action)(q->vcnt, q->vaddr, actarg);
+                (*action)(q->vcnt, vaddrs, actarg);
 ##          }
        }
 
     } else {
        if (pqual) {
            cqual = pqual;
-##          retrieve unique (param (q->tlist, q->vaddr)) where cqual
+##          retrieve unique (param (q->tlist, vaddrs)) where cqual
 ##          {
-                (*action)(q->vcnt, q->vaddr, actarg);
+                (*action)(q->vcnt, vaddrs, actarg);
 ##          }
        } else {
-##          retrieve unique (param (q->tlist, q->vaddr))
+##          retrieve unique (param (q->tlist, vaddrs))
 ##          {
-                (*action)(q->vcnt, q->vaddr, actarg);
+                (*action)(q->vcnt, vaddrs, actarg);
 ##          }
        }
     }
@@ -643,7 +668,6 @@ do_update(q, argv, qual, action, actarg)
 ##  char *rvar;
 ##  char *rtable;
 ##  char *cqual;
-##  int rowcount;
 ##  int errorno;
       
     rvar = q->rvar;
@@ -655,7 +679,10 @@ do_update(q, argv, qual, action, actarg)
 ##  where cqual
 
 ##  inquire_equel (errorno = "errorno")
-    if (errorno != 0) return(SMS_INGRES_ERR);
+    if (errorno == INGRES_BAD_INT)
+       return(SMS_INTEGER);
+    else if (errorno != 0)
+       return(SMS_INGRES_ERR);
     return(SMS_SUCCESS);
 ##}
 
@@ -683,7 +710,10 @@ do_append(q, argv, pqual, action, actarg)
     }
 
 ##  inquire_equel (errorno = "errorno")
-    if (errorno != 0) return(SMS_INGRES_ERR);
+    if (errorno == INGRES_BAD_INT)
+       return(SMS_INTEGER);
+    else if (errorno != 0)
+       return(SMS_INGRES_ERR);
     return(SMS_SUCCESS);
 ##}
 
@@ -711,6 +741,78 @@ do_delete(q, qual, action, actarg)
 ##}
 
 
+/**
+ ** set_next_object_id - set next object id in values table
+ **
+ ** Inputs: object - object name in values table and in objects
+ **        table - name of table objects are found in
+ **
+ ** - called before an APPEND operation to set the next object id to
+ **   be used for the new record to the next free value
+ **
+ **/
+
+set_next_object_id(object, table)
+    char *object;
+    char *table;
+##{
+##  char *name, *tbl;
+##  int rowcount, exists, value;
+
+    name = object;
+    tbl = table;
+##  range of v is values
+##  repeat retrieve (value = v.#value) where v.#name = @name
+##  inquire_equel(rowcount = "rowcount")
+    if (rowcount != 1)
+       return(SMS_NO_ID);
+
+##  retrieve (exists = any(tbl.name where tbl.name = value))
+##  inquire_equel(rowcount = "rowcount")
+    if (rowcount != 1)
+       return(SMS_NO_ID);
+    while (exists) {
+       value++;
+       if (value > MAX_ID_VALUE)
+           value = MIN_ID_VALUE;
+##     retrieve (exists = any(tbl.name where tbl.name = value))
+    }
+
+    if (LOG_RES)
+        com_err(whoami, 0, "setting ID %s to %d", name, value);
+##  repeat replace v (#value = @value) where v.#name = @name
+    if (ingres_errno != 0) return(ingres_errno);
+    return(SMS_SUCCESS);
+##}
+
+
+/* For now this just checks the argc's.  It should also see that there
+ * are no duplicate names.
+ */
+
+sanity_check_queries()
+{
+    register int i;
+    int maxv = 0, maxa = 0;
+    extern int QueryCount1, QueryCount2;
+    extern struct query Queries1[], Queries2[];
+#define MAX(x,y) ((x) > (y) ? (x) : (y))
+
+    for (i = 0; i < QueryCount1; i++) {
+       maxv = MAX(maxv, Queries1[i].vcnt);
+       maxa = MAX(maxa, Queries1[i].argc);
+    }
+    for (i = 0; i < QueryCount2; i++) {
+       maxv = MAX(maxv, Queries2[i].vcnt);
+       maxa = MAX(maxa, Queries2[i].argc);
+    }
+    if (MAX(maxv, maxa) > QMAXARGS) {
+       com_err(whoami, 0, "A query has more args than QMAXARGS");
+       exit(1);
+    }
+}
+
+
 /*
  * Local Variables:
  * mode: c
This page took 0.10776 seconds and 4 git commands to generate.