]> andersk Git - moira.git/blobdiff - server/qsupport.dc
put the name of the database in a variable
[moira.git] / server / qsupport.dc
index 08d5cc5d886125b4d66c07c3da8ad1f7e2f78993..cf85811fe3dd9eb4bc5a61f80ae266c7ee48d963 100644 (file)
@@ -29,8 +29,6 @@ extern int ingres_errno, mr_errcode;
 
 EXEC SQL BEGIN DECLARE SECTION;
 extern char stmt_buf[];
-int idummy;
-extern char cdummy[];           /** Routines which need these vars should use mr_select_any() **/
 EXEC SQL END DECLARE SECTION;
 
 /* Specialized Access Routines */
@@ -72,12 +70,12 @@ access_login(q, argv, cl)
     EXEC SQL END DECLARE SECTION;
 
     build_qual(q->qual, q->argc, argv, qual);
-    if(!strcmp(q->shortname,"guau")) {
-      EXEC SQL SELECT users_id INTO :id FROM users u, strings str WHERE :qual;
+    if (!strncmp(q->name,"get_user_account",strlen("get_user_account"))) {
+       EXEC SQL SELECT users_id INTO :id FROM users u, strings str WHERE :qual;
     } else {
-      EXEC SQL SELECT users_id INTO :id FROM users WHERE :qual;
+       EXEC SQL SELECT users_id INTO :id FROM users u WHERE :qual;
     }
-    
+
     if (sqlca.sqlerrd[2] != 1 || id != cl->users_id)
        return(MR_PERM);
     else
@@ -398,7 +396,7 @@ int setup_dusr(q, argv)
     char **argv;
 {
     EXEC SQL BEGIN DECLARE SECTION;
-    int flag, id;
+    int flag, id, cnt;
     EXEC SQL END DECLARE SECTION;
 
     id = *(int *)argv[0];
@@ -409,26 +407,27 @@ int setup_dusr(q, argv)
     if (flag != 0 && flag != 4)
       return(MR_IN_USE);
 
-    EXEC SQL REPEATED DELETE FROM quota WHERE entity_id = :id AND type = 'USER';
+    EXEC SQL REPEATED DELETE FROM quota WHERE entity_id = :id AND type='USER';
     EXEC SQL REPEATED DELETE FROM krbmap WHERE users_id = :id;
-    EXEC SQL REPEATED SELECT member_id INTO :idummy FROM imembers
+    EXEC SQL REPEATED SELECT COUNT(member_id) INTO :cnt FROM imembers
       WHERE member_id = :id AND member_type = 'USER';
-    if (sqlca.sqlerrd[2] > 0)
+    if (cnt > 0)
        return(MR_IN_USE);
-    EXEC SQL REPEATED SELECT label INTO :cdummy FROM filesys WHERE owner = :id;
-    if (sqlca.sqlerrd[2]> 0)
+    EXEC SQL REPEATED SELECT COUNT(label) INTO :cnt FROM filesys 
+       WHERE owner = :id;
+    if (cnt > 0)
        return(MR_IN_USE);
-    EXEC SQL REPEATED SELECT name INTO :cdummy FROM list
+    EXEC SQL REPEATED SELECT COUNT(name) INTO :cnt FROM list
       WHERE acl_id = :id AND acl_type = 'USER';
-    if (sqlca.sqlerrd[2] > 0)
+    if (cnt > 0)
        return(MR_IN_USE);
-    EXEC SQL REPEATED SELECT name INTO :cdummy FROM servers
+    EXEC SQL REPEATED SELECT COUNT(name) INTO :cnt FROM servers
       WHERE acl_id = :id AND acl_type = 'USER';
-    if (sqlca.sqlerrd[2] > 0)
+    if (cnt > 0)
        return(MR_IN_USE);
-    EXEC SQL REPEATED SELECT acl_id INTO :idummy FROM hostaccess
+    EXEC SQL REPEATED SELECT COUNT(acl_id) INTO :cnt FROM hostaccess
       WHERE acl_id = :id AND acl_type = 'USER';
-    if (sqlca.sqlerrd[2] > 0)
+    if (cnt > 0)
        return(MR_IN_USE);
     if (ingres_errno)
        return(mr_errcode);
@@ -494,37 +493,37 @@ int setup_dmac(q, argv)
     char **argv;
 {
     EXEC SQL BEGIN DECLARE SECTION;
-    int flag, id;
+    int flag, id, cnt;
     EXEC SQL END DECLARE SECTION;
 
     id = *(int *)argv[0];
-    EXEC SQL REPEATED SELECT login INTO :cdummy FROM users
+    EXEC SQL REPEATED SELECT COUNT(login) INTO :cnt FROM users
       WHERE potype='POP' AND pop_id = :id;
-    if (sqlca.sqlerrd[2] > 0)
+    if (cnt > 0)
        return(MR_IN_USE);
-    EXEC SQL REPEATED SELECT mach_id INTO :idummy FROM serverhosts
+    EXEC SQL REPEATED SELECT COUNT(mach_id) INTO :cnt FROM serverhosts
       WHERE mach_id = :id;
-    if (sqlca.sqlerrd[2] > 0)
+    if (cnt > 0)
        return(MR_IN_USE);
-    EXEC SQL REPEATED SELECT mach_id INTO :idummy FROM nfsphys
+    EXEC SQL REPEATED SELECT COUNT(mach_id) INTO :cnt FROM nfsphys
       WHERE mach_id = :id;
-    if (sqlca.sqlerrd[2] > 0)
+    if (cnt > 0)
        return(MR_IN_USE);
-    EXEC SQL REPEATED SELECT mach_id INTO :idummy FROM hostaccess
+    EXEC SQL REPEATED SELECT COUNT(mach_id) INTO :cnt FROM hostaccess
       WHERE mach_id = :id;
-    if (sqlca.sqlerrd[2] > 0)
+    if (cnt > 0)
        return(MR_IN_USE);
-    EXEC SQL REPEATED SELECT mach_id INTO :idummy FROM printcap
+    EXEC SQL REPEATED SELECT COUNT(mach_id) INTO :cnt FROM printcap
       WHERE mach_id = :id;
-    if (sqlca.sqlerrd[2] > 0)
+    if (cnt > 0)
        return(MR_IN_USE);
-    EXEC SQL REPEATED SELECT quotaserver INTO :idummy FROM printcap
+    EXEC SQL REPEATED SELECT COUNT(quotaserver) INTO :cnt FROM printcap
       WHERE quotaserver = :id;
-    if (sqlca.sqlerrd[2] > 0)
+    if (cnt > 0)
        return(MR_IN_USE);
-    EXEC SQL REPEATED SELECT mach_id INTO :idummy FROM palladium
+    EXEC SQL REPEATED SELECT COUNT(mach_id) INTO :cnt FROM palladium
       WHERE mach_id = :id;
-    if (sqlca.sqlerrd[2] > 0)
+    if (cnt > 0)
        return(MR_IN_USE);
 
     EXEC SQL REPEATED DELETE FROM mcmap WHERE mach_id = :id;
@@ -542,17 +541,17 @@ int setup_dclu(q, argv)
     char **argv;
 {
     EXEC SQL BEGIN DECLARE SECTION;
-    int id;
+    int id, cnt;
     EXEC SQL END DECLARE SECTION;
 
     id = *(int *)argv[0];
-    EXEC SQL REPEATED SELECT mach_id INTO :idummy FROM mcmap
+    EXEC SQL REPEATED SELECT COUNT(mach_id) INTO :cnt FROM mcmap
       WHERE clu_id = :id;
-    if (sqlca.sqlerrd[2] > 0)
+    if (cnt > 0)
        return(MR_IN_USE);
-    EXEC SQL REPEATED SELECT clu_id INTO :idummy FROM svc
+    EXEC SQL REPEATED SELECT COUNT(clu_id) INTO :cnt FROM svc
       WHERE clu_id = :id;
-    if (sqlca.sqlerrd[2] > 0)
+    if (cnt > 0)
        return(MR_IN_USE);
     if (ingres_errno)
        return(mr_errcode);
@@ -700,17 +699,26 @@ int setup_dsin(q, argv)
     struct query *q;
     char **argv;
 {
+    EXEC SQL BEGIN DECLARE SECTION;     
     int ec;
+    char *svrname;
+    EXEC SQL END DECLARE SECTION;
 
     sprintf(stmt_buf,"SELECT service FROM serverhosts WHERE service = UPPERCASE('%s')",argv[0]);
     if(ec=mr_select_any(stmt_buf)) {
-       if(ec==MR_EXISTS) return(MR_IN_USE); else return(ec);
+       if(ec==MR_EXISTS) 
+           return(MR_IN_USE); 
+       else 
+           return(ec);
     }
 
-    sprintf(stmt_buf,"SELECT inprogress FROM servers WHERE name = UPPERCASE('%s')",argv[0]);
-    if(ec=mr_select_any(stmt_buf)) {
-       if(ec==MR_EXISTS) return(MR_IN_USE); else return(ec);
-    }
+    svrname=argv[0];
+    EXEC SQL SELECT inprogress INTO :ec FROM servers 
+      WHERE name=UPPERCASE(:svrname);
+    if(ingres_errno) 
+       return(mr_errcode);
+    if(ec) 
+       return(MR_IN_USE); 
 
     return(MR_SUCCESS);
 }
@@ -726,15 +734,19 @@ int setup_dshi(q, argv)
 {
     EXEC SQL BEGIN DECLARE SECTION;
     int id, ec;
+    char *svrname;
     EXEC SQL END DECLARE SECTION;
 
+    svrname=argv[0];
     id = *(int *)argv[1];
 
-    sprintf(stmt_buf,"SELECT inprogress FROM serverhosts \
-WHERE service = UPPERCASE('%s') AND mach_id = %d",argv[0],id);
-    if(ec=mr_select_any(stmt_buf)) {
-       if(ec==MR_EXISTS) return(MR_IN_USE); else return(ec);
-    }
+    EXEC SQL SELECT inprogress INTO :ec FROM serverhosts 
+      WHERE service=UPPERCASE(:svrname) AND mach_id = :id;
+    if(ingres_errno) 
+       return(mr_errcode);
+    if(ec) 
+       return(MR_IN_USE); 
+
 
     return(MR_SUCCESS);
 }
@@ -789,17 +801,17 @@ setup_afil(q, argv, cl)
     var_phys_id = 0;
 
     sprintf(ftype, "fs_access_%s", type);
-    EXEC SQL SELECT trans INTO :cdummy FROM alias
-      WHERE name = :ftype AND type = 'TYPE' and trans = :access;   /** Use mr_select_any() */
+    EXEC SQL SELECT COUNT(trans) INTO :ok FROM alias
+       WHERE name = :ftype AND type = 'TYPE' and trans = :access;   
     if (ingres_errno) return(mr_errcode);
-    if (sqlca.sqlerrd[2] == 0) return(MR_FILESYS_ACCESS);
-
-    if (!strcmp(type, "NFS"))
-       return (check_nfs(mach_id, name, access));
+    if (ok == 0) return(MR_FILESYS_ACCESS);
 
     if((mr_errcode=prefetch_value(q,argv,cl))!=MR_SUCCESS)
       return(mr_errcode);
 
+    if (!strcmp(type, "NFS"))
+       return (check_nfs(mach_id, name, access));
+
     return(MR_SUCCESS);
 }
 
@@ -817,9 +829,10 @@ setup_ufil(q, argv, cl)
     int mach_id, status;
     char *type, *name;
     EXEC SQL BEGIN DECLARE SECTION;
-    int fid, total, who;
+    int fid, total, who, ok;
     char *entity, ftype[32], *access;
     int var_phys_id = 0;
+    short int total_null;
     EXEC SQL END DECLARE SECTION;
 
     type = argv[2];
@@ -831,10 +844,15 @@ setup_ufil(q, argv, cl)
     entity = cl->entity;
 
     sprintf(ftype, "fs_access_%s", type);
-    EXEC SQL SELECT trans INTO :cdummy FROM alias
+    EXEC SQL SELECT COUNT(trans) INTO :ok FROM alias
       WHERE name = :ftype AND type='TYPE' AND trans = :access;
     if (ingres_errno) return(mr_errcode);
-    if (sqlca.sqlerrd[2] == 0) return(MR_FILESYS_ACCESS);
+    if (ok == 0) return(MR_FILESYS_ACCESS);
+
+    EXEC SQL SELECT type INTO :ftype FROM filesys
+      WHERE filsys_id = :fid;
+    strtrim(ftype);
+    if (ingres_errno) return(mr_errcode);
 
     if (!strcmp(type, "NFS")) {
        status = check_nfs(mach_id, name, access);
@@ -842,14 +860,14 @@ setup_ufil(q, argv, cl)
          WHERE filsys_id = :fid;
        if (ingres_errno) return(mr_errcode);
        return(status);
-    } else if (!strcmp(type, "AFS")) {
+    } else if (!strcmp(type, "AFS") && strcmp(ftype, "AFS")) {
        total = 0;
        EXEC SQL REPEATED DELETE FROM quota
          WHERE type = 'ANY' AND filsys_id = :fid;
-       EXEC SQL SELECT SUM (quota) INTO :total FROM quota
+       EXEC SQL SELECT SUM (quota) INTO :total:total_null FROM quota
          WHERE filsys_id = :fid AND phys_id != 0;
        if (ingres_errno) return(mr_errcode);
-       if (total != 0) {
+       if (!total_null && (total != 0)) {
 /*
  *             append quota (quota = total, filsys_id = fid,
  *                          phys_id = 0, entity_id = 0, type = "ANY",
@@ -927,18 +945,25 @@ setup_dfil(q, argv, cl)
     client *cl;
 {
     EXEC SQL BEGIN DECLARE SECTION;
-    int id, total;
+    int id, total, phys_id;
+    short int none;
     EXEC SQL END DECLARE SECTION;
 
     id = *(int *)argv[0];
-    EXEC SQL REPEATED SELECT SUM (quota) INTO :total FROM quota
+    EXEC SQL REPEATED SELECT SUM (quota) INTO :total:none FROM quota
+      WHERE filsys_id = :id;
+
+    if(none) total=0;
+
+    /** What if there are multiple phys_id's per f/s? (bad data) **/
+    EXEC SQL REPEATED SELECT phys_id INTO :phys_id FROM filesys
       WHERE filsys_id = :id;
     EXEC SQL REPEATED UPDATE nfsphys SET allocated = allocated - :total
-      WHERE nfsphys_id = filesys.phys_id AND filesys.filsys_id = :id;
-    /** Is SQL smart enough to do the PRODUCT above? */
-    /** Or should we code it using another SELECT?  */
+      WHERE nfsphys_id = :phys_id;
 
-    EXEC SQL REPEATED DELETE FROM quota WHERE filsys_id = :id;
+    if(!none) {
+       EXEC SQL REPEATED DELETE FROM quota WHERE filsys_id = :id;
+    }
     EXEC SQL REPEATED DELETE FROM fsgroup WHERE filsys_id = :id;
     EXEC SQL REPEATED DELETE FROM fsgroup WHERE group_id = :id;
     if (ingres_errno) return(mr_errcode);
@@ -956,16 +981,16 @@ setup_dnfp(q, argv, cl)
     client *cl;
 {
     EXEC SQL BEGIN DECLARE SECTION;
-    int id;
+    int id, cnt;
     char *dir;
     EXEC SQL END DECLARE SECTION;
 
     id = *(int *)argv[0];
     dir = argv[1];
-    EXEC SQL REPEATED SELECT label INTO :cdummy FROM filesys fs, nfsphys np
+    EXEC SQL REPEATED SELECT label INTO :cnt FROM filesys fs, nfsphys np
       WHERE fs.mach_id = :id AND fs.phys_id = np.nfsphys_id
        AND np.mach_id = :id AND np.dir = :dir;
-    if (sqlca.sqlerrd[2] > 0)
+    if (cnt > 0)
       return(MR_IN_USE);
     if (ingres_errno)
       return(mr_errcode);
@@ -1013,15 +1038,18 @@ setup_dqot(q, argv, cl)
 /* setup_sshi: don't exclusive lock the machine table during
  * set_server_host_internal.
  */
-
+/** Not allowed under (INGRES) SQL **/
 setup_sshi(q, argv, cl)
     struct query  *q;
     char **argv;
     client *cl;
 {
+#if 0
 #ifsql INGRES
     EXEC SQL set lockmode session where readlock = system;
 #endsql
+#endif
+    return(MR_SUCCESS);
 }
 
 
@@ -1397,12 +1425,16 @@ followup_guax(q, sq, v, action, actarg, cl)
     EXEC SQL BEGIN DECLARE SECTION; 
     int timestamp, who;
     char *login;
+    varchar struct { short data_size; char data_buf[257];} rsig;
     EXEC SQL END DECLARE SECTION; 
 #endif /* GDSS */
     int id, status;
 
     i = q->vcnt - 2;
     while (sq_get_data(sq, &argv)) {
+#ifdef DEBUG
+       com_err(whoami, 0, "argv[SIGNATURE] = \"%s\"", argv[U_SIGNATURE]);
+#endif /* DEBUG */
        id = atoi(argv[i]);
        if (id > 0)
          status = id_to_name(id, "USER", &argv[i]);
@@ -1413,9 +1445,9 @@ followup_guax(q, sq, v, action, actarg, cl)
 #ifdef GDSS
        if (q->vcnt == U_END && strlen(argv[U_SIGNATURE])) {
            login = argv[U_NAME];
-           EXEC SQL REPEATED SELECT sigdate, sigwho 
-             INTO :timestamp, :who FROM users
-             WHERE login = LEFT(:login,SIZE(login));
+           EXEC SQL REPEATED SELECT signature, sigdate, sigwho 
+             INTO :rsig, :timestamp, :who FROM users
+             WHERE login = :login;
            /** What about (INGRES) error handling? **/
            kname = malloc(1);
             status = id_to_name(who, "STRING", &kname);
@@ -1423,9 +1455,16 @@ followup_guax(q, sq, v, action, actarg, cl)
            si.SigInfoVersion = 0; /* XXXXX this isn't used */
            kname_parse(si.pname, si.pinst, si.prealm, kname);
             free(kname);
-            si.rawsig = (unsigned char *)argv[U_SIGNATURE];
+           rsig.data_buf[rsig.data_size] = 0;
+            si.rawsig = (unsigned char *)strsave(rsig.data_buf);
+           if (log_flags & LOG_GDSS)
+             com_err(whoami, 0, "rawsig length = %d, sig=\"%s\"", strlen(si.rawsig), si.rawsig);
            GDSS_Recompose(&si, sigbuf);
+           free(si.rawsig);
+           free(argv[U_SIGNATURE]);
            argv[U_SIGNATURE] = strsave(sigbuf);
+           if (log_flags & LOG_GDSS)
+             com_err(whoami, 0, "generated signature length %d", strlen(sigbuf));
        }
 #endif /* GDSS */
        (*action)(q->vcnt, argv, actarg);
@@ -1478,12 +1517,19 @@ followup_ausr(q, argv, cl)
 
 #ifdef GDSS
       if (q->vcnt == U_END && *argv[U_SIGNATURE]) {
+       /* unquote ' chars in signature */
+       for (dst = src = argv[U_SIGNATURE]; *src; ) {
+           if (*src == '\'')
+             src++;
+           *dst++ = *src++;
+       }
+       *dst = 0;
         sprintf(databuf, "%s:%s", argv[U_NAME], argv[U_MITID]);
         /* skip bytes for timestamp & kname */
         si.rawsig = (unsigned char *) rawsig;
         status = GDSS_Verify(databuf, strlen(databuf), argv[U_SIGNATURE], &si);
        if (strlen(rawsig) > mr_sig_length) {
-           com_err(whoami, 0, "GDSS signature too long.");  /** ??? **/
+           com_err(whoami, 0, "GDSS signature would be truncated.");  /** untested **/
            return(MR_INTERNAL);
        }
         if (status == 0) {
@@ -1495,13 +1541,16 @@ followup_ausr(q, argv, cl)
                 sigwho++;
                 EXEC SQL REPEATED UPDATE numvalues SET value = :sigwho
                  WHERE name='strings_id';
-                INSERT INTO strings (string_id, string) 
+                EXEC SQL INSERT INTO strings (string_id, string) 
                  VALUES (:sigwho, :name);
             } else if (status)
-              return(gdss2et(status));
+              return(status);
             timestamp = si.timestamp;
-        } else
-          return(gdss2et(status));
+        } else {
+           if (log_flags & LOG_GDSS)
+             hex_dump(argv[U_SIGNATURE]);
+           return(gdss2et(status));
+       }
       } else {
         rawsig[0] = 0;
         sigwho = 0;
@@ -1551,7 +1600,7 @@ followup_uuac(q, argv, cl)
 {
     EXEC SQL BEGIN DECLARE SECTION; 
     int who, status, id;
-    char *entity, *name;
+    char *entity, *name, *src, *dst;
     EXEC SQL END DECLARE SECTION; 
 #ifdef GDSS
     char databuf[32], *kname_unparse();
@@ -1569,15 +1618,28 @@ followup_uuac(q, argv, cl)
     
 #ifdef GDSS
     if (q->vcnt == U_MODTIME && *argv[U_SIGNATURE + 1]) {
+       /* unquote ' chars in signature */
+       for (dst = src = argv[U_SIGNATURE+1]; *src; ) {
+           if (*src == '\'')
+             src++;
+           *dst++ = *src++;
+       }
+       *dst = 0;
         login = malloc(1);
         status = id_to_name(id, "USER", &login);
         sprintf(databuf, "%s:%s", login, argv[U_MITID+1]);
         free(login);
         /* skip bytes for timestamp & kname */
         si.rawsig = (unsigned char *) rawsig;
+#ifdef DEBUG
+       com_err(whoami, 0, "verifying sig");
+#endif /* DEBUG */
         status = GDSS_Verify(databuf, strlen(databuf), argv[U_SIGNATURE+1], &si);
+#ifdef DEBUG
+       com_err(whoami, 0, "verified");
+#endif /* DEBUG */
        if (strlen(rawsig) > mr_sig_length) {
-           com_err(whoami, 0, "GDSS signature too long.");  /** ??? **/
+           com_err(whoami, 0, "GDSS signature would be truncated.");  /** untested **/
            return(MR_INTERNAL);
        }
         if (status == 0) {
@@ -1589,13 +1651,16 @@ followup_uuac(q, argv, cl)
                 sigwho++;
                 EXEC SQL REPEATED UPDATE numvalues SET value = :sigwho
                  WHERE name='strings_id';
-                INSERT INTO strings (string_id, string) 
+                EXEC SQL INSERT INTO strings (string_id, string) 
                  VALUES (:sigwho, :name);
             } else if (status)
-              return(gdss2et(status));
+              return(status);
             timestamp = si.timestamp;
-        } else
-          return(gdss2et(status));
+        } else {
+           if (log_flags & LOG_GDSS)
+             hex_dump(argv[U_SIGNATURE+1]);
+           return(gdss2et(status));
+       }
     } else {
         rawsig[0] = 0;
         sigwho = 0;
@@ -1849,27 +1914,71 @@ followup_aqot(q, argv, cl)
 {
     EXEC SQL BEGIN DECLARE SECTION;
     int quota, id, fs, who, physid;
-    char *entity, *qtype;
+    char *entity, *qtype, *table_name;
     EXEC SQL END DECLARE SECTION;
+    char incr_qual[60];
+    char *incr_argv[2];
+    int status;
 
+    table_name=q->rtable;
     fs = *(int *)argv[0];
-    if (!strcmp(q->name, "add_quota") || !strcmp(q->name, "update_quota")) {
+    EXEC SQL REPEATED SELECT phys_id INTO :physid FROM filesys
+      WHERE filsys_id = :fs;
+    if(ingres_errno)
+       return(mr_errcode);
+
+    if (!strcmp(q->shortname, "aqot") || !strcmp(q->shortname, "uqot")) {
        qtype = argv[1];
        id = *(int *)argv[2];
        quota = atoi(argv[3]);
+       sprintf(incr_qual,"q.filsys_id = %d",fs);
     } else {
        qtype = "USER";
        id = *(int *)argv[1];
        quota = atoi(argv[2]);
+       sprintf(incr_qual,"q.filsys_id=%d AND q.type='%s' AND q.entity_id=%d",
+               fs,qtype,id);
+    }
+
+    /* quota case of incremental_{before|after} only looks at slot 1 */
+    incr_argv[1]=qtype;
+
+    /* Follows one of many possible gross hacks to fix these particular
+     * conflicts between what is possible in the query table and what 
+     * is possible in SQL.    
+     */
+    if(q->type==APPEND) {
+       incremental_clear_before();
+       EXEC SQL INSERT INTO quota 
+           (filsys_id, type, entity_id, quota, phys_id) 
+         VALUES (:fs, :qtype, :id, :quota, :physid);
+       incremental_after(table_name, incr_qual, incr_argv);    
+    } else {
+       incremental_before(table_name, incr_qual, incr_argv);
+       EXEC SQL UPDATE quota SET quota = :quota
+         WHERE filsys_id = :fs AND type = :qtype AND entity_id = :id;
+       status = mr_errcode;
+       incremental_after(table_name, incr_qual, incr_argv);
+    }
+
+    if (ingres_errno)
+       return(mr_errcode);
+    flush_name(argv[0], q->rtable);  
+    if(q->type==APPEND) {
+        EXEC SQL UPDATE tblstats SET appends = appends + 1, modtime = 'now'
+         WHERE table_name = :table_name;
+    } else {
+        EXEC SQL UPDATE tblstats SET updates = updates + 1, modtime = 'now'
+         WHERE table_name = :table_name;
     }
+       
+    /* Proceed with original followup */
     who = cl->client_id;
     entity = cl->entity;
 
     EXEC SQL REPEATED UPDATE quota
       SET modtime = 'now', modby = :who, modwith = :entity
       WHERE filsys_id = :fs and type = :qtype and entity_id = :id;
-    EXEC SQL REPEATED SELECT phys_id INTO :physid FROM filesys
-      WHERE filsys_id = :fs;
     EXEC SQL REPEATED UPDATE nfsphys SET allocated = allocated + :quota
       WHERE nfsphys_id = :physid;
     if (ingres_errno) return(mr_errcode);
@@ -1877,6 +1986,51 @@ followup_aqot(q, argv, cl)
 }
 
 
+/* Necessitated by the requirement of a correlation name by the incremental
+ * routines, since query table deletes don't provide one. 
+ */
+followup_dqot(q,argv,cl)
+    struct query *q;
+    char **argv;
+    struct client *cl;
+{
+    char *qtype;
+    int id, fs;
+    char *incr_argv[2];
+    EXEC SQL BEGIN DECLARE SECTION; 
+    char incr_qual[80];
+    char *tblname;
+    EXEC SQL END DECLARE SECTION; 
+
+    fs = *(int *)argv[0];
+    if (!strcmp(q->shortname, "dqot")) {
+       qtype = argv[1];
+       id = *(int *)argv[2];
+    } else {
+       qtype = "USER";
+       id = *(int *)argv[1];
+    }
+    sprintf(incr_qual,"q.filsys_id=%d AND q.type='%s' AND q.entity_id=%d",
+           fs,qtype,id);
+
+    /* quota case of incremental_{before|after} only looks at slot 1 */
+    incr_argv[1]=qtype;
+
+    incremental_before(q->rtable, incr_qual, incr_argv);
+    EXEC SQL DELETE FROM quota q WHERE :incr_qual;
+    incremental_clear_after();
+
+    if (ingres_errno) 
+       return(mr_errcode);
+    flush_name(argv[0], q->rtable);
+
+    tblname = q->rtable;
+    EXEC SQL UPDATE tblstats SET deletes = deletes + 1, modtime = 'now'
+      WHERE table_name = :tblname;
+    return(MR_SUCCESS);
+}
+
+
 followup_gpce(q, sq, v, action, actarg, cl)
     struct query *q;
     register struct save_queue *sq;
@@ -2089,7 +2243,7 @@ int set_pobox(q, argv, cl)
 
     set_pobox_modtime(q, argv, cl);
     EXEC SQL REPEATED UPDATE tblstats SET updates = updates+1, modtime='now'
-      WHERE tblstats.table_name='users';
+      WHERE table_name='users';
     if (ingres_errno) return(mr_errcode);
     return(MR_SUCCESS);
 }
@@ -2113,6 +2267,7 @@ get_list_info(q, aargv, cl, action, actarg)
     char maillist[5], grouplist[5], gid_str[6], acl_name[256], desc[256];
     char modtime[27], modby[256], modwith[9];
     int id, rowcount, acl_id, hid, modby_id;
+    char qual[80];
     EXEC SQL END DECLARE SECTION;
     int returned, status;
     struct save_queue *sq, *sq_create();
@@ -2122,8 +2277,10 @@ get_list_info(q, aargv, cl, action, actarg)
     convert_wildcards(name);
 
     sq = sq_create();
+    sprintf(qual,"name LIKE '%s' ESCAPE '*'",name);
+    optimize_sql_stmt(qual);
     EXEC SQL DECLARE csr102 CURSOR FOR SELECT list_id FROM list
-      WHERE name LIKE :name ESCAPE '*';
+      WHERE :qual;
     EXEC SQL OPEN csr102;
     while(1)
     {
@@ -2209,7 +2366,7 @@ int add_member_to_list(q, argv, cl)
     client *cl;
 {
     EXEC SQL BEGIN DECLARE SECTION;
-    int id, lid, mid, error, who, ref;
+    int id, lid, mid, error, who, ref, rowcnt;
     char *mtype, dtype[9], *entity;
     EXEC SQL END DECLARE SECTION;
     int ancestors[MAXLISTDEPTH], aref[MAXLISTDEPTH], acount, a;
@@ -2222,10 +2379,10 @@ int add_member_to_list(q, argv, cl)
     mtype = argv[1];
     mid = *(int *)argv[2];
     /* if the member is already a direct member of the list, punt */
-    EXEC SQL REPEATED SELECT list_id INTO :idummy FROM imembers
+    EXEC SQL REPEATED SELECT COUNT(list_id) INTO :rowcnt FROM imembers
       WHERE list_id = :lid AND member_id = :mid
        AND member_type = :mtype AND direct = 1;
-    if (sqlca.sqlerrd[2] > 0)
+    if (rowcnt > 0)
       return(MR_EXISTS);
     if (!strcasecmp(mtype, "STRING")) {
        buf = malloc(0);
@@ -2308,19 +2465,20 @@ int add_member_to_list(q, argv, cl)
            if (mid == lid && !strcmp(mtype, "LIST")) {
                return(MR_LISTLOOP);
            }
-           EXEC SQL REPEATED SELECT ref_count INTO :idummy FROM imembers
+           EXEC SQL REPEATED SELECT COUNT(ref_count) INTO :rowcnt 
+               FROM imembers
              WHERE list_id = :lid AND member_id = :mid
                AND member_type = :mtype;
            ref = aref[a] * dref[d];
-           if (sqlca.sqlerrd[2] > 0) {
+           if (rowcnt > 0) {
                if (a == 0 && d == 0) {
                    EXEC SQL UPDATE imembers
-                     SET ref_count = ref_count+ref, direct=1
+                     SET ref_count = ref_count+:ref, direct=1
                      WHERE list_id = :lid AND member_id = :mid
                        AND member_type = :mtype;
                } else {
                    EXEC SQL UPDATE imembers
-                     SET ref_count = ref_count+ref
+                     SET ref_count = ref_count+:ref
                      WHERE list_id = :lid AND member_id = :mid
                        AND member_type = :mtype;
                }
@@ -2374,11 +2532,11 @@ int delete_member_from_list(q, argv, cl)
     mtype = argv[1];
     mid = *(int *)argv[2];
     /* if the member is not a direct member of the list, punt */
-    EXEC SQL REPEATED SELECT list_id INTO :idummy FROM imembers
+    EXEC SQL REPEATED SELECT COUNT(list_id) INTO :cnt FROM imembers
       WHERE list_id = :lid AND member_id = :mid
        AND member_type = :mtype AND direct = 1;
     if (ingres_errno) return(mr_errcode);
-    if (sqlca.sqlcode == 100)
+    if (cnt == 0)
       return(MR_NO_MATCH);
     ancestors[0] = lid;
     aref[0] = 1;
@@ -2461,12 +2619,12 @@ int delete_member_from_list(q, argv, cl)
                incremental_clear_after();
            } else if (a == 0 && d == 0) {
                EXEC SQL UPDATE imembers
-                 SET ref_count = refcount - :ref, direct = 0
+                 SET ref_count = ref_count - :ref, direct = 0
                  WHERE list_id = :lid AND member_id = :mid
                    AND member_type = :mtype;
            } else {
                EXEC SQL UPDATE imembers
-                 SET ref_count=refcount-:ref
+                 SET ref_count = ref_count - :ref
                  WHERE list_id = :lid AND member_id = :mid
                    AND member_type = :mtype;
            }
@@ -2668,9 +2826,9 @@ get_ace_internal(atype, aid, action, actarg)
 
     rargv[0] = "HOSTACCESS";
     EXEC SQL DECLARE csr115 CURSOR FOR
-      SELECT name FROM machine, hostaccess
-       WHERE mach_id = hostaccess.mach_id AND hostaccess.acl_type = :atype
-         AND hostaccess.acl_id = :aid;
+      SELECT name FROM machine m, hostaccess ha
+       WHERE m.mach_id = ha.mach_id AND ha.acl_type = :atype
+         AND ha.acl_id = :aid;
     EXEC SQL OPEN csr115;
     while(1) {
        EXEC SQL FETCH csr115 INTO :name;
@@ -2682,11 +2840,11 @@ get_ace_internal(atype, aid, action, actarg)
 
     rargv[0] = "ZEPHYR";
     EXEC SQL DECLARE csr116 CURSOR FOR
-      SELECT class FROM zephyr
-       WHERE zephyr.xmt_type = :atype AND zephyr.xmt_id = :aid
-         OR zephyr.sub_type = :atype AND zephyr.sub_id = :aid
-         OR zephyr.iws_type = :atype AND zephyr.iws_id = :aid
-          OR zephyr.iui_type = :atype AND zephyr.iui_id = :aid;
+      SELECT class FROM zephyr z
+       WHERE z.xmt_type = :atype AND z.xmt_id = :aid
+         OR z.sub_type = :atype AND z.sub_id = :aid
+         OR z.iws_type = :atype AND z.iws_id = :aid
+          OR z.iui_type = :atype AND z.iui_id = :aid;
     EXEC SQL OPEN csr116;
     while(1) {
        EXEC SQL FETCH csr116 INTO :name;
@@ -2751,11 +2909,11 @@ int get_lists_of_member(q, argv, cl, action, actarg)
     rargv[5] = grouplist;
     if (direct) {
        EXEC SQL DECLARE csr117a CURSOR FOR
-         SELECT name, CHAR(active), CHAR(publicflg), CHAR(hidden),
-             CHAR(maillist), CHAR(grouplist)
-           FROM list l, imembers m
-           WHERE l.list_id = m.list_id AND m.direct = 1
-             AND m.member_type = :atype AND m.member_id = :aid;
+         SELECT l.name, CHAR(l.active), CHAR(l.publicflg), CHAR(l.hidden),
+             CHAR(l.maillist), CHAR(l.grouplist)
+           FROM list l, imembers im
+           WHERE l.list_id = im.list_id AND im.direct = 1
+             AND im.member_type = :atype AND im.member_id = :aid;
        EXEC SQL OPEN csr117a;
        while(1) {
            EXEC SQL FETCH csr117a
@@ -2767,11 +2925,11 @@ int get_lists_of_member(q, argv, cl, action, actarg)
        EXEC SQL CLOSE csr117a;
     } else {
        EXEC SQL DECLARE csr117b CURSOR FOR
-         SELECT name, CHAR(active), CHAR(publicflg), CHAR(hidden),
-             CHAR(maillist), CHAR(grouplist)
-           FROM list l, imembers m
-           WHERE l.list_id = m.list_id
-             AND m.member_type = :atype AND m.member_id = :aid;
+         SELECT l.name, CHAR(l.active), CHAR(l.publicflg), CHAR(l.hidden),
+             CHAR(l.maillist), CHAR(l.grouplist)
+           FROM list l, imembers im
+           WHERE l.list_id = im.list_id
+             AND im.member_type = :atype AND im.member_id = :aid;
        EXEC SQL OPEN csr117b;
        while(1) {
            EXEC SQL FETCH csr117b
@@ -2795,7 +2953,7 @@ int get_lists_of_member(q, argv, cl, action, actarg)
  * where clause based on the arguments, then doing a retrieve.
  */
 
-static char *lflags[5] = { "active", "publicflg", "hidden", "maillist", "group" };
+static char *lflags[5] = { "active", "publicflg", "hidden", "maillist", "grouplist" };
 
 int qualified_get_lists(q, argv, cl, action, actarg)
     struct query *q;
@@ -2920,9 +3078,9 @@ gmol_internal(q, argv, cl, action, actarg, flag)
     targv[1] = member_name;
     targv[0] = "USER";
     EXEC SQL DECLARE csr119 CURSOR FOR
-      SELECT users.login FROM users, imembers
-       WHERE imembers.list_id = :list_id AND imembers.member_type = 'USER'
-         AND imembers.member_id = users.users_id AND imembers.direct > :direct
+      SELECT u.login FROM users u, imembers im
+       WHERE im.list_id = :list_id AND im.member_type = 'USER'
+         AND im.member_id = u.users_id AND im.direct > :direct
        ORDER BY 1;
     EXEC SQL OPEN csr119;
     while(1) {
@@ -2935,9 +3093,9 @@ gmol_internal(q, argv, cl, action, actarg, flag)
 
     targv[0] = "LIST";
     EXEC SQL DECLARE csr120 CURSOR FOR
-      SELECT list.name FROM list, imembers
-       WHERE imembers.list_id = :list_id AND imembers.member_type='LIST'
-         AND imembers.member_id = list.list_id AND imembers.direct > :direct
+      SELECT l.name FROM list l, imembers im
+       WHERE im.list_id = :list_id AND im.member_type='LIST'
+         AND im.member_id = l.list_id AND im.direct > :direct
         ORDER BY 1;
     EXEC SQL OPEN csr120;
     while(1) {
@@ -2950,10 +3108,9 @@ gmol_internal(q, argv, cl, action, actarg, flag)
 
     targv[0] = "STRING";
     EXEC SQL DECLARE csr121 CURSOR FOR
-      SELECT strings.string FROM strings, imembers
-       WHERE imembers.list_id = :list_id AND imembers.member_type='STRING'
-         AND imembers.member_id = strings.string_id 
-         AND imembers.direct > :direct
+      SELECT CHAR(str.string) FROM strings str, imembers im
+       WHERE im.list_id = :list_id AND im.member_type='STRING'
+         AND im.member_id = str.string_id AND im.direct > :direct
        ORDER BY 1;
     EXEC SQL OPEN csr121;
     while(1) {
@@ -2966,10 +3123,10 @@ gmol_internal(q, argv, cl, action, actarg, flag)
 
     targv[0] = "KERBEROS";
     EXEC SQL DECLARE csr122 CURSOR FOR
-      SELECT strings.string FROM strings, imembers
-       WHERE imembers.list_id = :list_id AND imembers.member_type='KERBEROS'
-         AND imembers.member_id = strings.string_id 
-         AND imembers.direct > :direct
+      SELECT CHAR(str.string) FROM strings str, imembers im
+       WHERE im.list_id = :list_id AND im.member_type='KERBEROS'
+         AND im.member_id = str.string_id 
+         AND im.direct > :direct
        ORDER BY 1;
     EXEC SQL OPEN csr122;
     while(1) {
@@ -3057,16 +3214,16 @@ int qualified_get(q, argv, action, actarg, start, range, field, flags)
     strcpy(qual, start);
     for (i = 0; i < q->argc; i++) {
        if (!strcmp(argv[i], "TRUE")) {
-           sprintf(buf, " and %s.%s != 0", range, flags[i]);
+           sprintf(buf, " AND %s.%s != 0", range, flags[i]);
            (void) strcat(qual, buf);
        } else if (!strcmp(argv[i], "FALSE")) {
-           sprintf(buf, " and %s.%s = 0", range, flags[i]);
+           sprintf(buf, " AND %s.%s = 0", range, flags[i]);
            (void) strcat(qual, buf);
        }
     }
 
     rargv[0] = SQLDA->sqlvar[0].sqldata;
-    sprintf(stmt_buf,"SELECT %s.%s FROM %s %s WHERE %s",q->rtable,field,q->rtable,q->rvar,qual);
+    sprintf(stmt_buf,"SELECT CHAR(%s.%s) FROM %s %s WHERE %s",range,field,q->rtable,range,qual);
     EXEC SQL PREPARE stmt INTO :SQLDA USING NAMES FROM :stmt_buf;
     if(sqlca.sqlcode)
       return(MR_INTERNAL);
@@ -3108,16 +3265,14 @@ int qualified_get_serverhost(q, argv, cl, action, actarg)
     char *rargv[2], buf[32];
     int i, rowcount;
 
-    /** the uppercase() function is INGRES-specific */
-    sprintf(qual, "machine.mach_id = serverhosts.mach_id AND \
-serverhosts.service = uppercase('%s')",
+    sprintf(qual, "m.mach_id = sh.mach_id AND sh.service = uppercase('%s')",
            argv[0]);
     for (i = 1; i < q->argc; i++) {
        if (!strcmp(argv[i], "TRUE")) {
-           sprintf(buf, " AND serverhosts.%s != 0", shflags[i]);
+           sprintf(buf, " AND sh.%s != 0", shflags[i]);
            strcat(qual, buf);
        } else if (!strcmp(argv[i], "FALSE")) {
-           sprintf(buf, " AND serverhosts.%s = 0", shflags[i]);
+           sprintf(buf, " AND sh.%s = 0", shflags[i]);
            strcat(qual, buf);
        }
     }
@@ -3125,7 +3280,7 @@ serverhosts.service = uppercase('%s')",
     rargv[0] = sname;
     rargv[1] = mname;
     EXEC SQL DECLARE csr124 CURSOR FOR
-      SELECT serverhosts.service, machine.name FROM serverhosts, machine
+      SELECT sh.service, m.name FROM serverhosts sh, machine m
        WHERE :qual;
     EXEC SQL OPEN csr124;
     while(1) {
@@ -3181,18 +3336,18 @@ register_user(q, argv, cl)
       return(MR_NOT_UNIQUE);
 
     /* check new login name */
-    EXEC SQL REPEATED SELECT login INTO :cdummy FROM users
+    EXEC SQL REPEATED SELECT COUNT(login) INTO :rowcount FROM users
       WHERE login = LEFT(:login,SIZE(login)) AND users_id != :users_id;
     if (ingres_errno) return(mr_errcode);
-    if (sqlca.sqlerrd[2] > 0) return(MR_IN_USE);
-    EXEC SQL REPEATED SELECT name INTO :cdummy FROM list
+    if (rowcount > 0) return(MR_IN_USE);
+    EXEC SQL REPEATED SELECT COUNT(name) INTO :rowcount FROM list
       WHERE name = LEFT(:login,SIZE(name));
     if (ingres_errno) return(mr_errcode);
-    if (sqlca.sqlerrd[2] > 0) return(MR_IN_USE);
-    EXEC SQL REPEATED SELECT label INTO :cdummy FROM filesys
+    if (rowcount > 0) return(MR_IN_USE);
+    EXEC SQL REPEATED SELECT COUNT(label) INTO :rowcount FROM filesys
       WHERE label = LEFT(:login,SIZE(label));
     if (ingres_errno) return(mr_errcode);
-    if (sqlca.sqlerrd[2] > 0) return(MR_IN_USE);
+    if (rowcount > 0) return(MR_IN_USE);
     com_err(whoami, 0, "login name OK");
 
     /* choose place for pobox, put in mid */
@@ -3214,7 +3369,7 @@ register_user(q, argv, cl)
     }
 
     /* change login name, set pobox */
-    sprintf(buffer, "users.users_id = %d", users_id);
+    sprintf(buffer, "u.users_id = %d", users_id);
     incremental_before("users", buffer, 0);
     nstatus = 2;
     if (ostatus == 5 || ostatus == 6)
@@ -3254,7 +3409,7 @@ register_user(q, argv, cl)
     if (ingres_errno) return(mr_errcode);
     if (sqlca.sqlerrd[2] != 1)
       return(MR_INTERNAL);
-    sprintf(buffer, "list_id = %d", list_id);
+    sprintf(buffer, "l.list_id = %d", list_id);
     incremental_after("list", buffer, 0);
     aargv[0] = (char *) list_id;
     aargv[1] = "USER";
@@ -3305,11 +3460,12 @@ register_user(q, argv, cl)
 
     /* set quota */
     if (def_quota == 0) {
-       EXEC SQL REPEATED SELECT value INTO :quota FROM numvalues
+       EXEC SQL REPEATED SELECT value INTO :def_quota FROM numvalues
          WHERE name='def_quota';
        if (ingres_errno) return(mr_errcode);
        if (sqlca.sqlerrd[2] != 1)
          return(MR_NO_QUOTA);
+       
     }
     incremental_clear_before();
     EXEC SQL REPEATED INSERT INTO quota
@@ -3363,6 +3519,22 @@ static int set_pop_usage(id, cnt)
     return(MR_SUCCESS);
 }
 
+int _sdl_followup(q, argv, cl)
+    struct query *q;
+    char **argv;
+    client *cl;
+{
+    int i;
+    i = atoi(argv[0]);
+    log_flags = i;
+    if (i & LOG_SQL) {
+       EXEC SQL set printqry;
+    } else {
+       EXEC SQL set noprintqry;
+    }
+    return(MR_SUCCESS);
+}
+
 
 \f
 /* Validation Routines */
@@ -3553,7 +3725,7 @@ validate_id(q, argv, vo)
            EXEC SQL SELECT value INTO :id FROM numvalues
              WHERE name = 'strings_id';
            id++;
-           EXEC SQL UPDATE numvalues SET value = :id WHERE name = 'string_id';
+           EXEC SQL UPDATE numvalues SET value = :id WHERE name = 'strings_id';
            EXEC SQL INSERT INTO strings (string_id, string) VALUES (:id, :name);
            cache_entry(name, "STRING", id);
            *(int *)argv[vo->index] = id;
@@ -3698,6 +3870,7 @@ validate_type(argv, vo)
     EXEC SQL BEGIN DECLARE SECTION;
     char *typename;
     char *val;
+    int cnt;
     EXEC SQL END DECLARE SECTION;
     register char *c;
 
@@ -3711,10 +3884,10 @@ validate_type(argv, vo)
     /* uppercase type fields */
     for (c = val; *c; c++) if (islower(*c)) *c = toupper(*c);
 
-    EXEC SQL SELECT trans INTO :cdummy FROM alias
+    EXEC SQL SELECT COUNT(trans) INTO :cnt FROM alias
       WHERE name = :typename AND type='TYPE' AND trans = :val;
     if (ingres_errno) return(mr_errcode);
-    return (sqlca.sqlerrd[2] ? MR_EXISTS : vo->error);
+    return (cnt ? MR_EXISTS : vo->error);
 }
 
 /* validate member or type-specific data field */
@@ -3960,8 +4133,8 @@ prefetch_filesys(q,argv,cl)
     if(ingres_errno) return(mr_errcode);
 
     argc=q->argc+q->vcnt;
-    sprintf(argv[argc++],"%d",fid);
-    sprintf(argv[argc],"%d",phid);
+    sprintf(argv[argc++],"%d",phid);
+    sprintf(argv[argc],"%d",fid);
 
     return(MR_SUCCESS);
 }
@@ -4049,4 +4222,51 @@ mr_select_any(stmt)
 } 
 
 
+
+static hex_dump(p)
+unsigned  char *p;
+{
+    char buf[BUFSIZ];
+    int i;
+
+    fprintf(stderr, "Size: %d\n", strlen(p));
+    while (strlen(p) >= 8) {
+       fprintf(stderr, "%02x %02x %02x %02x %02x %02x %02x %02x\n",
+               p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7]);
+       p += 8;
+    }
+    switch (strlen(p)) {
+    case 7:
+       fprintf(stderr, "%02x %02x %02x %02x %02x %02x %02x\n",
+               p[0], p[1], p[2], p[3], p[4], p[5], p[6]);
+       break;
+    case 6:
+       fprintf(stderr, "%02x %02x %02x %02x %02x %02x\n",
+               p[0], p[1], p[2], p[3], p[4], p[5]);
+       break;
+    case 5:
+       fprintf(stderr, "%02x %02x %02x %02x %02x\n",
+               p[0], p[1], p[2], p[3], p[4]);
+       break;
+    case 4:
+       fprintf(stderr, "%02x %02x %02x %02x\n",
+               p[0], p[1], p[2], p[3]);
+       break;
+    case 3:
+       fprintf(stderr, "%02x %02x %02x\n",
+               p[0], p[1], p[2]);
+       break;
+    case 2:
+       fprintf(stderr, "%02x %02x\n",
+               p[0], p[1]);
+       break;
+    case 1:
+       fprintf(stderr, "%02x\n",
+               p[0]);
+       break;
+    default:
+       return;
+    }
+}
+
 /* eof:qsupport.dc */
This page took 0.088973 seconds and 4 git commands to generate.